From gallium.arsenide at gmail.com Wed Apr 1 00:00:30 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Tue, 31 Mar 2009 21:00:30 -0700 (PDT) Subject: Python Goes Mercurial References: Message-ID: <35d429fa-5d13-4703-a443-6a95c740c9fb@o6g2000yql.googlegroups.com> Lawrence D'Oliveiro and andrew cooke exchanged: > >>> So what were these "strong antipathies" towards Git, exactly? > > >> compared to what i've read on dev > >> "strong antipathies" sounds a bit over-hyped. > > > That was the phrase used by GvR. > > well if you find any, please do report back. Andrew, perhaps you stopped reading too early. Here's one that clearly expresses strong antipathy: http://mail.python.org/pipermail/python-dev/2009-March/087971.html (When challenged, he later goes on to claim "Internet hyperbole", which I'll buy.) John From jfager at gmail.com Wed Apr 1 00:15:19 2009 From: jfager at gmail.com (jfager) Date: Tue, 31 Mar 2009 21:15:19 -0700 (PDT) Subject: Thoughts on language-level configuration support? References: <36148830-22c0-4f19-ab23-d04d8755ab4a@s28g2000vbp.googlegroups.com> <01e0d6e4$0$19301$c3e8da3@news.astraweb.com> <62a06f4c-82e3-4a32-b6e2-ad69ec6f4ac0@f32g2000vbf.googlegroups.com> Message-ID: On Mar 31, 10:44?pm, "Rhodri James" wrote: > On Tue, 31 Mar 2009 07:06:50 +0100, jfager wrote: > > On Mar 30, 9:31?pm, "Rhodri James" > > wrote: > >> On Mon, 30 Mar 2009 16:59:12 +0100, jfager wrote: > >> > It's the configuration problem. ?Right now you would use something > >> > like ConfigParser or optparse to populate some configuration object, > >> > which you would then pass around and extract values from. ?This would > >> > provide two advantages over these approaches: ?it would separate "what > >> > can be configured" from "the mechanism by which it is configured" - > >> > i.e., I, programmer, don't have to make a decision about what the end > >> > user has to do to give me those values. > > >> This would be a interesting idea, but ultimately no more than a veneer > >> over the current set of configuration possibilities. ?Quite how such a > >> system would tell whether to get configuration data from command line > >> parameters, a config file somewhere, or just pull bytes off the Z-wave > >> ?from Mars, I'm not at all clear. > > > Not a veneer over; a foundation under. ?As I acknowledged in my blog > > post, this would require a small bit of bootstrapping, to configure > > which end-user configuration system was used. ?But this would simply > > point to an adapter, that would map from the desired configuration > > interface into the canonical standard api provided by the language > > system. > > By magic: see later > > > The problem with the current set of configuration possibilities is > > that there's nothing really constant between them, unless the > > programmer explicitly codes it, even though they're basically > > accomplishing the same thing. ?There's nothing amazingly special about > > this proposal, it's just saying: ?that basic thing that we do in a lot > > of different ways, let's make as much of that as possible standard. ?I > > think that cutoff point is below the end-user interface, since there > > are a lot of valid ways to want to actually inject config data > > (command line, local files, from a db, etc.), but there's no reason > > that the end-user interfaces can't rest on top of a lower-level common > > format that the programmer can rely on. > > There is actually. ?The reason is that those different configuration > interfaces are actually accomplishing different things. ?You're lumping > together both config files (wherever they may be and whatever format > they may be in) with command line parameters and GUI command interfaces > when the one is providing long-term defaults and the other short-term > overrides. > > The level of detail that you can have in a config file is something that > you never want to see on a command line. ?When you do, as with say "gcc", > the results are rather mind-numbing. ?This distinction is something that > your reduction of configuration to a single black box fails to capture. The fact that you 'can' expose something through a particular interface doesn't mean you 'have to'. If command line args don't make sense for a particular program, nobody is would be forced to use them. That's the point - the interface the end user works with is flexible, it's what they want to deal with. > >> > ?And it would allow the > >> > configurable surface of the program to be discoverable; I wouldn't > >> > have to poke through code or documentation that was maintained > >> > separate from the source code. > > >> Except that you still do. ? > > > No, you don't. ?If you do, then this proposal hasn't been implemented > > (and the little code snippet I provided in my post, please don't > > consider that a full implementation, it was just a sketch I threw > > together in a really short time). ?I'm not saying discoverability > > magically arises; I'm saying that it's one the main points that would > > be deliberately put into place. > > Yes you do. ?This spread-about inline style of configuration is in no > sense discoverable to the casual reader, who isn't going to know that > a particular config option even exists unless he or she chances upon > the line where it's registered. ? "Discoverable", as in built-in tools that let you have the following conversation: "Program, tell me all the things I can configure about you" - "Okay, here they all are". No digging through the source required. > I'm not commenting on your code > snippet here -- or I'd be here all night, and you really wouldn't > like me at the end of it -- :) Dude, that was all of 15 minutes of coding (not counting the diversions spent poking around the bytecode tools for the first time). > but I am commenting on the usage style > it's obviously built to support. ?That usage style is much less > visible than any of the common (or uncommon, for that matter) styles > of getting config input, and discourages documentation habits that > would otherwise mitigate it. > > >> You've just specified a different way in > >> which you have to do this, one that's a good deal less visible in > >> the code > > > Why would it be less visible? ?If it's syntax, you would know exactly > > where it was just by looking. ?I would argue that it's more clear - no > > more boilerplate code spent shuffling around config objects that you > > peel values out of, just write the code that does the work you're > > actually trying to accomplish, with a small bit of syntax to mark that > > the end user can provide an alternate value. > > As I said, it's less visible because it's decentralised. ?The moment > you do that, you lose all hope of centralising the documentation, and > the visibility of your options plummets. The tools provided by the language do the centralization for you, that was one of the main points. > > >> and a lot less self-documenting because it's spread out all > >> over the place. ? > > > Actually, you get the best of both worlds. ?You get to see clearly in > > the code how the configured values are actually used, and you get the > > gathered summary of configurable values from the "discoverability" > > implementation. > > What "discoverability"? ?You still haven't told me anything to make > me believe that this configuration setup is discoverable at all without > external tools. The whole point is that the tools for this would be provided by the language system. > >> It also has the major disadvantage from my point of > >> view of requiring Python to do magic in the background to figure out > >> just what is being configured. > > > If it's built into the language, it stops being magic, and turns into > > "how it works". > > It remains magic when it requires the language to invent things on the > fly that the programmer has no means of discovering until the code is > running, by which time it's generally too late. ?For example: > > ? ?spam = conf(1) > > How does that translate into a command-line option? ?-s? ?--span? > Both? ?-S, even? ?--first-config-option-I-came-across? ?If the first, > what happens when: > > ? ?span = conf(0) > > shows up to initialise a class attribute some time later? ?What > restrictions can be put on the value you get back? ?What can the > help system say about this, or do we have to go back to doing all > that by hand? ?Now translate all those questions into the very > different environment of a config file. ?Repeat with a database, > and all it's quirks. ?By the time your colossus has acquired > enough parameters to at least hint at the desirable answers to > these questions, you've effectively duplicated the interfaces to > all of the config mechanisms you're trying to replace and you've > still lost a whole heap of flexibility. Yes, you're right, the code that actually injects the configuration isn't trivial. I never intended to imply that it was. But it would probably only have to be written once (people would only write their own if they had a special need). The win is that the underlying code doesn't have to change just because the end-user configuration format did. - Jason > > -- > Rhodri James *-* Wildebeeste Herder to the Masses From tunpishuang at gmail.com Wed Apr 1 00:17:29 2009 From: tunpishuang at gmail.com (tunpishuang) Date: Tue, 31 Mar 2009 21:17:29 -0700 (PDT) Subject: the return of urllib.request.urlopen("http://www.example.com/", params) Message-ID: hey guys , i'm new in python ...here i got a little problem that get me confused... i wanna do an uthentication of an login page , here is the example from the python lib ref. : >>> import urllib.request >>> import urllib.parse >>> params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0}) >>> f = urllib.request.urlopen("http://www.musi-cal.com/cgi-bin/query?%s" % params) >>> print(f.read()) i wanted to know what the the return value of f ,,if it's an instance of http.client.HTTPResponse and why the return source of the web page is quote like this : b'' if i wanna to read the first 10 bytes of f and compare with the web source. what the first 10 bytes will be ? is it "b' Hi all, i am trying to make an exe out of my py *prg which write to a text file*. On --> python setup.py py2exe the dist folder is created but the exe creted is not working it throws an error "pythons ps.popen function" setup.py: #python setup.py py2exe from distutils.core import setup import py2exe setup(console=['nameofprg.py']) Regards Praksh -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjreedy at udel.edu Wed Apr 1 00:36:52 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 01 Apr 2009 00:36:52 -0400 Subject: Python Goes Mercurial In-Reply-To: References: Message-ID: Lawrence D'Oliveiro wrote: > > > So what were these "strong antipathies" towards Git, exactly? The relevant PEP is http://www.python.org/dev/peps/pep-0374/ To some Pythonic eyes, the git command syntax in the examples given is less graceful. Another thing mentioned in the py-dev thread. Git apparently allows 'changing history' by changing the underlying directed acyclic graph. Some like that, some do not. (I do not completely understand this, but you asked...) tjr From steven at REMOVE.THIS.cybersource.com.au Wed Apr 1 00:49:30 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 01 Apr 2009 04:49:30 GMT Subject: the return of urllib.request.urlopen("http://www.example.com/", params) References: Message-ID: On Tue, 31 Mar 2009 21:17:29 -0700, tunpishuang wrote: > hey guys , i'm new in python ...here i got a little problem that get me > confused... > i wanna do an uthentication of an login page , here is the example from > the python lib ref. : > >>>> import urllib.request >>>> import urllib.parse >>>> params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0}) f >>>> = urllib.request.urlopen("http://www.musi-cal.com/cgi-bin/query?%s" % >>>> params) print(f.read()) > > i wanted to know what the the return value of f ,,if it's an instance of > http.client.HTTPResponse > and why the return source of the web page is quote like this : > > b'' Looks like you are using Python 3.0. In Python 2.x, the native string type is bytes (commonly called "ASCII"). So a string of bytes is displayed like this: 'abcdef...' and a string of Unicode characters is displayed like this: u'abcdef...' In Python 3.0, the native string type is unicode, but HTTP responses are bytes. Bytes are displayed like this: b'abcdef...' and strings of characters like this: 'abcdef...' Notice that the B on the outside of the quotes is not part of the string, it is part of the display format, just like the quotes themselves. You can convert the bytes into a string by just called str() on the output: # untested >>> print(str(f.read())) '' This should work for the simple case, although once you start getting more complicated strings, with international (non-English) characters, you will need to supply an encoding. Does this help? -- Steven From wuwei23 at gmail.com Wed Apr 1 00:50:02 2009 From: wuwei23 at gmail.com (alex23) Date: Tue, 31 Mar 2009 21:50:02 -0700 (PDT) Subject: I find explicit self much easier to understand if i replace it in my mind with the word "instance" References: Message-ID: On Apr 1, 12:05?am, fro... at gmail.com wrote: > Discuss please. Search for past discussion first, please. From tunpishuang at gmail.com Wed Apr 1 00:59:00 2009 From: tunpishuang at gmail.com (tunpishuang) Date: Tue, 31 Mar 2009 21:59:00 -0700 (PDT) Subject: the return of urllib.request.urlopen("http://www.example.com/", params) References: Message-ID: On Apr 1, 12:49?pm, Steven D'Aprano wrote: > On Tue, 31 Mar 2009 21:17:29 -0700, tunpishuang wrote: > > hey guys , i'm new in python ...here i got a little problem that get me > > confused... > > i wanna do an uthentication of an login page , here is the example from > > the python lib ref. : > > >>>> import urllib.request > >>>> import urllib.parse > >>>> params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0}) f > >>>> = urllib.request.urlopen("http://www.musi-cal.com/cgi-bin/query?%s" % > >>>> params) print(f.read()) > > > i wanted to know what the the return value of f ,,if it's an instance of > > http.client.HTTPResponse > > and why the return source of the web page is quote like this : > > > b'' > > Looks like you are using Python 3.0. > > In Python 2.x, the native string type is bytes (commonly called "ASCII"). > So a string of bytes is displayed like this: > > 'abcdef...' > > and a string of Unicode characters is displayed like this: > > u'abcdef...' > > In Python 3.0, the native string type is unicode, but HTTP responses are > bytes. Bytes are displayed like this: > > b'abcdef...' > > and strings of characters like this: > > 'abcdef...' > > Notice that the B on the outside of the quotes is not part of the string, > it is part of the display format, just like the quotes themselves. > > You can convert the bytes into a string by just called str() on the > output: > > # untested>>> print(str(f.read())) > > '' > > This should work for the simple case, although once you start getting > more complicated strings, with international (non-English) characters, > you will need to supply an encoding. > > Does this help? > > -- > Steven so much thanks Steven ,,, one day u came to travel in China ,i'll be you guide~ :) From tjreedy at udel.edu Wed Apr 1 01:03:50 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 01 Apr 2009 01:03:50 -0400 Subject: Beazley on Generators Message-ID: At PyCon2008, David Beazley presented an excellent talk on generators. Generator Tricks for Systems Programmers http://www.dabeaz.com/generators/index.html At PyCon2009, he followed up with another talk on more advanced generator usage, which Guido commended on the python-ideas list: A Curious Course on Coroutines and Concurrency http://dabeaz.com/coroutines/ I have just started (this one will take more than one sitting ;-) but it looks just as good. tjr From michele.simionato at gmail.com Wed Apr 1 01:21:37 2009 From: michele.simionato at gmail.com (Michele Simionato) Date: Tue, 31 Mar 2009 22:21:37 -0700 (PDT) Subject: Beazley on Generators References: Message-ID: On Apr 1, 7:03?am, Terry Reedy wrote: > At PyCon2008, David Beazley presented an excellent talk on generators. > Generator Tricks for Systems Programmershttp://www.dabeaz.com/generators/index.html > > At PyCon2009, he followed up with another talk on more advanced > generator usage, which Guido commended on the python-ideas list: > A Curious Course on Coroutines and Concurrencyhttp://dabeaz.com/coroutines/ > > I have just started (this one will take more than one sitting ;-) but it > looks just as good. > > tjr I concur. I have just read the first pages, but it says all good things I had to discover the hard way in years past. Excellent reading for everybody wanting to understand cooperative concurrency! M. Simionato From hniksic at xemacs.org Wed Apr 1 01:48:45 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Wed, 01 Apr 2009 07:48:45 +0200 Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <87hc19mcgj.fsf@benfinney.id.au> <79e5t4d1bhp0ml87j5fqcu1jd040edr07m@4ax.com> Message-ID: <87wsa4gc02.fsf@busola.homelinux.net> Chris Rebert writes: > Among other things, it has the nice property that: > len(some_list[n:m]) == m-n And also that it is intuitive how to represent an empty slice (foo[n:n]). When traversing over sublists, it's also a useful property that foo[a:b] + foo[b:c] == foo. From ldo at geek-central.gen.new_zealand Wed Apr 1 01:54:41 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Wed, 01 Apr 2009 18:54:41 +1300 Subject: Python Goes Mercurial References: Message-ID: In message , Terry Reedy wrote: > Lawrence D'Oliveiro wrote: >> >> >> So what were these "strong antipathies" towards Git, exactly? > > The relevant PEP is http://www.python.org/dev/peps/pep-0374/ Useful link, though it seems incomplete. Interesting phrase "To make up for svn's lack of cheap branching"--it's not the branching that's the problem in Subversion, it's the merging. :) > Git apparently allows 'changing history' by changing the underlying > directed acyclic graph. Every object in a Git repository is identified by the SHA-1 hash of its entire contents. For a commit object, that includes the references to its parent commit(s). Thus, there's no way to change these while preserving the identity of the commit object. Yes, it's easy with the low-level Git commands to go back and synthesize past history, but you can't hide the fact that you've done so. One of the design goals of Git was guaranteeing the integrity of the entire commit history. From ldo at geek-central.gen.new_zealand Wed Apr 1 01:56:23 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Wed, 01 Apr 2009 18:56:23 +1300 Subject: Python Goes Mercurial References: <35d429fa-5d13-4703-a443-6a95c740c9fb@o6g2000yql.googlegroups.com> Message-ID: In message <35d429fa-5d13-4703- a443-6a95c740c9fb at o6g2000yql.googlegroups.com>, John Yeung wrote: > Here's one that clearly expresses strong antipathy: > > http://mail.python.org/pipermail/python-dev/2009-March/087971.html There are lots of GUI- and Web-based front ends to Git. And look at on-line services like GitHub and Gitorious. The level of support for it is huge. From ldo at geek-central.gen.new_zealand Wed Apr 1 01:57:49 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Wed, 01 Apr 2009 18:57:49 +1300 Subject: Beazley on Generators References: Message-ID: In message , Michele Simionato wrote: > Excellent reading for everybody wanting to understand cooperative > concurrency! Hey, some of us were doing "cooperative concurrency" programming old MacOS for years. It was generally considered a poor alternative to "true multitasking". From castironpi at gmail.com Wed Apr 1 02:09:28 2009 From: castironpi at gmail.com (Aaron Brady) Date: Tue, 31 Mar 2009 23:09:28 -0700 (PDT) Subject: Thoughts on language-level configuration support? References: <36148830-22c0-4f19-ab23-d04d8755ab4a@s28g2000vbp.googlegroups.com> Message-ID: <0f526eec-dd06-4e92-a574-1bbad42d8b07@q16g2000yqg.googlegroups.com> On Mar 30, 8:40?am, jfager wrote: > I've written a short post on including support for configuration down > at the language level, including a small preliminary half-functional > example of what this might look like in Python, available athttp://jasonfager.com/?p=440. > > The basic idea is that a language could offer syntactic support for > declaring configurable points in the program. ?The language system > would then offer an api to allow the end user to discover a programs > configuration service, as well as a general api for providing > configuration values. > > The included example implements the first bit and hints at the third, > defining a function that looks up what variable its output will be > assigned to and tries to find a corresponding value from a > configuration source. ?It's very preliminary, but I hope it gives a > flavor of the general idea. > > Any thoughts or feedback would be greatly appreciated. Hi, joining late. I think you are talking about modifying a running program's code and data from an external process. My first guess at the closest Python can come is: import shelve configs= shelve.open( 'configs.dat' ) #stuff for some computation: calc= Calc( ) calc.precision= configs[ 'precision' ] calc.compute( ) All well and good if you can synchronize access to 'configs.dat', but a subclass of shelve.DbfilenameShelf could accomplish that. For background, "a shelf is a persistent, dictionary-like object" (docs). 'configs.dat' would reside on disk, so any process could access it. From http Wed Apr 1 02:18:21 2009 From: http (Paul Rubin) Date: 31 Mar 2009 23:18:21 -0700 Subject: Python Goes Mercurial References: Message-ID: <7x7i2499si.fsf@ruckus.brouhaha.com> Terry Reedy writes: > > So what were these "strong antipathies" towards Git, exactly? > > The relevant PEP is http://www.python.org/dev/peps/pep-0374/ Interesting. I'm on a project that switched from Mercurial to Git recently. I don't have much of a sense of the relevant differences but other people on the project seemed to prefer Git rather strongly. Git does seem to faster, contrary to the measurements in that PEP. From cs at zip.com.au Wed Apr 1 02:20:28 2009 From: cs at zip.com.au (Cameron Simpson) Date: Wed, 1 Apr 2009 17:20:28 +1100 Subject: urllib2, proxies, and pac files on OS X In-Reply-To: Message-ID: <20090401062028.GA22004@cskk.homeip.net> On 30Mar2009 11:00, Mani Ghasemlou wrote: | urllib2 correctly detects proxies as configured in my preferences pane | on OS X 10.5: [...] | However, when configuring a proxy via PAC file, this does not seem to | be the case: [...] | Is there any way to obtain (and parse using something like pacparser) | the PAC file via urllib2 (or some other standard python library), or | is this something I need to tackle using Cocoa (in my case PyObjC)? You'll need a JavaScript interpreter. A PAC file contains a JavaScript function that is run _per_URL_ to decide what proxy, if any, to use for that particular fetch. Clearly you can't get a static proxy description in the general case. -- Cameron Simpson DoD#743 http://www.cskk.ezoshosting.com/cs/ Look, Dave... I can see you're really upset about this... I honestly think you ought to sit down calmly, ... take a stress pill, and think things over. - HAL, _2001_ From daku9999 at gmail.com Wed Apr 1 02:35:27 2009 From: daku9999 at gmail.com (daku9999 at gmail.com) Date: Tue, 31 Mar 2009 23:35:27 -0700 (PDT) Subject: Stripping non-numbers from a file parse without nested lists? References: <34f50d37-b96b-4ef0-b4ca-f2c471f4c4db@w35g2000prg.googlegroups.com> Message-ID: <4b42a91c-b871-401e-b2b8-097e8087a32d@f1g2000prb.googlegroups.com> On Mar 31, 6:47?pm, "Rhodri James" wrote: > What you're doing (pace error checking) seems fine for the data > structures that you're using. ?I'm not entirely clear what your usage > pattern for "dip" and "dir" is once you've got them, so I can't say > whether there's a more appropriate shape for them. ?I am a bit curious > though as to why a nested list is non-ideal? > > ... > ? ? ?if "/" in word and "dip" not in word: > ? ? ? ? dip_n_dir.append(word.split("/", 1)) > > is marginally shorter, and has the virtue of making it harder to use > unrelated dip and dir values together. > > -- > Rhodri James *-* Wildebeeste Herder to the Masses Rhodri, Thanks. That works better than what I had before and I learned a new method of parsing what I was looking for. Now I'm on to jumping a set number of lines from a given positive search match: ...(lines of garbage)... 5656 (or some other value I want, but don't explicitly know) ...(18 lines of garbage)... search object ...(lines of garbage)... I've tried: def read_poles(filename): index = 0 fh = None try: fh = open(filename, "r") lines=fh.readlines() while True: if "search object" in lines[index] poles = int(lines[index-18]) print(poles) index +=1 except(IndexError): pass finally: if fh is not None: # close file fh.close() ------------------ Which half works. If it's not found, IndexError is caught and passed (avoids quitting on lines[index out of range]. The print(poles) properly displays the value I am looking for (_always_ 18 lines before the search object). However, since it is assigned using the index variable, the value of poles doesn't keep (poles is always zero when referenced outside of the read_poles function). I'm assuming because I'm pointing to a certain position of an object and once index moves on, it no longer points to anything valid. My python book suggested using copy.deepcopy, but that didn't get around the fact I am calling it on (index-18). Any experience jumping back (or forward) a set number of lines once a search object is found? This is the only way I can think of doing it and it clearly has some problems. Reading the file line by line using for line in blah works for finding the search object, but I can't see a way of going back the 18 lines to grabbing what I need. Thanks for the help! I'm slowly getting this mangled mess of a file into something automated (hand investigating the several thousand files I need to do would be unpleasant). From pavlovevidence at gmail.com Wed Apr 1 03:01:55 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 1 Apr 2009 00:01:55 -0700 (PDT) Subject: Python Goes Mercurial References: Message-ID: <2972356f-a2df-4dc5-9071-4ac8518e128b@j18g2000prm.googlegroups.com> On Mar 31, 6:25?pm, Lawrence D'Oliveiro wrote: > > > So what were these "strong antipathies" towards Git, exactly? Apparently Mercurial had it's own hate club, and the reaction on python-dev was so severe GvR backtracked on his decision. Trying to appease a least common denominator, he changed his mind and Python is going with SCCS instead. Carl Banks From clp2 at rebertia.com Wed Apr 1 03:15:07 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 1 Apr 2009 00:15:07 -0700 Subject: Python Goes Mercurial In-Reply-To: <2972356f-a2df-4dc5-9071-4ac8518e128b@j18g2000prm.googlegroups.com> References: <2972356f-a2df-4dc5-9071-4ac8518e128b@j18g2000prm.googlegroups.com> Message-ID: <50697b2c0904010015jd1e16f4w137f1ffbd95b58be@mail.gmail.com> On Wed, Apr 1, 2009 at 12:01 AM, Carl Banks wrote: > On Mar 31, 6:25?pm, Lawrence D'Oliveiro central.gen.new_zealand> wrote: >> >> >> So what were these "strong antipathies" towards Git, exactly? > > Apparently Mercurial had it's own hate club, and the reaction on > python-dev was so severe GvR backtracked on his decision. > > Trying to appease a least common denominator, he changed his mind and > Python is going with SCCS instead. This is why I hate staying up late on March 31st... Cheers, Chris -- I have a blog: http://blog.rebertia.com From gabriel.rossetti at arimaz.com Wed Apr 1 03:25:39 2009 From: gabriel.rossetti at arimaz.com (Gabriel Rossetti) Date: Wed, 01 Apr 2009 09:25:39 +0200 Subject: Beautiful soup : why does "string" not give me the string? Message-ID: <49D316F3.4030303@arimaz.com> Hello everyone, I am using beautiful soup to parse some HTML and I came across something strange. Here is an illustration: >>> soup = BeautifulSoup(u'
hello ?a boume
>> soup
hello ?a boume
>>> soup.find("div", "text")
hello ?a boume
>>> soup.find("div", "text").string >>> soup.find("div", "text").next u'hello \xe7a boume' why does soup.find("div", "text").string not give me the string? Is it because there is a
? Is there a way to have it ignore the
tag? Am I doing something wrong? Thank you, Gabriel From gabriel.rossetti at arimaz.com Wed Apr 1 03:28:09 2009 From: gabriel.rossetti at arimaz.com (Gabriel Rossetti) Date: Wed, 01 Apr 2009 09:28:09 +0200 Subject: urllib2 problem, data param not working? : DISREGARD In-Reply-To: <49D2486E.6020809@arimaz.com> References: <49D2486E.6020809@arimaz.com> Message-ID: <49D31789.9050005@arimaz.com> Gabriel Rossetti wrote: > Hello everyone, > > I am having a problem with urllib2, when I do this : > > post = urllib.urlencode(post) > request = urllib2.Request(url, post) > response = urllib2.urlopen(request) > > or this : > > post = urllib.urlencode(post) > response = urllib2.urlopen(url, post) > > or this : > > post = urllib.urlencode(post) > request = urllib2.Request(url) > response = urllib2.urlopen(request, post) > > it doesn't work, it's like if the post params weren't added, and if I > do this : > > post = urllib.urlencode(post) > request = url + '?' + post > response = urllib2.urlopen(request) > > it works as expected, can anyone explain what is going on? I know that > if I don't add the data ('post' in my case) param it uses an HTTP GET, > could that be why it works when I add them manually? > > Thank you, > Gabriel > -- > Please disregard this msg, I didn't intend to send it, I figured out what was wrong, it was an HTTP GET/POST problem as I suspected. Gabriel From kay.schluehr at gmx.net Wed Apr 1 03:29:05 2009 From: kay.schluehr at gmx.net (Kay Schluehr) Date: Wed, 1 Apr 2009 00:29:05 -0700 (PDT) Subject: Thoughts on language-level configuration support? References: <36148830-22c0-4f19-ab23-d04d8755ab4a@s28g2000vbp.googlegroups.com> <01e0d6e4$0$19301$c3e8da3@news.astraweb.com> <62a06f4c-82e3-4a32-b6e2-ad69ec6f4ac0@f32g2000vbf.googlegroups.com> Message-ID: > "Discoverable", as in built-in tools that let you have the following > conversation: "Program, tell me all the things I can configure about > you" - "Okay, here they all are". No digging through the source > required. But this doesn't have any particular meaning. If I run a dir(obj) command all attributes of obj will be returned and I can be sure these are all. In C# I can reflect on attributes of an assembly which is a well defined entity. "Program" is not an entity. It's kind of a user interface name in StarTreck ( which prefers "computer" for this purpose though ). This way we cannot design systems. From pavlovevidence at gmail.com Wed Apr 1 03:40:17 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 1 Apr 2009 00:40:17 -0700 (PDT) Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <1ej5t4930m29h0f6ttpdcd83t08q2q3sab@4ax.com> Message-ID: <85ca8f71-e5a7-4594-9ec4-ae85f4ba0cc0@r31g2000prh.googlegroups.com> On Mar 31, 7:23?pm, Lada Kugis wrote: > On 01 Apr 2009 01:26:41 GMT, Steven D'Aprano > > wrote: > > >Why Python (and other languages) count from zero instead of one, and > >why half-open intervals are better than closed intervals: > > >http://www.johndcook.com/blog/2008/06/26/why-computer-scientists-coun... > >http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html > > steven, thanks for answering, > > yes, i saw the second one a little time ago (someone else posted it as > well in really cute handwriting version :) and the first time just > now, but the examples which both of them give don't seem to me to be > that relevant, e.g. the pros don't overcome the cons. > > imho, although both sides (mathematical vs engineer) adress some > points, none of them give the final decisive argument. > i understand the math. point of view, but from the practical side it > is not good. it goes nicely into his tidy theory of everything, but > practical and intuitive it is not. as i said, being an engineer, i > tend towards the other side, Lada, I am also an engineer, and I can tell your idea of intuitive is not universal, even among engineers. I certainly do not lean toward one- based indexing. >From a programming standpoint--and remember Python is a programming language--zero-based indexing eliminates the need for a whole lot of extra +1s and -1s when indexing, slicing, and iterating, a lot more than it causes, and that is worth the "cost". This might not be apparent to you if you never tried seriously taking advantage of indexing from zero, or if your programming experience is very narrow. These both seem to be true for you, so you'll just have to take my word for it. I'm sorry that the zero-based indexing isn't the most natural for your field. There are some things about Python that aren't the natural choice for me, too. I would, however, highly recommend that you avoid the ridiculous pseudo-one-based indexing trick you pulled when interfacing C to Fortran. Python is zero-based, and you are going to have a much better experience if you don't fight it. I assure you that it is really not that hard to cope with indices being off by one from what you have written down. Really. I have to interface two languages, one of which indexes from one, the other from zero, and it really is no problem. Carl Banks From ldo at geek-central.gen.new_zealand Wed Apr 1 03:41:27 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Wed, 01 Apr 2009 20:41:27 +1300 Subject: Does Python have certificate? References: <73bdbfcc-e32c-4df6-bdc4-afc0b95ffa89@r33g2000yqn.googlegroups.com> Message-ID: From newptcai at gmail.com Wed Apr 1 03:44:01 2009 From: newptcai at gmail.com (=?GB2312?B?0rvK18qr?=) Date: Wed, 1 Apr 2009 00:44:01 -0700 (PDT) Subject: A design problem I met again and again. Message-ID: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> Hi all, I am a programmer who works with some different kinds of programming languages, like python, C++(in COM), action script, C#, etc. Today, I realized that, what ever language I use, I always meet a same problem and I think I never solve it very well. The problem is : how to break my app into functional pieces? I know it's important to break an application to lots of pieces to make it flexible. But it's easier said than done. I can split an application to 4 or 5 pieces based on "programming functions", for example, logging, socket, string, math, ... When it comes to the business logic, I found I always provide a big class with many methods, and it grow bigger when new functions are added. Recently I use twisted to write a server. It has several protocol classes which decode and encode different kinds of network protocols , and a protocol independent service class which handle request from clients according to business logic. Protocol classes receive message from client, decode it, call method of service, encode result and send it back to client. There are also some utility packages such as logging as I mentioned before. So far so fine, every thing is clear. Until one day I find service has nearly 100 methods and 6000 lines of code. I don't need to read any programming book to know that it's too big. But I can not find an easier way to split it. Here are some solutions I found: 1. add several business classes, and move code in service into them. But this means although service will contains much less code, it still has to keep lots of methods, and the only functions of these methods is call corresponding methods in business classes. The number of methods in service will keep growing for ever. 2. completely move codes in service to business classes containing only classmethods. These protocol classes calls these classmethods directly instead of call service. But this pattern doesn't look that OO. 3. completely move codes in service to business classes. Initialize these classes and pass them to protocol classes. These protocol classes calls these instances of business classes instead of call service. These means whenever I add a new business class. I have to add a parameter to __init__ methods of every protocol class. Not very clear either. ========================================== I got the same problem when writing C#/C++ when I have to provide a lot of method to my code's user. So I create a big class as the entry point of my code. Although these big classes doesn't contains much logic, they do grow bigger and bigger. From deets at nospam.web.de Wed Apr 1 03:50:36 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 01 Apr 2009 09:50:36 +0200 Subject: python for loop In-Reply-To: <1ej5t4930m29h0f6ttpdcd83t08q2q3sab@4ax.com> References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <1ej5t4930m29h0f6ttpdcd83t08q2q3sab@4ax.com> Message-ID: <73gkmcFunr94U1@mid.uni-berlin.de> Lada Kugis schrieb: > On 01 Apr 2009 01:26:41 GMT, Steven D'Aprano > wrote: > >> Why Python (and other languages) count from zero instead of one, and >> why half-open intervals are better than closed intervals: >> >> http://www.johndcook.com/blog/2008/06/26/why-computer-scientists-count-from-zero/ >> http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html > > steven, thanks for answering, > > yes, i saw the second one a little time ago (someone else posted it as > well in really cute handwriting version :) and the first time just > now, but the examples which both of them give don't seem to me to be > that relevant, e.g. the pros don't overcome the cons. > > imho, although both sides (mathematical vs engineer) adress some > points, none of them give the final decisive argument. > i understand the math. point of view, but from the practical side it > is not good. it goes nicely into his tidy theory of everything, but > practical and intuitive it is not. You keep throwing around the concept of "intuition" as if it were something that existis in a globally fixed frame of reference. It is not. From [1]: """ Klein found that under time pressure, high stakes, and changing parameters, experts used their base of experience to identify similar situations and intuitively choose feasible solutions. """ In other words: your gained *knowledge* (including mathematical know-how) determines what's intuitive - to *you*. So all arguing about intuition is rather irrelevant - to me, zero-based counting is intuitive, and it is so with the same right as you prefer one-based. OTOH zero-based counting has technical reasons that also reduces the amount of leaky abstraction problems [2], as it models the underlying reality of memory locations + indexing. Diez [1] http://en.wikipedia.org/wiki/Intuition_(knowledge) [2] http://www.joelonsoftware.com/articles/LeakyAbstractions.html From steven at REMOVE.THIS.cybersource.com.au Wed Apr 1 04:06:28 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 01 Apr 2009 08:06:28 GMT Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <72i5t4tgfo2h4gd6ggcs02flkca85kgoku@4ax.com> <0dbc9665-3ee8-4d62-8e9e-75c6d700076e@f1g2000prb.googlegroups.com> Message-ID: On Wed, 01 Apr 2009 04:39:26 +0100, Rhodri James wrote: > Dragging this back to the original topic, you clearly find starting list > indices from zero unintuitive. To me, with a mathematical background, > it's not just intuitive, it's correct. All sorts of useful properties > fall out from that, not the least of which is the fact that > "a[0:len(a)]" slices the whole of a list. But some non-useful properties fall out of that too. Want the fifth item? a[5] gives you the *sixth* item, which is weird, so you have to use a[5-1] to get the fifth item. There is a major clash between the names of ordinals in human languages and zero-based counting. In human languages, the Nth-ordinal item comes in position N. You can keep that useful convention with zero-based counting by inventing the ugly word "zeroth", but that just leads to bizarro-talk like "the zeroeth item comes first, the first item comes second, and so on". a[0:len(a)] is legal, a[0] is legal, but surprisingly a[len(a)] is an error. Despite coming from a Pascal background, I've come to appreciate and prefer zero-based indexing for programming. But I'm not blind to the disadvantages. I'll often work out an algorithm using pencil and paper and counting from one, and then subtract one to get zero-based indexes. There are advantages and disadvantages to both systems, but on balance, I think that zero-based is a better system for programming, and one-based for natural language. -- Steven From michele.simionato at gmail.com Wed Apr 1 04:22:30 2009 From: michele.simionato at gmail.com (Michele Simionato) Date: Wed, 1 Apr 2009 01:22:30 -0700 (PDT) Subject: Beazley on Generators References: Message-ID: <13298fc5-5024-4343-bf5a-7e271a08d806@o11g2000yql.googlegroups.com> On Apr 1, 7:57?am, Lawrence D'Oliveiro wrote: > In message > , Michele > > Simionato wrote: > > Excellent reading for everybody wanting to understand cooperative > > concurrency! > > Hey, some of us were doing "cooperative concurrency" programming old MacOS > for years. It was generally considered a poor alternative to "true > multitasking". It is a poor alternative compared to OS level processes and even compared to preemptive threads in some cases. However, there are situations when you need thousands of lightweight threads of execution, and in that case coroutines could be the only viable solution. Just read David Beazley's presentation. From jeremiah.dodds at gmail.com Wed Apr 1 04:31:00 2009 From: jeremiah.dodds at gmail.com (Jeremiah Dodds) Date: Wed, 1 Apr 2009 09:31:00 +0100 Subject: Beautiful soup : why does "string" not give me the string? In-Reply-To: <49D316F3.4030303@arimaz.com> References: <49D316F3.4030303@arimaz.com> Message-ID: <12cbbbfc0904010131s329d829dh5db0d97147c6bd30@mail.gmail.com> On Wed, Apr 1, 2009 at 8:25 AM, Gabriel Rossetti < gabriel.rossetti at arimaz.com> wrote: > Hello everyone, > > I am using beautiful soup to parse some HTML and I came across something > strange. > Here is an illustration: > > >>> soup = BeautifulSoup(u'
hello ?a boume
>>> soup >
hello ?a boume
> >>> soup.find("div", "text") >
hello ?a boume
> >>> soup.find("div", "text").string > >>> soup.find("div", "text").next > u'hello \xe7a boume' > > why does soup.find("div", "text").string not give me the string? Is it > because there is a
? IIRC, yes it is, and there's not much you can do about it other than use .next.string or .contents[0] or stripping out brs. See http://www.crummy.com/software/BeautifulSoup/documentation.html , particularly the "Removing Elements" and "string" sections. -------------- next part -------------- An HTML attachment was scrubbed... URL: From jeremiah.dodds at gmail.com Wed Apr 1 04:39:06 2009 From: jeremiah.dodds at gmail.com (Jeremiah Dodds) Date: Wed, 1 Apr 2009 09:39:06 +0100 Subject: udp package header In-Reply-To: References: <200903232217.13195.metebilgin48@gmail.com> Message-ID: <12cbbbfc0904010139s33f0c026o40f4c55078a180c0@mail.gmail.com> On Tue, Mar 31, 2009 at 6:19 PM, Artur M. Piwko wrote: > In the darkest hour on Tue, 24 Mar 2009 00:50:10 +0000 (UTC), > R. David Murray screamed: > >> I got a problem. ? want to send udp package and get this package (server > and > >> clinet ). it's easy to python but i want to look the udp header how can > i > >> do ? > > > > The English word is 'packet'. > > > > If you are on Linux you can use raw sockets for this. > > > > With a little drawback - raw sockets require root privilege. > > Only some of them, or am I misinterpreting the term "raw socket"? -------------- next part -------------- An HTML attachment was scrubbed... URL: From ldo at geek-central.gen.new_zealand Wed Apr 1 04:55:23 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Wed, 01 Apr 2009 21:55:23 +1300 Subject: A design problem I met again and again. References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> Message-ID: In message <48506803-a6b9-432b-acef- b75f76e907da at v23g2000pro.googlegroups.com>, ??? wrote: > Until one day I find service has nearly 100 methods and 6000 lines of > code. I don't need to read any programming book to know that it's > too big. The question is not how many lines or how many methods, but whether it makes sense to remain as one piece or not. In one previous project, I had one source file with nearly 15,000 lines in it. Did it make sense to split that up? Not really. From nicogrubert at gmail.com Wed Apr 1 04:57:04 2009 From: nicogrubert at gmail.com (Nico Grubert) Date: Wed, 01 Apr 2009 10:57:04 +0200 Subject: List of paths Message-ID: <49D32C60.6080201@gmail.com> Dear Python developers I have the following (sorted) list. ['/notebook', '/notebook/mac', '/notebook/mac/macbook', '/notebook/mac/macbookpro', '/notebook/pc', '/notebook/pc/lenovo', '/notebook/pc/hp', '/notebook/pc/sony', '/desktop', '/desktop/pc/dell', '/desktop/mac/imac', '/server/hp/proliant', '/server/hp/proliant/385', '/server/hp/proliant/585' ] I want to remove all paths x from the list if there is a path y in the list which is part of x so y.startswith(x) is true. The list I want to have is: ['/notebook', '/desktop', '/server/hp/proliant'] Any idea how I can do this in Python? Thanks in advance Nico From ldo at geek-central.gen.new_zealand Wed Apr 1 05:01:19 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Wed, 01 Apr 2009 22:01:19 +1300 Subject: I find explicit self much easier to understand if i replace it in my mind with the word "instance" References: Message-ID: In message , Steven D'Aprano wrote: > On Tue, 31 Mar 2009 07:05:13 -0700, frolib wrote: > >> I find explicit self much easier to understand if i replace it in my >> mind with the word "instance". I know that using the word "self" though >> is a strong convention. > > Call it "self", "instance", "me" or "One" (as in, One is not amused) in > your own code, it's all the same. Earlier versions of the Python docs said that the name "self" was expected by certain documentation-extraction tools. I can't find that statement any more. From dorzey at googlemail.com Wed Apr 1 05:45:06 2009 From: dorzey at googlemail.com (dorzey) Date: Wed, 1 Apr 2009 02:45:06 -0700 (PDT) Subject: List of paths References: Message-ID: You could use the followingm, where the_list is your list. (I'm new to python so there might be a better way): toremove = [] for x in the_list: for y in the_list: if y.startswith(x) and y != x: toremove.append(y) difference = filter(lambda x:x not in toremove, the_list) From andrew at acooke.org Wed Apr 1 06:35:25 2009 From: andrew at acooke.org (andrew cooke) Date: Wed, 1 Apr 2009 06:35:25 -0400 (CLT) Subject: List of paths In-Reply-To: <49D32C60.6080201@gmail.com> References: <49D32C60.6080201@gmail.com> Message-ID: <0da986c1411dcb6faa11c28057a50874.squirrel@localhost> >>> def filter(values): ... last = None ... for value in values: ... if last is None or not value.startswith(last): ... yield value ... last = value ... >>> for x in filter(['/notebook', ....]): ... print(x) ... /notebook /desktop /server/hp/proliant andrew Nico Grubert wrote: > Dear Python developers > > I have the following (sorted) list. > ['/notebook', > '/notebook/mac', > '/notebook/mac/macbook', > '/notebook/mac/macbookpro', > '/notebook/pc', > '/notebook/pc/lenovo', > '/notebook/pc/hp', > '/notebook/pc/sony', > '/desktop', > '/desktop/pc/dell', > '/desktop/mac/imac', > '/server/hp/proliant', > '/server/hp/proliant/385', > '/server/hp/proliant/585' > ] > > I want to remove all paths x from the list if there is a path y in the > list which is part of x so y.startswith(x) is true. > > The list I want to have is: > ['/notebook', '/desktop', '/server/hp/proliant'] > > Any idea how I can do this in Python? > > Thanks in advance > Nico > -- > http://mail.python.org/mailman/listinfo/python-list > > From andrew at acooke.org Wed Apr 1 06:40:02 2009 From: andrew at acooke.org (andrew cooke) Date: Wed, 1 Apr 2009 06:40:02 -0400 (CLT) Subject: A design problem I met again and again. In-Reply-To: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> Message-ID: <7e8799bcf74375c368e9db855ce52f2c.squirrel@localhost> ?????? wrote: > 3. completely move codes in service to business classes. Initialize > these classes and pass them to protocol classes. > These protocol classes calls these instances of business classes > instead of call service. These means whenever I add a new business > class. I have to add a parameter to __init__ methods of every > protocol class. Not very clear either. i don't fully understand your problem, but i would guess (3) is the correct solution. you can probably avoid adding a new parameter by writing code in a generic way (using lists of arguments, perhaps using introspection to find method names, etc) andrew From andrew at acooke.org Wed Apr 1 07:04:12 2009 From: andrew at acooke.org (andrew cooke) Date: Wed, 1 Apr 2009 07:04:12 -0400 (CLT) Subject: python for loop In-Reply-To: References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <72i5t4tgfo2h4gd6ggcs02flkca85kgoku@4ax.com> <0dbc9665-3ee8-4d62-8e9e-75c6d700076e@f1g2000prb.googlegroups.com> Message-ID: <75d7ff1a9c13dc73ec6b4359be250ce3.squirrel@localhost> something i don't think has been mentioned much - if you're using "range()" in your python code then you're almost always doing it wrong. i just grepped lepl and i use range 20 times in 9600 lines of code. out of those, all but 3 are in "quick and dirty" tests or experimental code, not in the main library itself (which is 6300 lines). so in my main code, i use range() once in every 2000 lines of code, approximately. the three examples are: (1) where i need to access two adjacent members of a list, and which has a comment in the code explaining why it is not an error (in other words, i was so unhappy with my code i needed to leave a note explaining why it was like that) (2) a use irrelevant to this discussion because i do not use the value to an index an array. (3) in the rather complex implementation of a circular buffer. so in a small/moderate size library of 600 lines (including blanks and comments, but excluding tests and exploratory code) the only time i have used range with array indices i was either unhappy with the code, or implementing a complex data structure. andrew From andrew at acooke.org Wed Apr 1 07:05:46 2009 From: andrew at acooke.org (andrew cooke) Date: Wed, 1 Apr 2009 07:05:46 -0400 (CLT) Subject: python for loop In-Reply-To: <75d7ff1a9c13dc73ec6b4359be250ce3.squirrel@localhost> References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <72i5t4tgfo2h4gd6ggcs02flkca85kgoku@4ax.com> <0dbc9665-3ee8-4d62-8e9e-75c6d700076e@f1g2000prb.googlegroups.com> <75d7ff1a9c13dc73ec6b4359be250ce3.squirrel@localhost> Message-ID: andrew cooke wrote: [...] > so in a small/moderate size library of 600 lines (including blanks and 6000 > comments, but excluding tests and exploratory code) the only time i have > used range with array indices i was either unhappy with the code, or > implementing a complex data structure. From davea at ieee.org Wed Apr 1 07:06:27 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 01 Apr 2009 06:06:27 -0500 Subject: python for loop In-Reply-To: References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <72i5t4tgfo2h4gd6ggcs02flkca85kgoku@4ax.com> <0dbc9665-3ee8-4d62-8e9e-75c6d700076e@f1g2000prb.googlegroups.com> Message-ID: <49D34AB3.5070201@ieee.org> Natural language is full of ambiguity, which is why my parents used to argue about the meaning of "next Wednesday," or of "the next exit." Until you have a starting reference, and until you decide whether it's a closed or open interval, you can't be sure everyone will get the same semantics. Because of their bickering 50 years ago, I try to use a longer phrase, such as "the Wednesday a week from today" or "exit 52." In most? of Europe, the first floor is the one you get to after you go up one flight of stairs. In the US, first floor is usually the ground floor. My ruler starts at zero, and has an explicit zero on it. Most rulers start the same way, but don't actually show the zero. But if you had a ruler that started with one, you'd end up with some off-by-one errors. I repeatedly see people think that if Joe has $100, and Mary has three times more, that she has $300. Yet these same people would balk at the notion that 10% more would be $10. English is a funny language (as I suspect most languages are), and full of ambiguities and foolish inconsistencies. Having worked with some 35 languages over 35 years in the industry, I was glad that most of them were zero based. Steven D'Aprano wrote: > On Wed, 01 Apr 2009 04:39:26 +0100, Rhodri James wrote: > > >> Dragging this back to the original topic, you clearly find starting list >> indices from zero unintuitive. To me, with a mathematical background, >> it's not just intuitive, it's correct. All sorts of useful properties >> fall out from that, not the least of which is the fact that >> "a[0:len(a)]" slices the whole of a list. >> > > But some non-useful properties fall out of that too. > > Want the fifth item? a[5] gives you the *sixth* item, which is weird, so > you have to use a[5-1] to get the fifth item. > > There is a major clash between the names of ordinals in human languages > and zero-based counting. In human languages, the Nth-ordinal item comes > in position N. You can keep that useful convention with zero-based > counting by inventing the ugly word "zeroth", but that just leads to > bizarro-talk like "the zeroeth item comes first, the first item comes > second, and so on". > > a[0:len(a)] is legal, a[0] is legal, but surprisingly a[len(a)] is an > error. > > Despite coming from a Pascal background, I've come to appreciate and > prefer zero-based indexing for programming. But I'm not blind to the > disadvantages. I'll often work out an algorithm using pencil and paper > and counting from one, and then subtract one to get zero-based indexes. > > There are advantages and disadvantages to both systems, but on balance, I > think that zero-based is a better system for programming, and one-based > for natural language. > > > > > From gabriel.rossetti at arimaz.com Wed Apr 1 07:14:11 2009 From: gabriel.rossetti at arimaz.com (Gabriel Rossetti) Date: Wed, 01 Apr 2009 13:14:11 +0200 Subject: BeautifulSoup : why does "string" not give me the string? Message-ID: <49D34C83.6030105@arimaz.com> Hello everyone, I am using BeautifulSoup to parse some HTML and I came across something strange. Here is an illustration: >>> soup = BeautifulSoup(u'
hello ?a boume
>> soup
hello ?a boume
>>> soup.find("div", "text")
hello ?a boume
>>> soup.find("div", "text").string >>> soup.find("div", "text").next u'hello \xe7a boume' why does soup.find("div", "text").string not give me the string? Is it because there is a
? Is there a way to have it ignore the
tag? Am I doing something wrong? Thank you, Gabriel PS Sorry if this appears twice on the list, but I sent it 4 hours ago and it never showed up so I resent it. From gabriel.rossetti at arimaz.com Wed Apr 1 07:20:54 2009 From: gabriel.rossetti at arimaz.com (Gabriel Rossetti) Date: Wed, 01 Apr 2009 13:20:54 +0200 Subject: BeautifulSoup : why does "string" not give me the string? In-Reply-To: <49D34C83.6030105@arimaz.com> References: <49D34C83.6030105@arimaz.com> Message-ID: <49D34E16.6080709@arimaz.com> Gabriel Rossetti wrote: > Hello everyone, > > I am using BeautifulSoup to parse some HTML and I came across something > strange. > Here is an illustration: > >>>> soup = BeautifulSoup(u'
hello ?a boume
>>> soup >
hello ?a boume
>>>> soup.find("div", "text") >
hello ?a boume
>>>> soup.find("div", "text").string >>>> soup.find("div", "text").next > u'hello \xe7a boume' > > why does soup.find("div", "text").string not give me the string? Is it > because there is a
? Is there a way to have it ignore the
> tag? Am I doing something wrong? > > Thank you, > Gabriel > > PS > Sorry if this appears twice on the list, but I sent it 4 hours ago and > it never showed up so I resent it. > Ok, it was my mail reader, I had one instance open on one PC and another on another PC and the IMAP was out of sync or something, the msg wqas only present on the other PC, sorry for the spam... From jfager at gmail.com Wed Apr 1 07:29:54 2009 From: jfager at gmail.com (jfager) Date: Wed, 1 Apr 2009 04:29:54 -0700 (PDT) Subject: Thoughts on language-level configuration support? References: <36148830-22c0-4f19-ab23-d04d8755ab4a@s28g2000vbp.googlegroups.com> <01e0d6e4$0$19301$c3e8da3@news.astraweb.com> <62a06f4c-82e3-4a32-b6e2-ad69ec6f4ac0@f32g2000vbf.googlegroups.com> Message-ID: On Apr 1, 3:29?am, Kay Schluehr wrote: > > "Discoverable", as in built-in tools that let you have the following > > conversation: ?"Program, tell me all the things I can configure about > > you" - "Okay, here they all are". ?No digging through the source > > required. > > But this doesn't have any particular meaning. If I run a dir(obj) > command all attributes of obj will be returned and I can be sure these > are all. In C# I can reflect on attributes of an assembly which is a > well defined entity. "Program" is not an entity. It's kind of a user > interface name in StarTreck ( which prefers "computer" for this > purpose though ). This way we cannot design systems. "Module and transitive graph of other modules (constructed through 'import' statements), tell me all the things I can configure about you". Is that a little clearer? From bearophileHUGS at lycos.com Wed Apr 1 07:39:39 2009 From: bearophileHUGS at lycos.com (bearophileHUGS at lycos.com) Date: Wed, 1 Apr 2009 04:39:39 -0700 (PDT) Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <72i5t4tgfo2h4gd6ggcs02flkca85kgoku@4ax.com> Message-ID: Lada Kugis: > (you have 1 apple, you start counting from 1 ...< To little children I now show how to count starting from zero: apple number zero, apple number one, etc, and they find it natural enough :-) Bye, bearophile From gabriel.rossetti at arimaz.com Wed Apr 1 08:15:58 2009 From: gabriel.rossetti at arimaz.com (Gabriel Rossetti) Date: Wed, 01 Apr 2009 14:15:58 +0200 Subject: Beautiful soup : why does "string" not give me the string? In-Reply-To: <12cbbbfc0904010131s329d829dh5db0d97147c6bd30@mail.gmail.com> References: <49D316F3.4030303@arimaz.com> <12cbbbfc0904010131s329d829dh5db0d97147c6bd30@mail.gmail.com> Message-ID: <49D35AFE.4030508@arimaz.com> Jeremiah Dodds wrote: > > > On Wed, Apr 1, 2009 at 8:25 AM, Gabriel Rossetti > > wrote: > > Hello everyone, > > I am using beautiful soup to parse some HTML and I came across > something strange. > Here is an illustration: > > >>> soup = BeautifulSoup(u'
hello ?a boume
/> >>> soup >
hello ?a boume
> >>> soup.find("div", "text") >
hello ?a boume
> >>> soup.find("div", "text").string > >>> soup.find("div", "text").next > u'hello \xe7a boume' > > why does soup.find("div", "text").string not give me the string? > Is it because there is a
? > > > IIRC, yes it is, and there's not much you can do about it other than > use .next.string or .contents[0] or stripping out brs. See > http://www.crummy.com/software/BeautifulSoup/documentation.html , > particularly the "Removing Elements" and "string" sections. > > Ok, thanks, I also found that I can do this : soup.find(text=lambda t: isinstance(t, basestring)) or this: soup.find(text=True) it seems faster than doing this : [br.extract() for br in soup.findAll("br")] soup.string but I may be wrong. Thanks again! Gabriel From steve at REMOVE-THIS-cybersource.com.au Wed Apr 1 08:34:44 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 01 Apr 2009 12:34:44 GMT Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <72i5t4tgfo2h4gd6ggcs02flkca85kgoku@4ax.com> Message-ID: <01e353ba$0$20654$c3e8da3@news.astraweb.com> On Wed, 01 Apr 2009 04:39:39 -0700, bearophileHUGS wrote: > Lada Kugis: >> (you have 1 apple, you start counting from 1 ...< > > To little children I now show how to count starting from zero: apple > number zero, apple number one, etc, and they find it natural enough :-) Ah, but that's not the same as counting ordinals from zero. Consider the difference between an empty list, which has zero items, and a list of length one, which as that item in position zero. The empty list [] clearly has zero items, in zero "bins", so the bins aren't numbered. A list with one item [x] has one bin, labelled "Index 0". A list with two items [x, y] has two bins, labelled Index 0 and Index 1. A list with three items [x, y, z] has three bins, labelled Index 0, Index 1 and Index 2. etc. Counting "No apples plus one apple makes one apple" is easy enough. But pointing to an apple clearly there and saying "Let's call that apple number zero, even though there is one apple", that requires a very special child! -- Steven From paul at boddie.org.uk Wed Apr 1 09:05:45 2009 From: paul at boddie.org.uk (Paul Boddie) Date: Wed, 1 Apr 2009 06:05:45 -0700 (PDT) Subject: Python Goes Mercurial References: <7x7i2499si.fsf@ruckus.brouhaha.com> Message-ID: <7a1dd0d8-1978-470b-a80d-57478d7f7f9e@q16g2000yqg.googlegroups.com> On 1 Apr, 08:18, Paul Rubin wrote: > Terry Reedy writes: > > > So what were these "strong antipathies" towards Git, exactly? > > > The relevant PEP ishttp://www.python.org/dev/peps/pep-0374/ > > Interesting. ?I'm on a project that switched from Mercurial to Git > recently. ?I don't have much of a sense of the relevant differences > but other people on the project seemed to prefer Git rather strongly. > Git does seem to faster, contrary to the measurements in that PEP. >From what I've seen by browsing the blogs of various large communities, there always seems to be large numbers of Git advocates (although "fanboys" might be a more accurate term in numerous cases) ready to crowd out blog comments with single line insistences that projects switch to Git. Indeed, I've even seen something resembling an advocacy site for Git where there's a comparison to Mercurial and Bazaar, although the benchmarks seem to be done with a certain amount of honesty. Consequently, many projects seem to choose Git either on the basis of what other people have supposedly done, or on the "hacker cred" supposedly conferred through its association with Linus Torvalds. I've also heard various tales of Git usage where "strong antipathies" would be a fairly accurate summary of user experiences, and this is where such experiences can be contrasted with solid experiences with other version control systems. And I've heard stories of "bait and switch" with Git: "you can do XYZ with Git but not with ..." followed by the discovery that you can't realistically do XYZ with Git, either. Such advocacy makes me deeply cynical about Git. To be fair, Git usability has apparently come some way since, say, 2006 when I first encountered Git and Mercurial, with the former being recommended by the guys who develop itools (whose judgement I generally trust) but with the caveat that one used the Cogito front- end at that time, and the latter by the guys who develop MoinMoin (whose judgement I also trust). The difference between the two DVCSs as far as I have seen is that Mercurial has always been as usable as it is today without additional tools. In addition, there seems to be more honesty from the Mercurial community about what Mercurial can and cannot support, as opposed to the disinformation cloud (or perhaps uninformation cloud) that seems to surround Git. As for the performance of these tools, you wouldn't think Mercurial was written in Python (admittedly with some C) and Git was written in C looking at various benchmarks. For what it's worth, this BDFL decision is one I actually agree with. Paul From ericcire at gmail.com Wed Apr 1 09:43:48 2009 From: ericcire at gmail.com (Eric) Date: Wed, 1 Apr 2009 06:43:48 -0700 (PDT) Subject: The spam to content ratio of this group Message-ID: <8a96baf2-25cc-4bc8-a4c8-7e85e02f47f2@v28g2000vbb.googlegroups.com> is getting very high. Why aren't captcha's used to prevent all of this noise? From digitig at gmail.com Wed Apr 1 09:45:47 2009 From: digitig at gmail.com (Tim Rowe) Date: Wed, 1 Apr 2009 14:45:47 +0100 Subject: python for loop In-Reply-To: <85ca8f71-e5a7-4594-9ec4-ae85f4ba0cc0@r31g2000prh.googlegroups.com> References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <1ej5t4930m29h0f6ttpdcd83t08q2q3sab@4ax.com> <85ca8f71-e5a7-4594-9ec4-ae85f4ba0cc0@r31g2000prh.googlegroups.com> Message-ID: 2009/4/1 Carl Banks : > I am also an engineer, and I can tell your idea of intuitive is not > universal, even among engineers. ?I certainly do not lean toward one- > based indexing. Another engineer here who finds 0-based indexing more intuitive than 1-based indexing. -- Tim Rowe From tundra at tundraware.com Wed Apr 1 09:46:37 2009 From: tundra at tundraware.com (Tim Daneliuk) Date: Wed, 01 Apr 2009 08:46:37 -0500 Subject: Python Goes Mercurial In-Reply-To: <2972356f-a2df-4dc5-9071-4ac8518e128b@j18g2000prm.googlegroups.com> References: <2972356f-a2df-4dc5-9071-4ac8518e128b@j18g2000prm.googlegroups.com> Message-ID: Carl Banks wrote: > On Mar 31, 6:25 pm, Lawrence D'Oliveiro central.gen.new_zealand> wrote: >> >> >> So what were these "strong antipathies" towards Git, exactly? > > Apparently Mercurial had it's own hate club, and the reaction on > python-dev was so severe GvR backtracked on his decision. > > Trying to appease a least common denominator, he changed his mind and > Python is going with SCCS instead. > > > Carl Banks ;) A few years ago I had a very large client - many multi-billions of dollars in revenue annually - that ran their entire custom IT system from a home grown variant of SCCS. It did work, but when I suggested that better open source tools existed, they kindly explained their complete lack of interest in moving several millions of lines of code to anything new. Go figure. -- ---------------------------------------------------------------------------- Tim Daneliuk tundra at tundraware.com PGP Key: http://www.tundraware.com/PGP/ From xdicry at gmail.com Wed Apr 1 09:53:34 2009 From: xdicry at gmail.com (Evan) Date: Wed, 1 Apr 2009 06:53:34 -0700 (PDT) Subject: how to handle/generate pcap file Message-ID: <2c6e2640-0229-4886-bf22-d62636d9e13a@a5g2000pre.googlegroups.com> Hello - I'm trying to decode the pcap file which is packet capture by tcpdump or wireshark. Is there a python module that I can use it for this problem? Can python-libpcap or pycap or dpkt do that? On the other hand, I also generate some packets by dpkt module, can I also dump these packets to a pcap file? such as *.pcap. The problem is I do not want to send these packets, I just want to generate some special packets and then store it in a pcap file (libpcap format), so other guys can check it by tcpdump or wireshark. Thanks, Evan From pecora at anvil.nrl.navy.mil Wed Apr 1 09:55:58 2009 From: pecora at anvil.nrl.navy.mil (Lou Pecora) Date: Wed, 01 Apr 2009 09:55:58 -0400 Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <1ej5t4930m29h0f6ttpdcd83t08q2q3sab@4ax.com> Message-ID: In article <1ej5t4930m29h0f6ttpdcd83t08q2q3sab at 4ax.com>, Lada Kugis wrote: > On 01 Apr 2009 01:26:41 GMT, Steven D'Aprano > wrote: > > > > >Why Python (and other languages) count from zero instead of one, and > >why half-open intervals are better than closed intervals: > > > >http://www.johndcook.com/blog/2008/06/26/why-computer-scientists-count-from-z > >ero/ > >http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html > > steven, thanks for answering, > > yes, i saw the second one a little time ago (someone else posted it as > well in really cute handwriting version :) and the first time just > now, but the examples which both of them give don't seem to me to be > that relevant, e.g. the pros don't overcome the cons. > > imho, although both sides (mathematical vs engineer) adress some > points, none of them give the final decisive argument. > i understand the math. point of view, but from the practical side it > is not good. it goes nicely into his tidy theory of everything, but > practical and intuitive it is not. as i said, being an engineer, i > tend towards the other side, so this is biased opinion (nobody can be > unbiased) but from a practical side it seems unpractical for > engineering problems (and to me, the purpose of computers is to help > humans to build a better world, not to prove theories - theories are > useless if they don't help us in reality. so we should try to adapt > computing to real world, not our world to computers). There is nothing more practical than a good theory. --- James Clerk Maxwell You said you came from the C world (besides Fortran). If so, you have already seen array indexing starting with 0 and going to n-1. Why, then, should Python be so foreign to you? -- -- Lou Pecora From steve at REMOVE-THIS-cybersource.com.au Wed Apr 1 09:56:47 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 01 Apr 2009 13:56:47 GMT Subject: The spam to content ratio of this group References: <8a96baf2-25cc-4bc8-a4c8-7e85e02f47f2@v28g2000vbb.googlegroups.com> Message-ID: <01e366f4$0$20654$c3e8da3@news.astraweb.com> On Wed, 01 Apr 2009 06:43:48 -0700, Eric wrote: > is getting very high. Why aren't captcha's used to prevent all of this > noise? /me opens mouth to make sarcastic comment /me shuts mouth again That's like saying "There's a lot of oil slicks washing up onto the beach. Why don't we put more trashcans by the road to the beach?" What good do you think captchas will do on a newsgroup? -- Steven who has noticed that just as Hotmail became the new AOL a few years back, now Gmail has become the new Hotmail. From greg at grsdesign.com Wed Apr 1 09:59:38 2009 From: greg at grsdesign.com (greg at grsdesign.com) Date: Wed, 01 Apr 2009 06:59:38 -0700 Subject: Need a Python Training Class recommendation Message-ID: <6b8863e1ae533aebd843b9a60559c32c@127.0.0.1> Hi guys, My company has offered to let me attend a week or so of python training in the New England area. I have looked around but can't seem to find a sit down course. Do you have any suggestions? I live in Manchester NH but would be willing to drive into Boston area. Any experiences, ideas or options are welcome. Thanks, Greg From pruebauno at latinmail.com Wed Apr 1 10:00:18 2009 From: pruebauno at latinmail.com (pruebauno at latinmail.com) Date: Wed, 1 Apr 2009 07:00:18 -0700 (PDT) Subject: complaints about no replies last week References: <2d80ec1b-5eb5-4e82-9a4a-36934dd53800@z9g2000yqi.googlegroups.com> Message-ID: <31a9bb25-8949-48c5-bbd3-7c5a7f4d91b6@e2g2000vbe.googlegroups.com> On Mar 31, 4:07?pm, Arnaud Delobelle wrote: > prueba... at latinmail.com writes: > > [...] > > > > > Well since I attracted a couple people's attention I will describe the > > problem in more detail. Describing the problem properly is probably as > > hard as solving it, so excuse me if I struggle a bit. > > > The problem is for a health insurance company and involves the period > > of time a person is covered. Most insurance companies allow not only > > for the main member to be insured but his family: the spouse and the > > dependents (children). This additional coverage costs extra but less > > than a full new insurance. So for example if Alice buys an insurance > > worth at 100 dollars a month, she can insure her husband Bob for an > > additional 50 dollars. Under certain circumstances Alice may go off > > the insurance and only Bob stays. In that case the price goes back to > > 100 dollars or maybe there is a deal for 80 or something like that. In > > other words the cost of the insurance is dependent on the combination > > of family members that participate in it. Additionally not only do we > > have different family compositions but also different insurance > > products. So you can get medical, dental and vision insurance. > > > All that data is stored in a database that is not very tidy and looks > > something like this: > > > First Day of Coverage, Last Day of Coverage, Relationship, Product > > 5/3/2005, 5/3/2005, D, M > > 9/10/2005, 10/10/2005, S, V > > 3/15/2005, 7/15/2005, M, M > > 3/1/2005, 6/1/2005, S, M > > 5/15/2005, 7/20/2005, D, D > > 9/10/2005, 1/1/2140, M, V > > 2/1/2005, 5/3/2005, M, M > > > Where > > Relationship: M=Main Member, S=Spouse, D=Dependent > > Product: M=Medical, D=Dental, V=Vision > > > As far as I know at the present time there are no deals based on > > combination of products purchased so we will handle each product > > independently. What I want is a simple algorithm that allows me to > > calculate something like this out of it (hopefully I didn?t make a > > mistake): > > > Medical: > > 2/1/2005, 2/28/2005, M > > 3/1/2005, 5/2/2005, M&S > > 5/3/2005, 5/3/2005, M&S&D > > 5/4/2005, 6/1/2005, M&S > > 6/2/2005, 7/15/2005, M > > > Dental: > > 5/15/2005, 7/20/2005, D > > > Vision: > > 9/10/2005, 10/10/2005, M&S > > 10/11/2005, 1/1/2140, M > > OK the approach I describe in my previous email works fine for this > particular problem. I implement it below - the function walk_ivals is at > the heart of it, I've made it as simple as possible and it's pretty > clear that it is O(nlogn). > > The function that actually sorts the data is union(), and it's just a > call to walk_ivals with callback the function acc() which is constant > time, therefore union() itself has the same complexity as walk_ivals. > > There are no comments - I don't have the time to add any, sorry! > > -------------------------------------------------- > import datetime > from collections import defaultdict > > def walk_ivals(ivals, callback, endvals=(-1, 1)): > ? ? endpoints = [(x, data) for ival, data in ivals for x in zip(ival, endvals)] > ? ? endpoints.sort() > ? ? for (endpoint, endval), data in endpoints: > ? ? ? ? callback(endpoint, endval, data) > > def union(ivals): > ? ? timelines = defaultdict(list) > ? ? mult = defaultdict(lambda: defaultdict(int)) > ? ? def acc(date, step, data): > ? ? ? ? rel, prod = data > ? ? ? ? old_mult = mult[prod][rel] > ? ? ? ? mult[prod][rel] -= step > ? ? ? ? if not(old_mult and mult[prod][rel]): > ? ? ? ? ? ? rels = [rel for rel, m in mult[prod].iteritems() if m] > ? ? ? ? ? ? if timelines[prod] and timelines[prod][-1][0] == date: > ? ? ? ? ? ? ? ? timelines[prod][-1][1] = rels > ? ? ? ? ? ? else: > ? ? ? ? ? ? ? ? timelines[prod].append([date, rels]) > ? ? walk_ivals(ivals, acc) > ? ? return timelines > > test_data = """5/3/2005, 5/3/2005, D, M > 9/10/2005, 10/10/2005, S, V > 3/15/2005, 7/15/2005, M, M > 3/1/2005, 6/1/2005, S, M > 5/15/2005, 7/20/2005, D, D > 9/10/2005, 1/1/2140, M, V > 2/1/2005, 5/3/2005, M, M""" > > def parse_date(date_string): > ? ? month, day, year = map(int, date_string.split('/')) > ? ? return datetime.date(year, month, day) > > def parse_data(data_string): > ? ? data = [] > ? ? for line in data_string.split("\n"): > ? ? ? ? start, end, rel, prod = line.split(",") > ? ? ? ? start, end = map(parse_date, (start + datetime.timedelta(1), end)) > ? ? ? ? rel, prod = rel.strip(), prod.strip() > ? ? ? ? data.append([(start, end), (rel, prod)]) > ? ? return data > > def test(): > ? ? ivals = parse_data(test_data) > ? ? timelines = union(ivals) > ? ? for prod, timeline in timelines.iteritems(): > ? ? ? ? print "-"*20 > ? ? ? ? print "Product", prod > ? ? ? ? for date, covers in timeline: > ? ? ? ? ? ? print date, ' & '.join(covers) > > if __name__ == '__main__': > ? ?test() ? ? ? > -------------------------------------------------- > > Here is what it outputs: > > marigold:junk arno$ python intervals2.py > -------------------- > Product M > 2005-02-01 M > 2005-03-01 S & M > 2005-05-03 S & M & D > 2005-05-04 S & M > 2005-06-02 M > 2005-07-16 > -------------------- > Product D > 2005-05-15 D > 2005-07-21 > -------------------- > Product V > 2005-09-10 S & M > 2005-10-11 M > 2140-01-02 > > -------------------------------------------------- > > The date output is slightly different from yours - I didn't realise you > had time intervals and now I don't have the time to change it, although > it's only a cosmetic change (the end-date of each interval is the > start-date of the next one minus 1 day). > > HTH > > PS: warning - I have done some minor editing of the code after pasting > it, so it might break (although it should be fine). > > -- > Arnaud Thanks Arnaud, indeed I got this error when running the program: Traceback (most recent call last): File "C:/Python26/timeint2.py", line 57, in test() File "C:/Python26/timeint2.py", line 48, in test ivals = parse_data(test_data) File "C:/Python26/timeint2.py", line 42, in parse_data start, end = map(parse_date, (start + datetime.timedelta(1), end)) TypeError: cannot concatenate 'str' and 'datetime.timedelta' objects easy fix: start, end = map(parse_date, (start , end)) end+=datetime.timedelta(1) I am assuming you want close-open intervals. I also added another line to the test: test_data = """5/3/2005, 5/3/2005, D, M 9/10/2005, 10/10/2005, S, V 3/15/2005, 7/15/2005, M, M 3/1/2005, 6/1/2005, S, M 5/15/2005, 7/20/2005, D, D 9/10/2005, 1/1/2140, M, V 2/1/2005, 5/3/2005, M, M 3/1/2004, 7/1/2004, M, M""" result: -------------------- Product M 2004-03-01 M 2004-07-02 2005-02-01 M 2005-03-01 S & M 2005-05-03 S & M & D 2005-05-04 S & M 2005-06-02 M 2005-07-16 -------------------- Product D 2005-05-15 D 2005-07-21 -------------------- Product V 2005-09-10 S & M 2005-10-11 M 2140-01-02 The output will have to be massaged to get the begin and end of each interval, but otherwise this is more or less what I was looking for. The walk_ivals and union function are 20 lines of pretty dense code. I will have to study it a bit to understand how it really works. Thanks again From csd01412 at gmail.com Wed Apr 1 10:02:21 2009 From: csd01412 at gmail.com (akshat agarwal) Date: Wed, 1 Apr 2009 19:32:21 +0530 Subject: Regex trouble Message-ID: <2bcaec310904010702t59077209y55fd93a1070fd64@mail.gmail.com> Hi, I am trying to use the following snippet of code to print a regex match. s = '01234567890123456789x012' pat = r'(.*?x|[^a]+)*y' mo = re.search(pat, s) if mo is not None: print mo.group(0) By adding one character before the 'x' in the input string, the time taken to print the match doubles. This behaviour is not observed in perl. I am curious to know about the difference the in regex implementations of perl and python which causes this. Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From notvalid2 at sbcglobal.net Wed Apr 1 10:07:29 2009 From: notvalid2 at sbcglobal.net (W. eWatson) Date: Wed, 01 Apr 2009 07:07:29 -0700 Subject: Default Tkinter Structure of a 640x480 PIL BMP File? Message-ID: See Subject. Does it have a header, DIB, palette, and data section? What is the default depth? -- 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 lada.kugis at gmail.com Wed Apr 1 10:08:44 2009 From: lada.kugis at gmail.com (Lada Kugis) Date: Wed, 01 Apr 2009 16:08:44 +0200 Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <1ej5t4930m29h0f6ttpdcd83t08q2q3sab@4ax.com> <85ca8f71-e5a7-4594-9ec4-ae85f4ba0cc0@r31g2000prh.googlegroups.com> Message-ID: <91t6t4hfjicgvdrcgkhdjfro3ko3ktum8l@4ax.com> On Wed, 1 Apr 2009 00:40:17 -0700 (PDT), Carl Banks wrote: > >Lada, > >I am also an engineer, and I can tell your idea of intuitive is not >universal, even among engineers. I certainly do not lean toward one- >based indexing. > >From a programming standpoint--and remember Python is a programming >language--zero-based indexing eliminates the need for a whole lot of >extra +1s and -1s when indexing, slicing, and iterating, a lot more >than it causes, and that is worth the "cost". This might not be >apparent to you if you never tried seriously taking advantage of >indexing from zero, or if your programming experience is very narrow. >These both seem to be true for you, so you'll just have to take my >word for it. You have repeated several cs based points already stated. But apart from a programming standpoint - could you give a few examples, where "on paper" (as to avoid stepping into "programmer's territory") zero indexing could be more intuitive ? (of course, taking into account your previous based calculations, which are based on 1 indexing - I imagine you still use matrices with a11 as a first element) Lada From pecora at anvil.nrl.navy.mil Wed Apr 1 10:09:24 2009 From: pecora at anvil.nrl.navy.mil (Lou Pecora) Date: Wed, 01 Apr 2009 10:09:24 -0400 Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <72i5t4tgfo2h4gd6ggcs02flkca85kgoku@4ax.com> Message-ID: In article <72i5t4tgfo2h4gd6ggcs02flkca85kgoku at 4ax.com>, Lada Kugis wrote: > and > the (m-n) point Chris was trying to explain doesn't seem that relevant > to me. That's because you haven't done enough programming really using the Python structures and objects. You can really do a lot with lists and tuples. When you do you will see Chris' point emphatically. -- -- Lou Pecora From lada.kugis at gmail.com Wed Apr 1 10:12:27 2009 From: lada.kugis at gmail.com (Lada Kugis) Date: Wed, 01 Apr 2009 16:12:27 +0200 Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <72i5t4tgfo2h4gd6ggcs02flkca85kgoku@4ax.com> <0dbc9665-3ee8-4d62-8e9e-75c6d700076e@f1g2000prb.googlegroups.com> Message-ID: On 01 Apr 2009 08:06:28 GMT, Steven D'Aprano wrote: > >There are advantages and disadvantages to both systems, but on balance, I >think that zero-based is a better system for programming, and one-based >for natural language. Nicely put. Yes, along with some of your other arguments, I think I can agree that this sums it up best. I'll just have to adapt myself to natural language thinking at one side, and programming thinking at the other. Lada From nicogrubert at gmail.com Wed Apr 1 10:16:32 2009 From: nicogrubert at gmail.com (Nico Grubert) Date: Wed, 01 Apr 2009 16:16:32 +0200 Subject: List of paths In-Reply-To: <35253ca50904010338v612d1072j9ff7abf920ad6aa8@mail.gmail.com> References: <49D32C60.6080201@gmail.com> <35253ca50904010338v612d1072j9ff7abf920ad6aa8@mail.gmail.com> Message-ID: <49D37740.60005@gmail.com> > May be not so much pythonic, but works > > for i in range(len(q)): > for x in q[i:]: > if x.startswith(q[i]) and x!=q[i]: > q.remove(x) ...but works fine. Thanks, Eugene. Also thanks to Andrew. Your example works fine, too. Thanks to remind me of the 'yield' statement! ;-) Regards Nico From Maria.Reinhammar at accalon.com Wed Apr 1 10:19:25 2009 From: Maria.Reinhammar at accalon.com (Maria R) Date: Wed, 1 Apr 2009 07:19:25 -0700 (PDT) Subject: comtypes CreateObject problem Message-ID: We get the following, less favourable respons :o( Any advice is greatly appreciated!! Setting is python 2.5.2, windows xp professional SP3, SolidEdge V20 SP10 We have success with comtypes interfacing other software like SmarTeam (PDM/PLM software) and of course Excel and alike. Thnx IDLE 1.2.2 >>> import comtypes.client >>> foo = comtypes.client.CreateObject("SolidEdge.Application") # Generating comtypes.gen._8A7EFA3A_F000_11D1_BDFC_080036B4D502_0_1_0 # Generating comtypes.gen._00020430_0000_0000_C000_000000000046_0_2_0 # Generating comtypes.gen.stdole Traceback (most recent call last): File "", line 1, in foo = comtypes.client.CreateObject("SolidEdge.Application") File "C:\Python25\lib\site-packages\comtypes\client\__init__.py", line 204, in CreateObject return _manage(obj, clsid, interface=interface) File "C:\Python25\lib\site-packages\comtypes\client\__init__.py", line 165, in _manage obj = GetBestInterface(obj) File "C:\Python25\lib\site-packages\comtypes\client\__init__.py", line 102, in GetBestInterface mod = GetModule(tlib) File "C:\Python25\lib\site-packages\comtypes\client\_generate.py", line 112, in GetModule mod = _CreateWrapper(tlib, pathname) File "C:\Python25\lib\site-packages\comtypes\client\_generate.py", line 188, in _CreateWrapper mod = _my_import(fullname) File "C:\Python25\lib\site-packages\comtypes\client\_generate.py", line 26, in _my_import return __import__(fullname, globals(), locals(), ['DUMMY']) File "C:\Python25\lib\site-packages\comtypes\gen \_8A7EFA3A_F000_11D1_BDFC_080036B4D502_0_1_0.py", line 3393, in ( ['in'], POINTER(_midlSAFEARRAY(c_char)), 'Accelerators' )), File "C:\Python25\lib\site-packages\comtypes\safearray.py", line 18, in _midlSAFEARRAY sa_type = _make_safearray_type(itemtype) File "C:\Python25\lib\site-packages\comtypes\safearray.py", line 52, in _make_safearray_type raise TypeError(itemtype) TypeError: >>> From flankw at gmail.com Wed Apr 1 10:20:39 2009 From: flankw at gmail.com (Coonay) Date: Wed, 1 Apr 2009 07:20:39 -0700 (PDT) Subject: i have to change default tab length in pydev References: <7edeb68b-e111-42eb-a60f-47dbb4030589@u9g2000pre.googlegroups.com> <344c55f0-d876-4dc5-bf0a-3269b87ba9f7@g19g2000yql.googlegroups.com> Message-ID: <6e155ea0-d73e-4565-a500-76766664bfdd@v35g2000pro.googlegroups.com> On Mar 31, 8:39?am, "Rhodri James" wrote: > > i understand that you get request token,and you need to get Access > > tokens after that, > > my question is about Access tokens:which mechanism ?do u use to tore > > access tokens for future use > > I'm sorry, I have no idea what you're talking about or how it relates > to bad indent levels. ?What *exactly* (including the *whole* traceback) > is Python saying to you? > > -- > Rhodri James *-* Wildebeeste Herder to the Masses sorry , Rhodri ,i replythe wrong post,As of this my own post,i think your opnion is reasonable though,but it's still a little wield of python indentation syntax From loluengo at gmail.com Wed Apr 1 10:21:52 2009 From: loluengo at gmail.com (Lorenzo) Date: Wed, 1 Apr 2009 07:21:52 -0700 (PDT) Subject: Stripping non-numbers from a file parse without nested lists? References: <34f50d37-b96b-4ef0-b4ca-f2c471f4c4db@w35g2000prg.googlegroups.com> Message-ID: <4631054b-dfb5-4c1b-8333-f24cee714905@q2g2000vbr.googlegroups.com> Maybe you can try a regex, something like ------ import re pattern = re.compile('^(\d+)/(\d+).*') ????def read_data(filename): fh = open(filename, "r", encoding="ascii") for line in fh: if pattern.match(line): dip_,dir_ = pattern.match(line).groups() dip.append(dip_) dir.append(dir_) ----- From ricaraoz at gmail.com Wed Apr 1 10:26:26 2009 From: ricaraoz at gmail.com (=?ISO-8859-1?Q?Ricardo_Ar=E1oz?=) Date: Wed, 01 Apr 2009 11:26:26 -0300 Subject: python for loop In-Reply-To: <1ej5t4930m29h0f6ttpdcd83t08q2q3sab@4ax.com> References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <1ej5t4930m29h0f6ttpdcd83t08q2q3sab@4ax.com> Message-ID: <49D37992.2000101@gmail.com> Lada Kugis wrote: > On 01 Apr 2009 01:26:41 GMT, Steven D'Aprano > wrote: > > >> Why Python (and other languages) count from zero instead of one, and >> why half-open intervals are better than closed intervals: >> >> http://www.johndcook.com/blog/2008/06/26/why-computer-scientists-count-from-zero/ >> http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html >> > > steven, thanks for answering, > > yes, i saw the second one a little time ago (someone else posted it as > well in really cute handwriting version :) and the first time just > now, but the examples which both of them give don't seem to me to be > that relevant, e.g. the pros don't overcome the cons. > > imho, although both sides (mathematical vs engineer) adress some > points, none of them give the final decisive argument. > i understand the math. point of view, but from the practical side it > is not good. it goes nicely into his tidy theory of everything, but > practical and intuitive it is not. as i said, being an engineer, i > tend towards the other side, so this is biased opinion (nobody can be > unbiased) but from a practical side it seems unpractical for > engineering problems (and to me, the purpose of computers is to help > humans to build a better world, not to prove theories - theories are > useless if they don't help us in reality. so we should try to adapt > computing to real world, not our world to computers). > Speaking about "reality", it would have saved a lot of time (but maybe not fun) to just do : >>> myRange = lambda x : range(1, x+1) >>> myRange(4) [1, 2, 3, 4] Put it in a file named "MyIntuition.py" in the python path, and then in your programs you can code : from MyIntuition import myRange -------------- next part -------------- An HTML attachment was scrubbed... URL: From kay.schluehr at gmx.net Wed Apr 1 10:33:37 2009 From: kay.schluehr at gmx.net (Kay Schluehr) Date: Wed, 1 Apr 2009 07:33:37 -0700 (PDT) Subject: Beazley on Generators References: Message-ID: <9aa35106-a495-4079-b9ed-fe2a61e26a24@g19g2000yql.googlegroups.com> On 1 Apr., 07:03, Terry Reedy wrote: > At PyCon2008, David Beazley presented an excellent talk on generators. > Generator Tricks for Systems Programmershttp://www.dabeaz.com/generators/index.html > > At PyCon2009, he followed up with another talk on more advanced > generator usage, which Guido commended on the python-ideas list: > A Curious Course on Coroutines and Concurrencyhttp://dabeaz.com/coroutines/ > > I have just started (this one will take more than one sitting ;-) but it > looks just as good. > > tjr There is just one thing I find disappointing. Since the talk is almost a compendium of advanced uses of generators I'm missing a reference to Peter Thatchers implementation of monads: http://www.valuedlessons.com/2008/01/monads-in-python-with-nice-syntax.html Peters implementation can be simplified but it already contains all relevant ideas. From newptcai at gmail.com Wed Apr 1 10:38:43 2009 From: newptcai at gmail.com (=?UTF-8?B?5LiA6aaW6K+X?=) Date: Wed, 1 Apr 2009 07:38:43 -0700 (PDT) Subject: A design problem I met again and again. References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> Message-ID: <5e6ad73b-837a-4b0a-89a7-0f0392b60dd7@k19g2000prh.googlegroups.com> I also think that's my best choice. Before I wrote my mail, I already knew that this is not a good question. It lacks details, and it is too big. But I think the first step to resolve a problem is to describe it. In that way, I might find the answer myself On Apr 1, 6:40?pm, "andrew cooke" wrote: > ?????? wrote: > > 3. completely move codes in service to business classes. ?Initialize > > these classes and pass them to protocol classes. > > These protocol classes calls these instances of business classes > > instead of call service. ?These means whenever I add a new business > > class. ?I have to add a parameter to __init__ methods of every > > protocol class. ?Not very clear either. > > i don't fully understand your problem, but i would guess (3) is the > correct solution. ?you can probably avoid adding a new parameter by > writing code in a generic way (using lists of arguments, perhaps using > introspection to find method names, etc) > > andrew From newptcai at gmail.com Wed Apr 1 10:40:40 2009 From: newptcai at gmail.com (=?UTF-8?B?5LiA6aaW6K+X?=) Date: Wed, 1 Apr 2009 07:40:40 -0700 (PDT) Subject: A design problem I met again and again. References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> Message-ID: <158986a9-b2d2-413e-9ca0-c584299f176b@f1g2000prb.googlegroups.com> On Apr 1, 4:55?pm, Lawrence D'Oliveiro wrote: > In message <48506803-a6b9-432b-acef- > > b75f76e90... at v23g2000pro.googlegroups.com>, ??? wrote: > > Until one day I find service has nearly 100 methods and 6000 lines of > > code. ? I don't need to read any programming book to know that it's > > too big. > > The question is not how many lines or how many methods, but whether it makes > sense to remain as one piece or not. In one previous project, I had one > source file with nearly 15,000 lines in it. Did it make sense to split that > up? Not really. What are the average size of source files in your project? If it's far lower than 15,000, don't feel it's a little unbalance? From jud779 at gmail.com Wed Apr 1 10:43:33 2009 From: jud779 at gmail.com (jud779 at gmail.com) Date: Wed, 1 Apr 2009 07:43:33 -0700 (PDT) Subject: xml to xhtml Message-ID: Hi, I have a data in xml file and i want to show this on html page and i am planning to put his on http server. I am exploring my options to show content of xml file. Consider me new with html and all i am looking to generate simple html page from xml data. Also, this xml file that i created was from python code element tree. If anyone can give me some guidance what should be the best way to generate html/xhtml page using python would be great. I am open to other options like xsl or anything else that can make things simple. Thanks in advance. From google at mrabarnett.plus.com Wed Apr 1 10:45:55 2009 From: google at mrabarnett.plus.com (MRAB) Date: Wed, 01 Apr 2009 15:45:55 +0100 Subject: python for loop In-Reply-To: <72i5t4tgfo2h4gd6ggcs02flkca85kgoku@4ax.com> References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <72i5t4tgfo2h4gd6ggcs02flkca85kgoku@4ax.com> Message-ID: <49D37E23.5060800@mrabarnett.plus.com> Lada Kugis wrote: [snip] > Yes, that's it. I won't argue over it, since I can't change it, but 1 > is still more natural to me (it is "the real world" way). The above > pros seem to me to be very little compared to the cons of the ... and > the (m-n) point Chris was trying to explain doesn't seem that relevant > to me. > I always thought since python is trying so hard to be as intuitive as > it can get it would be the other way (you have 1 apple, you start > counting from 1 ... > [snip] But you might not have any apples, so you should start from 0! :-) From google at mrabarnett.plus.com Wed Apr 1 10:47:15 2009 From: google at mrabarnett.plus.com (MRAB) Date: Wed, 01 Apr 2009 15:47:15 +0100 Subject: python for loop In-Reply-To: References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <72i5t4tgfo2h4gd6ggcs02flkca85kgoku@4ax.com> <0dbc9665-3ee8-4d62-8e9e-75c6d700076e@f1g2000prb.googlegroups.com> Message-ID: <49D37E73.7080902@mrabarnett.plus.com> Steven D'Aprano wrote: > On Wed, 01 Apr 2009 04:58:48 +0200, Lada Kugis wrote: > >> Why do we try to create languages that are intuitive to humans, then ? > > Because of the foolish hope that sufficiently easy syntax will make > excellent programmers out of average people. > > Programming is not intuitive to humans. *Counting* isn't intuitive to > humans -- children need to learn how to count. > [snip] Research suggests that your wrong. For example, even baby chicks can count: http://news.bbc.co.uk/1/hi/sci/tech/7975260.stm From invalid at invalid Wed Apr 1 10:57:16 2009 From: invalid at invalid (Grant Edwards) Date: Wed, 01 Apr 2009 09:57:16 -0500 Subject: The spam to content ratio of this group References: <8a96baf2-25cc-4bc8-a4c8-7e85e02f47f2@v28g2000vbb.googlegroups.com> Message-ID: On 2009-04-01, Eric wrote: > is getting very high. Why aren't captcha's used to prevent all of this > noise? 1) Captcha's don't work. 2) The NNTP protocol doesn't support Captcha's 3) Simply ignoring all posts made from Google Groups works quite well. -- Grant Edwards grante Yow! Did YOU find a at DIGITAL WATCH in YOUR box visi.com of VELVEETA? From andrew at acooke.org Wed Apr 1 11:00:51 2009 From: andrew at acooke.org (andrew cooke) Date: Wed, 1 Apr 2009 11:00:51 -0400 (CLT) Subject: Regex trouble In-Reply-To: <2bcaec310904010702t59077209y55fd93a1070fd64@mail.gmail.com> References: <2bcaec310904010702t59077209y55fd93a1070fd64@mail.gmail.com> Message-ID: <509e36f968a3aeacb7ad8aaf4f5c8685.squirrel@localhost> ".*?" is a "not greedy" match, which is significantly more difficult to handle than a normal ".*". so the performance will depend on quite complex details of how the regular expression engine is implemented. it wouldn't surprise me if perl was better here, because it comes from a background with a much stronger emphasis on regular expressions. i know that not an exact answer, but unless you find the author of the re library i am not sure you will get a much better explanation. it comes down to whether the regular expression can be implemented using a deterministic finite automaton (rather than an indeterministic one). if you look at the table at the bottom of http://en.wikipedia.org/wiki/Finite_state_machine then i believe (i am not 100% sure) that the ".*?" match requires at least a pushdown automota, while ".*" can be handled with a simple finite automaton. disclaimer: this is all fairly new to me as i just recently implemented a regular expression matcher myself, and i may be wrong on some of the details. andrew akshat agarwal wrote: > Hi, > > I am trying to use the following snippet of code to print a regex match. > > s = '01234567890123456789x012' > > pat = r'(.*?x|[^a]+)*y' > > mo = re.search(pat, s) > if mo is not None: > print mo.group(0) > > By adding one character before the 'x' in the input string, the time taken > to print the match doubles. This behaviour is not observed in perl. I am > curious to know about the difference the in regex implementations of perl > and python which causes this. > > Thanks > -- > http://mail.python.org/mailman/listinfo/python-list > From andrew at acooke.org Wed Apr 1 11:02:55 2009 From: andrew at acooke.org (andrew cooke) Date: Wed, 1 Apr 2009 11:02:55 -0400 (CLT) Subject: Regex trouble In-Reply-To: <509e36f968a3aeacb7ad8aaf4f5c8685.squirrel@localhost> References: <2bcaec310904010702t59077209y55fd93a1070fd64@mail.gmail.com> <509e36f968a3aeacb7ad8aaf4f5c8685.squirrel@localhost> Message-ID: <4b8470fc5da4298d22103ab0ad6ebf0c.squirrel@localhost> more exactly, my guess is perl has a special case for this that avoids doing a search over all possible matchers via the pushdown stack. andrew cooke wrote: > > ".*?" is a "not greedy" match, which is significantly more difficult to > handle than a normal ".*". so the performance will depend on quite > complex details of how the regular expression engine is implemented. it > wouldn't surprise me if perl was better here, because it comes from a > background with a much stronger emphasis on regular expressions. > > i know that not an exact answer, but unless you find the author of the re > library i am not sure you will get a much better explanation. it comes > down to whether the regular expression can be implemented using a > deterministic finite automaton (rather than an indeterministic one). if > you look at the table at the bottom of > http://en.wikipedia.org/wiki/Finite_state_machine then i believe (i am not > 100% sure) that the ".*?" match requires at least a pushdown automota, > while ".*" can be handled with a simple finite automaton. > > disclaimer: this is all fairly new to me as i just recently implemented a > regular expression matcher myself, and i may be wrong on some of the > details. > > andrew > > > akshat agarwal wrote: >> Hi, >> >> I am trying to use the following snippet of code to print a regex match. >> >> s = '01234567890123456789x012' >> >> pat = r'(.*?x|[^a]+)*y' >> >> mo = re.search(pat, s) >> if mo is not None: >> print mo.group(0) >> >> By adding one character before the 'x' in the input string, the time >> taken >> to print the match doubles. This behaviour is not observed in perl. I am >> curious to know about the difference the in regex implementations of >> perl >> and python which causes this. >> >> Thanks >> -- >> http://mail.python.org/mailman/listinfo/python-list >> > > From andrew at acooke.org Wed Apr 1 11:04:50 2009 From: andrew at acooke.org (andrew cooke) Date: Wed, 1 Apr 2009 11:04:50 -0400 (CLT) Subject: List of paths In-Reply-To: <49D37740.60005@gmail.com> References: <49D32C60.6080201@gmail.com> <35253ca50904010338v612d1072j9ff7abf920ad6aa8@mail.gmail.com> <49D37740.60005@gmail.com> Message-ID: Nico Grubert wrote: >> May be not so much pythonic, but works >> >> for i in range(len(q)): >> for x in q[i:]: >> if x.startswith(q[i]) and x!=q[i]: >> q.remove(x) > > ...but works fine. Thanks, Eugene. > Also thanks to Andrew. Your example works fine, too. Thanks to remind me > of the 'yield' statement! ;-) in case it's not clear, the method above is O(n^2) while mine was O(n). that will not matter for small lists, but for long ones mine will be much faster. andrew From dear.jay.logan at gmail.com Wed Apr 1 11:05:01 2009 From: dear.jay.logan at gmail.com (jay logan) Date: Wed, 1 Apr 2009 08:05:01 -0700 (PDT) Subject: Stripping non-numbers from a file parse without nested lists? References: <34f50d37-b96b-4ef0-b4ca-f2c471f4c4db@w35g2000prg.googlegroups.com> <4b42a91c-b871-401e-b2b8-097e8087a32d@f1g2000prb.googlegroups.com> Message-ID: <647caf7a-ffad-4da9-a9f7-33c3d11c9af1@o6g2000yql.googlegroups.com> On Apr 1, 2:35?am, daku9... at gmail.com wrote: > On Mar 31, 6:47?pm, "Rhodri James" > wrote: > > > What you're doing (pace error checking) seems fine for the data > > structures that you're using. ?I'm not entirely clear what your usage > > pattern for "dip" and "dir" is once you've got them, so I can't say > > whether there's a more appropriate shape for them. ?I am a bit curious > > though as to why a nested list is non-ideal? > > > ... > > ? ? ?if "/" in word and "dip" not in word: > > ? ? ? ? dip_n_dir.append(word.split("/", 1)) > > > is marginally shorter, and has the virtue of making it harder to use > > unrelated dip and dir values together. > > > -- > > Rhodri James *-* Wildebeeste Herder to the Masses > > Rhodri, > > Thanks. ?That works better than what I had before and I learned a new > method of parsing what I was looking for. > > Now I'm on to jumping a set number of lines from a given positive > search match: > > ...(lines of garbage)... > 5656 ? ? ?(or some other value I want, but don't explicitly know) > ...(18 lines of garbage)... > search object > ...(lines of garbage)... > > I've tried: > > def read_poles(filename): > ? index = 0 > ? fh = None > ? try: > ? ? ? fh = open(filename, "r") > ? ? ? lines=fh.readlines() > ? ? ? while True: > > ? ? ? ? ? if "search object" in lines[index] > ? ? ? ? ? ? ? poles = int(lines[index-18]) > ? ? ? ? ? ? ? print(poles) > > ? ? ? ? ? index +=1 > > ? except(IndexError): pass > > ? finally: > ? ? ? if fh is not None: # close file > ? ? ? ? ? fh.close() > > ------------------ > > Which half works. ?If it's not found, IndexError is caught and passed > (avoids quitting on lines[index out of range]. ?The print(poles) > properly displays the value I am looking for (_always_ 18 lines before > the search object). > > However, since it is assigned using the index variable, the value of > poles doesn't keep (poles is always zero when referenced outside of > the read_poles function). ?I'm assuming because I'm pointing to a > certain position of an object and once index moves on, it no longer > points to anything valid. ?My python book suggested using > copy.deepcopy, but that didn't get around the fact I am calling it on > (index-18). > > Any experience jumping back (or forward) a set number of lines once a > search object is found? ?This is the only way I can think of doing it > and it clearly has some problems. > > Reading the file line by line using for line in blah works for finding > the search object, but I can't see a way of going back the 18 lines to > grabbing what I need. > > Thanks for the help! ?I'm slowly getting this mangled mess of a file > into something automated (hand investigating the several thousand > files I need to do would be unpleasant). # You could try using a deque holding 18 lines and search using that deque # This is untested, but here's a try (>=Python 3.0) from collections import deque import itertools as it import sys def read_poles(filename): with open(filename) as f: line_iter = iter(f) d = deque(it.islice(line_iter,17), maxlen=18) for line in line_iter: d.append(line) if 'search object' in line: poles = int(d[0]) print(poles) return poles else: print('No poles found in', filename, file=sys.err) From dear.jay.logan at gmail.com Wed Apr 1 11:10:23 2009 From: dear.jay.logan at gmail.com (jay logan) Date: Wed, 1 Apr 2009 08:10:23 -0700 (PDT) Subject: Stripping non-numbers from a file parse without nested lists? References: <34f50d37-b96b-4ef0-b4ca-f2c471f4c4db@w35g2000prg.googlegroups.com> <4b42a91c-b871-401e-b2b8-097e8087a32d@f1g2000prb.googlegroups.com> <647caf7a-ffad-4da9-a9f7-33c3d11c9af1@o6g2000yql.googlegroups.com> Message-ID: On Apr 1, 11:05?am, jay logan wrote: > On Apr 1, 2:35?am, daku9... at gmail.com wrote: > > > > > On Mar 31, 6:47?pm, "Rhodri James" > > wrote: > > > > What you're doing (pace error checking) seems fine for the data > > > structures that you're using. ?I'm not entirely clear what your usage > > > pattern for "dip" and "dir" is once you've got them, so I can't say > > > whether there's a more appropriate shape for them. ?I am a bit curious > > > though as to why a nested list is non-ideal? > > > > ... > > > ? ? ?if "/" in word and "dip" not in word: > > > ? ? ? ? dip_n_dir.append(word.split("/", 1)) > > > > is marginally shorter, and has the virtue of making it harder to use > > > unrelated dip and dir values together. > > > > -- > > > Rhodri James *-* Wildebeeste Herder to the Masses > > > Rhodri, > > > Thanks. ?That works better than what I had before and I learned a new > > method of parsing what I was looking for. > > > Now I'm on to jumping a set number of lines from a given positive > > search match: > > > ...(lines of garbage)... > > 5656 ? ? ?(or some other value I want, but don't explicitly know) > > ...(18 lines of garbage)... > > search object > > ...(lines of garbage)... > > > I've tried: > > > def read_poles(filename): > > ? index = 0 > > ? fh = None > > ? try: > > ? ? ? fh = open(filename, "r") > > ? ? ? lines=fh.readlines() > > ? ? ? while True: > > > ? ? ? ? ? if "search object" in lines[index] > > ? ? ? ? ? ? ? poles = int(lines[index-18]) > > ? ? ? ? ? ? ? print(poles) > > > ? ? ? ? ? index +=1 > > > ? except(IndexError): pass > > > ? finally: > > ? ? ? if fh is not None: # close file > > ? ? ? ? ? fh.close() > > > ------------------ > > > Which half works. ?If it's not found, IndexError is caught and passed > > (avoids quitting on lines[index out of range]. ?The print(poles) > > properly displays the value I am looking for (_always_ 18 lines before > > the search object). > > > However, since it is assigned using the index variable, the value of > > poles doesn't keep (poles is always zero when referenced outside of > > the read_poles function). ?I'm assuming because I'm pointing to a > > certain position of an object and once index moves on, it no longer > > points to anything valid. ?My python book suggested using > > copy.deepcopy, but that didn't get around the fact I am calling it on > > (index-18). > > > Any experience jumping back (or forward) a set number of lines once a > > search object is found? ?This is the only way I can think of doing it > > and it clearly has some problems. > > > Reading the file line by line using for line in blah works for finding > > the search object, but I can't see a way of going back the 18 lines to > > grabbing what I need. > > > Thanks for the help! ?I'm slowly getting this mangled mess of a file > > into something automated (hand investigating the several thousand > > files I need to do would be unpleasant). > > # You could try using a deque holding 18 lines and search using that > deque > # This is untested, but here's a try (>=Python 3.0) > from collections import deque > import itertools as it > import sys > > def read_poles(filename): > ? ? with open(filename) as f: > ? ? ? ? line_iter = iter(f) > ? ? ? ? d = deque(it.islice(line_iter,17), maxlen=18) > > ? ? ? ? for line in line_iter: > ? ? ? ? ? ? d.append(line) > > ? ? ? ? ? ? if 'search object' in line: > ? ? ? ? ? ? ? ? poles = int(d[0]) > ? ? ? ? ? ? ? ? print(poles) > ? ? ? ? ? ? ? ? return poles > ? ? ? ? else: > ? ? ? ? ? ? print('No poles found in', filename, file=sys.err) Notice that I returned the "pole" from the function so you could catch the return value as follows: pole = read_poles(filename) if pole is None: # no poles found else: print('Function returned this pole:', pole) If you need a list of poles, then return a list: def read_poles(filename): all_poles = [] with open(filename) as f: line_iter = iter(f) d = deque(it.islice(line_iter,17), maxlen=18) for line in line_iter: d.append(line) if 'search object' in line: all_poles.append(int(d[0])) return all_poles ... poles = read_poles(filename) if poles: print('Here are the poles:\n', '\n'.join(map(str,poles))) else: print('There were no poles found in', filename) From jjposner at snet.net Wed Apr 1 11:10:38 2009 From: jjposner at snet.net (John Posner) Date: Wed, 01 Apr 2009 11:10:38 -0400 Subject: Which is more Pythonic? (was: Detecting Binary content in files) In-Reply-To: References: Message-ID: <8A212AF984C24BE6A8C5325B1FA29866@AMDUP> Dennis Lee Bieber presented a code snippet with two consecutive statements that made me think, "I'd code this differently". So just for fun ... is Dennis's original statement or my "_alt" statement more idiomatically Pythonic? Are there even more Pythonic alternative codings? mrkrs = [b for b in block if b > 127 or b in [ "\r", "\n", "\t" ] ] mrkrs_alt1 = filter(lambda b: b > 127 or b in [ "\r", "\n", "\t" ], block) mrkrs_alt2 = filter(lambda b: b > 127 or b in list("\r\n\t"), block) (Note: Dennis's statement converts a string into a list; mine does not.) --- binary = (float(len(mrkrs)) / len(block)) > 0.30 binary_alt = 1.0 * len(mrkrs) / len(block) > 0.30 -John E-mail message checked by Spyware Doctor (6.0.0.386) Database version: 5.12090 http://www.pctools.com/en/spyware-doctor-antivirus/ From andrew at acooke.org Wed Apr 1 11:10:40 2009 From: andrew at acooke.org (andrew cooke) Date: Wed, 1 Apr 2009 11:10:40 -0400 (CLT) Subject: python for loop In-Reply-To: <49D37E73.7080902@mrabarnett.plus.com> References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <72i5t4tgfo2h4gd6ggcs02flkca85kgoku@4ax.com> <0dbc9665-3ee8-4d62-8e9e-75c6d700076e@f1g2000prb.googlegroups.com> <49D37E73.7080902@mrabarnett.plus.com> Message-ID: MRAB wrote: > Steven D'Aprano wrote: >> On Wed, 01 Apr 2009 04:58:48 +0200, Lada Kugis wrote: >> >>> Why do we try to create languages that are intuitive to humans, then ? >> >> Because of the foolish hope that sufficiently easy syntax will make >> excellent programmers out of average people. >> >> Programming is not intuitive to humans. *Counting* isn't intuitive to >> humans -- children need to learn how to count. >> > [snip] > Research suggests that your wrong. For example, even baby chicks can > count: > > http://news.bbc.co.uk/1/hi/sci/tech/7975260.stm i saw that earlier today. it's really pushing the definition of "count", at least as described there. there have been similar experiments where they address whether the animal is actually looking at "how big" the total "pile" is rather than counting (do they know the difference between two small things and one big thing, for example). that experiment doesn't seem to address this. andrew From andrew at acooke.org Wed Apr 1 11:15:51 2009 From: andrew at acooke.org (andrew cooke) Date: Wed, 1 Apr 2009 11:15:51 -0400 (CLT) Subject: Beazley on Generators In-Reply-To: <9aa35106-a495-4079-b9ed-fe2a61e26a24@g19g2000yql.googlegroups.com> References: <9aa35106-a495-4079-b9ed-fe2a61e26a24@g19g2000yql.googlegroups.com> Message-ID: Kay Schluehr wrote: > There is just one thing I find disappointing. Since the talk is almost > a compendium of advanced uses of generators I'm missing a reference to > Peter Thatchers implementation of monads: > > http://www.valuedlessons.com/2008/01/monads-in-python-with-nice-syntax.html > > Peters implementation can be simplified but it already contains all > relevant ideas. oh that's neat. thanks for that. andrew From goon12 at gmail.com Wed Apr 1 11:16:15 2009 From: goon12 at gmail.com (Joe Riopel) Date: Wed, 1 Apr 2009 11:16:15 -0400 Subject: xml to xhtml In-Reply-To: References: Message-ID: <6a2ccd190904010816w471722q48ad37d9236ea66d@mail.gmail.com> On Wed, Apr 1, 2009 at 10:43 AM, wrote: > If anyone can give me some guidance what should be the best way to > generate html/xhtml page using python would be great. I am open to > other options like xsl or anything else that can make things simple. Since you're open to other options, I would take a look at XSLT. http://www.w3schools.com/xsl/ From skip at pobox.com Wed Apr 1 11:20:22 2009 From: skip at pobox.com (skip at pobox.com) Date: Wed, 1 Apr 2009 10:20:22 -0500 Subject: The spam to content ratio of this group In-Reply-To: <01e366f4$0$20654$c3e8da3@news.astraweb.com> References: <8a96baf2-25cc-4bc8-a4c8-7e85e02f47f2@v28g2000vbb.googlegroups.com> <01e366f4$0$20654$c3e8da3@news.astraweb.com> Message-ID: <18899.34358.313434.924884@montanaro.dyndns.org> Eric> is getting very high. Why aren't captcha's used to prevent all of Eric> this noise? Steven> /me opens mouth to make sarcastic comment Steven> /me shuts mouth again Steven> That's like saying "There's a lot of oil slicks washing up onto Steven> the beach. Why don't we put more trashcans by the road to the Steven> beach?" What good do you think captchas will do on a newsgroup? Come on over the python-list at python.org. The water's fine! -- Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/ From mail at timgolden.me.uk Wed Apr 1 11:23:56 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Wed, 01 Apr 2009 16:23:56 +0100 Subject: The spam to content ratio of this group In-Reply-To: <18899.34358.313434.924884@montanaro.dyndns.org> References: <8a96baf2-25cc-4bc8-a4c8-7e85e02f47f2@v28g2000vbb.googlegroups.com> <01e366f4$0$20654$c3e8da3@news.astraweb.com> <18899.34358.313434.924884@montanaro.dyndns.org> Message-ID: <49D3870C.4050706@timgolden.me.uk> skip at pobox.com wrote: > Eric> is getting very high. Why aren't captcha's used to prevent all of > Eric> this noise? > > Steven> /me opens mouth to make sarcastic comment > Steven> /me shuts mouth again > > Steven> That's like saying "There's a lot of oil slicks washing up onto > Steven> the beach. Why don't we put more trashcans by the road to the > Steven> beach?" What good do you think captchas will do on a newsgroup? > > Come on over the python-list at python.org. The water's fine! > I was going to say much the same, coupled with a vote of thanks to the people (person?) who's done all the work to keep the spam at bay. (part-pun partly intended) TJG From eugene.perederey at gmail.com Wed Apr 1 11:24:18 2009 From: eugene.perederey at gmail.com (Eugene Perederey) Date: Wed, 1 Apr 2009 19:24:18 +0400 Subject: List of paths In-Reply-To: <35253ca50904010823m39320f70m9106b0a5333e5ec5@mail.gmail.com> References: <49D32C60.6080201@gmail.com> <35253ca50904010338v612d1072j9ff7abf920ad6aa8@mail.gmail.com> <49D37740.60005@gmail.com> <35253ca50904010823m39320f70m9106b0a5333e5ec5@mail.gmail.com> Message-ID: <35253ca50904010824s74e52151i99906b6445dc0350@mail.gmail.com> Sure, generators rock! :-) 2009/4/1 andrew cooke : > Nico Grubert wrote: >>> May be not so much pythonic, but works >>> >>> for i in range(len(q)): >>> ? ? for x in q[i:]: >>> ? ? ? ?if x.startswith(q[i]) and x!=q[i]: >>> ? ? ? ? ? ?q.remove(x) >> >> ...but works fine. Thanks, Eugene. >> Also thanks to Andrew. Your example works fine, too. Thanks to remind me >> of the 'yield' statement! ;-) > > in case it's not clear, the method above is O(n^2) while mine was O(n). > that will not matter for small lists, but for long ones mine will be much > faster. > > andrew > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Sincerely yours, Eugene Perederey > -- Sincerely yours, Eugene Perederey From jeremiah.dodds at gmail.com Wed Apr 1 11:36:16 2009 From: jeremiah.dodds at gmail.com (Jeremiah Dodds) Date: Wed, 1 Apr 2009 16:36:16 +0100 Subject: A design problem I met again and again. In-Reply-To: <158986a9-b2d2-413e-9ca0-c584299f176b@f1g2000prb.googlegroups.com> References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> <158986a9-b2d2-413e-9ca0-c584299f176b@f1g2000prb.googlegroups.com> Message-ID: <12cbbbfc0904010836l47158be4ydcd5d7201599a3d6@mail.gmail.com> On Wed, Apr 1, 2009 at 3:40 PM, ??? wrote: > What are the average size of source files in your project? If it's > far lower than 15,000, don't feel it's a little unbalance? > -- > http://mail.python.org/mailman/listinfo/python-list > While I think 15,000 is, in the vast majority of cases, quite high, what we're talking about here isn't about LOC, it's about whether or not things are separated cleanly. I'm sure that sometimes that means a 15,000 line file (although I can't think of a relevant example off the top of my head). Whether or not the other files are a lot smaller doesn't matter. "Correctly" organizing a project is more about separation of responsibility than line-count. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ericofam at yahoo.com Wed Apr 1 12:03:47 2009 From: ericofam at yahoo.com (olusina eric) Date: Wed, 1 Apr 2009 09:03:47 -0700 (PDT) Subject: Matrix operations on character matrix element? Message-ID: <159284.7265.qm@web53611.mail.re2.yahoo.com> I hope somebody will be able to help me here. I am trying to solve some physical problems that will require the generation of some function in terms of some parameters. These functions are derived from matrix operation on ?characters?. Are there ways numpy/scipy perform matrix operations on characters? For example A =? matrix([[a, b,c],[d,e,f],[1,2,3]])?? ????????????????????????? B = matrix([[g,h,4],[I,j,5],[k,l,6]]) Is it possible to perform operations like A*B or A+B And most especially: linalg.solve(A,eye(3,3)) Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From bieffe62 at gmail.com Wed Apr 1 12:31:44 2009 From: bieffe62 at gmail.com (bieffe62 at gmail.com) Date: Wed, 1 Apr 2009 09:31:44 -0700 (PDT) Subject: Which is more Pythonic? (was: Detecting Binary content in files) References: Message-ID: <917541bf-1751-4b83-b570-25804eb63cda@f19g2000yqo.googlegroups.com> On Apr 1, 5:10?pm, John Posner wrote: > Dennis Lee Bieber presented a code snippet with two consecutive statements > that made me think, "I'd code this differently". So just for fun ... is > Dennis's original statement or my "_alt" statement more idiomatically > Pythonic? Are there even more Pythonic alternative codings? > > ? ?mrkrs = [b for b in block > ? ? ?if b > 127 > ? ? ? ?or b in [ "\r", "\n", "\t" ] ? ? ? ] > > ? ?mrkrs_alt1 = filter(lambda b: b > 127 or b in [ "\r", "\n", "\t" ], > block) > ? ?mrkrs_alt2 = filter(lambda b: b > 127 or b in list("\r\n\t"), block) > Never tested my 'pythonicity', but I would do: def test(b) : b > 127 or b in r"\r\n\t" mrkrs = filter( test, block ) Note: before starting to study haskell, I would probably have used the list comprehension. Still can't stand anonimous functions though. > (Note: Dennis's statement converts a string into a list; mine does not.) > > --- > > ? ?binary = (float(len(mrkrs)) / len(block)) > 0.30 > > ? ?binary_alt = 1.0 * len(mrkrs) / len(block) > 0.30 > I believe now one should do (at least on new code): from __future__ import division # not needed for python 3.0 binary = ( len( mrks) / len (blocks) ) > 3.0 In the past, I often used the * 1.0 trick, but nevertheless believe that it is better using explicit cast. > -John > Ciao ----- FB From ppearson at nowhere.invalid Wed Apr 1 12:48:10 2009 From: ppearson at nowhere.invalid (Peter Pearson) Date: 1 Apr 2009 16:48:10 GMT Subject: Beazley on Generators References: Message-ID: <73hk6aFv51cbU1@mid.individual.net> On Wed, 01 Apr 2009 01:03:50 -0400, Terry Reedy wrote: > At PyCon2008, David Beazley presented an excellent talk on generators. > Generator Tricks for Systems Programmers > http://www.dabeaz.com/generators/index.html > > At PyCon2009, he followed up with another talk on more advanced > generator usage, which Guido commended on the python-ideas list: > A Curious Course on Coroutines and Concurrency > http://dabeaz.com/coroutines/ Great presentations. Thanks. -- To email me, substitute nowhere->spamcop, invalid->net. From ntwrkd at gmail.com Wed Apr 1 13:03:35 2009 From: ntwrkd at gmail.com (ntwrkd) Date: Wed, 1 Apr 2009 10:03:35 -0700 Subject: Creating a Python Module - Available Implementations Message-ID: I have been browsing through creating a Python module for common custom functions that I frequently use, but I am wondering, is this the best method, and is it necessary? Really all I need is to import functions from another plaintext Python source file, how might I do this? What would merit the need to create a C-based Python module? Thanks in advance From pavlovevidence at gmail.com Wed Apr 1 13:05:36 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 1 Apr 2009 10:05:36 -0700 (PDT) Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <1ej5t4930m29h0f6ttpdcd83t08q2q3sab@4ax.com> <85ca8f71-e5a7-4594-9ec4-ae85f4ba0cc0@r31g2000prh.googlegroups.com> <91t6t4hfjicgvdrcgkhdjfro3ko3ktum8l@4ax.com> Message-ID: On Apr 1, 7:08?am, Lada Kugis wrote: > On Wed, 1 Apr 2009 00:40:17 -0700 (PDT), Carl Banks > > > > > > wrote: > > >Lada, > > >I am also an engineer, and I can tell your idea of intuitive is not > >universal, even among engineers. ?I certainly do not lean toward one- > >based indexing. > > >From a programming standpoint--and remember Python is a programming > >language--zero-based indexing eliminates the need for a whole lot of > >extra +1s and -1s when indexing, slicing, and iterating, a lot more > >than it causes, and that is worth the "cost". ?This might not be > >apparent to you if you never tried seriously taking advantage of > >indexing from zero, or if your programming experience is very narrow. > >These both seem to be true for you, so you'll just have to take my > >word for it. > > You have repeated several cs based points already stated. But apart > from a programming standpoint - could you give a few examples, where > "on paper" (as to avoid stepping into "programmer's territory") zero > indexing could be more intuitive ? That's beside the point. Python's job is to be usable as a programming language. It made a decision to use zero-based indexing so as to simplify programming usage. Therefore, that's what you have to use for matrix indices, whether it is intuitive for you or not. I have no real opinion whether zero- or one-based indexing works best on paper for engineering calculations, except that it's a trivial consideration. > (of course, taking into account your previous based calculations, > which are based on 1 indexing - I imagine you still use matrices with > a11 as a first element) I don't know what you're referring to by my previous based calculations, and the sentences doesn't make sense, so I can't answer this. FTR: I use zero-based matrix indices when I'm using a language with zero-based indexing, and one-based indices when using a language with one-based indexing. Simple as that. You should do the same. Carl Banks From google at mrabarnett.plus.com Wed Apr 1 13:08:45 2009 From: google at mrabarnett.plus.com (MRAB) Date: Wed, 01 Apr 2009 18:08:45 +0100 Subject: python for loop In-Reply-To: References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <72i5t4tgfo2h4gd6ggcs02flkca85kgoku@4ax.com> <0dbc9665-3ee8-4d62-8e9e-75c6d700076e@f1g2000prb.googlegroups.com> <49D37E73.7080902@mrabarnett.plus.com> Message-ID: <49D39F9D.6080407@mrabarnett.plus.com> andrew cooke wrote: > MRAB wrote: >> Steven D'Aprano wrote: >>> On Wed, 01 Apr 2009 04:58:48 +0200, Lada Kugis wrote: >>> >>>> Why do we try to create languages that are intuitive to humans, then ? >>> Because of the foolish hope that sufficiently easy syntax will make >>> excellent programmers out of average people. >>> >>> Programming is not intuitive to humans. *Counting* isn't intuitive to >>> humans -- children need to learn how to count. >>> >> [snip] >> Research suggests that your wrong. For example, even baby chicks can >> count: >> >> http://news.bbc.co.uk/1/hi/sci/tech/7975260.stm > > i saw that earlier today. it's really pushing the definition of "count", > at least as described there. > > there have been similar experiments where they address whether the animal > is actually looking at "how big" the total "pile" is rather than counting > (do they know the difference between two small things and one big thing, > for example). that experiment doesn't seem to address this. > Just occurred to me. Chicks and eggs, on 1 April, with Easter approaching. Hmm... From Tribulations at Paralleles.invalid Wed Apr 1 13:13:33 2009 From: Tribulations at Paralleles.invalid (TP) Date: Wed, 01 Apr 2009 19:13:33 +0200 Subject: double/float precision question Message-ID: Hi everybody, Try the following python statements: >>> "%.40f" % 0.2222222222222222222222222222222 '0.2222222222222222098864108374982606619596' >>> float( 0.2222222222222222222222222222222) 0.22222222222222221 It seems the first result is the same than the following C program: ################ #include int main(void) { double a = 0.2222222222222222222222222222222; printf( "%.40f\n", a ); return 0; } ################# My problem is the following: * the precision "40" (for example) is given by the user, not by the programmer. * I want to use the string conversion facility with specifier "e", that yields number is scientific format; so I cannot apply float() on the result of "%.40e" % 0.2222222222222222222222222222222, I would lost the scientific format. Is there any means to obtain the full C double in Python, or should I limit the precision given by the user (and used in "%.*e") to the one of a Python float? Thanks in advance Julien -- python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.\ 9&1+,\'Z4(55l4('])" "When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong." (first law of AC Clarke) From rt8396 at gmail.com Wed Apr 1 13:21:31 2009 From: rt8396 at gmail.com (r) Date: Wed, 1 Apr 2009 10:21:31 -0700 (PDT) Subject: The spam to content ratio of this group References: <8a96baf2-25cc-4bc8-a4c8-7e85e02f47f2@v28g2000vbb.googlegroups.com> Message-ID: <96e3f528-e70c-4dac-998e-fc08d52fae95@r3g2000vbp.googlegroups.com> On Apr 1, 9:57?am, Grant Edwards wrote: > On 2009-04-01, Eric wrote: > 3) Simply ignoring all posts made from Google Groups works > ? ?quite well. Yea, there's no spam in Usenet land APRIL FOOLS!!!! From ross.jett at gmail.com Wed Apr 1 13:35:15 2009 From: ross.jett at gmail.com (Ross) Date: Wed, 1 Apr 2009 10:35:15 -0700 (PDT) Subject: league problem in python Message-ID: I'm new to programming and have chosen Python as my first language. I've gone through Allen Downey's Think Python book and I think I'm ready to dive into a project. The first problem I've chosen to tackle is a problem I have seen at my tennis club. Each spring/fall, the pro puts out a sheet of paper for people to sign up for tennis leagues. Depending on how many people sign up for a league, he'll assign a certain number of courts each week to that league. After this, he makes up a schedule of who plays who on each week and who has a bye. Unfortunately, he does this by hand and a lot of times, some people will play certain people more than once and certain other people never. Other problems that arise: some people have more bye weeks than others, some people have all their bye weeks clumped together so that they don't play for weeks on end. I would like to create a simple program where the pro could enter in how many people were in the league, the number of courts available, and the number of weeks the schedule would run and then generate a schedule where everybody played everybody else once and got the same number of bye weeks, preferably spaced out evenly. How should I go about starting this problem...I'm feel like this is a really simple problem, but I'm having writer's/coder's block. Can you guys help? From castironpi at gmail.com Wed Apr 1 13:40:51 2009 From: castironpi at gmail.com (Aaron Brady) Date: Wed, 1 Apr 2009 10:40:51 -0700 (PDT) Subject: Thoughts on language-level configuration support? References: <36148830-22c0-4f19-ab23-d04d8755ab4a@s28g2000vbp.googlegroups.com> <01e0d6e4$0$19301$c3e8da3@news.astraweb.com> <62a06f4c-82e3-4a32-b6e2-ad69ec6f4ac0@f32g2000vbf.googlegroups.com> Message-ID: <4442f26b-58a9-4db7-85f6-82221d44fab8@b6g2000pre.googlegroups.com> On Apr 1, 6:29?am, jfager wrote: > On Apr 1, 3:29?am, Kay Schluehr wrote: > > > > "Discoverable", as in built-in tools that let you have the following > > > conversation: ?"Program, tell me all the things I can configure about > > > you" - "Okay, here they all are". ?No digging through the source > > > required. > > > But this doesn't have any particular meaning. If I run a dir(obj) > > command all attributes of obj will be returned and I can be sure these > > are all. In C# I can reflect on attributes of an assembly which is a > > well defined entity. "Program" is not an entity. It's kind of a user > > interface name in StarTreck ( which prefers "computer" for this > > purpose though ). This way we cannot design systems. > > "Module and transitive graph of other modules (constructed through > 'import' statements), tell me all the things I can configure about > you". ?Is that a little clearer? Using shelve, which I suggested earlier, you have to rely on modules to choose the options they want to make available. You could require them to store configs. on a per-module basis in separate files, or require them to use separate namespaces in the shelf. It could also be possible to interface with the target process's garbage collector, but you're limited to objects you can identify in the list of tracked objects, and only the ones that are mutable at that. From jjposner at snet.net Wed Apr 1 13:44:01 2009 From: jjposner at snet.net (John Posner) Date: Wed, 01 Apr 2009 13:44:01 -0400 Subject: Which is more Pythonic? (was: Detecting Binary content in files) In-Reply-To: <917541bf-1751-4b83-b570-25804eb63cda@f19g2000yqo.googlegroups.com> References: <917541bf-1751-4b83-b570-25804eb63cda@f19g2000yqo.googlegroups.com> Message-ID: <22294518AFC948EC85085431537B7707@AMDUP> >> > ? ?mrkrs_alt2 = filter(lambda b: b > 127 or b in list("\r\n\t"), block) >> > >> >> Never tested my 'pythonicity', but I would do: >> >> def test(b) : b > 127 or b in r"\r\n\t" Oops! Clearly, b in "\r\n\t" .... is preferable to ... b in list("\r\n\t") You do *not* want to use a raw string here: >>> len ("\n\r\t") 3 >>> len (r"\n\r\t") 6 E-mail message checked by Spyware Doctor (6.0.0.386) Database version: 5.12090 http://www.pctools.com/en/spyware-doctor-antivirus/ From mensanator at aol.com Wed Apr 1 13:47:27 2009 From: mensanator at aol.com (Mensanator) Date: Wed, 1 Apr 2009 10:47:27 -0700 (PDT) Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <1ej5t4930m29h0f6ttpdcd83t08q2q3sab@4ax.com> <85ca8f71-e5a7-4594-9ec4-ae85f4ba0cc0@r31g2000prh.googlegroups.com> <91t6t4hfjicgvdrcgkhdjfro3ko3ktum8l@4ax.com> Message-ID: On Apr 1, 9:08?am, Lada Kugis wrote: > On Wed, 1 Apr 2009 00:40:17 -0700 (PDT), Carl Banks > > > > > > wrote: > > >Lada, > > >I am also an engineer, and I can tell your idea of intuitive is not > >universal, even among engineers. ?I certainly do not lean toward one- > >based indexing. > > >From a programming standpoint--and remember Python is a programming > >language--zero-based indexing eliminates the need for a whole lot of > >extra +1s and -1s when indexing, slicing, and iterating, a lot more > >than it causes, and that is worth the "cost". ?This might not be > >apparent to you if you never tried seriously taking advantage of > >indexing from zero, or if your programming experience is very narrow. > >These both seem to be true for you, so you'll just have to take my > >word for it. > > You have repeated several cs based points already stated. But apart > from a programming standpoint - could you give a few examples, where > "on paper" (as to avoid stepping into "programmer's territory") zero > indexing could be more intuitive ? Here's an easy example: Standard Positional Number Systems. 765 in octal is 7 * 8**2 + 6 * 8**1 + 5 * 8**0 123 in decimal is 1 * 10**2 + 2 * 10**1 + 3 * 10**0 666 in hexadecimal is 6 * 16**2 + 6 * 16**1 + 6 * 16**0 0-based indexing is kinda important. > (of course, taking into account your previous based calculations, > which are based on 1 indexing - I imagine you still use matrices with > a11 as a first element) > > Lada- Hide quoted text - > > - Show quoted text - From mensanator at aol.com Wed Apr 1 13:52:10 2009 From: mensanator at aol.com (Mensanator) Date: Wed, 1 Apr 2009 10:52:10 -0700 (PDT) Subject: double/float precision question References: Message-ID: On Apr 1, 12:13?pm, TP wrote: > Hi everybody, > > Try the following python statements: > > >>> "%.40f" % 0.2222222222222222222222222222222 > > '0.2222222222222222098864108374982606619596'>>> float( 0.2222222222222222222222222222222) > > 0.22222222222222221 > > It seems the first result is the same than the following C program: > ################ > #include > > int main(void) > { > ? ? double a = 0.2222222222222222222222222222222; > > ? ? printf( "%.40f\n", a ); > ? ? return 0;} > > ################# > > My problem is the following: > * the precision "40" (for example) is given by the user, not by the > programmer. > * I want to use the string conversion facility with specifier "e", that > yields number is scientific format; so I cannot apply float() on the result > of "%.40e" % 0.2222222222222222222222222222222, I would lost the scientific > format. > > Is there any means to obtain the full C double in Python, or should I limit > the precision given by the user (and used in "%.*e") to the one of a Python > float? You can get arbitrary precision floats from the gmpy module. > > Thanks in advance > > Julien > > -- > python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.\ > 9&1+,\'Z4(55l4('])" > > "When a distinguished but elderly scientist states that something is > possible, he is almost certainly right. When he states that something is > impossible, he is very probably wrong." (first law of AC Clarke) From bearophileHUGS at lycos.com Wed Apr 1 13:57:46 2009 From: bearophileHUGS at lycos.com (bearophileHUGS at lycos.com) Date: Wed, 1 Apr 2009 10:57:46 -0700 (PDT) Subject: league problem in python References: Message-ID: <4763a0ca-90de-4b90-aa00-ac0e0c953365@c36g2000yqn.googlegroups.com> Ross: > How should I go about starting this problem...I'm feel like this is a > really simple problem, but I'm having writer's/coder's block. Can you > guys help? There are refined ways to design a program, but this sounds like a simple and small one, so you probably don't need much formal things to solve it. So start writing down: 1) A precise description of what results you want 2) A precise description of what data you have to find those answers 2b) You may want to think about what kind of user interface to give to your program, but being this program very small and being you not much experienced yet, you may think about this later too (see point 9). Then: 3) Invent the simplest way to feed that data to the program, for example reading a TXT file. 4) Create artificially some toy data files, very short, and able to reveal several of the corner cases of your problem. Then start programming in a test-driven way. Write a tiny program that just reads and decodes the input data of one of your inputs and shows them. 5) Once that is done, you may write some outputs, that is the result of each of those little inputs. 6) Try to slowly grow the code to go toward the solutions you look for. 7) If the algorithm and processing required to find the solutions isn't simple, then you may want to step away from the computer, take a graphite pencil, eraser and paper and invent how to solve the problem generally. 7b) sometimes you may also need to invent how to represent data and solutions (and even intermediate stages) into your small program. 8) Keep creating more complex test cases and look if their solution is correct. If it's wrong then debug your program. Soon your purpose will be to invent stranger and stranger corner cases able to break your program (that make your program give a wrong answer). 9) Eventually you will have to think to improve the user interface. In bigger programs this has to be done after point (2), but for your small program you may do it now too. This programming strategy is not good enough if you are writing big programs or very difficult ones, but for small easy programs it's more than enough. Bye, bearophile From pecora at anvil.nrl.navy.mil Wed Apr 1 14:14:47 2009 From: pecora at anvil.nrl.navy.mil (Lou Pecora) Date: Wed, 01 Apr 2009 14:14:47 -0400 Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <1ej5t4930m29h0f6ttpdcd83t08q2q3sab@4ax.com> <85ca8f71-e5a7-4594-9ec4-ae85f4ba0cc0@r31g2000prh.googlegroups.com> <91t6t4hfjicgvdrcgkhdjfro3ko3ktum8l@4ax.com> Message-ID: In article <91t6t4hfjicgvdrcgkhdjfro3ko3ktum8l at 4ax.com>, Lada Kugis wrote: > On Wed, 1 Apr 2009 00:40:17 -0700 (PDT), Carl Banks > wrote: > > > > >Lada, > > > >I am also an engineer, and I can tell your idea of intuitive is not > >universal, even among engineers. I certainly do not lean toward one- > >based indexing. > > > >From a programming standpoint--and remember Python is a programming > >language--zero-based indexing eliminates the need for a whole lot of > >extra +1s and -1s when indexing, slicing, and iterating, a lot more > >than it causes, and that is worth the "cost". This might not be > >apparent to you if you never tried seriously taking advantage of > >indexing from zero, or if your programming experience is very narrow. > >These both seem to be true for you, so you'll just have to take my > >word for it. > > > You have repeated several cs based points already stated. But apart > from a programming standpoint - could you give a few examples, where > "on paper" (as to avoid stepping into "programmer's territory") zero > indexing could be more intuitive ? This has become a moving target. I thought your original complaint was about Python (the programming language) vs. Fortran (the programming language) and C (the programming language used in an odd way). -- -- Lou Pecora From davea at ieee.org Wed Apr 1 14:25:08 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 01 Apr 2009 13:25:08 -0500 Subject: Creating a Python Module - Available Implementations In-Reply-To: References: Message-ID: <49D3B184.4070902@ieee.org> A python source file *is* a module. And you import it the same way you import any of the system modules. Just use the basename, without the .py extension. import mylib The only catch is where you locate this module. When you first write it, just put it in the same directory as your scripts, and it'll automatically be found. Then when you're using it from scripts that aren't all located in the same directory, move it to a place on the sys.path. Finally, when you have several related modules, you can make them into a package. A package is a directory located on the path, that contains afiloe wit the special name __init__.py There's some description needed for each of these steps, but just knowing what they are should be enough for now. Besides, the description varies by python version. The only needs to create a C-based module would be if 1) performance is an overriding consideration 2) or it's already written in C 3) or it needs access to some system facility that's not easily available directly from Python. Even then, you probably would write a bit of C "glue," and the rest in Python. ntwrkd wrote: > I have been browsing through creating a Python module for common > custom functions that I frequently use, but I am wondering, is this > the best method, and is it necessary? > Really all I need is to import functions from another plaintext Python > source file, how might I do this? > What would merit the need to create a C-based Python module? > > Thanks in advance > > From nick at craig-wood.com Wed Apr 1 14:30:05 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Wed, 01 Apr 2009 13:30:05 -0500 Subject: A design problem I met again and again. References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> <5e6ad73b-837a-4b0a-89a7-0f0392b60dd7@k19g2000prh.googlegroups.com> Message-ID: ??? wrote: > But I think the first step to resolve a problem is to describe it. In > that way, I might find the answer myself :-) That is a great saying! To answer your original question, split your code up into sections that can be tested independently. If you can test code in a isolated way then it belongs in a class / module of its own. If you have a class that is too big, then factor independent classes out of it until it is the right size. That is easier said than done and may require some creativity on your part. It will pay dividends though as the level of abstraction in your program will rise. I've noticed some programmers think in big classes and some think in small classes. Train yourself to do the other thing and your programming will improve greatly! -- Nick Craig-Wood -- http://www.craig-wood.com/nick From ptmcg at austin.rr.com Wed Apr 1 14:37:03 2009 From: ptmcg at austin.rr.com (Paul McGuire) Date: Wed, 1 Apr 2009 11:37:03 -0700 (PDT) Subject: List of paths References: Message-ID: <1e64ca55-2ef0-430a-b992-b16099381b5b@a7g2000yqk.googlegroups.com> On Apr 1, 3:57?am, Nico Grubert wrote: > Dear Python developers > > I have the following (sorted) list. > ['/notebook', > ? '/notebook/mac', > ? '/notebook/mac/macbook', > ? '/notebook/mac/macbookpro', > ? '/notebook/pc', > ? '/notebook/pc/lenovo', > ? '/notebook/pc/hp', > ? '/notebook/pc/sony', > ? '/desktop', > ? '/desktop/pc/dell', > ? '/desktop/mac/imac', > ? '/server/hp/proliant', > ? '/server/hp/proliant/385', > ? '/server/hp/proliant/585' > ] > > I want to remove all paths x from the list if there is a path y in the > list which is part of x so y.startswith(x) is true. > > The list I want to have is: > ['/notebook', '/desktop', '/server/hp/proliant'] > > Any idea how I can do this in Python? > > Thanks in advance > Nico paths = ['/notebook', '/notebook/mac', '/notebook/mac/macbook', '/notebook/mac/macbookpro', '/notebook/pc', '/notebook/pc/lenovo', '/notebook/pc/hp', '/notebook/pc/sony', '/desktop', '/desktop/pc/dell', '/desktop/mac/imac', '/server/hp/proliant', '/server/hp/proliant/385', '/server/hp/proliant/585' ] seen = set() basepaths = [ seen.add(s) or s for s in paths if not any(s.startswith(ss) for ss in seen) ] gives: ['/notebook', '/desktop', '/server/hp/proliant'] -- Paul From davea at ieee.org Wed Apr 1 14:48:34 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 01 Apr 2009 13:48:34 -0500 Subject: double/float precision question In-Reply-To: References: Message-ID: <49D3B702.2010801@ieee.org> It's not at all clear what you really want. You say you want to "use" the %e format, but then imply you're then going to turn it back into a float. Since I don't know what the end goal is, I'll just comment generally. All Python floating point is equivalent to the 'double' type of the C implementation. That may vary by C compiler, or by processor, but what you see is typical of an 8 byte double type. I don't really remember my values, but it's around 16 digits of precision. Anything displayed beyond those digits is noise, and shouldn't be considered reproducible. In other words, those two results are the same, the rounding is just different. To put it simply, in Python, float() on a floating point number does not reduce the precision in the least. As for having the user specify the number of digits he/she wants, that just means you have to construct the format string in a variable. Something like: digits = 14 (this you'd have gotten from a user) format = "%." + str(digits) + "e" result = format % number #this does the formatting TP wrote: > Hi everybody, > > Try the following python statements: > > >>>> "%.40f" % 0.2222222222222222222222222222222 >>>> > '0.2222222222222222098864108374982606619596' > >>>> float( 0.2222222222222222222222222222222) >>>> > 0.22222222222222221 > > It seems the first result is the same than the following C program: > ################ > #include > > int main(void) > { > double a = 0.2222222222222222222222222222222; > > printf( "%.40f\n", a ); > return 0; > } > ################# > > My problem is the following: > * the precision "40" (for example) is given by the user, not by the > programmer. > * I want to use the string conversion facility with specifier "e", that > yields number is scientific format; so I cannot apply float() on the result > of "%.40e" % 0.2222222222222222222222222222222, I would lost the scientific > format. > > Is there any means to obtain the full C double in Python, or should I limit > the precision given by the user (and used in "%.*e") to the one of a Python > float? > > Thanks in advance > > Julien > > From locoyfeo at gmail.com Wed Apr 1 14:55:32 2009 From: locoyfeo at gmail.com (El Loco) Date: Wed, 1 Apr 2009 11:55:32 -0700 (PDT) Subject: [Ann] New super python vm Message-ID: Hi all, This is to announce that right after a few weeks after our first coding sprint, our project, "Unswallowed-snot", has already achieved substantial results. In our tests, runtime performance shows a 150x slowdown. This is due mainly to our lead developer (myself) still not knowing enough python, but we expect the situation improves, or not, in the next couple of months. Interested hackers, please drop me an email. Thanks! l&f From rt8396 at gmail.com Wed Apr 1 15:09:04 2009 From: rt8396 at gmail.com (r) Date: Wed, 1 Apr 2009 12:09:04 -0700 (PDT) Subject: league problem in python References: Message-ID: On Apr 1, 12:35?pm, Ross wrote: [snip] > How should I go about starting this problem...I'm feel like this is a > really simple problem, but I'm having writer's/coder's block. Can you > guys help? Ross, I highly disagree with bear on this. What you have here is a 90 percent math problem and a 10 percent code problem. First get the numbers to add up correctly and then code that sucker up! From rt8396 at gmail.com Wed Apr 1 15:12:24 2009 From: rt8396 at gmail.com (r) Date: Wed, 1 Apr 2009 12:12:24 -0700 (PDT) Subject: New super python vm References: Message-ID: On Apr 1, 1:55?pm, El Loco wrote: > Hi all, > > This is to announce that right after a few weeks after our first > coding sprint, > our project, "Unswallowed-snot", has already achieved substantial > results. > In our tests, runtime performance shows a 150x slowdown. > This is due mainly to our lead developer (myself) still not knowing > enough python, > but we expect the situation improves, or not, in the next couple of > months. > > Interested hackers, please drop me an email. > Thanks! > l&f Sounds good, but do you have binaries yet? because i can't compile from source distros. From martin.hellwig at dcuktec.org Wed Apr 1 15:14:59 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Wed, 01 Apr 2009 20:14:59 +0100 Subject: A design problem I met again and again. In-Reply-To: <5e6ad73b-837a-4b0a-89a7-0f0392b60dd7@k19g2000prh.googlegroups.com> References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> <5e6ad73b-837a-4b0a-89a7-0f0392b60dd7@k19g2000prh.googlegroups.com> Message-ID: ??? wrote: > But I think the first step to resolve a problem is to describe it. In > that way, I might find the answer myself That is an excellent approach, knowing you have a problem and describing it is actually the hardest part of a design, the rest is more like a puzzle. What I guess so far is that you tried to (re)design your work by grouping on functionality and using classes for more clearer work. From what you wrote (that is if I understood you correctly), both of these approaches don't really seem to get 'there'. It might be worth to try another approach, instead of focussing on the characteristics of the functions and using them as a guideline for your design you could try this: Step 1: Write a Functional Design from a user perspective, restrain yourself from implying anything technical or choosing specific tools. Imagine yourself as an end-user and not as a developer. Pick a random person of the street that looks literate but is not working in IT (secretaries are usually great for this!), let them comment on your language and then quiz them about the content to see if they actually understood what you wrote. If commenting on language seems strange, in my experience if I can't properly describe what I want to say then there is a good chance that I haven't thought about it sufficiently or I was lazy in describing it. Step 2: Take this functional design and write a functional specification. This is much like the design but instead focusses on the business processes and interdependencies of these. Write out implied constraints and things you might think is obvious, although the specification are technical in nature you should still avoid naming specific tools unless it is to describe functionality, i.e. google like approach of indexing data. Use plain English (or whatever language you want to write it in) for this, don't use any diagrams, SQL table layouts, UML etc. Pick a random IT related colleague (network administrators are usually my preferred choice), let them read it and quiz them to make sure the specification are clear enough. Step 3: When you have your functional specification, write a technical design. Here you make a choice on the tools you are going to use based on evidence based research and describe the general outline of your solution. Pour your co-worker a nice cup of beverage of their choice and let them read it and of course quiz them. Step 4: Finally, use the technical design for writing a technical specification. Design you program using UML (or whatever thing that makes you look like you are developing without writing code). Specify deep, down to the name of all 'public' functions. Step 5: Let it rest for the weekend. Step 6: Reread your technical specification, if it still makes sense, continue. If it doesn't, go back to step 1 and repeat the process with the changes you made. Step 7: Do what you usually do (I write my unit-tests first and then solve them). Step 8: Compare the end product with your original functional design. If they do not align go back to Step 1. Some hints I found useful during step 4. I try to take in account that it is not me who is going to develop it but a team of reasonable qualified developers. Thus I split up the work in parts that can be simultaneously done by more then one person without them needing to know exactly what the other one is doing. If there is a need to know what the other developer is doing then the specification was not precise enough. If during the whole process something comes up that shows a better way, change your documentation accordingly. When all of this still results in an 'ugly' design, try letting more people read your documentation, if that doesn't help then one or more of the following may apply: - Despite of its ugliness it is the most elegant design possible. - You are working on something that is fundamentally broken. - You haven't met the person that can give you more insight. YMMV -- mph From mynthon1 at gmail.com Wed Apr 1 15:17:13 2009 From: mynthon1 at gmail.com (mynthon) Date: Wed, 1 Apr 2009 12:17:13 -0700 (PDT) Subject: imp.load_source() - explanation needed Message-ID: Hi! I need help. I don't understand what doc says. I load module from path testmod/mytest.py using imp.load_source(). My code is import imp testmod = imp.load_source('koko', 'testmod/mytest.py) print testmod but i don't understand whatt is first (name) argument for. Docs says that "The name argument is used to create or access a module object." But i still don't understand. I cant do this import imp testmod = imp.load_source('koko', 'testmod/mytest.py) print koko How i can access module object using its name? Is " print sys.modules ['koko'] " only way or am i missing something? From simon.hibbs at gmail.com Wed Apr 1 15:35:30 2009 From: simon.hibbs at gmail.com (Simon Hibbs) Date: Wed, 1 Apr 2009 12:35:30 -0700 (PDT) Subject: Converting a PIL image object to a buffer Message-ID: I'm trying to dump a snapshot of my application window to the clipboard. I can use ImageGrab in PIL to get the screen data into a PIL image object, which i have converted to a bitmap using ImageWin, but when I try to pass this to the clipboard using - win32clipboard.SetClipboardData(win32clipboard.CF_BITMAP, img) It fails, telling be that "The object must support the buffer interface". How can I convert a PIL image into a buffer object? I can't find any clues. Help appreciated, Simon Hibbs From daku9999 at gmail.com Wed Apr 1 16:20:14 2009 From: daku9999 at gmail.com (daku9999 at gmail.com) Date: Wed, 1 Apr 2009 13:20:14 -0700 (PDT) Subject: Stripping non-numbers from a file parse without nested lists? References: <34f50d37-b96b-4ef0-b4ca-f2c471f4c4db@w35g2000prg.googlegroups.com> <4b42a91c-b871-401e-b2b8-097e8087a32d@f1g2000prb.googlegroups.com> <647caf7a-ffad-4da9-a9f7-33c3d11c9af1@o6g2000yql.googlegroups.com> Message-ID: <2d3b6059-a569-48d2-b486-0e78b3564196@q30g2000prq.googlegroups.com> On Apr 1, 8:10?am, jay logan wrote: > On Apr 1, 11:05?am, jay logan wrote: > > > > > On Apr 1, 2:35?am, daku9... at gmail.com wrote: > > > > On Mar 31, 6:47?pm, "Rhodri James" > > > wrote: > > > > > What you're doing (pace error checking) seems fine for the data > > > > structures that you're using. ?I'm not entirely clear what your usage > > > > pattern for "dip" and "dir" is once you've got them, so I can't say > > > > whether there's a more appropriate shape for them. ?I am a bit curious > > > > though as to why a nested list is non-ideal? > > > > > ... > > > > ? ? ?if "/" in word and "dip" not in word: > > > > ? ? ? ? dip_n_dir.append(word.split("/", 1)) > > > > > is marginally shorter, and has the virtue of making it harder to use > > > > unrelated dip and dir values together. > > > > > -- > > > > Rhodri James *-* Wildebeeste Herder to the Masses > > > > Rhodri, > > > > Thanks. ?That works better than what I had before and I learned a new > > > method of parsing what I was looking for. > > > > Now I'm on to jumping a set number of lines from a given positive > > > search match: > > > > ...(lines of garbage)... > > > 5656 ? ? ?(or some other value I want, but don't explicitly know) > > > ...(18 lines of garbage)... > > > search object > > > ...(lines of garbage)... > > > > I've tried: > > > > def read_poles(filename): > > > ? index = 0 > > > ? fh = None > > > ? try: > > > ? ? ? fh = open(filename, "r") > > > ? ? ? lines=fh.readlines() > > > ? ? ? while True: > > > > ? ? ? ? ? if "search object" in lines[index] > > > ? ? ? ? ? ? ? poles = int(lines[index-18]) > > > ? ? ? ? ? ? ? print(poles) > > > > ? ? ? ? ? index +=1 > > > > ? except(IndexError): pass > > > > ? finally: > > > ? ? ? if fh is not None: # close file > > > ? ? ? ? ? fh.close() > > > > ------------------ > > > > Which half works. ?If it's not found, IndexError is caught and passed > > > (avoids quitting on lines[index out of range]. ?The print(poles) > > > properly displays the value I am looking for (_always_ 18 lines before > > > the search object). > > > > However, since it is assigned using the index variable, the value of > > > poles doesn't keep (poles is always zero when referenced outside of > > > the read_poles function). ?I'm assuming because I'm pointing to a > > > certain position of an object and once index moves on, it no longer > > > points to anything valid. ?My python book suggested using > > > copy.deepcopy, but that didn't get around the fact I am calling it on > > > (index-18). > > > > Any experience jumping back (or forward) a set number of lines once a > > > search object is found? ?This is the only way I can think of doing it > > > and it clearly has some problems. > > > > Reading the file line by line using for line in blah works for finding > > > the search object, but I can't see a way of going back the 18 lines to > > > grabbing what I need. > > > > Thanks for the help! ?I'm slowly getting this mangled mess of a file > > > into something automated (hand investigating the several thousand > > > files I need to do would be unpleasant). > > > # You could try using a deque holding 18 lines and search using that > > deque > > # This is untested, but here's a try (>=Python 3.0) > > from collections import deque > > import itertools as it > > import sys > > > def read_poles(filename): > > ? ? with open(filename) as f: > > ? ? ? ? line_iter = iter(f) > > ? ? ? ? d = deque(it.islice(line_iter,17), maxlen=18) > > > ? ? ? ? for line in line_iter: > > ? ? ? ? ? ? d.append(line) > > > ? ? ? ? ? ? if 'search object' in line: > > ? ? ? ? ? ? ? ? poles = int(d[0]) > > ? ? ? ? ? ? ? ? print(poles) > > ? ? ? ? ? ? ? ? return poles > > ? ? ? ? else: > > ? ? ? ? ? ? print('No poles found in', filename, file=sys.err) > > Notice that I returned the "pole" from the function so you could catch > the return value as follows: > pole = read_poles(filename) > > if pole is None: > ? ? # no poles found > else: > ? ? print('Function returned this pole:', pole) > > If you need a list of poles, then return a list: > > def read_poles(filename): > ? ? all_poles = [] > ? ? with open(filename) as f: > ? ? ? ? line_iter = iter(f) > ? ? ? ? d = deque(it.islice(line_iter,17), maxlen=18) > > ? ? ? ? for line in line_iter: > ? ? ? ? ? ? d.append(line) > > ? ? ? ? ? ? if 'search object' in line: > ? ? ? ? ? ? ? ? all_poles.append(int(d[0])) > ? ? return all_poles > > ... > poles = read_poles(filename) > > if poles: > ? ? print('Here are the poles:\n', '\n'.join(map(str,poles))) > else: > ? ? print('There were no poles found in', filename) I think I found an easier (if possibly uglier way) of doing it: for filenames in files.split(): try: fh = open(filenames.replace("/","\\"),"r") lines=fh.readlines() except(IOError) as err: print(filename, err) finally: if fh is not None: fh.close() print(read_poles4(lines)) ... which opens my file (always < 10 megs) into the list lines def read_poles4(lines): try: poles = lines[(lines.index("Poles Plotted\n") - 18)].rstrip() return poles except ValueError as err: return err ... Seems like the simpler solution, at least for small files where I can hold the entire thing in memory. Thanks! From nick at craig-wood.com Wed Apr 1 16:30:05 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Wed, 01 Apr 2009 15:30:05 -0500 Subject: double/float precision question References: Message-ID: TP wrote: > Hi everybody, > > Try the following python statements: > > >>> "%.40f" % 0.2222222222222222222222222222222 > '0.2222222222222222098864108374982606619596' > >>> float( 0.2222222222222222222222222222222) > 0.22222222222222221 > > It seems the first result is the same than the following C program: > ################ > #include > > int main(void) > { > double a = 0.2222222222222222222222222222222; > > printf( "%.40f\n", a ); > return 0; > } > ################# > > My problem is the following: > * the precision "40" (for example) is given by the user, not by the > programmer. > * I want to use the string conversion facility with specifier "e", that > yields number is scientific format; so I cannot apply float() on the result > of "%.40e" % 0.2222222222222222222222222222222, I would lost the scientific > format. > > Is there any means to obtain the full C double in Python, or should I limit > the precision given by the user (and used in "%.*e") to the one of a Python > float? Python floats are actually C doubles (as you proved yourself with your little test program). Eg >>> 1.+2.**-52 1.0000000000000002 >>> 1.+2.**-53 1.0 Indicating that python floats have about 52 bits of precision, so are definitely what C calls doubles. When you do >>> float( 0.2222222222222222222222222222222) 0.22222222222222221 Python prints as many decimal places as are significant in the answer. This is covered in the FAQ http://www.python.org/doc/faq/general/#why-are-floating-point-calculations-so-inaccurate If you want more precision use the built in decimal module or the third party gmpy module. -- Nick Craig-Wood -- http://www.craig-wood.com/nick From betianakr at hotmail.com Wed Apr 1 16:34:24 2009 From: betianakr at hotmail.com (Betiana Krancenblum) Date: Wed, 1 Apr 2009 17:34:24 -0300 Subject: Demographic Information about Python Message-ID: Hi, I'm looking for statistical information about where Python is beeing used as a programming language and where it is teached as a language for beginner programmers. Where do you think I can find that information? Thanks, Betiana _________________________________________________________________ ?Quer?s cuidarte y estar bien? Conoc? MSN Salud y Bienestar http://salud.latam.msn.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From marduk at letterboxes.org Wed Apr 1 16:34:30 2009 From: marduk at letterboxes.org (Albert Hopkins) Date: Wed, 01 Apr 2009 16:34:30 -0400 Subject: imp.load_source() - explanation needed In-Reply-To: References: Message-ID: <1238618070.29774.10.camel@blackwidow.nbk> On Wed, 2009-04-01 at 12:17 -0700, mynthon wrote: > Hi! > > I need help. I don't understand what doc says. > > I load module from path testmod/mytest.py using imp.load_source(). My > code is > > import imp > testmod = imp.load_source('koko', 'testmod/mytest.py) > print testmod > > but i don't understand whatt is first (name) argument for. Docs says > that "The name argument is used to create or access a module object." > But i still don't understand. I cant do this > > import imp > testmod = imp.load_source('koko', 'testmod/mytest.py) > print koko > I think that importing and loading are seperate steps. So either >>> koko = imp.load_source('koko', 'testmod/mytest.py') or >>> imp.load_source('koko', 'testmod/mytest.py') >>> import koko From gherron at islandtraining.com Wed Apr 1 16:43:58 2009 From: gherron at islandtraining.com (Gary Herron) Date: Wed, 01 Apr 2009 13:43:58 -0700 Subject: Converting a PIL image object to a buffer In-Reply-To: References: Message-ID: <49D3D20E.8040907@islandtraining.com> Simon Hibbs wrote: > I'm trying to dump a snapshot of my application window to the > clipboard. I can use ImageGrab in PIL to get the screen data into a > PIL image object, which i have converted to a bitmap using ImageWin, > but when I try to pass this to the clipboard using - > > win32clipboard.SetClipboardData(win32clipboard.CF_BITMAP, img) > > It fails, telling be that "The object must support the buffer > interface". > > How can I convert a PIL image into a buffer object? I can't find any > clues. > PIL images have a tostring method that returns a string containing all the pixel data. Would that help you to either create the needed buffer? Or perhaps you could by-pass the need for a buffer, and just use the byte string. Gary Herron > Help appreciated, > > Simon Hibbs > > > -- > http://mail.python.org/mailman/listinfo/python-list > From mailing at supai.de Wed Apr 1 16:51:52 2009 From: mailing at supai.de (Wolfgang Forstmeier) Date: Wed, 01 Apr 2009 22:51:52 +0200 Subject: py2exe problem Message-ID: Hey list, what kind of error do I have with getting this error at starting my app. Im am not using IdleConf.GetOption right now. Warning: configHandler.py - IdleConf.GetOption - problem retrieving configration option 'name' from section 'Keys'. returning default value: '' From locoyfeo at gmail.com Wed Apr 1 16:55:20 2009 From: locoyfeo at gmail.com (locoyfeo at gmail.com) Date: Wed, 1 Apr 2009 13:55:20 -0700 (PDT) Subject: New super python vm References: Message-ID: <472a6f97-56a9-477b-9859-b33740d0ac33@h28g2000yqd.googlegroups.com> > Sounds good, but do you have binaries yet? because i can't compile > from source distros. Binaries?? Hmm... ok, I filed a bug. From clp2 at rebertia.com Wed Apr 1 16:58:56 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 1 Apr 2009 13:58:56 -0700 Subject: Demographic Information about Python In-Reply-To: References: Message-ID: <50697b2c0904011358v72ba0526ibe289bf575a4c10d@mail.gmail.com> 2009/4/1 Betiana Krancenblum : > Hi, > > I'm looking for statistical information about where Python is beeing used as > a programming language and where it is teached as a language for beginner > programmers. > Where do you think I can find that information? I don't think there are any statistics per se, but MIT tried (is trying?) Python for its CS intro course - http://www.amk.ca/diary/2006/11/mit_to_try_python_for_introduc.html Cheers, Chris -- I have a blog: http://blog.rebertia.com From jafo at tummy.com Wed Apr 1 17:10:17 2009 From: jafo at tummy.com (Sean Reifschneider) Date: Wed, 1 Apr 2009 15:10:17 -0600 Subject: Alpha/For Discussion: RPMs for around 3,000 PyPI packages. Message-ID: <20090401211017.GA20606@tummy.com> At PyCon I got this idea in my head to try building packages for everything in the cheeseshop. I've had some success, getting over 3,000 packages built on Fedora 10 (and fewer on CentOS 5 and fewer still on 4). This is out of 6176 packages. I've made these initial packages, which should be considered no better than *ALPHA* quality) available at: http://10tons.tummy.com/pypi/ If you are a package maintainer, or otherwise want to look at why a package failed to build, you can get the build output at: http://10tons.tummy.com/pypi-output/ At this point I'm looking for feedback on the packages as they are built, and looking to resolve issues found with these packages. The future will hopefully also include builds of Debian packages for at least Debian and Ubuntu, probably on stable and possibly on the latest release as well. USER NOTES ========== These packages are built simply by doing "python setup.py bdist_rpm". There are packages for both i386 and x86_64. Dependencies are likely incomplete if not incorrect, and we'll probably have to come up with a way of the package maintainers specifying the build and install dependencies. So at this point you will probably have to manually chase dependencies. Sorry about that. PACKAGE MAINTAINER NOTES ======================== If you maintain a package on the cheeseshop (http://pypi.python.org/pypi), there are a few things you should know about this repository: You can see the build output of all packages at: http://10tons.tummy.com/pypi-output/ Currently the builds are not automated. This is one of the first things I want to fix, but I will need to set up a secure way of rebuilding packages (so that one package can't compromise another, for example). The current packages were built with a 30 or 60 second time limit on the build. Some packages, if they looked like they were going well and just died, may have died because of the time limit. This time limit will probably be extended in the future. The packages are built basically just by doing "python setup.py bdist_rpm". There are some base set of packages installed on the system, but currently it doesn't do anything about installing dependencies that packages need which are not currently installed. We will probably need a way to specify this, on a per-distribution basis possibly, for both building and installing. I would like to have some community discussion on this. I rely on your source distribution being available either on the pypi.python.org machine (via the "python setup.py upload" command), or that the download link in PyPI point directly at a tar or zip file (.tar, .tar.gz, .tar.bz2, .tgz, .zip extensions). If your download link points at a generic download landing page, my programs can't find your code. I'm open to discussion on this, but in discussions I've had so far I've had no objections to the requirement that the source be on the pypi server. I'm hoping that package maintainers and I can work together to get maintainers the information they need to tweak their packages so that the build system can do the right thing without too many special cases on a package-by-package basis. I appreciate any review and thoughts you have. Feel free to either discuss it on the python mailing list or via e-mail to jafo-community at tummy.com Thanks, Sean -- "Never interrupt your enemy when he is making a mistake." -- Napoleon Bonaparte Sean Reifschneider, Member of Technical Staff tummy.com, ltd. - Linux Consulting since 1995: Ask me about High Availability From andrew at acooke.org Wed Apr 1 17:17:37 2009 From: andrew at acooke.org (andrew cooke) Date: Wed, 1 Apr 2009 17:17:37 -0400 (CLT) Subject: Alpha/For Discussion: RPMs for around 3,000 PyPI packages. In-Reply-To: <20090401211017.GA20606@tummy.com> References: <20090401211017.GA20606@tummy.com> Message-ID: Sean Reifschneider wrote: > At PyCon I got this idea in my head to try building packages for > everything > in the cheeseshop. I've had some success, getting over 3,000 packages > built on Fedora 10 (and fewer on CentOS 5 and fewer still on 4). This is > out of 6176 packages. neat idea. the info for each package includes information on which python versions it is compatible with. wouldn't it make sense to use that? might explain a lot of failures. andrew From arnodel at googlemail.com Wed Apr 1 17:32:37 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Wed, 01 Apr 2009 22:32:37 +0100 Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> Message-ID: Lada Kugis writes: > I'm coming from fortran and c background so I'm certainly biased by > them. But if you could explain one thing to me: > > in fortran for example: > for i=1,n > goes from 1,2,3,4,...,n > > in python for example: > for i in range(1,n) > goes from 1,2,3,4,...,n-1 > (that is, it goes from 1 up to, but not including n) > > Why is that so ? What were the reasons for that "not including" part ? > It troubles me greatly, and I cannot see it's advantages over the > "standard" "up to and including" n. > > Best regards > Lada Luckily Python allows you to create your own indexing on lists: def dec(i): if isinstance(i, slice): return slice(dec(i.start), dec(i.stop), i.step) elif i is None or i < 0: return i else: return i - 1 defop = """ def __%sitem__(s,i,*r): val = list.__%sitem__(s,dec(i),*r) if isinstance(i, slice): val = List1(val) return val def __%sslice__(s,i,j,*r): return List1(list.__%sslice__(s,dec(i),dec(j),*r)) """ class List1(list): for op in 'del', 'get', 'set': exec defop % (op, op, op, op) def index(self, x): return list.index(self, x) + 1 def insert(self, i, x): list.insert(self, dec(i), x) def pop(self, i=None): return list.pop() if i is None else list.pop(dec(i)) for op in 'add', 'mul', 'radd', 'rmul': exec "def __%s__(*r): return List1(list.__%s__(*r))" % (op, op) l1 = List1(range(10)) l2 = List1("Python rules") I'll let you play with l1 and l2. -- Arnaud PS. What day is it again? From steve at REMOVE-THIS-cybersource.com.au Wed Apr 1 17:34:40 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 01 Apr 2009 21:34:40 GMT Subject: double/float precision question References: Message-ID: <01e3d243$0$20654$c3e8da3@news.astraweb.com> On Wed, 01 Apr 2009 19:13:33 +0200, TP wrote: > Hi everybody, > > Try the following python statements: > >>>> "%.40f" % 0.2222222222222222222222222222222 > '0.2222222222222222098864108374982606619596' >>>> float( 0.2222222222222222222222222222222) > 0.22222222222222221 Remove the leading quote from the first one, and you'll see that the two numbers look pretty similar: 0.2222222222222222098864108374982606619596 0.22222222222222221 By the way, calling float(0.2222...2) is redundant, because 0.222...2 is already a float. Calling float again just wastes CPU cycles, because the same object is returned again. >>> x = 0.2222222222222222222222222222222222 >>> x is float(x) # check for object identity (same memory address) True We can see that floats have more precision than they display by default: >>> x 0.22222222222222221 >>> x - 0.2222222222222222 == 0 # Sixteen of digit 2 True >>> x - 0.222222222222222 # Fifteen of digit 2 2.2204460492503131e-16 Notice that doing this reveals more significant digits than were apparent from just printing x. > My problem is the following: > * the precision "40" (for example) is given by the user, not by the > programmer. > * I want to use the string conversion facility with specifier "e", that > yields number is scientific format; so I cannot apply float() on the > result of "%.40e" % 0.2222222222222222222222222222222, I would lost the > scientific format. No, this is confused. The float you create is the exact same object whether you use scientific format or not. >>> a = 0.0123 >>> b = 1.23e-2 >>> a == b True >>> a 0.0123 >>> b 0.0123 *All* floats contain mantissa and an exponent, but in binary, not decimal: >>> math.frexp(a) (0.78720000000000001, -6) >>> 0.78720000000000001 * 2**-6 0.0123 > Is there any means to obtain the full C double in Python Floats in Python *are* C doubles. -- Steven From sjmachin at lexicon.net Wed Apr 1 17:39:15 2009 From: sjmachin at lexicon.net (John Machin) Date: Wed, 1 Apr 2009 14:39:15 -0700 (PDT) Subject: Detecting Binary content in files References: <6bea512a-e1c8-4279-9620-21ea64a49400@u9g2000pre.googlegroups.com> Message-ID: On Apr 1, 4:59?pm, Dennis Lee Bieber wrote: > On Tue, 31 Mar 2009 14:26:08 -0700 (PDT), ritu > declaimed the following in > gmane.comp.python.general: > > > > > if ( ( -B $filename || > > ? ? ? ? ? ?$filename =~ /\.pdf$/ ) && > > ? ? ? ? ?-s $filename > 0 ) { > > ? ? ? ? return(1); > > ? ? } > > ? ? ? ? According to my old copy of the Camel, -B only reads the "first > block" of the file. If the block contains a , or if ~30% of the > block contains bytes >127 or from some (undefined) set of control > characters (that is, I expect it does not count , , , , > , maybe some others)... So... Not sure whether this is meant to be rough pseudocode or an April 1 "jeu d'esprit" or ... > > def isbin(fid): > ? ? ? ? fin = open(fid, "r") (1) mode = "rb" might be better > ? ? ? ? block = fin.read(1024) ?#what is the size of a "block" these days > ? ? ? ? binary = "\0" in block > ? ? ? ? if not binary: > ? ? ? ? ? ? ? ? mrkrs = [b for b in block > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if b > 127 (2) [assuming Python 2.x] b is a str object; change 127 to "\x3f" > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? or b in [ "\r", "\n", "\t" ] ? ? ?] ? ? ? #add needed (3) surely you mean "b not in" (4) possible improvements on ["\r", etc etc] : (4a) use tuple ("\r", etc etc) (4b) use string "\r\n\t" (you don't really want to build that list from scratch for each byte tested, do you?) > ? ? ? ? ? ? ? ? binary = (float(len(mrkrs)) / len(block)) > 0.30 > ? ? ? ? fin.close() > ? ? ? ? return binary Cheers, John From steve at REMOVE-THIS-cybersource.com.au Wed Apr 1 17:57:56 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 01 Apr 2009 21:57:56 GMT Subject: TIOBE programming language community index Message-ID: <01e3d7b7$0$20654$c3e8da3@news.astraweb.com> The TIOBE programming community index has some interesting data this month. http://www.tiobe.com/content/paperinfo/tpci/index.html * The top three languages, C, C++ and Java between them have a combined rating approaching 50%; * Python has increased popularity over the last year, from position #7 to #6, with more than a 5% rating; * Ruby has remained steady at position 11 over the last year; * But Perl has dropped from #6 to #9; * Much to my happy surprise, Pascal and Delphi are both in the top twenty, at positions 16 and 10 respectively; * Despite the extremely high profile of Lisp and Scheme on discussion lists, it only manages to reach position 23 on the TIOBE index. This should put to rest the fears of certain people that Python is being abandoned in droves for Ruby. Now they can complain that we're not doing enough to overthrow the harsh tyranny of Java and C. -- Steven From pavlovevidence at gmail.com Wed Apr 1 17:58:55 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 1 Apr 2009 14:58:55 -0700 (PDT) Subject: A design problem I met again and again. References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> Message-ID: <33e08fde-1d4e-41a2-9ce2-7e1bec3abd90@r29g2000vbp.googlegroups.com> On Apr 1, 12:44 am, ??? wrote: > I got the same problem when writing C#/C++ when I have to provide a > lot of method to my code's user. So I create a big class as the entry > point of my code. Although these big classes doesn't contains much > logic, they do grow bigger and bigger. This seems to be a classic result of "code-based organization", that is, you are organizing your code according to how your functions are used. That's appropriate sometimes. Procedural libraries are often organized by grouping functions according to use. The os module is a good example. However, it's usually much better to organize code according to what data it acts upon: "data-based organization". In other words, go though your big class and figure out what data belongs together conceptually, make a class for each conceptual set of data, then assign methods to classes based on what data the methods act upon. Consider the os module again. It's a big collection of functions, but there are a group of functions is os that all act on a particular piece of data, namely a file descriptor. This suggests tha all the functions that act upon file descriptors (os.open, os.close, os.seek, etc.) could instead be methods of a single class, with the file descriptor as a class member. (Note: the os library doesn't do that because functions like os.open are supposed to represent low-level operations corresponding to the underlying system calls, but never mind that. Ordinarily a bunch of functions operating on common data should be organized as a class.) Carl Banks From callen314 at gmail.com Wed Apr 1 18:07:42 2009 From: callen314 at gmail.com (Craig Allen) Date: Wed, 1 Apr 2009 15:07:42 -0700 (PDT) Subject: Beazley on Generators References: Message-ID: this is great, thanks... we have used generators to create something akin to a cooperative tasking environment... not to implement multitasking, but to be able to control low level data processing scripts. These scripts, written as generators, yield control to a control loop which then can pause, resume, abort, or change the state of shared context objects which the script uses as it's input and output space. E.g. the control loop can see there is intermediate output which an operator (managing a data reduction pipeline) might want to see. I can see from the first few slide I need to understand this. It already seems clear that there are ways to improve our approach to what we have done, though the overall approach is solid and works well. anyway thanks. From sjmachin at lexicon.net Wed Apr 1 18:13:03 2009 From: sjmachin at lexicon.net (John Machin) Date: Wed, 1 Apr 2009 15:13:03 -0700 (PDT) Subject: Detecting Binary content in files References: <6bea512a-e1c8-4279-9620-21ea64a49400@u9g2000pre.googlegroups.com> Message-ID: <7922b376-ef69-4fd4-a837-87a82cd879fc@y6g2000prf.googlegroups.com> On Apr 2, 8:39?am, John Machin wrote: > On Apr 1, 4:59?pm, Dennis Lee Bieber wrote: > > > > > On Tue, 31 Mar 2009 14:26:08 -0700 (PDT), ritu > > declaimed the following in > > gmane.comp.python.general: > > > > if ( ( -B $filename || > > > ? ? ? ? ? ?$filename =~ /\.pdf$/ ) && > > > ? ? ? ? ?-s $filename > 0 ) { > > > ? ? ? ? return(1); > > > ? ? } > > > ? ? ? ? According to my old copy of the Camel, -B only reads the "first > > block" of the file. If the block contains a , or if ~30% of the > > block contains bytes >127 or from some (undefined) set of control > > characters (that is, I expect it does not count , , , , > > , maybe some others)... So... > > Not sure whether this is meant to be rough pseudocode or an April 1 > "jeu d'esprit" or ... > > > > > def isbin(fid): > > ? ? ? ? fin = open(fid, "r") > > (1) mode = "rb" might be better > > > ? ? ? ? block = fin.read(1024) ?#what is the size of a "block" these days > > ? ? ? ? binary = "\0" in block > > ? ? ? ? if not binary: > > ? ? ? ? ? ? ? ? mrkrs = [b for b in block > > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if b > 127 > > (2) [assuming Python 2.x] > b is a str object; change 127 to "\x3f" Gah ... it must be gamma rays from outer space! Trying again: change 127 to "\x7f" (and actually "\x7e" would be a better choice) > > > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? or b in [ "\r", "\n", "\t" ] ? ? ?] ? ? ? #add needed > > (3) surely you mean "b not in" take 2: surely you mean ... or b < "\x20" and b not in "\r\n\t" and at that stage the idea of making a set of chars befor entering the loop has some attraction :-) From simon.hibbs at gmail.com Wed Apr 1 18:20:43 2009 From: simon.hibbs at gmail.com (Simon Hibbs) Date: Wed, 1 Apr 2009 15:20:43 -0700 (PDT) Subject: Converting a PIL image object to a buffer References: Message-ID: <79ddf944-2fee-46c5-aed7-e6276dce88ed@s19g2000vbp.googlegroups.com> On 1 Apr, 21:43, Gary Herron wrote: > Simon Hibbs wrote: > > I'm trying to dump a snapshot of my application window to the > > clipboard. I can use ImageGrab in PIL to get the screen data into a > > PIL image object, which i have converted to a bitmap using ImageWin, > > but when I try to pass this to the clipboard using - > > > win32clipboard.SetClipboardData(win32clipboard.CF_BITMAP, img) > > > It fails, telling be that "The object must support the buffer > > interface". > > > How can I convert a PIL image into a buffer object? I can't find any > > clues. > > PIL images have a tostring method that returns a string containing all > the pixel data. ?Would that help you to either create the needed > buffer? ?Or perhaps you could by-pass the need for a buffer, and just > use the byte string. If I use tostring I get a string which I can put on the clipboard, but it isn't any kind of image. I can make a PIL image from the string but them I'm back to square one again. I suppse I could save the image object to a real file and then send that to the clipboard, but that seems wasteful and I'd have to worry about what to call it and where to put it. Much neater if I could just create it in memory somehow. Simon Hibbs From sjmachin at lexicon.net Wed Apr 1 18:22:23 2009 From: sjmachin at lexicon.net (John Machin) Date: Wed, 1 Apr 2009 15:22:23 -0700 (PDT) Subject: Which is more Pythonic? (was: Detecting Binary content in files) References: Message-ID: On Apr 2, 2:10?am, John Posner wrote: > Dennis Lee Bieber presented a code snippet with two consecutive statements > that made me think, "I'd code this differently". So just for fun ... is > Dennis's original statement or my "_alt" statement more idiomatically > Pythonic? Are there even more Pythonic alternative codings? > > ? ?mrkrs = [b for b in block > ? ? ?if b > 127 > ? ? ? ?or b in [ "\r", "\n", "\t" ] ? ? ? ] I'd worry about "correct" before "Pythonic" ... see my responses to Dennis in the original thread. > > ? ?mrkrs_alt1 = filter(lambda b: b > 127 or b in [ "\r", "\n", "\t" ], > block) > ? ?mrkrs_alt2 = filter(lambda b: b > 127 or b in list("\r\n\t"), block) Try this on and see if it fits: num_bin_chars = sum(b > "\x7f" or b < "\x20" and b not in "\r\n\t" for b in block) > (Note: Dennis's statement converts a string into a list; mine does not.) What is list("\r\n\t") doing, if it's not (needlessly) converting a string into a list? > --- > > ? ?binary = (float(len(mrkrs)) / len(block)) > 0.30 > > ? ?binary_alt = 1.0 * len(mrkrs) / len(block) > 0.30 > num_bin_chars > 0.30 * len(block) (no mucking about with float() or 1.0, and it doesn't blow up on a zero-length block) Cheers, John From pavlovevidence at gmail.com Wed Apr 1 18:23:03 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 1 Apr 2009 15:23:03 -0700 (PDT) Subject: Beazley on Generators References: Message-ID: <16621f3c-2712-4da2-a33b-adf93e388044@e2g2000vbe.googlegroups.com> On Mar 31, 10:03?pm, Terry Reedy wrote: > At PyCon2008, David Beazley presented an excellent talk on generators. > Generator Tricks for Systems Programmershttp://www.dabeaz.com/generators/index.html > > At PyCon2009, he followed up with another talk on more advanced > generator usage, which Guido commended on the python-ideas list: > A Curious Course on Coroutines and Concurrencyhttp://dabeaz.com/coroutines/ > > I have just started (this one will take more than one sitting ;-) but it > looks just as good. Yet another great thing about Python. The ability to run coroutines in Matlab would make my working life a lot easier right now. Carl Banks From Scott.Daniels at Acm.Org Wed Apr 1 18:29:05 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Wed, 01 Apr 2009 15:29:05 -0700 Subject: List of paths In-Reply-To: References: Message-ID: Nico Grubert wrote: > Dear Python developers... I have the following (sorted) list.... > > I want to remove all paths x from the list if there is a path y in the > list which is part of x so y.startswith(x) is true. > > The list I want to have is: > ['/notebook', '/desktop', '/server/hp/proliant'] > > Any idea how I can do this in Python? > > Thanks in advance > Nico Here's a tricky case that doesn't show up in your example: In each case above, the directory names are distinct. how about: ['/desk', '/desk/ethanallen', '/desk/ikea', '/desktop', /desktop/pc', '/desktop/mac'] Should the answer be ['/desk'] or ['/desk', '/desktop'] ? --Scott David Daniels Scott.Daniels at Acm.Org From pavlovevidence at gmail.com Wed Apr 1 18:35:36 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 1 Apr 2009 15:35:36 -0700 (PDT) Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> Message-ID: <5a87e703-6737-46c2-b215-52123a870c3c@l13g2000vba.googlegroups.com> On Apr 1, 2:32?pm, Arnaud Delobelle wrote: > Lada Kugis writes: > > I'm coming from fortran and c background so I'm certainly biased by > > them. But if you could explain one thing to me: > > > in fortran for example: > > for i=1,n > > goes from 1,2,3,4,...,n > > > in python for example: > > for i in range(1,n) > > goes from 1,2,3,4,...,n-1 > > (that is, it goes from 1 up to, but not including n) > > > Why is that so ? What were the reasons for that "not including" part ? > > It troubles me greatly, and I cannot see it's advantages over the > > "standard" "up to and including" n. > > > Best regards > > Lada > > Luckily Python allows you to create your own indexing on lists: > > def dec(i): > ? ? if isinstance(i, slice): > ? ? ? ? return slice(dec(i.start), dec(i.stop), i.step) > ? ? elif i is None or i < 0: > ? ? ? ? return i > ? ? else: > ? ? ? ? return i - 1 > > defop = """ > def __%sitem__(s,i,*r): > ? ? val = list.__%sitem__(s,dec(i),*r) > ? ? if isinstance(i, slice): val = List1(val) > ? ? return val > def __%sslice__(s,i,j,*r): > ? ? return List1(list.__%sslice__(s,dec(i),dec(j),*r)) > """ > > class List1(list): > ? ? for op in 'del', 'get', 'set': > ? ? ? ? exec defop % (op, op, op, op) > ? ? def index(self, x): > ? ? ? ? return list.index(self, x) + 1 > ? ? def insert(self, i, x): > ? ? ? ? list.insert(self, dec(i), x) > ? ? def pop(self, i=None): > ? ? ? ? return list.pop() if i is None else list.pop(dec(i)) > ? ? for op in 'add', 'mul', 'radd', 'rmul': > ? ? ? ? exec "def __%s__(*r): return List1(list.__%s__(*r))" % (op, op) > > l1 = List1(range(10)) > l2 = List1("Python rules") > > I'll let you play with l1 and l2. If I were your boss and you ever pulled something like this, your ass would be so fired. This is unforgiveable, not only changing the indexing semantics of Python (because a user would have NO CLUE that something underlying has been changed, and thus it should never be done), but also for the needless abuse of exec. Carl Banks From ldo at geek-central.gen.new_zealand Wed Apr 1 18:37:46 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Thu, 02 Apr 2009 11:37:46 +1300 Subject: Beazley on Generators References: <13298fc5-5024-4343-bf5a-7e271a08d806@o11g2000yql.googlegroups.com> Message-ID: In message <13298fc5-5024-4343- bf5a-7e271a08d806 at o11g2000yql.googlegroups.com>, Michele Simionato wrote: > However, there are situations when you need thousands of lightweight > threads of execution ;;; The Linux kernel has been tested running hundreds of thousands of threads. From rhodri at wildebst.demon.co.uk Wed Apr 1 18:59:01 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Wed, 01 Apr 2009 23:59:01 +0100 Subject: how to handle/generate pcap file In-Reply-To: <2c6e2640-0229-4886-bf22-d62636d9e13a@a5g2000pre.googlegroups.com> References: <2c6e2640-0229-4886-bf22-d62636d9e13a@a5g2000pre.googlegroups.com> Message-ID: On Wed, 01 Apr 2009 14:53:34 +0100, Evan wrote: > > Hello - > > I'm trying to decode the pcap file which is packet capture by tcpdump > or wireshark. Is there a python module that I can use it for this > problem? > > Can python-libpcap or pycap or dpkt do that? A quick browse of the pypcap website suggests that yes, it can. -- Rhodri James *-* Wildebeeste Herder to the Masses From rhodri at wildebst.demon.co.uk Wed Apr 1 19:17:18 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Thu, 02 Apr 2009 00:17:18 +0100 Subject: python for loop In-Reply-To: References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <72i5t4tgfo2h4gd6ggcs02flkca85kgoku@4ax.com> <0dbc9665-3ee8-4d62-8e9e-75c6d700076e@f1g2000prb.googlegroups.com> Message-ID: On Wed, 01 Apr 2009 15:12:27 +0100, Lada Kugis wrote: > On 01 Apr 2009 08:06:28 GMT, Steven D'Aprano > wrote: > >> >> There are advantages and disadvantages to both systems, but on balance, >> I >> think that zero-based is a better system for programming, and one-based >> for natural language. > > Nicely put. > > Yes, along with some of your other arguments, I think I can agree that > this sums it up best. I'll just have to adapt myself to natural > language thinking at one side, and programming thinking at the other. I always think it's sad that the concept of "zero" arrived too late to influence our fundamentally latinate language for ordinal numbers. (In other words, don't go thinking that there's anything logical about natural language :-) -- Rhodri James *-* Wildebeeste Herder to the Masses From rhodri at wildebst.demon.co.uk Wed Apr 1 20:07:42 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Thu, 02 Apr 2009 01:07:42 +0100 Subject: Thoughts on language-level configuration support? In-Reply-To: References: <36148830-22c0-4f19-ab23-d04d8755ab4a@s28g2000vbp.googlegroups.com> <01e0d6e4$0$19301$c3e8da3@news.astraweb.com> <62a06f4c-82e3-4a32-b6e2-ad69ec6f4ac0@f32g2000vbf.googlegroups.com> Message-ID: On Wed, 01 Apr 2009 05:15:19 +0100, jfager wrote: > On Mar 31, 10:44 pm, "Rhodri James" wrote: >> [...] What >> restrictions can be put on the value you get back? What can the >> help system say about this, or do we have to go back to doing all >> that by hand? Now translate all those questions into the very >> different environment of a config file. Repeat with a database, >> and all it's quirks. By the time your colossus has acquired >> enough parameters to at least hint at the desirable answers to >> these questions, you've effectively duplicated the interfaces to >> all of the config mechanisms you're trying to replace and you've >> still lost a whole heap of flexibility. > Yes, you're right, the code that actually injects the configuration > isn't trivial. I never intended to imply that it was. But it would > probably only have to be written once (people would only write their > own if they had a special need). The win is that the underlying code > doesn't have to change just because the end-user configuration format > did. On the contrary, because the configurable items can be introduced pretty much anywhere in module, class or function, the code that injects the configuration ends up having to be written over and over and over again. None of the questions I asked are rocket science, most of them apply to all configurables differently, and none of them can be interpolated from the name being assigned to the object produced by the config and the default. This is not going to be a win. -- Rhodri James *-* Wildebeeste Herder to the Masses From tjreedy at udel.edu Wed Apr 1 20:12:30 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 01 Apr 2009 20:12:30 -0400 Subject: Matrix operations on character matrix element? In-Reply-To: <159284.7265.qm@web53611.mail.re2.yahoo.com> References: <159284.7265.qm@web53611.mail.re2.yahoo.com> Message-ID: olusina eric wrote: > I hope somebody will be able to help me here. > I am trying to solve some physical problems that will require the > generation of some function in terms of some parameters. These functions > are derived from matrix operation on ?characters?. Are there ways > numpy/scipy perform matrix operations on characters? > > For example A = matrix([[a, b,c],[d,e,f],[1,2,3]]) > > B = matrix([[g,h,4],[I,j,5],[k,l,6]]) A to l are identifiers, not characters, and must be bound to objects for the above to make any sense. Did you mean 'a' to 'l'? > Is it possible to perform operations like A*B or A+B > > And most especially: linalg.solve(A,eye(3,3)) If you mean, operate on symbols to do symbolic computation, the same way one might with paper and pencil, the simple answer is no. Numpy is not a computer algebra system. Computer algebra requires that one define classes such as Symbol, with all the usual arithmetic operations. I am not sure whether numpy algorithms can work on arrays of instances of user-defined classes such as this. tjr From steven at REMOVE.THIS.cybersource.com.au Wed Apr 1 20:14:37 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 02 Apr 2009 00:14:37 GMT Subject: Beazley on Generators References: <13298fc5-5024-4343-bf5a-7e271a08d806@o11g2000yql.googlegroups.com> Message-ID: On Thu, 02 Apr 2009 11:37:46 +1300, Lawrence D'Oliveiro wrote: > In message <13298fc5-5024-4343- > bf5a-7e271a08d806 at o11g2000yql.googlegroups.com>, Michele Simionato > wrote: > >> However, there are situations when you need thousands of lightweight >> threads of execution ;;; > > The Linux kernel has been tested running hundreds of thousands of > threads. Did it pass or fail that test? -- Steven From tjreedy at udel.edu Wed Apr 1 20:19:50 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 01 Apr 2009 20:19:50 -0400 Subject: Demographic Information about Python In-Reply-To: References: Message-ID: Betiana Krancenblum wrote: > Hi, > > I'm looking for statistical information about where Python is beeing > used as a programming language and where it is teached as a language for > beginner programmers. > Where do you think I can find that information? There is some info at python.org. Ask on the edu-sig list for more about educational usage. You might look at how information is gather for http://www.tiobe.com/content/paperinfo/tpci/index.html From robert.kern at gmail.com Wed Apr 1 20:27:22 2009 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 01 Apr 2009 19:27:22 -0500 Subject: Matrix operations on character matrix element? In-Reply-To: References: <159284.7265.qm@web53611.mail.re2.yahoo.com> Message-ID: On 2009-04-01 19:12, Terry Reedy wrote: > olusina eric wrote: >> I hope somebody will be able to help me here. >> I am trying to solve some physical problems that will require the >> generation of some function in terms of some parameters. These >> functions are derived from matrix operation on ?characters?. Are there >> ways numpy/scipy perform matrix operations on characters? >> >> For example A = matrix([[a, b,c],[d,e,f],[1,2,3]]) >> B = matrix([[g,h,4],[I,j,5],[k,l,6]]) > > A to l are identifiers, not characters, and must be bound to objects for > the above to make any sense. Did you mean 'a' to 'l'? > >> Is it possible to perform operations like A*B or A+B >> >> And most especially: linalg.solve(A,eye(3,3)) > > If you mean, operate on symbols to do symbolic computation, the same way > one might with paper and pencil, the simple answer is no. Numpy is not a > computer algebra system. > > Computer algebra requires that one define classes such as Symbol, with > all the usual arithmetic operations. I am not sure whether numpy > algorithms can work on arrays of instances of user-defined classes such > as this. They can't. -- 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 cosmo_general at yahoo.com Wed Apr 1 20:55:52 2009 From: cosmo_general at yahoo.com (Muddy Coder) Date: Wed, 1 Apr 2009 17:55:52 -0700 (PDT) Subject: Help for Toplevel Message-ID: Hi Folks, I have a problem of handling Toplevel window. Basically, I wrote a listbox viewer with scrollbars, and saved in file listbo.py. Then in my main GUI window, with menu, I need to launch the listbox viewer, in a new window. Obviously, a Toplevel window is needed. But, I failed at passing parameters over to Toplevel window. Please take a look at my code: Listbox viewer: class ScrolledList(Frame): def __init__(self, options, parent=None): Frame.__init__(self, parent) self.pack(expand=YES, fill=BOTH) self.makeWidgets(options) In my main GUI: from XXX import ScrolledList class Foo: def __init__(self): ........ def call_listbox(self, params): new = Toplevel() alist = ['foor','bar'] ScrolledList(new,alist) With the code above, the widgets did not show on the Toplevel window, and the data list ['foo','bar'] did not show up either. Can somebody help me on it? Thanks a lot! Muddy Coder From debatem1 at gmail.com Wed Apr 1 20:56:01 2009 From: debatem1 at gmail.com (CTO) Date: Wed, 1 Apr 2009 17:56:01 -0700 (PDT) Subject: Thoughts on language-level configuration support? References: <36148830-22c0-4f19-ab23-d04d8755ab4a@s28g2000vbp.googlegroups.com> <5411b35b-56d7-4fd1-af0b-f1fe70143c20@h28g2000yqd.googlegroups.com> <781bf317-b269-41a0-a82b-0ef79a4f4064@s20g2000yqh.googlegroups.com> <6b45e790-fb60-4aca-a6a6-1cc5e491d801@c9g2000yqm.googlegroups.com> Message-ID: <85772d96-6508-4110-b033-c4f81c7d9ff3@z19g2000yqe.googlegroups.com> >?I just mean that there should be a > clear and easy way to do it, that it should be considered a basic > service, and that if the best way to satisfy all the goals is to > integrate it directly into the language, that shouldn't be shied away > from. Honestly, the programming language and the configuration languages should never, ever, ever be inseparable. I don't see a reason for it and I see some great reasons not to do it. > The example that I have on my blog post, I consider that 'syntax', > even though it's implemented as a function, mainly just because it > digs into the bytecode and modifies the normal way a function is > evaluated (the function's value is determined by where the output > would go). I don't particularly see why this is needed. To my mind the strongest argument you are making, by far, is the "discoverable" argument. If you were able to build a system which permitted other components to discover configuration options and give configurable levels of permanence and visibility to them, that would be great. If you were able to do it and make it easy for a programmer to interact with, you would most definitely have a module I would use. But I daresay you're going to have to build it (or at least mock it up) before you're going to get a lot of folks jumping on the bandwagon, and its a *long* slog before you hit the level of support and stability you're going to need to convince folks to trust their precious config files to it. From castironpi at gmail.com Wed Apr 1 20:58:59 2009 From: castironpi at gmail.com (Aaron Brady) Date: Wed, 1 Apr 2009 17:58:59 -0700 (PDT) Subject: pygame and socket.recv Message-ID: <1c6574dd-1bfb-4c45-a744-58761e5d1f06@k2g2000yql.googlegroups.com> Hi, I tried writing a small game on a pygame layer. The graphics are fine, and at the moment, it is not graphics intensive. It is multi- player, and for the communication, I am sending a pickle string across a LAN, once per frame. I'm observing some latency. It seems that socket.recv isn't performing consistently. The server is using time.clock and time.sleep to keep the frame rate at 40 frames per second. Latency occurred regardless of whether the connection was ethernet or wireless, although worse with wireless. Does anyone have any hints or suggestions? I am on Windows XP. From tim.wintle at teamrubber.com Wed Apr 1 21:28:19 2009 From: tim.wintle at teamrubber.com (Tim Wintle) Date: Thu, 02 Apr 2009 02:28:19 +0100 Subject: pygame and socket.recv In-Reply-To: <1c6574dd-1bfb-4c45-a744-58761e5d1f06@k2g2000yql.googlegroups.com> References: <1c6574dd-1bfb-4c45-a744-58761e5d1f06@k2g2000yql.googlegroups.com> Message-ID: <1238635700.9829.10.camel@tim-laptop> On Wed, 2009-04-01 at 17:58 -0700, Aaron Brady wrote: > I tried writing a small game on a pygame layer. The graphics are > fine, and at the moment, it is not graphics intensive. It is multi- > player, and for the communication, I am sending a pickle string across > a LAN, once per frame. > > I'm observing some latency. It seems that socket.recv isn't > performing consistently. Not sure I understand the question, are you blocking for the data to come down the network before rendering the next frame? For game programming I've always used select with non-blocking sockets to receive data - and kept the transmissions to UDP to save time (obviously you have to expect some data to be lost). Wire time is always going to have too much latency for a message to be happily passed within the time it takes to render a frame. For syncing time I use a really simple algorithm - both machines send each other their local [game] time every few seconds, and if the received time is ahead of the local time then the receiving machine updates it's time to match - that way they are always out by at most the shortest time it takes for a packet to travel from one to the other. Tim Wintle From castironpi at gmail.com Wed Apr 1 21:45:08 2009 From: castironpi at gmail.com (Aaron Brady) Date: Wed, 1 Apr 2009 18:45:08 -0700 (PDT) Subject: pygame and socket.recv References: <1c6574dd-1bfb-4c45-a744-58761e5d1f06@k2g2000yql.googlegroups.com> Message-ID: <2084b37d-485c-435f-be5c-8ee1bc901d70@p11g2000yqe.googlegroups.com> On Apr 1, 8:28?pm, Tim Wintle wrote: > On Wed, 2009-04-01 at 17:58 -0700, Aaron Brady wrote: > > I tried writing a small game on a pygame layer. ?The graphics are > > fine, and at the moment, it is not graphics intensive. ?It is multi- > > player, and for the communication, I am sending a pickle string across > > a LAN, once per frame. > > > I'm observing some latency. ?It seems that socket.recv isn't > > performing consistently. > > Not sure I understand the question, are you blocking for the data to > come down the network before rendering the next frame? > > For game programming I've always used select with non-blocking sockets > to receive data - and kept the transmissions to UDP to save time > (obviously you have to expect some data to be lost). Wire time is always > going to have too much latency for a message to be happily passed within > the time it takes to render a frame. > > For syncing time I use a really simple algorithm - both machines send > each other their local [game] time every few seconds, and if the > received time is ahead of the local time then the receiving machine > updates it's time to match - that way they are always out by at most the > shortest time it takes for a packet to travel from one to the other. > > Tim Wintle My game loop looks like this: poll events, get 1 at most send to server wait for server reply render entire frame Yes, I am blocking for the data to come down the network. Unfortunately, if I use any "prediction," I will have to go back and un-render the previous frame, then redraw with the new information. 40 transmissions per second in each way can't be too much to ask, it's just that they have to alternate, up one, down one. I don't understand your solution. I can't picture it for my favorite RTS game or the one I'm writing. Are you saying that the slower machine just jumps ahead, and its user just doesn't have the opportunity to make moves on the omitted frames? I am using TCP, socket.SOCK_STREAM. UDP is a potential solution, but it still doesn't fix my main loop. From xdicry at gmail.com Wed Apr 1 21:59:12 2009 From: xdicry at gmail.com (Evan) Date: Wed, 1 Apr 2009 18:59:12 -0700 (PDT) Subject: how to handle/generate pcap file References: <2c6e2640-0229-4886-bf22-d62636d9e13a@a5g2000pre.googlegroups.com> Message-ID: <6d880039-fcc8-4b14-9972-6c60230ca865@u9g2000pre.googlegroups.com> On Apr 2, 6:59?am, "Rhodri James" wrote: > On Wed, 01 Apr 2009 14:53:34 +0100, Evan wrote: > > > Hello - > > > I'm trying to decode thepcapfilewhich is packet capture by tcpdump > > or wireshark. ? Is there a python module that I can use it for this > > problem? > > > Can python-libpcap or pycap or dpkt do that? > > A quick browse of the pypcap website suggests that yes, it can. > > -- > Rhodri James *-* Wildebeeste Herder to the Masses Yap, I found that dpkt can do this, Thanks all. Evan From amoradi at fedoraproject.org Wed Apr 1 21:59:42 2009 From: amoradi at fedoraproject.org (Armin) Date: Wed, 1 Apr 2009 22:59:42 -0300 Subject: python needs leaning stuff from other language In-Reply-To: <333edbe80904021840t15cf8801p933b780242950b97@mail.gmail.com> References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <333edbe80904021840t15cf8801p933b780242950b97@mail.gmail.com> Message-ID: <200904012259.43394.amoradi@fedoraproject.org> On Thursday 02 April 2009 22:40:08 Zac Burns wrote: > Is it really worth it to not implement list.clear and answer this > question over and over again? > > I see no reason that a list shouldn't have a .clear method. > > -- > Zachary Burns > (407)590-4814 > Aim - Zac256FL > Production Engineer (Digital Overlord) > Zindagi Games > > On Thu, Apr 2, 2009 at 5:32 PM, Esmail wrote: > > Emile van Sebille wrote: > >> Esmail wrote: > >>> Diez B. Roggisch wrote: > >>>> online.service at ymail.com schrieb: > >>>>> python's list needs a thing list.clear() like c# arraylist > >>>>> and > >>>> > >>>> some_list[:] = [] > >>> > >>> I agree that this is nice and clear, but as a relative newbie > >>> wouldn't > >>> > >>> some_list = [] > >> > >> This is different -- it creates a new list. Consider: > >> > >> >>> some_list = [1,2,3] > >> >>> d = some_list > >> >>> d[1] > >> 2 > >> >>> some_list[:] = ['a','b','c'] > >> >>> d[1] > >> 'b' > >> >>> some_list = [1,2,3] > >> >>> d[1] > >> 'b' > >> > >> the [:] form allows references into the list to remain valid while the > >> direct assignment dopes not. > > > > Ah .. thanks for clarifying this .. makes sense. > > > > Also, thank you Luis for your post. > > > > Esmail > > > > -- > > http://mail.python.org/mailman/listinfo/python-list > > -- > http://mail.python.org/mailman/listinfo/python-list A .clear() method wouldn't be beneficial: del mylist[:] -- Armin Moradi From http Wed Apr 1 22:02:48 2009 From: http (Paul Rubin) Date: 01 Apr 2009 19:02:48 -0700 Subject: Beazley on Generators References: <13298fc5-5024-4343-bf5a-7e271a08d806@o11g2000yql.googlegroups.com> Message-ID: <7xljqjn77b.fsf@ruckus.brouhaha.com> Lawrence D'Oliveiro writes: > > However, there are situations when you need thousands of lightweight > > threads of execution ;;; > > The Linux kernel has been tested running hundreds of thousands of threads. Those are still heavyweight threads requiring context switches to switch from one to another. If you look at the multi-threading benchmarks in the Alioth shootout, languages with lightweight threads (such as Erlang or GHC) can switch orders of magnitude faster than those that use kernel threads. From steven at REMOVE.THIS.cybersource.com.au Wed Apr 1 22:20:20 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 02 Apr 2009 02:20:20 GMT Subject: Thoughts on language-level configuration support? References: <36148830-22c0-4f19-ab23-d04d8755ab4a@s28g2000vbp.googlegroups.com> Message-ID: On Mon, 30 Mar 2009 06:40:00 -0700, jfager wrote: > The basic idea is that a language could offer syntactic support for > declaring configurable points in the program. The language system would > then offer an api to allow the end user to discover a programs > configuration service, as well as a general api for providing > configuration values. Completely coincidentally, a colleague forwarded me this cartoon about exposing program internals as the user interface: http://www.ok-cancel.com/comic/4.html I thought it was amusing, because I've seen programs just like that. Yes, even "GUI applications", where the developer thought that creating a user interface was exposing the values of internal variables to the user to modify directly. How does your proposal differ from that? -- Steven From ldo at geek-central.gen.new_zealand Wed Apr 1 22:26:35 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Thu, 02 Apr 2009 15:26:35 +1300 Subject: Python Goes Mercurial References: <2972356f-a2df-4dc5-9071-4ac8518e128b@j18g2000prm.googlegroups.com> Message-ID: In message , Tim Daneliuk wrote: > ,,, when I suggested that better open source tools existed, they kindly > explained their complete lack of interest in moving several millions > of lines of code to anything new. What was their explanation? From ldo at geek-central.gen.new_zealand Wed Apr 1 22:27:25 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Thu, 02 Apr 2009 15:27:25 +1300 Subject: Python Goes Mercurial References: <7x7i2499si.fsf@ruckus.brouhaha.com> <7a1dd0d8-1978-470b-a80d-57478d7f7f9e@q16g2000yqg.googlegroups.com> Message-ID: In message <7a1dd0d8-1978-470b- a80d-57478d7f7f9e at q16g2000yqg.googlegroups.com>, Paul Boddie wrote: > And I've heard stories of "bait and > switch" with Git: "you can do XYZ with Git but not with ..." followed > by the discovery that you can't realistically do XYZ with Git, either. Cite? From dunwitch at gmail.com Wed Apr 1 22:30:28 2009 From: dunwitch at gmail.com (Dunwitch) Date: Wed, 1 Apr 2009 19:30:28 -0700 (PDT) Subject: Display directory pyqt4 and Python Message-ID: I've looked around for the answer and have decided to ask an expert for the solution. Whats suppose to happen is a user pushes a button and displays the directory content in the text edit window on the gui. Everything works, apart from the fact that it only shows the last file in the directory, not a full listing. Here is the code for reference. Any help would be appreciated, I'm missing something simple. -- Dunwitch # File : Video Search import sys, os, win32net from PyQt4 import QtGui, QtCore from findVideos import Ui_MainWindow # UI Files from QT Designer # New class derived from QMainWindow class TestApp(QtGui.QMainWindow): def __init__(self): QtGui.QMainWindow.__init__(self) self.ui = Ui_MainWindow() self.ui.setupUi(self) # Connect the signals andslots QtCore.QObject.connect(self.ui.findFiles,QtCore.SIGNAL("clicked ()"), self.showVideoFiles) #----------------------------- Code In Question --------------------------------- def showVideoFiles(self): enc7 = ('\\\\1.2.3.4\\somefolder') fileList=os.listdir(enc7) for x in (fileList): self.ui.displayVideo.setText(x) # This only shows the last file in the directory? #------------------------------------------------------------------------------------------- if __name__ == "__main__": app = QtGui.QApplication(sys.argv) window = TestApp() window.show() sys.exit(app.exec_()) From tundra at tundraware.com Wed Apr 1 22:42:03 2009 From: tundra at tundraware.com (Tim Daneliuk) Date: Wed, 01 Apr 2009 21:42:03 -0500 Subject: Python Goes Mercurial In-Reply-To: References: <2972356f-a2df-4dc5-9071-4ac8518e128b@j18g2000prm.googlegroups.com> Message-ID: Lawrence D'Oliveiro wrote: > In message , Tim Daneliuk wrote: > >> ,,, when I suggested that better open source tools existed, they kindly >> explained their complete lack of interest in moving several millions >> of lines of code to anything new. > > What was their explanation? > Their entire internal workflow from development through final release and deployment was built around these ancient/nonstandard toolset. It was economically infeasible to retool both the technology and the processes for millions of lines of code under version control - there just wasn't an ROI for it. This is not uncommon in large legacy environments in my experience. -- ---------------------------------------------------------------------------- Tim Daneliuk tundra at tundraware.com PGP Key: http://www.tundraware.com/PGP/ From jfager at gmail.com Wed Apr 1 23:13:54 2009 From: jfager at gmail.com (jfager) Date: Wed, 1 Apr 2009 20:13:54 -0700 (PDT) Subject: Thoughts on language-level configuration support? References: <36148830-22c0-4f19-ab23-d04d8755ab4a@s28g2000vbp.googlegroups.com> <5411b35b-56d7-4fd1-af0b-f1fe70143c20@h28g2000yqd.googlegroups.com> <781bf317-b269-41a0-a82b-0ef79a4f4064@s20g2000yqh.googlegroups.com> <6b45e790-fb60-4aca-a6a6-1cc5e491d801@c9g2000yqm.googlegroups.com> <85772d96-6508-4110-b033-c4f81c7d9ff3@z19g2000yqe.googlegroups.com> Message-ID: On Apr 1, 8:56?pm, CTO wrote: > >?I just mean that there should be a > > clear and easy way to do it, that it should be considered a basic > > service, and that if the best way to satisfy all the goals is to > > integrate it directly into the language, that shouldn't be shied away > > from. > > Honestly, the programming language and the configuration languages > should never, ever, ever be inseparable. I don't see a reason for > it and I see some great reasons not to do it. I'm not saying the actual end-user configuration language would be tied to the programming language. I'm starting to think a better way to describe this is to play down configuration as a use case, and instead focus on what the actual mechanism is: a way to define points in your program that can have values injected into them at runtime by some external entity, a uniform naming scheme for those points derived from the code, and a way to expose those points to interested consumers. > > The example that I have on my blog post, I consider that 'syntax', > > even though it's implemented as a function, mainly just because it > > digs into the bytecode and modifies the normal way a function is > > evaluated (the function's value is determined by where the output > > would go). > > I don't particularly see why this is needed. It was a stab at the 'uniform naming scheme' - the function fetches a config value based on a key derived from the name of the variable its output will be assigned to. It definitely needs more thought, but at least I got to play around with the bytecode tools :) > To my mind the strongest > argument you are making, by far, is the "discoverable" argument. If > you were able to build a system which permitted other components to > discover configuration options and give configurable levels of > permanence and visibility to them, that would be great. If you were > able to do it and make it easy for a programmer to interact with, you > would most definitely have a module I would use. But I daresay you're > going to have to build it (or at least mock it up) before you're going > to get a lot of folks jumping on the bandwagon, and its a *long* slog > before you hit the level of support and stability you're going to need > to convince folks to trust their precious config files to it. Agreed, and I do intend to do a proper mockup when I get the time; I just wanted to get some initial reactions. Thanks for your feedback. From tjreedy at udel.edu Wed Apr 1 23:15:01 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 01 Apr 2009 23:15:01 -0400 Subject: Which is more Pythonic? In-Reply-To: References: Message-ID: John Machin wrote: > On Apr 2, 2:10 am, John Posner wrote: >> Dennis Lee Bieber presented a code snippet with two consecutive statements >> that made me think, "I'd code this differently". So just for fun ... is >> Dennis's original statement or my "_alt" statement more idiomatically >> Pythonic? Are there even more Pythonic alternative codings? >> >> mrkrs = [b for b in block >> if b > 127 >> or b in [ "\r", "\n", "\t" ] ] > > I'd worry about "correct" before "Pythonic" ... see my responses to > Dennis in the original thread. > >> mrkrs_alt1 = filter(lambda b: b > 127 or b in [ "\r", "\n", "\t" ], >> block) >> mrkrs_alt2 = filter(lambda b: b > 127 or b in list("\r\n\t"), block) Comprehensions combine map and filter and somewhat, to some people, replace both. Tastes vary. If one has a filter function f already, filter(f,seq) may be faster than (f(i) for i in seq). If one does not, ( for i in seq) will probably be faster than filter(lambda i: , seq) as it avoids a function call, using inlined expression code. So either can be more Pythonic, depending on the context. > Try this on and see if it fits: > > num_bin_chars = sum(b > "\x7f" or b < "\x20" and b not in "\r\n\t" for > b in block) However, for just counting, this is even better -- and most Pythonic! In fact, being able to count the number of True values in a stream of True and False by summation is part of the justification of bool being a subclass of int. >> (Note: Dennis's statement converts a string into a list; mine does not.) > > What is list("\r\n\t") doing, if it's not (needlessly) converting a > string into a list? > >> --- >> >> binary = (float(len(mrkrs)) / len(block)) > 0.30 >> >> binary_alt = 1.0 * len(mrkrs) / len(block) > 0.30 >> > > num_bin_chars > 0.30 * len(block) > > (no mucking about with float() or 1.0, and it doesn't blow up on a > zero-length block) Nice point! Terry Jan Reedy From tjreedy at udel.edu Wed Apr 1 23:38:08 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 01 Apr 2009 23:38:08 -0400 Subject: pygame and socket.recv In-Reply-To: <2084b37d-485c-435f-be5c-8ee1bc901d70@p11g2000yqe.googlegroups.com> References: <1c6574dd-1bfb-4c45-a744-58761e5d1f06@k2g2000yql.googlegroups.com> <2084b37d-485c-435f-be5c-8ee1bc901d70@p11g2000yqe.googlegroups.com> Message-ID: Aaron Brady wrote: > > My game loop looks like this: > > poll events, get 1 at most > send to server > wait for server reply > render entire frame I am very sure that commercial 'real-time' (versus turn-based) multiplayer games do not operate that way, at least not the ones I have played. I suspect that most do not operate by frames on the server, but rather sent a more or less continuous series of events. NPC movements come from the server (and can be given as vectors), other player movements get echoed as sent to the server. The client uses the most current data it has when it renders a frame. If the scene it too complex relative to the client rendering capability, the frames jump and the motion is choppy. If the info stream is interrupted, the scene may freeze a bit even as the client continues to re-render to account for player motion. Hope this helps a bit. > Yes, I am blocking for the data to come down the network. > Unfortunately, if I use any "prediction," I will have to go back and > un-render the previous frame, then redraw with the new information. > > 40 transmissions per second in each way can't be too much to ask, it's > just that they have to alternate, up one, down one. > > I don't understand your solution. I can't picture it for my favorite > RTS game or the one I'm writing. Are you saying that the slower > machine just jumps ahead, and its user just doesn't have the > opportunity to make moves on the omitted frames? > > I am using TCP, socket.SOCK_STREAM. UDP is a potential solution, but > it still doesn't fix my main loop. > -- > http://mail.python.org/mailman/listinfo/python-list > From research at johnohagan.com Thu Apr 2 00:23:32 2009 From: research at johnohagan.com (John O'Hagan) Date: Thu, 2 Apr 2009 04:23:32 +0000 Subject: python for loop In-Reply-To: References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> Message-ID: <200904020423.32622.research@johnohagan.com> On Wed, 1 Apr 2009, Steven D'Aprano wrote: > On Wed, 01 Apr 2009 04:39:26 +0100, Rhodri James wrote: > > Dragging this back to the original topic, you clearly find starting list > > indices from zero unintuitive. To me, with a mathematical background, > > it's not just intuitive, it's correct. All sorts of useful properties > > fall out from that, not the least of which is the fact that > > "a[0:len(a)]" slices the whole of a list. > > But some non-useful properties fall out of that too. > > Want the fifth item? a[5] gives you the *sixth* item, which is weird, so > you have to use a[5-1] to get the fifth item. > > There is a major clash between the names of ordinals in human languages > and zero-based counting. In human languages, the Nth-ordinal item comes > in position N. You can keep that useful convention with zero-based > counting by inventing the ugly word "zeroth", but that just leads to > bizarro-talk like "the zeroeth item comes first, the first item comes > second, and so on". > > a[0:len(a)] is legal, a[0] is legal, but surprisingly a[len(a)] is an > error. > > Despite coming from a Pascal background, I've come to appreciate and > prefer zero-based indexing for programming. But I'm not blind to the > disadvantages. I'll often work out an algorithm using pencil and paper > and counting from one, and then subtract one to get zero-based indexes. [...] Despite being thoroughly acclimatised to zero-based indexing and having no wish to change it, I'm starting to see the OP's point. Many of the arguments presented in this thread in favour of zero-based indexing have rather been arguments for half-open intervals, which I don't think are in dispute. We all want these to be true: foo[:n] is the first n items of the sequence foo foo[:n] + foo[n:] == foo len(foo[n:m]) == m-n (foo[n:n]) is an empty sequence etc. and they are true with 0-based indexing if we exclude the last number, or equally with 1-based indexing if we exclude the first. In a way, Python already has 1-based indexing, in terms of absolute index values when counting backwards: the last element of a sequence is indexed -1, the second-last -2, etc., so that the first element is foo[-len(foo)]. 1-based indexing mirrors this so that foo[len(foo)] is not a surprising error as Steven mentions above, but the last element of foo. Also the other weirdness Steven mentioned goes away: the nth element of foo becomes foo[n], and the OP's issue with an extant element having an ordinality of 0 ("zeroeth") also goes away, i.e. ordinality and cardinality are lined up. Beyond being part of a conventionally-ordered set of keys, what can an ordinality of zero actually mean? (That's a sincere question.) But as long as we need to both count items and measure intervals, we have to deal with these +/-1 adjustments in any case. As an aside, a similar issue arises in music theory, in which intervals are traditionally expressed relative to scale degrees starting from one (counting the notes), or in more modern language, in semitones starting from zero (measuring the interval). This gives rise to similar (minor and short-lived) confusions. +/-1 :) Regards, John From timr at probo.com Thu Apr 2 00:41:07 2009 From: timr at probo.com (Tim Roberts) Date: Wed, 01 Apr 2009 21:41:07 -0700 Subject: Creating huge data in very less time. References: <14b0a80b-4e77-4077-9129-6a526d26d912@v19g2000yqn.googlegroups.com> <8b745e98-a470-4e07-b0fb-e97b8d5b4b7c@j8g2000yql.googlegroups.com> Message-ID: Grant Edwards wrote: >On 2009-03-31, Dave Angel wrote: > >> They were added in NTFS, in the Windows 2000 timeframe, to my >> recollection. > >NTFS was added in NT 3.1 (which predates Win2K by 7-8 years). Although that's true, you didn't read his sentence. Sparse file support was not added to NTFS until Windows 2000, exactly as he said. -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From gjango.py at gmail.com Thu Apr 2 00:41:09 2009 From: gjango.py at gmail.com (guptha) Date: Wed, 1 Apr 2009 21:41:09 -0700 (PDT) Subject: Sending SMS using python script Message-ID: <2005e94f-94de-4566-be60-7245efa4271d@y6g2000prf.googlegroups.com> hi group, my application needs to send SMS occasionally to all the clients .Is there any library in python that supports in sending SMS. I like to conform few information i gathered in this regard. I can send SMS by two ways 1. Sending SMS using Email clients 2. Using sms gateway to send message(we can implement SMS Gateway API 's ,provided by vendor and ,send SMS -- we will be charged accordingly ) In case of First approach 1. We can make use of libgamil library to send SMS using gmail ( I ref : http://blog.datasingularity.com/?p=63 ) i suppose sending sms through gmail is not supported in India 2. Can we use Skype4py library, In case of second approach 1. Is there any way to send SMS for free inside India ,or ,Any free SMS gateway providers in India Any information regarding this is appreciable Thanks From Lie.1296 at gmail.com Thu Apr 2 00:58:47 2009 From: Lie.1296 at gmail.com (Lie) Date: Wed, 1 Apr 2009 21:58:47 -0700 (PDT) Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <72i5t4tgfo2h4gd6ggcs02flkca85kgoku@4ax.com> <0dbc9665-3ee8-4d62-8e9e-75c6d700076e@f1g2000prb.googlegroups.com> Message-ID: On Apr 1, 7:06?pm, Steven D'Aprano wrote: > There is a major clash between the names of ordinals in human languages > and zero-based counting. In human languages, the Nth-ordinal item comes > in position N. You can keep that useful convention with zero-based > counting by inventing the ugly word "zeroth", but that just leads to > bizarro-talk like "the zeroeth item comes first, the first item comes > second, and so on". No, there won't be any bizarro-talk. There is no argument: the zeroeth item comes zeroeth, the first item comes first, and so on. The index for the very zeroeth thing in a list is 0, so to get the zeroeth item you use s[0]. While to get the first item you use s[1]. It's very intuitive, isn't it? From castironpi at gmail.com Thu Apr 2 01:02:34 2009 From: castironpi at gmail.com (Aaron Brady) Date: Wed, 1 Apr 2009 22:02:34 -0700 (PDT) Subject: pygame and socket.recv References: <1c6574dd-1bfb-4c45-a744-58761e5d1f06@k2g2000yql.googlegroups.com> <2084b37d-485c-435f-be5c-8ee1bc901d70@p11g2000yqe.googlegroups.com> Message-ID: On Apr 1, 10:38?pm, Terry Reedy wrote: > Aaron Brady wrote: > > > My game loop looks like this: > > > poll events, get 1 at most > > send to server > > wait for server reply > > render entire frame > > I am very sure that commercial 'real-time' (versus turn-based) > multiplayer games do not operate that way, at least not the ones I have > played. > > I suspect that most do not operate by ?frames on the server, but rather > sent a more or less continuous series of events. ?NPC movements come > from the server (and can be given as vectors), other player movements > get echoed as sent to the server. ?The client uses the most current data > it has when it renders a frame. ?If the scene it too complex relative to > the client rendering capability, the frames jump and the motion is > choppy. ?If the info stream is interrupted, the scene may freeze a bit > even as the client continues to re-render to account for player motion. > ? Hope this helps a bit. I am thinking a couple of things. Take a branch in the reasoning. On arriving at the time to render the next frame, without any announcement from the other side: 1) Side A continues to model Side B as though no change in motion occurred. 2) Side A discontinues its model of Side B until next transmission. In 1, upon receiving the next update from B, it will have to retrace its steps, "unmodel" the changes, and bring them up to date. This can have retroactive cause-and-effect consequences on A as well, including but not limited to, live-or-dead state, position, and velocity. In 2, A's view of B looks jumpy-- B is halting, then jumping ten tiles, then moving smoothly, then halting and jumping. Retroactive changes are regardless possible. This was a the-long-way-'round word search for the word 'retroactive', which I'm glad is a word, incidentally. In either case, every time one player's packets are delayed, every other player's screen will alter state, more or less like they have been sleepwalking and just come to. There is the additional problem of what to do with A's interim actions. Reminds me of a diff-merge in source control! How about king-of-the-hill precedence for contradiction resolution? Then, the player whose packets are delayed has an advantage. B got to make a change to another player's state, as well as proceed with exclusive knowledge of it. B is the only one with an up-to-date model of the game during the lag. This could also apply to B instead, if the game structure makes the others' besides B the valid current model. B is sleepwalking for the length of his/er network delay, and his/er actions are discarded. This may be more what players are accustomed to; the interface goes dead, and issuing or rescinding orders isn't acknowledged or received. Any momentary loss of "visibility" could be mitigated in a low-impact game or simulation of physical motion, since it takes many Gs of acceleration to avert most-- all but glancing-- collisions that are destined in just a few frames. Mine is not such; position and velocity are subject to arbitrary changes. Maybe I just need more clicks per second, to give the user a feeling of accomplishing something, even though the same percentage of actions is successfully transmitted. Is satisfaction a simple decaying proportion? With discontinuities in position and momentum possible, is lockstep frame-for-frame synchronization my best option? Or should I fall back on a sturdier game model, where actions' consequences can't affect other players for several frames? Blue... no! Yellow! From castironpi at gmail.com Thu Apr 2 01:05:20 2009 From: castironpi at gmail.com (Aaron Brady) Date: Wed, 1 Apr 2009 22:05:20 -0700 (PDT) Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <72i5t4tgfo2h4gd6ggcs02flkca85kgoku@4ax.com> <0dbc9665-3ee8-4d62-8e9e-75c6d700076e@f1g2000prb.googlegroups.com> Message-ID: On Apr 1, 11:58?pm, Lie wrote: > On Apr 1, 7:06?pm, Steven D'Aprano > > wrote: > > There is a major clash between the names of ordinals in human languages > > and zero-based counting. In human languages, the Nth-ordinal item comes > > in position N. You can keep that useful convention with zero-based > > counting by inventing the ugly word "zeroth", but that just leads to > > bizarro-talk like "the zeroeth item comes first, the first item comes > > second, and so on". > > No, there won't be any bizarro-talk. There is no argument: the zeroeth > item comes zeroeth, the first item comes first, and so on. The index > for the very zeroeth thing in a list is 0, so to get the zeroeth item > you use s[0]. While to get the first item you use s[1]. It's very > intuitive, isn't it? Robot 1: I won zeroeth place at the contest, honey! Robot 2: Congratulations! I knew you could do it. From ldo at geek-central.gen.new_zealand Thu Apr 2 01:47:29 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Thu, 02 Apr 2009 18:47:29 +1300 Subject: A design problem I met again and again. References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> <158986a9-b2d2-413e-9ca0-c584299f176b@f1g2000prb.googlegroups.com> Message-ID: In message <158986a9-b2d2-413e-9ca0- c584299f176b at f1g2000prb.googlegroups.com>, ??? wrote: > On Apr 1, 4:55 pm, Lawrence D'Oliveiro central.gen.new_zealand> wrote: > >> In message <48506803-a6b9-432b-acef- >> >> b75f76e90... at v23g2000pro.googlegroups.com>, ??? wrote: >> > Until one day I find service has nearly 100 methods and 6000 lines of >> > code. I don't need to read any programming book to know that it's >> > too big. >> >> The question is not how many lines or how many methods, but whether it >> makes sense to remain as one piece or not. In one previous project, I had >> one source file with nearly 15,000 lines in it. Did it make sense to >> split that up? Not really. > > What are the average size of source files in your project? If it's > far lower than 15,000, don't feel it's a little unbalance? Why? From wolfgang at rohdewald.de Thu Apr 2 02:07:38 2009 From: wolfgang at rohdewald.de (Wolfgang Rohdewald) Date: Thu, 2 Apr 2009 08:07:38 +0200 Subject: Display directory pyqt4 and Python In-Reply-To: References: Message-ID: <200904020807.39073.wolfgang@rohdewald.de> On Donnerstag, 2. April 2009, Dunwitch wrote: > for x in (fileList): > self.ui.displayVideo.setText(x) # This only shows the last self.ui.displayVideo.setText('\n'.join(fileList)) but I would go for a solution with QDirModel / QListView http://doc.trolltech.com/4.5/itemviews-dirview.html -- Wolfgang -------------- next part -------------- An HTML attachment was scrubbed... URL: From mail at microcorp.co.za Thu Apr 2 02:19:51 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Thu, 2 Apr 2009 08:19:51 +0200 Subject: pygame and socket.recv References: <1c6574dd-1bfb-4c45-a744-58761e5d1f06@k2g2000yql.googlegroups.com> Message-ID: <011f01c9b367$a826faa0$0d00a8c0@Hendrik> "Aaron Brady" wrote: > Hi, > > I tried writing a small game on a pygame layer. The graphics are > fine, and at the moment, it is not graphics intensive. It is multi- > player, and for the communication, I am sending a pickle string across > a LAN, once per frame. How big is this pickle - i.e. how many packets does it translate to on the wire, given that an ethernet packet is less than 1500 bytes? What is the maximum number of full size packets that a simple echo server will echo per second on your setup? I predict that you will find that this is far less than what a calculation based on the bit rate will suggest... > > I'm observing some latency. It seems that socket.recv isn't > performing consistently. The server is using time.clock and > time.sleep to keep the frame rate at 40 frames per second. Latency > occurred regardless of whether the connection was ethernet or > wireless, although worse with wireless. What does "some latency" mean? - barely visible jitter, or a half second freeze? What do you do when the total round trip time takes longer than your target of one fortieth of a second? The fact that wireless is worse (54Mb/s? - down from 100Mb/s) makes me think you are running out of bandwidth, or that the LAN is busy and that the bandwidth is wasted on collisions. What happens when you drop the frame rate to 20 per second? (it is parameterised, isn't it?) > > Does anyone have any hints or suggestions? I am on Windows XP. What else is running on the machines at the same time? - your game may be sucking at the hind tit of processor time... - Hendrik From Lie.1296 at gmail.com Thu Apr 2 02:27:04 2009 From: Lie.1296 at gmail.com (Lie) Date: Wed, 1 Apr 2009 23:27:04 -0700 (PDT) Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <72i5t4tgfo2h4gd6ggcs02flkca85kgoku@4ax.com> <0dbc9665-3ee8-4d62-8e9e-75c6d700076e@f1g2000prb.googlegroups.com> Message-ID: <1335d3ad-c374-41f5-a783-9435ef8dd227@b7g2000pre.googlegroups.com> On Apr 2, 4:05?pm, Aaron Brady wrote: > On Apr 1, 11:58?pm, Lie wrote: > > > On Apr 1, 7:06?pm, Steven D'Aprano > > > wrote: > > > There is a major clash between the names of ordinals in human languages > > > and zero-based counting. In human languages, the Nth-ordinal item comes > > > in position N. You can keep that useful convention with zero-based > > > counting by inventing the ugly word "zeroth", but that just leads to > > > bizarro-talk like "the zeroeth item comes first, the first item comes > > > second, and so on". > > > No, there won't be any bizarro-talk. There is no argument: the zeroeth > > item comes zeroeth, the first item comes first, and so on. The index > > for the very zeroeth thing in a list is 0, so to get the zeroeth item > > you use s[0]. While to get the first item you use s[1]. It's very > > intuitive, isn't it? > > Robot 1: I won zeroeth place at the contest, honey! > Robot 2: Congratulations! ?I knew you could do it. That should be Robot 0 and Robot 1. From steven at REMOVE.THIS.cybersource.com.au Thu Apr 2 02:28:04 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 02 Apr 2009 06:28:04 GMT Subject: A design problem I met again and again. References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> <158986a9-b2d2-413e-9ca0-c584299f176b@f1g2000prb.googlegroups.com> Message-ID: On Thu, 02 Apr 2009 18:47:29 +1300, Lawrence D'Oliveiro wrote: >>> The question is not how many lines or how many methods, but whether it >>> makes sense to remain as one piece or not. In one previous project, I >>> had one source file with nearly 15,000 lines in it. Did it make sense >>> to split that up? Not really. >> >> What are the average size of source files in your project? If it's >> far lower than 15,000, don't feel it's a little unbalance? > > Why? If you have too much code in one file, it will upset the balance of the spinning hard drive platter, and it will start to wobble and maybe even cause a head-crash. -- Steven From hniksic at xemacs.org Thu Apr 2 02:28:19 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Thu, 02 Apr 2009 08:28:19 +0200 Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <5a87e703-6737-46c2-b215-52123a870c3c@l13g2000vba.googlegroups.com> Message-ID: <87ljqj7enw.fsf@busola.homelinux.net> Carl Banks writes: > This is unforgiveable, not only changing the indexing semantics of > Python (because a user would have NO CLUE that something underlying > has been changed, and thus it should never be done), but also for > the needless abuse of exec. Then I guess you'd fire Guido, too -- from socket.py: class _socketobject(object): [...] _s = ("def %s(self, *args): return self._sock.%s(*args)\n\n" "%s.__doc__ = _realsocket.%s.__doc__\n") for _m in _socketmethods: exec _s % (_m, _m, _m, _m) del _m, _s From steven at REMOVE.THIS.cybersource.com.au Thu Apr 2 02:28:49 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 02 Apr 2009 06:28:49 GMT Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> Message-ID: On Thu, 02 Apr 2009 04:23:32 +0000, John O'Hagan wrote: > Beyond being part of a conventionally-ordered set of keys, what can an > ordinality of zero actually mean? (That's a sincere question.) In set theory, you start by defining the integers like this: 0 is the cardinality (size) of the empty set, the set with nothing in it. 1 is the cardinality of the set of empty sets, that is, the set containing nothing but the empty set. 2 is the cardinality of the set of the empty set plus the set of empty sets. 3 is the cardinality of the set containing the empty set, plus the set of empty sets, plus the set of (the empty set plus the set of empty sets). And so forth, to infinity and beyond. Or to put it another way: 0 = len( {} ) 1 = len( {{}} ) 2 = len( {{}, {{}}} ) 3 = len( {{}, {{}}, {{}, {{}}} ) etc. For non-infinite sets, you can treat ordinal numbers and cardinal numbers as more or less identical. So an ordinality of zero just means the number of elements of something that doesn't exist. How that relates to whether indexing should start at one or zero, I have no idea. Oh, and speaking of... I'm shocked, SHOCKED I say, that nobody has given that quote about the compromise of 0.5. -- Steven From steven at REMOVE.THIS.cybersource.com.au Thu Apr 2 02:29:49 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 02 Apr 2009 06:29:49 GMT Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <72i5t4tgfo2h4gd6ggcs02flkca85kgoku@4ax.com> <0dbc9665-3ee8-4d62-8e9e-75c6d700076e@f1g2000prb.googlegroups.com> Message-ID: On Wed, 01 Apr 2009 21:58:47 -0700, Lie wrote: > On Apr 1, 7:06?pm, Steven D'Aprano > wrote: > >> There is a major clash between the names of ordinals in human languages >> and zero-based counting. In human languages, the Nth-ordinal item comes >> in position N. You can keep that useful convention with zero-based >> counting by inventing the ugly word "zeroth", but that just leads to >> bizarro-talk like "the zeroeth item comes first, the first item comes >> second, and so on". > > No, there won't be any bizarro-talk. There is no argument: the zeroeth > item comes zeroeth, the first item comes first, and so on. The index for > the very zeroeth thing in a list is 0, so to get the zeroeth item you > use s[0]. While to get the first item you use s[1]. It's very intuitive, > isn't it? No, because "first", "second", "third" etc. have existed in the English language for hundreds of years and everybody knows them. "Zeroeth" was probably invented a few decades ago, and is known by maybe 1% of the English-speaking population. Given the list [a, b, c], if you ask even a C programmer *in English* "what's the first item?", they will almost invariably answer a rather than b. -- Steven From csd01412 at gmail.com Thu Apr 2 02:35:04 2009 From: csd01412 at gmail.com (akshat agarwal) Date: Thu, 2 Apr 2009 12:05:04 +0530 Subject: Regex trouble In-Reply-To: <4b8470fc5da4298d22103ab0ad6ebf0c.squirrel@localhost> References: <2bcaec310904010702t59077209y55fd93a1070fd64@mail.gmail.com> <509e36f968a3aeacb7ad8aaf4f5c8685.squirrel@localhost> <4b8470fc5da4298d22103ab0ad6ebf0c.squirrel@localhost> Message-ID: <2bcaec310904012335y1ec229f4h184a42e7bf573714@mail.gmail.com> Thanks Andrew. I was also of the same view that perl handled this via some special cases. On Wed, Apr 1, 2009 at 8:32 PM, andrew cooke wrote: > > more exactly, my guess is perl has a special case for this that avoids > doing a search over all possible matchers via the pushdown stack. > > andrew cooke wrote: > > > > ".*?" is a "not greedy" match, which is significantly more difficult to > > handle than a normal ".*". so the performance will depend on quite > > complex details of how the regular expression engine is implemented. it > > wouldn't surprise me if perl was better here, because it comes from a > > background with a much stronger emphasis on regular expressions. > > > > i know that not an exact answer, but unless you find the author of the re > > library i am not sure you will get a much better explanation. it comes > > down to whether the regular expression can be implemented using a > > deterministic finite automaton (rather than an indeterministic one). if > > you look at the table at the bottom of > > http://en.wikipedia.org/wiki/Finite_state_machine then i believe (i am > not > > 100% sure) that the ".*?" match requires at least a pushdown automota, > > while ".*" can be handled with a simple finite automaton. > > > > disclaimer: this is all fairly new to me as i just recently implemented a > > regular expression matcher myself, and i may be wrong on some of the > > details. > > > > andrew > > > > > > akshat agarwal wrote: > >> Hi, > >> > >> I am trying to use the following snippet of code to print a regex match. > >> > >> s = '01234567890123456789x012' > >> > >> pat = r'(.*?x|[^a]+)*y' > >> > >> mo = re.search(pat, s) > >> if mo is not None: > >> print mo.group(0) > >> > >> By adding one character before the 'x' in the input string, the time > >> taken > >> to print the match doubles. This behaviour is not observed in perl. I am > >> curious to know about the difference the in regex implementations of > >> perl > >> and python which causes this. > >> > >> Thanks > >> -- > >> http://mail.python.org/mailman/listinfo/python-list > >> > > > > > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From pavlovevidence at gmail.com Thu Apr 2 02:47:00 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 1 Apr 2009 23:47:00 -0700 (PDT) Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> Message-ID: On Apr 1, 9:23?pm, John O'Hagan wrote: > Despite being thoroughly acclimatised to zero-based indexing and having no > wish to change it, I'm starting to see the OP's point. > > Many of the arguments presented in this thread in favour of zero-based > indexing have rather been arguments for half-open intervals, which I don't > think are in dispute. We all want these to be true: > > foo[:n] is the first n items of the sequence foo > foo[:n] + foo[n:] == foo > len(foo[n:m]) == m-n > (foo[n:n]) is an empty sequence > etc. > > and they are true with 0-based indexing if we exclude the last number, or > equally with 1-based indexing if we exclude the first. Unless I'm missing something, wouldn't that mean: range(0,10) -> [1,2,3,4,5,6,7,8,9,10] Even though it's theoretically just another way to line up the open interval, as practical matter it's going to be a lot more confusing. Of course you could exclude the last number with one-based indexing also, but that would be confusing too, since you would have to use something like range(1,len(x)+1) to iterate over the items of x. Given that, I'm going to disagree that a half-open interval is desirable in the case of one-based indexing. Furthermore, I know of no languages that use both one-based indexing and half-open intervals. Do you know of any? > Beyond being part of a conventionally-ordered set of keys, what can an > ordinality of zero actually mean? (That's a sincere question.) I think people were being facetious. To me the first item in the list is x[0]--ordinal does not match cardinal. However, I don't use ordinals much when talking about list items; I'll say item 2, not third item. Carl Banks From castironpi at gmail.com Thu Apr 2 02:47:13 2009 From: castironpi at gmail.com (Aaron Brady) Date: Wed, 1 Apr 2009 23:47:13 -0700 (PDT) Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <72i5t4tgfo2h4gd6ggcs02flkca85kgoku@4ax.com> <0dbc9665-3ee8-4d62-8e9e-75c6d700076e@f1g2000prb.googlegroups.com> Message-ID: On Apr 2, 1:29?am, Steven D'Aprano wrote: > On Wed, 01 Apr 2009 21:58:47 -0700, Lie wrote: > > On Apr 1, 7:06?pm, Steven D'Aprano > > wrote: > > >> There is a major clash between the names of ordinals in human languages > >> and zero-based counting. In human languages, the Nth-ordinal item comes > >> in position N. You can keep that useful convention with zero-based > >> counting by inventing the ugly word "zeroth", but that just leads to > >> bizarro-talk like "the zeroeth item comes first, the first item comes > >> second, and so on". > > > No, there won't be any bizarro-talk. There is no argument: the zeroeth > > item comes zeroeth, the first item comes first, and so on. The index for > > the very zeroeth thing in a list is 0, so to get the zeroeth item you > > use s[0]. While to get the first item you use s[1]. It's very intuitive, > > isn't it? > > No, because "first", "second", "third" etc. have existed in the English > language for hundreds of years and everybody knows them. "Zeroeth" was > probably invented a few decades ago, and is known by maybe 1% of the > English-speaking population. > > Given the list [a, b, c], if you ask even a C programmer *in English* > "what's the first item?", they will almost invariably answer a rather > than b. > > -- > Steven However, if you ask him/er, "What is the item that is 0 items from the start of the list?", what will s/he say? From deets at nospam.web.de Thu Apr 2 03:02:06 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 02 Apr 2009 09:02:06 +0200 Subject: Display directory pyqt4 and Python In-Reply-To: References: Message-ID: <73j67eFvev4hU1@mid.uni-berlin.de> Dunwitch schrieb: > I've looked around for the answer and have decided to ask an expert > for the solution. Whats suppose to happen is a user pushes a button > and displays the directory content in the text edit window on the gui. > Everything works, apart from the fact that it only shows the last file > in the directory, not a full listing. Here is the code for reference. > Any help would be appreciated, I'm missing something simple. > -- > Dunwitch > > # File : Video Search > import sys, os, win32net > > from PyQt4 import QtGui, QtCore > from findVideos import Ui_MainWindow # UI Files from QT Designer > > # New class derived from QMainWindow > class TestApp(QtGui.QMainWindow): > def __init__(self): > QtGui.QMainWindow.__init__(self) > self.ui = Ui_MainWindow() > self.ui.setupUi(self) > > # Connect the signals andslots > QtCore.QObject.connect(self.ui.findFiles,QtCore.SIGNAL("clicked > ()"), self.showVideoFiles) > > #----------------------------- Code In Question > --------------------------------- > def showVideoFiles(self): > enc7 = ('\\\\1.2.3.4\\somefolder') > fileList=os.listdir(enc7) > for x in (fileList): > self.ui.displayVideo.setText(x) # This only shows the last > file in the directory? Of course, if you always only set the *full* text of the displayVideo to the current list-item, it will end showing the last set item. self.ui.displayVideo("\n".join(fileList)) should do the trick (of course you can get rid of the for-loop) Diez From arnodel at googlemail.com Thu Apr 2 03:23:36 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Thu, 2 Apr 2009 00:23:36 -0700 (PDT) Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <5a87e703-6737-46c2-b215-52123a870c3c@l13g2000vba.googlegroups.com> Message-ID: Carl Banks wrote: > On Apr 1, 2:32?pm, Arnaud Delobelle wrote: Check the date on the line above (and the PS in that post). > > If I were your boss and you ever pulled something like this, your ass > would be so fired. > > This is unforgiveable, not only changing the indexing semantics of > Python (because a user would have NO CLUE that something underlying > has been changed, and thus it should never be done), but also for the > needless abuse of exec. Gotcha ;) -- Arnaud PS. I disagree about the 'needless abuse of exec'. From deets at nospam.web.de Thu Apr 2 03:28:21 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 02 Apr 2009 09:28:21 +0200 Subject: Converting a PIL image object to a buffer In-Reply-To: <79ddf944-2fee-46c5-aed7-e6276dce88ed@s19g2000vbp.googlegroups.com> References: <79ddf944-2fee-46c5-aed7-e6276dce88ed@s19g2000vbp.googlegroups.com> Message-ID: <73j7olFv6l7uU1@mid.uni-berlin.de> Simon Hibbs schrieb: > On 1 Apr, 21:43, Gary Herron wrote: >> Simon Hibbs wrote: >>> I'm trying to dump a snapshot of my application window to the >>> clipboard. I can use ImageGrab in PIL to get the screen data into a >>> PIL image object, which i have converted to a bitmap using ImageWin, >>> but when I try to pass this to the clipboard using - >>> win32clipboard.SetClipboardData(win32clipboard.CF_BITMAP, img) >>> It fails, telling be that "The object must support the buffer >>> interface". >>> How can I convert a PIL image into a buffer object? I can't find any >>> clues. >> PIL images have a tostring method that returns a string containing all >> the pixel data. Would that help you to either create the needed >> buffer? Or perhaps you could by-pass the need for a buffer, and just >> use the byte string. > > If I use tostring I get a string which I can put on the clipboard, but > it isn't any kind of image. I can make a PIL image from the string but > them I'm back to square one again. Did you actually try that? Strings support the buffer interface, and the type of the binary data you set should be defined by the first argument. Alternatively (if the string is not of the proper format), maybe storing the image to a (c)StringIO-object as BMP and retrieving it's value would help. However, I think your concerns about "wasting memory" when using a file are moot - creating an extra memory buffer isn't less memory consuming, and if the file is living only a few seconds it might not even actually hit the disk at all. In the end, the important thing is the working clipboard. Diez From zhubicen at gmail.com Thu Apr 2 03:28:22 2009 From: zhubicen at gmail.com (Hill) Date: Thu, 2 Apr 2009 00:28:22 -0700 (PDT) Subject: How to get the version of a file on WinOS? References: Message-ID: <90d33411-1aa3-4c25-be2a-98a7bd722291@y34g2000prb.googlegroups.com> On 3?26?, ??4?27?, Tim Golden wrote: > Hill wrote: > > As you know , we can see the file version from by "right click menu > > and click the detail tab". > > Now i am wondering how to get this version number using python? > > Thanks very much! > > If you're talking about a .DLL then this is probably > what you're looking for: > > http://timgolden.me.uk/python/win32_how_do_i/get_dll_version.html > > If you're talking about something else, say a Word doc > or some other structured storage file, the answer's > more complicated. > > TJG That's what i need. Thank you very much! From eric.brunel at nospam-pragmadev.com Thu Apr 2 03:38:50 2009 From: eric.brunel at nospam-pragmadev.com (Eric Brunel) Date: 02 Apr 2009 07:38:50 GMT Subject: Help for Toplevel References: Message-ID: Muddy Coder wrote: > Hi Folks, > > I have a problem of handling Toplevel window. Basically, I wrote a > listbox viewer with scrollbars, and saved in file listbo.py. Then in > my main GUI window, with menu, I need to launch the listbox viewer, in > a new window. Obviously, a Toplevel window is needed. But, I failed at > passing parameters over to Toplevel window. Please take a look at my > code: > > Listbox viewer: > class ScrolledList(Frame): > def __init__(self, options, parent=None): So the first parameter to the constructor is the scrolled list options, and the second one is its parent widget, right? > Frame.__init__(self, parent) > self.pack(expand=YES, fill=BOTH) > self.makeWidgets(options) > > In my main GUI: > from XXX import ScrolledList > class Foo: > def __init__(self): > ........ > def call_listbox(self, params): > new = Toplevel() > alist = ['foor','bar'] > ScrolledList(new,alist) So why are you passing the parent widget as first parameter and something else as second...? On a more general level, unless really needed, I try to avoid creating subclasses of exisiting classes with a constructor with completely different parameters than the super-class's one. I'm usually doing something like: class ScrolledList(Frame): def __init__(self, *args, **options): Frame.__init__(self, *args, **options) The only thing I (rarely...) allow myself to do is to pass custom options and getting/removing them with options.pop('my_option') before calling the super-class's constructor. Otherwise, I just add specific methods; it's far less confusing. HTH - Eric - From bearophileHUGS at lycos.com Thu Apr 2 03:39:24 2009 From: bearophileHUGS at lycos.com (bearophileHUGS at lycos.com) Date: Thu, 2 Apr 2009 00:39:24 -0700 (PDT) Subject: Python3: to add, remove and change Message-ID: <421c55fc-5acf-454b-830f-a7b5b579f36c@c9g2000yqm.googlegroups.com> Here an informal list in random order of things that I may like to add or to remove to/from Python3.x+. The things I list here don't come from fifty hours of thinking of mine, and they may be often wrong. But I use Python2.x often enough, so such things aren't totally random either. To remove: map and filter (ex-ifilter/imap). I don't use them often enough, because generator expression are an obvious way to map and filter, and to me they seem usually good enough. (So far ShedSkin has done well without map/filter). (they can be moved into the itertools module, of course). To add: I use groupby often enough, so I may like to see it as a builtin. To change and improve: the API of the "re" module. I think several of its APIs can be improved and made more intuitive, reducing both memory and cognitive load. Despite having used the re module many times I need still to look up details into the docs. So its API may be bad. To add: Python3 is much more lazy-flavour than Python 2.x (see lazy range, lazy dict views, etc). So islice becomes even more useful and more commonly used. So I may like to have a lazy slicing as built-in. But if you make it built-in, then why not add a syntax too? So I'd like the usual slicing syntax [::] to be added to all lazy generators too (to generator expressions, as an automatically created method of iterators, and of course usable by iterable classes too). Examples: >>> foo = (i for i in range(2, 1000) if all(i % d for d in range(2, i))) >>> list(foo[3: 14]) [7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43] >>> def bar(): ... for i in range(2, 1000): ... if all(i % d for d in range(2, i)): ... yield i ... >>> list(bar()[3: 14]) [7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43] To change: + is a commutative operation, while the concatenation of two sequences is not. So after using the D language for some time I have started to not appreciate anymore to use + to concatenate strings and lists. D language uses ~ for such operation (this isn't a perfect symbol, because I don't have it on my keyword, so on Windows I have to type ALT+126 to insert it, or I have to redefine/remap a keyboard key). D also has a specific operator that can be overloaded: opCat, opCat_r and opCatAssign, that map to ~ and ~= (the _r is the inverted order, like the __radd__): http://www.digitalmars.com/d/2.0/operatoroverloading.html#Binary So if you define a vector-like collection you can define both opAdd and opCat, the first may the sum between two vectors and the opCat is their joining. To add: in Python3 you use lazy constructs much more often, so itertools.chain may become more used. So it may be good to have chain as built-in. But then it can be good to have a better syntax support too. So I'd like all lazy iterables to support chaining. If you use the ~ operator you can write: range(100) ~ (i for i in range(2, 1000) if all(i % d for d in range(2, i))) (I have implemented most of itertools in D language, and I have added this ~ operator to all lazy constructs, plus I have added an easy way to add it to user-defined lazy iterables). Unladen Swallow for official CPython 2.x/3.x: yes, thank you, if it will be good enough. It can be what Psyco isn't able to become, and what PyPy has so far failed to be (PyPy seems Open Souce public-money- funded vaporwere that has tried to do too much). To add again: tuple unpacking in function arguments: it's handy, hi- level, de-clutters the code and shortens it too. To change: I'd like {:} as empty dict literal, and {} as empty set literal. To do nothing: probably I will never fully like the syntax of tuple literals. It's not clean. But in practice I can live with it, and I have so far failed to invent a better syntax. The main problem is that ASCII (and keyboards too, maybe) was created for business purposes and it has too few delimiters. While the sytax for lists: [] [1] [1, 2, 3] is perfect. To add: I use defaultdicts often enough, so I may even like to see it as built-in. But this isn't essential, using the import from the collections module is usually acceptable. Bye, bearophile From tadwelessar at gmail.com Thu Apr 2 03:41:32 2009 From: tadwelessar at gmail.com (Matteo) Date: Thu, 2 Apr 2009 00:41:32 -0700 (PDT) Subject: Sending SMS using python script References: <2005e94f-94de-4566-be60-7245efa4271d@y6g2000prf.googlegroups.com> Message-ID: I use a programme, written in Python, which sends sms through the sms providers. You might want to have a look to the source code: http://www.moioli.net/Progetti___1/MoioSMS___Messaggi_GRATIS_da_Internet____22.html From gagsl-py2 at yahoo.com.ar Thu Apr 2 03:48:40 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 02 Apr 2009 04:48:40 -0300 Subject: How to pass one HTML values to another HTML References: <7586a2d20903310427l75341f37t6370316aac0fc28@mail.gmail.com> Message-ID: En Tue, 31 Mar 2009 08:27:18 -0300, Kalyan escribi?: > by using python and google app engine how can i pass one HTML values > to > another HTML .. i am very new to Python programing > > Example : > in one HTML i entered Name and Address fields and i submit the page at > that > time i want to see those two values in another HTML page.. please reply > me.. You don't. The first page saves data into a database ("datastore" in AppEngine). The second page retrieves data from the datastore. Both pages aren't directly connected. There are many tutorials available about AppEngine, you may start here: http://code.google.com/intl/en/appengine/docs/ -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Thu Apr 2 03:48:44 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 02 Apr 2009 04:48:44 -0300 Subject: Hands on Python - Problem with Local Cgi Server References: Message-ID: En Tue, 31 Mar 2009 09:56:48 -0300, Gary Wood escribi?: > I have the DOS box with the message > Localhost CGI server started > > But when i try this I get the Windows Error > Failed to Connect > The connection was refused when attempting to contact localhost:8080. Try a different port instead of 8080 (e.g. 8765) both in the browser address and the Python script. > The py file brings up the DOS box as if its running ok And you don't see any error message in the console? It doesn't close automatically? -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Thu Apr 2 03:55:58 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 02 Apr 2009 04:55:58 -0300 Subject: Help for Toplevel References: Message-ID: En Wed, 01 Apr 2009 21:55:52 -0300, Muddy Coder escribi?: > I have a problem of handling Toplevel window. Basically, I wrote a > listbox viewer with scrollbars, and saved in file listbo.py. Then in > my main GUI window, with menu, I need to launch the listbox viewer, in > a new window. Obviously, a Toplevel window is needed. But, I failed at > passing parameters over to Toplevel window. Please take a look at my > code: > > Listbox viewer: > class ScrolledList(Frame): > def __init__(self, options, parent=None): > Frame.__init__(self, parent) > self.pack(expand=YES, fill=BOTH) > self.makeWidgets(options) > > In my main GUI: > from XXX import ScrolledList > class Foo: > def __init__(self): > ....... > def call_listbox(self, params): > new = Toplevel() > alist = ['foor','bar'] > ScrolledList(new,alist) shouldn't the last line be ScrolledList(alist,new)? -- Gabriel Genellina From pavlovevidence at gmail.com Thu Apr 2 04:02:16 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 2 Apr 2009 01:02:16 -0700 (PDT) Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <5a87e703-6737-46c2-b215-52123a870c3c@l13g2000vba.googlegroups.com> <87ljqj7enw.fsf@busola.homelinux.net> Message-ID: On Apr 1, 11:28?pm, Hrvoje Niksic wrote: > Carl Banks writes: > > This is unforgiveable, not only changing the indexing semantics of > > Python (because a user would have NO CLUE that something underlying > > has been changed, and thus it should never be done), but also for > > the needless abuse of exec. > > Then I guess you'd fire Guido, too -- from socket.py: > > class _socketobject(object): > ? ? [...] > ? ? _s = ("def %s(self, *args): return self._sock.%s(*args)\n\n" > ? ? ? ? ? "%s.__doc__ = _realsocket.%s.__doc__\n") > ? ? for _m in _socketmethods: > ? ? ? ? exec _s % (_m, _m, _m, _m) > ? ? del _m, _s Damn straight I would, recklessly using exec like he owns the language or something. Carl Bansk From mail at johnohagan.com Thu Apr 2 04:09:16 2009 From: mail at johnohagan.com (John O'Hagan) Date: Thu, 2 Apr 2009 08:09:16 +0000 Subject: python for loop In-Reply-To: References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> Message-ID: <200904020809.16679.mail@johnohagan.com> On Thu, 2 Apr 2009, Steven D'Aprano wrote: > On Thu, 02 Apr 2009 04:23:32 +0000, John O'Hagan wrote: > > Beyond being part of a conventionally-ordered set of keys, what can an > > ordinality of zero actually mean? (That's a sincere question.) > [snip erudite definition of cardinality] > For non-infinite sets, you can treat ordinal numbers and cardinal numbers > as more or less identical. So an ordinality of zero just means the number > of elements of something that doesn't exist. This is the bit I don't get - I had thought of ordinality as something attached to each item - ['a','b','c'] has a cardinality of 3, and elements of ordinality 1, 2 and 3 (first,second, third) respectively. So it's possible to have a cardinality of zero (an empty sequence does) but only "something that doesn't exist" can have an ordinality of zero; as soon as there is an item, its ordinality is 1. Shoot me down, please! > How that relates to whether indexing should start at one or zero, I have > no idea. Only insofar as the "weirdness" of indexing being out of step with ordinality/cardinality matters, i.e. not that much. :) John From gagsl-py2 at yahoo.com.ar Thu Apr 2 04:18:09 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 02 Apr 2009 05:18:09 -0300 Subject: how to optimize zipimport References: <8cdf9fd4-6c87-47eb-8a9e-989336428379@v39g2000yqm.googlegroups.com> <091189e3-44f2-4f2b-b5cc-090a4d422f61@v23g2000pro.googlegroups.com> Message-ID: En Mon, 30 Mar 2009 09:39:20 -0300, Coonay escribi?: > On Mar 26, 1:38?pm, John Machin wrote: >> Why are you explicitly using the zipimport module? In other words, why >> don't you put the full path of the zip file in sys.path and then just >> use the normal "import module_in_the_zip" mechanism? Note that this > > i found the the module in the zip reloaded everytime the code is > called,i mean, say ,first time it take me 1 second to call a method in > the zip,but it take another 1 second to call the mothod for another > time ,and so on Uh? Which Python version? Once a module is imported, it isn't loaded again, even from a zip file. Surely there is another reason for the delay, not the import itself. -- Gabriel Genellina From dunwitch at gmail.com Thu Apr 2 04:30:56 2009 From: dunwitch at gmail.com (Dunwitch) Date: Thu, 2 Apr 2009 01:30:56 -0700 (PDT) Subject: Display directory pyqt4 and Python References: <73j67eFvev4hU1@mid.uni-berlin.de> Message-ID: <0e42a731-7550-4064-9155-13aad0fd1695@k41g2000yqh.googlegroups.com> On Apr 2, 3:02?am, "Diez B. Roggisch" wrote: > Dunwitch schrieb: > > > > > I've looked around for the answer and have decided to ask an expert > > for the solution. Whats suppose to happen is a user pushes a button > > and displays the directory content in the text edit window on the gui. > > Everything works, apart from the fact that it only shows the last file > > in the directory, not a full listing. Here is the code for reference. > > Any help would be appreciated, I'm missing something simple. > > -- > > Dunwitch > > > # File : Video Search > > import sys, os, win32net > > > from PyQt4 import QtGui, QtCore > > from findVideos import Ui_MainWindow # UI Files from QT Designer > > > # New class derived from QMainWindow > > class TestApp(QtGui.QMainWindow): > > ? ? def __init__(self): > > ? ? ? ? QtGui.QMainWindow.__init__(self) > > ? ? ? ? self.ui = Ui_MainWindow() > > ? ? ? ? self.ui.setupUi(self) > > > ? ? ? ? # Connect the signals andslots > > ? ? ? ? QtCore.QObject.connect(self.ui.findFiles,QtCore.SIGNAL("clicked > > ()"), self.showVideoFiles) > > > #----------------------------- Code In Question > > --------------------------------- > > ? ? def showVideoFiles(self): > > ? ? ? ? enc7 = ('\\\\1.2.3.4\\somefolder') > > ? ? ? ? fileList=os.listdir(enc7) > > ? ? ? ? for x in (fileList): > > ? ? ? ? ? ? self.ui.displayVideo.setText(x) # This only shows the last > > file in the directory? > > Of course, if you always only set the *full* text of the displayVideo to > the current list-item, it will end showing the last set item. > > self.ui.displayVideo("\n".join(fileList)) > > should do the trick (of course you can get rid of the for-loop) > > Diez Thank you! I was having a rough time with that, time to keep digging deeper in the docs! From arnodel at googlemail.com Thu Apr 2 04:36:25 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Thu, 2 Apr 2009 01:36:25 -0700 (PDT) Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> Message-ID: <8e60ec51-4356-43b5-8eeb-9be99d579ea5@z1g2000yqn.googlegroups.com> Steven D'Aprano wrote: > In set theory, you start by defining the integers like this: > > 0 is the cardinality (size) of the empty set, the set with nothing in it. > > 1 is the cardinality of the set of empty sets, that is, the set > containing nothing but the empty set. > > 2 is the cardinality of the set of the empty set plus the set of empty > sets. > > 3 is the cardinality of the set containing the empty set, plus the set of > empty sets, plus the set of (the empty set plus the set of empty sets). > > And so forth, to infinity and beyond. > > Or to put it another way: > > > 0 = len( {} ) > 1 = len( {{}} ) > 2 = len( {{}, {{}}} ) > 3 = len( {{}, {{}}, {{}, {{}}} ) FWIW this is the way I learnt it AFAIK: Ordinals ======= 0 *is* the empty set 1 *is* the the the singleton composed of the empty set, i.e. {0} 2 *is* the set {0, 1} 3 *is* the set {0, 1, 2} ... n + 1 := n U {n} It's nice because: * the interval [0, n) is just the number n * n < m iff n is a subset of m iff n is a member of m Cardinals ========= A cardinal is an equivalence class under the equivalence relation S ~ S' iff there is a bijection between S and S'. Obviously, finite cardinals contain only one ordinal so finite cardinals can be identified with their ordinal representative. -- Arnaud From martin.hellwig at dcuktec.org Thu Apr 2 04:50:33 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Thu, 02 Apr 2009 09:50:33 +0100 Subject: A design problem I met again and again. In-Reply-To: References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> <158986a9-b2d2-413e-9ca0-c584299f176b@f1g2000prb.googlegroups.com> Message-ID: Steven D'Aprano wrote: > If you have too much code in one file, it will upset the balance of the > spinning hard drive platter, and it will start to wobble and maybe even > cause a head-crash. > That is why proper designed operating systems, like windows 95,rarely write one continuous block but spread the file all over the HD. -- mph From gagsl-py2 at yahoo.com.ar Thu Apr 2 05:07:43 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 02 Apr 2009 06:07:43 -0300 Subject: modifying a list element from a function References: <5fju96-rq6.ln1@rama.fbx.proxad.net> Message-ID: En Mon, 30 Mar 2009 13:19:57 -0300, TP escribi?: > Adrian Dziubek wrote: > >> Could you explain your high level goal for this? It looks like a very >> wicked way of doing things. Have You tried to read the list methods' >> documentation? Maybe there you find something you need (like >> list.index)? > > I have a "disambiguation" function that modifies a text > contained "somewhere" in each element of a list. The way this text is > accessed by the function should depend on an external accessor, or > setter, > so that any list could be processed, in a generic way. Use a "setter" function (as oposed to a "getter" that you used in your example). A direct translation woud be: def set_elem_1(elem, value): elem[1] = value def assign(setter, list_elem, new_textvalue): setter(list_elem, new_textvalue) assign(set_elem_1, a[0], "co") I imagine that you process the whole list: for elem in some_list: set_elem_1(elem, new_value) -- Gabriel Genellina From tim.wintle at teamrubber.com Thu Apr 2 05:13:48 2009 From: tim.wintle at teamrubber.com (Tim Wintle) Date: Thu, 02 Apr 2009 10:13:48 +0100 Subject: pygame and socket.recv In-Reply-To: <2084b37d-485c-435f-be5c-8ee1bc901d70@p11g2000yqe.googlegroups.com> References: <1c6574dd-1bfb-4c45-a744-58761e5d1f06@k2g2000yql.googlegroups.com> <2084b37d-485c-435f-be5c-8ee1bc901d70@p11g2000yqe.googlegroups.com> Message-ID: <1238663628.17544.15.camel@tim-laptop> On Wed, 2009-04-01 at 18:45 -0700, Aaron Brady wrote: > > My game loop looks like this: > > poll events, get 1 at most > send to server > wait for server reply > render entire frame The look I'm suggesting is: poll events write to (non-blocking) socket render frame check non-blocking socket and add events to the event queue > Yes, I am blocking for the data to come down the network. > Unfortunately, if I use any "prediction," I will have to go back and > un-render the previous frame, then redraw with the new information. Sounds like that may have to be re-factored slightly, afraid this is why real-time networked games are tough to make. > > 40 transmissions per second in each way can't be too much to ask, it's > just that they have to alternate, up one, down one. IMO It's very unlikely to be a bandwidth issue. It's more likely to just be a latency issue. > > I don't understand your solution. I can't picture it for my favorite > RTS game or the one I'm writing. Are you saying that the slower > machine just jumps ahead, and its user just doesn't have the > opportunity to make moves on the omitted frames? a) How much to move etc. is decided based on some real-time solution (not on the number of frames). Ideally all movement methods take a parameter that is a delta in time. (i.e. 1/40th of a second) b) That time is the time that is synced across machines - if machine B has to put it's timer forward 1/50 of a second, you call all the methods above with a timedelta of 1/50 before continuing. c) Obviously things like which frame a sprite is on isn't really necessary for the sake of a game, it's only game state variables that would be required. d) If you end up with jumping objects then you can create "ghost" objects for the other player's objects. When you get the state of a foreign object, update the real object with the properties, but draw the sprite at the position of your ghost object - and every frame move the ghost object towards the real one a little bit. That gets rid of the effect of jumping from one place to another, but keeps collision detection etc. correct wrt the other player. > > I am using TCP, socket.SOCK_STREAM. UDP is a potential solution, but > it still doesn't fix my main loop. > -- > http://mail.python.org/mailman/listinfo/python-list From gagsl-py2 at yahoo.com.ar Thu Apr 2 05:32:21 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 02 Apr 2009 06:32:21 -0300 Subject: py2exe fails to make valid exe References: <805f59d50903312130j7d679b77gedc5e998ff72bbfb@mail.gmail.com> Message-ID: En Wed, 01 Apr 2009 01:30:39 -0300, prakash jp escribi?: > On --> python setup.py py2exe > > the dist folder is created but the exe creted is not working it throws an > error "pythons ps.popen function" Please copy & paste the actual error message and the stack trace. > setup.py: > > #python setup.py py2exe > from distutils.core import setup > import py2exe > setup(console=['nameofprg.py']) This would be the minimal setup.py, and works with most simple programs. Note that some modules and libraries have special needs. What modules do you use in your code? -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Thu Apr 2 05:34:49 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 02 Apr 2009 06:34:49 -0300 Subject: py2exe problem References: Message-ID: En Wed, 01 Apr 2009 17:51:52 -0300, Wolfgang Forstmeier escribi?: > what kind of error do I have with getting this error at starting my app. > Im am not using IdleConf.GetOption right now. > > Warning: configHandler.py - IdleConf.GetOption - > problem retrieving configration option 'name' > from section 'Keys'. > returning default value: '' but do you use idle or idlelib in your application? -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Thu Apr 2 05:59:55 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 02 Apr 2009 06:59:55 -0300 Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <72i5t4tgfo2h4gd6ggcs02flkca85kgoku@4ax.com> <0dbc9665-3ee8-4d62-8e9e-75c6d700076e@f1g2000prb.googlegroups.com> <75d7ff1a9c13dc73ec6b4359be250ce3.squirrel@localhost> Message-ID: En Wed, 01 Apr 2009 08:04:12 -0300, andrew cooke escribi?: > something i don't think has been mentioned much - if you're using > "range()" in your python code then you're almost always doing it wrong. > > i just grepped lepl and i use range 20 times in 9600 lines of code. out > of those, all but 3 are in "quick and dirty" tests or experimental code, > not in the main library itself (which is 6300 lines). > > (1) where i need to access two adjacent members of a list, and which has > a > comment in the code explaining why it is not an error (in other words, i > was so unhappy with my code i needed to leave a note explaining why it > was > like that) From your description I guess this "range" usage could have been avoided, using enumerate instead. A silly example: for i,elem in enumerate(some_list): if i>0: prev = some_list[i-1] print (elem+prev)/2 instead of: for i in range(1, len(some_list)): elem = some_list[i] prev = some_list[i-1] print ... > (2) a use irrelevant to this discussion because i do not use the value to > an index an array. > > (3) in the rather complex implementation of a circular buffer. I can't tell, but perhaps enumerate() could have been used here too? > so in a small/moderate size library of 6000 lines (including blanks and > comments, but excluding tests and exploratory code) the only time i have > used range with array indices i was either unhappy with the code, or > implementing a complex data structure. Maybe the ratio is even less than that. -- Gabriel Genellina From digitig at gmail.com Thu Apr 2 06:01:48 2009 From: digitig at gmail.com (Tim Rowe) Date: Thu, 2 Apr 2009 11:01:48 +0100 Subject: A design problem I met again and again. In-Reply-To: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> Message-ID: 2009/4/1 ??? : > Hi all, > > I am a programmer who works with some different kinds of programming > languages, like python, C++(in COM), action script, C#, etc. > > Today, I realized that, what ever language I use, I always meet a same > problem and I think I never solve it very well. > > The problem is : how to break my app into functional pieces? One approach is to go through the specification of the program, underline all of the significant nouns and try to implement each of the nouns as a class. That won't take you all the way to a good design -- some of the resulting classes will be too trivial, and it won't give you the derived classes you need, but it's a good first step to breaking a problem down, and might help break your one big class habit. -- Tim Rowe From sjmachin at lexicon.net Thu Apr 2 06:26:48 2009 From: sjmachin at lexicon.net (John Machin) Date: Thu, 2 Apr 2009 03:26:48 -0700 (PDT) Subject: how to optimize zipimport References: <8cdf9fd4-6c87-47eb-8a9e-989336428379@v39g2000yqm.googlegroups.com> <091189e3-44f2-4f2b-b5cc-090a4d422f61@v23g2000pro.googlegroups.com> Message-ID: <6069d2e4-0ed0-481d-9e25-53f323cb72a2@j18g2000prm.googlegroups.com> On Mar 30, 11:39?pm, Coonay wrote: > On Mar 26, 1:38?pm, John Machin wrote: > > > On Mar 26, 2:06?pm, Coonay wrote: > > > > On Mar 26, 10:41?am, Coonay wrote: > > > > > in my mudule ,i import another mudule from a zip , > > > > > when i call ?my mudle method,the module in the zip will be import from > > > > the zip every time,that slow down the operation, > > > > > i try to search the some workarodnd or solution but i don't get one, > > i don't use zipimport directly,just like u said i put the zip in the > sys.path > > > i found the the module in the zip reloaded ?everytime the code is > called,i mean, say ,first time it take me 1 second to call a method in > the zip,but it take another 1 second to call the mothod for another > time ,and so on > How do you know that it is being reloaded each time a methos is called? > > Note: it may help when you reply to show a stripped-down version of > > your calling module, sufficient to back up your description of what is > > happening. We can only guess if you don't show us what is actually happening. Vague descriptions are not very useful. Show us how exactly you are importing the modules, how you are calling methods, with timing calls ... By the way, have you read this part of the documentation: "Note that if an archive only contains .py files, Python will not attempt to modify the archive by adding the corresponding .pyc or .pyo file, meaning that if a ZIP archive doesn't contain .pyc files, importing may be rather slow." ? Do you have .pyc files in your ZIP file? If not that would make the first import slower than it should be. However second, third, ... imports (if you do any) should be instantaneous -- unless you are messing about deleting modules from sys.modules. And *method* calls should be instantaneous. Does your app run at the same speed when you have all the modules in a directory instead of inside a zip file? Apart from code executed once at app start to inject the path to the zip file into sys.path, have you made any other changes to accommodate using import from zip? If so, what changes? Have you tried running your app from the shell (Windows translation: in a Command Prompt window) like this: python -v app_start.py This will show you all the imports as they happen. HTH ... but a clear description from you (plus code samples) of exactly what you are doing woul dhelp a whole lot more. John From gjango.py at gmail.com Thu Apr 2 06:47:18 2009 From: gjango.py at gmail.com (guptha) Date: Thu, 2 Apr 2009 03:47:18 -0700 (PDT) Subject: Sending SMS using python script References: <2005e94f-94de-4566-be60-7245efa4271d@y6g2000prf.googlegroups.com> Message-ID: <4221caab-bd11-4483-bcd0-00afc0f8cedb@c18g2000prh.googlegroups.com> On Apr 2, 12:41?pm, Matteo wrote: > I use a programme, written in Python, which sends sms through the sms > providers. You might want to have a look to the source code: > > http://www.moioli.net/Progetti___1/MoioSMS___Messaggi_GRATIS_da_Inter... Thanks for your reply Matteo It will be more helpful if you mention the concept of your programme. i.e .How your programme works(I can't understand the posting from your site - is not in English) As for my knowledge on this subject i can say 1. The sms send by us will reach SMS center of the service provider (example : Airtel , Vodofone etc) 2. Each service provider has their own SMS center 3. From SMS center the message reach to the recipient. In this scenario how can i make use of service providers SMS center to send my sms Is it possible to write our own sms gateway in python to interact with all type of sms centers. Do we have pay any charge to access providers SMS center, or comes free of cast. i supposes Most of mobile carriers offer free Email To SMS gateways which can be used to forward simple text emails to a mobile phones. Thanks From banibrata.dutta at gmail.com Thu Apr 2 07:00:14 2009 From: banibrata.dutta at gmail.com (Banibrata Dutta) Date: Thu, 2 Apr 2009 16:30:14 +0530 Subject: Sending SMS using python script In-Reply-To: <4221caab-bd11-4483-bcd0-00afc0f8cedb@c18g2000prh.googlegroups.com> References: <2005e94f-94de-4566-be60-7245efa4271d@y6g2000prf.googlegroups.com> <4221caab-bd11-4483-bcd0-00afc0f8cedb@c18g2000prh.googlegroups.com> Message-ID: <3de8e1f70904020400w61f489fbg5c6acfb0250495bd@mail.gmail.com> Hi Guptha: Most SMS providers (you'd be dealing with resellers mostly) have some kind of HTTP API to send SMS (even bulk SMS, upto a point). So you using standard Python HTTP module to invoke the SMS provider API. For SMS service providers, just google thru or search yellow pages in your region of operation. There are several Global providers as well, who provide ability to send SMS to almost any country around the world. As for Matteo's site, it is Italian (I think). Try Google or Altavista website translation engine, and you should manage to browse thru it. HTH, bani On Thu, Apr 2, 2009 at 4:17 PM, guptha wrote: > On Apr 2, 12:41 pm, Matteo wrote: > > I use a programme, written in Python, which sends sms through the sms > > providers. You might want to have a look to the source code: > > > > http://www.moioli.net/Progetti___1/MoioSMS___Messaggi_GRATIS_da_Inter... > > Thanks for your reply Matteo > It will be more helpful if you mention the concept of your programme. > i.e .How your programme works(I can't understand the posting from your > site - is not in English) > > As for my knowledge on this subject i can say > 1. The sms send by us will reach SMS center of the service provider > (example : Airtel , Vodofone etc) > 2. Each service provider has their own SMS center > 3. From SMS center the message reach to the recipient. > > In this scenario how can i make use of service providers SMS center to > send my sms > Is it possible to write our own sms gateway in python to interact with > all type of sms centers. > Do we have pay any charge to access providers SMS center, or comes > free of cast. > > i supposes Most of mobile carriers offer free Email To SMS gateways > which can be used to forward simple text emails to a mobile phones. > Thanks > -- > http://mail.python.org/mailman/listinfo/python-list > -- regards, Banibrata http://www.linkedin.com/in/bdutta -------------- next part -------------- An HTML attachment was scrubbed... URL: From mailing at supai.de Thu Apr 2 07:06:22 2009 From: mailing at supai.de (Wolfgang Forstmeier) Date: Thu, 02 Apr 2009 13:06:22 +0200 Subject: py2exe problem In-Reply-To: References: Message-ID: On 02.04.2009 11:34, Gabriel Genellina wrote: > En Wed, 01 Apr 2009 17:51:52 -0300, Wolfgang Forstmeier > escribi?: > >> what kind of error do I have with getting this error at starting my app. >> Im am not using IdleConf.GetOption right now. >> >> Warning: configHandler.py - IdleConf.GetOption - >> problem retrieving configration option 'name' >> from section 'Keys'. >> returning default value: '' > > but do you use idle or idlelib in your application? > Ah yes, after some research in my code I found the following import from idlelib.OutputWindow import tkMessageBox Maybe someone could assist me with fixing that issue, because I am really new to python and does not exactly know what to do now. From dunmer at dreams.sk Thu Apr 2 07:36:24 2009 From: dunmer at dreams.sk (Gabriel) Date: Thu, 02 Apr 2009 13:36:24 +0200 Subject: Low level hard drive reading Message-ID: <49D4A338.6030500@dreams.sk> Hello, I have to write linux application that will analyze disk/partition (ext3 filesystem) on really low level. It has to find/analyze files on the disk by reading disk blocks to analyze file's headers to find out file type and then blocks related to file to get file content. The second part have to be searching deleted files by this blocks reading (is this even possible?) Can i do this in python? For example can i open disk image file and read it block by block? Or is there even better solution? .) I tried search web but I wasn't successful.. I will appreciate any help. Thank you in advice.. Gabriel From andrew at acooke.org Thu Apr 2 07:45:46 2009 From: andrew at acooke.org (andrew cooke) Date: Thu, 2 Apr 2009 07:45:46 -0400 (CLT) Subject: A design problem I met again and again. In-Reply-To: References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> <158986a9-b2d2-413e-9ca0-c584299f176b@f1g2000prb.googlegroups.com> Message-ID: <7971934f435013b5640855ea7dd2bb8c.squirrel@localhost> Lawrence D'Oliveiro wrote: >> What are the average size of source files in your project? If it's >> far lower than 15,000, don't feel it's a little unbalance? > > Why? one reason is that it becomes inefficient to find code. if you structure code as a set of nested packages, then a module, and finally classes and methods, then you have a tree structure. and if you divide the structure along semantic lines then you can efficiently descend the tree to find what you want. if you choose the division carefully you can get a balanced tree, giving O(log(n)) access time. in contrast a single file means a linear scan, O(n). (i am talking about human use here - people reading and trying to understand code, perhaps during debugging or code review or whatever). andrew (you could argue that the file contents can be sorted in some way - you could even map from the tree to the file a traversal - but in practice humans seem to be a lot better at making a series of decisions descending a tree than holding the entire structure in their head as a sort order) From murali.au at gmail.com Thu Apr 2 07:49:11 2009 From: murali.au at gmail.com (Murali kumar) Date: Thu, 2 Apr 2009 17:19:11 +0530 Subject: speech recognition help In-Reply-To: <49C50372.5050600@gmail.com> References: <49C3DD60.3070701@gmail.com> <49C4F4ED.9040709@tim.thechases.com> <49C50372.5050600@gmail.com> Message-ID: hi all.. I tried following tools by ur advice.. 1. cmu sphinx: tried and managed run the demo programs.. but the accuracy is so bad. for that i tried for how to train the grammer. but for that i had no guidance.. docs give me the headache.. after 2 weeks.. i concluded that it will not be commercially useful for distributed application. 2. Naturally speaking: downloads only provided for cost. so no try. 3. windows sapi: windows provides redistributeable copies. also there are some python bindings available for to connect this engine. training with each user is easy in windows sapi ( for that i choosed this.) only limitation is usage within windows. so , concluded that using windows sapi.. am i correct? if u know any best articles/links for using windows sapi from python.. kindly give me that.. On Sat, Mar 21, 2009 at 8:40 PM, Stef Mientki wrote: > Tim Chase wrote: > >> do u know which one >>> 1. cmu sphinx >>> 2. natural speaking >>> 3. windows sapi >>> >>> is best ( in accuray and speed ) for predefined vocabulary.. and worth >>> for >>> learning as well.? >>> >> >> For a pre-defined vocabulary, they should all be pretty good. In general >> (for non-predefined vocabularies), I've heard that NS beats both Sphinx and >> SAPI in terms of recognition quality. >> > +1 > >> I haven't seen any comparisons between Sphinx and SAPI. >> >> Other considerations may also come into play: >> >> - do you expect all your users to have a copy of NS? Then you might have >> to look into something other than NS if they won't. Or perhaps NS has a >> developers' toolkit that allows for redistribution when built into your app. >> >> - do you expect all your users to run Windows (as I type this from my >> Linux box...)? If not, then SAPI won't be available to your >> Mac/Linux/BSD/BeOS/Amiga/whatever users. >> >> - do you need TTS (text-to-speech) capabilities as well? SAPI includes >> them. (there are other TTS libraries available such as mbrola/festival that >> are available for this if needed) >> >> - how easy is it to bind to Python? Sphinx has Python bindings detailed >> on the website. I can't speak for NS or SAPI. >> > both are covered by DragonFly > > Stef > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From paul at boddie.org.uk Thu Apr 2 08:12:16 2009 From: paul at boddie.org.uk (Paul Boddie) Date: Thu, 2 Apr 2009 05:12:16 -0700 (PDT) Subject: Python Goes Mercurial References: <7x7i2499si.fsf@ruckus.brouhaha.com> <7a1dd0d8-1978-470b-a80d-57478d7f7f9e@q16g2000yqg.googlegroups.com> Message-ID: <3e682785-7976-4c1b-993e-eadba3e3503e@37g2000yqp.googlegroups.com> On 2 Apr, 04:27, Lawrence D'Oliveiro wrote: > In message <7a1dd0d8-1978-470b- > > a80d-57478d7f7... at q16g2000yqg.googlegroups.com>, Paul Boddie wrote: > > And I've heard stories of "bait and > > switch" with Git: "you can do XYZ with Git but not with ..." followed > > by the discovery that you can't realistically do XYZ with Git, either. > > Cite? Well, I "heard" stories rather than read them, so I can't cite them, but I believe that one argument crafted to favour Git was that it is great for history editing, but it turns out that it isn't so great, as the following commentary points out: "You can do it, but as soon as you go to merge with another repo that had the unedited commit history, you?ll bump into weirdness (and probably invalidate your whole reason for rebasing, which was to clean up the history)." - http://adam.blog.heroku.com/past/2008/6/30/rebasing_is_editing_commits/ I'm sure other people have their own tales of a similar nature. Paul From jeremiah.dodds at gmail.com Thu Apr 2 08:16:21 2009 From: jeremiah.dodds at gmail.com (Jeremiah Dodds) Date: Thu, 2 Apr 2009 13:16:21 +0100 Subject: Low level hard drive reading In-Reply-To: <49D4A338.6030500@dreams.sk> References: <49D4A338.6030500@dreams.sk> Message-ID: <12cbbbfc0904020516v58b7f6a5vce11fe956ea73387@mail.gmail.com> On Thu, Apr 2, 2009 at 12:36 PM, Gabriel wrote: > Hello, > I have to write linux application that will analyze disk/partition (ext3 > filesystem) on really low level. It has to find/analyze files on the disk by > reading disk blocks to analyze file's headers to find out file type and > then blocks related to file to get file content. The second part have to > be searching deleted files by this blocks reading (is this even possible?) > > Can i do this in python? For example can i open disk image file and read it > block by block? > Or is there even better solution? .) I tried search web but I wasn't > successful.. > > I will appreciate any help. Thank you in advice.. > > Gabriel > -- > http://mail.python.org/mailman/listinfo/python-list I may be wrong, but I'd assume that you could just read by block size, opening the file that represents the disk in /dev (/dev/sda or similar). I don't see why you would want to do that to find out file type/headers though. You should be able to see the contents of "deleted" files that haven't been (entirely) overwritten on disk - I believe that when you delete a file, you really just delete the inode, or reference that tells the filesystem that there's a file there. Someone else who's keener on the details of these things will probably come with better information. -------------- next part -------------- An HTML attachment was scrubbed... URL: From jeremiah.dodds at gmail.com Thu Apr 2 08:19:16 2009 From: jeremiah.dodds at gmail.com (Jeremiah Dodds) Date: Thu, 2 Apr 2009 13:19:16 +0100 Subject: Python Goes Mercurial In-Reply-To: <3e682785-7976-4c1b-993e-eadba3e3503e@37g2000yqp.googlegroups.com> References: <7x7i2499si.fsf@ruckus.brouhaha.com> <7a1dd0d8-1978-470b-a80d-57478d7f7f9e@q16g2000yqg.googlegroups.com> <3e682785-7976-4c1b-993e-eadba3e3503e@37g2000yqp.googlegroups.com> Message-ID: <12cbbbfc0904020519p20d625baid0674f9ecb7cf64f@mail.gmail.com> The one thing that makes me want to use git more than any other dvcs is that you don't have to create a new directory for branches. This may be possible in other dvcs's , but git is the only one I've seen advertise the capability. -------------- next part -------------- An HTML attachment was scrubbed... URL: From murali.au at gmail.com Thu Apr 2 08:22:13 2009 From: murali.au at gmail.com (Murali kumar) Date: Thu, 2 Apr 2009 17:52:13 +0530 Subject: python installer Message-ID: hi all.. To distribute my application.. what will be the best..? python installer.. or py2exe..? using : python 2.6 , wxpython 2.8.9 -------------- next part -------------- An HTML attachment was scrubbed... URL: From ndbecker2 at gmail.com Thu Apr 2 08:32:51 2009 From: ndbecker2 at gmail.com (Neal Becker) Date: Thu, 02 Apr 2009 08:32:51 -0400 Subject: simple iterator question Message-ID: How do I interleave 2 sequences into a single sequence? How do I interleave N sequences into a single sequence? From arve.knudsen at gmail.com Thu Apr 2 08:35:53 2009 From: arve.knudsen at gmail.com (arve.knudsen at gmail.com) Date: Thu, 2 Apr 2009 05:35:53 -0700 (PDT) Subject: Incomplete exception tracebacks when importing from zipped modules Message-ID: I can't seem to get complete tracebacks when modules imported from zip archives raise exceptions. For instance, consider the following module: def i_raise(): raise Exception("Test!") i_raise() When I import this module, within a .zip, from a script, I get the following traceback: Traceback (most recent call last): File "tst.py", line 1, in import tst_mod File "build\bdist.win32\egg\tst_mod.py", line 4, in File "build\bdist.win32\egg\tst_mod.py", line 2, in i_raise Exception: Test! As you can see, the code for each stack entry is omitted. Is this normal?? traceback.print_exc() has the same problem, BUT; if I happen to call traceback.print_stack within tst_mod, traceback.print_exc prints a complete traceback in the calling script (i.e., including source code per stack frame). Mysterious .. From jud779 at gmail.com Thu Apr 2 08:41:12 2009 From: jud779 at gmail.com (jud779 at gmail.com) Date: Thu, 2 Apr 2009 05:41:12 -0700 (PDT) Subject: xml to xhtml References: Message-ID: On Apr 1, 11:16?am, Joe Riopel wrote: > On Wed, Apr 1, 2009 at 10:43 AM, ? wrote: > > If anyone can give me some guidance what should be the best way to > > generate html/xhtml page using python would be great. I am open to > > other options like xsl or anything else that can make things simple. > > Since you're open to other options, I would take a look at XSLT. > > http://www.w3schools.com/xsl/ XSLT sounds good. I have already created temporary XSL and its working fine. Still question is how to put up this on web server index.html. I am planning to create one index.html and call cgi python which in return produces html page from xml/xsl. Does this sounds like a good plan?? Is there any other options than the one i describe above. If, then pls help me out. Thanks in advance From gjango.py at gmail.com Thu Apr 2 08:45:18 2009 From: gjango.py at gmail.com (guptha) Date: Thu, 2 Apr 2009 05:45:18 -0700 (PDT) Subject: Any library to bind python with Yahoo mail service Message-ID: Hi friends, I came across libgmail library that can be used to bind python with Google Gmail's service .Likewise is there any library available to access the service of Yahoo mail from python code . I like to send sms from my application via Yahoo mail Service . Thanks From cournape at gmail.com Thu Apr 2 08:45:36 2009 From: cournape at gmail.com (David Cournapeau) Date: Thu, 2 Apr 2009 21:45:36 +0900 Subject: Python Goes Mercurial In-Reply-To: <3e682785-7976-4c1b-993e-eadba3e3503e@37g2000yqp.googlegroups.com> References: <7x7i2499si.fsf@ruckus.brouhaha.com> <7a1dd0d8-1978-470b-a80d-57478d7f7f9e@q16g2000yqg.googlegroups.com> <3e682785-7976-4c1b-993e-eadba3e3503e@37g2000yqp.googlegroups.com> Message-ID: <5b8d13220904020545t24788905uafbee618527ba6db@mail.gmail.com> On Thu, Apr 2, 2009 at 9:12 PM, Paul Boddie wrote: > > "You can do it, but as soon as you go to merge with another repo that > had the unedited commit history, you?ll bump into weirdness (and > probably invalidate your whole reason for rebasing, which was to clean > up the history)." > > ?- http://adam.blog.heroku.com/past/2008/6/30/rebasing_is_editing_commits/ > > I'm sure other people have their own tales of a similar nature. It is explained in this article why rebase can't be used for something which will be the base for upcoming merges; not all branches are intended this way (but most public ones are). I would say this shows one feature which I think matters a lot in git, more than rebasing itself: multiple branches in a repo, and very cheap branching (in CPU cost, space and workflow) so that you can use private branches for experimentation. cheers, David From kay.schluehr at gmx.net Thu Apr 2 08:53:20 2009 From: kay.schluehr at gmx.net (Kay Schluehr) Date: Thu, 2 Apr 2009 05:53:20 -0700 (PDT) Subject: Python Goes Mercurial References: <35d429fa-5d13-4703-a443-6a95c740c9fb@o6g2000yql.googlegroups.com> Message-ID: <4dad5e13-153f-446d-9f2b-06ebc653e003@c9g2000yqm.googlegroups.com> On 1 Apr., 07:56, Lawrence D'Oliveiro wrote: > In message <35d429fa-5d13-4703- > > a443-6a95c740c... at o6g2000yql.googlegroups.com>, John Yeung wrote: > > Here's one that clearly expresses strong antipathy: > > > http://mail.python.org/pipermail/python-dev/2009-March/087971.html > > There are lots of GUI- and Web-based front ends to Git. And look at on-line > services like GitHub and Gitorious. The level of support for it is huge. Ironically Mercurials most popular UI frontend Tortoise is going to crash Python tools ( like Wing-IDE ) on Windows. That's a known issue for about a year and more and the developers are not inclined to fix it. This doesn't really increase my trust that Mercurials UI tools are of a higher quality than Git's no matter which platform is used. From george.sakkis at gmail.com Thu Apr 2 08:56:00 2009 From: george.sakkis at gmail.com (George Sakkis) Date: Thu, 2 Apr 2009 05:56:00 -0700 (PDT) Subject: simple iterator question References: Message-ID: On Apr 2, 8:32?am, Neal Becker wrote: > How do I interleave 2 sequences into a single sequence? > > How do I interleave N sequences into a single sequence? http://lmgtfy.com/?q=python+interleave+sequences http://code.activestate.com/recipes/511480/ http://code.activestate.com/recipes/528936/ HTH, George From dns4 at cornell.edu Thu Apr 2 09:05:14 2009 From: dns4 at cornell.edu (David Smith) Date: Thu, 02 Apr 2009 09:05:14 -0400 Subject: Python Goes Mercurial In-Reply-To: <4dad5e13-153f-446d-9f2b-06ebc653e003@c9g2000yqm.googlegroups.com> References: <35d429fa-5d13-4703-a443-6a95c740c9fb@o6g2000yql.googlegroups.com> <4dad5e13-153f-446d-9f2b-06ebc653e003@c9g2000yqm.googlegroups.com> Message-ID: Kay Schluehr wrote: > On 1 Apr., 07:56, Lawrence D'Oliveiro central.gen.new_zealand> wrote: >> In message <35d429fa-5d13-4703- >> >> a443-6a95c740c... at o6g2000yql.googlegroups.com>, John Yeung wrote: >>> Here's one that clearly expresses strong antipathy: >>> http://mail.python.org/pipermail/python-dev/2009-March/087971.html >> There are lots of GUI- and Web-based front ends to Git. And look at on-line >> services like GitHub and Gitorious. The level of support for it is huge. > > Ironically Mercurials most popular UI frontend Tortoise is going to > crash Python tools ( like Wing-IDE ) on Windows. That's a known issue > for about a year and more and the developers are not inclined to fix > it. This doesn't really increase my trust that Mercurials UI tools are > of a higher quality than Git's no matter which platform is used. The conflict between TortoiseHg and Wing IDE can be fixed by simply uninstalling the Tortoise Overlays. You loose the graphic overlay on folders, but otherwise everything works. --David From andrew at acooke.org Thu Apr 2 09:05:42 2009 From: andrew at acooke.org (andrew cooke) Date: Thu, 2 Apr 2009 09:05:42 -0400 (CLT) Subject: simple iterator question In-Reply-To: References: Message-ID: <09a806b598d550097c9f1c9234f17f7c.squirrel@acooke.dyndns.org> Neal Becker wrote: > How do I interleave 2 sequences into a single sequence? > > How do I interleave N sequences into a single sequence? don't know if there's a better way, but from itertools import izip def interleave(*doodahs): for together in izip(*doodahs): for single in together: yield single seems like it should work (warning: untried). will terminate on shortest length doodah. andrew From castironpi at gmail.com Thu Apr 2 09:07:13 2009 From: castironpi at gmail.com (Aaron Brady) Date: Thu, 2 Apr 2009 06:07:13 -0700 (PDT) Subject: pygame and socket.recv References: <1c6574dd-1bfb-4c45-a744-58761e5d1f06@k2g2000yql.googlegroups.com> Message-ID: On Apr 2, 1:19?am, "Hendrik van Rooyen" wrote: > "Aaron Brady" ?wrote: > > > Hi, > > > I tried writing a small game on a pygame layer. ?The graphics are > > fine, and at the moment, it is not graphics intensive. ?It is multi- > > player, and for the communication, I am sending a pickle string across > > a LAN, once per frame. > > How big is this pickle - i.e. how many packets does it translate to > on the wire, given that an ethernet packet is less than 1500 bytes? The client sends a dictionary to the server. If it doesn't have any news, it's an empty dictionary, ~10 bytes. If it does, it's one or two entries, a short string to a short string or integer, ~40 bytes total. The server sends a dictionary back: { str: str, str: num, str: { int: X, int: Y, ..., int: Z } }, where X, Y and Z are the dictionaries it received from the individual clients, ~100 bytes total. I don't anticipate this number growing past 1K at later stages in the game, and I can always make it a fixed-length struct or CSV. > What is the maximum number of full size packets that a simple > echo server will echo per second on your setup? ?I predict that > you will find that this is far less than what a calculation based on > the bit rate will suggest... The test on localhost->localhost and back reached 700 cycles per second. The test on localhost->other machine and back ranged 100-300 cycles per second, IIRC recently. > > I'm observing some latency. ?It seems that socket.recv isn't > > performing consistently. ?The server is using time.clock and > > time.sleep to keep the frame rate at 40 frames per second. ?Latency > > occurred regardless of whether the connection was ethernet or > > wireless, although worse with wireless. > > What does "some latency" mean? - barely visible jitter, or a half > second freeze? I've got a rather amateur loop keeping the frame rate capped at 40 fps. The game won't exceed that, but when it drops /below/ it, it can fall to 30 or 20 or 15 fps. That was just a per-second averaging calculation; the bad freezes are about half a second. I'll try to get a reading of individual calls to 'recv' that are themselves below the threshold. Here's the loop: wait half the time left, then half that, then half that, and so on, until less than 1/1000th second is left. while 1: tcurr= time.clock( ) delay= ( ( tprev+ secsperframe )- tcurr )* 0.5 if delay< 0.001: break time.sleep( delay ) tprev= tcurr I don't know if it could be responsible. Would a mutex or Queue be better using a threading.Timer? > What do you do when the total round trip time takes longer > than your target of one fortieth of a second? I want to know that too! > The fact that wireless is worse (54Mb/s? - down from 100Mb/s) > makes me think you are running out of bandwidth, or that the > LAN is busy and that the bandwidth is wasted on collisions. Another party (friend, participant, colleague, etc.) and I tested it with wires. It improved it, but didn't eliminate it. > What happens when you drop the frame rate to 20 per second? > (it is parameterised, isn't it?) Yes. It still gets half-second freezes, and the aggregate per-second average still reads as low as 15. Yes it's parameterized, in the 'secsperframe' variable, seconds per frame, or fps**-1. > > Does anyone have any hints or suggestions? ?I am on Windows XP. > > What else is running on the machines at the same time? - your game > may be sucking at the hind tit of processor time... On one test, one machine ran only the server, and the other machine ran only one client! That is where the numbers I gave were generated. When one machine runs a server and a client, it's not much worse, though I didn't try that since I moved the delay loop to the client side. Thank you for your questions! From manu3d at gmail.com Thu Apr 2 09:07:20 2009 From: manu3d at gmail.com (Emanuele D'Arrigo) Date: Thu, 2 Apr 2009 06:07:20 -0700 (PDT) Subject: Class methods read-only by default? Message-ID: <7497f42a-46f0-46ce-9035-087da489205c@l10g2000vba.googlegroups.com> Hi Everybody! I just tried this: >>> class C(object): ... def method(self): ... pass ... >>> c = C() >>> delattr(c, "method") Traceback (most recent call last): File "", line 1, in AttributeError: 'C' object attribute 'method' is read-only How come? Who told the class to make the method read-only? I didn't! Manu From tadwelessar at gmail.com Thu Apr 2 09:08:07 2009 From: tadwelessar at gmail.com (Matteo) Date: Thu, 2 Apr 2009 06:08:07 -0700 (PDT) Subject: Sending SMS using python script References: <2005e94f-94de-4566-be60-7245efa4271d@y6g2000prf.googlegroups.com> <4221caab-bd11-4483-bcd0-00afc0f8cedb@c18g2000prh.googlegroups.com> Message-ID: <57195737-dcbe-4257-b7c8-54f043a08602@37g2000yqp.googlegroups.com> I don't really know, because I didn't write it myself ;) I think it basically logs in into a service provider site with pycurl, follows the right links, reads captcha's and writes an SMS, which is then sent by the provider itself. I can give you the direct link to the source code from the same site: http://www.moioli.net/files/MoioSMS2.18-src.zip From kdr2 at x-macro.com Thu Apr 2 09:11:54 2009 From: kdr2 at x-macro.com (KDr2) Date: Thu, 2 Apr 2009 21:11:54 +0800 Subject: simple iterator question In-Reply-To: References: Message-ID: >>> def xl(a): ... return list(reduce(lambda x,y:x+y,zip(*a))) ... >>> xl([[1,2],[3,4]]) [1, 3, 2, 4] like this? Best Regards, -- KDr2, at x-macro.com. On Thu, Apr 2, 2009 at 8:32 PM, Neal Becker wrote: > How do I interleave 2 sequences into a single sequence? > > How do I interleave N sequences into a single sequence? > > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From mail at johnohagan.com Thu Apr 2 09:15:05 2009 From: mail at johnohagan.com (John O'Hagan) Date: Thu, 2 Apr 2009 13:15:05 +0000 Subject: simple iterator question In-Reply-To: References: Message-ID: <200904021315.05375.mail@johnohagan.com> On Thu, 2 Apr 2009, Neal Becker wrote: > How do I interleave 2 sequences into a single sequence? > > How do I interleave N sequences into a single sequence? Here's one way: def interleave(*args): for n in range(min(len(i) for i in args)) : for i in args: yield i[n] HTH, John From nagraj.sigma at gmail.com Thu Apr 2 09:20:46 2009 From: nagraj.sigma at gmail.com (nagraj) Date: Thu, 2 Apr 2009 06:20:46 -0700 (PDT) Subject: Python wrapper for Ebay Message-ID: Hi Can anyone suggest me which is the best python wrapper for Ebay APIs? I've currently gone thru PyEbay and EasyBay. But have not seen much activities on them since couple of years. Also I'm not able to find any particular forums or discussion groups. I could find PyAWS for Amazon which is really helpful. I'm wondering whether I shud write the wrapper classes all by myself in this case? Please suggest. Thanks N From lenglish5 at cox.net Thu Apr 2 09:21:05 2009 From: lenglish5 at cox.net (Lawson English) Date: Thu, 02 Apr 2009 06:21:05 -0700 Subject: Python Client for Second Life Message-ID: <49D4BBC1.1010704@cox.net> hey all, a reminder for anyone interested in the python second life client, pyogp, we have our own irc channel on freenode called pyogp, and our own in-world discussion group, also called pyogp. Lawson (Saijanai in Second Life) _______________________________________________ mmox mailing list mmox at ietf.org https://www.ietf.org/mailman/listinfo/mmox pyogp mailing list https://lists.secondlife.com/cgi-bin/mailman/listinfo/pyogp http://wiki.secondlife.com/wiki/Pyogp From andrew at acooke.org Thu Apr 2 09:30:46 2009 From: andrew at acooke.org (andrew cooke) Date: Thu, 2 Apr 2009 09:30:46 -0400 (CLT) Subject: Class methods read-only by default? In-Reply-To: <7497f42a-46f0-46ce-9035-087da489205c@l10g2000vba.googlegroups.com> References: <7497f42a-46f0-46ce-9035-087da489205c@l10g2000vba.googlegroups.com> Message-ID: Emanuele D'Arrigo wrote: > Hi Everybody! > > I just tried this: > >>>> class C(object): > ... def method(self): > ... pass > ... >>>> c = C() >>>> delattr(c, "method") > > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'C' object attribute 'method' is read-only > > How come? Who told the class to make the method read-only? I didn't! i'm just guessing, but i suspect this is because of how it's implemented. class methods don't exist "in" the instance. instead, they exist in the class (which is itself an "object", hence metaclasses etc), and the instance forwards them to the class for evaluation (if the term vtable makes any sense to you then the vtable for class methods is owned by the class, not the instance). so you may be able to delete the method from the class, but then it will affect all instances. you can't delete it from one instance because it's not in that instance. as i said, just a guess, based on vague ideas about how python works. andrew From sion at viridian.paintbox Thu Apr 2 09:44:38 2009 From: sion at viridian.paintbox (Sion Arrowsmith) Date: Thu, 02 Apr 2009 13:44:38 GMT Subject: Help with dict and iter References: <49cf8dc4$0$17431$4fafbaef@reader5.news.tin.it> Message-ID: mattia wrote: > So, I'm looking for a way to "reset" the next() value every >time i complete the scan of a list. itertools.cycle ? -- \S under construction From kushal.kumaran at gmail.com Thu Apr 2 09:49:57 2009 From: kushal.kumaran at gmail.com (Kushal Kumaran) Date: Thu, 2 Apr 2009 19:19:57 +0530 Subject: Low level hard drive reading References: Message-ID: <20090402191957.0ad87d40@oxygen> On Thu, 02 Apr 2009 13:36:24 +0200 Gabriel wrote: > Hello, > I have to write linux application that will analyze disk/partition > (ext3 filesystem) on really low level. It has to find/analyze files > on the disk by reading disk blocks to analyze file's headers to find > out file type and then blocks related to file to get file content. > The second part have to be searching deleted files by this blocks > reading (is this even possible?) > > Can i do this in python? For example can i open disk image file and > read it block by block? > Or is there even better solution? .) I tried search web but I wasn't > successful.. > > I will appreciate any help. Thank you in advice.. > Searching for "ext3 recovering deleted files" has this as one of the results: http://projects.izzysoft.de/trac/ext3undel/ That said, if you want to read the disk, you can simply open the appropriate device (maybe one of the /dev/[hs]d*) and read it. You'll need root access, and knowledge of the on-disk layout of the filesystem. Also, writing to the wrong place into this file can badly mess up your filesystem, or even partition table. A good backup of the entire disk is a very good idea. -- kushal From castironpi at gmail.com Thu Apr 2 09:50:16 2009 From: castironpi at gmail.com (Aaron Brady) Date: Thu, 2 Apr 2009 06:50:16 -0700 (PDT) Subject: pygame and socket.recv References: <1c6574dd-1bfb-4c45-a744-58761e5d1f06@k2g2000yql.googlegroups.com> <2084b37d-485c-435f-be5c-8ee1bc901d70@p11g2000yqe.googlegroups.com> Message-ID: On Apr 2, 4:13?am, Tim Wintle wrote: > On Wed, 2009-04-01 at 18:45 -0700, Aaron Brady wrote: > > > My game loop looks like this: > > > poll events, get 1 at most > > send to server > > wait for server reply > > render entire frame > > The look I'm suggesting is: > > poll events > write to (non-blocking) socket > render frame > check non-blocking socket and add events to the event queue > > > Yes, I am blocking for the data to come down the network. > > Unfortunately, if I use any "prediction," I will have to go back and > > un-render the previous frame, then redraw with the new information. > > Sounds like that may have to be re-factored slightly, afraid this is why > real-time networked games are tough to make. +1 understatement of the week. Are you saying that there aren't that many out there? I.e., if average and worst-case packet times were faster, there would be more? > > 40 transmissions per second in each way can't be too much to ask, it's > > just that they have to alternate, up one, down one. > > IMO It's very unlikely to be a bandwidth issue. It's more likely to just > be a latency issue. The actual mechanics can't be that hard. If I had a dedicated pathway that the OS didn't even intervene in, 40 fps would be a trifle; and I would also have a custom OS. 'Doom 25: Now including operating system!' > > I don't understand your solution. ?I can't picture it for my favorite > > RTS game or the one I'm writing. ?Are you saying that the slower > > machine just jumps ahead, and its user just doesn't have the > > opportunity to make moves on the omitted frames? > > a) How much to move etc. is decided based on some real-time solution > (not on the number of frames). Ideally all movement methods take a > parameter that is a delta in time. (i.e. 1/40th of a second) > > b) That time is the time that is synced across machines - if machine B > has to put it's timer forward 1/50 of a second, you call all the methods > above with a timedelta of 1/50 before continuing. I must be writing a very peculiar game. Frame-for-frame matching is important. The game space is a grid, a logical space, zero mass, not a real one. I started to tell Terry yesterday that arbitrary discontinuities in position and momentum are possible. Your method ensures than an object is moving at a constant rate, distance over time, as it is perceived by the player. I want something like it, so that if I'm anticipating an upcoming turn, I can press the key at the time I want, and the model on every machine is informed, regardless of what the graphics did in the interim. > c) Obviously things like which frame a sprite is on isn't really > necessary for the sake of a game, it's only game state variables that > would be required. Yes, this. > d) If you end up with jumping objects then you can create "ghost" > objects for the other player's objects. When you get the state of a > foreign object, update the real object with the properties, but draw the > sprite at the position of your ghost object - and every frame move the > ghost object towards the real one a little bit. That gets rid of the > effect of jumping from one place to another, but keeps collision > detection etc. correct wrt the other player. It's just that if you register a collision in between the time that one object has changed its position and momentum, and the time you learn about it, you have to retroactively edit the collision, restore hit points, and recalculate the other object's position and momentum, to name a few. For example, if you hit some kind of power-up that warps your position to another place, you can't get hit by a weapon that is colliding with your old extrapolated place. Maybe each collision has to take a branch: if the collision turns out to have succeeded, proceed from state X->Y1, else from state X->Y2. That sounds combinatoric, when multiple collisions may or may not be interacting. { (X,Y)->(X1,Y1), (X,Y)->(X1,Y2), (X,Y)->(X2,Y1), (X,Y)-> (X2,Y1) }. Or, you could just freeze the models of some objects pending update, and continue with the models of the rest. This is all not to mention the balance between pre-transmission and post-transmission calculations, which is a time-space trade-off IMCAC if my calculations are correct. From castironpi at gmail.com Thu Apr 2 09:55:31 2009 From: castironpi at gmail.com (Aaron Brady) Date: Thu, 2 Apr 2009 06:55:31 -0700 (PDT) Subject: complaints about no replies last week References: <2d80ec1b-5eb5-4e82-9a4a-36934dd53800@z9g2000yqi.googlegroups.com> Message-ID: <31722b49-8fb5-418e-bc03-e57bca91c0fb@f19g2000yqo.googlegroups.com> On Mar 31, 4:34?pm, Arnaud Delobelle wrote: > a... at pythoncraft.com (Aahz) writes: > > Arnaud Delobelle ? wrote: > > >>There are no comments - I don't have the time to add any, sorry! > > > The margin is too small to contain the proof? > > I wish I could come up with such a resilient conjecture! > > Ah but in this case, the proof is in the program, as Curry-Howard may > well have said. Is it simple, complex, or complicated? IS IT FLAT OR NESTED! From steve at REMOVE-THIS-cybersource.com.au Thu Apr 2 09:56:01 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 02 Apr 2009 13:56:01 GMT Subject: Class methods read-only by default? References: <7497f42a-46f0-46ce-9035-087da489205c@l10g2000vba.googlegroups.com> Message-ID: <01e4b83f$0$20654$c3e8da3@news.astraweb.com> On Thu, 02 Apr 2009 06:07:20 -0700, Emanuele D'Arrigo wrote: > Hi Everybody! > > I just tried this: > >>>> class C(object): > ... def method(self): > ... pass > ... >>>> c = C() >>>> delattr(c, "method") > > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'C' object attribute 'method' is read-only > > How come? Who told the class to make the method read-only? I didn't! Nobody told the class to make the method read-only, because it isn't. You are trying to delete an *instance* attribute (one that lives inside the instance dictionary) but the method is a *class* attribute (it lives inside the class dictionary). >>> class C(object): ... def method(self): pass ... >>> c = C() >>> C.__dict__.keys() # class attributes ['__dict__', '__module__', '__weakref__', 'method', '__doc__'] >>> c.__dict__.keys() # instance attributes [] To remove the method, this will work: delattr(C, 'method') # call on the class But beware, if method is defined in a superclass of C, then that won't work either. This is a little confusing, it is a definite Gotcha, because getattr succeeds. But notice: >>> getattr(c, 'method') > See how it tells you that c.method is actually C.method? -- Steven From kushal.kumaran at gmail.com Thu Apr 2 10:05:15 2009 From: kushal.kumaran at gmail.com (Kushal Kumaran) Date: Thu, 2 Apr 2009 19:35:15 +0530 Subject: Any library to bind python with Yahoo mail service References: Message-ID: <20090402193515.166e926e@oxygen> On Thu, 2 Apr 2009 05:45:18 -0700 (PDT) guptha wrote: > Hi friends, > I came across libgmail library that can be used to bind python with > Google Gmail's service .Likewise is there any library available to > access the service of Yahoo mail from python code . > I like to send sms from my application via Yahoo mail Service . > Thanks You can always simply use the mail handling libraries available in python. I'm not familiar with sms sending facilities provided by Yahoo, but if it only requires sending a mail, you can simply use smtplib. -- kushal From sion at viridian.paintbox Thu Apr 2 10:15:10 2009 From: sion at viridian.paintbox (Sion Arrowsmith) Date: Thu, 02 Apr 2009 14:15:10 GMT Subject: simple iterator question References: Message-ID: Neal Becker wrote: >How do I interleave 2 sequences into a single sequence? > >How do I interleave N sequences into a single sequence? itertools.chain(*itertools.izip(*Nsequences)) -- \S under construction From steve at REMOVE-THIS-cybersource.com.au Thu Apr 2 10:30:55 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 02 Apr 2009 14:30:55 GMT Subject: A design problem I met again and again. References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> <158986a9-b2d2-413e-9ca0-c584299f176b@f1g2000prb.googlegroups.com> Message-ID: <01e4c06c$0$20654$c3e8da3@news.astraweb.com> On Thu, 02 Apr 2009 07:45:46 -0400, andrew cooke wrote: > Lawrence D'Oliveiro wrote: >>> What are the average size of source files in your project? If it's >>> far lower than 15,000, don't feel it's a little unbalance? >> >> Why? > > one reason is that it becomes inefficient to find code. if you > structure code as a set of nested packages, then a module, and finally > classes and methods, then you have a tree structure. and if you divide > the structure along semantic lines then you can efficiently descend the > tree to find what you want. if you choose the division carefully you > can get a balanced tree, giving O(log(n)) access time. in contrast a > single file means a linear scan, O(n). What's n supposed to be? The number of lines in a file? No, I don't think so -- you said it yourself: "if you divide the structure along semantic lines then you can efficiently descend the tree to find what you want". Not "arbitrarily divide the files after n lines". If one semantic division requires 15,000 lines, and another semantic division requires 15 lines, then the most efficient way to divide the code base is 15,000 lines in one module and 15 lines in another. Admittedly, I'd expect that any python module with 15,000 lines (approximately 900KB in size) could do with some serious refactoring into modules and packages, but hypothetically it could genuinely make up a single logical, semantic whole. That's "only" four and a half times larger than decimal.py. I can't imagine what sort of code would need to be that large without being divided into modules, but it could be possible. -- Steven From andrew at acooke.org Thu Apr 2 10:32:13 2009 From: andrew at acooke.org (andrew cooke) Date: Thu, 2 Apr 2009 10:32:13 -0400 (CLT) Subject: simple iterator question In-Reply-To: References: Message-ID: Sion Arrowsmith wrote: > Neal Becker wrote: >>How do I interleave 2 sequences into a single sequence? >> >>How do I interleave N sequences into a single sequence? > > itertools.chain(*itertools.izip(*Nsequences)) aha! thanks. andrew From invalid at invalid Thu Apr 2 10:33:03 2009 From: invalid at invalid (Grant Edwards) Date: Thu, 02 Apr 2009 09:33:03 -0500 Subject: Creating huge data in very less time. References: <14b0a80b-4e77-4077-9129-6a526d26d912@v19g2000yqn.googlegroups.com> <8b745e98-a470-4e07-b0fb-e97b8d5b4b7c@j8g2000yql.googlegroups.com> Message-ID: On 2009-04-02, Tim Roberts wrote: > Grant Edwards wrote: > >>On 2009-03-31, Dave Angel wrote: >> >>> They were added in NTFS, in the Windows 2000 timeframe, to my >>> recollection. >> >>NTFS was added in NT 3.1 (which predates Win2K by 7-8 years). > > Although that's true, you didn't read his sentence. I read it, but interpreted it differently. To me that phrase "X was added in NTFS" means X was added when NTFS was added. > Sparse file support was not added to NTFS until Windows 2000, > exactly as he said. Now that you've explained what was meant, I would have written it "They were added to NTFS in the Windows 2000 timeframe." -- Grant Edwards grante Yow! Well, O.K. at I'll compromise with my visi.com principles because of EXISTENTIAL DESPAIR! From arnodel at googlemail.com Thu Apr 2 10:38:10 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Thu, 02 Apr 2009 15:38:10 +0100 Subject: simple iterator question References: Message-ID: Neal Becker writes: > How do I interleave 2 sequences into a single sequence? Here's a way: >>> a = [1,2,3,4] >>> b = [5,6,7,8] >>> [x for S in zip(a, b) for x in S] [1, 5, 2, 6, 3, 7, 4, 8] > How do I interleave N sequences into a single sequence? In the same way as above. -- Arnaud From invalid at invalid Thu Apr 2 10:42:44 2009 From: invalid at invalid (Grant Edwards) Date: Thu, 02 Apr 2009 09:42:44 -0500 Subject: Low level hard drive reading References: Message-ID: On 2009-04-02, Gabriel wrote: > For example can i open disk image file and read it block by > block? Sure. Just open the file, seek to where you want and read it. Or, it'll probably be simpler to just mmap the file and then grab blocks using slices as indexes. -- Grant Edwards grante Yow! World War III? at No thanks! visi.com From mail at microcorp.co.za Thu Apr 2 10:49:58 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Thu, 2 Apr 2009 16:49:58 +0200 Subject: pygame and socket.recv References: <1c6574dd-1bfb-4c45-a744-58761e5d1f06@k2g2000yql.googlegroups.com> Message-ID: <009d01c9b3a2$4bb385a0$0d00a8c0@Hendrik> "Aaron Brady" wrote: On Apr 2, 1:19 am, "Hendrik van Rooyen" wrote: >> "Aaron Brady" wrote: >> 8< ---- stuff showing small packets and adequate bandwidth ---------- >> What does "some latency" mean? - barely visible jitter, or a half >> second freeze? > >I've got a rather amateur loop keeping the frame rate capped at 40 >fps. The game won't exceed that, but when it drops /below/ it, it can >fall to 30 or 20 or 15 fps. That was just a per-second averaging >calculation; the bad freezes are about half a second. I'll try to get >a reading of individual calls to 'recv' that are themselves below the >threshold. > >Here's the loop: wait half the time left, then half that, then half >that, and so on, until less than 1/1000th second is left. > > while 1: > tcurr= time.clock( ) > delay= ( ( tprev+ secsperframe )- tcurr )* 0.5 > if delay< 0.001: > break > time.sleep( delay ) > tprev= tcurr > This will hand control to the OS more than once. It may be worth it to try making a target "completion date" and to then sleep the difference between now and then, once. >I don't know if it could be responsible. Would a mutex or Queue be >better using a threading.Timer? Good question - I don't know. I am just assuming that the OS is somehow giving you short shrift in its task scheduling. Does anybody know what the granularity of task swapping is in XP? - Forty per second is only 25 milliseconds - Is a task more or less guaranteed to get a bite at the cherry in that time? We need a windoze expert to tell us what the minimum time is for swapping a task out and in. If you are less than that time away from your scheduled "completion date", it makes no sense to sleep... > >> What do you do when the total round trip time takes longer >> than your target of one fortieth of a second? > >I want to know that too! Well if you were to know that it has happened, it would make sense to do the stuff immediately, and to reschedule your future target "completion dates" 8< -------- more stuff showing adequate bandwidth ---------------- - Hendrik From google at mrabarnett.plus.com Thu Apr 2 10:59:08 2009 From: google at mrabarnett.plus.com (MRAB) Date: Thu, 02 Apr 2009 15:59:08 +0100 Subject: Python3: to add, remove and change In-Reply-To: <421c55fc-5acf-454b-830f-a7b5b579f36c@c9g2000yqm.googlegroups.com> References: <421c55fc-5acf-454b-830f-a7b5b579f36c@c9g2000yqm.googlegroups.com> Message-ID: <49D4D2BC.2050200@mrabarnett.plus.com> bearophileHUGS at lycos.com wrote: > Here an informal list in random order of things that I may like to add > or to remove to/from Python3.x+. > [snip] > To add: Python3 is much more lazy-flavour than Python 2.x (see lazy > range, lazy dict views, etc). So islice becomes even more useful and > more commonly used. So I may like to have a lazy slicing as built-in. > But if you make it built-in, then why not add a syntax too? So I'd > like the usual slicing syntax [::] to be added to all lazy generators > too (to generator expressions, as an automatically created method of > iterators, and of course usable by iterable classes too). > Examples: >>>> foo = (i for i in range(2, 1000) if all(i % d for d in range(2, i))) >>>> list(foo[3: 14]) > [7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43] >>>> def bar(): > ... for i in range(2, 1000): > ... if all(i % d for d in range(2, i)): > ... yield i > ... >>>> list(bar()[3: 14]) > [7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43] > How about adding [x : y : z] as a shortcut for range(x, y, z)? >>> foo = (i for i in [2 : 1000] if all(i % d for d in [2 : i])) > > > To change: + is a commutative operation, while the concatenation of > two sequences is not. So after using the D language for some time I > have started to not appreciate anymore to use + to concatenate strings > and lists. D language uses ~ for such operation (this isn't a perfect > symbol, because I don't have it on my keyword, so on Windows I have to > type ALT+126 to insert it, or I have to redefine/remap a keyboard > key). > D also has a specific operator that can be overloaded: opCat, opCat_r > and opCatAssign, that map to ~ and ~= (the _r is the inverted order, > like the __radd__): > http://www.digitalmars.com/d/2.0/operatoroverloading.html#Binary > So if you define a vector-like collection you can define both opAdd > and opCat, the first may the sum between two vectors and the opCat is > their joining. > -1. > > To add: in Python3 you use lazy constructs much more often, so > itertools.chain may become more used. So it may be good to have chain > as built-in. But then it can be good to have a better syntax support > too. So I'd like all lazy iterables to support chaining. If you use > the ~ operator you can write: > range(100) ~ (i for i in range(2, 1000) if all(i % d for d in range(2, > i))) > (I have implemented most of itertools in D language, and I have added > this ~ operator to all lazy constructs, plus I have added an easy way > to add it to user-defined lazy iterables). > For lazy concatenation/chaining, perhaps +1. > > To add again: tuple unpacking in function arguments: it's handy, hi- > level, de-clutters the code and shortens it too. > +1. > > To change: I'd like {:} as empty dict literal, and {} as empty set > literal. > +1. > > To do nothing: probably I will never fully like the syntax of tuple > literals. It's not clean. But in practice I can live with it, and I > have so far failed to invent a better syntax. The main problem is that > ASCII (and keyboards too, maybe) was created for business purposes and > it has too few delimiters. While the sytax for lists: [] [1] [1, 2, 3] > is perfect. > [snip] Could you wrap tuples in <...>? I'd need to check the syntax to see whether it's unambiguous. From steve at REMOVE-THIS-cybersource.com.au Thu Apr 2 10:59:29 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 02 Apr 2009 14:59:29 GMT Subject: Tab completion Message-ID: <01e4c71e$0$20654$c3e8da3@news.astraweb.com> Does anyone use the tab-completion recipe in the docs? http://docs.python.org/library/rlcompleter.html#module-rlcompleter suggests using this to enable tab-completion: try: import readline except ImportError: print "Module readline not available." else: import rlcompleter readline.parse_and_bind("tab: complete") which is all very nice, but it makes it rather difficult to indent code blocks: >>> def func(x): ... Display all 174 possibilities? (y or n) I like tab-completion, but I'd rather not be reduced to typing spaces for indents in the interpreter. What do other people do? Can I bind Ctrl-tab to the completion instead of regular tab? I tried readline.parse_and_bind("C-tab: complete") but it still completes on regular tab. The GNU readline library claims that M-tab (which I guess is Alt-tab) will enter a tab character. Problem is that Alt-tab is intercepted by my window manager for something else. Any other suggestions or hints? -- Steven From newptcai at gmail.com Thu Apr 2 11:02:23 2009 From: newptcai at gmail.com (=?GB2312?B?0rvK18qr?=) Date: Thu, 2 Apr 2009 08:02:23 -0700 (PDT) Subject: A design problem I met again and again. References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> <33e08fde-1d4e-41a2-9ce2-7e1bec3abd90@r29g2000vbp.googlegroups.com> Message-ID: <793a9568-7c89-412e-b14c-e2dd28640577@s22g2000prg.googlegroups.com> You get it. Sometimes I feel that my head is trained to work in a procedural way. I use a big class just as a container of functions. About the "data-based" approach, what if these functions all shares a little data, e.g. a socket, but nothing else? On Apr 2, 5:58 am, Carl Banks wrote: > On Apr 1, 12:44 am, ??? wrote: > > > I got the same problem when writing C#/C++ when I have to provide a > > lot of method to my code's user. So I create a big class as the entry > > point of my code. Although these big classes doesn't contains much > > logic, they do grow bigger and bigger. > > This seems to be a classic result of "code-based organization", that > is, you are organizing your code according to how your functions are > used. That's appropriate sometimes. Procedural libraries are often > organized by grouping functions according to use. The os module is a > good example. > > However, it's usually much better to organize code according to what > data it acts upon: "data-based organization". In other words, go > though your big class and figure out what data belongs together > conceptually, make a class for each conceptual set of data, then > assign methods to classes based on what data the methods act upon. > > Consider the os module again. It's a big collection of functions, but > there are a group of functions is os that all act on a particular > piece of data, namely a file descriptor. This suggests tha all the > functions that act upon file descriptors (os.open, os.close, os.seek, > etc.) could instead be methods of a single class, with the file > descriptor as a class member. > > (Note: the os library doesn't do that because functions like os.open > are supposed to represent low-level operations corresponding to the > underlying system calls, but never mind that. Ordinarily a bunch of > functions operating on common data should be organized as a class.) > > Carl Banks From miguel.register at gmail.com Thu Apr 2 11:12:27 2009 From: miguel.register at gmail.com (Miguel Prada) Date: Thu, 2 Apr 2009 08:12:27 -0700 (PDT) Subject: pygame and socket.recv References: <1c6574dd-1bfb-4c45-a744-58761e5d1f06@k2g2000yql.googlegroups.com> Message-ID: <36b50e9b-55c1-42a9-90ae-84d31f5665da@h28g2000yqd.googlegroups.com> I don't know if this might be causing your problem, but most socket implementations use quite a big buffer for incoming data by default. I had a lot of trouble with another real-time networked application until I realised this. Reducing this buffer to the minimum helped a lot in my case. Also, I would strongly recommend using UDP instead of TCP. BTW, does anyone know if any good bibliography about real-time networked games, or real-time networked general applications? From clp2 at rebertia.com Thu Apr 2 11:31:39 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 2 Apr 2009 08:31:39 -0700 Subject: Tab completion In-Reply-To: <01e4c71e$0$20654$c3e8da3@news.astraweb.com> References: <01e4c71e$0$20654$c3e8da3@news.astraweb.com> Message-ID: <50697b2c0904020831r6c75a37ak8f8b1c8a68e90e88@mail.gmail.com> On Thu, Apr 2, 2009 at 7:59 AM, Steven D'Aprano wrote: > Does anyone use the tab-completion recipe in the docs? > > http://docs.python.org/library/rlcompleter.html#module-rlcompleter > > suggests using this to enable tab-completion: > > try: > ? ?import readline > except ImportError: > ? ?print "Module readline not available." > else: > ? ?import rlcompleter > ? ?readline.parse_and_bind("tab: complete") > > which is all very nice, but it makes it rather difficult to indent code > blocks: > >>>> def func(x): > ... > Display all 174 possibilities? (y or n) > > > I like tab-completion, but I'd rather not be reduced to typing spaces for > indents in the interpreter. What do other people do? > > Can I bind Ctrl-tab to the completion instead of regular tab? I tried > readline.parse_and_bind("C-tab: complete") but it still completes on > regular tab. > > The GNU readline library claims that M-tab (which I guess is Alt-tab) > will enter a tab character. Problem is that Alt-tab is intercepted by my > window manager for something else. > > Any other suggestions or hints? You could write a ~/.inputrc to change the bindings. Cheers, Chris -- I have a blog: http://blog.rebertia.com From martin at v.loewis.de Thu Apr 2 11:32:02 2009 From: martin at v.loewis.de (=?ISO-8859-15?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Thu, 02 Apr 2009 10:32:02 -0500 Subject: PEP 382: Namespace Packages Message-ID: <49D4DA72.60401@v.loewis.de> I propose the following PEP for inclusion to Python 3.1. Please comment. Regards, Martin Abstract ======== Namespace packages are a mechanism for splitting a single Python package across multiple directories on disk. In current Python versions, an algorithm to compute the packages __path__ must be formulated. With the enhancement proposed here, the import machinery itself will construct the list of directories that make up the package. Terminology =========== Within this PEP, the term package refers to Python packages as defined by Python's import statement. The term distribution refers to separately installable sets of Python modules as stored in the Python package index, and installed by distutils or setuptools. The term vendor package refers to groups of files installed by an operating system's packaging mechanism (e.g. Debian or Redhat packages install on Linux systems). The term portion refers to a set of files in a single directory (possibly stored in a zip file) that contribute to a namespace package. Namespace packages today ======================== Python currently provides the pkgutil.extend_path to denote a package as a namespace package. The recommended way of using it is to put:: from pkgutil import extend_path __path__ = extend_path(__path__, __name__) int the package's ``__init__.py``. Every distribution needs to provide the same contents in its ``__init__.py``, so that extend_path is invoked independent of which portion of the package gets imported first. As a consequence, the package's ``__init__.py`` cannot practically define any names as it depends on the order of the package fragments on sys.path which portion is imported first. As a special feature, extend_path reads files named ``*.pkg`` which allow to declare additional portions. setuptools provides a similar function pkg_resources.declare_namespace that is used in the form:: import pkg_resources pkg_resources.declare_namespace(__name__) In the portion's __init__.py, no assignment to __path__ is necessary, as declare_namespace modifies the package __path__ through sys.modules. As a special feature, declare_namespace also supports zip files, and registers the package name internally so that future additions to sys.path by setuptools can properly add additional portions to each package. setuptools allows declaring namespace packages in a distribution's setup.py, so that distribution developers don't need to put the magic __path__ modification into __init__.py themselves. Rationale ========= The current imperative approach to namespace packages has lead to multiple slightly-incompatible mechanisms for providing namespace packages. For example, pkgutil supports ``*.pkg`` files; setuptools doesn't. Likewise, setuptools supports inspecting zip files, and supports adding portions to its _namespace_packages variable, whereas pkgutil doesn't. In addition, the current approach causes problems for system vendors. Vendor packages typically must not provide overlapping files, and an attempt to install a vendor package that has a file already on disk will fail or cause unpredictable behavior. As vendors might chose to package distributions such that they will end up all in a single directory for the namespace package, all portions would contribute conflicting __init__.py files. Specification ============= Rather than using an imperative mechanism for importing packages, a declarative approach is proposed here, as an extension to the existing ``*.pkg`` mechanism. The import statement is extended so that it directly considers ``*.pkg`` files during import; a directory is considered a package if it either contains a file named __init__.py, or a file whose name ends with ".pkg". In addition, the format of the ``*.pkg`` file is extended: a line with the single character ``*`` indicates that the entire sys.path will be searched for portions of the namespace package at the time the namespace packages is imported. Importing a package will immediately compute the package's __path__; the ``*.pkg`` files are not considered anymore after the initial import. If a ``*.pkg`` package contains an asterisk, this asterisk is prepended to the package's __path__ to indicate that the package is a namespace package (and that thus further extensions to sys.path might also want to extend __path__). At most one such asterisk gets prepended to the path. extend_path will be extended to recognize namespace packages according to this PEP, and avoid adding directories twice to __path__. No other change to the importing mechanism is made; searching modules (including __init__.py) will continue to stop at the first module encountered. Discussion ========== With the addition of ``*.pkg`` files to the import mechanism, namespace packages can stop filling out the namespace package's __init__.py. As a consequence, extend_path and declare_namespace become obsolete. It is recommended that distributions put a file .pkg into their namespace packages, with a single asterisk. This allows vendor packages to install multiple portions of namespace package into a single directory, with no risk of overlapping files. Namespace packages can start providing non-trivial __init__.py implementations; to do so, it is recommended that a single distribution provides a portion with just the namespace package's __init__.py (and potentially other modules that belong to the namespace package proper). The mechanism is mostly compatible with the existing namespace mechanisms. extend_path will be adjusted to this specification; any other mechanism might cause portions to get added twice to __path__. Copyright ========= This document has been placed in the public domain. From grahn+nntp at snipabacken.se Thu Apr 2 11:45:07 2009 From: grahn+nntp at snipabacken.se (Jorgen Grahn) Date: 2 Apr 2009 15:45:07 GMT Subject: how to handle/generate pcap file References: <2c6e2640-0229-4886-bf22-d62636d9e13a@a5g2000pre.googlegroups.com> <6d880039-fcc8-4b14-9972-6c60230ca865@u9g2000pre.googlegroups.com> Message-ID: On Wed, 1 Apr 2009 18:59:12 -0700 (PDT), Evan wrote: > On Apr 2, 6:59?am, "Rhodri James" wrote: >> On Wed, 01 Apr 2009 14:53:34 +0100, Evan wrote: >> >> > Hello - >> >> > I'm trying to decode thepcapfilewhich is packet capture by tcpdump >> > or wireshark. ? Is there a python module that I can use it for this >> > problem? >> >> > Can python-libpcap or pycap or dpkt do that? >> >> A quick browse of the pypcap website suggests that yes, it can. >> >> -- >> Rhodri James *-* Wildebeeste Herder to the Masses > > > Yap, I found that dpkt can do this, Thanks all. I have used the 'pcapy' module successfully for this. Might be better than the ones mentioned above, might be worse. Also, the pcap file format isn't really hard: you can write such code by yourself in a few hours. I've done that too. /Jorgen -- // Jorgen Grahn R'lyeh wgah'nagl fhtagn! From dullrich at sprynet.com Thu Apr 2 12:02:30 2009 From: dullrich at sprynet.com (David C. Ullrich) Date: Thu, 02 Apr 2009 11:02:30 -0500 Subject: Introducing Python to others References: <039360fb-a29c-4f43-b6e0-ba97fb5981a3@z23g2000prd.googlegroups.com> Message-ID: In article , "andrew cooke" wrote: > David C. Ullrich wrote: > > In article , > > Scott David Daniels wrote: [...] > >> > >> class Vector(list): > >> def __add__(self, other): > >> return type(self)(x + y for x, y in zip(self, other)) > > > > Question: I would have thought it would be > > > > return type(self)([x + y for x, y in zip(self, other)]) > > > > What's this thing that looks like a list comprehension but isn't? > > it's a generator expression. > http://docs.python.org/3.0/reference/expressions.html#index-3735 Ah, thanks. I see "dict comprehensions" there too - keen. Sometime I gotta get around to actually learning this 2.x stuff. Thought I had an idea how 1.x worked... > andrew -- David C. Ullrich From kyosohma at gmail.com Thu Apr 2 12:09:43 2009 From: kyosohma at gmail.com (Mike Driscoll) Date: Thu, 2 Apr 2009 09:09:43 -0700 (PDT) Subject: Any library to bind python with Yahoo mail service References: Message-ID: On Apr 2, 7:45?am, guptha wrote: > Hi friends, > I came across libgmail library that can be used to bind python with > Google Gmail's service .Likewise is there any library available to > access the service of Yahoo mail ?from python code . > I like to send sms from my application ?via Yahoo mail Service . > Thanks Google's first hit was this: http://developer.yahoo.com/python/ It's not a library, but it's the way that Yahoo itself recommends. - Mike From grahn+nntp at snipabacken.se Thu Apr 2 12:11:56 2009 From: grahn+nntp at snipabacken.se (Jorgen Grahn) Date: 2 Apr 2009 16:11:56 GMT Subject: A design problem I met again and again. References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> <33e08fde-1d4e-41a2-9ce2-7e1bec3abd90@r29g2000vbp.googlegroups.com> <793a9568-7c89-412e-b14c-e2dd28640577@s22g2000prg.googlegroups.com> Message-ID: [top-posting fixed] On Thu, 2 Apr 2009 08:02:23 -0700 (PDT), =?GB2312?B?0rvK18qr?= wrote: > On Apr 2, 5:58 am, Carl Banks wrote: >> On Apr 1, 12:44 am, ?????? wrote: >> >> > I got the same problem when writing C#/C++ when I have to provide a >> > lot of method to my code's user. So I create a big class as the entry >> > point of my code. Although these big classes doesn't contains much >> > logic, they do grow bigger and bigger. >> >> This seems to be a classic result of "code-based organization", that >> is, you are organizing your code according to how your functions are >> used. That's appropriate sometimes. Procedural libraries are often >> organized by grouping functions according to use. The os module is a >> good example. >> >> However, it's usually much better to organize code according to what >> data it acts upon: "data-based organization". In other words, go >> though your big class and figure out what data belongs together >> conceptually, make a class for each conceptual set of data, then >> assign methods to classes based on what data the methods act upon. >> >> Consider the os module again. It's a big collection of functions, but >> there are a group of functions is os that all act on a particular >> piece of data, namely a file descriptor. This suggests tha all the >> functions that act upon file descriptors (os.open, os.close, os.seek, >> etc.) could instead be methods of a single class, with the file >> descriptor as a class member. ... > You get it. Sometimes I feel that my head is trained to work in a > procedural way. I use a big class just as a container of functions. If that's true, then your problems are not surprising. A real class normally doesn't get that big. > About the "data-based" approach, what if these functions all shares a > little data, e.g. a socket, but nothing else? If that is true, then those functions *are* the Python socket class and everything has already been done for you. Turn your question around and it makes more sense (to me, at least). You don't primarily work with functions: you work with data, a.k.a. state, a.k.a. objects. The functions follow from the data. To me, if I can find something with a certain lifetime, a certain set of invariants, and a suitable name and catchphrase describing it, then that's probably a class. Then I keep my fingers crossed and hope it works out reasonably well. If it doesn't, I try another approach. /Jorgen -- // Jorgen Grahn R'lyeh wgah'nagl fhtagn! From pecora at anvil.nrl.navy.mil Thu Apr 2 12:44:38 2009 From: pecora at anvil.nrl.navy.mil (Lou Pecora) Date: Thu, 02 Apr 2009 12:44:38 -0400 Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> Message-ID: In article , Steven D'Aprano wrote: > So an ordinality of zero just means the number > of elements of something that doesn't exist. You do realize that will give most people headaches. :-) -- -- Lou Pecora From shandy.b at gmail.com Thu Apr 2 12:53:57 2009 From: shandy.b at gmail.com (sjbrown) Date: Thu, 2 Apr 2009 09:53:57 -0700 (PDT) Subject: Tab completion References: <01e4c71e$0$20654$c3e8da3@news.astraweb.com> Message-ID: readline.parse_and_bind('\C-n: complete') Makes it Ctrl-n do the completion, like vim. On Apr 2, 7:59?am, Steven D'Aprano wrote: > Does anyone use the tab-completion recipe in the docs? > > http://docs.python.org/library/rlcompleter.html#module-rlcompleter > > suggests using this to enable tab-completion: > > try: > ? ? import readline > except ImportError: > ? ? print "Module readline not available." > else: > ? ? import rlcompleter > ? ? readline.parse_and_bind("tab: complete") > > which is all very nice, but it makes it rather difficult to indent code > blocks: > > >>> def func(x): > > ... > Display all 174 possibilities? (y or n) > > I like tab-completion, but I'd rather not be reduced to typing spaces for > indents in the interpreter. What do other people do? > > Can I bind Ctrl-tab to the completion instead of regular tab? I tried > readline.parse_and_bind("C-tab: complete") but it still completes on > regular tab. > > The GNU readline library claims that M-tab (which I guess is Alt-tab) > will enter a tab character. Problem is that Alt-tab is intercepted by my > window manager for something else. > > Any other suggestions or hints? > > -- > Steven From gert.cuykens at gmail.com Thu Apr 2 13:01:02 2009 From: gert.cuykens at gmail.com (gert) Date: Thu, 2 Apr 2009 10:01:02 -0700 (PDT) Subject: with open('com1', 'r') as f: Message-ID: <8bc55c05-19da-41c4-b916-48e0a4be49b9@p11g2000yqe.googlegroups.com> from subprocess import * check_call(['mode', 'COM1:9600,N,8,1,P'],shell=True) while True: with open('com1', 'r') as f: for line in f: print('line') This works very well except for one thing. After a reboot I have to launch 1 time any windows serial exe application no mater with one, that just opens an closes the com port, before i can launch this script. The script keeps on working even after closing and reopening it, until i reboot the pc. Then again I have to launch one time a serial.exe and close it again. The exe does not run anything in the background it just does something in windows python does not do when it reads from the com port after a fresh reboot. And i really appreciate it if somebody knew what it was. From fred.sells at adventistcare.org Thu Apr 2 13:07:11 2009 From: fred.sells at adventistcare.org (Sells, Fred) Date: Thu, 2 Apr 2009 13:07:11 -0400 Subject: Introducing Python to others In-Reply-To: References: Message-ID: When doing the same thing, I like.... Using a dictionary to return a function or a class definition based on a msg id and let that returned value "handle" the message that contained the id. Something like Class XYZ: ... MyHandlers = {42:XYZ, ... Message = read_from_somewhere_else() HandlerDefn = MyHandlers[Message.id] Handler = HandlerDefn(Message) Handler.processMessage() As opposed to a switch statement in other languages. I get my fellow codes to install Eclipse+PyDev as I find it easier to comprehend that IDLE. It's nice to just hit F9; although ^c^c is cool for emacs folks. I prefer turbogears over Django because it is simpler (and thus less robust) but it sure is cool to edit your server side code, save it and watch the server reload it automatically. Of couse I hate HTML and use Flex for the client side and turbogears JSON or my own XML converter make Flex a joy. Good luck Fred. > -----Original Message----- > From: python-list-bounces+frsells=adventistcare.org at python.org > [mailto:python-list-bounces+frsells=adventistcare.org at python.org] On > Behalf Of Paddy O'Loughlin > Sent: Thursday, March 26, 2009 5:36 AM > To: python-list at python.org > Subject: Introducing Python to others > > Hi, > As our resident python advocate, I've been asked by my team leader to > give a bit of a presentation as an introduction to python to the rest > of our department. > It'll be less than an hour, with time for taking questions at the end. > > There's not going to be a whole lot of structure to it. First, I'm > going to open up a python terminal and show them how the interpreter > works and a few basic syntax things and then a file .py files (got to > show them that python's indenting structure is not something to be > afraid of :P). I think I'll mostly show things in the order that they > appear in the python tutorial (http://docs.python.org/tutorial/). > > My question to you, dear python-list, is what suggestions do you have > for aspects of python that I should show them to make them maybe think > that python is better than what they are using at the moment. > All of the audience will be experienced (4+ years) programmers, almost > all of them are PHP developers (2 others, plus myself, work in C, know > C#, perl, java, etc.). > Because of this, I was thinking of making sure I included exceptions > and handling, the richness of the python library and a pointing out > how many modules there were out there to do almost anything one could > think of. > Anything else you think could make PHP developers starting think that > python is a better choice? > If I were to do a (very) short demonstration one web framework for the > PHP devs, what should I use? CherryPy (seems to be the easiest), > Django (seems to be the "biggest"/most used), or something else? > > Any other suggestions for a possible "wow" reaction from an audience like > that? > > Thanks, > Paddy > > -- > "Ray, when someone asks you if you're a god, you say YES!" > -- > http://mail.python.org/mailman/listinfo/python-list ---[Notification]-------------------------------------------------------------------- The information contained in this message may be privileged and / or confidential and protected from disclosure. If the reader of this message is not 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 the sender immediately by replying to this message and deleting the material from any computer. -------------------------------------------------------------------------------------- From pje at telecommunity.com Thu Apr 2 13:14:42 2009 From: pje at telecommunity.com (P.J. Eby) Date: Thu, 02 Apr 2009 13:14:42 -0400 Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: <49D4DA72.60401@v.loewis.de> References: <49D4DA72.60401@v.loewis.de> Message-ID: <20090402171218.9DDEF3A40A7@sparrow.telecommunity.com> At 10:32 AM 4/2/2009 -0500, Martin v. L?wis wrote: >I propose the following PEP for inclusion to Python 3.1. >Please comment. An excellent idea. One thing I am not 100% clear on, is how to get additions to sys.path to work correctly with this. Currently, when pkg_resources adds a new egg to sys.path, it uses its existing registry of namespace packages in order to locate which packages need __path__ fixups. It seems under this proposal that it would have to scan sys.modules for objects with __path__ attributes that are lists that begin with a '*', instead... which is a bit troubling because sys.modules doesn't always only contain module objects. Many major frameworks place lazy module objects, and module proxies or wrappers of various sorts in there, so scanning through it arbitrarily is not really a good idea. Perhaps we could add something like a sys.namespace_packages that would be updated by this mechanism? Then, pkg_resources could check both that and its internal registry to be both backward and forward compatible. Apart from that, this mechanism sounds great! I only wish there was a way to backport it all the way to 2.3 so I could drop the messy bits from setuptools. From icanbob at gmail.com Thu Apr 2 13:26:54 2009 From: icanbob at gmail.com (bobicanprogram) Date: Thu, 2 Apr 2009 10:26:54 -0700 (PDT) Subject: openNetcam open source project recruiting developers Message-ID: <2707ce5c-c127-479c-a7b7-fe204ada64d4@r33g2000yqn.googlegroups.com> A new open source project has been started with the aim of building an open Linux framework for TCP/IP enabled video camera systems. http://www.openNetcam.net The framework will be layered on the ultra lightweight SIMPL toolkit (http://www.icanprogram.com/simpl) and when the framework is complete it will be a kind webservices lite taylored to camera systems. The target webserver is the lighttpd. There will be opportunities for openNetcam developers to create a mod-simpl plugin for that technology. Most of the systems will be deployed in an embedded Linux appliance. This will restrict the choice of programming languages for the target code somewhat. SIMPL supports C, C++, Python, Tcl/ Tk and JAVA. Of those C and C++ are probably the most embedded friendly. However, there will be lots of other aspects of the this project where Python could play a significant role. eg. test stubs. If this sort of project interests you (or if you know of anyone who might be interested) please don't hesitate to contact me offlist. bob From skabra at gmail.com Thu Apr 2 13:30:21 2009 From: skabra at gmail.com (Saurabh Kabra) Date: Thu, 2 Apr 2009 11:30:21 -0600 Subject: web application for scientific puposes Message-ID: <576804590904021030p3605bd73td38561f4f809879b@mail.gmail.com> Dear Group, I am new to python and I mostly use basic python scripts for small mathematical/scientific/intrumentation applications. Now, I want to make these application available to others by converting them to web based applications (so that it can be used by several people on our internal network without them having to install the application and python on every single computer). I am looking for the best available web framework for python which would be well suited for a scientific application. My requirements are lots of interactability like buttons, menus, drop downs etc. and good plotting - preferably interactive. The faster the better but speed is not a huge issue here - neither is web traffic. Can you guys recommend packages or combination of packages for such an application. Thanks in advance Saurabh -------------- next part -------------- An HTML attachment was scrubbed... URL: From cjns1989 at gmail.com Thu Apr 2 13:33:53 2009 From: cjns1989 at gmail.com (Chris Jones) Date: Thu, 02 Apr 2009 13:33:53 -0400 Subject: Tab completion In-Reply-To: <01e4c71e$0$20654$c3e8da3@news.astraweb.com> References: <01e4c71e$0$20654$c3e8da3@news.astraweb.com> Message-ID: <20090402173353.GA2898@turki.gavron.org> On Thu, Apr 02, 2009 at 10:59:29AM EDT, Steven D'Aprano wrote: > Does anyone use the tab-completion recipe in the docs? > > http://docs.python.org/library/rlcompleter.html#module-rlcompleter > > suggests using this to enable tab-completion: > > try: > import readline > except ImportError: > print "Module readline not available." > else: > import rlcompleter > readline.parse_and_bind("tab: complete") > > which is all very nice, but it makes it rather difficult to indent code > blocks: > > >>> def func(x): > ... > Display all 174 possibilities? (y or n) > > > I like tab-completion, but I'd rather not be reduced to typing spaces for > indents in the interpreter. What do other people do? > > Can I bind Ctrl-tab to the completion instead of regular tab? I tried > readline.parse_and_bind("C-tab: complete") but it still completes on > regular tab. > > The GNU readline library claims that M-tab (which I guess is Alt-tab) > will enter a tab character. Problem is that Alt-tab is intercepted by my > window manager for something else. > > Any other suggestions or hints? CTRL+V From tim.wintle at teamrubber.com Thu Apr 2 13:35:01 2009 From: tim.wintle at teamrubber.com (Tim Wintle) Date: Thu, 02 Apr 2009 18:35:01 +0100 Subject: python for loop In-Reply-To: References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> Message-ID: <1238693701.8079.17.camel@tim-laptop> On Thu, 2009-04-02 at 06:28 +0000, Steven D'Aprano wrote: > In set theory, you start by defining the integers like this: > > 0 = len( {} ) > 1 = len( {{}} ) > 2 = len( {{}, {{}}} ) > 3 = len( {{}, {{}}, {{}, {{}}} ) > etc. not quite len() - surely you mean something like "any object along with an algebra in which the left hand side is equivalent to the right in the algebra of set theory?" - at least for ordinals. The cardinal is then (for finite numbers) the length of that. Or, in a pythonic sense (taking 0,1,2,... to be variable names): 0 = set() 1 = set(0) 2 = set(1,0) 3 = set(2,1,0) 3 = set(3,2,1,0) etc. > How that relates to whether indexing should start at one or zero, I > have > no idea. so in this sense, range(n) is actually very close to the ordinal value of 0 (except for being a list and not a set - but it's not in 3.0) i.e. range(n) returns something very similar to the ordinal "n", and with cardinality n. That seems very sensible to me. > Oh, and speaking of... I'm shocked, SHOCKED I say, that nobody has > given that quote about the compromise of 0.5. "God made the integers, all else is the work of man" - Leopold Kronecker ...holding myself back from complaining about integer division in Py3K when the philosophical question of whether irrational numbers even exist (in a physics sense) is fairly open. Tim W From pavlovevidence at gmail.com Thu Apr 2 13:51:30 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 2 Apr 2009 10:51:30 -0700 (PDT) Subject: A design problem I met again and again. References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> <33e08fde-1d4e-41a2-9ce2-7e1bec3abd90@r29g2000vbp.googlegroups.com> <793a9568-7c89-412e-b14c-e2dd28640577@s22g2000prg.googlegroups.com> Message-ID: <1b60f999-aaf8-4b10-bd1f-ab46f03f44f6@m36g2000vbp.googlegroups.com> On Apr 2, 8:02 am, ??? wrote: > You get it. Sometimes I feel that my head is trained to work in a > procedural way. I use a big class just as a container of functions. > > About the "data-based" approach, what if these functions all shares a > little data, e.g. a socket, but nothing else? Then perhaps your problem is that you are too loose with the interface. Do you write new functions that are very similar to existing functions all the time? Perhaps you should consolidate, or think about how existing functions could do the job. Or perhaps you don't have a problem. There's nothing wrong with large classes per se, it's just a red flag. If you have all these functions that really all operate on only one piece of data, and really all do different things, then a large class is fine. Carl Banks From brentb at beanfield.com Thu Apr 2 14:18:01 2009 From: brentb at beanfield.com (Brent Bloxam) Date: Thu, 02 Apr 2009 14:18:01 -0400 Subject: Sending SMS using python script In-Reply-To: <2005e94f-94de-4566-be60-7245efa4271d@y6g2000prf.googlegroups.com> References: <2005e94f-94de-4566-be60-7245efa4271d@y6g2000prf.googlegroups.com> Message-ID: <49D50159.7050808@beanfield.com> guptha wrote: > hi group, > my application needs to send SMS occasionally to all the clients .Is > there any library in python that supports in sending SMS. > I like to conform few information i gathered in this regard. > > I can send SMS by two ways > > 1. Sending SMS using Email clients > 2. Using sms gateway to send message(we can implement SMS Gateway API > 's ,provided by vendor and ,send SMS -- we will be charged > accordingly ) > > In case of First approach > 1. We can make use of libgamil library to send SMS using gmail ( I > ref : http://blog.datasingularity.com/?p=63 ) > i suppose sending sms through gmail is not supported in India > 2. Can we use Skype4py library, > > In case of second approach > 1. Is there any way to send SMS for free inside India ,or ,Any free > SMS gateway providers in India > Any information regarding this is appreciable > Thanks > -- > http://mail.python.org/mailman/listinfo/python-list > While this may not be exactly what you want to do, I thought I'd offer this as an alternative. MultiModem has a hardware product that works as a GPRS modem (with either a serial or ethernet interface, see here: http://www.multitech.com/PRODUCTS/Families/MultiModemGPRS/), allowing direct access to standard AT commands. The modems require a regular SIM card with an account setup for it. For our needs we got a basic plan with unlimited SMS. This is after we went with a couple 3rd party SMS gateways and found their delivery unreliable for our clients. I've written a daemon in python called mmsmsd (multimodem sms daemon) for their ethernet model (MTCBA-G-EN-F4). It queues messages via an HTTP GET request, which are then handled by threads that maintain telnet connections to the GPRS modems AT command interface. You can check out the project here: http://code.google.com/p/mmsmsd/ (it's BSD licensed) If you go this route, feel free to submit any bug reports or requests. If anyone out there feels like doing a quick audit of the code, that would be appreciated as well :) This is my first project with Python. -- | .-> brent bloxam ~-. brentb @ beanfield.com | ( ) beanfield metroconnect | `~- wgxolq +uajq <-' 416.532.1555 ext. 2004 -- From kay.schluehr at gmx.net Thu Apr 2 14:24:49 2009 From: kay.schluehr at gmx.net (Kay Schluehr) Date: Thu, 2 Apr 2009 11:24:49 -0700 (PDT) Subject: Python Goes Mercurial References: <35d429fa-5d13-4703-a443-6a95c740c9fb@o6g2000yql.googlegroups.com> <4dad5e13-153f-446d-9f2b-06ebc653e003@c9g2000yqm.googlegroups.com> Message-ID: <17ab8b08-73cf-42ea-ba83-a8d6d8b9281b@c9g2000yqm.googlegroups.com> On 2 Apr., 15:05, David Smith wrote: > Kay Schluehr wrote: > > On 1 Apr., 07:56, Lawrence D'Oliveiro > central.gen.new_zealand> wrote: > >> In message <35d429fa-5d13-4703- > > >> a443-6a95c740c... at o6g2000yql.googlegroups.com>, John Yeung wrote: > >>> Here's one that clearly expresses strong antipathy: > >>> http://mail.python.org/pipermail/python-dev/2009-March/087971.html > >> There are lots of GUI- and Web-based front ends to Git. And look at on-line > >> services like GitHub and Gitorious. The level of support for it is huge. > > > Ironically Mercurials most popular UI frontend Tortoise is going to > > crash Python tools ( like Wing-IDE ) on Windows. That's a known issue > > for about a year and more and the developers are not inclined to fix > > it. This doesn't really increase my trust that Mercurials UI tools are > > of a higher quality than Git's no matter which platform is used. > > The conflict between TortoiseHg and Wing IDE can be fixed by simply > uninstalling the Tortoise Overlays. You loose the graphic overlay on > folders, but otherwise everything works. > > --David Good to know. Uninstalling a major feature that enhances usability just to make it usable isn't much less ironic though. From andrew.henshaw at gtri.gatech.edu Thu Apr 2 14:29:50 2009 From: andrew.henshaw at gtri.gatech.edu (Andrew Henshaw) Date: Thu, 2 Apr 2009 14:29:50 -0400 Subject: league problem in python References: Message-ID: "Ross" wrote in message news:d5cc0ec7-5223-4f6d-bab4-3801dee501cd at r37g2000yqn.googlegroups.com... ... snip ... > I would like to create a simple program where the pro could enter in > how many people were in the league, the number of courts available, > and the number of weeks the schedule would run and then generate a > schedule where everybody played everybody else once and got the same > number of bye weeks, preferably spaced out evenly. > > How should I go about starting this problem...I'm feel like this is a > really simple problem, but I'm having writer's/coder's block. Can you > guys help? At least as a start, you want the round-robin tournament algorithm (see http://en.wikipedia.org/wiki/Round-robin_tournament). Here's some code that I use: ###### begin code ####### def roundrobin(teams, rounds=1): # if odd number of teams, add a "bye" team if len(teams) % 2: teams.append(None) mid = len(teams) // 2 for i in range(rounds): yield zip(teams[:mid], teams[mid:]) teams = teams[0:1] + teams[mid:mid+1] + teams[1:mid-1] + teams[mid+1:] + teams[mid-1:mid] ### test code ### if __name__ == '__main__': ROUNDS = 10 # two test cases - even number of teams and odd number of teams for teams in (range(6), range(5)): print "\nteams =", teams for round in roundrobin(teams, ROUNDS): print round ###### end code ####### And the output: teams = [0, 1, 2, 3, 4, 5] [(0, 3), (1, 4), (2, 5)] [(0, 4), (3, 5), (1, 2)] [(0, 5), (4, 2), (3, 1)] [(0, 2), (5, 1), (4, 3)] [(0, 1), (2, 3), (5, 4)] [(0, 3), (1, 4), (2, 5)] [(0, 4), (3, 5), (1, 2)] [(0, 5), (4, 2), (3, 1)] [(0, 2), (5, 1), (4, 3)] [(0, 1), (2, 3), (5, 4)] teams = [0, 1, 2, 3, 4] [(0, 3), (1, 4), (2, None)] [(0, 4), (3, None), (1, 2)] [(0, None), (4, 2), (3, 1)] [(0, 2), (None, 1), (4, 3)] [(0, 1), (2, 3), (None, 4)] [(0, 3), (1, 4), (2, None)] [(0, 4), (3, None), (1, 2)] [(0, None), (4, 2), (3, 1)] [(0, 2), (None, 1), (4, 3)] [(0, 1), (2, 3), (None, 4)] -- Andy From pavlovevidence at gmail.com Thu Apr 2 14:38:50 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 2 Apr 2009 11:38:50 -0700 (PDT) Subject: PEP 382: Namespace Packages References: Message-ID: <3512eadf-ab56-4be9-9c84-5c5f60293ec2@r15g2000vbi.googlegroups.com> On Apr 2, 8:32?am, "Martin v. L?wis" wrote: > I propose the following PEP for inclusion to Python 3.1. > Please comment. > > Regards, > Martin > > Abstract > ======== > > Namespace packages are a mechanism for splitting a single Python > package across multiple directories on disk. In current Python > versions, an algorithm to compute the packages __path__ must be > formulated. With the enhancement proposed here, the import machinery > itself will construct the list of directories that make up the > package. -0 My main concern is that we'll start seeing all kinds of packages with names like: com.dusinc.sarray.ptookkit.v_1_34_beta.btree.BTree The current lack of global package namespace effectively prevents bureaucratic package naming, which in my mind makes it worth the cost. However, I'd be willing to believe this can be kept under control some other way. Carl Banks From aahz at pythoncraft.com Thu Apr 2 14:50:19 2009 From: aahz at pythoncraft.com (Aahz) Date: 2 Apr 2009 11:50:19 -0700 Subject: *args question References: <50d06eb9-2b87-43a0-a7e2-6b68e35fc496@y34g2000prb.googlegroups.com> Message-ID: In article <50d06eb9-2b87-43a0-a7e2-6b68e35fc496 at y34g2000prb.googlegroups.com>, grocery_stocker wrote: > >Given the following code... > >import thread Here's your problem; subclass threading.Thread instead, much easier. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." --Brian W. Kernighan From kushal.kumaran at gmail.com Thu Apr 2 14:53:47 2009 From: kushal.kumaran at gmail.com (Kushal Kumaran) Date: Fri, 3 Apr 2009 00:23:47 +0530 Subject: with open('com1', 'r') as f: References: <8bc55c05-19da-41c4-b916-48e0a4be49b9@p11g2000yqe.googlegroups.com> Message-ID: <20090403002347.163911b4@oxygen> On Thu, 2 Apr 2009 10:01:02 -0700 (PDT) gert wrote: > from subprocess import * > > check_call(['mode', 'COM1:9600,N,8,1,P'],shell=True) > while True: > with open('com1', 'r') as f: > for line in f: > print('line') > > This works very well except for one thing. After a reboot I have to > launch 1 time any windows serial exe application no mater with one, > that just opens an closes the com port, before i can launch this > script. The script keeps on working even after closing and reopening > it, until i reboot the pc. Then again I have to launch one time a > serial.exe and close it again. The exe does not run anything in the > background it just does something in windows python does not do when > it reads from the com port after a fresh reboot. > > And i really appreciate it if somebody knew what it was. I don't know why you're getting this behaviour, but have you tried using a python library for accessing the serial port? See http://pyserial.wiki.sourceforge.net/pySerial. -- kushal From kay.schluehr at gmx.net Thu Apr 2 14:56:09 2009 From: kay.schluehr at gmx.net (Kay Schluehr) Date: Thu, 2 Apr 2009 11:56:09 -0700 (PDT) Subject: PEP 382: Namespace Packages References: Message-ID: On 2 Apr., 17:32, "Martin v. L?wis" wrote: > I propose the following PEP for inclusion to Python 3.1. > Please comment. > > Regards, > Martin > > Abstract > ======== > > Namespace packages are a mechanism for splitting a single Python > package across multiple directories on disk. In current Python > versions, an algorithm to compute the packages __path__ must be > formulated. With the enhancement proposed here, the import machinery > itself will construct the list of directories that make up the > package. > > Terminology > =========== > > Within this PEP, the term package refers to Python packages as defined > by Python's import statement. The term distribution refers to > separately installable sets of Python modules as stored in the Python > package index, and installed by distutils or setuptools. The term > vendor package refers to groups of files installed by an operating > system's packaging mechanism (e.g. Debian or Redhat packages install > on Linux systems). > > The term portion refers to a set of files in a single directory (possibly > stored in a zip file) that contribute to a namespace package. > > Namespace packages today > ======================== > > Python currently provides the pkgutil.extend_path to denote a package as > a namespace package. The recommended way of using it is to put:: > > from pkgutil import extend_path > __path__ = extend_path(__path__, __name__) > > int the package's ``__init__.py``. Every distribution needs to provide > the same contents in its ``__init__.py``, so that extend_path is > invoked independent of which portion of the package gets imported > first. As a consequence, the package's ``__init__.py`` cannot > practically define any names as it depends on the order of the package > fragments on sys.path which portion is imported first. As a special > feature, extend_path reads files named ``*.pkg`` which allow to > declare additional portions. > > setuptools provides a similar function pkg_resources.declare_namespace > that is used in the form:: > > import pkg_resources > pkg_resources.declare_namespace(__name__) > > In the portion's __init__.py, no assignment to __path__ is necessary, > as declare_namespace modifies the package __path__ through sys.modules. > As a special feature, declare_namespace also supports zip files, and > registers the package name internally so that future additions to sys.path > by setuptools can properly add additional portions to each package. > > setuptools allows declaring namespace packages in a distribution's > setup.py, so that distribution developers don't need to put the > magic __path__ modification into __init__.py themselves. > > Rationale > ========= > > The current imperative approach to namespace packages has lead to > multiple slightly-incompatible mechanisms for providing namespace > packages. For example, pkgutil supports ``*.pkg`` files; setuptools > doesn't. Likewise, setuptools supports inspecting zip files, and > supports adding portions to its _namespace_packages variable, whereas > pkgutil doesn't. > > In addition, the current approach causes problems for system vendors. > Vendor packages typically must not provide overlapping files, and an > attempt to install a vendor package that has a file already on disk > will fail or cause unpredictable behavior. As vendors might chose to > package distributions such that they will end up all in a single > directory for the namespace package, all portions would contribute > conflicting __init__.py files. > > Specification > ============= > > Rather than using an imperative mechanism for importing packages, a > declarative approach is proposed here, as an extension to the existing > ``*.pkg`` mechanism. > > The import statement is extended so that it directly considers ``*.pkg`` > files during import; a directory is considered a package if it either > contains a file named __init__.py, or a file whose name ends with > ".pkg". > > In addition, the format of the ``*.pkg`` file is extended: a line with > the single character ``*`` indicates that the entire sys.path will > be searched for portions of the namespace package at the time the > namespace packages is imported. > > Importing a package will immediately compute the package's __path__; > the ``*.pkg`` files are not considered anymore after the initial import. > If a ``*.pkg`` package contains an asterisk, this asterisk is prepended > to the package's __path__ to indicate that the package is a namespace > package (and that thus further extensions to sys.path might also > want to extend __path__). At most one such asterisk gets prepended > to the path. > > extend_path will be extended to recognize namespace packages according > to this PEP, and avoid adding directories twice to __path__. > > No other change to the importing mechanism is made; searching > modules (including __init__.py) will continue to stop at the first > module encountered. > > Discussion > ========== > > With the addition of ``*.pkg`` files to the import mechanism, namespace > packages can stop filling out the namespace package's __init__.py. > As a consequence, extend_path and declare_namespace become obsolete. > > It is recommended that distributions put a file .pkg > into their namespace packages, with a single asterisk. This allows > vendor packages to install multiple portions of namespace package > into a single directory, with no risk of overlapping files. > > Namespace packages can start providing non-trivial __init__.py > implementations; to do so, it is recommended that a single distribution > provides a portion with just the namespace package's __init__.py > (and potentially other modules that belong to the namespace package > proper). > > The mechanism is mostly compatible with the existing namespace > mechanisms. extend_path will be adjusted to this specification; > any other mechanism might cause portions to get added twice to > __path__. > > Copyright > ========= > > This document has been placed in the public domain. Wow. You python-dev guys are really jumping the shark. Isn't your Rube Goldberg "import machinery" already complex enough for you? From clp2 at rebertia.com Thu Apr 2 15:08:36 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 2 Apr 2009 12:08:36 -0700 Subject: PEP 382: Namespace Packages In-Reply-To: <3512eadf-ab56-4be9-9c84-5c5f60293ec2@r15g2000vbi.googlegroups.com> References: <3512eadf-ab56-4be9-9c84-5c5f60293ec2@r15g2000vbi.googlegroups.com> Message-ID: <50697b2c0904021208g28cd2f73wc3d054ecd9f6b704@mail.gmail.com> On Thu, Apr 2, 2009 at 11:38 AM, Carl Banks wrote: > On Apr 2, 8:32?am, "Martin v. L?wis" wrote: >> I propose the following PEP for inclusion to Python 3.1. >> Please comment. >> >> Regards, >> Martin >> >> Abstract >> ======== >> >> Namespace packages are a mechanism for splitting a single Python >> package across multiple directories on disk. In current Python >> versions, an algorithm to compute the packages __path__ must be >> formulated. With the enhancement proposed here, the import machinery >> itself will construct the list of directories that make up the >> package. > > -0 > > My main concern is that we'll start seeing all kinds of packages with > names like: > > com.dusinc.sarray.ptookkit.v_1_34_beta.btree.BTree > > The current lack of global package namespace effectively prevents > bureaucratic package naming, which in my mind makes it worth the > cost. ?However, I'd be willing to believe this can be kept under > control some other way. Agreed, although I'd be slightly less optimistic on its usage being kept under control. It seems this goes a bit against the "Flat is better than nested" principle. Then again, we also have the "Namespaces are honkingly great" principle to contend with as well, so it's definitely a balancing act. Cheers, Chris -- I have a blog: http://blog.rebertia.com From python at rcn.com Thu Apr 2 15:11:44 2009 From: python at rcn.com (Raymond Hettinger) Date: Thu, 2 Apr 2009 12:11:44 -0700 (PDT) Subject: Python3: to add, remove and change References: <421c55fc-5acf-454b-830f-a7b5b579f36c@c9g2000yqm.googlegroups.com> Message-ID: <405e8439-382f-41e8-b177-2cb459297bd4@d19g2000prh.googlegroups.com> On Apr 2, 12:39?am, bearophileH... at lycos.com wrote: > To add again: tuple unpacking in function arguments: it's handy, hi- > level, de-clutters the code and shortens it too. +1 But you will have to talk to Brett about it. He's the one who led the effort to kill it. > To change: I'd like {:} as empty dict literal, and {} as empty set > literal. Unless you can time machine back to 1990, I would say that the {} notation for emtpy dicts is too firmly entrenched. I don't think it is possible to switch without causing a great deal of unnecessary pain. Besides, it doesn't hurt much to write: s=set() or d=dict() which are explicit, clear, and short. That's what I usually do in my code. > To do nothing: probably I will never fully like the syntax of tuple > literals. It's not clean. But in practice I can live with it, and I > have so far failed to invent a better syntax. The main problem is that > ASCII (and keyboards too, maybe) was created for business purposes and > it has too few delimiters. ? unicode has lots of delimiters ? Raymond From debacle at debian.org Thu Apr 2 15:14:51 2009 From: debacle at debian.org (W. Martin Borgert) Date: Thu, 2 Apr 2009 21:14:51 +0200 Subject: web application for scientific puposes In-Reply-To: <576804590904021030p3605bd73td38561f4f809879b@mail.gmail.com> References: <576804590904021030p3605bd73td38561f4f809879b@mail.gmail.com> Message-ID: <20090402191451.GA4018@beron.tangosoft.com> On 2009-04-02 11:30, Saurabh Kabra wrote: > Can you guys recommend packages or > combination of packages for such an application. Apache + mod_wsgi + Django + matplotlib. From oshecho at gmail.com Thu Apr 2 15:22:59 2009 From: oshecho at gmail.com (Echo) Date: Thu, 2 Apr 2009 15:22:59 -0400 Subject: Python Goes Mercurial In-Reply-To: <12cbbbfc0904020519p20d625baid0674f9ecb7cf64f@mail.gmail.com> References: <7x7i2499si.fsf@ruckus.brouhaha.com> <7a1dd0d8-1978-470b-a80d-57478d7f7f9e@q16g2000yqg.googlegroups.com> <3e682785-7976-4c1b-993e-eadba3e3503e@37g2000yqp.googlegroups.com> <12cbbbfc0904020519p20d625baid0674f9ecb7cf64f@mail.gmail.com> Message-ID: <910885da0904021222s6b3a20ddh2fec716e78b69922@mail.gmail.com> 2009/4/2 Jeremiah Dodds > The one thing that makes me want to use git more than any other dvcs is > that you don't have to create a new directory for branches. This may be > possible in other dvcs's , but git is the only one I've seen advertise the > capability. > > -- > http://mail.python.org/mailman/listinfo/python-list > > That is the main reason why I switched to git at my work. As for the git rebase, I don't ever plan on using that 'feature'. Even though I would rather have had GvR pick git, I think he did a good job deciding. -- -Brandon Singer -------------- next part -------------- An HTML attachment was scrubbed... URL: From notvalid2 at sbcglobal.net Thu Apr 2 15:26:39 2009 From: notvalid2 at sbcglobal.net (W. eWatson) Date: Thu, 02 Apr 2009 12:26:39 -0700 Subject: PIL Handbooks Message-ID: I'm very new to PIL, and don't see any handbooks for 1.1.6 or the forthcoming 1.1.7. In fact, this looks like the extent of them: * Python Imaging Library Handbook for 1.1.5 (online) * Python Imaging Library Handbook for 1.1.3 (PDF) Somewhere in my recent search I see that 1.1.6 has some features like digital cameras and scanners. Ah here, . Any other news? I'd settle even for a 1.1.5 pdf of the handbook right now. -- 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 aahz at pythoncraft.com Thu Apr 2 16:01:13 2009 From: aahz at pythoncraft.com (Aahz) Date: 2 Apr 2009 13:01:13 -0700 Subject: Fedora: Dual Python Versions Installed System Not Picking Up Newer Version References: <3103e4c6-831f-4408-947b-4bb0dddf012b@s22g2000prg.googlegroups.com> Message-ID: In article <3103e4c6-831f-4408-947b-4bb0dddf012b at s22g2000prg.googlegroups.com>, *nixtechno wrote: > >I have a fedora box and just installed python 2.6.1 along with 2.5.2, >so here's my issue, if I removed the "systems" garbage RPM it would >uninstall all the other crap along with it, so I went ahead and >trunked in and ./configure, build && build install and built python >2.6.1 along with this. However how can I stipulate that I want the >WHOLE system to use Python 2.6.1 rather than 2.5.2? I know there is >something to do with PATH, which I tried under my /root/.cshrc file, >but to no avail it didn't work. So is there something special I should >do with getting the system to pick up this directy: This is a Bad Idea; some of the system scripts rely on the built-in Python. You should either use a shell alias or explicitly specify the full path to the 2.6.1 binary inside your scripts. I think virtualenv might also be useful, but I haven't played with that myself yet. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." --Brian W. Kernighan From gervaz at gmail.com Thu Apr 2 16:01:42 2009 From: gervaz at gmail.com (mattia) Date: 02 Apr 2009 20:01:42 GMT Subject: Help with dict and iter References: <49cf8dc4$0$17431$4fafbaef@reader5.news.tin.it> Message-ID: <49d519a6$0$1109$4fafbaef@reader4.news.tin.it> Il Thu, 02 Apr 2009 13:44:38 +0000, Sion Arrowsmith ha scritto: > mattia wrote: >> So, I'm looking for a way to "reset" the next() value every >>time i complete the scan of a list. > > itertools.cycle ? Perfect, thanks. From andrew at acooke.org Thu Apr 2 16:02:06 2009 From: andrew at acooke.org (andrew cooke) Date: Thu, 2 Apr 2009 16:02:06 -0400 (CLT) Subject: Python Goes Mercurial In-Reply-To: <910885da0904021222s6b3a20ddh2fec716e78b69922@mail.gmail.com> References: <7x7i2499si.fsf@ruckus.brouhaha.com> <7a1dd0d8-1978-470b-a80d-57478d7f7f9e@q16g2000yqg.googlegroups.com> <3e682785-7976-4c1b-993e-eadba3e3503e@37g2000yqp.googlegroups.com> <12cbbbfc0904020519p20d625baid0674f9ecb7cf64f@mail.gmail.com> <910885da0904021222s6b3a20ddh2fec716e78b69922@mail.gmail.com> Message-ID: <9526e125eab49f61f79db30e721168ae.squirrel@localhost> Echo wrote: > 2009/4/2 Jeremiah Dodds > >> The one thing that makes me want to use git more than any other dvcs is >> that you don't have to create a new directory for branches. This may be >> possible in other dvcs's , but git is the only one I've seen advertise >> the >> capability. [...] > That is the main reason why I switched to git at my work. > As for the git rebase, I don't ever plan on using that 'feature'. Even > though I would rather have had GvR pick git, I think he did a good job > deciding. please can you explain this? i can think of a couple of things you could mean, but neither of them seem to make much sense to me. one is that hg only allows you one branch per repository. i checked the docs and that's not true - see 2/3 way down http://hgbook.red-bean.com/read/managing-releases-and-branchy-development.html - which describes how you can branch in the repository and then swap your working copy between them. another is that you want a directory structure like: myproject/ tag1/ tag2/ and for there to be a single repository in myproject, rather than two repositories, one in tag1 and one in tag2. since a dvcs works across repos as well as it works within a repo, i don't see how this is significantly different to having two repositories, one in tag1 and one in tag2. how does it matter? or do you mean something else? i don't use a dvcs myself - i'm happy with svn - but i am curious about what the advantages and disadvantages might be. thanks, andrew From chris at simplistix.co.uk Thu Apr 2 16:03:34 2009 From: chris at simplistix.co.uk (Chris Withers) Date: Thu, 02 Apr 2009 21:03:34 +0100 Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: <49D4DA72.60401@v.loewis.de> References: <49D4DA72.60401@v.loewis.de> Message-ID: <49D51A16.70804@simplistix.co.uk> Martin v. L?wis wrote: > I propose the following PEP for inclusion to Python 3.1. > Please comment. Would this support the following case: I have a package called mortar, which defines useful stuff: from mortar import content, ... I now want to distribute large optional chunks separately, but ideally so that the following will will work: from mortar.rbd import ... from mortar.zodb import ... from mortar.wsgi import ... Does the PEP support this? The only way I can currently think to do this would result in: from mortar import content,.. from mortar_rbd import ... from mortar_zodb import ... from mortar_wsgi import ... ...which looks a bit unsightly to me. cheers, Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk From chris at simplistix.co.uk Thu Apr 2 16:03:49 2009 From: chris at simplistix.co.uk (Chris Withers) Date: Thu, 02 Apr 2009 21:03:49 +0100 Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: <20090402171218.9DDEF3A40A7@sparrow.telecommunity.com> References: <49D4DA72.60401@v.loewis.de> <20090402171218.9DDEF3A40A7@sparrow.telecommunity.com> Message-ID: <49D51A25.10400@simplistix.co.uk> P.J. Eby wrote: > Apart from that, this mechanism sounds great! I only wish there was a > way to backport it all the way to 2.3 so I could drop the messy bits > from setuptools. Maybe we could? :-) Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk From andrew at acooke.org Thu Apr 2 16:14:14 2009 From: andrew at acooke.org (andrew cooke) Date: Thu, 2 Apr 2009 16:14:14 -0400 (CLT) Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: <49D51A16.70804@simplistix.co.uk> References: <49D4DA72.60401@v.loewis.de> <49D51A16.70804@simplistix.co.uk> Message-ID: <274e0e9e74f8a478c76b1a2a75e7ff85.squirrel@localhost> Chris Withers wrote: > Martin v. L?wis wrote: >> I propose the following PEP for inclusion to Python 3.1. >> Please comment. > > Would this support the following case: > > I have a package called mortar, which defines useful stuff: > > from mortar import content, ... > > I now want to distribute large optional chunks separately, but ideally > so that the following will will work: > > from mortar.rbd import ... > from mortar.zodb import ... > from mortar.wsgi import ... > > Does the PEP support this? The only way I can currently think to do this > would result in: > > from mortar import content,.. > from mortar_rbd import ... > from mortar_zodb import ... > from mortar_wsgi import ... i may be misunderstanding, but i think you can already do this. in lepl i have code spread across many modules (equivalent to your mortar.rbd, i have lepl.matchers etc). then in lepl/__init__.py i import those and define __this__ to export them into the lepl namespace. so you can import either do: from lepl import Literal or from lepl.matchers import Literal and you get the same code. i copied this from sqlalchemy, but i am sure other packages do something similar. it's described in the python docs. andrew From chris at simplistix.co.uk Thu Apr 2 16:18:57 2009 From: chris at simplistix.co.uk (Chris Withers) Date: Thu, 02 Apr 2009 21:18:57 +0100 Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: <274e0e9e74f8a478c76b1a2a75e7ff85.squirrel@localhost> References: <49D4DA72.60401@v.loewis.de> <49D51A16.70804@simplistix.co.uk> <274e0e9e74f8a478c76b1a2a75e7ff85.squirrel@localhost> Message-ID: <49D51DB1.3000404@simplistix.co.uk> andrew cooke wrote: >> I now want to distribute large optional chunks separately, but ideally >> so that the following will will work: >> >> from mortar.rbd import ... >> from mortar.zodb import ... >> from mortar.wsgi import ... > i may be misunderstanding, but i think you can already do this. > > in lepl i have code spread across many modules (equivalent to your > mortar.rbd, i have lepl.matchers etc). then in lepl/__init__.py i import > those and define __this__ to export them into the lepl namespace. so you > can import either do: Okay, but do you: - distribute lepl.matchers in a seperate distribution to lepl? - have actual code in the lepl package? cheers, Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk From andrew at acooke.org Thu Apr 2 16:29:24 2009 From: andrew at acooke.org (andrew cooke) Date: Thu, 2 Apr 2009 16:29:24 -0400 (CLT) Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: <274e0e9e74f8a478c76b1a2a75e7ff85.squirrel@localhost> References: <49D4DA72.60401@v.loewis.de> <49D51A16.70804@simplistix.co.uk> <274e0e9e74f8a478c76b1a2a75e7ff85.squirrel@localhost> Message-ID: andrew cooke wrote: > Chris Withers wrote: >> Martin v. L?wis wrote: >>> I propose the following PEP for inclusion to Python 3.1. >>> Please comment. >> >> Would this support the following case: >> >> I have a package called mortar, which defines useful stuff: >> >> from mortar import content, ... >> >> I now want to distribute large optional chunks separately, but ideally >> so that the following will will work: [...] > > i may be misunderstanding, but i think you can already do this. ah, sorry, i think i was misunderstanding. you mean within the mechanism provided by this new pep (which i also don't understand....) andrew From mal at egenix.com Thu Apr 2 16:33:25 2009 From: mal at egenix.com (M.-A. Lemburg) Date: Thu, 02 Apr 2009 22:33:25 +0200 Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: <49D4DA72.60401@v.loewis.de> References: <49D4DA72.60401@v.loewis.de> Message-ID: <49D52115.6020001@egenix.com> On 2009-04-02 17:32, Martin v. L?wis wrote: > I propose the following PEP for inclusion to Python 3.1. Thanks for picking this up. I'd like to extend the proposal to Python 2.7 and later. > Please comment. > > Regards, > Martin > > Specification > ============= > > Rather than using an imperative mechanism for importing packages, a > declarative approach is proposed here, as an extension to the existing > ``*.pkg`` mechanism. > > The import statement is extended so that it directly considers ``*.pkg`` > files during import; a directory is considered a package if it either > contains a file named __init__.py, or a file whose name ends with > ".pkg". That's going to slow down Python package detection a lot - you'd replace an O(1) test with an O(n) scan. Alternative Approach: --------------------- Wouldn't it be better to stick with a simpler approach and look for "__pkg__.py" files to detect namespace packages using that O(1) check ? This would also avoid any issues you'd otherwise run into if you want to maintain this scheme in an importer that doesn't have access to a list of files in a package directory, but is well capable for the checking the existence of a file. Mechanism: ---------- If the import mechanism finds a matching namespace package (a directory with a __pkg__.py file), it then goes into namespace package scan mode and scans the complete sys.path for more occurrences of the same namespace package. The import loads all __pkg__.py files of matching namespace packages having the same package name during the search. One of the namespace packages, the defining namespace package, will have to include a __init__.py file. After having scanned all matching namespace packages and loading the __pkg__.py files in the order of the search, the import mechanism then sets the packages .__path__ attribute to include all namespace package directories found on sys.path and finally executes the __init__.py file. (Please let me know if the above is not clear, I will then try to follow up on it.) Discussion: ----------- The above mechanism allows the same kind of flexibility we already have with the existing normal __init__.py mechanism. * It doesn't add yet another .pth-style sys.path extension (which are difficult to manage in installations). * It always uses the same naive sys.path search strategy. The strategy is not determined by some file contents. * The search is only done once - on the first import of the package. * It's possible to have a defining package dir and add-one package dirs. * Namespace packages are easy to recognize by testing for a single resource. * Namespace __pkg__.py modules can provide extra meta-information, logging, etc. to simplify debugging namespace package setups. * It's possible to freeze such setups, to put them into ZIP files, or only have parts of it in a ZIP file and the other parts in the file-system. Caveats: * Changes to sys.path will not result in an automatic rescan for additional namespace packages, if the package was already loaded. However, we could have a function to make such a rescan explicit. -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Apr 02 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/ ________________________________________________________________________ 2009-03-19: Released mxODBC.Connect 1.0.1 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 simon.hibbs at gmail.com Thu Apr 2 17:19:44 2009 From: simon.hibbs at gmail.com (Simon Hibbs) Date: Thu, 2 Apr 2009 14:19:44 -0700 (PDT) Subject: Converting a PIL image object to a buffer References: <79ddf944-2fee-46c5-aed7-e6276dce88ed@s19g2000vbp.googlegroups.com> <73j7olFv6l7uU1@mid.uni-berlin.de> Message-ID: <32ef1ca2-5ee0-441f-adad-cdd87b3ea23f@f11g2000vbf.googlegroups.com> On 2 Apr, 08:28, "Diez B. Roggisch" wrote: > Simon Hibbs schrieb: > > > > > On 1 Apr, 21:43, Gary Herron wrote: > >> Simon Hibbs wrote: > >>> I'm trying to dump a snapshot of my application window to the > >>> clipboard. I can use ImageGrab in PIL to get the screen data into a > >>> PIL image object, which i have converted to a bitmap using ImageWin, > >>> but when I try to pass this to the clipboard using - > >>> win32clipboard.SetClipboardData(win32clipboard.CF_BITMAP, img) > >>> It fails, telling be that "The object must support the buffer > >>> interface". > >>> How can I convert a PIL image into a buffer object? I can't find any > >>> clues. > >> PIL images have a tostring method that returns a string containing all > >> the pixel data. ?Would that help you to either create the needed > >> buffer? ?Or perhaps you could by-pass the need for a buffer, and just > >> use the byte string. > > > If I use tostring I get a string which I can put on the clipboard, but > > it isn't any kind of image. I can make a PIL image from the string but > > them I'm back to square one again. > > Did you actually try that? Strings support the buffer interface, and the > type of the binary data you set should be defined by the first argument. > > Alternatively (if the string is not of the proper format), maybe storing > the image to a (c)StringIO-object as BMP and retrieving it's value would > help. > > However, I think your concerns about "wasting memory" when using a file > are moot - creating an extra memory buffer isn't less memory consuming, > and if the file is living only a few seconds it might not even actually > hit the disk at all. In the end, the important thing is the working > clipboard. Yes I did try this, the code to dump the sting version to the clipboard worked, but pasting it back out wasn't possible although I could print them to the console. I don't mean wasting memory, just that actualy hitting the file system and creating a file seems as though it shouldn't be necessery. Is there any good reason why you can't just create an empty buffer object or file object directly? It seems like an unecessery restriction, unless there's some underlying reason such as that it's hard to implement in c, which seems implausible. Sorry, that's a rhetorical question. I've not had a chance to work on this today, if I find an elegant way round it I'll post the results for future googling. Simon Hibbs From pecora at anvil.nrl.navy.mil Thu Apr 2 17:29:58 2009 From: pecora at anvil.nrl.navy.mil (Lou Pecora) Date: Thu, 02 Apr 2009 17:29:58 -0400 Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> Message-ID: In article , Carl Banks wrote: > > I think people were being facetious. To me the first item in the list > is x[0]--ordinal does not match cardinal. However, I don't use > ordinals much when talking about list items; I'll say item 2, not > third item. Well, it's been said in many forms in this thread, but maybe this version will click with some. Thinking of ordinals as specifying position by order and cardinals as counting the number of steps from a specified place to an item you just have to realize that 0 and 1 based indexing have decided to use *different* definitions for the conglomerate symbols A[i] or A(i): 1 based indexing (ordinality): A[i] is the ith item in the ordered list (1st, 2nd, etc. - no need for 0th as an ordinal) 0 based indexing (cardinality): A[i] is the item i steps from the beginning of the list. I know most of what's been said is all around the above, I just thought explict statements might help. Both are prefectly reasonable and consistent definitions. Confusion only comes when you try to force the defintion of one of them on the other and then say it's illogical or not natural. Both are natural. Of course, in some languages like C an array name, say A, points to the first item in memory of the array which is assumed to be structured sequentially. Then A[0] is the first item at the address A or since we are using cardinality (0 based indexing) it's the item 0 steps from the beginning. A[1] is the item at address A+1, i.e. 1 step from the address A, the array beginning. I suspect that was a very practical choice for C since it's a systems language and down at that level you're flipping bit, bytes, addresses, etc. But a practical consequence was that there was very little +1 or -1 arithmetic necessary to manipulate the array elements. That carried over into other languages and their design. People have already pointed out the nice features of such in Python where A[n:n] is an empty list, A[:n]+A[n:]=A, etc. Now, I'm not a systems/computer guy. Just a scientist who does a lot of number crunching. But I have found out that even in that case you end of traversing lists, tuples, etc. a lot. Slicing and dicing them. And you quickly come to appreciate the 0 based approach to indexing and soon don't miss the Fortran 1-based indexing. -- -- Lou Pecora From irmen.NOSPAM at xs4all.nl Thu Apr 2 17:37:17 2009 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Thu, 02 Apr 2009 23:37:17 +0200 Subject: PIL Handbooks In-Reply-To: References: Message-ID: <49d53010$0$182$e4fe514c@news.xs4all.nl> W. eWatson wrote: > I'm very new to PIL, and don't see any handbooks for 1.1.6 or the > forthcoming 1.1.7. In fact, this looks like the extent of them: > > * Python Imaging Library Handbook for 1.1.5 (online) > * Python Imaging Library Handbook for 1.1.3 (PDF) > > Somewhere in my recent search I see that 1.1.6 has some features like > digital cameras and scanners. Ah here, > . Any other news? > > I'd settle even for a 1.1.5 pdf of the handbook right now. Download the source package tarball , it contains the HTML documentation. --irmen From ben+python at benfinney.id.au Thu Apr 2 17:51:47 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 03 Apr 2009 08:51:47 +1100 Subject: Tab completion References: <01e4c71e$0$20654$c3e8da3@news.astraweb.com> Message-ID: <87prfuhggc.fsf@benfinney.id.au> Steven D'Aprano writes: > I like tab-completion, but I'd rather not be reduced to typing > spaces for indents in the interpreter. What do other people do? Acknowledge that using U+0009 for indentation is wrong, and use beautiful U+0020 always. > The GNU readline library claims that M-tab (which I guess is > Alt-tab) will enter a tab character. ?M-tab? is Emacs-speak for ? plus ?. Not all keyboards have ?Alt?, you see (maybe none, when Emacs first started referring to the Meta modifier?). In most terminal programs, you can send ?-? also by pressing ?, ? (two separate keystrokes in sequence). > Any other suggestions or hints? Suck it up and accept the truth! U+0020 is the unambiguous indentation character that always does what it should, and U+0009 is a horrible mistake which must be suppressed with extreme prejudice. No true Pythonista would disagree. -- \ ?No matter how far down the wrong road you've gone, turn back.? | `\ ?Turkish proverb | _o__) | Ben Finney From ben+python at benfinney.id.au Thu Apr 2 17:59:41 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 03 Apr 2009 08:59:41 +1100 Subject: PEP 382: Namespace Packages References: Message-ID: <87ljqihg36.fsf@benfinney.id.au> Kay Schluehr writes: > Wow. You python-dev guys are really jumping the shark. Isn't your > Rube Goldberg "import machinery" already complex enough for you? Thanks for your constructive criticism, and your considerate quote trimming. -- \ ?I was married by a judge. I should have asked for a jury.? | `\ ?Groucho Marx | _o__) | Ben Finney From robert.kern at gmail.com Thu Apr 2 18:06:56 2009 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 02 Apr 2009 17:06:56 -0500 Subject: Tab completion In-Reply-To: <01e4c71e$0$20654$c3e8da3@news.astraweb.com> References: <01e4c71e$0$20654$c3e8da3@news.astraweb.com> Message-ID: On 2009-04-02 09:59, Steven D'Aprano wrote: > I like tab-completion, but I'd rather not be reduced to typing spaces for > indents in the interpreter. What do other people do? > Any other suggestions or hints? I use IPython. The completion is set up (I don't know how) such that tabbing for indentation does not trigger the completer. Additionally, you get reasonable auto-indents inside functions and loops so I rarely have to tab in the first place. -- 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 cdalten at gmail.com Thu Apr 2 18:14:49 2009 From: cdalten at gmail.com (grocery_stocker) Date: Thu, 2 Apr 2009 15:14:49 -0700 (PDT) Subject: Iteratoration question Message-ID: <2a43b3a8-84d5-434e-a9c3-5f60ed6ba5b2@r5g2000prh.googlegroups.com> Give the following code.. >>> class it: ... def __init__(self): ... self.count = -1 ... def next(self): ... self.count +=1 ... if self.count < 4: ... return self.count ... else: ... raise StopIteration ... >>> def some_func(): ... return it() ... >>> iterator = some_func() >>> iterator <__main__.it instance at 0xb7f482ac> >>> some_func >>> some_func() <__main__.it instance at 0xb7f4862c> How come when I call some_func().next(), the counter doesn't get incremented? >>> some_func().next() 0 >>> some_func().next() 0 >>> some_func().next() 0 But when I call iterator.next(), it does. >>> iterator.next() 0 >>> iterator.next() 1 >>> iterator.next() 2 >>> iterator.next() 3 >>> From rhodri at wildebst.demon.co.uk Thu Apr 2 18:16:22 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Thu, 02 Apr 2009 23:16:22 +0100 Subject: Introducing Python to others In-Reply-To: References: <039360fb-a29c-4f43-b6e0-ba97fb5981a3@z23g2000prd.googlegroups.com> Message-ID: On Thu, 02 Apr 2009 17:02:30 +0100, David C. Ullrich wrote: > Sometime I gotta get around to actually learning this 2.x > stuff. Thought I had an idea how 1.x worked... 3.x may come as a bit of a surprise :-) -- Rhodri James *-* Wildebeeste Herder to the Masses From emile at fenx.com Thu Apr 2 18:16:57 2009 From: emile at fenx.com (Emile van Sebille) Date: Thu, 02 Apr 2009 15:16:57 -0700 Subject: python for loop In-Reply-To: References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> Message-ID: Lou Pecora wrote: > Confusion only comes when you try to force the > defintion of one of them on the other and then say it's illogical or not > natural. Both are natural. Consider the French 'Premiere etage' vs the American 'First Floor' Emile From cdalten at gmail.com Thu Apr 2 18:17:43 2009 From: cdalten at gmail.com (grocery_stocker) Date: Thu, 2 Apr 2009 15:17:43 -0700 (PDT) Subject: iterator question References: <2a43b3a8-84d5-434e-a9c3-5f60ed6ba5b2@r5g2000prh.googlegroups.com> Message-ID: <96cb00ed-8941-429b-a428-c9f451a44182@v23g2000pro.googlegroups.com> On Apr 2, 3:14 pm, grocery_stocker wrote: > Give the following code.. > > >>> class it: > > ... def __init__(self): > ... self.count = -1 > ... def next(self): > ... self.count +=1 > ... if self.count < 4: > ... return self.count > ... else: > ... raise StopIteration > ...>>> def some_func(): > > ... return it() > ...>>> iterator = some_func() > >>> iterator > > <__main__.it instance at 0xb7f482ac>>>> some_func > > >>> some_func() > > <__main__.it instance at 0xb7f4862c> > > How come when I call some_func().next(), the counter doesn't get > incremented?>>> some_func().next() > 0 > >>> some_func().next() > 0 > >>> some_func().next() > > 0 > > But when I call iterator.next(), it does. > > >>> iterator.next() > 0 > >>> iterator.next() > 1 > >>> iterator.next() > 2 > >>> iterator.next() > 3 From max at alcyone.com Thu Apr 2 18:21:48 2009 From: max at alcyone.com (Erik Max Francis) Date: Thu, 02 Apr 2009 15:21:48 -0700 Subject: iterator question In-Reply-To: <96cb00ed-8941-429b-a428-c9f451a44182@v23g2000pro.googlegroups.com> References: <2a43b3a8-84d5-434e-a9c3-5f60ed6ba5b2@r5g2000prh.googlegroups.com> <96cb00ed-8941-429b-a428-c9f451a44182@v23g2000pro.googlegroups.com> Message-ID: grocery_stocker wrote: > How come when I call some_func().next(), the counter doesn't get > incremented? Because you're creating a new instance each time you call it. Each new instance starts with 0. > But when I call iterator.next(), it does. That's because you're iterating over a single object, so successive calls to .next give you success iteration values like you intended. -- 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 erikmaxfrancis We must all hang together, or, most assuredly, we will all hang separately. -- John Hancock From online.service at ymail.com Thu Apr 2 18:25:35 2009 From: online.service at ymail.com (online.service at ymail.com) Date: Thu, 2 Apr 2009 15:25:35 -0700 (PDT) Subject: python needs leaning stuff from other language Message-ID: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> python's list needs a thing list.clear() like c# arraylist and python needs a writeline() method From andrew at acooke.org Thu Apr 2 18:29:49 2009 From: andrew at acooke.org (andrew cooke) Date: Thu, 2 Apr 2009 18:29:49 -0400 (CLT) Subject: Iteratoration question In-Reply-To: <2a43b3a8-84d5-434e-a9c3-5f60ed6ba5b2@r5g2000prh.googlegroups.com> References: <2a43b3a8-84d5-434e-a9c3-5f60ed6ba5b2@r5g2000prh.googlegroups.com> Message-ID: grocery_stocker wrote: > Give the following code.. > >>>> class it: > ... def __init__(self): > ... self.count = -1 > ... def next(self): > ... self.count +=1 > ... if self.count < 4: > ... return self.count > ... else: > ... raise StopIteration > ... >>>> def some_func(): > ... return it() this creates a new instance of "it", with a new count, every time the function is called. >>>> iterator = some_func() this sets iterator to be an instance of "it". >>>> iterator > <__main__.it instance at 0xb7f482ac> this is the single "it" instance that iterator is bound to. >>>> some_func > >>>> some_func() > <__main__.it instance at 0xb7f4862c> this is another instance of "it" created when some_func was called. > How come when I call some_func().next(), the counter doesn't get > incremented? >>>> some_func().next() > 0 this creates yet another instance of "it" and calls next() on it. >>>> some_func().next() > 0 this creates another instance of "it" and calls next() on it. >>>> some_func().next() > 0 and another instance.... > But when I call iterator.next(), it does. >>>> iterator.next() > 0 this calls next() on the instance of "it" that iterator is bound to. >>>> iterator.next() > 1 this call next() again on the same instance of "it". >>>> iterator.next() > 2 >>>> iterator.next() > 3 in summary: iterator is bound to one instance of "it", while some_func() returns a new instance each time it is called. BUT while what you are doing is interesting, it is not the same as Python's iterators, which use "yield" from a function and don't require storing a value in a class. look for "yield" in the python docs. this comment may be irrelevant; i am just worried you are confusing the above (which apart from the mistake about instances is perfectly ok) and python's iterators (which use next(), yield, etc). andrew From rhodri at wildebst.demon.co.uk Thu Apr 2 18:33:16 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Thu, 02 Apr 2009 23:33:16 +0100 Subject: Iteratoration question In-Reply-To: <2a43b3a8-84d5-434e-a9c3-5f60ed6ba5b2@r5g2000prh.googlegroups.com> References: <2a43b3a8-84d5-434e-a9c3-5f60ed6ba5b2@r5g2000prh.googlegroups.com> Message-ID: On Thu, 02 Apr 2009 23:14:49 +0100, grocery_stocker wrote: > Give the following code.. > >>>> class it: > ... def __init__(self): > ... self.count = -1 > ... def next(self): > ... self.count +=1 > ... if self.count < 4: > ... return self.count > ... else: > ... raise StopIteration > ... >>>> def some_func(): > ... return it() > ... >>>> iterator = some_func() >>>> iterator > <__main__.it instance at 0xb7f482ac> >>>> some_func > >>>> some_func() > <__main__.it instance at 0xb7f4862c> > > How come when I call some_func().next(), the counter doesn't get > incremented? >>>> some_func().next() > 0 >>>> some_func().next() > 0 >>>> some_func().next() > 0 Here, you are creating an instance of the class "it", incrementing and returning that instance's counter, then throwing the instance away. > But when I call iterator.next(), it does. >>>> iterator.next() > 0 >>>> iterator.next() > 1 >>>> iterator.next() > 2 >>>> iterator.next() > 3 >>>> Here you already have a single instance, and you don't throw it away after incrementing its counter. -- Rhodri James *-* Wildebeeste Herder to the Masses From cdalten at gmail.com Thu Apr 2 18:37:16 2009 From: cdalten at gmail.com (grocery_stocker) Date: Thu, 2 Apr 2009 15:37:16 -0700 (PDT) Subject: Iteratoration question References: <2a43b3a8-84d5-434e-a9c3-5f60ed6ba5b2@r5g2000prh.googlegroups.com> Message-ID: > > in summary: iterator is bound to one instance of "it", while some_func() > returns a new instance each time it is called. > > BUT > > while what you are doing is interesting, it is not the same as Python's > iterators, which use "yield" from a function and don't require storing a > value in a class. look for "yield" in the python docs. this comment may > be irrelevant; i am just worried you are confusing the above (which apart > from the mistake about instances is perfectly ok) and python's iterators > (which use next(), yield, etc). > Okay, one last question for now When I have the follow class >>> class it: ... def __init__(self): ... self.count = -1 ... def next(self): ... self.count +=1 ... if self.count < 4: ... return self.count ... else: ... raise StopIteraton ... >>> value = it() How comes I can;t go over 'value' like in the following >>> for x in value: ... print x ... Traceback (most recent call last): File "", line 1, in ? TypeError: iteration over non-sequence But yet, I can do... >>> value.next() 0 >>> value.next() 1 >>> value.next() 2 >>> value.next() 3 >>> From deets at nospam.web.de Thu Apr 2 18:39:46 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 03 Apr 2009 00:39:46 +0200 Subject: python needs leaning stuff from other language In-Reply-To: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> Message-ID: <73kt5jFvitcdU1@mid.uni-berlin.de> online.service at ymail.com schrieb: > python's list needs a thing list.clear() like c# arraylist > and some_list[:] = [] > python needs a writeline() method print() Diez From tim.wintle at teamrubber.com Thu Apr 2 18:46:43 2009 From: tim.wintle at teamrubber.com (Tim Wintle) Date: Thu, 02 Apr 2009 23:46:43 +0100 Subject: pygame and socket.recv In-Reply-To: References: <1c6574dd-1bfb-4c45-a744-58761e5d1f06@k2g2000yql.googlegroups.com> <2084b37d-485c-435f-be5c-8ee1bc901d70@p11g2000yqe.googlegroups.com> Message-ID: <1238712403.4604.5.camel@tim-laptop> On Thu, 2009-04-02 at 06:50 -0700, Aaron Brady wrote: > It's just that if you register a collision in between the time that > one object has changed its position and momentum, and the time you > learn about it, you have to retroactively edit the collision, restore > hit points, and recalculate the other object's position and momentum, > to name a few. So it sounds like what you are trying to do is emulate shared RAM between the two players! The problem being that you have to effectively lock the variables on one machine while the other one reacts. I believe the answer normally used is the client-server architecture. Choose one machine to act as the "server" somehow, then let that control the entire game state. Both players pass events to this server method (using non-blocking sockets), and render things in the position they believe they are in. I believe that is how all the current RTS games work, certainly how Red Alert worked, and is how all FPS games work (e.g. in unreal there is always a "server", and in single player mode the clients are simply bots and the local client application.) You might find the docs for Unreal networking (c. 1999) useful to explain what they did better than I can. http://unreal.epicgames.com/Network.htm From darkstorme.ouroboros at gmail.com Thu Apr 2 18:49:05 2009 From: darkstorme.ouroboros at gmail.com (darkstorme) Date: Thu, 2 Apr 2009 15:49:05 -0700 (PDT) Subject: Python, the Win32api, and directing input to game clients Message-ID: I'm trying to use python to create a sort of control bridge between IRC and the Neverwinter Nights game client. One direction (output) is easy; the game, conveniently, writes its logs to a text file - polling and parsing do the rest in that regard. However, input, as yet, eludes me. I've fooled around with the windows api, but both SendKeys and SendMessage fail to register on the game window. When testing in Notepad, the text and other keypresses appear as planned. They do not, however, in Neverwinter. It's been suggested on some sites I've come across that Win32API calls might not work on games because the games are coded "closer to the metal", as it were, and don't fall under the abstraction of the Win32 shell. Basically, a) can anyone give me a pointer in a direction I should be heading, and/or b) is the Win32api a dead end for this goal? Many thanks in advance. From rhodri at wildebst.demon.co.uk Thu Apr 2 18:50:38 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Thu, 02 Apr 2009 23:50:38 +0100 Subject: Iteratoration question In-Reply-To: References: <2a43b3a8-84d5-434e-a9c3-5f60ed6ba5b2@r5g2000prh.googlegroups.com> Message-ID: On Thu, 02 Apr 2009 23:37:16 +0100, grocery_stocker wrote: > >> >> in summary: iterator is bound to one instance of "it", while some_func() >> returns a new instance each time it is called. >> >> BUT >> >> while what you are doing is interesting, it is not the same as Python's >> iterators, which use "yield" from a function and don't require storing a >> value in a class. look for "yield" in the python docs. this comment >> may >> be irrelevant; i am just worried you are confusing the above (which >> apart >> from the mistake about instances is perfectly ok) and python's iterators >> (which use next(), yield, etc). >> > > Okay, one last question for now > [snip] > > How comes I can;t go over 'value' like in the following > >>>> for x in value: > ... print x > ... > Traceback (most recent call last): > File "", line 1, in ? > TypeError: iteration over non-sequence I refer you back to Andrew's answer. Your "value" object isn't an iterator and doesn't obey the Iteration protocol. Expecting to be able to iterate over it is a tad optimistic. -- Rhodri James *-* Wildebeeste Herder to the Masses From ebonak at hotmail.com Thu Apr 2 18:54:01 2009 From: ebonak at hotmail.com (Esmail) Date: Thu, 02 Apr 2009 18:54:01 -0400 Subject: python needs leaning stuff from other language In-Reply-To: <73kt5jFvitcdU1@mid.uni-berlin.de> References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <73kt5jFvitcdU1@mid.uni-berlin.de> Message-ID: <49D54209.5090306@hotmail.com> Diez B. Roggisch wrote: > online.service at ymail.com schrieb: >> python's list needs a thing list.clear() like c# arraylist >> and > > some_list[:] = [] I agree that this is nice and clear, but as a relative newbie wouldn't some_list = [] be also acceptable (and pythonic?)? From ebonak at hotmail.com Thu Apr 2 18:54:01 2009 From: ebonak at hotmail.com (Esmail) Date: Thu, 02 Apr 2009 18:54:01 -0400 Subject: python needs leaning stuff from other language In-Reply-To: <73kt5jFvitcdU1@mid.uni-berlin.de> References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <73kt5jFvitcdU1@mid.uni-berlin.de> Message-ID: <49D54209.5090306@hotmail.com> Diez B. Roggisch wrote: > online.service at ymail.com schrieb: >> python's list needs a thing list.clear() like c# arraylist >> and > > some_list[:] = [] I agree that this is nice and clear, but as a relative newbie wouldn't some_list = [] be also acceptable (and pythonic?)? From castironpi at gmail.com Thu Apr 2 18:55:48 2009 From: castironpi at gmail.com (Aaron Brady) Date: Thu, 2 Apr 2009 15:55:48 -0700 (PDT) Subject: pygame and socket.recv References: <1c6574dd-1bfb-4c45-a744-58761e5d1f06@k2g2000yql.googlegroups.com> <36b50e9b-55c1-42a9-90ae-84d31f5665da@h28g2000yqd.googlegroups.com> Message-ID: <8a2b6a6d-6c28-4feb-b096-e1f85bf6096c@v15g2000yqn.googlegroups.com> On Apr 2, 10:12?am, Miguel Prada wrote: > I don't know if this might be causing your problem, but most socket > implementations use quite a big buffer for incoming data by default. I > had a lot of trouble with another real-time networked application > until I realised this. Reducing this buffer to the minimum helped a > lot in my case. Also, I would strongly recommend using UDP instead of > TCP. > > BTW, does anyone know if any good bibliography about real-time > networked games, or real-time networked general applications? I switched to UDP. My average round-trip time is at 50 trips/sec, but my worst round-trip time is still in the 10-20 range. I also tried buffer sizes of 2**8 and 2**12, with about the same results. So, UDP might free up some processor time, but it still leaves me with the problem of retroactive model updates. From kyrie at uh.cu Thu Apr 2 18:56:02 2009 From: kyrie at uh.cu (Luis Alberto Zarrabeitia Gomez) Date: Thu, 02 Apr 2009 18:56:02 -0400 Subject: python needs leaning stuff from other language In-Reply-To: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> Message-ID: <1238712962.49d54282dbb83@mail.uh.cu> Quoting online.service at ymail.com: > python's list needs a thing list.clear() like c# arraylist It has: >>> l[:] = [] > python needs a writeline() method Now, that could be useful, a writeline method that knew the EOL convention for the OS and were not as deceiving as the current .writelines(). BTW, check out the print statement from python2, and the print function in python3: >>> print >>fileobj, "this will print with an eol" # python2 >>> print("this will print with an eol", file=fileobj) -- 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 kyrie at uh.cu Thu Apr 2 19:02:55 2009 From: kyrie at uh.cu (Luis Alberto Zarrabeitia Gomez) Date: Thu, 02 Apr 2009 19:02:55 -0400 Subject: python needs leaning stuff from other language In-Reply-To: <49D54209.5090306@hotmail.com> References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <73kt5jFvitcdU1@mid.uni-berlin.de> <49D54209.5090306@hotmail.com> Message-ID: <1238713375.49d5441f456e4@mail.uh.cu> Quoting Esmail : > Diez B. Roggisch wrote: > > > > some_list[:] = [] > > I agree that this is nice and clear, Not very intuitive, though, until you learn the syntax. (but, slice accessing and slice assignment are among the first few things one learns about python anyway, and once you learn it, it seems more than natural) > but as a relative newbie > wouldn't > > some_list = [] > > be also acceptable (and pythonic?)? Pythonic, yes. Acceptable, it may not be (depending on the problem). And I would adventure that if someone needs to clear a list instead of creating an empty one, its likely that someone is facing one of those problems (i.e: clearing a list that you got as a function argument, or playing with os.walk). -- 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 gert.cuykens at gmail.com Thu Apr 2 19:04:14 2009 From: gert.cuykens at gmail.com (gert) Date: Thu, 2 Apr 2009 16:04:14 -0700 (PDT) Subject: with open('com1', 'r') as f: References: <8bc55c05-19da-41c4-b916-48e0a4be49b9@p11g2000yqe.googlegroups.com> <20090403002347.163911b4@oxygen> Message-ID: <8de4f974-073f-41cb-9729-341a02ca4c3f@u8g2000yqn.googlegroups.com> On Apr 2, 8:53?pm, Kushal Kumaran wrote: > On Thu, 2 Apr 2009 10:01:02 -0700 (PDT) > > > > gert wrote: > > from subprocess import * > > > check_call(['mode', 'COM1:9600,N,8,1,P'],shell=True) > > while True: > > ? ? with open('com1', 'r') as f: > > ? ? ? ? for line in f: > > ? ? ? ? ? ? ?print('line') > > > This works very well except for one thing. After a reboot I have to > > launch 1 time any windows serial exe application no mater with one, > > that just opens an closes the com port, before i can launch this > > script. The script keeps on working even after closing and reopening > > it, until i reboot the pc. Then again I have to launch one time a > > serial.exe and close it again. The exe does not run anything in the > > background it just does something in windows python does not do when > > it reads from the com port after a fresh reboot. > > > And i really appreciate it if somebody knew what it was. > > I don't know why you're getting this behaviour, but have you tried using > a python library for accessing the serial port? Seehttp://pyserial.wiki.sourceforge.net/pySerial. > I am sorry but I don't think pyserial will work on python3.x and I also like to know whats going on before I consider it. Maybe its a bug in open() on windows? From emile at fenx.com Thu Apr 2 19:07:02 2009 From: emile at fenx.com (Emile van Sebille) Date: Thu, 02 Apr 2009 16:07:02 -0700 Subject: python needs leaning stuff from other language In-Reply-To: <49D54209.5090306@hotmail.com> References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <73kt5jFvitcdU1@mid.uni-berlin.de> <49D54209.5090306@hotmail.com> Message-ID: Esmail wrote: > Diez B. Roggisch wrote: >> online.service at ymail.com schrieb: >>> python's list needs a thing list.clear() like c# arraylist >>> and >> >> some_list[:] = [] > > I agree that this is nice and clear, but as a relative newbie > wouldn't > > some_list = [] This is different -- it creates a new list. Consider: >>> some_list = [1,2,3] >>> d = some_list >>> d[1] 2 >>> some_list[:] = ['a','b','c'] >>> d[1] 'b' >>> some_list = [1,2,3] >>> d[1] 'b' the [:] form allows references into the list to remain valid while the direct assignment dopes not. Emile > > be also acceptable (and pythonic?)? > > -- > http://mail.python.org/mailman/listinfo/python-list > From rhodri at wildebst.demon.co.uk Thu Apr 2 19:07:27 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Fri, 03 Apr 2009 00:07:27 +0100 Subject: pygame and socket.recv In-Reply-To: <8a2b6a6d-6c28-4feb-b096-e1f85bf6096c@v15g2000yqn.googlegroups.com> References: <1c6574dd-1bfb-4c45-a744-58761e5d1f06@k2g2000yql.googlegroups.com> <36b50e9b-55c1-42a9-90ae-84d31f5665da@h28g2000yqd.googlegroups.com> <8a2b6a6d-6c28-4feb-b096-e1f85bf6096c@v15g2000yqn.googlegroups.com> Message-ID: On Thu, 02 Apr 2009 23:55:48 +0100, Aaron Brady wrote: > I switched to UDP. My average round-trip time is at 50 trips/sec, but > my worst round-trip time is still in the 10-20 range. > > I also tried buffer sizes of 2**8 and 2**12, with about the same > results. > > So, UDP might free up some processor time, but it still leaves me with > the problem of retroactive model updates. You are dealing with a network. Errors *will* happen. Plan for them from the start, or they will turn your design into spaghetti :-) -- Rhodri James *-* Wildebeeste Herder to the Masses From andrew at acooke.org Thu Apr 2 19:08:43 2009 From: andrew at acooke.org (andrew cooke) Date: Thu, 2 Apr 2009 19:08:43 -0400 (CLT) Subject: Iteratoration question In-Reply-To: References: <2a43b3a8-84d5-434e-a9c3-5f60ed6ba5b2@r5g2000prh.googlegroups.com> Message-ID: <02adc575560fa8733aee3c7f59668dac.squirrel@localhost> grocery_stocker wrote: > >> >> in summary: iterator is bound to one instance of "it", while some_func() >> returns a new instance each time it is called. >> >> BUT >> >> while what you are doing is interesting, it is not the same as Python's >> iterators, which use "yield" from a function and don't require storing a >> value in a class. look for "yield" in the python docs. this comment >> may >> be irrelevant; i am just worried you are confusing the above (which >> apart >> from the mistake about instances is perfectly ok) and python's iterators >> (which use next(), yield, etc). >> > > Okay, one last question for now > > When I have the follow class > >>>> class it: > ... def __init__(self): > ... self.count = -1 > ... def next(self): > ... self.count +=1 > ... if self.count < 4: > ... return self.count > ... else: > ... raise StopIteraton > ... > > > >>>> value = it() > > How comes I can;t go over 'value' like in the following > >>>> for x in value: > ... print x > ... > Traceback (most recent call last): > File "", line 1, in ? > TypeError: iteration over non-sequence > > But yet, I can do... > >>>> value.next() > 0 >>>> value.next() > 1 >>>> value.next() > 2 >>>> value.next() > 3 replace return with yield and it might work. i have to go eat, but if it doesn't read the python docs on iterators - for example http://docs.python.org/reference/expressions.html#index-1825 andrew From robert.kern at gmail.com Thu Apr 2 19:18:02 2009 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 02 Apr 2009 18:18:02 -0500 Subject: Iteratoration question In-Reply-To: <02adc575560fa8733aee3c7f59668dac.squirrel@localhost> References: <2a43b3a8-84d5-434e-a9c3-5f60ed6ba5b2@r5g2000prh.googlegroups.com> <02adc575560fa8733aee3c7f59668dac.squirrel@localhost> Message-ID: On 2009-04-02 18:08, andrew cooke wrote: > grocery_stocker wrote: >>> in summary: iterator is bound to one instance of "it", while some_func() >>> returns a new instance each time it is called. >>> >>> BUT >>> >>> while what you are doing is interesting, it is not the same as Python's >>> iterators, which use "yield" from a function and don't require storing a >>> value in a class. look for "yield" in the python docs. this comment >>> may >>> be irrelevant; i am just worried you are confusing the above (which >>> apart >>> from the mistake about instances is perfectly ok) and python's iterators >>> (which use next(), yield, etc). >>> >> Okay, one last question for now >> >> When I have the follow class >> >>>>> class it: >> ... def __init__(self): >> ... self.count = -1 >> ... def next(self): >> ... self.count +=1 >> ... if self.count< 4: >> ... return self.count >> ... else: >> ... raise StopIteraton >> ... >> >> >> >>>>> value = it() >> How comes I can;t go over 'value' like in the following >> >>>>> for x in value: >> ... print x >> ... >> Traceback (most recent call last): >> File "", line 1, in ? >> TypeError: iteration over non-sequence >> >> But yet, I can do... >> >>>>> value.next() >> 0 >>>>> value.next() >> 1 >>>>> value.next() >> 2 >>>>> value.next() >> 3 > > replace return with yield and it might work. > > i have to go eat, but if it doesn't read the python docs on iterators - > for example http://docs.python.org/reference/expressions.html#index-1825 No, .next() needs to be a regular function that returns a value. What he needs is an __iter__() method that returns self. Alternately, __iter__ could be a generator, and you wouldn't implement a .next() at all. In [1]: class it(object): ...: def __init__(self): ...: self.count = -1 ...: def __iter__(self): ...: return self ...: def next(self): ...: self.count += 1 ...: if self.count < 4: ...: return self.count ...: else: ...: raise StopIteration ...: ...: In [2]: class it2(object): ...: def __init__(self): ...: self.count = -1 ...: def __iter__(self): ...: self.count += 1 ...: while self.count < 4: ...: yield self.count ...: self.count += 1 ...: ...: In [3]: list(it()) Out[3]: [0, 1, 2, 3] In [4]: list(it2()) Out[4]: [0, 1, 2, 3] -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco From drobinow at gmail.com Thu Apr 2 19:20:18 2009 From: drobinow at gmail.com (drobinow at gmail.com) Date: Thu, 2 Apr 2009 16:20:18 -0700 (PDT) Subject: PEP 382: Namespace Packages References: <87ljqihg36.fsf@benfinney.id.au> Message-ID: On Apr 2, 5:59?pm, Ben Finney wrote: > Kay Schluehr writes: > > Wow. You python-dev guys are really jumping the shark. Isn't your > > Rube Goldberg "import machinery" already complex enough for you? > > Thanks for your constructive criticism, and your considerate quote > trimming. Ben, you should use google groups. No trimming necessary. From rhodri at wildebst.demon.co.uk Thu Apr 2 19:21:01 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Fri, 03 Apr 2009 00:21:01 +0100 Subject: Python Goes Mercurial In-Reply-To: <17ab8b08-73cf-42ea-ba83-a8d6d8b9281b@c9g2000yqm.googlegroups.com> References: <35d429fa-5d13-4703-a443-6a95c740c9fb@o6g2000yql.googlegroups.com> <4dad5e13-153f-446d-9f2b-06ebc653e003@c9g2000yqm.googlegroups.com> <17ab8b08-73cf-42ea-ba83-a8d6d8b9281b@c9g2000yqm.googlegroups.com> Message-ID: On Thu, 02 Apr 2009 19:24:49 +0100, Kay Schluehr wrote: > Good to know. Uninstalling a major feature that enhances usability > just to make it usable isn't much less ironic though. Meh. Use the command line like God intended. -- Rhodri James *-* Wildebeeste Herder to the Masses From gherron at islandtraining.com Thu Apr 2 19:29:33 2009 From: gherron at islandtraining.com (Gary Herron) Date: Thu, 02 Apr 2009 16:29:33 -0700 Subject: python needs leaning stuff from other language In-Reply-To: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> Message-ID: <49D54A5D.1030200@islandtraining.com> online.service at ymail.com wrote: > python's list needs a thing list.clear() like c# arraylist > and > python needs a writeline() method > > -- > http://mail.python.org/mailman/listinfo/python-list > (While you are correct that Python needs these things, a better attitude, as a newbie, would be to *ask* how Python supplies these features you want, because in fact, they have been included in the language for a very long time.) Both some_list[:] = [] and del some_list[:] will clear the contents of a list, but will leave the identity of the list unchanged. That is A = [1,2,3] B = A del A[:] will leave both A and B referencing the same empty list. Alternatively A = [1,2,3] B = A A = [] will leave A referencing a new empty list, but B will reference the original [1,2,3] list. As for a writeline(), and guessing what it is you want, I'd say look at the print>>outfile, ... form in Python 2.5. In Python 3.X, the print(...) function will get you the functionality (I think) you want. Gary Herron From tim.wintle at teamrubber.com Thu Apr 2 19:34:37 2009 From: tim.wintle at teamrubber.com (Tim Wintle) Date: Fri, 03 Apr 2009 00:34:37 +0100 Subject: python for loop In-Reply-To: References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> Message-ID: <1238715277.13834.3.camel@tim-laptop> On Thu, 2009-04-02 at 15:16 -0700, Emile van Sebille wrote: > Lou Pecora wrote: > > Confusion only comes when you try to force the > > defintion of one of them on the other and then say it's illogical or not > > natural. Both are natural. > > Consider the French 'Premiere etage' vs the American 'First Floor' or even in the same language - "first floor" in English (UK) is very different from "first floor" in English (US). > > Emile > > > > -- > http://mail.python.org/mailman/listinfo/python-list From ben+python at benfinney.id.au Thu Apr 2 19:36:08 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 03 Apr 2009 10:36:08 +1100 Subject: PEP 382: Namespace Packages References: <87ljqihg36.fsf@benfinney.id.au> Message-ID: <87fxgqfx1z.fsf@benfinney.id.au> "drobinow at gmail.com" writes: > On Apr 2, 5:59?pm, Ben Finney wrote: > > Thanks for your constructive criticism, and your considerate quote > > trimming. > Ben, you should use google groups. No, I really shouldn't. > No trimming necessary. It's not me that should do the trimming. -- \ ?Jealousy: The theory that some other fellow has just as little | `\ taste.? ?Henry L. Mencken | _o__) | Ben Finney From andrew at acooke.org Thu Apr 2 19:41:44 2009 From: andrew at acooke.org (andrew cooke) Date: Thu, 2 Apr 2009 19:41:44 -0400 (CLT) Subject: Iteratoration question In-Reply-To: References: <2a43b3a8-84d5-434e-a9c3-5f60ed6ba5b2@r5g2000prh.googlegroups.com> <02adc575560fa8733aee3c7f59668dac.squirrel@localhost> Message-ID: Robert Kern wrote: >> replace return with yield and it might work. >> >> i have to go eat, but if it doesn't read the python docs on iterators - >> for example http://docs.python.org/reference/expressions.html#index-1825 > > No, .next() needs to be a regular function that returns a value. What he > needs > is an __iter__() method that returns self. Alternately, __iter__ could be yeah, sorry, i was in a rush and not thinking straight. andrew From emile at fenx.com Thu Apr 2 19:51:24 2009 From: emile at fenx.com (Emile van Sebille) Date: Thu, 02 Apr 2009 16:51:24 -0700 Subject: A design problem I met again and again. In-Reply-To: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> Message-ID: ??? wrote: > Hi all, > > I am a programmer who works with some different kinds of programming > languages, like python, C++(in COM), action script, C#, etc. > > Today, I realized that, what ever language I use, I always meet a same > problem and I think I never solve it very well. > > The problem is : how to break my app into functional pieces? My question would be why? Refactoring adds nothing to a functioning app but clarity and maintainability -- both admirable qualities, granted, and both unnecessary until needed. When I need to update an app is when I start refactoring, and then just those areas that need it. Certainly I refactor constantly during development to avoid code reuse through cut-n-paste, but once I've got it going, whether it's 1000 or 6000 lines, it doesn't matter as long as it works. I'll tease it out when the upgrades are needed, new applications can reuse pieces, or sooner if business refactoring requires it. Emile, writing in the role of sole developer and maintainer of 500k lines of code dating back 35 years... From abakarmukhtar at optusnet.com.au Thu Apr 2 19:52:13 2009 From: abakarmukhtar at optusnet.com.au (abakarmukhtar) Date: Thu, 2 Apr 2009 15:52:13 -0800 Subject: dictionary and list Message-ID: <200904020728.n327SCJv032572@mail04.syd.optusnet.com.au> -------------- next part -------------- An HTML attachment was scrubbed... URL: From ebonak at hotmail.com Thu Apr 2 20:32:00 2009 From: ebonak at hotmail.com (Esmail) Date: Thu, 02 Apr 2009 20:32:00 -0400 Subject: python needs leaning stuff from other language In-Reply-To: References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <73kt5jFvitcdU1@mid.uni-berlin.de> <49D54209.5090306@hotmail.com> Message-ID: Emile van Sebille wrote: > Esmail wrote: >> Diez B. Roggisch wrote: >>> online.service at ymail.com schrieb: >>>> python's list needs a thing list.clear() like c# arraylist >>>> and >>> >>> some_list[:] = [] >> >> I agree that this is nice and clear, but as a relative newbie >> wouldn't >> >> some_list = [] > > This is different -- it creates a new list. Consider: > > >>> some_list = [1,2,3] > >>> d = some_list > >>> d[1] > 2 > >>> some_list[:] = ['a','b','c'] > >>> d[1] > 'b' > >>> some_list = [1,2,3] > >>> d[1] > 'b' > > the [:] form allows references into the list to remain valid while the > direct assignment dopes not. Ah .. thanks for clarifying this .. makes sense. Also, thank you Luis for your post. Esmail From notvalid2 at sbcglobal.net Thu Apr 2 20:36:43 2009 From: notvalid2 at sbcglobal.net (W. eWatson) Date: Thu, 02 Apr 2009 17:36:43 -0700 Subject: PIL Handbooks In-Reply-To: <49d53010$0$182$e4fe514c@news.xs4all.nl> References: <49d53010$0$182$e4fe514c@news.xs4all.nl> Message-ID: Irmen de Jong wrote: > W. eWatson wrote: >> I'm very new to PIL, and don't see any handbooks for 1.1.6 or the >> forthcoming 1.1.7. In fact, this looks like the extent of them: >> >> * Python Imaging Library Handbook for 1.1.5 (online) >> * Python Imaging Library Handbook for 1.1.3 (PDF) >> >> Somewhere in my recent search I see that 1.1.6 has some features like >> digital cameras and scanners. Ah here, >> . Any other news? >> >> I'd settle even for a 1.1.5 pdf of the handbook right now. > > Download the source package tarball , it contains the HTML documentation. > > --irmen In the Doc foler, I see a bunch of html files, but nothing about a handbook. It seems like the front sections are missing in that folder. Concepts, tutor, whatever. -- 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 pje at telecommunity.com Thu Apr 2 20:44:00 2009 From: pje at telecommunity.com (P.J. Eby) Date: Thu, 02 Apr 2009 20:44:00 -0400 Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: <49D52115.6020001@egenix.com> References: <49D4DA72.60401@v.loewis.de> <49D52115.6020001@egenix.com> Message-ID: <20090403004135.B76443A40A7@sparrow.telecommunity.com> At 10:33 PM 4/2/2009 +0200, M.-A. Lemburg wrote: >That's going to slow down Python package detection a lot - you'd >replace an O(1) test with an O(n) scan. I thought about this too, but it's pretty trivial considering that the only time it takes effect is when you have a directory name that matches the name you're importing, and that it will only happen once for that directory, unless there is no package on sys.path with that name, and the program tries to import the package multiple times. In other words, the overhead isn't likely to be much, compared to the time needed to say, open and marshal even a trivial __init__.py file. >Alternative Approach: >--------------------- > >Wouldn't it be better to stick with a simpler approach and look for >"__pkg__.py" files to detect namespace packages using that O(1) check ? I thought the same thing (or more precisely, a single .pkg file), but when I got lower in the PEP I saw the reason was to support system packages not having overlapping filenames. The PEP could probably be a little clearer about the connection between needing *.pkg and the system-package use case. >One of the namespace packages, the defining namespace package, will have >to include a __init__.py file. Note that there is no such thing as a "defining namespace package" -- namespace package contents are symmetrical peers. >The above mechanism allows the same kind of flexibility we already >have with the existing normal __init__.py mechanism. > >* It doesn't add yet another .pth-style sys.path extension (which are >difficult to manage in installations). > >* It always uses the same naive sys.path search strategy. The strategy >is not determined by some file contents. The above are also true for using only a '*' in .pkg files -- in that event there are no sys.path changes. (Frankly, I'm doubtful that anybody is using extend_path and .pkg files to begin with, so I'd be fine with a proposal that instead used something like '.nsp' files that didn't even need to be opened and read -- which would let the directory scan stop at the first .nsp file found. >* The search is only done once - on the first import of the package. I believe the PEP does this as well, IIUC. >* It's possible to have a defining package dir and add-one package >dirs. Also possible in the PEP, although the __init__.py must be in the first such directory on sys.path. (However, such "defining" packages are not that common now, due to tool limitations.) From cdalten at gmail.com Thu Apr 2 21:07:38 2009 From: cdalten at gmail.com (grocery_stocker) Date: Thu, 2 Apr 2009 18:07:38 -0700 (PDT) Subject: Iteratoration question References: <2a43b3a8-84d5-434e-a9c3-5f60ed6ba5b2@r5g2000prh.googlegroups.com> <02adc575560fa8733aee3c7f59668dac.squirrel@localhost> Message-ID: On Apr 2, 4:41 pm, "andrew cooke" wrote: > Robert Kern wrote: > >> replace return with yield and it might work. > > >> i have to go eat, but if it doesn't read the python docs on iterators - > >> for examplehttp://docs.python.org/reference/expressions.html#index-1825 > > > No, .next() needs to be a regular function that returns a value. What he > > needs > > is an __iter__() method that returns self. Alternately, __iter__ could be > > yeah, sorry, i was in a rush and not thinking straight. andrew Okay, I was thinking more about this. I think this is also what is irking me. Say I have the following.. >>> a = [1,2,3,4] >>> for x in a: ... print x ... 1 2 3 4 >>> Would 'a' somehow call __iter__ and next()? If so, does python just perform this magically? From ndbecker2 at gmail.com Thu Apr 2 21:15:49 2009 From: ndbecker2 at gmail.com (Neal Becker) Date: Thu, 02 Apr 2009 21:15:49 -0400 Subject: PEP 382: Namespace Packages References: <49D4DA72.60401@v.loewis.de> Message-ID: While solving this problem, is it possible also to address an issue that shows up in certain distributions? I'm specifically talking about the fact that on Redhat/Fedora, we have on x86_64 both /usr/lib/pythonxx/ and /usr/lib64/pythonxx. The former is supposed to be for non-arch specific packages (pure python) and the latter for arch-specific. If it happens that there is: /usr/lib/pythonxxx/site-packages/mypackage/ and /usr/lib64/pythonxxx/site-packages/mypackage/subpackage This (and probably some similar variations) won't work with the current module loading algorithm. (I believe this is the issue I encountered, it was a while ago). From doko at ubuntu.com Thu Apr 2 21:21:10 2009 From: doko at ubuntu.com (Matthias Klose) Date: Fri, 03 Apr 2009 03:21:10 +0200 Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: <49D4DA72.60401@v.loewis.de> References: <49D4DA72.60401@v.loewis.de> Message-ID: <49D56486.8020708@ubuntu.com> Martin v. L?wis schrieb: > I propose the following PEP for inclusion to Python 3.1. > Please comment. > > Regards, > Martin > > Abstract > ======== > > Namespace packages are a mechanism for splitting a single Python > package across multiple directories on disk. In current Python > versions, an algorithm to compute the packages __path__ must be > formulated. With the enhancement proposed here, the import machinery > itself will construct the list of directories that make up the > package. +1 speaking as a downstream packaging python for Debian/Ubuntu I welcome this approach. The current practice of shipping the very same file (__init__.py) in different packages leads to conflicts for the installation of these packages (this is not specific to dpkg, but is true for rpm packaging as well). Current practice of packaging (for downstreams) so called "name space packages" is: - either to split out the namespace __init__.py into a separate (linux distribution) package (needing manual packaging effort for each name space package) - using downstream specific packaging techniques to handle conflicting files (diversions) - replicating the current behaviour of setuptools simply overwriting the file conflicts. Following this proposal (downstream) packaging of namespace packages is made possible independent of any manual downstream packaging decisions or any downstream specific packaging decisions. Matthias From andrew at acooke.org Thu Apr 2 21:29:24 2009 From: andrew at acooke.org (andrew cooke) Date: Thu, 2 Apr 2009 21:29:24 -0400 (CLT) Subject: Iteratoration question In-Reply-To: References: <2a43b3a8-84d5-434e-a9c3-5f60ed6ba5b2@r5g2000prh.googlegroups.com> <02adc575560fa8733aee3c7f59668dac.squirrel@localhost> Message-ID: grocery_stocker wrote: > Okay, I was thinking more about this. I think this is also what is > irking me. Say I have the following.. > >>>> a = [1,2,3,4] >>>> for x in a: > ... print x > ... > 1 > 2 > 3 > 4 >>>> > > Would 'a' somehow call __iter__ and next()? If so, does python just > perform this magically? yes! in fact you can see them: Python 2.5.4 (r254:67916, Mar 23 2009, 17:43:35) [GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> l = [1,2,3,4] >>> dir(l) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__str__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] >>> l.__iter__ >>> i = l.__iter__() >>> dir(i) ['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', '__init__', '__iter__', '__length_hint__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'next'] >>> i.next() 1 >>> i.next() 2 note that the list only has __iter__, not next. calling __iter__() returns an iterator (something that has a next method) and calling next on that gives you the same result. dir() just shows all the attributes of an object. andrew From andrew at acooke.org Thu Apr 2 21:32:41 2009 From: andrew at acooke.org (andrew cooke) Date: Thu, 2 Apr 2009 21:32:41 -0400 (CLT) Subject: Iteratoration question In-Reply-To: References: <2a43b3a8-84d5-434e-a9c3-5f60ed6ba5b2@r5g2000prh.googlegroups.com> <02adc575560fa8733aee3c7f59668dac.squirrel@localhost> Message-ID: <60430e70d9783eb3b767e48be1f5ad78.squirrel@localhost> grocery_stocker wrote: > Okay, I was thinking more about this. I think this is also what is > irking me. Say I have the following.. > >>>> a = [1,2,3,4] >>>> for x in a: > ... print x > ... > 1 > 2 > 3 > 4 >>>> > > Would 'a' somehow call __iter__ and next()? If so, does python just > perform this magically? and the same is true when you use yield: >>> def count(): ... c = 0 ... while c < 4: ... yield c ... c += 1 ... >>> for i in count(): print i ... 0 1 2 3 >>> x = count() >>> dir(x) ['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', '__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'close', 'gi_frame', 'gi_running', 'next', 'send', 'throw'] >>> i = x.__iter__() >>> i.next() 0 >>> i.next() 1 andrew From rhodri at wildebst.demon.co.uk Thu Apr 2 21:33:23 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Fri, 03 Apr 2009 02:33:23 +0100 Subject: Iteratoration question In-Reply-To: References: <2a43b3a8-84d5-434e-a9c3-5f60ed6ba5b2@r5g2000prh.googlegroups.com> <02adc575560fa8733aee3c7f59668dac.squirrel@localhost> Message-ID: On Fri, 03 Apr 2009 02:07:38 +0100, grocery_stocker wrote: > Okay, I was thinking more about this. I think this is also what is > irking me. Say I have the following.. > >>>> a = [1,2,3,4] >>>> for x in a: > ... print x > ... > 1 > 2 > 3 > 4 >>>> > > Would 'a' somehow call __iter__ and next()? If so, does python just > perform this magically? No. It's "for" that invokes the iteration protocol; that's pretty much the definition of it. You have read the iteration protocol after it's been mentioned so many times now, haven't you? "for" calls iter(a), which in turn calls a.__iter__(), to get an iterator. Once it's got, "for" calls next() on the iterator each time round the loop. Very approximately, that little for-loop translates to: a = [1,2,3,4] i = iter(a) try: while True: x = i.next() print x except StopIteration: pass -- Rhodri James *-* Wildebeeste Herder to the Masses From andrew at acooke.org Thu Apr 2 21:38:52 2009 From: andrew at acooke.org (andrew cooke) Date: Thu, 2 Apr 2009 21:38:52 -0400 (CLT) Subject: Iteratoration question In-Reply-To: References: <2a43b3a8-84d5-434e-a9c3-5f60ed6ba5b2@r5g2000prh.googlegroups.com> <02adc575560fa8733aee3c7f59668dac.squirrel@localhost> Message-ID: Rhodri James wrote: > On Fri, 03 Apr 2009 02:07:38 +0100, grocery_stocker > wrote: >> Would 'a' somehow call __iter__ and next()? If so, does python just >> perform this magically? > > No. It's "for" that invokes the iteration protocol; that's pretty > much the definition of it. You have read the iteration protocol > after it's been mentioned so many times now, haven't you? argh. when i said "yes" i meant that something calls them - that they realy do exist behind the scenes. but rhodri is right (again), it's "for" that does the work. sorry. andrew From google at mrabarnett.plus.com Thu Apr 2 21:39:04 2009 From: google at mrabarnett.plus.com (MRAB) Date: Fri, 03 Apr 2009 02:39:04 +0100 Subject: Iteratoration question In-Reply-To: References: <2a43b3a8-84d5-434e-a9c3-5f60ed6ba5b2@r5g2000prh.googlegroups.com> <02adc575560fa8733aee3c7f59668dac.squirrel@localhost> Message-ID: <49D568B8.3080902@mrabarnett.plus.com> grocery_stocker wrote: > On Apr 2, 4:41 pm, "andrew cooke" wrote: >> Robert Kern wrote: >>>> replace return with yield and it might work. >>>> i have to go eat, but if it doesn't read the python docs on iterators - >>>> for examplehttp://docs.python.org/reference/expressions.html#index-1825 >>> No, .next() needs to be a regular function that returns a value. What he >>> needs >>> is an __iter__() method that returns self. Alternately, __iter__ could be >> yeah, sorry, i was in a rush and not thinking straight. andrew > > Okay, I was thinking more about this. I think this is also what is > irking me. Say I have the following.. > >>>> a = [1,2,3,4] >>>> for x in a: > ... print x > ... > 1 > 2 > 3 > 4 > > Would 'a' somehow call __iter__ and next()? If so, does python just > perform this magically? > The for loop calls the __iter__() method to get an iterator object and then calls the next() method of the iterator object repeatedly to get the values. Sometimes you want to give an existing iterator to the for loop; for that reason iterator objects (can) have an __iter__() method which just returns the object itself. For example: The 'list' class has the __iter__() method: >>> dir([]) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] The __iter__() method returns a 'listiterator' instance: >>> i = iter([]) >>> type(i) The 'listiterator' class also has the __iter__() method: >>> dir(i) ['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__length_hint__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'next'] which just returns itself: >>> iter(i) is i True From zac256 at gmail.com Thu Apr 2 21:40:08 2009 From: zac256 at gmail.com (Zac Burns) Date: Thu, 2 Apr 2009 18:40:08 -0700 Subject: python needs leaning stuff from other language In-Reply-To: References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <73kt5jFvitcdU1@mid.uni-berlin.de> <49D54209.5090306@hotmail.com> Message-ID: <333edbe80904021840t15cf8801p933b780242950b97@mail.gmail.com> Is it really worth it to not implement list.clear and answer this question over and over again? I see no reason that a list shouldn't have a .clear method. -- Zachary Burns (407)590-4814 Aim - Zac256FL Production Engineer (Digital Overlord) Zindagi Games On Thu, Apr 2, 2009 at 5:32 PM, Esmail wrote: > Emile van Sebille wrote: >> >> Esmail wrote: >>> >>> Diez B. Roggisch wrote: >>>> >>>> online.service at ymail.com schrieb: >>>>> >>>>> python's list needs a thing ?list.clear() ?like c# arraylist >>>>> and >>>> >>>> some_list[:] = [] >>> >>> I agree that this is nice and clear, but as a relative newbie >>> wouldn't >>> >>> some_list = [] >> >> This is different -- it creates a new list. ?Consider: >> >> ?>>> some_list = [1,2,3] >> ?>>> d = some_list >> ?>>> d[1] >> 2 >> ?>>> some_list[:] = ['a','b','c'] >> ?>>> d[1] >> 'b' >> ?>>> some_list = [1,2,3] >> ?>>> d[1] >> 'b' >> >> the [:] form allows references into the list to remain valid while the >> direct assignment dopes not. > > Ah .. thanks for clarifying this .. makes sense. > > Also, thank you Luis for your post. > > Esmail > > -- > http://mail.python.org/mailman/listinfo/python-list > From tjreedy at udel.edu Thu Apr 2 21:40:23 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 02 Apr 2009 21:40:23 -0400 Subject: python needs leaning stuff from other language In-Reply-To: <73kt5jFvitcdU1@mid.uni-berlin.de> References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <73kt5jFvitcdU1@mid.uni-berlin.de> Message-ID: Diez B. Roggisch wrote: > online.service at ymail.com schrieb: >> python's list needs a thing list.clear() like c# arraylist >> and > > some_list[:] = [] Or del some_list[:] From google at mrabarnett.plus.com Thu Apr 2 21:53:32 2009 From: google at mrabarnett.plus.com (MRAB) Date: Fri, 03 Apr 2009 02:53:32 +0100 Subject: python needs leaning stuff from other language In-Reply-To: <333edbe80904021840t15cf8801p933b780242950b97@mail.gmail.com> References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <73kt5jFvitcdU1@mid.uni-berlin.de> <49D54209.5090306@hotmail.com> <333edbe80904021840t15cf8801p933b780242950b97@mail.gmail.com> Message-ID: <49D56C1C.3070203@mrabarnett.plus.com> Zac Burns wrote: > Is it really worth it to not implement list.clear and answer this > question over and over again? > > I see no reason that a list shouldn't have a .clear method. > Does dict have a .clear method? Yes. Does set have a .clear method? Yes. Does list have a .clear method? No. Of course, with a list you can do del my_list[:]; there's no simple equivalent for dict or set. But, overall, I'm +1. From david.birdsong at gmail.com Thu Apr 2 22:07:26 2009 From: david.birdsong at gmail.com (birdsong) Date: Thu, 2 Apr 2009 19:07:26 -0700 (PDT) Subject: interacting with an updatedb generated data file within python Message-ID: Does anybody have any recommendations on how to interact with the data file that updatedb generates? I'm running through a file list in sqlite that I want to check against the file system. updatedb is pretty optimized for building an index and storing it, but I see no way to query the db file other than calling locate itself. This would require me to fork and exec for every single file I want to verify - I'd be better off doing the stat myself in that case, but I'd really rather let updatedb build the index for me. I searched high and low for any sort of library that is well suited for reading these data files, but I've found nothing for any language other than the source for locate and updatedb itself. From cdalten at gmail.com Thu Apr 2 22:19:07 2009 From: cdalten at gmail.com (grocery_stocker) Date: Thu, 2 Apr 2009 19:19:07 -0700 (PDT) Subject: Iteratoration question References: <2a43b3a8-84d5-434e-a9c3-5f60ed6ba5b2@r5g2000prh.googlegroups.com> <02adc575560fa8733aee3c7f59668dac.squirrel@localhost> Message-ID: <67d3b45e-0f18-4f7f-afc6-4a51dff83730@c18g2000prh.googlegroups.com> On Apr 2, 6:33 pm, "Rhodri James" wrote: > On Fri, 03 Apr 2009 02:07:38 +0100, grocery_stocker > wrote: > > > Okay, I was thinking more about this. I think this is also what is > > irking me. Say I have the following.. > > >>>> a = [1,2,3,4] > >>>> for x in a: > > ... print x > > ... > > 1 > > 2 > > 3 > > 4 > > > Would 'a' somehow call __iter__ and next()? If so, does python just > > perform this magically? > > No. It's "for" that invokes the iteration protocol; that's pretty > much the definition of it. You have read the iteration protocol > after it's been mentioned so many times now, haven't you? > > "for" calls iter(a), which in turn calls a.__iter__(), to get an > iterator. Once it's got, "for" calls next() on the iterator each > time round the loop. Very approximately, that little for-loop > translates to: > > a = [1,2,3,4] > i = iter(a) > try: > while True: > x = i.next() > print x > except StopIteration: > pass > Okay, at the risk of sounding like a total slacker, I've only briefly scanned over the iteration protocol. After I get up in the morning, I'll probably read it more closely. From steve at REMOVE-THIS-cybersource.com.au Thu Apr 2 22:23:39 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Apr 2009 02:23:39 GMT Subject: Tab completion References: <01e4c71e$0$20654$c3e8da3@news.astraweb.com> <87prfuhggc.fsf@benfinney.id.au> Message-ID: <01e56775$0$20654$c3e8da3@news.astraweb.com> On Fri, 03 Apr 2009 08:51:47 +1100, Ben Finney wrote: > Steven D'Aprano writes: > >> I like tab-completion, but I'd rather not be reduced to typing spaces >> for indents in the interpreter. What do other people do? > > Acknowledge that using U+0009 for indentation is wrong, and use > beautiful U+0020 always. Perhaps you missed the "in the interpreter". I'm *not* talking about what happens when I hit TAB in my editor, I'm talking about hitting TAB in an interactive Python session in a console window. I expect TAB to result in an indent level, and backspace to remove that indent level. Tab- completion clashes with that. When defining a function in the interactive interpreter: >>> def spam(): ... return "spam" ... >>> I do not wish to type four literal spaces to indent the 'return' line, or backspace four times to remove it, but with tab-completion I am forced to. I'm used to pressing the TAB key once to get an indent. Within the interpreter, I do not care whether it inserts a tab character or four spaces or seven formfeed characters, so long as the Python parser recognises it as a single indent level and typing backspace once removes that indent level. I'm sorry if this was unclear from my post. >> The GNU readline library claims that M-tab (which I guess is Alt-tab) >> will enter a tab character. > > ?M-tab? is Emacs-speak for ? plus ?. Not all keyboards have > ?Alt?, you see (maybe none, when Emacs first started referring to the > Meta modifier?). But on my keyboard, I have no Meta key, I have an Alt key. > In most terminal programs, you can send ?-? also by pressing > ?, ? (two separate keystrokes in sequence). > >> Any other suggestions or hints? > > Suck it up and accept the truth! U+0020 is the unambiguous indentation > character that always does what it should, and U+0009 is a horrible > mistake which must be suppressed with extreme prejudice. No true > Pythonista would disagree. The "No true Scotsman" fallacy. But regardless of what I use in my source code, I'm purely talking about interactive sessions in a console, not an editor. -- Steven From rhodri at wildebst.demon.co.uk Thu Apr 2 22:27:05 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Fri, 03 Apr 2009 03:27:05 +0100 Subject: python needs leaning stuff from other language In-Reply-To: <333edbe80904021840t15cf8801p933b780242950b97@mail.gmail.com> References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <73kt5jFvitcdU1@mid.uni-berlin.de> <49D54209.5090306@hotmail.com> <333edbe80904021840t15cf8801p933b780242950b97@mail.gmail.com> Message-ID: On Fri, 03 Apr 2009 02:40:08 +0100, Zac Burns wrote: > Is it really worth it to not implement list.clear and answer this > question over and over again? For some value of "over and over again" that allows this to be the first time I've seen it several months of reading the newsgroup. > I see no reason that a list shouldn't have a .clear method. This I wouldn't disagree with, though I imagine it will increase the frequency of complaints from people who haven't understood Python's assignment model. -- Rhodri James *-* Wildebeeste Herder to the Masses From beau at open-source-staffing.com Thu Apr 2 22:35:07 2009 From: beau at open-source-staffing.com (OSS) Date: Thu, 2 Apr 2009 19:35:07 -0700 (PDT) Subject: [JOB] Sr. Python Developer, NYC | 120-130k Message-ID: <3f2595bb-ae22-47b4-860b-61347261e712@o6g2000yql.googlegroups.com> I'm working with another recruiter on this and his client needs a Python Developer...someone who is a real expert. The position is in Manhattan and they need someone ASAP. We are dealing with the manager directly. Base salary is 120,000 - 130,000 not including bonus. Please submit your resume and salary requirements to beau at open- source-staffing.com Thank you, Beau J. Gould Open Source Staffing www.open-source-staffing.com beau at open-source-staffing.com http://groups.yahoo.com/group/pythonzopejobs From steve at REMOVE-THIS-cybersource.com.au Thu Apr 2 22:35:18 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Apr 2009 02:35:18 GMT Subject: python needs leaning stuff from other language References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <73kt5jFvitcdU1@mid.uni-berlin.de> <49D54209.5090306@hotmail.com> Message-ID: <01e56a2f$0$20654$c3e8da3@news.astraweb.com> On Thu, 02 Apr 2009 18:40:08 -0700, Zac Burns wrote: > Is it really worth it to not implement list.clear and answer this > question over and over again? > > I see no reason that a list shouldn't have a .clear method. The usual answer to that is that there's already two ways of clearing a list: del alist[:] alist[:] = [] and we don't need a third way. Dicts and sets need a clear() method, because there's no equivalent to slicing. I still think that alist.clear() would be a fine addition that matches my intuition and aesthetic sense. Alas, I'm apparently not Dutch. BTW Zac, on this list we don't appreciate top-posting, and we encourage people to trim the quoted replies. -- Steven From kay.schluehr at gmx.net Thu Apr 2 22:36:14 2009 From: kay.schluehr at gmx.net (Kay Schluehr) Date: Thu, 2 Apr 2009 19:36:14 -0700 (PDT) Subject: Python Goes Mercurial References: <35d429fa-5d13-4703-a443-6a95c740c9fb@o6g2000yql.googlegroups.com> <4dad5e13-153f-446d-9f2b-06ebc653e003@c9g2000yqm.googlegroups.com> <17ab8b08-73cf-42ea-ba83-a8d6d8b9281b@c9g2000yqm.googlegroups.com> Message-ID: > Meh. Use the command line like God intended. I'm sorry to say this Rhodri but there is probably no god ;) The reason I like overlays is that they are data displays that highlight changes without letting me do any action. The VCS works for me before I'm doing any work with it and that's a good thing because I'm *always* lazy. From ben+python at benfinney.id.au Thu Apr 2 22:41:50 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 03 Apr 2009 13:41:50 +1100 Subject: Tab completion References: <01e4c71e$0$20654$c3e8da3@news.astraweb.com> <87prfuhggc.fsf@benfinney.id.au> <01e56775$0$20654$c3e8da3@news.astraweb.com> Message-ID: <873acqfogh.fsf@benfinney.id.au> Steven D'Aprano writes: > I do not wish to type four literal spaces to indent the 'return' > line, or backspace four times to remove it, but with tab-completion > I am forced to. I'm used to pressing the TAB key once to get an > indent. Within the interpreter, I do not care whether it inserts a > tab character or four spaces or seven formfeed characters, so long > as the Python parser recognises it as a single indent level and > typing backspace once removes that indent level. It sounds like, instead of pressing once, you can press once and meet your requirements. > On Fri, 03 Apr 2009 08:51:47 +1100, Ben Finney wrote: > > ?M-tab? is Emacs-speak for ? plus ?. Not all > > keyboards have ?Alt?, you see (maybe none, when Emacs first > > started referring to the Meta modifier?). > > But on my keyboard, I have no Meta key, I have an Alt key. AFAIK, no keyboard has a ?Meta? key. The term was chosen AFAICT to refer to ?the modifier key that's conceptually the next level up from ?. For you and I and just about everyone whose keyboard design has been influenced by the IBM Personal Computer, that's the key. But at the point that was introduced, it was merely one keyboard layout among many, and by the point it *was* de facto standard, Emacs keyboard terminology was long established. > > Suck it up and accept the truth! U+0020 is the unambiguous > > indentation character that always does what it should, and U+0009 > > is a horrible mistake which must be suppressed with extreme > > prejudice. No true Pythonista would disagree. > > The "No true Scotsman" fallacy. No true Pythonista would point that out. -- \ ?[T]he speed of response of the internet will re-introduce us | `\ to that from which our political systems have separated us for | _o__) so long, the consequences of our own actions.? ?Douglas Adams | Ben Finney From ben+python at benfinney.id.au Thu Apr 2 22:52:20 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 03 Apr 2009 13:52:20 +1100 Subject: python needs leaning stuff from other language References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <73kt5jFvitcdU1@mid.uni-berlin.de> <49D54209.5090306@hotmail.com> <01e56a2f$0$20654$c3e8da3@news.astraweb.com> Message-ID: <87y6uie9ej.fsf@benfinney.id.au> Steven D'Aprano writes: > The usual answer to that is that there's already two ways of clearing a > list: > > del alist[:] > alist[:] = [] > > and we don't need a third way. Dicts and sets need a clear() method, > because there's no equivalent to slicing. > > I still think that alist.clear() would be a fine addition that > matches my intuition and aesthetic sense. Alas, I'm apparently not > Dutch. I think it would also be better to have One (and prefereably Only One) Obvious Way To Do It. That obvious way, for those who work with Python's ?set? and ?dict?, is a ?clear? method. It seems best to have ?list? conform with this also. > BTW Zac, on this list we don't appreciate top-posting, and we > encourage people to trim the quoted replies. -- \ ?Generally speaking, the errors in religion are dangerous; | `\ those in philosophy only ridiculous.? ?David Hume, _A Treatise | _o__) of Human Nature_, 1739 | Ben Finney From steve at REMOVE-THIS-cybersource.com.au Thu Apr 2 22:58:49 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Apr 2009 02:58:49 GMT Subject: Tab completion References: <01e4c71e$0$20654$c3e8da3@news.astraweb.com> <87prfuhggc.fsf@benfinney.id.au> <01e56775$0$20654$c3e8da3@news.astraweb.com> <873acqfogh.fsf@benfinney.id.au> Message-ID: <01e56fb2$0$20654$c3e8da3@news.astraweb.com> On Fri, 03 Apr 2009 13:41:50 +1100, Ben Finney wrote: > Steven D'Aprano writes: > >> I do not wish to type four literal spaces to indent the 'return' line, >> or backspace four times to remove it, but with tab-completion I am >> forced to. I'm used to pressing the TAB key once to get an indent. >> Within the interpreter, I do not care whether it inserts a tab >> character or four spaces or seven formfeed characters, so long as the >> Python parser recognises it as a single indent level and typing >> backspace once removes that indent level. > > It sounds like, instead of pressing once, you can press > once and meet your requirements. Yuck! Well, okay, technically that would work, but it's seriously disturbing to set your indent level to one space. If there's a more elegant solution that gives four-space indents I'd prefer that. Thanks to all for your suggestions. -- Steven From aahz at pythoncraft.com Thu Apr 2 23:02:42 2009 From: aahz at pythoncraft.com (Aahz) Date: 2 Apr 2009 20:02:42 -0700 Subject: Puzzling: local variable in recursive function made global? References: Message-ID: In article , andrew cooke wrote: > >sorry for the shouting, but someone asks this EVERY DAY AND I CAN'T TAKE >ANY MORE. Nobody's forcing you to respond. Nobody's forcing you to top-post, either. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." --Brian W. Kernighan From pje at telecommunity.com Thu Apr 2 23:12:18 2009 From: pje at telecommunity.com (P.J. Eby) Date: Thu, 02 Apr 2009 23:12:18 -0400 Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: <49D56486.8020708@ubuntu.com> References: <49D4DA72.60401@v.loewis.de> <49D56486.8020708@ubuntu.com> Message-ID: <20090403030953.32A493A40A7@sparrow.telecommunity.com> At 03:21 AM 4/3/2009 +0200, Matthias Klose wrote: >+1 speaking as a downstream packaging python for Debian/Ubuntu I >welcome this approach. The current practice of shipping the very >same file (__init__.py) in different packages leads to conflicts for >the installation of these packages (this is not specific to dpkg, >but is true for rpm packaging as well). Current practice of >packaging (for downstreams) so called "name space packages" is: - >either to split out the namespace __init__.py into a >separate (linux distribution) package (needing manual packaging >effort for each name space package) - using downstream specific >packaging techniques to handle conflicting files (diversions) - >replicating the current behaviour of setuptools simply overwriting >the file conflicts. Following this proposal (downstream) >packaging of namespace packages is made possible independent of any >manual downstream packaging decisions or any downstream specific >packaging decisions A clarification: setuptools does not currently install the __init__.py file when installing in --single-version-externally-managed or --root mode. Instead, it uses a project-version-nspkg.pth file that essentially simulates a variation of Martin's .pkg proposal, by abusing .pth file support. If this PEP is adopted, setuptools would replace its nspkg.pth file with a .pkg file on Python versions that provide native support for .pkg imports, keeping the .pth file only for older Pythons. (.egg files and directories will not be affected by the change, unless the zipimport module will also supports .pkg files... and again, only for Python versions that support the new approach.) From castironpi at gmail.com Thu Apr 2 23:21:47 2009 From: castironpi at gmail.com (Aaron Brady) Date: Thu, 2 Apr 2009 20:21:47 -0700 (PDT) Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> Message-ID: <5c92e9bd-1fb4-4c01-a928-04d7f673329a@e21g2000yqb.googlegroups.com> On Apr 2, 6:34?pm, Tim Wintle wrote: > On Thu, 2009-04-02 at 15:16 -0700, Emile van Sebille wrote: > > Lou Pecora wrote: > > > Confusion only comes when you try to force the > > > defintion of one of them on the other and then say it's illogical or not > > > natural. ?Both are natural. > > > Consider the French 'Premiere etage' vs the American 'First Floor' > > or even in the same language - "first floor" in English (UK) is very > different from "first floor" in English (US). Did I tell you guys that 'natural' has 38 definitions at dictionary.com? From gagsl-py2 at yahoo.com.ar Thu Apr 2 23:29:46 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 03 Apr 2009 00:29:46 -0300 Subject: py2exe problem References: Message-ID: En Thu, 02 Apr 2009 08:06:22 -0300, Wolfgang Forstmeier escribi?: > On 02.04.2009 11:34, Gabriel Genellina wrote: >> En Wed, 01 Apr 2009 17:51:52 -0300, Wolfgang Forstmeier >> escribi?: >> >>> what kind of error do I have with getting this error at starting my >>> app. >>> Im am not using IdleConf.GetOption right now. >>> >>> Warning: configHandler.py - IdleConf.GetOption - >>> problem retrieving configration option 'name' >>> from section 'Keys'. >>> returning default value: '' >> >> but do you use idle or idlelib in your application? >> > after some research in my code I found the following import > > from idlelib.OutputWindow import tkMessageBox > > Maybe someone could assist me with fixing that issue, > because I am really new to python and does not exactly know what to do > now. Ok, but do you really use idlelib for something? Or it's just some random code you found somewhere and drop into your application? Also, note that you got just a warning, not an error; chances are your program may still work. BTW, what's your program for? -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Thu Apr 2 23:29:46 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 03 Apr 2009 00:29:46 -0300 Subject: py2exe problem References: Message-ID: En Thu, 02 Apr 2009 08:06:22 -0300, Wolfgang Forstmeier escribi?: > On 02.04.2009 11:34, Gabriel Genellina wrote: >> En Wed, 01 Apr 2009 17:51:52 -0300, Wolfgang Forstmeier >> escribi?: >> >>> what kind of error do I have with getting this error at starting my >>> app. >>> Im am not using IdleConf.GetOption right now. >>> >>> Warning: configHandler.py - IdleConf.GetOption - >>> problem retrieving configration option 'name' >>> from section 'Keys'. >>> returning default value: '' >> >> but do you use idle or idlelib in your application? >> > after some research in my code I found the following import > > from idlelib.OutputWindow import tkMessageBox > > Maybe someone could assist me with fixing that issue, > because I am really new to python and does not exactly know what to do > now. Ok, but do you really use idlelib for something? Or it's just some random code you found somewhere and drop into your application? Also, note that you got just a warning, not an error; chances are your program may still work. BTW, what's your program for? -- Gabriel Genellina From castironpi at gmail.com Thu Apr 2 23:31:36 2009 From: castironpi at gmail.com (Aaron Brady) Date: Thu, 2 Apr 2009 20:31:36 -0700 (PDT) Subject: iterator question References: <2a43b3a8-84d5-434e-a9c3-5f60ed6ba5b2@r5g2000prh.googlegroups.com> <96cb00ed-8941-429b-a428-c9f451a44182@v23g2000pro.googlegroups.com> Message-ID: <323cbc39-9bf5-4a25-b5a5-36e31d3e8451@o6g2000yql.googlegroups.com> On Apr 2, 5:17?pm, grocery_stocker wrote: > On Apr 2, 3:14 pm, grocery_stocker wrote: > > Discussion subject changed to "iterator question" by grocery_stocker Well, I thought it was funny. 'iteratoration'. Next, conversateration? From steve at REMOVE-THIS-cybersource.com.au Fri Apr 3 00:23:42 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Apr 2009 04:23:42 GMT Subject: A design problem I met again and again. References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> Message-ID: <01e58397$0$20654$c3e8da3@news.astraweb.com> On Thu, 02 Apr 2009 16:51:24 -0700, Emile van Sebille wrote: > ??? wrote: >> Hi all, >> >> I am a programmer who works with some different kinds of programming >> languages, like python, C++(in COM), action script, C#, etc. >> >> Today, I realized that, what ever language I use, I always meet a same >> problem and I think I never solve it very well. >> >> The problem is : how to break my app into functional pieces? > > My question would be why? Refactoring adds nothing to a functioning app > but clarity and maintainability -- both admirable qualities, granted, > and both unnecessary until needed. But they're always needed, except possibly for use-once throw-away scripts. > When I need to update an app is when > I start refactoring, and then just those areas that need it. Certainly > I refactor constantly during development Well, that pretty much disproves your assertion that refactoring is only needed when updating an application. > to avoid code reuse through > cut-n-paste, but once I've got it going, whether it's 1000 or 6000 > lines, it doesn't matter as long as it works. If you've been refactoring during development, and gotten to the point where it is working, clear and maintainable, then there's very little refactoring left to do. I don't think anyone suggests that you refactor code that doesn't need refactoring. Once it is already split into functional pieces, there's no need to continue breaking it up further. -- Steven From emile at fenx.com Fri Apr 3 01:18:02 2009 From: emile at fenx.com (Emile van Sebille) Date: Thu, 02 Apr 2009 22:18:02 -0700 Subject: A design problem I met again and again. In-Reply-To: <01e58397$0$20654$c3e8da3@news.astraweb.com> References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> <01e58397$0$20654$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Thu, 02 Apr 2009 16:51:24 -0700, Emile van Sebille wrote: >> I refactor constantly during development to avoid code reuse through >> cut-n-paste, but once I've got it going, whether it's 1000 or 6000 >> lines, it doesn't matter as long as it works. > > If you've been refactoring during development, and gotten to the point > where it is working, yes, but > clear and maintainable, not necessarily > then there's very little refactoring left to do. Again, not necessarily. I often find it easier to refactor old code when I'm maintaining it to better understand how to best implement the change I'm incorporating at the moment. The refactoring certainly may have been done when the code was originally written, but at that time refactoring would have only served to pretty it up as it already worked. > I don't think anyone suggests that you refactor > code that doesn't need refactoring. That's exactly what I read the OP as wanting to do. That's why I was asking why. So, I think the question becomes, when does code need refactoring? Emile From ISF (Computer Scientists without Frontiers, Fri Apr 3 01:50:15 2009 From: ISF (Computer Scientists without Frontiers, (ISF (Computer Scientists without Frontiers,) Date: Thu, 2 Apr 2009 22:50:15 -0700 (PDT) Subject: Sending SMS using python script References: <2005e94f-94de-4566-be60-7245efa4271d@y6g2000prf.googlegroups.com> Message-ID: On 2 Apr, 06:41, guptha wrote: > hi group, > my application needs to sendSMSoccasionally to all the clients ?.Is > there any library in python that supports in sendingSMS. > I like to conform few information i gathered in this regard. > > I can sendSMSby two ways > > 1. SendingSMSusing Email clients > 2. Usingsmsgateway to send message(we can implementSMSGateway API > 's ,provided by vendor and ,sendSMS-- we will be charged > accordingly ) > > In case of First approach > 1. We can make use of libgamil library to sendSMSusing gmail ( I > ref :http://blog.datasingularity.com/?p=63) > i suppose sendingsmsthrough gmail is not supported in India > ?2. Can we use Skype4py library, > > In case of second approach > 1. Is there any way to sendSMSfor free inside India ,or ,Any freeSMSgateway providers in India > Any information regarding this is appreciable > ? Thanks A friend from India sent me this hint: the following link can be explored further for sending SMS on pyS60 (python for symbian OS) http://mobilenin.com/pys60/menu.htm wkr, Aldo From michele.simionato at gmail.com Fri Apr 3 01:55:14 2009 From: michele.simionato at gmail.com (Michele Simionato) Date: Thu, 2 Apr 2009 22:55:14 -0700 (PDT) Subject: A design problem I met again and again. References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> <01e58397$0$20654$c3e8da3@news.astraweb.com> Message-ID: <9e788ffa-5d23-4175-89bd-b63b30e36632@q9g2000yqc.googlegroups.com> On Apr 3, 7:18?am, Emile van Sebille wrote: > ?So, I think the question becomes, when does code need > refactoring? I would say that 99.9% of the times a single class with 15,000 lines of code is a signal that something is wrong, and refactoring is needed. M. Simionato From stefan_ml at behnel.de Fri Apr 3 02:00:07 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Fri, 03 Apr 2009 08:00:07 +0200 Subject: xml to xhtml In-Reply-To: References: Message-ID: <49d5a5e7$0$32672$9b4e6d93@newsspool2.arcor-online.net> jud779 at gmail.com wrote: > On Apr 1, 11:16 am, Joe Riopel wrote: >> On Wed, Apr 1, 2009 at 10:43 AM, wrote: >>> If anyone can give me some guidance what should be the best way to >>> generate html/xhtml page using python would be great. I am open to >>> other options like xsl or anything else that can make things simple. >> Since you're open to other options, I would take a look at XSLT. >> >> http://www.w3schools.com/xsl/ > > XSLT sounds good. I have already created temporary XSL and its working > fine. Still question is how to put up this on web server index.html. > I am planning to create one index.html and call cgi python which in > return produces html page from xml/xsl. Does this sounds like a good > plan?? > Is there any other options than the one i describe above. If, then pls > help me out. There are tons of web servers and web frameworks that support Python. http://wiki.python.org/moin/WebProgramming http://wiki.python.org/moin/WebFrameworks Using CGI is a particularly bad idea, as it is very resource consuming to start up a new Python interpreter for each request. Most web servers also support Fast-CGI, which basically keeps the interpreter running. Here are some other ways to do similar things: http://wiki.python.org/moin/WebServers You might also want to read about WSGI, which is the Python standard for web server interfaces. Most web frameworks support it, so this keeps your application independent of a particular web server (and testable even without a web server). If you want to do XSLT in Python, lxml is a very good choice: http://codespeak.net/lxml/ Stefan From newptcai at gmail.com Fri Apr 3 02:25:42 2009 From: newptcai at gmail.com (=?GB2312?B?0rvK18qr?=) Date: Thu, 2 Apr 2009 23:25:42 -0700 (PDT) Subject: A design problem I met again and again. References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> <33e08fde-1d4e-41a2-9ce2-7e1bec3abd90@r29g2000vbp.googlegroups.com> <793a9568-7c89-412e-b14c-e2dd28640577@s22g2000prg.googlegroups.com> <1b60f999-aaf8-4b10-bd1f-ab46f03f44f6@m36g2000vbp.googlegroups.com> Message-ID: Consolidate existing functions? I've thought about it. For example, I have two functions: #========================= def startXXX(id): pass def startYYY(id): pass #========================= I could turn it into one: #========================= def start(type, id): if(type == "XXX"): pass else if(type == "YYY"): pass #========================= But isn't the first style more clear for my code's user? That's one reason why my interfaces grow fast. On Apr 3, 1:51 am, Carl Banks wrote: > On Apr 2, 8:02 am, ??? wrote: > > > You get it. Sometimes I feel that my head is trained to work in a > > procedural way. I use a big class just as a container of functions. > > > About the "data-based" approach, what if these functions all shares a > > little data, e.g. a socket, but nothing else? > > Then perhaps your problem is that you are too loose with the > interface. Do you write new functions that are very similar to > existing functions all the time? Perhaps you should consolidate, or > think about how existing functions could do the job. > > Or perhaps you don't have a problem. There's nothing wrong with large > classes per se, it's just a red flag. If you have all these functions > that really all operate on only one piece of data, and really all do > different things, then a large class is fine. > > Carl Banks From Lie.1296 at gmail.com Fri Apr 3 02:46:57 2009 From: Lie.1296 at gmail.com (Lie) Date: Thu, 2 Apr 2009 23:46:57 -0700 (PDT) Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <72i5t4tgfo2h4gd6ggcs02flkca85kgoku@4ax.com> <0dbc9665-3ee8-4d62-8e9e-75c6d700076e@f1g2000prb.googlegroups.com> Message-ID: <6e1e8c53-2b3e-4ade-b52e-507c53721c98@v1g2000prd.googlegroups.com> On Apr 2, 5:29?pm, Steven D'Aprano wrote: > On Wed, 01 Apr 2009 21:58:47 -0700, Lie wrote: > > On Apr 1, 7:06?pm, Steven D'Aprano > > wrote: > > >> There is a major clash between the names of ordinals in human languages > >> and zero-based counting. In human languages, the Nth-ordinal item comes > >> in position N. You can keep that useful convention with zero-based > >> counting by inventing the ugly word "zeroth", but that just leads to > >> bizarro-talk like "the zeroeth item comes first, the first item comes > >> second, and so on". > > > No, there won't be any bizarro-talk. There is no argument: the zeroeth > > item comes zeroeth, the first item comes first, and so on. The index for > > the very zeroeth thing in a list is 0, so to get the zeroeth item you > > use s[0]. While to get the first item you use s[1]. It's very intuitive, > > isn't it? > > No, because "first", "second", "third" etc. have existed in the English > language for hundreds of years and everybody knows them. "Zeroeth" was > probably invented a few decades ago, and is known by maybe 1% of the > English-speaking population. > > Given the list [a, b, c], if you ask even a C programmer *in English* > "what's the first item?", they will almost invariably answer a rather > than b. > > -- > Steven Alternatively: "One friend of mine half-seriously advanced the following thesis: We should count from zero. But "first" is, etymologically, a diminution of "foremost", and (as TomStambaugh says) should mean "0th" when we count from 0. And "second" is from the Latin "secundus", meaning "following", so it should mean "1th" when we count from 0. Obviously the ordinals from "third" onwards get their names from the numbers. So... we need a new word for "2th". He proposed "twifth". Thus: first, second, twifth, third, fourth, ..." -- GarethMcCaughan (from http://c2.com/cgi/wiki?ZeroAndOneBasedIndexes ) No zeroeth, "twifth" From mailing at supai.de Fri Apr 3 02:55:20 2009 From: mailing at supai.de (Wolfgang Forstmeier) Date: Fri, 03 Apr 2009 08:55:20 +0200 Subject: py2exe problem In-Reply-To: References: Message-ID: On 03.04.2009 05:29, Gabriel Genellina wrote: > En Thu, 02 Apr 2009 08:06:22 -0300, Wolfgang Forstmeier > escribi?: >> On 02.04.2009 11:34, Gabriel Genellina wrote: >>> En Wed, 01 Apr 2009 17:51:52 -0300, Wolfgang Forstmeier >>> escribi?: >>> >>>> what kind of error do I have with getting this error at starting my >>>> app. >>>> Im am not using IdleConf.GetOption right now. >>>> >>>> Warning: configHandler.py - IdleConf.GetOption - >>>> problem retrieving configration option 'name' >>>> from section 'Keys'. >>>> returning default value: '' >>> > > Ok, but do you really use idlelib for something? Or it's just some > random code you found somewhere and drop into your application? Ah yes, I really use this. I create some message boxes for a little GUI application that controls some other program with COM. Running my app without py2exe, just with python, there is no warning at all. This comes in with py2exe first. Here some piece of code that I use for tkMessageBox. from idlelib.OutputWindow import tkMessageBox ... # Define about message box def about(self): tkMessageBox.showinfo("About", "My little about text box.") # -- ... There is some more GUI programming arround that def in my class, but that uses only Tkinter, should not be interesting for that error. > Also, note that you got just a warning, not an error; chances are your > program may still work. That is right, but there is an annoying message box at closing my program that tells me the error above. BTW, what's your program for? Connecting a MFC COM application with an old FORTRAN program. From 42flicks at gmail.com Fri Apr 3 03:04:04 2009 From: 42flicks at gmail.com (Mike) Date: Fri, 3 Apr 2009 20:04:04 +1300 Subject: HTML Generation Message-ID: <2b54d4370904030004p4b9261dfjdf809bf861fb517e@mail.gmail.com> Hello all, I'm writing a web app and wanted to do some html generation (I really do not like to maintain or write html). I'm thinking of writing a dsl based on the following: def html(): return def a(): return def body(): return (html, ...(head, (style, "id", {"font-color":"black"})) ...(body, ...... (a, "id", {"title":"bla"}, ("link title","http://www.link.com")), ......(p, "paragraph text") ...... ) ...) ) And then parsing the resulting list. (the specification of sorts is as far as I've got) I'd have to create the list as a string and then call a method which executes the contents as the methods would not be in the local scope. The reason I'm posting is I'm curious what people think, I've been starting to perfer this style and am curious if a similar solution exists or if this is a good idea :) I'll proberly finish it as a learning experience regardless. I appreciate any responses :) -------------- next part -------------- An HTML attachment was scrubbed... URL: From ahqylanglei at gmail.com Fri Apr 3 03:05:52 2009 From: ahqylanglei at gmail.com (Linuxwell) Date: Fri, 3 Apr 2009 00:05:52 -0700 (PDT) Subject: "Pythoner",Wish me luck! Message-ID: <54afb932-1a42-42d2-a3b9-0151b2bd410b@q30g2000prq.googlegroups.com> Starting today I would like to study Python,Wish me luck! From tadwelessar at gmail.com Fri Apr 3 03:21:19 2009 From: tadwelessar at gmail.com (Matteo) Date: Fri, 3 Apr 2009 00:21:19 -0700 (PDT) Subject: "Pythoner",Wish me luck! References: <54afb932-1a42-42d2-a3b9-0151b2bd410b@q30g2000prq.googlegroups.com> Message-ID: On Apr 3, 9:05?am, Linuxwell wrote: > Starting today I would like to study Python,Wish me luck! Good luck! Don't forget to... >>> print 'Hello World!' ;) From sjmachin at lexicon.net Fri Apr 3 03:25:03 2009 From: sjmachin at lexicon.net (John Machin) Date: Fri, 3 Apr 2009 00:25:03 -0700 (PDT) Subject: interacting with an updatedb generated data file within python References: Message-ID: <24247ea1-1d7b-4ab9-afcd-308a65b0b28d@z23g2000prd.googlegroups.com> On Apr 3, 1:07?pm, birdsong wrote: > Does anybody have any recommendations on how to interact with the data > file that updatedb generates? ?I'm running through a file list in > sqlite that I want to check against the file system. updatedb is > pretty optimized for building an index and storing it, but I see no > way to query the db file other than calling locate itself. ?This would > require me to fork and exec for every single file I want to verify - > I'd be better off doing the stat myself in that case, but I'd really > rather let updatedb build the index for me. > > I searched high and low for any sort of library that is well suited > for reading these data files, but I've found nothing for any language > other than the source for locate and updatedb itself. Disclaimer: I had to google to find out what "updatedb" is so don't take me as any authority on this :-) The format appears to be documented e.g. http://www.delorie.com/gnu/docs/findutils/locatedb.5.html and thus should be found on the locatedb(5) man page on your system. Assuming that you don't have the old version, it should take about 20 lines of Python to loop around extracting the file names, plus some more to open the file, read it in as one big string (how big is it?), and check the dummy "LOCATE02" entry up the front -- it's a bit hard to be sure how the prefix length of the first non-dummy entry is determined without seeing an actual example, but my guess is that the file will start like this: "\x00LOCATE02\x00\xF8name-of-first-file-in-full\x00........." where the "\xF8" is -8 meaning ignore the 8-character previous name "LOCATE02" i.e. previous name can be regarded as "". Anyway, I reckon utter max 50 lines of Python to produce a module with a generator that yields one file name at a time, or a function returning a list or a set. HTH ... feel free to ask more if the above is a little obscure. But do accompany any questions with the result of doing this: print repr(open('the_locatedb_file').read(400)) plus what you believe the full name of the first non-dummy file should be. Cheers, John From tino at living-examples.com Fri Apr 3 04:04:33 2009 From: tino at living-examples.com (Tino Wildenhain) Date: Fri, 03 Apr 2009 10:04:33 +0200 Subject: HTML Generation In-Reply-To: <2b54d4370904030004p4b9261dfjdf809bf861fb517e@mail.gmail.com> References: <2b54d4370904030004p4b9261dfjdf809bf861fb517e@mail.gmail.com> Message-ID: <49D5C311.7020504@living-examples.com> Hi Mike, Mike wrote: > Hello all, > > I'm writing a web app and wanted to do some html generation (I really do > not like to maintain or write html). > > I'm thinking of writing a dsl based on the following: > > def html(): > return > > def a(): > return > > def body(): > return That would be writing HTML just another way and also mixing code and representation, this is generally not a good idea. If you really don't want to maintain HTML along with your code, I'd suggest an approach like Zope Page Templates http://en.wikipedia.org/wiki/Zope_Page_Templates#Zope_Page_Templates which can be used outside Zope as well, (even with many other languages) For example: http://zpt.sourceforge.net/ Regards Tino From deets at nospam.web.de Fri Apr 3 04:11:32 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 03 Apr 2009 10:11:32 +0200 Subject: Iteratoration question In-Reply-To: References: <2a43b3a8-84d5-434e-a9c3-5f60ed6ba5b2@r5g2000prh.googlegroups.com> Message-ID: <73lulkFvfi5dU1@mid.uni-berlin.de> > > while what you are doing is interesting, it is not the same as Python's > iterators, which use "yield" from a function and don't require storing a > value in a class. look for "yield" in the python docs. this comment may > be irrelevant; i am just worried you are confusing the above (which apart > from the mistake about instances is perfectly ok) and python's iterators > (which use next(), yield, etc). You are confusing generators with the iterator-protocol. Iteration has been part of python long before generators appeared on the scene. Diez From sjmachin at lexicon.net Fri Apr 3 04:28:40 2009 From: sjmachin at lexicon.net (John Machin) Date: Fri, 3 Apr 2009 01:28:40 -0700 (PDT) Subject: interacting with an updatedb generated data file within python References: <24247ea1-1d7b-4ab9-afcd-308a65b0b28d@z23g2000prd.googlegroups.com> Message-ID: <6d77274b-a4b6-468e-8148-c64ac5f9ef82@w31g2000prd.googlegroups.com> On Apr 3, 6:25?pm, John Machin wrote: > The format appears to be documented e.g.http://www.delorie.com/gnu/docs/findutils/locatedb.5.html > and thus should be found on the locatedb(5) man page on your system. More comprehensive: http://www.gnu.org/software/findutils/manual/html_node/find_html/Database-Formats.html#Database-Formats > Assuming that you don't have the old version, it should take about 20 > lines of Python to loop around extracting the file names, plus some > more to open the file, read it in as one big string (how big is it?), > and check the dummy "LOCATE02" entry up the front -- it's a bit hard > to be sure how the prefix length of the first non-dummy entry is > determined without seeing an actual example, but my guess is that the > file will start like this: > > "\x00LOCATE02\x00\xF8name-of-first-file-in-full\x00........." > where the "\xF8" is -8 meaning ignore the 8-character previous name > "LOCATE02" i.e. previous name can be regarded as "". After noticing there was in fact an example, make that: "\x00LOCATE02\x00\x00name-of-first-file-in-full\x00........." i.e. you can assert that buffer[:10] == "\x00LOCATE02\x00" and start the loop from offset 10 with the previous name set to "". Cheers, John From theller at python.net Fri Apr 3 04:36:12 2009 From: theller at python.net (Thomas Heller) Date: Fri, 03 Apr 2009 10:36:12 +0200 Subject: pyreadline, InteractiveConsole, and tab completion on Windows Message-ID: <73m03sFv43cbU1@mid.individual.net> I have installed pyreadline, and get nice tab completion in the normal interactive interpreter: 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 rlcompleter >>> import readline >>> readline.parse_and_bind("tab: complete") >>> and oct UserWarning ArithmeticError assert SystemExit filter str break StandardError range property [...] >>> import sys >>> sys. sys.__displayhook__ sys.call_tracing sys.getfilesystemencoding sys.ps1 sys.__doc__ sys.callstats sys.getrecursionlimit sys.ps2 [...] >>> sys. However, in 'code.interact()', the behaviour is different. Hitting TAB at the top level works as before, but hitting TAB after entering 'sys.' for example doesn't show any completions: 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 rlcompleter >>> import readline >>> readline.parse_and_bind("tab: complete") >>> import code >>> code.interact() 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. (InteractiveConsole) >>> and oct UserWarning ArithmeticError assert SystemExit filter str break StandardError range property [...] >>> import sys >>> sys. How can I get the same tab-completion? Thanks, Thomas From jeremiah.dodds at gmail.com Fri Apr 3 04:45:31 2009 From: jeremiah.dodds at gmail.com (Jeremiah Dodds) Date: Fri, 3 Apr 2009 09:45:31 +0100 Subject: Python Goes Mercurial In-Reply-To: <9526e125eab49f61f79db30e721168ae.squirrel@localhost> References: <7x7i2499si.fsf@ruckus.brouhaha.com> <7a1dd0d8-1978-470b-a80d-57478d7f7f9e@q16g2000yqg.googlegroups.com> <3e682785-7976-4c1b-993e-eadba3e3503e@37g2000yqp.googlegroups.com> <12cbbbfc0904020519p20d625baid0674f9ecb7cf64f@mail.gmail.com> <910885da0904021222s6b3a20ddh2fec716e78b69922@mail.gmail.com> <9526e125eab49f61f79db30e721168ae.squirrel@localhost> Message-ID: <12cbbbfc0904030145i74ea3bawa56244c4462870e4@mail.gmail.com> On Thu, Apr 2, 2009 at 9:02 PM, andrew cooke wrote: > Echo wrote: > > 2009/4/2 Jeremiah Dodds > > > >> The one thing that makes me want to use git more than any other dvcs is > >> that you don't have to create a new directory for branches. This may be > >> possible in other dvcs's , but git is the only one I've seen advertise > >> the > >> capability. > [...] > > That is the main reason why I switched to git at my work. > > As for the git rebase, I don't ever plan on using that 'feature'. Even > > though I would rather have had GvR pick git, I think he did a good job > > deciding. > > please can you explain this? i can think of a couple of things you could > mean, but neither of them seem to make much sense to me. > > > one is that hg only allows you one branch per repository. i checked the > docs and that's not true - see 2/3 way down > > http://hgbook.red-bean.com/read/managing-releases-and-branchy-development.html > - which describes how you can branch in the repository and then swap your > working copy between them. > It looks like basically the same thing is accomplishable in hg, but is a bit discouraged (I am not experienced with hg, and haven't read the docs thoroughly, so I could be off base here). In most (d)vcs's , there is normally a one-to-one relationship between project branches and directories on your filesystem. In git, there is normally a many-to-on relationship between project branches and directories on your filesystem - branching is cheap and easy, and you can branch for every little fix or feature you want to do, keeping one directory with multiple logical development paths with basically no pain. -------------- next part -------------- An HTML attachment was scrubbed... URL: From fetchinson at googlemail.com Fri Apr 3 04:51:03 2009 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Fri, 3 Apr 2009 01:51:03 -0700 Subject: HTML Generation In-Reply-To: <2b54d4370904030004p4b9261dfjdf809bf861fb517e@mail.gmail.com> References: <2b54d4370904030004p4b9261dfjdf809bf861fb517e@mail.gmail.com> Message-ID: > Hello all, > > I'm writing a web app and wanted to do some html generation (I really do not > like to maintain or write html). > > I'm thinking of writing a dsl based on the following: > > def html(): > return > > def a(): > return > > def body(): > return > (html, > ...(head, (style, "id", {"font-color":"black"})) > ...(body, > ...... (a, "id", {"title":"bla"}, ("link title","http://www.link.com")), > ......(p, "paragraph text") > ...... ) > ...) > ) > > And then parsing the resulting list. (the specification of sorts is as far > as I've got) I'd have to create the list as a string and then call a method > which executes the contents as the methods would not be in the local scope. > > The reason I'm posting is I'm curious what people think, I've been starting > to perfer this style and am curious if a similar solution exists or if this > is a good idea :) > > I'll proberly finish it as a learning experience regardless. > > I appreciate any responses :) You might find the following helpful: http://wiki.python.org/moin/Templating HTH, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From mail at microcorp.co.za Fri Apr 3 05:39:14 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Fri, 3 Apr 2009 11:39:14 +0200 Subject: "Pythoner",Wish me luck! References: <54afb932-1a42-42d2-a3b9-0151b2bd410b@q30g2000prq.googlegroups.com> Message-ID: <004601c9b440$e4899be0$0d00a8c0@Hendrik> "Matteo" wrote: On Apr 3, 9:05 am, Linuxwell wrote: >> Starting today I would like to study Python,Wish me luck! > >Good luck! > >Don't forget to... > >>>> print 'Hello World!' This is better advice than what you may think, because the interactive interpreter is your very best friend when studying the language. You get there by typing "python" at the command line, and pressing enter. Using it, you will save yourself many hours of misunderstanding. - Hendrik From murali.au at gmail.com Fri Apr 3 05:45:07 2009 From: murali.au at gmail.com (Murali kumar) Date: Fri, 3 Apr 2009 15:15:07 +0530 Subject: config parser -help Message-ID: hi all, i want to all my configuration file names in current/user/home directory.. for that i came across following function in configparser class.. Is it must to specify file names? or only directories enough.. if there anyway to read conf filenames in current/user directory... config.read(['site.cfg', os.path.expanduser('~/.myapp.cfg')]) -------------- next part -------------- An HTML attachment was scrubbed... URL: From saluk64007 at gmail.com Fri Apr 3 05:56:14 2009 From: saluk64007 at gmail.com (Patrick Mullen) Date: Fri, 3 Apr 2009 02:56:14 -0700 Subject: Python Goes Mercurial In-Reply-To: <12cbbbfc0904030145i74ea3bawa56244c4462870e4@mail.gmail.com> References: <7x7i2499si.fsf@ruckus.brouhaha.com> <7a1dd0d8-1978-470b-a80d-57478d7f7f9e@q16g2000yqg.googlegroups.com> <3e682785-7976-4c1b-993e-eadba3e3503e@37g2000yqp.googlegroups.com> <12cbbbfc0904020519p20d625baid0674f9ecb7cf64f@mail.gmail.com> <910885da0904021222s6b3a20ddh2fec716e78b69922@mail.gmail.com> <9526e125eab49f61f79db30e721168ae.squirrel@localhost> <12cbbbfc0904030145i74ea3bawa56244c4462870e4@mail.gmail.com> Message-ID: 2009/4/3 Jeremiah Dodds : > > > On Thu, Apr 2, 2009 at 9:02 PM, andrew cooke wrote: >> >> Echo wrote: >> > 2009/4/2 Jeremiah Dodds >> > >> >> The one thing that makes me want to use git more than any other dvcs is >> >> that you don't have to create a new directory for branches. This may be >> >> possible in other dvcs's , but git is the only one I've seen advertise >> >> the >> >> capability. >> [...] >> > That is the main reason why I switched to git at my work. >> > As for the git rebase, I don't ever plan on using that 'feature'. Even >> > though I would rather have had GvR pick git, I think he did a good job >> > deciding. > It looks like basically the same thing is accomplishable in hg, but is a bit > discouraged (I am not experienced with hg, and haven't read the docs > thoroughly, so I could be off base here). In most (d)vcs's , there is > normally a one-to-one relationship between project branches and directories > on your filesystem. In git, there is normally a many-to-on relationship > between project branches and directories on your filesystem - branching is > cheap and easy, and you can branch for every little fix or feature you want > to do, keeping one directory with multiple logical development paths with > basically no pain. > > > -- > http://mail.python.org/mailman/listinfo/python-list > > I don't think it's discouraged at all. There are several ways to accomplish a similar workflow if you want a quick, short-lived branch. The ones I know of, are named branches, bookmarks, and mercurial queues. I have a hard time wrapping my head around what all of these (and git branching) mean, since a quick branch is something I rarely need in my personal development. The differences between each approach is very difficult for me to see. But they all let you have one set of files and work on disparate things at once to merge later. Queues (which is a patch stack) seem useful but also dangerous, since you can do a lot of switching back and forth without actually commiting. Since you can apply any of the queues to your working directory, they might work well for a short lived feature that you potentially put into a real branch or into the main if the feature is more complete and less experimental. Named branches work off of a changeset I believe. At any time you can rename the current branch, commit new stuff from that point in history, and you have a new branch; using the name to keep track of where you are. If you want to switch branches, you just update the working copy to be the new branch you want to work from. There are probably some advantages to how git handles things, considering this is the main feature I hear cited at something git does certifiably better, but I wouldn't know what that would be. Oh, and merc recently got a rebase as well, for good or ill. From steve at REMOVE-THIS-cybersource.com.au Fri Apr 3 05:57:11 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Apr 2009 09:57:11 GMT Subject: Iteratoration question References: <2a43b3a8-84d5-434e-a9c3-5f60ed6ba5b2@r5g2000prh.googlegroups.com> <02adc575560fa8733aee3c7f59668dac.squirrel@localhost> Message-ID: <01e5d1be$0$20654$c3e8da3@news.astraweb.com> On Thu, 02 Apr 2009 18:07:38 -0700, grocery_stocker wrote: > Okay, I was thinking more about this. I think this is also what is > irking me. Say I have the following.. > >>>> a = [1,2,3,4] >>>> for x in a: > ... print x > ... > 1 > 2 > 3 > 4 >>>> >>>> > Would 'a' somehow call __iter__ and next()? If so, does python just > perform this magically? Not necessarily. For loops will call __iter__ automatically if it exists, but that isn't the only way that for loops can work. There is an older sequence protocol that the for loop will use as well: >>> class D(object): ... def __getitem__(self, i): ... if i < 5: return i ... raise IndexError ... >>> d = D() >>> for i in d: ... print i ... 0 1 2 3 4 -- Steven From ozgurvt at gmail.com Fri Apr 3 06:27:24 2009 From: ozgurvt at gmail.com (ozgur vatansever) Date: Fri, 3 Apr 2009 03:27:24 -0700 (PDT) Subject: SOAPpy serious problem Message-ID: <3f30aad9-acb2-4700-92c3-b84acde40b8e@j8g2000yql.googlegroups.com> Hi, I have a problem about soappy. I have a wsdl file located at http://212.175.81.30:7777/importWebService/services/ShippingOrderDispatcher?wsdl I want to call the function "dispatch" but that function takes an array of ShippingOrder objects. I couldn't manage to define that array of ShippingOrder structs. I tried SOAPpy.structType, SOAPpy.typedArrayType. I'm missing something. Any suggestions? Here is the code: # -*- coding: utf-8 -*- import SOAPpy WSDLFILE = "http://212.175.81.30:7777/importWebService/services/ ShippingOrderDispatcher?wsdl" server = SOAPpy.WSDL.Proxy(WSDLFILE) stl = SOAPpy.typedArrayType(name="ArrayOfShippingOrder") st = SOAPpy.structType(name="ShippingOrder") st._addItem("invoiceKey","213123") st._addItem("cargoKey","12312312") st._addItem("receiverCustName","asdasdasd") st._addItem("receiverAddress","asdasdasdasdas") st._addItem("receiverPhone1","2122222222") st._addItem("receiverPhone2","") st._addItem("receiverPhone3","") st._addItem("cityName","ANKARA") st._addItem("townName", "") st._addItem("custProdId", "1") st._addItem("desi", 4.67) st._addItem("kg", 0.0) st._addItem("cargoCount", 1) st._addItem("waybillNo", "") st._addItem("specialField1", "") st._addItem("specialField2", "") st._addItem("specialField3", "") st._addItem("ttCollectionType", "") st._addItem("ttDocumentId", long(1.0)) st._addItem("ttDocumentSaveType", "") st._addItem("ttInvoiceAmount", "") st._addItem("orgReceiverCustId", "") st._addItem("description", "") st._addItem("taxOfficeId", long(-1)) st._addItem("taxNumber", "") stl.append(st) server.soapproxy.config.dumpSOAPOut = 1 print server.dispatch(stl,"USER","PASSWD") I got the following error: SOAPpy.Types.faultType: References: Message-ID: Is there anyway to read all my configuration filenames with extension (.cfg)? Advanced thanks.. On Fri, Apr 3, 2009 at 3:15 PM, Murali kumar wrote: > hi all, > > i want to all my configuration file names in current/user/home directory.. > > for that i came across following function in configparser class.. > Is it must to specify file names? or only directories enough.. > if there anyway to read conf filenames in current/user directory... > > config.read(['site.cfg', os.path.expanduser('~/.myapp.cfg')]) > > > > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at REMOVE-THIS-cybersource.com.au Fri Apr 3 06:55:06 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Apr 2009 10:55:06 GMT Subject: A design problem I met again and again. References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> <01e58397$0$20654$c3e8da3@news.astraweb.com> Message-ID: <01e5df51$0$20654$c3e8da3@news.astraweb.com> On Thu, 02 Apr 2009 22:18:02 -0700, Emile van Sebille wrote: > Steven D'Aprano wrote: >> On Thu, 02 Apr 2009 16:51:24 -0700, Emile van Sebille wrote: > >>> I refactor constantly during development to avoid code reuse through >>> cut-n-paste, but once I've got it going, whether it's 1000 or 6000 >>> lines, it doesn't matter as long as it works. >> >> If you've been refactoring during development, and gotten to the point >> where it is working, > > yes, but > >> clear and maintainable, > > not necessarily If it's not clear and maintainable, then there *is* refactoring left to do. Whether you (generic you) choose to do so or not is a separate issue. >> then there's very little refactoring left to do. > > Again, not necessarily. I often find it easier to refactor old code > when I'm maintaining it to better understand how to best implement the > change I'm incorporating at the moment. The refactoring certainly may > have been done when the code was originally written, but at that time > refactoring would have only served to pretty it up as it already worked. > >> I don't think anyone suggests that you refactor code that doesn't need >> refactoring. > > That's exactly what I read the OP as wanting to do. That's why I was > asking why. So, I think the question becomes, when does code need > refactoring? (1) When the code isn't clear and maintainable. (2) When you need to add or subtract functionality which would leave the code unclear or unmaintainable. (3) When refactoring would make the code faster, more efficient, or otherwise better in some way. (4) When you're changing the API. -- Steven From murali.au at gmail.com Fri Apr 3 07:46:17 2009 From: murali.au at gmail.com (Murali kumar) Date: Fri, 3 Apr 2009 17:16:17 +0530 Subject: config parser -help - anybody please immediately help for me... Message-ID: On Fri, Apr 3, 2009 at 3:59 PM, Murali kumar wrote: > Is there anyway to read all my configuration filenames with extension > (.cfg)? > > Advanced thanks.. > > > On Fri, Apr 3, 2009 at 3:15 PM, Murali kumar wrote: > >> hi all, >> >> i want to all my configuration file names in current/user/home directory.. >> >> >> for that i came across following function in configparser class.. >> Is it must to specify file names? or only directories enough.. >> if there anyway to read conf filenames in current/user directory... >> >> config.read(['site.cfg', os.path.expanduser('~/.myapp.cfg')]) >> >> >> >> >> >> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: From pecora at anvil.nrl.navy.mil Fri Apr 3 08:36:17 2009 From: pecora at anvil.nrl.navy.mil (Lou Pecora) Date: Fri, 03 Apr 2009 08:36:17 -0400 Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <5c92e9bd-1fb4-4c01-a928-04d7f673329a@e21g2000yqb.googlegroups.com> Message-ID: In article <5c92e9bd-1fb4-4c01-a928-04d7f673329a at e21g2000yqb.googlegroups.com>, Aaron Brady wrote: > On Apr 2, 6:34?pm, Tim Wintle wrote: > > On Thu, 2009-04-02 at 15:16 -0700, Emile van Sebille wrote: > > > Lou Pecora wrote: > > > > Confusion only comes when you try to force the > > > > defintion of one of them on the other and then say it's illogical or not > > > > natural. ?Both are natural. > > > > > Consider the French 'Premiere etage' vs the American 'First Floor' > > > > or even in the same language - "first floor" in English (UK) is very > > different from "first floor" in English (US). > > Did I tell you guys that 'natural' has 38 definitions at > dictionary.com? Amazing. I suggest you pick the one that fits best. -- -- Lou Pecora From fairwinds.dp at gmail.com Fri Apr 3 08:46:14 2009 From: fairwinds.dp at gmail.com (David Pratt) Date: Fri, 3 Apr 2009 09:46:14 -0300 Subject: mmap regex search replace Message-ID: <377B7B82-E663-4D28-8FDD-3AF0A2FECF8B@gmail.com> Hi. I have a circumstance where I have to search and replace a block of text in a very large file. I have written some psuedo code to locate the text and print the span of text to be removed and replaced by new block. Can someone advise what to do to remove the text span and insert with the new text. the match.span() provides a tuple of the starting and ending position. Many thanks. Regards, David import mmap import re text_to_insert = 'the block to insert' pattern = re.compile(my regex here) f = open('my_large_file.dat', 'r+') try: m = mmap.mmap(f.fileno(), 0) try: match = chart_re.search(m) print match.span() finally: m.close() finally: f.close() From gagsl-py2 at yahoo.com.ar Fri Apr 3 08:59:54 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 03 Apr 2009 09:59:54 -0300 Subject: py2exe problem References: Message-ID: En Fri, 03 Apr 2009 03:55:20 -0300, Wolfgang Forstmeier escribi?: > On 03.04.2009 05:29, Gabriel Genellina wrote: >> En Thu, 02 Apr 2009 08:06:22 -0300, Wolfgang Forstmeier >> escribi?: >>> On 02.04.2009 11:34, Gabriel Genellina wrote: >>>> En Wed, 01 Apr 2009 17:51:52 -0300, Wolfgang Forstmeier >>>> escribi?: >>>> >>>>> what kind of error do I have with getting this error at starting my >>>>> app. >>>>> Im am not using IdleConf.GetOption right now. > > from idlelib.OutputWindow import tkMessageBox > > ... > # Define about message box > def about(self): > tkMessageBox.showinfo("About", "My little about text box.") > # -- > ... > > There is some more GUI programming arround that def in my class, but > that uses only Tkinter, should not be interesting for that error. The above doesn't use idlelib either; tkMessageBox comes with Tkinter. import tkMessageBox is enough, and doesn't carry idlelib as a dependency. If you remove all idlelib remaining references -if any- that annoying warning should go away too (and your program should be smaller and load faster). -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Fri Apr 3 09:01:01 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 03 Apr 2009 10:01:01 -0300 Subject: Regression test References: Message-ID: En Mon, 23 Mar 2009 21:04:35 -0300, Colin J. Williams escribi?: > I am running a regression test, mainly because I wish to explore > subprocess, and get a number of errors before crashing. Is this to be > expected? The run output is below. > > C:\Documents and Settings\cjw\Desktop>C:\python26\python.exe > Python 2.6 (r26:66721, Oct 2 2008, 11:35:03) [MSC v.1500 32 bit > (Intel)] on win32 > Type "help", "copyright", "credits" or "license" for more information. >>>> from test import regrtest >>>> regrtest.main() Not sure about 2.6, but 2.6.1 fails some tests too -- I assume they were considered minor issues... -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Fri Apr 3 09:07:40 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 03 Apr 2009 10:07:40 -0300 Subject: config parser -help - anybody please immediately help for me... References: Message-ID: En Fri, 03 Apr 2009 08:46:17 -0300, Murali kumar escribi?: >> Is there anyway to read all my configuration filenames with extension >> (.cfg)? See the glob module http://docs.python.org/library/glob.html glob.glob(os.path.expanduser('~/*.cfg')) -- Gabriel Genellina From manu3d at gmail.com Fri Apr 3 09:11:22 2009 From: manu3d at gmail.com (Emanuele D'Arrigo) Date: Fri, 3 Apr 2009 06:11:22 -0700 (PDT) Subject: Class methods read-only by default? References: <7497f42a-46f0-46ce-9035-087da489205c@l10g2000vba.googlegroups.com> <01e4b83f$0$20654$c3e8da3@news.astraweb.com> Message-ID: Thank you both, Steven and Andrew, for the insightful explanation. I shall keep it in mind when thinking about classes methods and instances. Thank you again. Manu From mark.dufour at gmail.com Fri Apr 3 09:11:29 2009 From: mark.dufour at gmail.com (srepmub) Date: Fri, 3 Apr 2009 06:11:29 -0700 (PDT) Subject: c.l.py dead, news at 11 (was Re: Mangle function name with decorator?) References: <9c29cb06-9710-4f5c-b26b-70c15ef9deb1@b16g2000yqb.googlegroups.com> <1bednZO_Cc026VDUnZ2dnUVZ_hSdnZ2d@giganews.com> <01defa5a$0$20627$c3e8da3@news.astraweb.com> <7xzlf34eu1.fsf@ruckus.brouhaha.com> <6b82a17d-b052-4b1d-813a-7ab9014cf7d1@o36g2000yqh.googlegroups.com> <53c52411-4d8d-45af-a6c7-059d0fa8efd9@r33g2000yqn.googlegroups.com> Message-ID: <4cb9caca-c4e7-44bf-b6e0-0a348f79c0bf@j12g2000vbl.googlegroups.com> On Mar 30, 4:36?pm, Michele Simionato wrote: > On Mar 30, 3:31?pm, srepmub wrote: > > > for the record, the input forShedskinis pure Python, so there is no > > added syntax or optional type declaration system. that said, I can > > understand it not being on some list for not being production-ready. > > > thanks, > > mark dufour. > > But doesShedSkinaccepts all valid Python constructs? > I thought there were restrictions. there are certainly several important restrictions, but what I meant was that if it works with Shedskin it is also valid Python code, and there are no hidden type declarations or hints hidden in docstrings and such. thanks, mark dufour. From google at mrabarnett.plus.com Fri Apr 3 09:27:28 2009 From: google at mrabarnett.plus.com (MRAB) Date: Fri, 03 Apr 2009 14:27:28 +0100 Subject: python for loop In-Reply-To: <6e1e8c53-2b3e-4ade-b52e-507c53721c98@v1g2000prd.googlegroups.com> References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <72i5t4tgfo2h4gd6ggcs02flkca85kgoku@4ax.com> <0dbc9665-3ee8-4d62-8e9e-75c6d700076e@f1g2000prb.googlegroups.com> <6e1e8c53-2b3e-4ade-b52e-507c53721c98@v1g2000prd.googlegroups.com> Message-ID: <49D60EC0.6010506@mrabarnett.plus.com> Lie wrote: [snip] > Alternatively: > "One friend of mine half-seriously advanced the following thesis: We > should count from zero. But "first" is, etymologically, a diminution > of "foremost", and (as TomStambaugh says) should mean "0th" when we > count from 0. And "second" is from the Latin "secundus", meaning > "following", so it should mean "1th" when we count from 0. Obviously > the ordinals from "third" onwards get their names from the numbers. > So... we need a new word for "2th". He proposed "twifth". Thus: first, > second, twifth, third, fourth, ..." > I propose "twoth" (sounds like "tooth"). :-) From google at mrabarnett.plus.com Fri Apr 3 09:33:03 2009 From: google at mrabarnett.plus.com (MRAB) Date: Fri, 03 Apr 2009 14:33:03 +0100 Subject: "Pythoner",Wish me luck! In-Reply-To: References: <54afb932-1a42-42d2-a3b9-0151b2bd410b@q30g2000prq.googlegroups.com> Message-ID: <49D6100F.6040809@mrabarnett.plus.com> Matteo wrote: > On Apr 3, 9:05 am, Linuxwell wrote: >> Starting today I would like to study Python,Wish me luck! > > Good luck! > > Don't forget to... > >>>> print 'Hello World!' > Or: >>> print('Hello World!') if using Python 3. From emile at fenx.com Fri Apr 3 09:36:33 2009 From: emile at fenx.com (Emile van Sebille) Date: Fri, 03 Apr 2009 06:36:33 -0700 Subject: A design problem I met again and again. In-Reply-To: <01e5df51$0$20654$c3e8da3@news.astraweb.com> References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> <01e58397$0$20654$c3e8da3@news.astraweb.com> <01e5df51$0$20654$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Thu, 02 Apr 2009 22:18:02 -0700, Emile van Sebille wrote: > >> Steven D'Aprano wrote: >>> On Thu, 02 Apr 2009 16:51:24 -0700, Emile van Sebille wrote: >> >>>> I refactor constantly during development to avoid code reuse through >>>> cut-n-paste, but once I've got it going, whether it's 1000 or 6000 >>>> lines, it doesn't matter as long as it works. >>> If you've been refactoring during development, and gotten to the point >>> where it is working, >> yes, but >> >>> clear and maintainable, >> not necessarily > > If it's not clear and maintainable, then there *is* refactoring left to > do. Agreed. > Whether you (generic you) choose to do so or not is a separate issue. Also agreed - and that is really my point. Doing so feels to me like continuing to look for a lost object once you've found it. >> So, I think the question becomes, when does code need refactoring? > (1) When the code isn't clear and maintainable. > > (2) When you need to add or subtract functionality which would leave the > code unclear or unmaintainable. > > (3) When refactoring would make the code faster, more efficient, or > otherwise better in some way. > > (4) When you're changing the API. Certainly agreed on (2) and (4). (1) follows directly from (3). And (3) only after an issue has been observed. Emile From gagsl-py2 at yahoo.com.ar Fri Apr 3 09:44:09 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 03 Apr 2009 10:44:09 -0300 Subject: with open('com1', 'r') as f: References: <8bc55c05-19da-41c4-b916-48e0a4be49b9@p11g2000yqe.googlegroups.com> <20090403002347.163911b4@oxygen> <8de4f974-073f-41cb-9729-341a02ca4c3f@u8g2000yqn.googlegroups.com> Message-ID: En Thu, 02 Apr 2009 20:04:14 -0300, gert escribi?: > On Apr 2, 8:53?pm, Kushal Kumaran wrote: >> On Thu, 2 Apr 2009 10:01:02 -0700 (PDT) >> gert wrote: >> > from subprocess import * >> > check_call(['mode', 'COM1:9600,N,8,1,P'],shell=True) >> > while True: >> > ? ? with open('com1', 'r') as f: >> > ? ? ? ? for line in f: >> > ? ? ? ? ? ? ?print('line') >> >> > This works very well except for one thing. After a reboot I have to >> > launch 1 time any windows serial exe application no mater with one, >> > that just opens an closes the com port, before i can launch this >> > script. The script keeps on working even after closing and reopening >> > it, until i reboot the pc. Then again I have to launch one time a >> > serial.exe and close it again. The exe does not run anything in the >> > background it just does something in windows python does not do when >> > it reads from the com port after a fresh reboot. >> >> > And i really appreciate it if somebody knew what it was. >> >> I don't know why you're getting this behaviour, but have you tried using >> a python library for accessing the serial port? >> Seehttp://pyserial.wiki.sourceforge.net/pySerial. > > I am sorry but I don't think pyserial will work on python3.x and I > also like to know whats going on before I consider it. A real Windows program accessing the serial port is likely to use SetupComm, SetCommState, and other functions in addition to CreateFile. See http://msdn.microsoft.com/en-us/library/aa363196(VS.85).aspx pySerial takes care of all those details, as suggested. > Maybe its a bug in open() on windows? open() doesn't care about the file name; it's the OS that interprets "com1" as a serial port. -- Gabriel Genellina From davea at dejaviewphoto.com Fri Apr 3 09:58:48 2009 From: davea at dejaviewphoto.com (Dave Angel) Date: Fri, 03 Apr 2009 08:58:48 -0500 Subject: py2exe problem In-Reply-To: References: Message-ID: <49D61618.6080708@dejaviewphoto.com> Wolfgang Forstmeier wrote: > > > >> >> Ok, but do you really use idlelib for something? Or it's just some >> random code you found somewhere and drop into your application? > > Ah yes, I really use this. I create some message boxes for a little > GUI application that controls some other program with COM. > Running my app without py2exe, just with python, there is no warning > at all. This comes in with py2exe first. > > Here some piece of code that I use for tkMessageBox. > > from idlelib.OutputWindow import tkMessageBox > > ... > # Define about message box > def about(self): > tkMessageBox.showinfo("About", "My little about text box.") > # -- > ... > > There is some more GUI programming arround that def in my class, but > that uses only Tkinter, should not be interesting for that error. > > So why not use tkMessageBox directly, and skip Idle's namespace wrapping? import tkMessageBox tkMessageBox.showinfo("About", "My little about text box.") From andrew at acooke.org Fri Apr 3 09:58:55 2009 From: andrew at acooke.org (andrew cooke) Date: Fri, 3 Apr 2009 09:58:55 -0400 (CLT) Subject: A design problem I met again and again. In-Reply-To: References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> <01e58397$0$20654$c3e8da3@news.astraweb.com> <01e5df51$0$20654$c3e8da3@news.astraweb.com> Message-ID: <8a030dbc1b01b5a431cf731abe204b98.squirrel@localhost> Emile van Sebille wrote: >>> Whether you (generic you) choose to do so or not is a separate issue. > > Also agreed - and that is really my point. Doing so feels to me like > continuing to look for a lost object once you've found it. i can see your point here, but there's two things more to consider: 1 - if you do need to refactor it later, because there is a bug say, it will be harder to do so because you will have forgotten much about the code. so if it is likely that you will need to refactor in the future, it may pay to do some of that work now. 2 - if someone else needs to work with the code then the worse state it is in - even if it works - the harder time they will have understanding it. which could lead to them using or extending it incorrectly, for example. both of the above fall under the idea that code isn't just a machine that produces a result, but also serves as documentation. and working code isn't necessarily good documentation. i don't think there's a clear, fixed answer to this (i don't think "stop refactoring as soon as all tests work" can be a reliable general rule any more than "refactor until it is the most beautiful code in the world" can be). you need to use your judgement on a case-by-case basis. in fact, the thing i am most sure of in this thread is that 15000 lines of code in one module is a disaster. the likelihood of that being ok seems so small, compared to all the other uncertainties in software development, that i cannot see why people are even discussing it (well, i can understand, because human nature is what it is, and software development seems to attract a certain kind of pedantic, rigid mind, but even so...) andrew From james at agentultra.com Fri Apr 3 10:33:52 2009 From: james at agentultra.com (J Kenneth King) Date: Fri, 03 Apr 2009 10:33:52 -0400 Subject: HTML Generation References: <2b54d4370904030004p4b9261dfjdf809bf861fb517e@mail.gmail.com> Message-ID: <874ox56c33.fsf@agentultra.com> Tino Wildenhain writes: > Hi Mike, > > > Mike wrote: >> Hello all, >> >> I'm writing a web app and wanted to do some html generation (I >> really do not like to maintain or write html). >> >> I'm thinking of writing a dsl based on the following: >> >> def html(): >> return >> >> def a(): >> return >> >> def body(): >> return > > That would be writing HTML just another way and also > mixing code and representation, this is generally not a > good idea. > > If you really don't want to maintain HTML along with > your code, I'd suggest an approach like > > Zope Page Templates > > http://en.wikipedia.org/wiki/Zope_Page_Templates#Zope_Page_Templates > > which can be used outside Zope as well, (even with many other languages) > > For example: http://zpt.sourceforge.net/ > > Regards > Tino You could also look at CL-WHO for inspiration. I feel the same way about templates -- the seperation is unnecessary complexity. One knowledge domain for everything please. We're not building the tower of Babel here. I use Python through all of my desktop application development without switching to markup languages and stylesheets and other scripting languages (at least VERY rarely). Why is the web any different? It's rather somewhat trivial to implement a simple grammar in Python for describing a page using Python data structures and Pythonic idioms. I've toyed with it using a base "Tag" metaclass which creates the __str__() representation for a Tag-metaclassed object based on the name of the class. It would look something like this: code: ------------------------------------------------------------------------ from tags import html, head, meta, title, body, div, p, a mypage = html( head( meta(attrs={'http-equiv': "Content-Type", 'content': "text/html;"}), title("My Page")), body(attrs={'background': "#ffffff;"}, div(attrs={'id': "article-content"}, p(a(attrs={'href': "http://python.org"}, "Python.org"))))) tabbed_file(mypage, open('myfile.html', 'w')) ------------------------------------------------------------------------ Unfortunately Python isn't as malleable when it comes to syntax as Lisp, but I don't think it's a lost endeavour. Having to know a template language, markup, stylesheets, CSS, plus all of Python and good web application design is a huge headache and puts too many cooks in the kitchen. I'd rather 95% of the work be in pure Python and keep the team small. Just 0.02 monetary units. From stefan_ml at behnel.de Fri Apr 3 10:59:20 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Fri, 03 Apr 2009 16:59:20 +0200 Subject: HTML Generation In-Reply-To: <874ox56c33.fsf@agentultra.com> References: <2b54d4370904030004p4b9261dfjdf809bf861fb517e@mail.gmail.com> <874ox56c33.fsf@agentultra.com> Message-ID: <49d62448$0$31338$9b4e6d93@newsspool4.arcor-online.net> J Kenneth King wrote: > from tags import html, head, meta, title, body, div, p, a > > mypage = html( > head( > meta(attrs={'http-equiv': "Content-Type", > 'content': "text/html;"}), > title("My Page")), > body(attrs={'background': "#ffffff;"}, > div(attrs={'id': "article-content"}, > p(a(attrs={'href': "http://python.org"}, > "Python.org"))))) > > tabbed_file(mypage, open('myfile.html', 'w')) See here for another example that uses lxml.html: http://codespeak.net/lxml/lxmlhtml.html#creating-html-with-the-e-factory Stefan From python.list at tim.thechases.com Fri Apr 3 11:01:43 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 03 Apr 2009 10:01:43 -0500 Subject: config parser -help - anybody please immediately help for me... In-Reply-To: References: Message-ID: <49D624D7.9000802@tim.thechases.com> 1) Posting the same question multiple times in a 3 hour span isn't likely to get you anything more than people who get peeved at your impatience. Take a moment to realize that you posted at 4:00am for my local (CST/CDT) time. That means that, at least for those of us in the US, you posted between 2:00am and 5:00am. Most of us in this swath of the globe are asleep at that hour. Our good brothers & sisters across the pond have been awake for a while, but remember you're only addressing a portion of the newsgroup until you've let at least 16-24 hours pass. 2) Post the question clearly the first time, to the best of your ability. With verbs: "i want to all my configuration file names..." You want to WHAT all your configuration file names? Your second post hints that you want to automatically tack on the extension, an aspect that you *totally* omitted in the initial posting. To answer what I believe you're asking, yes, you do have to supply the full filename (not just the directory). You can use a list-comprehension to tack on extensions if you want, but for only 2 file-names, it's a bit of overkill: ["%s.cfg" % path for path in lst_of_filenames_without_ext] If you, for some crazy reason, want to match all the filenames in a directory matching the pattern "*.cfg", you can use the "glob" module. However, a lot of files with .cfg extensions don't need to be in "INI" format (the format read by configparser), so trying to use configparser to read them may fail. 3) "anybody please immediately help for me". whine, whine. You already have a working solution. You're only looking to tweak the code for some unfathomable reason. There's no apparent need for urgency unless you're taking an exam or have a homework problem due that you haven't been able to figure out. Give it a break. Waiting-for-an-hour-or-two-to-hit-Send-to-teach-patience'ly yours, -tkc From clp2 at rebertia.com Fri Apr 3 11:08:26 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 3 Apr 2009 08:08:26 -0700 Subject: config parser -help In-Reply-To: References: Message-ID: <50697b2c0904030808w696244a1wc76cce9a99d0d6ec@mail.gmail.com> > On Fri, Apr 3, 2009 at 3:15 PM, Murali kumar wrote: >> >> hi all, >> >> i want to all my configuration file names in current/user/home directory.. >> >> for that i came across following function in configparser class.. >> Is it must to specify file names? or only directories enough.. >> if there anyway to read conf filenames in current/user directory... >> >> config.read(['site.cfg', os.path.expanduser('~/.myapp.cfg')]) 2009/4/3 Murali kumar : > Is there anyway to read all my configuration filenames with extension > (.cfg)? > > Advanced thanks.. > Not directly, but you can get a list of all such files using glob.glob() [http://docs.python.org/library/glob.html] and then easily write such logic yourself from there. Cheers, Chris -- I have a blog: http://blog.rebertia.com From zamnedix at gmail.com Fri Apr 3 11:23:22 2009 From: zamnedix at gmail.com (Zamnedix) Date: Fri, 3 Apr 2009 08:23:22 -0700 (PDT) Subject: python needs leaning stuff from other language References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> Message-ID: <1c03c513-8316-4b6a-afba-d5b345afad23@v23g2000pro.googlegroups.com> On Apr 2, 3:25 pm, online.serv... at ymail.com wrote: > python's list needs a thing list.clear() like c# arraylist > and > python needs a writeline() method Please don't post things like list before you do any research. You don't know what you are talking about. From craig.emery at gmail.com Fri Apr 3 11:27:50 2009 From: craig.emery at gmail.com (Craig) Date: Fri, 3 Apr 2009 08:27:50 -0700 (PDT) Subject: Sending SMS using python script References: <2005e94f-94de-4566-be60-7245efa4271d@y6g2000prf.googlegroups.com> Message-ID: <6417a53c-11fe-4683-b57b-9b9e7a94c2a3@n33g2000vba.googlegroups.com> There's a Python wrapper to the Skype API here: http://sourceforge.net/projects/skype4py/ On Linux I've used the PyGTK GUI that uses this. It's called SkySentials here: http://www.kolmann.at/philipp/linux/skysentials/ Craig On Apr 3, 6:50?am, "ISF (Computer Scientists without Frontiers, Italy)" wrote: > On 2 Apr, 06:41, guptha wrote: > > > > > hi group, > > my application needs to sendSMSoccasionally to all the clients ?.Is > > there any library in python that supports in sendingSMS. > > I like to conform few information i gathered in this regard. > > > I can sendSMSby two ways > > > 1. SendingSMSusing Email clients > > 2. Usingsmsgateway to send message(we can implementSMSGateway API > > 's ,provided by vendor and ,sendSMS-- we will be charged > > accordingly ) > > > In case of First approach > > 1. We can make use of libgamil library to sendSMSusing gmail ( I > > ref :http://blog.datasingularity.com/?p=63) > > i suppose sendingsmsthrough gmail is not supported in India > > ?2. Can we use Skype4py library, > > > In case of second approach > > 1. Is there any way to sendSMSfor free inside India ,or ,Any freeSMSgateway providers in India > > Any information regarding this is appreciable > > ? Thanks > > A friend from India sent me this hint: > > the following link can be explored further for sending SMS on pyS60 > (python for symbian OS)http://mobilenin.com/pys60/menu.htm > > wkr, > Aldo From mwilson at the-wire.com Fri Apr 3 11:41:10 2009 From: mwilson at the-wire.com (Mel) Date: Fri, 03 Apr 2009 11:41:10 -0400 Subject: python needs leaning stuff from other language References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <73kt5jFvitcdU1@mid.uni-berlin.de> <49D54209.5090306@hotmail.com> <01e56a2f$0$20654$c3e8da3@news.astraweb.com> <87y6uie9ej.fsf@benfinney.id.au> Message-ID: Ben Finney wrote: > I think it would also be better to have One (and prefereably Only One) > Obvious Way To Do It. That obvious way, for those who work with > Python's ?set? and ?dict?, is a ?clear? method. It seems best to have > ?list? conform with this also. Does that mean a one-off special case rule to forbid slices having a default? Mel. From wuwei23 at gmail.com Fri Apr 3 11:43:35 2009 From: wuwei23 at gmail.com (alex23) Date: Fri, 3 Apr 2009 08:43:35 -0700 (PDT) Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <5c92e9bd-1fb4-4c01-a928-04d7f673329a@e21g2000yqb.googlegroups.com> Message-ID: On Apr 3, 10:36?pm, Lou Pecora wrote: > ?Aaron Brady wrote: > > Did I tell you guys that 'natural' has 38 definitions at > > dictionary.com? > > Amazing. ?I suggest you pick the one that fits best. You mean the one that feels most natural? From steve at REMOVE-THIS-cybersource.com.au Fri Apr 3 11:48:14 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Apr 2009 15:48:14 GMT Subject: python needs leaning stuff from other language References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <1c03c513-8316-4b6a-afba-d5b345afad23@v23g2000pro.googlegroups.com> Message-ID: <01e62404$0$20654$c3e8da3@news.astraweb.com> On Fri, 03 Apr 2009 08:23:22 -0700, Zamnedix wrote: > On Apr 2, 3:25 pm, online.serv... at ymail.com wrote: >> python's list needs a thing list.clear() like c# arraylist and >> python needs a writeline() method > > Please don't post things like list before you do any research. You don't > know what you are talking about. The original poster may or may not know what he is talking about, but adding a clear() method to lists seems to be very much in demand. I'd vote Yes for one. Besides, this news group is for people to ask questions about Python, even stupid questions. It's not just for experts only. -- Steven From steve at REMOVE-THIS-cybersource.com.au Fri Apr 3 11:51:13 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Apr 2009 15:51:13 GMT Subject: python needs leaning stuff from other language References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <73kt5jFvitcdU1@mid.uni-berlin.de> <49D54209.5090306@hotmail.com> <01e56a2f$0$20654$c3e8da3@news.astraweb.com> <87y6uie9ej.fsf@benfinney.id.au> Message-ID: <01e624b7$0$20654$c3e8da3@news.astraweb.com> On Fri, 03 Apr 2009 11:41:10 -0400, Mel wrote: > Ben Finney wrote: > >> I think it would also be better to have One (and prefereably Only One) >> Obvious Way To Do It. That obvious way, for those who work with >> Python's ?set? and ?dict?, is a ?clear? method. It seems best to have >> ?list? conform with this also. > > Does that mean a one-off special case rule to forbid slices having a > default? Why would it do that? -- Steven From ma.ston.99 at hotmail.com Fri Apr 3 11:58:16 2009 From: ma.ston.99 at hotmail.com (Mark) Date: Fri, 3 Apr 2009 17:58:16 +0200 Subject: django model problem Message-ID: <19a1cxf57zsli.1wmngjrxnlagf$.dlg@40tude.net> Hi, Say I have these simple models: class Musician(models.Model): first_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50) class Album(models.Model): artist = models.ForeignKey(Musician) name = models.CharField(max_length=100) Now in `Musician` I want to add a field "last_album". How can I do that? I'd like to be able to write: m = Musician(pk=1) print m.last_album.name When I try: last_album = models.OneToOneField(Album) it somehow works but creates a (useless) unique index for last_album. And I don't need additional related field in the Album model (it's also created now). Thanks, MS From aaron.hildebrandt at gmail.com Fri Apr 3 12:04:42 2009 From: aaron.hildebrandt at gmail.com (Aaron Scott) Date: Fri, 3 Apr 2009 09:04:42 -0700 (PDT) Subject: Best way to pickle functions Message-ID: <29bfcf8a-984d-47df-a116-7a1f80f171ba@z9g2000yqi.googlegroups.com> I have a number of functions that I need to pickle without necessarily knowing their names in advance. My first thought was to put all the functions in a class, then pickle the class, but it doesn't really work like I expected it to. import cPickle class PickleClass: def Awesome(self): pass stored = cPickle.dumps(PickleClass) del PickleClass restored = cPickle.loads(stored) Results: Traceback (most recent call last): File "pickletest.py", line 7, in restored = cPickle.loads(stored) AttributeError: 'module' object has no attribute 'PickleClass' So, the class itself isn't being pickled, just an instance of it. This being the case, what's the best way to store these functions? Maybe dump the class to a string and pull them back with an exec statement when I need them? From emile at fenx.com Fri Apr 3 12:21:41 2009 From: emile at fenx.com (Emile van Sebille) Date: Fri, 03 Apr 2009 09:21:41 -0700 Subject: A design problem I met again and again. In-Reply-To: <8a030dbc1b01b5a431cf731abe204b98.squirrel@localhost> References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> <01e58397$0$20654$c3e8da3@news.astraweb.com> <01e5df51$0$20654$c3e8da3@news.astraweb.com> <8a030dbc1b01b5a431cf731abe204b98.squirrel@localhost> Message-ID: andrew cooke wrote: > Emile van Sebille wrote: >>>> Whether you (generic you) choose to do so or not is a separate issue. >> Also agreed - and that is really my point. Doing so feels to me like >> continuing to look for a lost object once you've found it. > > i can see your point here, but there's two things more to consider: > > 1 - if you do need to refactor it later, because there is a bug say, it > will be harder to do so because you will have forgotten much about the > code. Yes, I generally count on it. Refactoring at that time is precisely when you get the most benefit, as it will concisely focus your attentions on the sections of code that need to be clearer to support the debugging changes. Face it, you'll have to get your head around the code anyway, be it 1, 5, or 10k lines and all beautifully structured or not. Remember, proper refactoring by definition does not change functionality -- so that bug in the code will be there regardless. > so if it is likely that you will need to refactor in the future, it > may pay to do some of that work now. Certainly -- and I envy those who know which sections to apply their attentions to and when to stop. Personally, I stop when it works and wait for feedback. > 2 - if someone else needs to work with the code then the worse state it is > in - even if it works - the harder time they will have understanding it. > which could lead to them using or extending it incorrectly, for example. Assuming you're talking about non-refactored code when you say worse, consider Zope vs Django. I have no doubt that both meet an acceptable level of organization and structure intended in part to facilitate maintenance. I've got multiple deployed projects of each. But I'll hack on Django if it doesn't do what I want and I find that easy, while hacking on Zope ranks somewhere behind having my mother-in-law come for a three-week stay on my favorite-things-to-do list. Refactored code doesn't necessarily relate to easier understanding. > both of the above fall under the idea that code isn't just a machine that > produces a result, but also serves as documentation. and working code > isn't necessarily good documentation. Here I agree. Once I've got it working and I have the time I will add minor clean up and some notes to help me the next time I'm in there. Clean up typically consists of dumping unused cruft, relocating imports to the top, and adding a couple lines of overview comments. On the other hand, I do agree with Aahz's sometimes tag line quote accepting all comments in code as lies. It's akin to believing a user -- do so only at your own peril. They're really bad witnesses. > i don't think there's a clear, fixed answer to this (i don't think "stop > refactoring as soon as all tests work" can be a reliable general rule any > more than "refactor until it is the most beautiful code in the world" can > be). you need to use your judgement on a case-by-case basis. Well said. > in fact, the thing i am most sure of in this thread is that 15000 lines of > code in one module is a disaster. Agreed. I took a quick scan and the largest modules I'm working with look to be closer to 1500 lines. Except tiddlywiki of course, which comes in at 9425 lines in the current download before adding anything to it. I bet I'd prefer even hacking that to zope though. One programmer's disaster is another programmer's refactoring dream :) Emile From castironpi at gmail.com Fri Apr 3 12:57:42 2009 From: castironpi at gmail.com (Aaron Brady) Date: Fri, 3 Apr 2009 09:57:42 -0700 (PDT) Subject: Best way to pickle functions References: <29bfcf8a-984d-47df-a116-7a1f80f171ba@z9g2000yqi.googlegroups.com> Message-ID: On Apr 3, 11:04?am, Aaron Scott wrote: > I have a number of functions that I need to pickle without necessarily > knowing their names in advance. My first thought was to put all the > functions in a class, then pickle the class, but it doesn't really > work like I expected it to. > > ? ? ? ? import cPickle > ? ? ? ? class PickleClass: > ? ? ? ? ? ? ? ? def Awesome(self): > ? ? ? ? ? ? ? ? ? ? ? ? pass > ? ? ? ? stored = cPickle.dumps(PickleClass) snip > So, the class itself isn't being pickled, just an instance of it. > > This being the case, what's the best way to store these functions? > Maybe dump the class to a string and pull them back with an exec > statement when I need them? All pickling a class does is pickle its module and name. You can't pickle functions in principle because byte-code is sensitive and volatile, and is least likely to run consistently later on. 'pickle' is not just for serialization, it's for persistence. Pickling the source code is much sturdier. It's very unlikely that the same code runs differently in different interpreters. It's much more likely that the same code runs the same, or not at all. It's better yet to just get the source from the original place every time: instead, pickle a file name and open the file. From castironpi at gmail.com Fri Apr 3 12:59:15 2009 From: castironpi at gmail.com (Aaron Brady) Date: Fri, 3 Apr 2009 09:59:15 -0700 (PDT) Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <5c92e9bd-1fb4-4c01-a928-04d7f673329a@e21g2000yqb.googlegroups.com> Message-ID: <7a374dce-baf5-4692-8e1b-20b44415bfca@g19g2000yql.googlegroups.com> On Apr 3, 10:43?am, alex23 wrote: > On Apr 3, 10:36?pm, Lou Pecora wrote: > > > ?Aaron Brady wrote: > > > Did I tell you guys that 'natural' has 38 definitions at > > > dictionary.com? > > > Amazing. ?I suggest you pick the one that fits best. > > You mean the one that feels most natural? No, not feels. *Is*. From mwilson at the-wire.com Fri Apr 3 13:12:55 2009 From: mwilson at the-wire.com (Mel) Date: Fri, 03 Apr 2009 13:12:55 -0400 Subject: python needs leaning stuff from other language References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <73kt5jFvitcdU1@mid.uni-berlin.de> <49D54209.5090306@hotmail.com> <01e56a2f$0$20654$c3e8da3@news.astraweb.com> <87y6uie9ej.fsf@benfinney.id.au> <01e624b7$0$20654$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Fri, 03 Apr 2009 11:41:10 -0400, Mel wrote: > >> Ben Finney wrote: >> >>> I think it would also be better to have One (and prefereably Only One) >>> Obvious Way To Do It. That obvious way, for those who work with >>> Python's ?set? and ?dict?, is a ?clear? method. It seems best to have >>> ?list? conform with this also. >> >> Does that mean a one-off special case rule to forbid slices having a >> default? > > Why would it do that? Well, if list.clear were truly and strictly to be the only way to clear the contents of a list, then assigning nothing via the default slice would have to be ruled out. `somelist[:] = []` is just a special case of assignment to a slice generally. Mel. From gert.cuykens at gmail.com Fri Apr 3 13:21:31 2009 From: gert.cuykens at gmail.com (gert) Date: Fri, 3 Apr 2009 10:21:31 -0700 (PDT) Subject: with open('com1', 'r') as f: References: <8bc55c05-19da-41c4-b916-48e0a4be49b9@p11g2000yqe.googlegroups.com> <20090403002347.163911b4@oxygen> <8de4f974-073f-41cb-9729-341a02ca4c3f@u8g2000yqn.googlegroups.com> Message-ID: <575f77ac-9f14-490b-a165-3ac38127b87a@l38g2000vba.googlegroups.com> On Apr 3, 3:44?pm, "Gabriel Genellina" wrote: > En Thu, 02 Apr 2009 20:04:14 -0300, gert escribi?: > > > On Apr 2, 8:53?pm, Kushal Kumaran wrote: > >> On Thu, 2 Apr 2009 10:01:02 -0700 (PDT) > >> gert wrote: > >> > from subprocess import * > >> > check_call(['mode', 'COM1:9600,N,8,1,P'],shell=True) > >> > while True: > >> > ? ? with open('com1', 'r') as f: > >> > ? ? ? ? for line in f: > >> > ? ? ? ? ? ? ?print(line) > > >> > This works very well except for one thing. After a reboot I have to > >> > launch 1 time any windows serial exe application no mater with one, > >> > that just opens an closes the com port, before i can launch this > >> > script. The script keeps on working even after closing and reopening > >> > it, until i reboot the pc. Then again I have to launch one time a > >> > serial.exe and close it again. The exe does not run anything in the > >> > background it just does something in windows python does not do when > >> > it reads from the com port after a fresh reboot. > > >> > And i really appreciate it if somebody knew what it was. > > >> I don't know why you're getting this behaviour, but have you tried using > >> a python library for accessing the serial port? ? > >> Seehttp://pyserial.wiki.sourceforge.net/pySerial. > > > I am sorry but I don't think pyserial will work on python3.x and I > > also like to know whats going on before I consider it. > > A real Windows program accessing the serial port is likely to use ? > SetupComm, SetCommState, and other functions in addition to CreateFile. > Seehttp://msdn.microsoft.com/en-us/library/aa363196(VS.85).aspx > > pySerial takes care of all those details, as suggested. > > > Maybe its a bug in open() on windows? > > open() doesn't care about the file name; it's the OS that interprets ? > "com1" as a serial port. I do understand, and I went looking into pySerial, but it is a long way from getting compatible with python3.x and involves other libs that are big and non pyhton3.x compatible. Also I can not imaging activating a com port wouldn't be possible with a check_call dos instruction. I can already configure com with mode. From newptcai at gmail.com Fri Apr 3 13:34:34 2009 From: newptcai at gmail.com (=?GB2312?B?0rvK18qr?=) Date: Fri, 3 Apr 2009 10:34:34 -0700 (PDT) Subject: User or UserManager ? Problems of Observer Pattern Message-ID: #This is a real world problem I met. # #We have a database containing serveral tables, like : user, role, organization. Each 2 of them has m:n relationships. These relations are stored in association tables. # #Not we have to load all these data in memory to gain higher performances. We create 3 classes , User, Role, Organization, to hold these data. # #The question is : should I implement "add/delete/update" as methods of User, or should I add another class UserManager? #---------------- # Choice 1 #---------------- user_dict = {} role_dict = {} class User: on_del_funcs = [] roles = [] def del(self): user_dict.pop(self.name) for f in self.on_del_funcs: f(self) # Using Observer Pattern to notify this user is deleted. def addUserDelListener(self, on_del_func): on_del_funcs.append(on_del_funcs) def delUserDelListener(self, on_del_func): on_del_funcs.remove(on_del_func) class Role: users = [] def addUsser(self, user): self.users.append(user) user.roles.append(self) users.addUserDelListener(self.onUserDel) def onUserDel(self, user): self.users.remove(user) user.delUserDelListener(self.onUserDel) #---------------- # Choice 2 #---------------- class UserManager: users = [] @classmethod def delUser(cls, user): cls.users.remove(user) RoleManager.onUserDel(user) class RoleManager: roles = [] @classmethod def onUserDel(cls, user): for r in cls.roles.items(): r.users.remove(user) # These codes are not complete, but that's enough to show my question. # The first choice, which use Observer Pattern, has a very big problem. # When calling addUserDelListener, user got not only a callback function, it # also add reference count of role. So when we want to delete a role. We have # to carefully remove all *listener* hold by other objects, otherwise this role # will never be garbage collected. # Not a big problem for only 2 classes. But when there are 10 *subject* classes which # Role want to *observe*, 10 listeners has to be removed when delete a role. # And if these *subject* have 5 different types of events, then 50 listeners has # to be removed. # Actually I tried this approach once, and it is still a big headache for 10 # programmers. ( The company I worked at that time hate any change to so-called # working code ) # The second choice, which I am using right now, actually has nothing to do with # Object Oriented Designing. The manager classes are not real classes, just # container of methods. These User/Role classes are only container of data, # not behavior. # # For many times, I asked my self, "Is it too hard for me to see the power of # OO, or I have just never met a problem suitable for a OO resolution?" From ben.taylor at email.com Fri Apr 3 13:50:08 2009 From: ben.taylor at email.com (ben.taylor at email.com) Date: Fri, 3 Apr 2009 10:50:08 -0700 (PDT) Subject: Hash of None varies per-machine Message-ID: <4c62537d-583b-4441-89f9-b341247d0b13@l22g2000vba.googlegroups.com> Found this while trying to do something unrelated and was curious... If you hash an integer (eg. hash(3)) you get the same integer out. If you hash a string you also get an integer. If you hash None you get an integer again, but the integer you get varies depending on which machine you're running python on (which isn't true for numbers and strings). This raises the following questions: 1. Is it correct that if you hash two things that are not equal they might give you the same hash value? Like, for instance, None and the number 261862182320 (which is what my machine gives me if I hash None). Note this is just an example, I'm aware hashing integers is probably daft. I'm guessing that's fine, since you can't hash something to a number without colliding with that number (or at least without hashing the number to something else, like hashing every number to itself * 2, which would then mean you couldn't hash very large numbers) 2. Should the hash of None vary per-machine? I can't think why you'd write code that would rely on the value of the hash of None, but you might I guess. 3. Given that presumably not all things can be hashed (since the documentation description of hash() says it gives you the hash of the object "if it can be hashed"), should None be hashable? Bit esoteric perhaps, but like I said, I'm curious. ;-) Ben From aaron.hildebrandt at gmail.com Fri Apr 3 13:50:33 2009 From: aaron.hildebrandt at gmail.com (Aaron Scott) Date: Fri, 3 Apr 2009 10:50:33 -0700 (PDT) Subject: Best way to pickle functions References: <29bfcf8a-984d-47df-a116-7a1f80f171ba@z9g2000yqi.googlegroups.com> Message-ID: > Pickling the source code is much sturdier. ?It's very unlikely that > the same code runs differently in different interpreters. ?It's much > more likely that the same code runs the same, or not at all. Okay, I've run into another problem. I've saved the code to a string, so I can call it up when I need it. I want to keep these functions all together, though, so I'm pushing them into a dictionary when I execute it. It seems like when I put it in a dictionary, though, it messes up the scope of the functions contained within. For example: import cPickle def Main(): holder = {} functiontest = "def PickleTest():\n\tprint cPickle" exec functiontest in holder print holder["PickleTest"]() Main() ... produces: Traceback (most recent call last): File "pickletest.py", line 11, in Main() File "pickletest.py", line 9, in Main print holder["PickleTest"]() File "", line 2, in PickleTest NameError: global name 'cPickle' is not defined Is there any way to do this so that the functions have access to the higher scope? Thanks. From szager at gmail.com Fri Apr 3 13:51:08 2009 From: szager at gmail.com (szager at gmail.com) Date: Fri, 3 Apr 2009 10:51:08 -0700 (PDT) Subject: platform.architecture, __path__, PYTHONPATH, and dist.utils Message-ID: Howdy, I need to support both 32-bit and 64-bit versions of compiled python modules that cannot be installed into the default search path of the python interpreter(s). I use PYTHONPATH to point to the module installations, but I have a problem: I don't know, a priori, whether the user will be running a 32-bit or 64-bit python interpreter. So, I would like to set up a hybrid installation that will be able to load either version, based on the architecture of the currently-running interpreter. ** How can I use dist.utils to create a hybrid installation of an extension module? ** Currently, I'm manually hacking the build results, but my method is very platform-dependent. It would be nicer to have an automated, platform-agnostic way to do this. Here's what I'm doing now: /path/to/foo/32bit/libfoo.so /path/to/foo/64bit/libfoo.so In /path/to/foo/__init__.py: import os import platform # Note that I can't use platform.architecture(), # because that doesn't work when using embedded python exe_arch = platform.architecture('/proc/self/exe') __path__[0] = os.path.join(__path[0]__, exe_arch[0]) from libfoo import * Thanks in advance, Stefan Zager From http Fri Apr 3 13:57:05 2009 From: http (Paul Rubin) Date: 03 Apr 2009 10:57:05 -0700 Subject: Hash of None varies per-machine References: <4c62537d-583b-4441-89f9-b341247d0b13@l22g2000vba.googlegroups.com> Message-ID: <7xfxgp39ji.fsf@ruckus.brouhaha.com> ben.taylor at email.com writes: > 1. Is it correct that if you hash two things that are not equal they > might give you the same hash value? Yes, hashes are 32 bit numbers and there are far more than 2**32 possible Python values (think of long ints), so obviously there must be multiple values that hash to the same slot. > 2. Should the hash of None vary per-machine? If the docs say this shouldn't happen, then it's a bug. Otherwise, it should probably be considered ok. > 3. Given that presumably not all things can be hashed (since the > documentation description of hash() says it gives you the hash of the > object "if it can be hashed"), should None be hashable? Yes, anything that can be used as a dict key (basically all immutable values with equality comparison) should be hashable. From paul at subsignal.org Fri Apr 3 14:02:39 2009 From: paul at subsignal.org (paul) Date: Fri, 03 Apr 2009 20:02:39 +0200 Subject: A design problem I met again and again. In-Reply-To: References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> <33e08fde-1d4e-41a2-9ce2-7e1bec3abd90@r29g2000vbp.googlegroups.com> <793a9568-7c89-412e-b14c-e2dd28640577@s22g2000prg.googlegroups.com> <1b60f999-aaf8-4b10-bd1f-ab46f03f44f6@m36g2000vbp.googlegroups.com> Message-ID: ??? schrieb: > Consolidate existing functions? > > I've thought about it. > > For example, I have two functions: > > #========================= > > def startXXX(id): > pass > > def startYYY(id): > pass > #========================= > > I could turn it into one: > > #========================= > def start(type, id): > if(type == "XXX"): > pass > else if(type == "YYY"): > pass > #========================= > > But isn't the first style more clear for my code's user? Depends ;) There are more ways to structure code than using classes. To avoid the if-elif-elif-elif-else problem you could start using a dispatch table which maps types to functions (fex using a dict) start_methods = { 'type1': startXX, 'type2': startYY, } def start(type, id): func = start_methods.get(type, None) if func: func(id) else: raise ... Or maybe look at trac's (http://trac.edgewall.com) use of Components and Interfaces. Very lightweight and modular. You can start reading here: http://trac.edgewall.org/browser/trunk/trac/core.py cheers Paul > > That's one reason why my interfaces grow fast. > > On Apr 3, 1:51 am, Carl Banks wrote: >> On Apr 2, 8:02 am, ??? wrote: >> >>> You get it. Sometimes I feel that my head is trained to work in a >>> procedural way. I use a big class just as a container of functions. >>> About the "data-based" approach, what if these functions all shares a >>> little data, e.g. a socket, but nothing else? >> Then perhaps your problem is that you are too loose with the >> interface. Do you write new functions that are very similar to >> existing functions all the time? Perhaps you should consolidate, or >> think about how existing functions could do the job. >> >> Or perhaps you don't have a problem. There's nothing wrong with large >> classes per se, it's just a red flag. If you have all these functions >> that really all operate on only one piece of data, and really all do >> different things, then a large class is fine. >> >> Carl Banks > > -- > http://mail.python.org/mailman/listinfo/python-list From aaron.hildebrandt at gmail.com Fri Apr 3 14:04:19 2009 From: aaron.hildebrandt at gmail.com (Aaron Scott) Date: Fri, 3 Apr 2009 11:04:19 -0700 (PDT) Subject: Best way to pickle functions References: <29bfcf8a-984d-47df-a116-7a1f80f171ba@z9g2000yqi.googlegroups.com> Message-ID: <32a17ded-57d5-4d80-8ee3-7444e7991a8b@l22g2000vba.googlegroups.com> Never mind. Solved the problem by putting the functions in a class and dumping that into a string. Then, when I need it, I executed the string to get myself the class, then created an instance of that class which gave me access to those functions along with the correct scope. Probably not the smartest solution, but it works for now. From jrosen at ll.mit.edu Fri Apr 3 14:18:56 2009 From: jrosen at ll.mit.edu (Joshua Judson Rosen) Date: Fri, 03 Apr 2009 14:18:56 -0400 Subject: Hash of None varies per-machine References: <4c62537d-583b-4441-89f9-b341247d0b13@l22g2000vba.googlegroups.com> <7xfxgp39ji.fsf@ruckus.brouhaha.com> Message-ID: <87y6uhob1r.fsf@jrosen-desktop.llan.ll.mit.edu> Paul Rubin writes: > > ben.taylor at email.com writes: > > 1. Is it correct that if you hash two things that are not equal they > > might give you the same hash value? > > Yes, hashes are 32 bit numbers and there are far more than 2**32 > possible Python values (think of long ints), so obviously there must > be multiple values that hash to the same slot. This is not true. CPython integers, at least up through the 2.x series, are implemented as C *long integers*; on some platforms, this means that they're 32 bits long. But on an increasing number of platforms, long integes are 64 bits long. But, more specifically, consider the following: > > 2. Should the hash of None vary per-machine? > > If the docs say this shouldn't happen, then it's a bug. Otherwise, > it should probably be considered ok. > > > 3. Given that presumably not all things can be hashed (since the > > documentation description of hash() says it gives you the hash of the > > object "if it can be hashed"), should None be hashable? > > Yes, anything that can be used as a dict key (basically all immutable > values with equality comparison) should be hashable. My recollection is that what you're seeing here is that, when hash() doesn't have any `proper value' to use other than object-identity, it just returns the result of id(). And id() is documented as: Return the "identity" of an object. This is an integer (or long integer) which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value. (Implementation note: this is the address of the object.) So, not only is the return-value from id() (and hash(), if there's not actually a __hash__ method defined) non-portable between different machines, it's not even necessarily portable between two *runs* on the *same* machine. In practice, your OS will probably start each new process with the same virtual memory-address range, and a given *build* of Python will probably initialise the portion of its memory-segment leading up to the None-object the same way each time, but.... -- Don't be afraid to ask (Lf.((Lx.xx) (Lr.f(rr)))). From davea at dejaviewphoto.com Fri Apr 3 14:21:14 2009 From: davea at dejaviewphoto.com (Dave Angel) Date: Fri, 03 Apr 2009 13:21:14 -0500 Subject: django model problem In-Reply-To: <19a1cxf57zsli.1wmngjrxnlagf$.dlg@40tude.net> References: <19a1cxf57zsli.1wmngjrxnlagf$.dlg@40tude.net> Message-ID: <49D6539A.5020505@dejaviewphoto.com> Mark wrote: > Hi, > > Say I have these simple models: > > class Musician(models.Model): > first_name = models.CharField(max_length=50) > last_name = models.CharField(max_length=50) > > class Album(models.Model): > artist = models.ForeignKey(Musician) > name = models.CharField(max_length=100) > > I think the first thing you need to do is decide if there is going to be more than one Musician object. and more than one Album object. Presently you are giving all musicians the same first_name and last_name. I suggest you look up the documentation for the special method __init__() Then you need to realize that assigning new attributes to an instance object needs to be done inside an instance method, either __init__() or some other method with a self parameter. From bbaja42 at gmail.com Fri Apr 3 14:33:39 2009 From: bbaja42 at gmail.com (barisa) Date: Fri, 3 Apr 2009 11:33:39 -0700 (PDT) Subject: "Pythoner",Wish me luck! References: <54afb932-1a42-42d2-a3b9-0151b2bd410b@q30g2000prq.googlegroups.com> Message-ID: <19963172-7ac1-4796-abb7-3387a65ba91d@f19g2000yqo.googlegroups.com> On Apr 3, 11:39?am, "Hendrik van Rooyen" wrote: > "Matteo" wrote: > > On Apr 3, 9:05 am, Linuxwell wrote: > > >> Starting today I would like to study Python,Wish me luck! > > >Good luck! > > >Don't forget to... > > >>>> print 'Hello World!' > > This is better advice than what you may think, > because the interactive interpreter is your very > best friend when studying the language. > > You get there by typing "python" at the command > line, and pressing enter. > > Using it, you will save yourself many hours of > misunderstanding. > > - Hendrik Hi, I'm also begginer in python; i did few basic programs about graph etc.. my question is : what benefit is using interactive intrepreter ? i come from java backround, so I use eclipse for python as well. I start my program, it does it's job, and that's it. (after some debugging ofc) From james at agentultra.com Fri Apr 3 14:43:56 2009 From: james at agentultra.com (J Kenneth King) Date: Fri, 03 Apr 2009 14:43:56 -0400 Subject: HTML Generation References: <2b54d4370904030004p4b9261dfjdf809bf861fb517e@mail.gmail.com> <874ox56c33.fsf@agentultra.com> <49d62448$0$31338$9b4e6d93@newsspool4.arcor-online.net> Message-ID: <87wsa1h91v.fsf@agentultra.com> Stefan Behnel writes: > J Kenneth King wrote: >> from tags import html, head, meta, title, body, div, p, a >> >> mypage = html( >> head( >> meta(attrs={'http-equiv': "Content-Type", >> 'content': "text/html;"}), >> title("My Page")), >> body(attrs={'background': "#ffffff;"}, >> div(attrs={'id': "article-content"}, >> p(a(attrs={'href': "http://python.org"}, >> "Python.org"))))) >> >> tabbed_file(mypage, open('myfile.html', 'w')) > > See here for another example that uses lxml.html: > > http://codespeak.net/lxml/lxmlhtml.html#creating-html-with-the-e-factory > > Stefan Ah, looks good. Have never used nor finished the example I had given -- only meant as inspiration. I'm not surprised it has been done by someone else. I've been thinking about the co-routines presentation recently given at Pycon and have been thinking about ways they could be used to extend the grammar a bit. At present, it looks like in my example and the lxml.html example that the HTML structures created are declarative. Scripting the generation of child elements for example would break up the flow... code: ------------------------------------------------------------------------ E.HTML( E.HEAD( E.TITLE("Best Page Evar!") ), E.BODY( E.H1(E.CLASS("best-articles-heading"), "Best Articles"), E.UL(E.CLASS("articles")) ) ) for article in articles: E.HTML.BODY.UL.append(E.LI(article['title])) ------------------------------------------------------------------------ ... at least that's how I assume it would work. I think in my prototype you could use list-comprehensions in-line to add the elements, but it was quite an eyesore. code: ------------------------------------------------------------------------ my_html = html( head( title("Best Page Evar!") ), body( ul( [li(article['title']) for article in articles] ) ) ) ------------------------------------------------------------------------ I guess it's not that bad, but I had a feeling it could look prettier like how naturally CL-WHO reads in Lisp. It's just that the list comprehension doesn't read well in this context; it'd be more natural to read it as "for each article in article, create a list element with the article title" instead. I get the impression that creating a chain of co-routines would reveal a grammar for talking about generating web pages. Something like... code: ------------------------------------------------------------------------ html_to_file( html( head( title("Best page evar") ), body( h1({'class': "underline-heading"}, "Newest Articles"), unordered_list( articles, ['title', 'href'], '%-10s: %s') ) ), file=sys.stdout ) ------------------------------------------------------------------------ Where `unordered_list` would walk the elements in its first argument, extract the values from the keys specified by its second argument, and format the results using the string from its third argument and simply return a ul() object with nested li() objects with all the data inserted into them. Of course, this is very off-the-cuff; I only started picking up interest in this old subject this morning. ;) I could be talking way out of my ass here. No idea if any of it's even practical. Anyway -- OP: there are many ways to approach HTML generation and it's a good pursuit. If you come up with something new and unique, please share! Down with templates! :) From python-url at phaseit.net Fri Apr 3 14:44:22 2009 From: python-url at phaseit.net (Gabriel Genellina) Date: Fri, 3 Apr 2009 18:44:22 +0000 (UTC) Subject: Python-URL! - weekly Python news and links (Apr 3) Message-ID: QOTW: "A programmer has to know the name of many data structures." - bearophile Code organization: how to split a project into modules http://groups.google.com/group/comp.lang.python/browse_thread/thread/56c320cea02796cc/ A speech generator, expert in leading-edge Web-based technologies: http://groups.google.com/group/comp.lang.python/browse_thread/thread/cc8013d5399dd65d/ Unladen-Swallow: a new runtime for Python based on LLVM http://groups.google.com/group/comp.lang.python/browse_thread/thread/15f4f9bec82c80b9/ http://groups.google.com/group/comp.lang.python/browse_thread/thread/ec66471ebe5948c6/ Why indexes start at 0, and ranges are semi-open? http://groups.google.com/group/comp.lang.python/browse_thread/thread/ba73814384f7c0ef/ Advanced generator usage, a talk by David Beazley at PyCon2009: http://groups.google.com/group/comp.lang.python/browse_thread/thread/aacd809829d6b6ce/ Mercurial to be the new distributed version control system for Python http://groups.google.com/group/comp.lang.python/browse_thread/thread/1f99be59ea13bd22/ How to split a string at commas respecting quotes http://groups.google.com/group/comp.lang.python/browse_thread/thread/aacac2d2a5c192e9/ Some tkinter design questions: why things are the way they are? http://groups.google.com/group/comp.lang.python/browse_thread/thread/75d3ce79c919b76d/ PEP 382: A proposal to add namespace packages http://groups.google.com/group/comp.lang.python/browse_thread/thread/aebfb3311a2f5eb6/ Still discussing an ordered set implementation http://groups.google.com/group/comp.lang.python/browse_thread/thread/90d818e016632a2c/ ======================================================================== 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 enthusiats": 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/ The somewhat older Vaults of Parnassus ambitiously collects references to all sorts of Python resources. http://www.vex.net/~x/parnassus/ 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 *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 martin at v.loewis.de Fri Apr 3 14:46:57 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Fri, 03 Apr 2009 20:46:57 +0200 Subject: minor revision encoded in SONAME in libpython.so In-Reply-To: References: <49C94F53.6040102@v.loewis.de> Message-ID: <49d659a1$0$2454$9b622d9e@news.freenet.de> >> So no, no minor revision gets encoded into the SONAME. > > Then what's the significance of the .1.0 at the end of the SONAME? Is > it just nipples for men? (I hope no one objects to my extending the > Monty Python theme to Time Bandits). Some systems require that shared libraries have a version in their SONAME, so we provide a version. Regards, Martin From davea at dejaviewphoto.com Fri Apr 3 14:52:54 2009 From: davea at dejaviewphoto.com (Dave Angel) Date: Fri, 03 Apr 2009 13:52:54 -0500 Subject: Best way to pickle functions In-Reply-To: References: <29bfcf8a-984d-47df-a116-7a1f80f171ba@z9g2000yqi.googlegroups.com> Message-ID: <49D65B06.90102@dejaviewphoto.com> Aaron Scott wrote: >> Pickling the source code is much sturdier. It's very unlikely that >> the same code runs differently in different interpreters. It's much >> more likely that the same code runs the same, or not at all. >> > > Okay, I've run into another problem. I've saved the code to a string, > so I can call it up when I need it. I want to keep these functions all > together, though, so I'm pushing them into a dictionary when I execute > it. It seems like when I put it in a dictionary, though, it messes up > the scope of the functions contained within. For example: > > import cPickle > def Main(): > holder =} > functiontest =def PickleTest():\n\tprint cPickle" > exec functiontest in holder > print holder["PickleTest"]() > Main() > > ... produces: > > Traceback (most recent call last): > File "pickletest.py", line 11, in > Main() > File "pickletest.py", line 9, in Main > print holder["PickleTest"]() > File "", line 2, in PickleTest > NameError: global name 'cPickle' is not defined > > Is there any way to do this so that the functions have access to the > higher scope? > > Thanks. > > Why not use import ? Simply recreate the source file, if necessary, and import it again. If you must import without it being in a clear text file, check out the deprecated imputil module, standard in 2.x, but removed in Python 3.0. And if you find some way to get __import__ or one of its relatives to work from a stream instead of a file, please let us know. From martin at v.loewis.de Fri Apr 3 14:54:26 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Fri, 03 Apr 2009 20:54:26 +0200 Subject: Python Goes Mercurial In-Reply-To: References: Message-ID: <49d65b62$0$30904$9b622d9e@news.freenet.de> >>>> So what were these "strong antipathies" towards Git, exactly? >>> i haven't read the article you link to, but compared to what i've read >>> on >>> dev "strong antipathies" sounds a bit over-hyped. >> That was the phrase used by GvR. > > well if you find any, please do report back. I don't like git because it is too difficult for me. In many cases, git would refuse to do operations like updating or local committing, producing error messages I was not able to understand (most of the time including long sequences of hexdigits which I did not recognize, or complaining about conflicts in long lists of files I knew I hadn't touched at all). In some cases, I lost all my changes, and had to do them all over again. Regards, Martin From nrballard at gmail.com Fri Apr 3 14:58:27 2009 From: nrballard at gmail.com (nrballard at gmail.com) Date: Fri, 3 Apr 2009 11:58:27 -0700 (PDT) Subject: "Pythoner",Wish me luck! References: <54afb932-1a42-42d2-a3b9-0151b2bd410b@q30g2000prq.googlegroups.com> <19963172-7ac1-4796-abb7-3387a65ba91d@f19g2000yqo.googlegroups.com> Message-ID: <3a4f64da-561f-40e5-9c19-7d15c02fe94d@d25g2000prn.googlegroups.com> On Apr 3, 12:33?pm, barisa wrote: > On Apr 3, 11:39?am, "Hendrik van Rooyen" wrote: > > > > > "Matteo" wrote: > > > On Apr 3, 9:05 am, Linuxwell wrote: > > > >> Starting today I would like to study Python,Wish me luck! > > > >Good luck! > > > >Don't forget to... > > > >>>> print 'Hello World!' > > > This is better advice than what you may think, > > because the interactive interpreter is your very > > best friend when studying the language. > > > You get there by typing "python" at the command > > line, and pressing enter. > > > Using it, you will save yourself many hours of > > misunderstanding. > > > - Hendrik > > Hi, > I'm also begginer in python; > i did few basic programs about graph etc.. > > my question is : what benefit is using interactive intrepreter ? > > i come from java backround, so I use eclipse for python as well. > I start my program, it does it's job, and that's it. ?(after some > debugging ofc) I'm also a beginner in Python, but from my own experience the interactive interpreter is great for experimenting with new modules and output formatting because it allows you to see the immediate output of a function before you write it into your program. The immediate result is that you'll see any errors and be able to fix them before they end up in your script. Nick Ballard http://90daysofpython.blogspot.com From ra21vi at gmail.com Fri Apr 3 15:01:14 2009 From: ra21vi at gmail.com (Ravi Kumar) Date: Sat, 4 Apr 2009 00:31:14 +0530 Subject: "Pythoner",Wish me luck! In-Reply-To: <19963172-7ac1-4796-abb7-3387a65ba91d@f19g2000yqo.googlegroups.com> References: <54afb932-1a42-42d2-a3b9-0151b2bd410b@q30g2000prq.googlegroups.com> <19963172-7ac1-4796-abb7-3387a65ba91d@f19g2000yqo.googlegroups.com> Message-ID: <9a63e8920904031201qa65e54exe26c053d973566f@mail.gmail.com> > > Hi, > I'm also begginer in python; > i did few basic programs about graph etc.. > > my question is : what benefit is using interactive intrepreter ? > > the IDLE interractive python IDE is best comparing to any other most advanced IDE available in world. Really, if you are learning the python at any level, go for IDLE. What you do, you write every step of statements and that is executed as soon as you finish the block. So you have chance to understand the things step by step. Just like learning by debugging.. Try it self, no one's review can satisfy you. -- -=Ravi=- -------------- next part -------------- An HTML attachment was scrubbed... URL: From davidj411 at gmail.com Fri Apr 3 15:03:23 2009 From: davidj411 at gmail.com (davidj411) Date: Fri, 3 Apr 2009 12:03:23 -0700 (PDT) Subject: IIS python web application mapping issue - resolved Message-ID: <58a2e78f-853c-4e4e-b965-116d92841e51@b16g2000yqb.googlegroups.com> i searched the internet for an hour , never found this info, and figured it out myself. posting this so that others won't have to look so hard. ran across this issue and it seems that nobody really documented this correctly on http://support.microsoft.com/kb/276494 in IIS i could not add the "python mapping" to the web mappings like this: "c:\\python.exe -u %s %s" windows said the file does not exist, even when i unchecked the option to make sure it exists (and it does exist!). it would not allow the arguments, i.e. it allowed the mapping when i just used "c:\\python.exe". solution was to do it like this: c:\\python.exe -u "%s %s" making the arguments have there own set of quotes. remember to also do the following have web service extensions enabled for cgi. register python using pyscript.py. thanks! David From davea at ieee.org Fri Apr 3 15:09:26 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 03 Apr 2009 14:09:26 -0500 Subject: Hash of None varies per-machine In-Reply-To: <4c62537d-583b-4441-89f9-b341247d0b13@l22g2000vba.googlegroups.com> References: <4c62537d-583b-4441-89f9-b341247d0b13@l22g2000vba.googlegroups.com> Message-ID: <49D65EE6.2010605@ieee.org> ben.taylor at email.com wrote: > Found this while trying to do something unrelated and was curious... > > If you hash an integer (eg. hash(3)) you get the same integer out. If > you hash a string you also get an integer. If you hash None you get an > integer again, but the integer you get varies depending on which > machine you're running python on (which isn't true for numbers and > strings). > > This raises the following questions: > 1. Is it correct that if you hash two things that are not equal they > might give you the same hash value? Like, for instance, None and the > number 261862182320 (which is what my machine gives me if I hash > None). Note this is just an example, I'm aware hashing integers is > probably daft. I'm guessing that's fine, since you can't hash > something to a number without colliding with that number (or at least > without hashing the number to something else, like hashing every > number to itself * 2, which would then mean you couldn't hash very > large numbers) > 2. Should the hash of None vary per-machine? I can't think why you'd > write code that would rely on the value of the hash of None, but you > might I guess. > 3. Given that presumably not all things can be hashed (since the > documentation description of hash() says it gives you the hash of the > object "if it can be hashed"), should None be hashable? > > Bit esoteric perhaps, but like I said, I'm curious. ;-) > > Ben > > 1. Most definitely. Every definition of hash (except for "perfect hash") makes it a many-to-one mapping. Its only intent is to reduce the likelihood of collision between dissimilar objects. And Python's spec that says that integers, longs and floats that are equal are guaranteed the same hash value is a new one for me. Thanks for making me look it up. 2. Nothing guarantees that the Python hash() will return the same value for the same object between implementations, or even between multiple runs with the same version on the same machine. In fact, the default hash for user-defined classes is the id() of the object, which will definitely vary between program runs. Currently, id() is implemented to just return the address of the object. 3. Normally, it's just mutable objects that are unhashable. Since None is definitely immutable, it should have a hash. Besides, if it weren't hashable, it couldn't be usable as a key in a dictionary. All my opinions, of course. DaveA From bbaja42 at gmail.com Fri Apr 3 15:47:05 2009 From: bbaja42 at gmail.com (barisa) Date: Fri, 3 Apr 2009 12:47:05 -0700 (PDT) Subject: "Pythoner",Wish me luck! References: <54afb932-1a42-42d2-a3b9-0151b2bd410b@q30g2000prq.googlegroups.com> <19963172-7ac1-4796-abb7-3387a65ba91d@f19g2000yqo.googlegroups.com> <3a4f64da-561f-40e5-9c19-7d15c02fe94d@d25g2000prn.googlegroups.com> Message-ID: <0108c1c8-f7d2-4ff5-9570-18126cf4e17d@e38g2000yqa.googlegroups.com> On Apr 3, 8:58?pm, nrball... at gmail.com wrote: > On Apr 3, 12:33?pm, barisa wrote: > > > > > On Apr 3, 11:39?am, "Hendrik van Rooyen" wrote: > > > > "Matteo" wrote: > > > > On Apr 3, 9:05 am, Linuxwell wrote: > > > > >> Starting today I would like to study Python,Wish me luck! > > > > >Good luck! > > > > >Don't forget to... > > > > >>>> print 'Hello World!' > > > > This is better advice than what you may think, > > > because the interactive interpreter is your very > > > best friend when studying the language. > > > > You get there by typing "python" at the command > > > line, and pressing enter. > > > > Using it, you will save yourself many hours of > > > misunderstanding. > > > > - Hendrik > > > Hi, > > I'm also begginer in python; > > i did few basic programs about graph etc.. > > > my question is : what benefit is using interactive intrepreter ? > > > i come from java backround, so I use eclipse for python as well. > > I start my program, it does it's job, and that's it. ?(after some > > debugging ofc) > > I'm also a beginner in Python, but from my own experience the > interactive interpreter is great for experimenting with new modules > and output formatting because it allows you to see the immediate > output of a function before you write it into your program. ?The > immediate result is that you'll see any errors and be able to fix them > before they end up in your script. > > Nick Ballardhttp://90daysofpython.blogspot.com thanks, i'll give it a try From martin at v.loewis.de Fri Apr 3 15:49:58 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Fri, 03 Apr 2009 21:49:58 +0200 Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: <20090402171218.9DDEF3A40A7@sparrow.telecommunity.com> References: <49D4DA72.60401@v.loewis.de> <20090402171218.9DDEF3A40A7@sparrow.telecommunity.com> Message-ID: <49D66866.5020505@v.loewis.de> > Perhaps we could add something like a sys.namespace_packages that would > be updated by this mechanism? Then, pkg_resources could check both that > and its internal registry to be both backward and forward compatible. I could see no problem with that, so I have added this to the PEP. Thanks for the feedback, Martin From hackingkk at gmail.com Fri Apr 3 15:53:16 2009 From: hackingkk at gmail.com (Krishnakant) Date: Sat, 04 Apr 2009 01:23:16 +0530 Subject: confused with creating doctest in xmlrpc server In-Reply-To: <9a63e8920904031201qa65e54exe26c053d973566f@mail.gmail.com> References: <54afb932-1a42-42d2-a3b9-0151b2bd410b@q30g2000prq.googlegroups.com> <19963172-7ac1-4796-abb7-3387a65ba91d@f19g2000yqo.googlegroups.com> <9a63e8920904031201qa65e54exe26c053d973566f@mail.gmail.com> Message-ID: <1238788396.7253.6.camel@kk-laptop> hello all, I am thinking of using the doctest module for my unit testing code in python. I have no problems doing this in usual classes but I am a bit confused with my twisted based rpc classes. given that I directly take the output of running functions on a python prompt for the dockstrings, how do I get them for my twisted class which has a published object? What I mean is that in normal classes I would just start the python prompt, import the module, create the object and run the methods to get the output. Then I take the output and put it into a file and then use those dockstrings for my tests. As you all know an rpc server app can't be run like this. To my knowledge an rpc server is a service that listens on a port on the given ip address. So how do I extract the dockstrings from the functions inside my xmlrpc class? obviously it is not run on a python prompt, so what is the solution? happy hacking. Krishnakant. From martin at v.loewis.de Fri Apr 3 15:55:22 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Fri, 03 Apr 2009 21:55:22 +0200 Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: <49D51A16.70804@simplistix.co.uk> References: <49D4DA72.60401@v.loewis.de> <49D51A16.70804@simplistix.co.uk> Message-ID: <49D669AA.6080001@v.loewis.de> Chris Withers wrote: > Martin v. L?wis wrote: >> I propose the following PEP for inclusion to Python 3.1. >> Please comment. > > Would this support the following case: > > I have a package called mortar, which defines useful stuff: > > from mortar import content, ... > > I now want to distribute large optional chunks separately, but ideally > so that the following will will work: > > from mortar.rbd import ... > from mortar.zodb import ... > from mortar.wsgi import ... > > Does the PEP support this? That's the primary purpose of the PEP. You can do this today already (see the zope package, and the reference to current techniques in the PEP), but the PEP provides a cleaner way. In each chunk (which the PEP calls portion), you had a structure like this: mortar/ mortar/rbd.pkg (contains just "*") mortar/rbd.py or mortar/ mortar/zobd.pkg mortar/zobd/ mortar/zobd/__init__.py mortar/zobd/backends.py As a site effect, you can also do "import mortar", but that would just give you the (nearly) empty namespace package, whose only significant contents is the variable __path__. Regards, Martin From ma.ston.99 at hotmail.com Fri Apr 3 16:03:57 2009 From: ma.ston.99 at hotmail.com (Mark) Date: Fri, 3 Apr 2009 22:03:57 +0200 Subject: django model problem References: <19a1cxf57zsli.1wmngjrxnlagf$.dlg@40tude.net> Message-ID: <2jd2j8x0sir0.1jje7ogy6pgpd.dlg@40tude.net> > I think the first thing you need to do is decide if there is going to be > more than one Musician object. and more than one Album object. > Presently you are giving all musicians the same first_name and > last_name. I suggest you look up the documentation for the special > method __init__() > > Then you need to realize that assigning new attributes to an instance > object needs to be done inside an instance method, either __init__() or > some other method with a self parameter. What you say would normally make sense, but it's Django and it works that way here. See: http://docs.djangoproject.com/en/dev/topics/db/models/#fields Regards MS From martin at v.loewis.de Fri Apr 3 16:07:10 2009 From: martin at v.loewis.de (=?ISO-8859-15?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Fri, 03 Apr 2009 22:07:10 +0200 Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: <49D52115.6020001@egenix.com> References: <49D4DA72.60401@v.loewis.de> <49D52115.6020001@egenix.com> Message-ID: <49D66C6E.3090602@v.loewis.de> > I'd like to extend the proposal to Python 2.7 and later. I don't object, but I also don't want to propose this, so I added it to the discussion. My (and perhaps other people's) concern is that 2.7 might well be the last release of the 2.x series. If so, adding this feature to it would make 2.7 an odd special case for users and providers of third party tools. > That's going to slow down Python package detection a lot - you'd > replace an O(1) test with an O(n) scan. I question that claim. In traditional Unix systems, the file system driver performs a linear search of the directory, so it's rather O(n)-in-kernel vs. O(n)-in-Python. Even for advanced file systems, you need at least O(log n) to determine whether a specific file is in a directory. For all practical purposes, the package directory will fit in a single disk block (containing a single .pkg file, and one or few subpackages), making listdir complete as fast as stat. > Wouldn't it be better to stick with a simpler approach and look for > "__pkg__.py" files to detect namespace packages using that O(1) check ? Again - this wouldn't be O(1). More importantly, it breaks system packages, which now again have to deal with the conflicting file names if they want to install all portions into a single location. > This would also avoid any issues you'd otherwise run into if you want > to maintain this scheme in an importer that doesn't have access to a list > of files in a package directory, but is well capable for the checking > the existence of a file. Do you have a specific mechanism in mind? Regards, Martin From lists at cheimes.de Fri Apr 3 16:07:52 2009 From: lists at cheimes.de (Christian Heimes) Date: Fri, 03 Apr 2009 22:07:52 +0200 Subject: Hash of None varies per-machine In-Reply-To: <7xfxgp39ji.fsf@ruckus.brouhaha.com> References: <4c62537d-583b-4441-89f9-b341247d0b13@l22g2000vba.googlegroups.com> <7xfxgp39ji.fsf@ruckus.brouhaha.com> Message-ID: Paul Rubin wrote: > Yes, hashes are 32 bit numbers and there are far more than 2**32 > possible Python values (think of long ints), so obviously there must > be multiple values that hash to the same slot. No, hashs are C longs. On most 64bit platforms a C long has 64bits. As far as I know only 64bit Windows has a 32bit long type. > If the docs say this shouldn't happen, then it's a bug. Otherwise, > it should probably be considered ok. Can you show me the exact place in the docs? The NoneType code uses _Py_HashPointer which returns the address of the PyObject* as hash. The value may vary between platforms and compilers. > Yes, anything that can be used as a dict key (basically all immutable > values with equality comparison) should be hashable. The __eq__ equality method is not related to the hash functions. But __eq__ is very import to resolve hash collisions. Christian From lists at cheimes.de Fri Apr 3 16:10:36 2009 From: lists at cheimes.de (Christian Heimes) Date: Fri, 03 Apr 2009 22:10:36 +0200 Subject: with open('com1', 'r') as f: In-Reply-To: <575f77ac-9f14-490b-a165-3ac38127b87a@l38g2000vba.googlegroups.com> References: <8bc55c05-19da-41c4-b916-48e0a4be49b9@p11g2000yqe.googlegroups.com> <20090403002347.163911b4@oxygen> <8de4f974-073f-41cb-9729-341a02ca4c3f@u8g2000yqn.googlegroups.com> <575f77ac-9f14-490b-a165-3ac38127b87a@l38g2000vba.googlegroups.com> Message-ID: gert wrote: > I do understand, and I went looking into pySerial, but it is a long > way from getting compatible with python3.x and involves other libs > that are big and non pyhton3.x compatible. So don't use Python 3.0. Most people are still using Python 2.5 or 2.6. Christian From martin at v.loewis.de Fri Apr 3 16:15:55 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Fri, 03 Apr 2009 22:15:55 +0200 Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: <20090403004135.B76443A40A7@sparrow.telecommunity.com> References: <49D4DA72.60401@v.loewis.de> <49D52115.6020001@egenix.com> <20090403004135.B76443A40A7@sparrow.telecommunity.com> Message-ID: <49D66E7B.9080304@v.loewis.de> > Note that there is no such thing as a "defining namespace package" -- > namespace package contents are symmetrical peers. With the PEP, a "defining package" becomes possible - at most one portion can define an __init__.py. I know that the current mechanisms don't support it, and it might not be useful in general, but now there is a clean way of doing it, so I wouldn't exclude it. Distribution-wise, all distributions relying on the defining package would need to require (or install_require, or depend on) it. > The above are also true for using only a '*' in .pkg files -- in that > event there are no sys.path changes. (Frankly, I'm doubtful that > anybody is using extend_path and .pkg files to begin with, so I'd be > fine with a proposal that instead used something like '.nsp' files that > didn't even need to be opened and read -- which would let the directory > scan stop at the first .nsp file found. That would work for me as well. Nobody at PyCon could remember where .pkg files came from. > I believe the PEP does this as well, IIUC. Correct. >> * It's possible to have a defining package dir and add-one package >> dirs. > > Also possible in the PEP, although the __init__.py must be in the first > such directory on sys.path. I should make it clear that this is not the case. I envision it to work this way: import zope - searches sys.path, until finding either a directory zope, or a file zope.{py,pyc,pyd,...} - if it is a directory, it checks for .pkg files. If it finds any, it processes them, extending __path__. - it *then* checks for __init__.py, taking the first hit anywhere on __path__ (just like any module import would) - if no .pkg was found, nor an __init__.py, it proceeds with the next sys.path item (skipping the directory entirely) Regards, Martin From aaron.hildebrandt at gmail.com Fri Apr 3 16:48:17 2009 From: aaron.hildebrandt at gmail.com (Aaron Scott) Date: Fri, 3 Apr 2009 13:48:17 -0700 (PDT) Subject: Best way to pickle functions References: <29bfcf8a-984d-47df-a116-7a1f80f171ba@z9g2000yqi.googlegroups.com> Message-ID: <5fce0034-3598-4d8a-9a2d-9832e8bea5ba@j39g2000yqn.googlegroups.com> > Why not use import ? ?Simply recreate the source file, if necessary, and > import it again. > Ah, you'd think it would be that easy :P The problem with just importing a module is that the module is then cached in memory. Multiple copies of the program are running on a server, and each of them have something akin to a "randomfunctions" module. When the first program is accessed, it loads "randomfunctions". When the second program is accessed, it uses the "randomfunctions" module already in memory, even though it doesn't contain the right functions. So, I have to pull in these functions dynamically. From torriem at gmail.com Fri Apr 3 16:58:28 2009 From: torriem at gmail.com (Michael Torrie) Date: Fri, 03 Apr 2009 14:58:28 -0600 Subject: is there a way to collect twitts with python? In-Reply-To: <1078018b0903271054y3d4a7162w885c61bf63bc2048@mail.gmail.com> References: <1078018b0903271054y3d4a7162w885c61bf63bc2048@mail.gmail.com> Message-ID: <49D67874.8080209@gmail.com> '2+ wrote: > i found a guy twittin supercollider code > this means his followers can listen to a noiz by activating that 1 line > (well if he has sc installed) > if lots of sc users start twittin ... it would be no good to follow each > > collecting a sc related twitt can be done with python? > if there's a lib already any good pointers to start learnin thangs at? > > maybe someday > jython or pyjamas can be used to launch a > sctwitt strreaming radio? > (this should be the one listeners can mix his favorite sctwittists) Oh wow. If this is what Twitter does to one's ability to articulate clearly, I hope Twitter dies a horrible death and any APIs and Python bindings with it! From hirudo_1 at hotmail.com Fri Apr 3 17:03:57 2009 From: hirudo_1 at hotmail.com (hirudo #1) Date: Fri, 3 Apr 2009 23:03:57 +0200 Subject: SFTP libraries in pure Python? Message-ID: Hi, I am looking for SFTP libraries that are written in pure Python. I already checked out paramiko, but as far as I can see, it requires pycrypto, which is not pure Python. Another candidate, Twisted, isn't pure Python either. I don't really care about speed as much as about portability. I'm hoping for some pointers here. -Peter- _________________________________________________________________ What can you do with the new Windows Live? Find out http://www.microsoft.com/windows/windowslive/default.aspx -------------- next part -------------- An HTML attachment was scrubbed... URL: From glyph at divmod.com Fri Apr 3 17:16:49 2009 From: glyph at divmod.com (glyph at divmod.com) Date: Fri, 03 Apr 2009 21:16:49 -0000 Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: <49D66E7B.9080304@v.loewis.de> References: <49D4DA72.60401@v.loewis.de> <49D52115.6020001@egenix.com> <20090403004135.B76443A40A7@sparrow.telecommunity.com> <49D66E7B.9080304@v.loewis.de> Message-ID: <20090403211649.12555.1005832716.divmod.xquotient.6954@weber.divmod.com> On 08:15 pm, martin at v.loewis.de wrote: >>Note that there is no such thing as a "defining namespace package" -- >>namespace package contents are symmetrical peers. > >With the PEP, a "defining package" becomes possible - at most one >portion can define an __init__.py. For what it's worth, this is a _super_ useful feature for Twisted. We have one "defining package" for the "twisted" package (twisted core) and then a bunch of other things which want to put things into twisted.* (twisted.web, twisted.conch, et. al.). For debian we already have separate packages, but such a definition of namespace packages would allow us to actually have things separated out on the cheeseshop as well. From pje at telecommunity.com Fri Apr 3 17:23:19 2009 From: pje at telecommunity.com (P.J. Eby) Date: Fri, 03 Apr 2009 17:23:19 -0400 Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: <49D66E7B.9080304@v.loewis.de> References: <49D4DA72.60401@v.loewis.de> <49D52115.6020001@egenix.com> <20090403004135.B76443A40A7@sparrow.telecommunity.com> <49D66E7B.9080304@v.loewis.de> Message-ID: <20090403212054.D15F73A40A7@sparrow.telecommunity.com> At 10:15 PM 4/3/2009 +0200, Martin v. L?wis wrote: >I should make it clear that this is not the case. I envision it to work >this way: import zope >- searches sys.path, until finding either a directory zope, or a file > zope.{py,pyc,pyd,...} >- if it is a directory, it checks for .pkg files. If it finds any, > it processes them, extending __path__. >- it *then* checks for __init__.py, taking the first hit anywhere > on __path__ (just like any module import would) >- if no .pkg was found, nor an __init__.py, it proceeds with the next > sys.path item (skipping the directory entirely) Ah, I missed that. Maybe the above should be added to the PEP to clarify. From aaron.hildebrandt at gmail.com Fri Apr 3 17:31:22 2009 From: aaron.hildebrandt at gmail.com (Aaron Scott) Date: Fri, 3 Apr 2009 14:31:22 -0700 (PDT) Subject: Module caching Message-ID: <8350f62e-c620-4abc-90dc-ef3af60494f6@3g2000yqk.googlegroups.com> Is there a way to make a Python app running in mod_python with zero persistence? I have an app that should be resetting its variables every time you access it, but sometimes -- and only sometimes -- the variables persist through a couple refreshes. They'll even persist through multiple browsers, so I know it's a Python issue and not a browser caching issue. Any assistance would be appreciated. From davidj411 at gmail.com Fri Apr 3 17:32:11 2009 From: davidj411 at gmail.com (davidj411) Date: Fri, 3 Apr 2009 14:32:11 -0700 (PDT) Subject: IIS python web application mapping issue - resolved References: <58a2e78f-853c-4e4e-b965-116d92841e51@b16g2000yqb.googlegroups.com> Message-ID: I thought i was being clever but not only did i typo , but it does not work with the "-u" for unbuffered option. remove the "-u" to avoid the ugly message: CGI Error The specified CGI application misbehaved by not returning a complete set of HTTP headers. I am going to use the CGI script to upload files (.exe and .zip files) and had read that "-u" would be needed. i am going to need to research how this is done now that "-u" is a known issue with IIS. i think that i will need to baseencode64 the data before writing it to a file. not sure really... any thoughts are appreciated. From tim.wintle at teamrubber.com Fri Apr 3 17:42:33 2009 From: tim.wintle at teamrubber.com (Tim Wintle) Date: Fri, 03 Apr 2009 22:42:33 +0100 Subject: python needs leaning stuff from other language In-Reply-To: References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <73kt5jFvitcdU1@mid.uni-berlin.de> <49D54209.5090306@hotmail.com> <01e56a2f$0$20654$c3e8da3@news.astraweb.com> <87y6uie9ej.fsf@benfinney.id.au> <01e624b7$0$20654$c3e8da3@news.astraweb.com> Message-ID: <1238794953.8087.1.camel@tim-laptop> On Fri, 2009-04-03 at 13:12 -0400, Mel wrote: > >>> I think it would also be better to have One (and prefereably Only One) > >>> Obvious Way To Do It. That obvious way, for those who work with > >>> Python's ?set? and ?dict?, is a ?clear? method. It seems best to have > >>> ?list? conform with this also. > >> > >> Does that mean a one-off special case rule to forbid slices having a > >> default? > > > > Why would it do that? > > Well, if list.clear were truly and strictly to be the only way to clear the > contents of a list, then assigning nothing via the default slice would have > to be ruled out. `somelist[:] = []` is just a special case of assignment to > a slice generally. agreed. If .clear was to be added then really assignments to slices should be entirely removed. Tim W From sloisel at gmail.com Fri Apr 3 18:10:32 2009 From: sloisel at gmail.com (sloisel) Date: Fri, 3 Apr 2009 15:10:32 -0700 (PDT) Subject: Let-expressions Message-ID: Dear All, I searched this group and found that there have been discussions about introducing a let expression to the language so that you can define local variables in a lambda. I.e., something like f=lambda x: let y=x^2 in sin(y). (My syntax is unpythonic, but I hope you get it). Can someone tell me what eventually happened? Thanks, Seb From aaron.hildebrandt at gmail.com Fri Apr 3 18:25:52 2009 From: aaron.hildebrandt at gmail.com (Aaron Scott) Date: Fri, 3 Apr 2009 15:25:52 -0700 (PDT) Subject: Module caching References: <8350f62e-c620-4abc-90dc-ef3af60494f6@3g2000yqk.googlegroups.com> Message-ID: <3a0704ea-703a-445f-804d-73f363ca9376@c11g2000yqj.googlegroups.com> Okay, I'm at my wit's end. I have a Python app, running via mod_python. There are variables in this app that, when changed, save their changes to a pickled file tied to a session ID. Then, when the page is accessed again, the variables are loaded from the respective file. But, when one user uses the page and a number of variables are changed, these changes persist, even if I try to load the saved values over them. So, if my Python file has a value of "5", the "custom values" file has a value of "10", but a user does something that changes the variable to "20", the next user who accesses the page with see the value as "20", even if their "custom values" file tries to set it differently. If anyone has experience with mod_python, either drop me a message here or e-mail me at aaron(at)manlab.com. I'd really appreciate some help with this. From aaron.hildebrandt at gmail.com Fri Apr 3 18:44:53 2009 From: aaron.hildebrandt at gmail.com (Aaron Scott) Date: Fri, 3 Apr 2009 15:44:53 -0700 (PDT) Subject: Module caching References: <8350f62e-c620-4abc-90dc-ef3af60494f6@3g2000yqk.googlegroups.com> <3a0704ea-703a-445f-804d-73f363ca9376@c11g2000yqj.googlegroups.com> Message-ID: Huzzah, another post. I just discovered that even physically deleting the variable doesn't work. The module storylab.game has the class InitGame, which contains "daemons = {}". A user runs the code, resulting in some values in "daemons": "{'berry2': , 'berry3': , 'berry1': }". These are pickled. The next user runs the code. I put this in to make sure "daemons" is getting reset: req.write(str(lab.game.settings.daemons)) del lab.game.settings req.write(str(lab.game.settings.daemons)) lab.game.settings = lab.game.InitGame() req.write(str(lab.game.settings.daemons)) Okay, that should wipe out any of the values and leave us with a clean slate, right? {'berry2': , 'berry3': , 'berry1': }failed{'berry2': , 'berry3': , 'berry1': } Oh, you'd be so lucky. Why? WHY? Why does these values persist? They persist if I change them, they persist if I delete them. Help... please :( From aaron.hildebrandt at gmail.com Fri Apr 3 18:50:49 2009 From: aaron.hildebrandt at gmail.com (Aaron Scott) Date: Fri, 3 Apr 2009 15:50:49 -0700 (PDT) Subject: Module caching References: <8350f62e-c620-4abc-90dc-ef3af60494f6@3g2000yqk.googlegroups.com> <3a0704ea-703a-445f-804d-73f363ca9376@c11g2000yqj.googlegroups.com> Message-ID: <14752d03-3308-4672-bcfd-c9b37c9e8285@j39g2000yqn.googlegroups.com> > ? ? ? ? req.write(str(lab.game.settings.daemons)) > ? ? ? ? del lab.game.settings > ? ? ? ? req.write(str(lab.game.settings.daemons)) > ? ? ? ? lab.game.settings = lab.game.InitGame() > ? ? ? ? req.write(str(lab.game.settings.daemons)) > Sorry, that should have been: req.write(str(lab.game.game.daemons)) del lab.game.game try: req.write(str(lab.game.game.daemons)) except: req.write("failed") lab.game.game = lab.game.InitGame() req.write(str(lab.game.game.daemons)) From andrew at acooke.org Fri Apr 3 18:50:51 2009 From: andrew at acooke.org (andrew cooke) Date: Fri, 3 Apr 2009 18:50:51 -0400 (CLT) Subject: Module caching In-Reply-To: References: <8350f62e-c620-4abc-90dc-ef3af60494f6@3g2000yqk.googlegroups.com> <3a0704ea-703a-445f-804d-73f363ca9376@c11g2000yqj.googlegroups.com> Message-ID: <51abf722a16891cc981c8be69fc2683a.squirrel@localhost> are you an experienced python programmer? a lot of newbies post here with problems related to unexpected results because they make "the usual" mistakes about list mutability and default function arguments. i suspect that's not the case here, but it seemed worth mentioning, just in case. andrew Aaron Scott wrote: > Huzzah, another post. > > I just discovered that even physically deleting the variable doesn't > work. > > The module storylab.game has the class InitGame, which contains > "daemons = {}". > > A user runs the code, resulting in some values in "daemons": > "{'berry2': , 'berry3': , 'berry1': }". These are pickled. > > The next user runs the code. I put this in to make sure "daemons" is > getting reset: > > req.write(str(lab.game.settings.daemons)) > del lab.game.settings > req.write(str(lab.game.settings.daemons)) > lab.game.settings = lab.game.InitGame() > req.write(str(lab.game.settings.daemons)) > > Okay, that should wipe out any of the values and leave us with a clean > slate, right? > > {'berry2': , 'berry3': , 'berry1': }failed{'berry2': , 'berry3': , > 'berry1': } > > Oh, you'd be so lucky. > > Why? WHY? Why does these values persist? They persist if I change > them, they persist if I delete them. > > Help... please :( > -- > http://mail.python.org/mailman/listinfo/python-list > > From piet at cs.uu.nl Fri Apr 3 18:52:06 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sat, 04 Apr 2009 00:52:06 +0200 Subject: Class methods read-only by default? References: <7497f42a-46f0-46ce-9035-087da489205c@l10g2000vba.googlegroups.com> Message-ID: >>>>> "Emanuele D'Arrigo" (ED) wrote: >ED> Hi Everybody! >ED> I just tried this: >>>>> class C(object): >ED> ... def method(self): >ED> ... pass >ED> ... >>>>> c = C() >>>>> delattr(c, "method") >ED> Traceback (most recent call last): >ED> File "", line 1, in >ED> AttributeError: 'C' object attribute 'method' is read-only >ED> How come? Who told the class to make the method read-only? I didn't! Methods in a class are done with the descriptor protocol. All access to the method through an instance is executed via the descriptor. The delete calls the __delete__ method of the descriptor which isn't implemented for functions. See http://docs.python.org/reference/datamodel.html?highlight=descriptor#implementing-descriptors (Actually, IIRC, the function object is its own descriptor) >>> class C(object): ... def method(self): ... pass ... >>> c=C() >>> C.__dict__['method'] >>> C.__dict__['method'].__get__ >>> C.__dict__['method'].__delete__ Traceback (most recent call last): File "", line 1, in AttributeError: 'function' object has no attribute '__delete__' >>> -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From google at mrabarnett.plus.com Fri Apr 3 18:55:51 2009 From: google at mrabarnett.plus.com (MRAB) Date: Fri, 03 Apr 2009 23:55:51 +0100 Subject: python needs leaning stuff from other language In-Reply-To: <1238794953.8087.1.camel@tim-laptop> References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <73kt5jFvitcdU1@mid.uni-berlin.de> <49D54209.5090306@hotmail.com> <01e56a2f$0$20654$c3e8da3@news.astraweb.com> <87y6uie9ej.fsf@benfinney.id.au> <01e624b7$0$20654$c3e8da3@news.astraweb.com> <1238794953.8087.1.camel@tim-laptop> Message-ID: <49D693F7.5090705@mrabarnett.plus.com> Tim Wintle wrote: > On Fri, 2009-04-03 at 13:12 -0400, Mel wrote: >>>>> I think it would also be better to have One (and prefereably Only One) >>>>> Obvious Way To Do It. That obvious way, for those who work with >>>>> Python's ?set? and ?dict?, is a ?clear? method. It seems best to have >>>>> ?list? conform with this also. >>>> Does that mean a one-off special case rule to forbid slices having a >>>> default? >>> Why would it do that? >> Well, if list.clear were truly and strictly to be the only way to clear the >> contents of a list, then assigning nothing via the default slice would have >> to be ruled out. `somelist[:] = []` is just a special case of assignment to >> a slice generally. > > agreed. If .clear was to be added then really assignments to slices > should be entirely removed. > Should we also remove .update from dict? I see no problem in collections having a .clear method. Saying that "if c is a collection then c.clear() clears that collection" seems to be a very duck-typy(?) thing to me. Assignments to slices is just a feature of ordered collections (well, lists), and clearing a list by assigning an empty list is just a special case of that. From ldo at geek-central.gen.new_zealand Fri Apr 3 18:57:01 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sat, 04 Apr 2009 11:57:01 +1300 Subject: Python Goes Mercurial References: <49d65b62$0$30904$9b622d9e@news.freenet.de> Message-ID: In message <49d65b62$0$30904$9b622d9e at news.freenet.de>, "Martin v. L?wis" wrote: > I don't like git because it is too difficult for me. In many cases, > git would refuse to do operations like updating or local committing, > producing error messages I was not able to understand ... Post an example of what you were trying to do, with the exact messages, and we can walk you through it. From ldo at geek-central.gen.new_zealand Fri Apr 3 18:58:23 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sat, 04 Apr 2009 11:58:23 +1300 Subject: with open('com1', 'r') as f: References: <8bc55c05-19da-41c4-b916-48e0a4be49b9@p11g2000yqe.googlegroups.com> Message-ID: In message <8bc55c05-19da-41c4- b916-48e0a4be49b9 at p11g2000yqe.googlegroups.com>, gert wrote: > with open('com1', 'r') as f: > for line in f: > print('line') Why bother, why not just for line in open('com1', 'r') : print line From aaron.hildebrandt at gmail.com Fri Apr 3 18:58:37 2009 From: aaron.hildebrandt at gmail.com (Aaron Scott) Date: Fri, 3 Apr 2009 15:58:37 -0700 (PDT) Subject: Module caching References: <8350f62e-c620-4abc-90dc-ef3af60494f6@3g2000yqk.googlegroups.com> <3a0704ea-703a-445f-804d-73f363ca9376@c11g2000yqj.googlegroups.com> Message-ID: <657ed616-ca5f-4f45-b056-9db877f76beb@v15g2000yqn.googlegroups.com> > are you an experienced python programmer? > Yeah, I'd link to think I'm fairly experienced and not making any stupid mistakes. That said, I'm fairly new to working with mod_python. All I really want is to have mod_python stop caching variables. This seems like it should be easy enough to do, but I can't for the life of me find information on how to do it. Aaron From google at mrabarnett.plus.com Fri Apr 3 19:01:26 2009 From: google at mrabarnett.plus.com (MRAB) Date: Sat, 04 Apr 2009 00:01:26 +0100 Subject: Unix programmers and Idle In-Reply-To: <20090331012112.GE5560@vnl.com> References: <20090330231640.GA5560@vnl.com> <49D16DAE.8010007@ieee.org> <20090331012112.GE5560@vnl.com> Message-ID: <49D69546.2010309@mrabarnett.plus.com> Dale Amon wrote: > On Mon, Mar 30, 2009 at 08:11:10PM -0500, Dave Angel wrote: >> I don't know what Idle has to do with it. sys.args contains the command >> line arguments used to start a script. >> >> Dale Amon wrote: >>> I wonder if someone could point me at documentation on how to debug >>> some of the standard Unix type things >>> in Idle. I cannot seem to figure out how to set my >>> argument line for the program I am debugging in an Idle >>> window. for example: >>> >>> vlmdeckcheck.py --strict --debug file.dat >>> >>> There must be a way to tell it what the command line args >>> are for the test run but I can't find it so far. >>> > > The line above represent what I want to emulate within idle. > If you run idle, select File->Open; then select the program name > as above to open; select Debug->Debugger; then start the program > with F5... which is lovely but I cannot find a way to tell idle > what the args are. > > idle is really nice but I am stuck doing my debugging in pdb > because of this. > FYI I've just submitted a patch which adds a dialog for entering command-line arguments in IDLE (issue #5680). From ben+python at benfinney.id.au Fri Apr 3 19:12:00 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sat, 04 Apr 2009 10:12:00 +1100 Subject: python needs leaning stuff from other language References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <73kt5jFvitcdU1@mid.uni-berlin.de> <49D54209.5090306@hotmail.com> <01e56a2f$0$20654$c3e8da3@news.astraweb.com> <87y6uie9ej.fsf@benfinney.id.au> <01e624b7$0$20654$c3e8da3@news.astraweb.com> Message-ID: <87fxgpe3i7.fsf@benfinney.id.au> Mel writes: > Well, if list.clear were truly and strictly to be the only way to > clear the contents of a list Who ever suggested that? Note that the ?OOW? in OOWTDI does *not* mean ?Only One Way?. It means ?One Obvious Way?. Having other Ways To Do It is only mildly deprecated, not forbidden. -- \ ?It ain't so much the things we don't know that get us in | `\ trouble. It's the things we know that ain't so.? ?Artemus Ward | _o__) (1834-67), U.S. journalist | Ben Finney From ben+python at benfinney.id.au Fri Apr 3 19:14:54 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sat, 04 Apr 2009 10:14:54 +1100 Subject: Python Goes Mercurial References: <49d65b62$0$30904$9b622d9e@news.freenet.de> Message-ID: <87bprde3dd.fsf@benfinney.id.au> Lawrence D'Oliveiro writes: > In message <49d65b62$0$30904$9b622d9e at news.freenet.de>, "Martin v. L?wis" > wrote: > > > I don't like git because it is too difficult for me. In many > > cases, git would refuse to do operations like updating or local > > committing, producing error messages I was not able to understand > > ... > > Post an example of what you were trying to do, with the exact > messages, and we can walk you through it. No, please, not in this forum. It would be quite off-topic, and Git has its own discussion forums; please use those. -- \ Eccles: ?I just saw the Earth through the clouds!? Lew: ?Did | `\ it look round?? Eccles: ?Yes, but I don't think it saw me.? | _o__) ?The Goon Show, _Wings Over Dagenham_ | Ben Finney From deank at yahoo.com Fri Apr 3 19:16:06 2009 From: deank at yahoo.com (dean) Date: Sat, 4 Apr 2009 01:16:06 +0200 Subject: How to add lines to the beginning of a text file? Message-ID: Hello, As the subject says how would I go about adding the lines to the beginning of a text file? Thanks in advance. From tjreedy at udel.edu Fri Apr 3 19:18:23 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 03 Apr 2009 19:18:23 -0400 Subject: Let-expressions In-Reply-To: References: Message-ID: sloisel wrote: > Dear All, > > I searched this group and found that there have been discussions about > introducing a let expression to the language so that you can define > local variables in a lambda. I.e., something like f=lambda x: let > y=x^2 in sin(y). (My syntax is unpythonic, but I hope you get it). > > Can someone tell me what eventually happened? The discussions ended. Proposals for assignment expressions have been rejected. From robert.kern at gmail.com Fri Apr 3 19:27:14 2009 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 03 Apr 2009 18:27:14 -0500 Subject: python needs leaning stuff from other language In-Reply-To: <1238794953.8087.1.camel@tim-laptop> References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <73kt5jFvitcdU1@mid.uni-berlin.de> <49D54209.5090306@hotmail.com> <01e56a2f$0$20654$c3e8da3@news.astraweb.com> <87y6uie9ej.fsf@benfinney.id.au> <01e624b7$0$20654$c3e8da3@news.astraweb.com> <1238794953.8087.1.camel@tim-laptop> Message-ID: On 2009-04-03 16:42, Tim Wintle wrote: > On Fri, 2009-04-03 at 13:12 -0400, Mel wrote: >>>>> I think it would also be better to have One (and prefereably Only One) >>>>> Obvious Way To Do It. That obvious way, for those who work with >>>>> Python's ?set? and ?dict?, is a ?clear? method. It seems best to have >>>>> ?list? conform with this also. >>>> Does that mean a one-off special case rule to forbid slices having a >>>> default? >>> Why would it do that? >> Well, if list.clear were truly and strictly to be the only way to clear the >> contents of a list, then assigning nothing via the default slice would have >> to be ruled out. `somelist[:] = []` is just a special case of assignment to >> a slice generally. > > agreed. If .clear was to be added then really assignments to slices > should be entirely removed. Please tell me you are joking. -- 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 lists at cheimes.de Fri Apr 3 19:30:32 2009 From: lists at cheimes.de (Christian Heimes) Date: Sat, 04 Apr 2009 01:30:32 +0200 Subject: Module caching In-Reply-To: <657ed616-ca5f-4f45-b056-9db877f76beb@v15g2000yqn.googlegroups.com> References: <8350f62e-c620-4abc-90dc-ef3af60494f6@3g2000yqk.googlegroups.com> <3a0704ea-703a-445f-804d-73f363ca9376@c11g2000yqj.googlegroups.com> <657ed616-ca5f-4f45-b056-9db877f76beb@v15g2000yqn.googlegroups.com> Message-ID: Aaron Scott wrote: > Yeah, I'd link to think I'm fairly experienced and not making any > stupid mistakes. That said, I'm fairly new to working with mod_python. > > All I really want is to have mod_python stop caching variables. This > seems like it should be easy enough to do, but I can't for the life of > me find information on how to do it. First of all I like to highly recommend against mod_python and for mod_wsgi. WSGI is *the* Python standard for web services. Why are you using mod_python at all? If you neither need persistent variables nor shared state then a CGI script is easier to use. Christian From brett at python.org Fri Apr 3 19:37:06 2009 From: brett at python.org (Brett Cannon) Date: Fri, 3 Apr 2009 16:37:06 -0700 Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: <49D66E7B.9080304@v.loewis.de> References: <49D4DA72.60401@v.loewis.de> <49D52115.6020001@egenix.com> <20090403004135.B76443A40A7@sparrow.telecommunity.com> <49D66E7B.9080304@v.loewis.de> Message-ID: On Fri, Apr 3, 2009 at 13:15, "Martin v. L?wis" wrote: > > Note that there is no such thing as a "defining namespace package" -- > > namespace package contents are symmetrical peers. > > With the PEP, a "defining package" becomes possible - at most one > portion can define an __init__.py. > > I know that the current mechanisms don't support it, and it might > not be useful in general, but now there is a clean way of doing it, > so I wouldn't exclude it. Distribution-wise, all distributions > relying on the defining package would need to require (or > install_require, or depend on) it. > > > The above are also true for using only a '*' in .pkg files -- in that > > event there are no sys.path changes. (Frankly, I'm doubtful that > > anybody is using extend_path and .pkg files to begin with, so I'd be > > fine with a proposal that instead used something like '.nsp' files that > > didn't even need to be opened and read -- which would let the directory > > scan stop at the first .nsp file found. > > That would work for me as well. Nobody at PyCon could remember where > .pkg files came from. > > > I believe the PEP does this as well, IIUC. > > Correct. > > >> * It's possible to have a defining package dir and add-one package > >> dirs. > > > > Also possible in the PEP, although the __init__.py must be in the first > > such directory on sys.path. > > I should make it clear that this is not the case. I envision it to work > this way: import zope > - searches sys.path, until finding either a directory zope, or a file > zope.{py,pyc,pyd,...} > - if it is a directory, it checks for .pkg files. If it finds any, > it processes them, extending __path__. > - it *then* checks for __init__.py, taking the first hit anywhere > on __path__ (just like any module import would) Just so people know how this __init__ search could be done such that __path__ is set from the .pkg is to treat it as a reload (assuming .pkg files can only be found off of sys.path). -Brett > - if no .pkg was found, nor an __init__.py, it proceeds with the next > sys.path item (skipping the directory entirely) > -------------- next part -------------- An HTML attachment was scrubbed... URL: From joncle at googlemail.com Fri Apr 3 19:41:04 2009 From: joncle at googlemail.com (Jon Clements) Date: Fri, 3 Apr 2009 16:41:04 -0700 (PDT) Subject: Module caching References: <8350f62e-c620-4abc-90dc-ef3af60494f6@3g2000yqk.googlegroups.com> <3a0704ea-703a-445f-804d-73f363ca9376@c11g2000yqj.googlegroups.com> <657ed616-ca5f-4f45-b056-9db877f76beb@v15g2000yqn.googlegroups.com> Message-ID: <3b9b538a-0048-4730-9681-618b540c3926@z15g2000yqm.googlegroups.com> On 3 Apr, 23:58, Aaron Scott wrote: > > are you an experienced python programmer? > > Yeah, I'd link to think I'm fairly experienced and not making any > stupid mistakes. That said, I'm fairly new to working with mod_python. > > All I really want is to have mod_python stop caching variables. This > seems like it should be easy enough to do, but I can't for the life of > me find information on how to do it. > > Aaron Umm... Well, mod_python works for long running processes that don't really store data, but return it on demand... so keeping module level variables around is going to be a gotcha. It's a kludge, but setting MaxRequestsPerChild to 1 in the Apache config basically forces a reload of everything for every request... that might be worth a go -- but it's nasty... Cheers, Jon. From clp2 at rebertia.com Fri Apr 3 20:20:51 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 3 Apr 2009 17:20:51 -0700 Subject: Let-expressions In-Reply-To: References: Message-ID: <50697b2c0904031720o7d8e8463tce944cf41e6581a3@mail.gmail.com> On Fri, Apr 3, 2009 at 4:18 PM, Terry Reedy wrote: > sloisel wrote: >> >> Dear All, >> >> I searched this group and found that there have been discussions about >> introducing a let expression to the language so that you can define >> local variables in a lambda. I.e., something like f=lambda x: let >> y=x^2 in sin(y). (My syntax is unpythonic, but I hope you get it). >> >> Can someone tell me what eventually happened? > > The discussions ended. > Proposals for assignment expressions have been rejected. To elaborate slightly, if you need anything more complicated than a single expression, then *don't use a lambda*. Just use a named function. It's much clearer and easier. Proposals to extend lambda usually spiral/evolve into wanting to make lambda multi-line, which has repeatedly been found to be incompatible with Python's indentation-based syntax; and, again, you can just use a named function. Cheers, Chris -- I have a blog: http://blog.rebertia.com From amon at vnl.com Fri Apr 3 20:36:33 2009 From: amon at vnl.com (Dale Amon) Date: Sat, 4 Apr 2009 01:36:33 +0100 Subject: Unix programmers and Idle In-Reply-To: References: Message-ID: <20090404003633.GN30919@vnl.com> On Mon, Mar 30, 2009 at 10:54:56PM -0700, Niklas Norrthon wrote: > I make sure my scripts are on the form: > > # imports > # global initialization (not depending on sys.argv) > def main(): > # initialization (might depend on sys.argv) > # script logic > # other functions > if __name__ == '__main__': > main() > > Then I have a trivial debug script named debug_whatever.py, which I > use as my entry point during debugging: > > # debug_whatever.py: > import sys > sys.argv[1:] = ['arg1', 'arg2', 'arg3'] > import whatever > whatever.main() I've found this approach very useful in another way as well. I write all my programs, in whatever language, with a perldoc section at the very end of the file where it doesn't get in the way of my seeing the code, but is still at least in the same module. Due to the problems of forward referencing I had not heretofore been able to use it inside the program module as it was obviously not defined when the code ran. However, with your method and with those two lines place at the very end after the perldoc documentation the code execution is always delayed until I can stuff it into __doc__ and thus have it available to print with a --man command line switch which I like to have in all my code. (I just pipe the __doc__ string through perl2man there) Works for me! -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 196 bytes Desc: Digital signature URL: From amon at vnl.com Fri Apr 3 20:48:48 2009 From: amon at vnl.com (Dale Amon) Date: Sat, 4 Apr 2009 01:48:48 +0100 Subject: Unix programmers and Idle In-Reply-To: <20090404003633.GN30919@vnl.com> References: <20090404003633.GN30919@vnl.com> Message-ID: <20090404004848.GO30919@vnl.com> Just in case anyone else finds it useful, to be precise I use: if opts.man: p1 = Popen(["echo", __doc__], stdout=PIPE) p2 = Popen(["pod2man"], stdin=p1.stdout, stdout=PIPE) p3 = Popen(["nroff","-man"], stdin=p2.stdout, stdout=PIPE) output = p3.communicate()[0] print output inside the def main(). -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 196 bytes Desc: Digital signature URL: From steve at REMOVE-THIS-cybersource.com.au Fri Apr 3 21:02:53 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 04 Apr 2009 01:02:53 GMT Subject: Hash of None varies per-machine References: <4c62537d-583b-4441-89f9-b341247d0b13@l22g2000vba.googlegroups.com> Message-ID: <01e6a5ff$0$20654$c3e8da3@news.astraweb.com> On Fri, 03 Apr 2009 10:50:08 -0700, ben.taylor wrote: > 1. Is it correct that if you hash two things that are not equal they > might give you the same hash value? Absolutely. From help(hash): hash(...) hash(object) -> integer Return a hash value for the object. Two objects with the same value have the same hash value. The reverse is not necessarily true, but likely. This is the pigeon-hole principle. On my PC, hash() returns a 32-bit integer between -2147483648 and 2147483647, so there are 2**32 unique hash values (the pigeon-holes). Presumably on 64-bit versions of Python, hash() will return a 64-bit result, giving 2**64 unique hash values. But there are an infinite number of possible objects which can be hashed (the pigeons), and so you have to have more than one pigeon per pigeon-hole. > Like, for instance, None and the > number 261862182320 (which is what my machine gives me if I hash None). > Note this is just an example, I'm aware hashing integers is probably > daft. Hashing has a very important role in Python, and you will be using it very often behind the scenes. You couldn't use integers as keys in dictionaries if they couldn't be hashed: another name for a dict is a "hash table". > I'm guessing that's fine, since you can't hash something to a > number without colliding with that number (or at least without hashing > the number to something else, like hashing every number to itself * 2, > which would then mean you couldn't hash very large numbers) You can hash numbers no matter how big they are. >>> hash(float('inf')) 314159 >>> hash(2**999) 128 > 2. Should the hash of None vary per-machine? I can't think why you'd > write code that would rely on the value of the hash of None, but you > might I guess. The value of hash(None) appears to be the value of id(None), which means it is the memory address that None happens to get, which means it will depend on the precise order that Python allocates things when it starts up, which will vary from platform to platform and version to version. > 3. Given that presumably not all things can be hashed (since the > documentation description of hash() says it gives you the hash of the > object "if it can be hashed"), should None be hashable? Any object can be hashed if it has a working __hash__ method. There's no reason not to have None hashable -- it costs nothing and allows you to use None as a dict key. -- Steven From invalid.pointer at gmail.com Fri Apr 3 21:11:39 2009 From: invalid.pointer at gmail.com (Manish Jain) Date: Sat, 04 Apr 2009 06:41:39 +0530 Subject: Undefined symbol PyUnicodeUCS2_DecodeUTF8 Message-ID: <49D6B3CB.5010501@gmail.com> Hi all, I am using Gnome on FreeBSD 7.1. A few days back, my gnome crashed and I have had to spend over 4 days recovering my gnome environment. Pretty much everything is okay now except for a few python-dependent applications (alacarte, for instance), which exit immediately with the following error message : > Traceback (most recent call last): > File "/usr/local/bin/alacarte", line 22, in > from Alacarte.MainWindow import MainWindow > File "/usr/local/lib/python2.5/site-packages/Alacarte/MainWindow.py", line 19, in > import gtk, gtk.glade, gmenu, gobject, gio > File "/usr/local/lib/python2.5/site-packages/gtk-2.0/gtk/__init__.py", line 38, in > import gobject as _gobject > File "/usr/local/lib/python2.5/site-packages/gtk-2.0/gobject/__init__.py", line 33, in > from glib import spawn_async, idle_add, timeout_add, timeout_add_seconds, \ > File "/usr/local/lib/python2.5/site-packages/gtk-2.0/glib/__init__.py", line 30, in > from glib._glib import * > ImportError: /usr/local/lib/python2.5/site-packages/gtk-2.0/glib/_glib.so: Undefined symbol "PyUnicodeUCS2_DecodeUTF8" > > [1]+ Exit 1 alacarte I have searched google/yahoo for any help without luck. I have rebuilt all python and gtk2 ports from scratch but the error continues. Can anybody please point out the source and/or remedy for the problem ? I am using the python25 port. -- Thank you and Regards Manish Jain invalid.pointer at gmail.com +91-99830-62246 NB : Laast year I kudn't spell Software Engineer. Now I are won. From bwgoudey at gmail.com Fri Apr 3 21:14:41 2009 From: bwgoudey at gmail.com (bwgoudey) Date: Fri, 3 Apr 2009 18:14:41 -0700 (PDT) Subject: Best way to extract from regex in if statement Message-ID: <22878967.post@talk.nabble.com> I have a lot of if/elif cases based on regular expressions that I'm using to filter stdin and using print to stdout. Often I want to print something matched within the regular expression and the moment I've got a lot of cases like: ... elif re.match("^DATASET:\s*(.+) ", line): m=re.match("^DATASET:\s*(.+) ", line) print m.group(1)) which is ugly because of the duplication but I can't think of a nicer of way of doing this that will allow for a lot of these sorts of cases. Any suggestions? -- View this message in context: http://www.nabble.com/Best-way-to-extract-from-regex-in-if-statement-tp22878967p22878967.html Sent from the Python - python-list mailing list archive at Nabble.com. From deank at yahoo.com Fri Apr 3 21:21:34 2009 From: deank at yahoo.com (dean) Date: Sat, 4 Apr 2009 03:21:34 +0200 Subject: How to add lines to the beginning of a text file? Message-ID: Hello, As the subject says how would I go about adding the lines to the beginning of a text file? Thanks in advance. From steve at REMOVE-THIS-cybersource.com.au Fri Apr 3 21:25:30 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 04 Apr 2009 01:25:30 GMT Subject: python needs leaning stuff from other language References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <73kt5jFvitcdU1@mid.uni-berlin.de> <49D54209.5090306@hotmail.com> <01e56a2f$0$20654$c3e8da3@news.astraweb.com> <87y6uie9ej.fsf@benfinney.id.au> <01e624b7$0$20654$c3e8da3@news.astraweb.com> Message-ID: <01e6ab4c$0$20654$c3e8da3@news.astraweb.com> On Fri, 03 Apr 2009 22:42:33 +0100, Tim Wintle wrote: > On Fri, 2009-04-03 at 13:12 -0400, Mel wrote: >> >>> I think it would also be better to have One (and prefereably Only >> >>> One) Obvious Way To Do It. That obvious way, for those who work >> >>> with Python's ?set? and ?dict?, is a ?clear? method. It seems best >> >>> to have ?list? conform with this also. >> >> >> >> Does that mean a one-off special case rule to forbid slices having a >> >> default? >> > >> > Why would it do that? >> >> Well, if list.clear were truly and strictly to be the only way to clear >> the contents of a list, then assigning nothing via the default slice >> would have to be ruled out. `somelist[:] = []` is just a special case >> of assignment to a slice generally. > > agreed. If .clear was to be added then really assignments to slices > should be entirely removed. That's total nonsense. Where do people get this ridiculous urban legend that there is "only one way to do it" in Python? The Zen says: "There should be one-- and preferably only one --obvious way to do it." does not mean "only one way to do it". It is a *prescription* that there SHOULD be one OBVIOUS way to do a task, not a prohibition on more than one way to do a task. Such a prohibition would be stupid *and* impossible to enforce: # Four ways to remove trailing spaces from a string. There are others. s.rstrip(" ") # the one obvious way while s.endswith(" "): s = s[:-1] while True: if s[-1] == ' ': s = s[0:-1] else: break L = [] trailing_spaces = True for c in reversed(s): if c == ' ' and trailing_spaces: continue trailing_spaces = False L.append(c) L.reverse() s = ''.join(L) -- Steven From steve at REMOVE-THIS-cybersource.com.au Fri Apr 3 21:48:01 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 04 Apr 2009 01:48:01 GMT Subject: How to add lines to the beginning of a text file? References: Message-ID: <01e6b093$0$20654$c3e8da3@news.astraweb.com> On Sat, 04 Apr 2009 03:21:34 +0200, dean wrote: > Hello, > > As the subject says how would I go about adding the lines to the > beginning of a text file? Thanks in advance. I'm not aware of any operating system that allows you to insert data into the middle or beginning of a file, although I've heard rumours that some ancient operating systems (VAX?) may have. To insert into a file, you have to do all the moving of data yourself. If the file is small enough to fit into memory, the easiest way is something like this: # read the current contents of the file f = open('filename') text = f.read() f.close() # open the file again for writing f = open('filename', 'w') f.write("This is the new first line\n") # write the original contents f.write(text) f.close() But note that this isn't entirely safe, if your Python session crashes after opening the file the second time and before closing it again, you will lose data. For quick scripts, that's a minuscule risk, but for bigger applications, you should use a safer method, something like this: # read the current contents of the file f = open('filename') text = f.read() f.close() # open a different file for writing f = open('filename~', 'w') f.write("This is the new first line\n") # write the original contents f.write(text) f.close() # did everything work safely? # if we get here, we're probably safe os.rename('filename~', 'filename') But note that even this is not entirely safe: it uses a predictable file name for the temporary file, which is generally a bad thing to do, it fails to flush data to disk, which means you're at the mercy of vagaries of the file system, and it doesn't do *any* error handling at all. The standard library really needs a good "save data to file" solution. -- Steven From joncle at googlemail.com Fri Apr 3 21:50:22 2009 From: joncle at googlemail.com (Jon Clements) Date: Fri, 3 Apr 2009 18:50:22 -0700 (PDT) Subject: How to add lines to the beginning of a text file? References: Message-ID: <47650f8f-316d-421c-8b50-4fb3497e4300@y7g2000yqa.googlegroups.com> On 4 Apr, 02:21, dean wrote: > Hello, > > As the subject says how would I go about adding the lines to the beginning > of a text file? Thanks in advance. I'd create a new file, then write your new lines, then iterate the existing file and write those lines... If no errors occcur, issue a delete for the old file, and a rename for new file to the old name. hth Jon. From gnewsg at gmail.com Fri Apr 3 21:52:52 2009 From: gnewsg at gmail.com (Giampaolo Rodola') Date: Fri, 3 Apr 2009 18:52:52 -0700 (PDT) Subject: python needs leaning stuff from other language References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <73kt5jFvitcdU1@mid.uni-berlin.de> <49D54209.5090306@hotmail.com> <01e56a2f$0$20654$c3e8da3@news.astraweb.com> <87y6uie9ej.fsf@benfinney.id.au> <01e624b7$0$20654$c3e8da3@news.astraweb.com> <01e6ab4c$0$20654$c3e8da3@news.astraweb.com> Message-ID: <43a2034b-c520-45d0-af3c-951e60fbc86a@e18g2000yqo.googlegroups.com> If "there should be one-- and preferably only one --obvious way to do it" then my_list.clear() is more obvious than del my_list[:]. Honestly I'm a little surprised that such a topic hasn't been raised before. --- Giampaolo http://code.google.com/p/pyftpdlib From joncle at googlemail.com Fri Apr 3 21:56:54 2009 From: joncle at googlemail.com (Jon Clements) Date: Fri, 3 Apr 2009 18:56:54 -0700 (PDT) Subject: Best way to extract from regex in if statement References: Message-ID: <357a2596-460c-409e-932a-97c9cd14d0a6@g19g2000yql.googlegroups.com> On 4 Apr, 02:14, bwgoudey wrote: > I have a lot of if/elif cases based on regular expressions that I'm using to > filter stdin and using print to stdout. Often I want to print something > matched within the regular expression and the moment I've got a lot of cases > like: > > ... > elif re.match("^DATASET:\s*(.+) ", line): > ? ? ? ? m=re.match("^DATASET:\s*(.+) ", line) > ? ? ? ? print m.group(1)) > > which is ugly because of the duplication but I can't think of a nicer of way > of doing this that will allow for a lot of these sorts of cases. Any > suggestions? > -- > View this message in context:http://www.nabble.com/Best-way-to-extract-from-regex-in-if-statement-... > Sent from the Python - python-list mailing list archive at Nabble.com. How about something like: your_regexes = [ re.compile('rx1'), re.compile('rx2'), # etc.... ] for line in lines: for rx in your_regexes: m = rx.match(line) if m: print m.group(1) break # if only the first matching regex is required, otherwise leave black for all Untested, but seems to make sense hth, Jon From python.list at tim.thechases.com Fri Apr 3 22:11:04 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 03 Apr 2009 21:11:04 -0500 Subject: Best way to extract from regex in if statement In-Reply-To: <22878967.post@talk.nabble.com> References: <22878967.post@talk.nabble.com> Message-ID: <49D6C1B8.4070508@tim.thechases.com> bwgoudey wrote: > I have a lot of if/elif cases based on regular expressions that I'm using to > filter stdin and using print to stdout. Often I want to print something > matched within the regular expression and the moment I've got a lot of cases > like: > > ... > elif re.match("^DATASET:\s*(.+) ", line): > m=re.match("^DATASET:\s*(.+) ", line) > print m.group(1)) > > > which is ugly because of the duplication but I can't think of a nicer of way > of doing this that will allow for a lot of these sorts of cases. Any > suggestions? I've done this in the past with re-to-function pairings: def action1(matchobj): print matchobj.group(1) def action2(matchobj): print matchobj.group(3) # ... other actions to perform searches = [ (re.compile(PATTERN1), action1), (re.compile(PATTERN2), action2), # other pattern-to-action pairs ] # ... line = ... for regex, action in searches: m = regex.match(line) if m: action(m) break else: no_match(line) (note that that's a for/else loop, not an if/else pair) -tkc From steve at REMOVE-THIS-cybersource.com.au Fri Apr 3 22:19:05 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 04 Apr 2009 02:19:05 GMT Subject: python needs leaning stuff from other language References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <73kt5jFvitcdU1@mid.uni-berlin.de> <49D54209.5090306@hotmail.com> <01e56a2f$0$20654$c3e8da3@news.astraweb.com> <87y6uie9ej.fsf@benfinney.id.au> <01e624b7$0$20654$c3e8da3@news.astraweb.com> <01e6ab4c$0$20654$c3e8da3@news.astraweb.com> <43a2034b-c520-45d0-af3c-951e60fbc86a@e18g2000yqo.googlegroups.com> Message-ID: <01e6b7db$0$20654$c3e8da3@news.astraweb.com> On Fri, 03 Apr 2009 18:52:52 -0700, Giampaolo Rodola' wrote: > If "there should be one-- and preferably only one --obvious way to do > it" then my_list.clear() is more obvious than del my_list[:]. Honestly > I'm a little surprised that such a topic hasn't been raised before. I'm a little surprised that you didn't bother googling before making that statement. Of course it has been raised before. See (for example) these threads: http://mail.python.org/pipermail/python-dev/2005-July/054564.html http://mail.python.org/pipermail/python-list/2006-April/549278.html http://mail.python.org/pipermail/python-list/2006-May/556977.html http://mail.python.org/pipermail/python-ideas/2007-May/000692.html The good news is that Python-Ideas seems to be receptive to the idea, including some heavy-weights like Raymond Hettinger, which leads me to be hopeful that Python-Dev and Guido himself will agree: http://mail.python.org/pipermail/python-ideas/2009-April/003897.html Keep your fingers crossed that this is the last time we need to have this discussion! -- Steven From dhruv.howudoin at gmail.com Fri Apr 3 22:19:34 2009 From: dhruv.howudoin at gmail.com (Dhruv) Date: Fri, 3 Apr 2009 19:19:34 -0700 (PDT) Subject: SendKeys not working in command line interface Message-ID: hi everyone. I am fairly new to python. When I go to IDLE GUI for python 2.6 and type: >>> import SendKeys >>> SendKeys.SendKeys('{LWIN} r notepad', pause=0.5) it works fine. however when I write the same code using jEdit and run it in command line, it does not give error but it doesn't run either. Same problem if I start the command line shell of python 2.6 instead of its IDLE Gui Any suggestions/solutions? Thanks in advance. From george.sakkis at gmail.com Fri Apr 3 22:24:19 2009 From: george.sakkis at gmail.com (George Sakkis) Date: Fri, 3 Apr 2009 19:24:19 -0700 (PDT) Subject: Best way to extract from regex in if statement References: <357a2596-460c-409e-932a-97c9cd14d0a6@g19g2000yql.googlegroups.com> Message-ID: On Apr 3, 9:56?pm, Jon Clements wrote: > On 4 Apr, 02:14, bwgoudey wrote: > > > > > I have a lot of if/elif cases based on regular expressions that I'm using to > > filter stdin and using print to stdout. Often I want to print something > > matched within the regular expression and the moment I've got a lot of cases > > like: > > > ... > > elif re.match("^DATASET:\s*(.+) ", line): > > ? ? ? ? m=re.match("^DATASET:\s*(.+) ", line) > > ? ? ? ? print m.group(1)) > > > which is ugly because of the duplication but I can't think of a nicer of way > > of doing this that will allow for a lot of these sorts of cases. Any > > suggestions? > > -- > > View this message in context:http://www.nabble.com/Best-way-to-extract-from-regex-in-if-statement-... > > Sent from the Python - python-list mailing list archive at Nabble.com. > > How about something like: > > your_regexes = [ > ? ? re.compile('rx1'), > ? ? re.compile('rx2'), > ? ? # etc.... > ] > > for line in lines: > ? ? for rx in your_regexes: > ? ? ? ? m = rx.match(line) > ? ? ? ? if m: > ? ? ? ? ? ? print m.group(1) > ? ? ? ? ? ? break # if only the first matching regex is required, > otherwise leave black for all > > Untested, but seems to make sense Or in case you want to handle each regexp differently, you can construct a dict {regexp : callback_function} that picks the right action depending on which regexp matched. As for how to populate the dict, if most methods are short expressions, lambda comes in pretty handly, e.g. { rx1: lambda match: match.group(1), rx2: lambda match: sum(map(int, match.groups())), ... } If not, you can combine the handler definition with the mapping update by using a simple decorator factory such as the following (untested): def rxhandler(rx, mapping): rx = re.compile(rx) def deco(func): mapping[rx] = func return func return deco d = {} @rxhandler("^DATASET:\s*(.+) ", d) def handle_dataset(match): ... @rxhandler("^AUTHORS:\s*(.+) ", d) def handle_authors(match): ... HTH, George From http Fri Apr 3 22:26:25 2009 From: http (Paul Rubin) Date: 03 Apr 2009 19:26:25 -0700 Subject: Best way to extract from regex in if statement References: Message-ID: <7xvdplupbi.fsf@ruckus.brouhaha.com> bwgoudey writes: > elif re.match("^DATASET:\s*(.+) ", line): > m=re.match("^DATASET:\s*(.+) ", line) > print m.group(1)) Sometimes I like to make a special class that saves the result: class Reg(object): # illustrative code, not tested def match(self, pattern, line): self.result = re.match(pattern, line) return self.result Then your example would look something like: save_re = Reg() .... elif save_re.match("^DATASET:\s*(.+) ", line): print save_re.result.group(1) From python.list at tim.thechases.com Fri Apr 3 22:32:55 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 03 Apr 2009 21:32:55 -0500 Subject: Best way to extract from regex in if statement In-Reply-To: References: <357a2596-460c-409e-932a-97c9cd14d0a6@g19g2000yql.googlegroups.com> Message-ID: <49D6C6D7.5090604@tim.thechases.com> > Or in case you want to handle each regexp differently, you can > construct a dict {regexp : callback_function} that picks the right > action depending on which regexp matched. One word of caution: dicts are unsorted, so if more than one regexp can match a given line, they either need to map to the same function, or you need to use a list of regexp-to-functions (see my previous post) for a determinate order. -tkc From sloisel at gmail.com Fri Apr 3 22:35:51 2009 From: sloisel at gmail.com (sloisel) Date: Fri, 3 Apr 2009 19:35:51 -0700 (PDT) Subject: Let-expressions References: Message-ID: Thanks for the quick replies. I didn't want statements in my expressions, just let-expressions. That's okay, it was just a question. S?bastien Loisel On Apr 3, 7:20?pm, Chris Rebert wrote: > On Fri, Apr 3, 2009 at 4:18 PM, Terry Reedy wrote: > > sloisel wrote: > > >> Dear All, > > >> I searched this group and found that there have been discussions about > >> introducing a let expression to the language so that you can define > >> local variables in a lambda. I.e., something like f=lambda x: let > >> y=x^2 in sin(y). (My syntax is unpythonic, but I hope you get it). > > >> Can someone tell me what eventually happened? > > > The discussions ended. > > Proposals for assignment expressions have been rejected. > > To elaborate slightly, if you need anything more complicated than a > single expression, then *don't use a lambda*. Just use a named > function. It's much clearer and easier. > Proposals to extend lambda usually spiral/evolve into wanting to make > lambda multi-line, which has repeatedly been found to be incompatible > with Python's indentation-based syntax; and, again, you can just use a > named function. > > Cheers, > Chris > > -- > I have a blog:http://blog.rebertia.com From andre.roberge at gmail.com Fri Apr 3 22:39:31 2009 From: andre.roberge at gmail.com (=?ISO-8859-1?Q?Andr=E9?=) Date: Fri, 3 Apr 2009 19:39:31 -0700 (PDT) Subject: Problem understanding some unit tests in Python distribution Message-ID: <99be651d-bf9f-453f-a1ae-80ce102b2431@z14g2000yqa.googlegroups.com> Hi everyone, In the hope of perhaps contributing some additional unit tests for Python (thus contributing back to the community), I dove in the code and found something that should be simple but that I can not wrap my head around. In list_tests.py, one finds === from test import test_support, seq_tests class CommonTest(seq_tests.CommonTest): def test_init(self): # Iterable arg is optional self.assertEqual(self.type2test([]), self.type2test()) # etc. === Wanting to figure out what the type2test() method does, I looked in seq_tests.py and found the following: === class CommonTest(unittest.TestCase): # The type to be tested type2test = None def test_constructors(self): l0 = [] l1 = [0] l2 = [0, 1] u = self.type2test() u0 = self.type2test(l0) u1 = self.type2test(l1) u2 = self.type2test(l2) # etc. === No where do I find a definition for the type2test() method - other than seeing it as a class variable defined to be None by default. I looked in unittest.TestCase and did not see it anywhere. Am I missing something obvious? I would appreciate if someone could point me in the right direction. Cheers, Andr? From ahqylanglei at gmail.com Fri Apr 3 22:45:17 2009 From: ahqylanglei at gmail.com (Linuxwell) Date: Fri, 3 Apr 2009 19:45:17 -0700 (PDT) Subject: "Pythoner",Wish me luck! References: <54afb932-1a42-42d2-a3b9-0151b2bd410b@q30g2000prq.googlegroups.com> Message-ID: <768fd39b-1762-460b-a658-4b4346b693a5@a5g2000pre.googlegroups.com> Thanks everyone,thank you very much!!! From george.sakkis at gmail.com Fri Apr 3 23:06:01 2009 From: george.sakkis at gmail.com (George Sakkis) Date: Fri, 3 Apr 2009 20:06:01 -0700 (PDT) Subject: "Pythoner",Wish me luck! References: <54afb932-1a42-42d2-a3b9-0151b2bd410b@q30g2000prq.googlegroups.com> <19963172-7ac1-4796-abb7-3387a65ba91d@f19g2000yqo.googlegroups.com> <3a4f64da-561f-40e5-9c19-7d15c02fe94d@d25g2000prn.googlegroups.com> <0108c1c8-f7d2-4ff5-9570-18126cf4e17d@e38g2000yqa.googlegroups.com> Message-ID: On Apr 3, 3:47?pm, barisa wrote: > On Apr 3, 8:58?pm, nrball... at gmail.com wrote: > > > > > On Apr 3, 12:33?pm, barisa wrote: > > > > On Apr 3, 11:39?am, "Hendrik van Rooyen" wrote: > > > > > "Matteo" wrote: > > > > > On Apr 3, 9:05 am, Linuxwell wrote: > > > > > >> Starting today I would like to study Python,Wish me luck! > > > > > >Good luck! > > > > > >Don't forget to... > > > > > >>>> print 'Hello World!' > > > > > This is better advice than what you may think, > > > > because the interactive interpreter is your very > > > > best friend when studying the language. > > > > > You get there by typing "python" at the command > > > > line, and pressing enter. > > > > > Using it, you will save yourself many hours of > > > > misunderstanding. > > > > > - Hendrik > > > > Hi, > > > I'm also begginer in python; > > > i did few basic programs about graph etc.. > > > > my question is : what benefit is using interactive intrepreter ? > > > > i come from java backround, so I use eclipse for python as well. > > > I start my program, it does it's job, and that's it. ?(after some > > > debugging ofc) > > > I'm also a beginner in Python, but from my own experience the > > interactive interpreter is great for experimenting with new modules > > and output formatting because it allows you to see the immediate > > output of a function before you write it into your program. ?The > > immediate result is that you'll see any errors and be able to fix them > > before they end up in your script. > > > Nick Ballardhttp://90daysofpython.blogspot.com > > thanks, i'll give it a try Or even better, install IPython [1], a python interpreter on steroids. It's the first 3rd party python package I install on every new system I work on; it's so powerful and versatile, it has almost displaced the regular linux shell for me. I highly recommend it. George [1] http://ipython.scipy.org/ From castironpi at gmail.com Fri Apr 3 23:32:26 2009 From: castironpi at gmail.com (Aaron Brady) Date: Fri, 3 Apr 2009 20:32:26 -0700 (PDT) Subject: Best way to pickle functions References: <29bfcf8a-984d-47df-a116-7a1f80f171ba@z9g2000yqi.googlegroups.com> <5fce0034-3598-4d8a-9a2d-9832e8bea5ba@j39g2000yqn.googlegroups.com> Message-ID: <09a3664d-6cc4-4131-97fd-325af72814d8@e18g2000yqo.googlegroups.com> On Apr 3, 3:48?pm, Aaron Scott wrote: > > Why not use import ? ?Simply recreate the source file, if necessary, and > > import it again. > > Ah, you'd think it would be that easy :P > > The problem with just importing a module is that the module is then > cached in memory. Multiple copies of the program are running on a > server, and each of them have something akin to a "randomfunctions" > module. When the first program is accessed, it loads > "randomfunctions". When the second program is accessed, it uses the > "randomfunctions" module already in memory, even though it doesn't > contain the right functions. So, I have to pull in these functions > dynamically. Here I found this cookie you can have it. imp.load_source(name, pathname[, file]) Load and initialize a module implemented as a Python source file and return its module object. If the module was already initialized, it will be initialized again. 'again' was actually in italics in the docs. I presume you're aware of eval and exec. From pavlovevidence at gmail.com Sat Apr 4 00:10:30 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 3 Apr 2009 21:10:30 -0700 (PDT) Subject: A design problem I met again and again. References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> <33e08fde-1d4e-41a2-9ce2-7e1bec3abd90@r29g2000vbp.googlegroups.com> <793a9568-7c89-412e-b14c-e2dd28640577@s22g2000prg.googlegroups.com> <1b60f999-aaf8-4b10-bd1f-ab46f03f44f6@m36g2000vbp.googlegroups.com> Message-ID: <22c65831-cdfb-4ac3-95d9-215ba338e89a@u9g2000pre.googlegroups.com> On Apr 2, 11:25 pm, ??? wrote: > Consolidate existing functions? > > I've thought about it. > > For example, I have two functions: > > #========================= > > def startXXX(id): > pass > > def startYYY(id): > pass > #========================= > > I could turn it into one: > > #========================= > def start(type, id): > if(type == "XXX"): > pass > else if(type == "YYY"): > pass > #========================= > > But isn't the first style more clear for my code's user? Not necessarily, especially if the user wants to dynamically choose which start*** function to call. I have one more suggestion. Consider whether there are groups of methods that are used together but aren't used with other groups of functions. For instance, maybe there is a group of methods that can only be called after a call to startXXX. If that's the case, you might want to separate those groups into different classes. The branched-off class would then act as a sort of session handler. A piece of user code that looked like this (where sc is an instance of your enormous class): sc.startX() sc.send_data_via_X() sc.receive_data_via_X() sc.stopX() might look like this after you factor it out: session = sc.startX() # creates and returns a new XSession object session.send_data() # these are methods of the XSession session.receive_data() session.stop() Any methods that are callable any time, you can retain in the big class, or put in a base class of all the sessions. Carl Banks From wuwei23 at gmail.com Sat Apr 4 00:19:19 2009 From: wuwei23 at gmail.com (alex23) Date: Fri, 3 Apr 2009 21:19:19 -0700 (PDT) Subject: is there a way to collect twitts with python? References: <1078018b0903271054y3d4a7162w885c61bf63bc2048@mail.gmail.com> Message-ID: On Apr 4, 6:58?am, Michael Torrie wrote: > Oh wow. ?If this is what Twitter does to one's ability to articulate > clearly, I hope Twitter dies a horrible death and any APIs and Python > bindings with it! At least he was able to use more than 140 chars :) From jdoe at usenetlove.invalid Sat Apr 4 00:21:04 2009 From: jdoe at usenetlove.invalid (John Doe) Date: Sat, 04 Apr 2009 04:21:04 GMT Subject: Cannot find text in *.py files with Windows Explorer? Message-ID: Anybody have a solution for Windows (XP) Explorer search not finding ordinary text in *.py files? Thanks. From bellman at lysator.liu.se Sat Apr 4 00:25:40 2009 From: bellman at lysator.liu.se (Thomas Bellman) Date: Sat, 4 Apr 2009 04:25:40 +0000 (UTC) Subject: Hash of None varies per-machine References: <4c62537d-583b-4441-89f9-b341247d0b13@l22g2000vba.googlegroups.com> <01e6a5ff$0$20654$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > You can hash numbers no matter how big they are. > >>> hash(float('inf')) > 314159 Cute. And hash(float('-inf')) is -271828... -- Thomas Bellman, Lysator Computer Club, Link??ping University, Sweden "God is real, but Jesus is an integer." ! bellman @ lysator.liu.se ! Make Love -- Nicht Wahr! From tim.wintle at teamrubber.com Sat Apr 4 00:48:20 2009 From: tim.wintle at teamrubber.com (Tim Wintle) Date: Sat, 04 Apr 2009 05:48:20 +0100 Subject: python needs leaning stuff from other language In-Reply-To: References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <73kt5jFvitcdU1@mid.uni-berlin.de> <49D54209.5090306@hotmail.com> <01e56a2f$0$20654$c3e8da3@news.astraweb.com> <87y6uie9ej.fsf@benfinney.id.au> <01e624b7$0$20654$c3e8da3@news.astraweb.com> <1238794953.8087.1.camel@tim-laptop> Message-ID: <1238820500.1029.69.camel@tim-laptop> On Fri, 2009-04-03 at 18:27 -0500, Robert Kern wrote: > > agreed. If .clear was to be added then really assignments to slices > > should be entirely removed. > > Please tell me you are joking. Well I'm not joking as such. I've noticed that python-ideas seems to be positive on the idea, and has a patch ready for Guido, obviously I'm not that anti it that I'd always be complaining if it is implemented, I just see it as unnecessary: http://mail.python.org/pipermail/python-ideas/2009-April/003933.html (I didn't expect such strong responses btw!) you can already do: del mylist[:] * or * mylist[:] = [] * or * mylist = [] which, although semantically similar are different as far as the interpreter are concerned (since two of them create a new list): (Python 2.5.2 - don't have a newer version on this machine to check) {{{ import dis >>> def f(a): ... del a[:] ... >>> def g(a): ... a[:] = [] ... >>> def h(a): ... a = [] ... >>> dis.dis(f) 2 0 LOAD_FAST 0 (a) 3 DELETE_SLICE+0 4 LOAD_CONST 0 (None) 7 RETURN_VALUE >>> dis.dis(g) 2 0 BUILD_LIST 0 3 LOAD_FAST 0 (a) 6 STORE_SLICE+0 7 LOAD_CONST 0 (None) 10 RETURN_VALUE >>> dis.dis(h) 2 0 BUILD_LIST 0 3 STORE_FAST 0 (a) 6 LOAD_CONST 0 (None) 9 RETURN_VALUE }}} so it seems silly to introduce a *third* formal description of what is (almost) semantically the same! My knowledge of the interpreter begins to get fuzzy here, but would this add extra overhead by the look-ups to the .clear method (really don't have the time to compile a patched version of trunk to test!) - it might be a constant change, but it's still a difference to what I believe is likely to be required to be fairly high-performance when it is used. e.g. {{{ >>> def f(a): ... a.clear() ... >>> dis.dis(f) 2 0 LOAD_FAST 0 (a) 3 LOAD_ATTR 0 (clear) 6 CALL_FUNCTION 0 9 POP_TOP 10 LOAD_CONST 0 (None) 13 RETURN_VALUE }}} The current versions at least do two different things quite efficiently - if you are clearing the list and expect the list to be small / zero length next time, then (I believe) it's more memory efficient to try mylist = [] ,where del mylist[:] will (I'm assuming - correct me if I'm wrong) reduce the size of the list, but using the clever list memory usage it will scale to a large size again fairly well. As for: "Should we also remove .update from dict?" Am I missing something? What is the single statement that is equivalent to .update (although personally I don't think I have *ever* used .update - I'm normally using a set for situations where I would require .update) Oh, and can I have the reference for Raymond Hettinger's blessing (mentioned in a different branch of this thread)? As far as I can see he has only (in archives at least) replied to a question about whether it's worth formalising the request as a PEP, and hasn't entered a vote either way: """ Just ask Guido for his blessing. The implementation is trivial. """ http://mail.python.org/pipermail/python-ideas/2009-April/003938.html Ironically much of my - very limited - knowledge of the interpreter stems from many of his talks, which were the encouragement to start actually reading the source for the interpreter! I'm more than willing to drop my issue if it's voted against, but it does feel dirty to me to add extra syntax that have completely parallel semantics. In general language design (not just programming languages) I personally take any (provable) requirement for this as a _possible_ problem with the chosen basis / semantic value function (in 1st order language terms), although I understand that it is sometimes unavoidable in real life. For example, this makes me question the orthogonality of "ordered" and "collection" with semantics dictated by their methods/features. Proposing that the object: mylist[a:b:c] return an iterator that is non-assignable appears to fix this non-orthogonality as far as I can immediately see (although I am *very* tired, and haven't thought it through far enough!) Tim Wintle From tim.wintle at teamrubber.com Sat Apr 4 00:56:21 2009 From: tim.wintle at teamrubber.com (Tim Wintle) Date: Sat, 04 Apr 2009 05:56:21 +0100 Subject: is there a way to collect twitts with python? In-Reply-To: <49D67874.8080209@gmail.com> References: <1078018b0903271054y3d4a7162w885c61bf63bc2048@mail.gmail.com> <49D67874.8080209@gmail.com> Message-ID: <1238820981.1029.76.camel@tim-laptop> On Fri, 2009-04-03 at 14:58 -0600, Michael Torrie wrote: > Oh wow. If this is what Twitter does to one's ability to articulate > clearly, I hope Twitter dies a horrible death and any APIs and Python > bindings with it! Thank you, thank you, thank you .... everyone around me seems to love that thing (twitter), and I still can't work out why (apart from hacks such as using it as a hosted queue for cross-server comms, or receiving cheap sms to your app) Tim Wintle From nick at stinemates.org Sat Apr 4 01:03:04 2009 From: nick at stinemates.org (Nick Stinemates) Date: Fri, 3 Apr 2009 21:03:04 -0800 Subject: is there a way to collect twitts with python? In-Reply-To: <1238820981.1029.76.camel@tim-laptop> References: <1078018b0903271054y3d4a7162w885c61bf63bc2048@mail.gmail.com> <49D67874.8080209@gmail.com> <1238820981.1029.76.camel@tim-laptop> Message-ID: <000801c9b4e2$a3584750$ea08d5f0$@org> > Thank you, thank you, thank you .... > everyone around me seems to love that thing (twitter), and I still can't > work out why (apart from hacks such as using it as a hosted queue for > cross-server comms, or receiving cheap sms to your app) > Tim Wintle You mean you don't want to read every detail of someone's life? Damn.. From michele.simionato at gmail.com Sat Apr 4 01:14:38 2009 From: michele.simionato at gmail.com (Michele Simionato) Date: Fri, 3 Apr 2009 22:14:38 -0700 (PDT) Subject: User or UserManager ? Problems of Observer Pattern References: Message-ID: <57f21569-644e-4c10-8702-5e2363f81f74@f19g2000yqh.googlegroups.com> On Apr 3, 7:34 pm, ??? wrote: > #---------------- > # Choice 2 > #---------------- > > class UserManager: > > users = [] > > @classmethod > def delUser(cls, user): > cls.users.remove(user) > > RoleManager.onUserDel(user) > > class RoleManager: > > roles = [] > > @classmethod > def onUserDel(cls, user): > for r in cls.roles.items(): > r.users.remove(user) Choice #2 is better, but it should not be implemented this way. Using a class as a pure container of methods, a big singleton, makes little sense, since you would be better off with a module and old fashioned procedural programming. You recognize this and are worried about this. You do not need to be worried to much, since there is nothing wrong with procedural design (but you should use modules not classes). If you want to be more object oriented, the first thing is to understand is that you define objects in order to pass them to other objects. For instance, you are using a global list of users as a class attribute, but I would just pass the list in the __init__ method, and make it an instance attribute. I also would make a RoleManager instance and pass it to the UserManager, so that it can be used directly. Or perhaps I would define a single UserRoleManager doing all the job. It depends. But all the point of OOP is to pass objects to other objects. It is not bad to think of a data object as of a record on steroids. Objects which are purely data and not behavior are the simplest and the best objects, start from them. There are also objects which are mostly behavior and nearly no data (the Manager objects here, which just act over collections of other objects). Those are more difficult to write. OTOH, nobody forces you to write the managers as objects. You may find easier to write a set of manager functions. This set of functions can later become methods of a Manager object, if it seems fit, but that must be done a posteriori, not a priori. Perhaps you should read some book like "Programming Python" by Ludz which has many examples of how to write OO applications in Python (I mean no offense, but indeed from you examples is seems to me that you are missing the point of OOP, as you are the first to recognize). HTH, Michele Simionato From michele.simionato at gmail.com Sat Apr 4 01:21:07 2009 From: michele.simionato at gmail.com (Michele Simionato) Date: Fri, 3 Apr 2009 22:21:07 -0700 (PDT) Subject: Books about refactoring in Python (Was: A design problem I met again and again.) References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> <33e08fde-1d4e-41a2-9ce2-7e1bec3abd90@r29g2000vbp.googlegroups.com> <793a9568-7c89-412e-b14c-e2dd28640577@s22g2000prg.googlegroups.com> <1b60f999-aaf8-4b10-bd1f-ab46f03f44f6@m36g2000vbp.googlegroups.com> <22c65831-cdfb-4ac3-95d9-215ba338e89a@u9g2000pre.googlegroups.com> Message-ID: On Apr 4, 6:10?am, Carl Banks wrote: > A piece of user code that looked like this (where sc is an instance of > your enormous class): > > sc.startX() > sc.send_data_via_X() > sc.receive_data_via_X() > sc.stopX() > > might look like this after you factor it out: > > session = sc.startX() ?# creates and returns a new XSession object > session.send_data() ? ?# these are methods of the XSession > session.receive_data() > session.stop() > > Any methods that are callable any time, you can retain in the big > class, or put in a base class of all the sessions. That's good advice. A typical refactoring technique when working with blob classes is to extract groups of methods with commmon functionality, put them in a helper class, make a helper object and pass it to the original blob. In other words, tp split the blob object as a composition of small logically independent objects. BTW, is there anybody in this lists that can suggest good books about refactoring in Python? There are plenty of books about refactoring for Java and C++ but on top of my mind I cannot think of a Python book right now. From torriem at gmail.com Sat Apr 4 01:31:35 2009 From: torriem at gmail.com (Michael Torrie) Date: Fri, 03 Apr 2009 23:31:35 -0600 Subject: is there a way to collect twitts with python? In-Reply-To: <1238820981.1029.76.camel@tim-laptop> References: <1078018b0903271054y3d4a7162w885c61bf63bc2048@mail.gmail.com> <49D67874.8080209@gmail.com> <1238820981.1029.76.camel@tim-laptop> Message-ID: <49D6F0B7.2050608@gmail.com> Tim Wintle wrote: > On Fri, 2009-04-03 at 14:58 -0600, Michael Torrie wrote: >> Oh wow. If this is what Twitter does to one's ability to articulate >> clearly, I hope Twitter dies a horrible death and any APIs and Python >> bindings with it! > > Thank you, thank you, thank you .... > > everyone around me seems to love that thing (twitter), and I still can't > work out why (apart from hacks such as using it as a hosted queue for > cross-server comms, or receiving cheap sms to your app) Yeah. Of course I always thought that IRC was a good fit for this purpose. Virus writers seem to think so. I wonder how Twitter would deal with viruses and worms using Twitter and a command and control communications mechanism. People who love Twitter are also people who SMS a lot it seems. Twitter probably is a natural evolution of SMS, melding the IRC idea with ridiculously short, hard-to-read, cryptic, lol-speak, messages. I think the Japanese just might be on to something as no one over there uses SMS. It's all e-mail to them. Granted the scourge of abbreviated words and lol-speak is just as bad there. Sigh. From mail at timgolden.me.uk Sat Apr 4 02:09:19 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Sat, 04 Apr 2009 07:09:19 +0100 Subject: Cannot find text in *.py files with Windows Explorer? In-Reply-To: References: Message-ID: <49D6F98F.5090406@timgolden.me.uk> John Doe wrote: > Anybody have a solution for Windows (XP) Explorer search not finding > ordinary text in *.py files? It came up a couple of months ago either on this list or on python-win32. Don't have web access at the moment, but try searching the archives for references to search, registry settings and probably an msdn link. Now I think about it, try searching for "xplorer2" since I think I mentioned that I have used that instead of explorer for some while. That should get you the thread at any rate. TJG From jdoe at usenetlove.invalid Sat Apr 4 02:35:27 2009 From: jdoe at usenetlove.invalid (John Doe) Date: Sat, 04 Apr 2009 06:35:27 GMT Subject: Cannot find text in *.py files with Windows Explorer? References: Message-ID: Tim Golden wrote: > Now I think about it, try searching for "xplorer2" since I think I > mentioned that I have used that instead of explorer for some > while. Yeah... at least by the time I move from Windows XP to Windows 7, very likely I will be using a different file manager. If I cannot search Python files, now might be a good time to switch. Thanks for the search criteria, I will look for that thread. From robert.kern at gmail.com Sat Apr 4 03:03:07 2009 From: robert.kern at gmail.com (Robert Kern) Date: Sat, 04 Apr 2009 02:03:07 -0500 Subject: python needs leaning stuff from other language In-Reply-To: <1238820500.1029.69.camel@tim-laptop> References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <73kt5jFvitcdU1@mid.uni-berlin.de> <49D54209.5090306@hotmail.com> <01e56a2f$0$20654$c3e8da3@news.astraweb.com> <87y6uie9ej.fsf@benfinney.id.au> <01e624b7$0$20654$c3e8da3@news.astraweb.com> <1238794953.8087.1.camel@tim-laptop> <1238820500.1029.69.camel@tim-laptop> Message-ID: On 2009-04-03 23:48, Tim Wintle wrote: > On Fri, 2009-04-03 at 18:27 -0500, Robert Kern wrote: >>> agreed. If .clear was to be added then really assignments to slices >>> should be entirely removed. >> Please tell me you are joking. > > Well I'm not joking as such. > > I've noticed that python-ideas seems to be positive on the idea, and has > a patch ready for Guido, obviously I'm not that anti it that I'd always > be complaining if it is implemented, I just see it as unnecessary: > http://mail.python.org/pipermail/python-ideas/2009-April/003933.html Let's be clear: python-ideas seems positive on the idea of adding a .clear() method. *Completely removing* slice assignment has not been broached there. > (I didn't expect such strong responses btw!) You are proposing the removal of a general, orthogonal feature (and breaking code in consequence!) just because of a new syntax for a single special case of that feature. That is quite simply ridiculous. .clear() would be non-orthogonal syntactic sugar. That's okay! Python has syntactic sugar in a number of other places, too! Appropriate doses of syntactic sugar and non-orthogonality are precisely what lets you implement "There should be one-- and preferably only one --obvious way to do it." The really key word in that sentence is "obvious", not "one". FWIW, removing slice assignment would be a gross form of non-orthogonality, too. __getitem__, __setitem__ and __delitem__ should all be able to accept the same indices (or else raise exceptions in the case of immutability). -- 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 __peter__ at web.de Sat Apr 4 03:38:51 2009 From: __peter__ at web.de (Peter Otten) Date: Sat, 04 Apr 2009 09:38:51 +0200 Subject: Problem understanding some unit tests in Python distribution References: <99be651d-bf9f-453f-a1ae-80ce102b2431@z14g2000yqa.googlegroups.com> Message-ID: Andr? wrote: > Hi everyone, > > In the hope of perhaps contributing some additional unit tests for > Python (thus contributing back to the community), I dove in the code > and found something that should be simple but that I can not wrap my > head around. > > In list_tests.py, one finds > === > from test import test_support, seq_tests > > class CommonTest(seq_tests.CommonTest): > > def test_init(self): > # Iterable arg is optional > self.assertEqual(self.type2test([]), self.type2test()) > # etc. > === > > Wanting to figure out what the type2test() method does, I looked in > seq_tests.py and found the following: > > === > class CommonTest(unittest.TestCase): > # The type to be tested > type2test = None > > def test_constructors(self): > l0 = [] > l1 = [0] > l2 = [0, 1] > > u = self.type2test() > u0 = self.type2test(l0) > u1 = self.type2test(l1) > u2 = self.type2test(l2) > > # etc. > === > No where do I find a definition for the type2test() method - other > than seeing it as a class variable defined to be None by default. I > looked in unittest.TestCase and did not see it anywhere. > > Am I missing something obvious? I would appreciate if someone could > point me in the right direction. Use grep ;) CommonTest is not run standalone, it is used as a(n abstract) base class for several other tests that override type2test. E. g. in Lib/test/list_tests.py: from test import test_support, seq_tests class CommonTest(seq_tests.CommonTest): ... And then in Lib/test/test_list.py: from test import test_support, list_tests class ListTest(list_tests.CommonTest): type2test = list ... Peter From Graham.Dumpleton at gmail.com Sat Apr 4 03:42:04 2009 From: Graham.Dumpleton at gmail.com (Graham Dumpleton) Date: Sat, 4 Apr 2009 00:42:04 -0700 (PDT) Subject: Module caching References: <8350f62e-c620-4abc-90dc-ef3af60494f6@3g2000yqk.googlegroups.com> <3a0704ea-703a-445f-804d-73f363ca9376@c11g2000yqj.googlegroups.com> <657ed616-ca5f-4f45-b056-9db877f76beb@v15g2000yqn.googlegroups.com> <3b9b538a-0048-4730-9681-618b540c3926@z15g2000yqm.googlegroups.com> Message-ID: <64d56ba2-7ce5-42f3-a59a-f94f4cadda7b@f41g2000pra.googlegroups.com> On Apr 4, 10:41?am, Jon Clements wrote: > On 3 Apr, 23:58, Aaron Scott wrote: > > > > are you an experienced python programmer? > > > Yeah, I'd link to think I'm fairly experienced and not making any > > stupid mistakes. That said, I'm fairly new to working with mod_python. > > > All I really want is to have mod_python stop caching variables. This > > seems like it should be easy enough to do, but I can't for the life of > > me find information on how to do it. > > > Aaron > > Umm... Well, mod_python works for long running processes that don't > really store data, but return it on demand... so keeping module level > variables around is going to be a gotcha. > > It's a kludge, but setting MaxRequestsPerChild to 1 in the Apache > config basically forces a reload of everything for every request... > that might be worth a go -- but it's nasty... They may as well use CGI then. Personally I would never recommend MaxRequestsPerChild be set to 1. Anyway, this person also posted on mod_python list. One of the things I highlighted there was that mod_python for some configurations is multithreaded and as such they may not be properly protecting variables if they are storing them at global scope. They haven't responded to any comments about it on mod_python list. They were also told to read: http://www.dscpl.com.au/wiki/ModPython/Articles/TheProcessInterpreterModel Graham From john_re at fastmail.us Sat Apr 4 04:00:20 2009 From: john_re at fastmail.us (john_re) Date: Sat, 04 Apr 2009 01:00:20 -0700 Subject: TODAY April 4 -Guido & Python 3000 @ Global FSW Voice Meeting BerkeleyTIP -Linus,Guido,Shuttleworth... Message-ID: <1238832020.7235.1308924717@webmail.messagingengine.com> Guido - Python 3000 video. 5PM Python hour Anyone: Please email me or the BTIP list if you know of any recent (past 12 months) Python videos. Thanks. :) Join with the friendly, productive, Global FSW community, in the _TWICE_ monthly, Voice over internet meeting, BerkeleyTIP-Global. GNU(Linux) & BSD, Free SW, HW & FreeCulture, Talks Installfest Project/ProgrammingParty http://sites.google.com/site/berkeleytip/ NEW- _TWO_ monthly day long meetings- 10AM-6PM Pacific (GMT -8H) time, = 1P - 9P Eastern = 6PM - 2AM (Sat to Sunday) GMT Join in as short or long as you like. 1st Saturday - April 4 ***** TODAY ***** 3rd Sunday - April 20 ===== LOCATION - ONLINE, IN YOUR AREA, OR AT U. California Berkeley http://sites.google.com/site/berkeleytip/remote-attendance http://sites.google.com/site/berkeleytip/directions ===== NEW VIDEOS: Linux Torvalds - GIT Mark Shuttleworth - Debian & Ubuntu - Debian & Derived Distros Krafft Shuttleworth Levsen - Debian Derivers Roundtable Fabricio Cannini - Debian High Performance clusters and supercomputers Guido Van Rossum - Python 3000 BestTech LAMPR - Get Started with Ruby on Rails in < 5 minutes Culture - Professor Wikipedia - The funniest video of the year. [Citation needed.] [Thanks to all the speakers, videographers, & organizations. :) Please excuse if I mistyped names. <8-0 I hereby invite the speakers to attend BTIP for Q&A & discussion. Please notify the speakers if you know how to contact them, thanks. :) ] Download the videos & watch them before or during the meeting. Join online during the relevant topic hour to discuss each video. [See below for longer talk descriptions.] http://sites.google.com/site/berkeleytip/talk-videos ===== YOU GIVE A 5 MINUTE LIGHTNING TALK 4 PM. Let us know in advance what you'd like to talk about. :) ===== NEW MEETING COMPONENTS FREE CULTURE - Wikipedia, Creative Commons, etc COLLEGE MAJORS Business, Medicine, Law, Engineering, Art/Lit/Science/Social/Psych/Humanities/LiberalArts, etc SOFTWARE: Games ===== SCHEDULE / AGENDA 10AM - 6PM Pacific time (= GMT - 8 Hours) TIME TOPIC / ACTIVITY 10 A Set up. Get on IRC & VOIP 11 A Installfest; Ekiga3 12 N Asterisk, OLPC, Games; PROGRAMMING PARTY: VOIP Conference client & server 1 P Xen, Virtualbox; LAMPR - Database; Law; GNU 2 P KDE & GNOME; Macintosh; FreeCulture: Wikipedia,CreativeCommons 3 P Debian; BSD; College & University groups; Business 4 P LIGHTNING TALKS; Ubuntu Hardware- Ex: OpenMoko Phone; Medicine 5 P Art/Literature/Music; Python; INetWebDev; Local simultaneous meetings arrangements for next meeting ===== Voice/VOIP CONFERENCE MEETING TECHNOLOGY Join in on IRC, & we'll help you get on VOIP. :) IRC: #BerkeleyTIP, irc.freenode.net Hardware: VOIP Headset- (USB recommended for echo cancellation?) Software: Ekiga(GnomeMeeting) recommended. SIP VOIP server: Ekiga.net http://sites.google.com/site/berkeleytip/remote-attendance ===== LOCATION GROWTH: GREAT PROGRESS In March we QUADRUPLED our international attendance. :) Welcome :) GLOBAL: Germany, England, Iran, India US: So far: Hawaii, California, Washington, Michigan, Virgina, NCarolina When will your state or country 1st join in??? ===== PROJECT / PROGRAMMING PARTY Work on your own project, or the group project. Share details of your project on IRC, VOIP & the mailing list. Invite others to join in your project. Or, work on the group project - Learning about & Improving Ekiga, Asterisk, & our VOIP conference system/technology. ===== Join the Global BerkeleyTIP mailing list http://groups.google.com/group/BerkTIPGlobal ===== THANKS, HOPE YOU JOIN; FOR FORWARDING I hope you join in the meeting. :) Join by yourself, or invite your friends over & have a party. Have a party at your home, or at a local to you location - a WiFi cafe, or at a college or university is a great place for a meeting. :) You are invited to forward this message wherever appropriate - Ex: perhaps your local meeting group (LUG, etc), or whatever, if you see this on a global mailing list. ======================================================================= ===== VIDEOS - MORE DETAILED DESCRIPTIONS: 3 from DebConf08: 1) Fabricio Cannini - Debian High Performance clusters and supercomputers, 1hr 2) Mark Shuttleworth on Debian and Ubuntu - Keynote current state of collaboration between Debian and Ubuntu, progress made, and new opportunities for collaboration and development. 3) Debian Derivers Roundtable discussion - prominent developers of Debian-derived works, including Martin (Debian Edu). 1hr Linus Torvalds on Git - Git is a rewrite from scratch concurrent versioning system that Linus wrote to replace cvs, subversion (svn) and other versioning systems used in large collaborative software development. Benefits, drawbacks and insufficiencies of other versioning systems in common use. Google Tech Talks Guido Van Rossum, Python 3000, Google, 1h 25m Topic LAMPR: Get Started with Ruby on Rails in less than five minutes - BestTech Free Culture - Professor Wikipedia - CollegeHumor - A short but interesting take on Wikipedia, and the side effect of anyone being able to edit the content. Listen as Professor Wikipedia alters his lecture presumably because people are "changing" his lecture behind the scenes. The funniest video of the year. [Citation needed.] 02:55 From flankw at gmail.com Sat Apr 4 04:06:44 2009 From: flankw at gmail.com (Coonay.appspot) Date: Sat, 4 Apr 2009 01:06:44 -0700 (PDT) Subject: how to optimize zipimport References: <8cdf9fd4-6c87-47eb-8a9e-989336428379@v39g2000yqm.googlegroups.com> <091189e3-44f2-4f2b-b5cc-090a4d422f61@v23g2000pro.googlegroups.com> <6069d2e4-0ed0-481d-9e25-53f323cb72a2@j18g2000prm.googlegroups.com> Message-ID: <2d687b09-9e17-4855-a015-70bb5c8e3338@c18g2000prh.googlegroups.com> On Apr 2, 6:26?pm, John Machin wrote: > On Mar 30, 11:39?pm, Coonay wrote: > > > > > On Mar 26, 1:38?pm, John Machin wrote: > > > > On Mar 26, 2:06?pm, Coonay wrote: > > > > > On Mar 26, 10:41?am, Coonay wrote: > > > > > > in my mudule ,i import another mudule from a zip , > > > > > > when i call ?my mudle method,the module in the zip will be import from > > > > > the zip every time,that slow down the operation, > > > > > > i try to search the some workarodnd or solution but i don't get one, > > > i don't use zipimport directly,just like u said i put the zip in the > > sys.path > > > i found the the module in the zip reloaded ?everytime the code is > > called,i mean, say ,first time it take me 1 second to call a method in > > the zip,but it take another 1 second to call the mothod for another > > time ,and so on > > How do you know that it is being reloaded each time a methos is > called? > > > > Note: it may help when you reply to show a stripped-down version of > > > your calling module, sufficient to back up your description of what is > > > happening. > > We can only guess if you don't show us what is actually happening. > Vague descriptions are not very useful. Show us how exactly you are > importing the modules, how you are calling methods, with timing > calls ... yes , i put the pyc file with py source file in the zip > > By the way, have you read this part of the documentation: > "Note that if an archive only contains .py files, Python will not > attempt to modify the archive by adding the corresponding .pyc or .pyo > file, meaning that if a ZIP archive doesn't contain .pyc files, > importing may be rather slow." > ? > > Do you have .pyc files in your ZIP file? If not that would make the > first import slower than it should be. > > However second, third, ... imports (if you do any) should be > instantaneous -- unless you are messing about deleting modules from > sys.modules. > > And *method* calls should be instantaneous. it run much faster when i unzip the lib and put then in a directory > > Does your app run at the same speed when you have all the modules in a > directory instead of inside a zip file? > > Apart from code executed once at app start to inject the path to the > zip file into sys.path, have you made any other changes to accommodate > using import from zip? If so, what changes? > > Have you tried running your app from the shell (Windows translation: > in a Command Prompt window) like this: > > python -v app_start.py > > This will show you all the imports as they happen. > > HTH ... but a clear description from you (plus code samples) of > exactly what you are doing woul dhelp a whole lot more. > > John From andre.roberge at gmail.com Sat Apr 4 04:51:52 2009 From: andre.roberge at gmail.com (=?ISO-8859-1?Q?Andr=E9?=) Date: Sat, 4 Apr 2009 01:51:52 -0700 (PDT) Subject: Problem understanding some unit tests in Python distribution References: <99be651d-bf9f-453f-a1ae-80ce102b2431@z14g2000yqa.googlegroups.com> Message-ID: <822f8199-1c85-4931-a45b-f9c4e003ea35@3g2000yqk.googlegroups.com> On Apr 4, 4:38?am, Peter Otten <__pete... at web.de> wrote: > Andr? wrote: > > Hi everyone, > > > In the hope of perhaps contributing some additional unit tests for > > Python (thus contributing back to the community), I dove in the code > > and found something that should be simple but that I can not wrap my > > head around. > > > In list_tests.py, one finds > > === > > from test import test_support, seq_tests > > > class CommonTest(seq_tests.CommonTest): > > > ? ? def test_init(self): > > ? ? ? ? # Iterable arg is optional > > ? ? ? ? self.assertEqual(self.type2test([]), self.type2test()) > > # etc. > > === > > > Wanting to figure out what the type2test() method does, I looked in > > seq_tests.py and found the following: > > > === > > class CommonTest(unittest.TestCase): > > ? ? # The type to be tested > > ? ? type2test = None > > > ? ? def test_constructors(self): > > ? ? ? ? l0 = [] > > ? ? ? ? l1 = [0] > > ? ? ? ? l2 = [0, 1] > > > ? ? ? ? u = self.type2test() > > ? ? ? ? u0 = self.type2test(l0) > > ? ? ? ? u1 = self.type2test(l1) > > ? ? ? ? u2 = self.type2test(l2) > > > # etc. > > === > > No where do I find a definition for the type2test() method - other > > than seeing it as a class variable defined to be None by default. ?I > > looked in unittest.TestCase and did not see it anywhere. > > > Am I missing something obvious? ?I would appreciate if someone could > > point me in the right direction. > > Use grep ;) > I sort of did (use a "find all" from within my editor...) > CommonTest is not run standalone, it is used as a(n abstract) base class for > several other tests that override type2test. > > E. g. in Lib/test/list_tests.py: > > from test import test_support, seq_tests > > class CommonTest(seq_tests.CommonTest): > ... > > And then in Lib/test/test_list.py: > > from test import test_support, list_tests > > class ListTest(list_tests.CommonTest): > ? ? type2test = list I can't believe I missed that one. Thank you very much! Andr? > ... > > Peter From electriclightheads at gmail.com Sat Apr 4 05:33:05 2009 From: electriclightheads at gmail.com ('2+) Date: Sat, 4 Apr 2009 18:33:05 +0900 Subject: is there a way to collect twitts with python? In-Reply-To: <49D6F0B7.2050608@gmail.com> References: <1078018b0903271054y3d4a7162w885c61bf63bc2048@mail.gmail.com> <49D67874.8080209@gmail.com> <1238820981.1029.76.camel@tim-laptop> <49D6F0B7.2050608@gmail.com> Message-ID: <1078018b0904040233h78a94d37rb4a698c70f2e7f7@mail.gmail.com> anyway i found: http://code.google.com/p/python-twitter/ and http://mike.verdone.ca/twitter/ and both were easy to install on intrepid .. but didn't work python-twitter did work by manually downloading: http://python-twitter.googlecode.com/svn/trunk/twitter.py well but it seems like i have to follow the guy to get his twit -- 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 ganeshborse at gmail.com Sat Apr 4 06:02:24 2009 From: ganeshborse at gmail.com (grbgooglefan) Date: Sat, 4 Apr 2009 03:02:24 -0700 (PDT) Subject: How to free /destroy object created by PyTuple_New Message-ID: <3c15a8d6-3095-4fff-b902-8bef22542b86@a5g2000pre.googlegroups.com> I am using PyTuple_New to pass function arguments to PyObject_CallObject for execution of a Python function. How can I free up the memory and object allocated by the PyTuple_New function? I am using Python interpreter in my multi-threaded C application & at load condition my application crashes in Python's evaluation function. I am not explicitely releasing the PyTuple_New object. Please guide how can I destory object created by PyTuple_New? From mail at microcorp.co.za Sat Apr 4 06:34:58 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Sat, 4 Apr 2009 12:34:58 +0200 Subject: "Pythoner",Wish me luck! References: <54afb932-1a42-42d2-a3b9-0151b2bd410b@q30g2000prq.googlegroups.com> <19963172-7ac1-4796-abb7-3387a65ba91d@f19g2000yqo.googlegroups.com> Message-ID: <004b01c9b511$372346a0$0d00a8c0@Hendrik> "barisa" wrote: >Hi, >I'm also begginer in python; >i did few basic programs about graph etc.. > >my question is : what benefit is using interactive intrepreter ? > >i come from java backround, so I use eclipse for python as well. >I start my program, it does it's job, and that's it. (after some >debugging ofc) > The Interactive Interpreter is not intended to be a development environment, (although you can use it like that for small things) but it is very handy to do simple tests in when you are not sure that you understand what some new (to you) language feature does exactly. In this way you can "test bench" small snippets of code, which makes subsequent debugging of your program less onerous, and makes it more likely that the "job" your program does is in fact the "job" that you imagined it should do when you started to write it. So, for instance, lets say that it is the first time that you use the append method of the built in list class, and you are not sure what it returns. you have two basic options - you can either go look for the documentation and RTFM, or you can go into your active interpreter and just try it. The second alternative is often the quickest, and it is definitely the safest, because what happens is the definitive behaviour of your installation. Reading the manual carries the following risks: - It could be the wrong manual - The manual and the code may not be congruent Another reason for using the II is to help you discover stuff. Calling dir(something) can teach you quite a lot that is of practical import. And then there is the help system too. hth - Hendrik From trigves at yahoo.com Sat Apr 4 06:42:09 2009 From: trigves at yahoo.com (Trigve) Date: Sat, 4 Apr 2009 10:42:09 +0000 (UTC) Subject: =?utf-8?b?UHlGdW5jdGlvbl9OZXc=?= examples Message-ID: Hi, I'm trying to use PyFunction_New() function but I've problem finding out how it works. What I'm trying to do is to create python function on the fly and then trying to create function object from if via PyFunction_New. My problems are similar to this: http://article.gmane.org/gmane.comp.python.general/548300/match=pyfunction_new Please CC me. thanks Trigve From sreejithemk at gmail.com Sat Apr 4 06:56:26 2009 From: sreejithemk at gmail.com (Sreejith K) Date: Sat, 4 Apr 2009 03:56:26 -0700 (PDT) Subject: statvfs clearance Message-ID: <22347545-9b8d-4d06-88af-3aaabea41a8b@r33g2000yqn.googlegroups.com> Python's statvfs module contains the following indexes to use with os.statvfs() that contains the specified information statvfs.F_BSIZE Preferred file system block size. statvfs.F_FRSIZE Fundamental file system block size. statvfs.F_BLOCKS Total number of blocks in the filesystem. statvfs.F_BFREE Total number of free blocks. statvfs.F_BAVAIL Free blocks available to non-super user. statvfs.F_FILES Total number of file nodes. statvfs.F_FFREE Total number of free file nodes. statvfs.F_FAVAIL Free nodes available to non-super user. statvfs.F_FLAG Flags. System dependent: see statvfs man page. statvfs.F_NAMEMAX Maximum file name length. Can anyone tell me (or give me some links to know) what are these values ? The first three I know, I need to know about the rest.... From mail at microcorp.co.za Sat Apr 4 07:09:06 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Sat, 4 Apr 2009 13:09:06 +0200 Subject: Hash of None varies per-machine References: <4c62537d-583b-4441-89f9-b341247d0b13@l22g2000vba.googlegroups.com> <01e6a5ff$0$20654$c3e8da3@news.astraweb.com> Message-ID: <001001c9b515$c56f0da0$0d00a8c0@Hendrik> "Steven D'Aprano" wrote: On Fri, 03 Apr 2009 10:50:08 -0700, ben.taylor wrote: >> 2. Should the hash of None vary per-machine? I can't think why you'd >> write code that would rely on the value of the hash of None, but you >> might I guess. > >The value of hash(None) appears to be the value of id(None), which means >it is the memory address that None happens to get, which means it will >depend on the precise order that Python allocates things when it starts >up, which will vary from platform to platform and version to version. > >> 3. Given that presumably not all things can be hashed (since the >> documentation description of hash() says it gives you the hash of the >> object "if it can be hashed"), should None be hashable? > >Any object can be hashed if it has a working __hash__ method. There's no >reason not to have None hashable -- it costs nothing and allows you to >use None as a dict key. So what happens if I try to pickle the dict and keep it for next time? Will I be able to access whatever I have associated with None? (directly - mydict[None], not in a for loop.) And if I send the pickle to another machine and unpickle it, what then? - is unpickling smart enough to construct the dict with the local hash of None? - Seems to me that if it isn't, and you want to do this, there would have to be a fixed, well known value for the hash of None. - Hendrik From deets at nospam.web.de Sat Apr 4 07:10:36 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sat, 04 Apr 2009 13:10:36 +0200 Subject: Undefined symbol PyUnicodeUCS2_DecodeUTF8 In-Reply-To: References: Message-ID: <73othdFtu8jhU1@mid.uni-berlin.de> Manish Jain schrieb: > > Hi all, > > I am using Gnome on FreeBSD 7.1. A few days back, my gnome crashed and I > have had to spend over 4 days recovering my gnome environment. Pretty > much everything is okay now except for a few python-dependent > applications (alacarte, for instance), which exit immediately with the > following error message : > >> Traceback (most recent call last): >> File "/usr/local/bin/alacarte", line 22, in >> from Alacarte.MainWindow import MainWindow >> File >> "/usr/local/lib/python2.5/site-packages/Alacarte/MainWindow.py", line >> 19, in >> import gtk, gtk.glade, gmenu, gobject, gio >> File >> "/usr/local/lib/python2.5/site-packages/gtk-2.0/gtk/__init__.py", line >> 38, in >> import gobject as _gobject >> File >> "/usr/local/lib/python2.5/site-packages/gtk-2.0/gobject/__init__.py", >> line 33, in >> from glib import spawn_async, idle_add, timeout_add, >> timeout_add_seconds, \ >> File >> "/usr/local/lib/python2.5/site-packages/gtk-2.0/glib/__init__.py", >> line 30, in >> from glib._glib import * >> ImportError: >> /usr/local/lib/python2.5/site-packages/gtk-2.0/glib/_glib.so: >> Undefined symbol "PyUnicodeUCS2_DecodeUTF8" >> >> [1]+ Exit 1 alacarte > > > I have searched google/yahoo for any help without luck. I have rebuilt > all python and gtk2 ports from scratch but the error continues. > > Can anybody please point out the source and/or remedy for the problem ? > I am using the python25 port. Your problem is that your Python interpreter is not compiled with the same settings as the binary extensions you use. Python can use UTF16 or UTF32 as internal representation for unicode. AFAIK nowadays, distributions agreed to use the latter. The gtk-binding of yours seems to be compiled against an interpreter that uses the former. So you have to either recompile python to match the extensions expectations - or the other way round. I'd suggest the latter, as you might have a mixture of extensions which some of then won't work with the new interpreter. Diez From marduk at letterboxes.org Sat Apr 4 07:19:07 2009 From: marduk at letterboxes.org (Albert Hopkins) Date: Sat, 04 Apr 2009 07:19:07 -0400 Subject: statvfs clearance In-Reply-To: <22347545-9b8d-4d06-88af-3aaabea41a8b@r33g2000yqn.googlegroups.com> References: <22347545-9b8d-4d06-88af-3aaabea41a8b@r33g2000yqn.googlegroups.com> Message-ID: <1238843947.24882.2.camel@blackwidow.nbk> On Sat, 2009-04-04 at 03:56 -0700, Sreejith K wrote: > Python's statvfs module contains the following indexes to use with > os.statvfs() that contains the specified information > > statvfs.F_BSIZE > Preferred file system block size. > > statvfs.F_FRSIZE > Fundamental file system block size. > > statvfs.F_BLOCKS > Total number of blocks in the filesystem. > > statvfs.F_BFREE > Total number of free blocks. > > statvfs.F_BAVAIL > Free blocks available to non-super user. > > statvfs.F_FILES > Total number of file nodes. > > statvfs.F_FFREE > Total number of free file nodes. > > statvfs.F_FAVAIL > Free nodes available to non-super user. > > statvfs.F_FLAG > Flags. System dependent: see statvfs man page. > > statvfs.F_NAMEMAX > Maximum file name length. > > Can anyone tell me (or give me some links to know) what are these > values ? The first three I know, I need to know about the rest.... The values would obviously depend on your particular system, which we don't have. So how could we, for example, know how many free blocks you have on your filesystem? From mail at microcorp.co.za Sat Apr 4 07:22:07 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Sat, 4 Apr 2009 13:22:07 +0200 Subject: is there a way to collect twitts with python? References: <1078018b0903271054y3d4a7162w885c61bf63bc2048@mail.gmail.com><49D67874.8080209@gmail.com> Message-ID: <001601c9b517$99b94160$0d00a8c0@Hendrik> "Dennis Lee Bieber" wrote: > Given the subject line -- my first thought was "Depends on the > density of the twitt population, and how hungry the python is" I see that everybody is politically correctly maintaining the three "t" twitt spelling, instead of yielding to the obvious temptation to drop the last one. Dennis, your reference to "density" comes perilously close... - Hendrik From newptcai at gmail.com Sat Apr 4 07:30:39 2009 From: newptcai at gmail.com (=?GB2312?B?0rvK18qr?=) Date: Sat, 4 Apr 2009 04:30:39 -0700 (PDT) Subject: User or UserManager ? Problems of Observer Pattern References: <57f21569-644e-4c10-8702-5e2363f81f74@f19g2000yqh.googlegroups.com> Message-ID: Thanks for your advice. I studied python from the tutorial and the library manual, and I think I am familiar enough with Python's grammar and API. That's why I never thought I need to read a book of Python. But if "Programming Python" also explains OO, I would be happy to read it. In fact, I am also planning to read again "Head First Object-Oriented Design and Analysis". I thought it's time for me to try again to find what is the *point* of Obejct. Anyway, this time I will be patient enough. I won't touch my code until I am sure find some way to improve it. On Apr 4, 1:14 pm, Michele Simionato wrote: > On Apr 3, 7:34 pm, ??? wrote: > > > > > #---------------- > > # Choice 2 > > #---------------- > > > class UserManager: > > > users = [] > > > @classmethod > > def delUser(cls, user): > > cls.users.remove(user) > > > RoleManager.onUserDel(user) > > > class RoleManager: > > > roles = [] > > > @classmethod > > def onUserDel(cls, user): > > for r in cls.roles.items(): > > r.users.remove(user) > > Choice #2 is better, but it should not be implemented > this way. Using a class as a pure container of methods, > a big singleton, makes little sense, since you would > be better off with a module and old fashioned procedural > programming. You recognize this and are worried about this. > You do not need to be worried to much, > since there is nothing wrong with procedural design > (but you should use modules not classes). If you want > to be more object oriented, the first thing is to understand > is that you define objects in order to pass them to > other objects. For instance, you are using a global > list of users as a class attribute, but I would > just pass the list in the __init__ method, and > make it an instance attribute. I also would make > a RoleManager instance and pass it to the UserManager, > so that it can be used directly. Or perhaps I would > define a single UserRoleManager doing all the job. > It depends. > But all the point of OOP is to pass objects to other objects. > It is not bad to think of a data object as of a record > on steroids. Objects which are purely data and not > behavior are the simplest and the best objects, start > from them. > There are also objects which are mostly behavior > and nearly no data (the Manager objects here, which > just act over collections of other objects). Those > are more difficult to write. OTOH, nobody forces you > to write the managers as objects. You may find easier > to write a set of manager functions. This set of > functions can later become methods of a Manager object, > if it seems fit, but that must be done a posteriori, > not a priori. > Perhaps you should read some book like "Programming Python" > by Ludz which has many examples of how to write OO applications > in Python (I mean no offense, but indeed from you examples > is seems to me that you are missing the point of OOP, > as you are the first to recognize). > HTH, > > Michele Simionato From steve at REMOVE-THIS-cybersource.com.au Sat Apr 4 07:44:52 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 04 Apr 2009 11:44:52 GMT Subject: Hash of None varies per-machine References: <4c62537d-583b-4441-89f9-b341247d0b13@l22g2000vba.googlegroups.com> <01e6a5ff$0$20654$c3e8da3@news.astraweb.com> Message-ID: <01e73c74$0$20654$c3e8da3@news.astraweb.com> On Sat, 04 Apr 2009 13:09:06 +0200, Hendrik van Rooyen wrote: >>Any object can be hashed if it has a working __hash__ method. There's no >>reason not to have None hashable -- it costs nothing and allows you to >>use None as a dict key. > > So what happens if I try to pickle the dict and keep it for next time? You pickle the dict and keep it for next time. > Will I be able to access whatever I have associated with None? Yes. > (directly > - mydict[None], not in a for loop.) And if I send the pickle to another > machine and unpickle it, what then? It just works. > - is unpickling smart enough to > construct the dict with the local hash of None? Yes. > - Seems to me that if it isn't, and you want to do this, there would > have to be a fixed, well known value for the hash of None. Seems to me you have misunderstood the way pickling works. On one machine: >>> import pickle >>> pickle.dump({None: "hello world"}, open("pickled", 'w')) And then on another: >>> import pickle >>> pickle.load(open('pickled')) {None: 'hello world'} It just works. -- Steven From davea at dejaviewphoto.com Sat Apr 4 07:52:55 2009 From: davea at dejaviewphoto.com (Dave Angel) Date: Sat, 04 Apr 2009 06:52:55 -0500 Subject: django model problem In-Reply-To: <2jd2j8x0sir0.1jje7ogy6pgpd.dlg@40tude.net> References: <19a1cxf57zsli.1wmngjrxnlagf$.dlg@40tude.net> <2jd2j8x0sir0.1jje7ogy6pgpd.dlg@40tude.net> Message-ID: <49D74A17.4030101@dejaviewphoto.com> Mark wrote: >> I think the first thing you need to do is decide if there is going to be >> more than one Musician object. and more than one Album object. >> Presently you are giving all musicians the same first_name and >> last_name. I suggest you look up the documentation for the special >> method __init__() >> >> Then you need to realize that assigning new attributes to an instance >> object needs to be done inside an instance method, either __init__() or >> some other method with a self parameter. >> > > > What you say would normally make sense, but it's Django and it works that > way here. See: > http://docs.djangoproject.com/en/dev/topics/db/models/#fields > > > Regards > MS > > Sorry, somehow I missed the subject line. I have no experience with Django, but once I got your message, I read a little of the introduction. Apparently the static fields in a class in models.py are treated specially, and describe rows of the database, and the relationships between those rows. Then I guess Django generates the necessary instance attributes and methods to make it live. If I had to guess, I'd say your redundant fields were caused by you defining a one-to-one relationship between objects that already had a many-to-one relationship. Presumably there's already a method (or attribute) to get a list of albums from musician object. You might better just add a date field to the Album class, and make a method or function to sort that list. Anyway, since I don't have time to actually install and configure Django to experiment, I'd suggest you post a query on the django-users mailing list, at http://groups.google.com/group/django-users Sorry I couldn't be more help. DaveA From newptcai at gmail.com Sat Apr 4 07:54:48 2009 From: newptcai at gmail.com (=?GB2312?B?0rvK18qr?=) Date: Sat, 4 Apr 2009 04:54:48 -0700 (PDT) Subject: A design problem I met again and again. References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> <33e08fde-1d4e-41a2-9ce2-7e1bec3abd90@r29g2000vbp.googlegroups.com> <793a9568-7c89-412e-b14c-e2dd28640577@s22g2000prg.googlegroups.com> <1b60f999-aaf8-4b10-bd1f-ab46f03f44f6@m36g2000vbp.googlegroups.com> <22c65831-cdfb-4ac3-95d9-215ba338e89a@u9g2000pre.googlegroups.com> Message-ID: <1425d754-10bc-4a8d-b7f9-684ca5d4a63a@f1g2000prb.googlegroups.com> That's clever. I never thought of that. Not only something concrete, like people, could be class, but a procedure, like a Session, could also be a Class. Thanks for you all who replied. I learned a lot from this thread and I even made some notes of all your advices because I think I might review them many times in my future work. On Apr 4, 12:10 pm, Carl Banks wrote: > On Apr 2, 11:25 pm, ??? wrote: > > > > > Consolidate existing functions? > > > I've thought about it. > > > For example, I have two functions: > > > #========================= > > > def startXXX(id): > > pass > > > def startYYY(id): > > pass > > #========================= > > > I could turn it into one: > > > #========================= > > def start(type, id): > > if(type == "XXX"): > > pass > > else if(type == "YYY"): > > pass > > #========================= > > > But isn't the first style more clear for my code's user? > > Not necessarily, especially if the user wants to dynamically choose > which start*** function to call. > > I have one more suggestion. Consider whether there are groups of > methods that are used together but aren't used with other groups of > functions. For instance, maybe there is a group of methods that can > only be called after a call to startXXX. If that's the case, you > might want to separate those groups into different classes. The > branched-off class would then act as a sort of session handler. > > A piece of user code that looked like this (where sc is an instance of > your enormous class): > > sc.startX() > sc.send_data_via_X() > sc.receive_data_via_X() > sc.stopX() > > might look like this after you factor it out: > > session = sc.startX() # creates and returns a new XSession object > session.send_data() # these are methods of the XSession > session.receive_data() > session.stop() > > Any methods that are callable any time, you can retain in the big > class, or put in a base class of all the sessions. > > Carl Banks From irmen.NOSPAM at xs4all.nl Sat Apr 4 08:05:46 2009 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Sat, 04 Apr 2009 14:05:46 +0200 Subject: [ANN] Pyro 3.9 released Message-ID: <49d74d1b$0$190$e4fe514c@news.xs4all.nl> Hi, Pyro 3.9 has been released! Pyro is a an advanced and powerful Distributed Object Technology system written entirely in Python, that is designed to be very easy to use. Have a look at http://pyro.sourceforge.net for more information. Highlights of this release are: - improved compatibility with Jython, - fixed a deadlock bug in the name server proxy, - fixed mobile code problem with dependent modules, - manual improvements - script tool improvements As always the detailed changes are in the changes chapter in the manual. You can download Pyro 3.9 from sourceforge: http://sourceforge.net/projects/pyro Enjoy, Irmen de Jong From dikkie at nospam.org Sat Apr 4 08:10:24 2009 From: dikkie at nospam.org (Dikkie Dik) Date: Sat, 04 Apr 2009 14:10:24 +0200 Subject: Cannot find text in *.py files with Windows Explorer? In-Reply-To: References: Message-ID: <49d74e30$0$10743$bf4948fe@news.tele2.nl> > Anybody have a solution for Windows (XP) Explorer search not finding > ordinary text in *.py files? This is a known issue. You won't be able to search PHP or even VBScript files either, as they are not "known" text formats and are therefore considered "binary, nothing to see here, move along please". I lost the link I used, but this site seems to have the info also: http://www.petri.co.il/windows_xp_search_bug.htm Best regards. From martin at v.loewis.de Sat Apr 4 08:24:34 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Sat, 04 Apr 2009 14:24:34 +0200 Subject: PEP 382: Namespace Packages In-Reply-To: <3512eadf-ab56-4be9-9c84-5c5f60293ec2@r15g2000vbi.googlegroups.com> References: <3512eadf-ab56-4be9-9c84-5c5f60293ec2@r15g2000vbi.googlegroups.com> Message-ID: <49d75185$0$30884$9b622d9e@news.freenet.de> > -0 > > My main concern is that we'll start seeing all kinds of packages with > names like: > > com.dusinc.sarray.ptookkit.v_1_34_beta.btree.BTree > > The current lack of global package namespace effectively prevents > bureaucratic package naming, which in my mind makes it worth the > cost. However, I'd be willing to believe this can be kept under > control some other way. In principle, people can do this today already. That they are not doing it is a good sign. I think this bureaucratic naming in Java originates more from an explicitly stated policy that people should use such naming than from the ability to actually do so easily. Regards, Martin From martin at v.loewis.de Sat Apr 4 08:27:06 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Sat, 04 Apr 2009 14:27:06 +0200 Subject: PEP 382: Namespace Packages In-Reply-To: References: <49D4DA72.60401@v.loewis.de> Message-ID: <49d7521c$0$30884$9b622d9e@news.freenet.de> Neal Becker wrote: > While solving this problem, is it possible also to address an issue that > shows up in certain distributions? I'm specifically talking about the fact > that on Redhat/Fedora, we have on x86_64 both /usr/lib/pythonxx/ and > /usr/lib64/pythonxx. The former is supposed to be for non-arch specific > packages (pure python) and the latter for arch-specific. I can't see how this is related to this PEP. It's not at all about how sys.path should be constructed. Regards, Martin From andrew at acooke.org Sat Apr 4 08:29:40 2009 From: andrew at acooke.org (andrew cooke) Date: Sat, 4 Apr 2009 08:29:40 -0400 (CLT) Subject: A design problem I met again and again. In-Reply-To: <1425d754-10bc-4a8d-b7f9-684ca5d4a63a@f1g2000prb.googlegroups.com> References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> <33e08fde-1d4e-41a2-9ce2-7e1bec3abd90@r29g2000vbp.googlegroups.com> <793a9568-7c89-412e-b14c-e2dd28640577@s22g2000prg.googlegroups.com> <1b60f999-aaf8-4b10-bd1f-ab46f03f44f6@m36g2000vbp.googlegroups.com> <22c65831-cdfb-4ac3-95d9-215ba338e89a@u9g2000pre.googlegroups.com> <1425d754-10bc-4a8d-b7f9-684ca5d4a63a@f1g2000prb.googlegroups.com> Message-ID: <881e5983fd289b9b8fbf247e8f04ebe8.squirrel@localhost> Note sure who wrote: >> > Consolidate existing functions? >> >> > I've thought about it. >> >> > For example, I have two functions: >> >> > #========================= >> >> > def startXXX(id): >> > pass >> >> > def startYYY(id): >> > pass >> > #========================= >> >> > I could turn it into one: >> >> > #========================= >> > def start(type, id): >> > if(type == "XXX"): >> > pass >> > else if(type == "YYY"): >> > pass >> > #========================= in general, if you are testing type that is an indication you should refactor to use a base class with subclassing. for example: def my_print(x): print 'i have a ', if type(x) is Foo: print 'furry foo' elif type(x) is Bar: print 'bubbly Bar; else: print 'strange beast' might be refactored as class MyPrintable(object): def my_print(self): print 'i have a ', self.describe() class Foo(MyPrintable): def describe(self): return 'furry Foo' etc etc. it's not always possible, but any type() or isinstance() in an OO program is a big red flag that the design is wrong. andrew From andrew at acooke.org Sat Apr 4 08:32:00 2009 From: andrew at acooke.org (andrew cooke) Date: Sat, 4 Apr 2009 08:32:00 -0400 (CLT) Subject: A design problem I met again and again. In-Reply-To: <881e5983fd289b9b8fbf247e8f04ebe8.squirrel@localhost> References: <48506803-a6b9-432b-acef-b75f76e907da@v23g2000pro.googlegroups.com> <33e08fde-1d4e-41a2-9ce2-7e1bec3abd90@r29g2000vbp.googlegroups.com> <793a9568-7c89-412e-b14c-e2dd28640577@s22g2000prg.googlegroups.com> <1b60f999-aaf8-4b10-bd1f-ab46f03f44f6@m36g2000vbp.googlegroups.com> <22c65831-cdfb-4ac3-95d9-215ba338e89a@u9g2000pre.googlegroups.com> <1425d754-10bc-4a8d-b7f9-684ca5d4a63a@f1g2000prb.googlegroups.com> <881e5983fd289b9b8fbf247e8f04ebe8.squirrel@localhost> Message-ID: <1e7b6244f30ce766f526bf5121dc4bee.squirrel@localhost> andrew cooke wrote: [...] >>> > #========================= >>> > def start(type, id): >>> > if(type == "XXX"): >>> > pass >>> > else if(type == "YYY"): >>> > pass >>> > #========================= i just realised i am assuming type is a type of an object, but you might be using it to mean something else here, in which case case my advice might be irrelevant (but not necessarily - the value of type might still depend on an object type in some way, and often does in oo programming). andrew From sjmachin at lexicon.net Sat Apr 4 08:45:11 2009 From: sjmachin at lexicon.net (John Machin) Date: Sat, 4 Apr 2009 05:45:11 -0700 (PDT) Subject: how to optimize zipimport References: <8cdf9fd4-6c87-47eb-8a9e-989336428379@v39g2000yqm.googlegroups.com> <091189e3-44f2-4f2b-b5cc-090a4d422f61@v23g2000pro.googlegroups.com> <6069d2e4-0ed0-481d-9e25-53f323cb72a2@j18g2000prm.googlegroups.com> <2d687b09-9e17-4855-a015-70bb5c8e3338@c18g2000prh.googlegroups.com> Message-ID: <5b0d6c9e-e9d6-43b7-9803-9e5cf45db674@s1g2000prd.googlegroups.com> On Apr 4, 7:06?pm, "Coonay.appspot" wrote: > On Apr 2, 6:26?pm, John Machin wrote: [snip] > > > i found the the module in the zip reloaded ?everytime the code is > > > called,i mean, say ,first time it take me 1 second to call a method in > > > the zip,but it take another 1 second to call the mothod for another > > > time ,and so on > > > How do you know that it is being reloaded each time a methos is > > called? You haven't answered the above question. > > > > Note: it may help when you reply to show a stripped-down version of > > > > your calling module, sufficient to back up your description of what is > > > > happening. > > > We can only guess if you don't show us what is actually happening. > > Vague descriptions are not very useful. Show us how exactly you are > > importing the modules, how you are calling methods, with timing > > calls ... No answer. [snip] > > Apart from code executed once at app start to inject the path to the > > zip file into sys.path, have you made any other changes to accommodate > > using import from zip? If so, what changes? No answer. > > Have you tried running your app from the shell (Windows translation: > > in a Command Prompt window) like this: > > > python -v app_start.py > > > This will show you all the imports as they happen. No answer. > > HTH ... but a clear description from you (plus code samples) of > > exactly what you are doing woul dhelp a whole lot more. No answer. Conclusion: you don't actually want any help. From gnewsg at gmail.com Sat Apr 4 09:29:58 2009 From: gnewsg at gmail.com (Giampaolo Rodola') Date: Sat, 4 Apr 2009 06:29:58 -0700 (PDT) Subject: python needs leaning stuff from other language References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <73kt5jFvitcdU1@mid.uni-berlin.de> <49D54209.5090306@hotmail.com> <01e56a2f$0$20654$c3e8da3@news.astraweb.com> <87y6uie9ej.fsf@benfinney.id.au> <01e624b7$0$20654$c3e8da3@news.astraweb.com> <01e6ab4c$0$20654$c3e8da3@news.astraweb.com> <43a2034b-c520-45d0-af3c-951e60fbc86a@e18g2000yqo.googlegroups.com> <01e6b7db$0$20654$c3e8da3@news.astraweb.com> Message-ID: <1f1f0da3-4e70-47df-bb38-ee300922cd03@q9g2000yqc.googlegroups.com> On 4 Apr, 04:19, Steven D'Aprano wrote: > On Fri, 03 Apr 2009 18:52:52 -0700, Giampaolo Rodola' wrote: > > If "there should be one-- and preferably only one --obvious way to do > > it" then my_list.clear() is more obvious than del my_list[:]. Honestly > > I'm a little surprised that such a topic hasn't been raised before. > > I'm a little surprised that you didn't bother googling before making that > statement. Of course it has been raised before. See (for example) these > threads: > > http://mail.python.org/pipermail/python-dev/2005-July/054564.html > > http://mail.python.org/pipermail/python-list/2006-April/549278.html > > http://mail.python.org/pipermail/python-list/2006-May/556977.html > > http://mail.python.org/pipermail/python-ideas/2007-May/000692.html > > The good news is that Python-Ideas seems to be receptive to the idea, > including some heavy-weights like Raymond Hettinger, which leads me to be > hopeful that Python-Dev and Guido himself will agree: > > http://mail.python.org/pipermail/python-ideas/2009-April/003897.html > > Keep your fingers crossed that this is the last time we need to have this > discussion! > > -- > Steven Sorry, it was 3:52 AM, that's why I didn't bother. =) --- Giampaolo http://code.google.com/p/pyftpdlib From grkuntzmd at gmail.com Sat Apr 4 09:37:44 2009 From: grkuntzmd at gmail.com (grkuntzmd at gmail.com) Date: Sat, 4 Apr 2009 06:37:44 -0700 (PDT) Subject: Testing dynamic languages Message-ID: <70d345df-33ca-4628-9fe9-dda113b5a2f4@n20g2000vba.googlegroups.com> I am a Java developer. There, I said it :-). When I am writing code, I can rely on the compiler to confirm that any methods I write will be called with parameters of the "right" type. I do not need to test that parameter #1 really is a String before I call some method on it that only works on Strings. If I am writing in Python, since it is dynamically, but strongly typed, I really should check that each parameter is of the expected type, or at least can respond to the method I plan on calling ("duck" typing). Every call should be wrapped in a try/except statement to prevent the method (and program) from crashing when my method is called with an integer instead of the expected string. Is this the experience that Python programmer (of large projects) see? Do you also write unit tests to confirm that the methods actually check for and catch "bad" parameter types? If I am writing small one- off scripts, I wouldn't worry about it, but if I am writing a large system that must have 99+% uptime without constant monitoring, this really should be verified. Up for discussion... From frr149 at gmail.com Sat Apr 4 09:42:04 2009 From: frr149 at gmail.com (Fernando) Date: Sat, 4 Apr 2009 06:42:04 -0700 (PDT) Subject: Trouble with subprocess.Popen() Message-ID: <60a4ee35-978e-401c-ba8e-5b78f1e8dc5a@o11g2000yql.googlegroups.com> Hi, I'm having a very strange issue with subprocess.Popen. I'm using it to call several times an external exe and keep the output in a list. Every time you call this external exe, it will return a different string. However, if I call it several times using Popen, it will always return the SAME string. =:-O It looks like Popen is returning always the same value from stdout, without recalling the exe. This is my code: ------------------------------------------------------------------------------------------------------------------------------------ def get_key(): from subprocess import Popen, PIPE args = [C_KEY_MAKER, '/26', USER_NAME, ENCRYPTION_TEMPLATE, '0', ] process = Popen(args, stdout=PIPE) output = process.communicate()[0].strip() return output print get_key() # Returns a certain string print get_key() # Should return another string, but returns the same! ------------------------------------------------------------------------------------------------------------------------------------- What on Earth am I doing wrong?! From dotancohen at gmail.com Sat Apr 4 09:45:13 2009 From: dotancohen at gmail.com (Dotan Cohen) Date: Sat, 4 Apr 2009 16:45:13 +0300 Subject: is there a way to collect twitts with python? In-Reply-To: <1078018b0903271054y3d4a7162w885c61bf63bc2048@mail.gmail.com> References: <1078018b0903271054y3d4a7162w885c61bf63bc2048@mail.gmail.com> Message-ID: <880dece00904040645g7419624cyc1b328fd2af7d90f@mail.gmail.com> Python can in fact collect twits: http://www.johnwoodwardphotography.com/images/snake_girl_promo_13_x_19.jpg http://www.bennadel.com/resources/uploads/been_busy_at_kinky_solutions.jpg -- Dotan Cohen http://what-is-what.com http://gibberish.co.il From hniksic at xemacs.org Sat Apr 4 09:48:38 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Sat, 04 Apr 2009 15:48:38 +0200 Subject: statvfs clearance References: <22347545-9b8d-4d06-88af-3aaabea41a8b@r33g2000yqn.googlegroups.com> Message-ID: <87iqlk7cnd.fsf@busola.homelinux.net> Sreejith K writes: > Python's statvfs module contains the following indexes to use with > os.statvfs() that contains the specified information > > statvfs.F_BSIZE > Preferred file system block size. [...] > statvfs.F_NAMEMAX > Maximum file name length. > > Can anyone tell me (or give me some links to know) what are these > values ? The first three I know, I need to know about the rest.... You can simply print them, they are integers: >>> import statvfs >>> statvfs.F_FLAG 8 From martin.hellwig at dcuktec.org Sat Apr 4 09:59:16 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Sat, 04 Apr 2009 14:59:16 +0100 Subject: Testing dynamic languages In-Reply-To: <70d345df-33ca-4628-9fe9-dda113b5a2f4@n20g2000vba.googlegroups.com> References: <70d345df-33ca-4628-9fe9-dda113b5a2f4@n20g2000vba.googlegroups.com> Message-ID: grkuntzmd at gmail.com wrote: > If I am writing in Python, since it is dynamically, but strongly > typed, I really should check that each parameter is of the expected > type, or at least can respond to the method I plan on calling ("duck" > typing). Every call should be wrapped in a try/except statement to > prevent the method (and program) from crashing when my method is > called with an integer instead of the expected string. Logically speaking, you only need one try/except at the very top of the program if you only want to catch these things, but that would be a really, really, really bad advice. I wonder if it wouldn't be better to design the program in such a way that the original input is handled in a way that it doesn't conflict with what you want to do with. m2c -- mph From andrew at acooke.org Sat Apr 4 10:11:47 2009 From: andrew at acooke.org (andrew cooke) Date: Sat, 4 Apr 2009 10:11:47 -0400 (CLT) Subject: Testing dynamic languages In-Reply-To: <70d345df-33ca-4628-9fe9-dda113b5a2f4@n20g2000vba.googlegroups.com> References: <70d345df-33ca-4628-9fe9-dda113b5a2f4@n20g2000vba.googlegroups.com> Message-ID: <2d56f77d6c2ec248ac654684cdd8292b.squirrel@localhost> grkuntzmd at gmail.com wrote: > If I am writing in Python, since it is dynamically, but strongly > typed, I really should check that each parameter is of the expected > type, or at least can respond to the method I plan on calling ("duck" > typing). Every call should be wrapped in a try/except statement to > prevent the method (and program) from crashing when my method is > called with an integer instead of the expected string. > > Is this the experience that Python programmer (of large projects) see? > Do you also write unit tests to confirm that the methods actually > check for and catch "bad" parameter types? If I am writing small one- > off scripts, I wouldn't worry about it, but if I am writing a large > system that must have 99+% uptime without constant monitoring, this > really should be verified. if you are going to do that, stay with java. seriously - i too, am a java developer about half the time, and you can make java pretty dynamic if you try hard enough. look at exploiting aspects and functional programming libraries, for example. the standard solution to what you describe (which is itself a standard problem with dynamic languages) is unit testing. proponents of dynamic languages argue that you need to do testing anyway for its other advantages. proponents of statically typed languages argue that you need to write more. the best solution is probably to use a better language (like ocaml or even haskell) (and i am only half joking). asserting types isn't really going to work once you start exploiting just what python can do (although look at abstract base classes - abcs - which make things easier). personally, i find that i write slightly more tests in python than java and that my python code is, to be honest, slightly less reliable. i also add type assertions at critical points when refactoring (but otherwise not). so in summary: - yes it's an issue - solve it with more unit testing, not type assertions - use the right tool for the job (which might be java) all the above just my experience. i tend to use java for the server side and python for getting data into the database, so each plays to its strengths. andrew From sjmachin at lexicon.net Sat Apr 4 10:14:29 2009 From: sjmachin at lexicon.net (John Machin) Date: Sat, 4 Apr 2009 07:14:29 -0700 (PDT) Subject: Cannot find text in *.py files with Windows Explorer? References: Message-ID: <410d24a2-6280-46f5-ba87-ae5c1bcec534@y33g2000prg.googlegroups.com> On Apr 4, 3:21?pm, John Doe wrote: > Anybody have a solution for Windows (XP) Explorer search not finding > ordinary text in *.py files? > Get a grep on yourself! http://gnuwin32.sourceforge.net/packages/grep.htm From emmanuel.surleau at gmail.com Sat Apr 4 10:17:58 2009 From: emmanuel.surleau at gmail.com (Emmanuel Surleau) Date: Sat, 4 Apr 2009 16:17:58 +0200 Subject: Testing dynamic languages In-Reply-To: <70d345df-33ca-4628-9fe9-dda113b5a2f4@n20g2000vba.googlegroups.com> References: <70d345df-33ca-4628-9fe9-dda113b5a2f4@n20g2000vba.googlegroups.com> Message-ID: <200904041617.58660.emmanuel.surleau@gmail.com> On Saturday 04 April 2009 15:37:44 grkuntzmd at gmail.com wrote: > I am a Java developer. There, I said it :-). > > When I am writing code, I can rely on the compiler to confirm that > any methods I write will be called with parameters of the "right" > type. I do not need to test that parameter #1 really is a String > before I call some method on it that only works on Strings. > > If I am writing in Python, since it is dynamically, but strongly > typed, I really should check that each parameter is of the expected > type, or at least can respond to the method I plan on calling ("duck" > typing). Every call should be wrapped in a try/except statement to > prevent the method (and program) from crashing when my method is > called with an integer instead of the expected string. Well, it depends on what you're trying to do, really. You do *not* want to check parameters in every function. If you are designing a library, it is strongly advised to document what kind of parameters your functions expect. Otherwise, if your program will handle arbitrary input, you should check its format and reject it if it's invalid. Cheers, Emm From ptmcg at austin.rr.com Sat Apr 4 10:26:05 2009 From: ptmcg at austin.rr.com (Paul McGuire) Date: Sat, 4 Apr 2009 07:26:05 -0700 (PDT) Subject: Best way to extract from regex in if statement References: <7xvdplupbi.fsf@ruckus.brouhaha.com> Message-ID: <2526c6c5-ec22-49c2-9e6e-f23296c9794a@o6g2000yql.googlegroups.com> On Apr 3, 9:26?pm, Paul Rubin wrote: > bwgoudey writes: > > elif re.match("^DATASET:\s*(.+) ", line): > > ? ? ? ? m=re.match("^DATASET:\s*(.+) ", line) > > ? ? ? ? print m.group(1)) > > Sometimes I like to make a special class that saves the result: > > ? class Reg(object): ? # illustrative code, not tested > ? ? ?def match(self, pattern, line): > ? ? ? ? self.result = re.match(pattern, line) > ? ? ? ? return self.result > I took this a little further, *and* lightly tested it too. Since this idiom makes repeated references to the input line, I added that to the constructor of the matching class. By using __call__, I made the created object callable, taking the RE expression as its lone argument and returning a boolean indicating match success or failure. The result of the re.match call is saved in self.matchresult. By using __getattr__, the created object proxies for the results of the re.match call. I think the resulting code looks pretty close to the original C or Perl idiom of cascading "elif (c=re_expr_match("..."))" blocks. (I thought about cacheing previously seen REs, or adding support for compiled REs instead of just strings - after all, this idiom usually occurs in a loop while iterating of some large body of text. It turns out that the re module already caches previously compiled REs, so I left my cacheing out in favor of that already being done in the std lib.) -- Paul import re class REmatcher(object): def __init__(self,sourceline): self.line = sourceline def __call__(self, regexp): self.matchresult = re.match(regexp, self.line) self.success = self.matchresult is not None return self.success def __getattr__(self, attr): return getattr(self.matchresult, attr) This test: test = """\ ABC 123 xyzzy Holy Hand Grenade Take the pebble from my hand, Grasshopper """ outfmt = "'%s' is %s [%s]" for line in test.splitlines(): matchexpr = REmatcher(line) if matchexpr(r"\d+$"): print outfmt % (line, "numeric", matchexpr.group()) elif matchexpr(r"[a-z]+$"): print outfmt % (line, "lowercase", matchexpr.group()) elif matchexpr(r"[A-Z]+$"): print outfmt % (line, "uppercase", matchexpr.group()) elif matchexpr(r"([A-Z][a-z]*)(\s[A-Z][a-z]*)*$"): print outfmt % (line, "a proper word or phrase", matchexpr.group()) else: print outfmt % (line, "something completely different", "...") Produces: 'ABC' is uppercase [ABC] '123' is numeric [123] 'xyzzy' is lowercase [xyzzy] 'Holy Hand Grenade' is a proper word or phrase [Holy Hand Grenade] 'Take the pebble from my hand, Grasshopper' is something completely different [...] From andrew.svetlov at gmail.com Sat Apr 4 10:27:56 2009 From: andrew.svetlov at gmail.com (Andrew Svetlov) Date: Sat, 4 Apr 2009 07:27:56 -0700 (PDT) Subject: How to free /destroy object created by PyTuple_New References: <3c15a8d6-3095-4fff-b902-8bef22542b86@a5g2000pre.googlegroups.com> Message-ID: <0aa96d3a-f646-4f98-b8c9-934985411917@v6g2000vbb.googlegroups.com> To destroy every python object you need to call Py_DECREF. To call python code fron you C thread you need to use pair PyGILState_Ensure/PyGILState_Release. From gert.cuykens at gmail.com Sat Apr 4 10:29:22 2009 From: gert.cuykens at gmail.com (gert) Date: Sat, 4 Apr 2009 07:29:22 -0700 (PDT) Subject: with open('com1', 'r') as f: References: <8bc55c05-19da-41c4-b916-48e0a4be49b9@p11g2000yqe.googlegroups.com> Message-ID: <91e09eaf-5a25-4a6b-b131-a5245970b337@f19g2000yqh.googlegroups.com> On Apr 4, 12:58?am, Lawrence D'Oliveiro wrote: > In message <8bc55c05-19da-41c4- > > b916-48e0a4be4... at p11g2000yqe.googlegroups.com>, gert wrote: > > ? ? with open('com1', 'r') as f: > > ? ? ? ? for line in f: > > ? ? ? ? ? ? ?print('line') > > Why bother, why not just > > ? ? for line in open('com1', 'r') : > ? ? ? ? print line Interesting :) So its does the same thing as with right ? Automatic closing and finalizing stuff. From luismgz at gmail.com Sat Apr 4 10:35:32 2009 From: luismgz at gmail.com (Luis Gonzalez) Date: Sat, 4 Apr 2009 07:35:32 -0700 (PDT) Subject: Testing dynamic languages References: <70d345df-33ca-4628-9fe9-dda113b5a2f4@n20g2000vba.googlegroups.com> Message-ID: On Apr 4, 11:17?am, Emmanuel Surleau wrote: > On Saturday 04 April 2009 15:37:44 grkunt... at gmail.com wrote: > > > I am a Java developer. There, I said it :-). Don't worry. I also do terrible things to support my family... From drobinow at gmail.com Sat Apr 4 10:36:26 2009 From: drobinow at gmail.com (drobinow at gmail.com) Date: Sat, 4 Apr 2009 07:36:26 -0700 (PDT) Subject: Cannot find text in *.py files with Windows Explorer? References: Message-ID: <604c48cf-5135-4b58-9692-08923ccac8b3@c9g2000yqm.googlegroups.com> On Apr 4, 12:21?am, John Doe wrote: > Anybody have a solution for Windows (XP) Explorer search not finding > ordinary text in *.py files? > > Thanks. Googling turns up this. http://www.pcmag.com/article2/0,4149,1206399,00.asp I haven't tried it myself. From andrew at acooke.org Sat Apr 4 10:38:47 2009 From: andrew at acooke.org (andrew cooke) Date: Sat, 4 Apr 2009 10:38:47 -0400 (CLT) Subject: Testing dynamic languages In-Reply-To: <2d56f77d6c2ec248ac654684cdd8292b.squirrel@localhost> References: <70d345df-33ca-4628-9fe9-dda113b5a2f4@n20g2000vba.googlegroups.com> <2d56f77d6c2ec248ac654684cdd8292b.squirrel@localhost> Message-ID: <4d87cd5c667bca267826efad42424133.squirrel@localhost> andrew cooke wrote: > if you are going to do that, stay with java. seriously - i too, am a java > developer about half the time, and you can make java pretty dynamic if you > try hard enough. look at exploiting aspects and functional programming > libraries, for example. also, of course, scala. andrew From ptmcg at austin.rr.com Sat Apr 4 10:40:02 2009 From: ptmcg at austin.rr.com (Paul McGuire) Date: Sat, 4 Apr 2009 07:40:02 -0700 (PDT) Subject: python needs leaning stuff from other language References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <73kt5jFvitcdU1@mid.uni-berlin.de> <49D54209.5090306@hotmail.com> <01e56a2f$0$20654$c3e8da3@news.astraweb.com> <87y6uie9ej.fsf@benfinney.id.au> <01e624b7$0$20654$c3e8da3@news.astraweb.com> <1238794953.8087.1.camel@tim-laptop> Message-ID: <88b541cd-9652-4e9b-82f3-06754eb0a9b1@3g2000yqk.googlegroups.com> On Apr 3, 11:48?pm, Tim Wintle wrote: > del mylist[:] > * or * > mylist[:] = [] > * or * > mylist = [] > > which, although semantically similar are different as far as the > interpreter are concerned (since two of them create a new list): > Only the last item creates a new list of any consequence. The first two retain the original list and delete or discard the items in it. A temporary list gets created in the 2nd option, and is then used to assign new contents to mylist's [:] slice - so yes, technically, a new list *is* created in the case of this option. But mylist does not get bound to it as in the 3rd case. In case 2, mylist's binding is unchanged, and the temporary list gets GC'ed almost immediately. -- Paul From gert.cuykens at gmail.com Sat Apr 4 10:44:16 2009 From: gert.cuykens at gmail.com (gert) Date: Sat, 4 Apr 2009 07:44:16 -0700 (PDT) Subject: with open('com1', 'r') as f: References: <8bc55c05-19da-41c4-b916-48e0a4be49b9@p11g2000yqe.googlegroups.com> <20090403002347.163911b4@oxygen> <8de4f974-073f-41cb-9729-341a02ca4c3f@u8g2000yqn.googlegroups.com> <575f77ac-9f14-490b-a165-3ac38127b87a@l38g2000vba.googlegroups.com> Message-ID: On Apr 3, 10:10?pm, Christian Heimes wrote: > gert wrote: > > I do understand, and I went looking into pySerial, but it is a long > > way from getting compatible with python3.x and involves other libs > > that are big and non pyhton3.x compatible. > > So don't use Python 3.0. Most people are still using Python 2.5 or 2.6. With all respect but why do people in general avoid the question when they do not know something? I appreciate the answer and its a valid solution, never the less useless in the answer I seek. I hope I did not offend anybody. When you want to do something about the future, you have to take the hard way, so others can take the easy way. If somebody is stuck going the hard way, to clear it for others, you can not expect them to be satisfied with a easy answer. Sorry. From marduk at letterboxes.org Sat Apr 4 10:46:09 2009 From: marduk at letterboxes.org (Albert Hopkins) Date: Sat, 04 Apr 2009 10:46:09 -0400 Subject: statvfs clearance In-Reply-To: <87iqlk7cnd.fsf@busola.homelinux.net> References: <22347545-9b8d-4d06-88af-3aaabea41a8b@r33g2000yqn.googlegroups.com> <87iqlk7cnd.fsf@busola.homelinux.net> Message-ID: <1238856369.24882.7.camel@blackwidow.nbk> On Sat, 2009-04-04 at 15:48 +0200, Hrvoje Niksic wrote: > Sreejith K writes: > > > Python's statvfs module contains the following indexes to use with > > os.statvfs() that contains the specified information > > > > statvfs.F_BSIZE > > Preferred file system block size. > [...] > > statvfs.F_NAMEMAX > > Maximum file name length. > > > > Can anyone tell me (or give me some links to know) what are these > > values ? The first three I know, I need to know about the rest.... > > You can simply print them, they are integers: > > >>> import statvfs > >>> statvfs.F_FLAG > 8 > -- Oh, did the OP mean the values of the indices? Yeah, of course you can just print them. Or even look at statvfs.py. It's a whopping 15 lines: """Constants for interpreting the results of os.statvfs() and os.fstatvfs().""" # Indices for statvfs struct members in the tuple returned by # os.statvfs() and os.fstatvfs(). F_BSIZE = 0 # Preferred file system block size F_FRSIZE = 1 # Fundamental file system block size F_BLOCKS = 2 # Total number of file system blocks (FRSIZE) F_BFREE = 3 # Total number of free blocks F_BAVAIL = 4 # Free blocks available to non-superuser F_FILES = 5 # Total number of file nodes F_FFREE = 6 # Total number of free file nodes F_FAVAIL = 7 # Free nodes available to non-superuser F_FLAG = 8 # Flags (see your local statvfs man page) F_NAMEMAX = 9 # Maximum file name length From zamnedix at gmail.com Sat Apr 4 11:15:03 2009 From: zamnedix at gmail.com (Zamnedix) Date: Sat, 4 Apr 2009 08:15:03 -0700 (PDT) Subject: python needs leaning stuff from other language References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <1c03c513-8316-4b6a-afba-d5b345afad23@v23g2000pro.googlegroups.com> <01e62404$0$20654$c3e8da3@news.astraweb.com> Message-ID: <770852f7-5df9-48e4-a453-525e77c09d00@u9g2000pre.googlegroups.com> On Apr 3, 8:48?am, Steven D'Aprano wrote: > On Fri, 03 Apr 2009 08:23:22 -0700, Zamnedix wrote: > > On Apr 2, 3:25 pm, online.serv... at ymail.com wrote: > >> python's list needs a thing ?list.clear() ?like c# arraylist and > >> python needs a writeline() method > > > Please don't post things like list before you do any research. You don't > > know what you are talking about. > > The original poster may or may not know what he is talking about, but > adding a clear() method to lists seems to be very much in demand. I'd > vote Yes for one. > > Besides, this news group is for people to ask questions about Python, > even stupid questions. It's not just for experts only. > > -- > Steven Sorry. It was inappropriate. I just get a little mad at people who don't even ATTEMPT to speak proper English. And then they go on bashing our language without any sort of context. From kushal.kumaran at gmail.com Sat Apr 4 11:20:40 2009 From: kushal.kumaran at gmail.com (Kushal Kumaran) Date: Sat, 4 Apr 2009 20:50:40 +0530 Subject: with open('com1', 'r') as f: References: <8bc55c05-19da-41c4-b916-48e0a4be49b9@p11g2000yqe.googlegroups.com> <20090403002347.163911b4@oxygen> <8de4f974-073f-41cb-9729-341a02ca4c3f@u8g2000yqn.googlegroups.com> <575f77ac-9f14-490b-a165-3ac38127b87a@l38g2000vba.googlegroups.com> Message-ID: <20090404205040.6e8c5cfc@oxygen> On Fri, 03 Apr 2009 22:10:36 +0200 Christian Heimes wrote: > gert wrote: > > I do understand, and I went looking into pySerial, but it is a long > > way from getting compatible with python3.x and involves other libs > > that are big and non pyhton3.x compatible. > > So don't use Python 3.0. Most people are still using Python 2.5 or > 2.6. > Alternatively, you could look into the pySerial source and find out what it does. -- kushal From cdalten at gmail.com Sat Apr 4 11:22:27 2009 From: cdalten at gmail.com (grocery_stocker) Date: Sat, 4 Apr 2009 08:22:27 -0700 (PDT) Subject: Why doesn't StopIteration get caught in the following code? Message-ID: Given the following [cdalten at localhost ~]$ python Python 2.4.3 (#1, Oct 1 2006, 18:00:19) [GCC 4.1.1 20060928 (Red Hat 4.1.1-28)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> def counter(): ... mylist = range(3) ... for i in mylist: ... yield i*i ... >>> counter >>> gen = counter() >>> gen >>> while True: ... i = gen.next() ... print i ... 0 1 4 Traceback (most recent call last): File "", line 2, in ? StopIteration I thought the 'for' in counter() was supposed to catch StopIteration. Ie, I thought that something like ... for i in mylist: ... yield i*i Got translated to something like >>> try: ... while True: ... do some stuff ... except StopIteration: ... pass ... From davea at ieee.org Sat Apr 4 11:23:52 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 04 Apr 2009 10:23:52 -0500 Subject: statvfs clearance In-Reply-To: <87iqlk7cnd.fsf@busola.homelinux.net> References: <22347545-9b8d-4d06-88af-3aaabea41a8b@r33g2000yqn.googlegroups.com> <87iqlk7cnd.fsf@busola.homelinux.net> Message-ID: <49D77B88.4060706@ieee.org> Hrvoje Niksic wrote: > Sreejith K writes: > > >> Python's statvfs module contains the following indexes to use with >> os.statvfs() that contains the specified information >> >> statvfs.F_BSIZE >> Preferred file system block size. >> > [...] > >> statvfs.F_NAMEMAX >> Maximum file name length. >> >> Can anyone tell me (or give me some links to know) what are these >> values ? The first three I know, I need to know about the rest.... >> > > The following web page describes them: http://docs.python.org/library/statvfs.html but you already knew that much. So what are you really asking for? > You can simply print them, they are integers: > > >>>> import statvfs >>>> statvfs.F_FLAG >>>> > 8 > > But those values may be specific to a particular python implementation. No reason to assume they'd be the same across OS platforms, or between versions 2.5 and 2.6. The whole thing is deprecated, and is eliminated in 3.0 The term we used to use for these is "magic numbers." Using magic-numbers directly in your code is a good way to ensure future time debugging. From dasacc22 at gmail.com Sat Apr 4 11:31:03 2009 From: dasacc22 at gmail.com (dasacc22) Date: Sat, 4 Apr 2009 08:31:03 -0700 (PDT) Subject: lxml and xslt extensions Message-ID: <4e26caff-d65b-42b7-a921-073974522dce@h28g2000yqd.googlegroups.com> Hi, Im not sure where else to ask this. But basically Im having trouble figuring out how to successfully apply multiple extensions in a single transformation. So for example if i have in my xsl and my xslt extension looks like class TagExtension(etree.XSLTExtension): def execute( ..., output_parent): print 'executing tag_extension' tag = etree.Element('p') tag.text = 'Hello' output_parent.append(tag) well then the transformation works for the first tag and appends it to the root of the created doc but all subsequent calls dont append (maybe b/c output_parent is now somewhere else for return?). And to clarify, I know that its the first call that completes and all subsequent calls fail b/c i have a subsequent call that performs a different transformation. Thanks for any help or hints, Daniel From davea at ieee.org Sat Apr 4 11:41:54 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 04 Apr 2009 10:41:54 -0500 Subject: Why doesn't StopIteration get caught in the following code? In-Reply-To: References: Message-ID: <49D77FC2.5050207@ieee.org> grocery_stocker wrote: > Given the following > > [cdalten at localhost ~]$ python > Python 2.4.3 (#1, Oct 1 2006, 18:00:19) > [GCC 4.1.1 20060928 (Red Hat 4.1.1-28)] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>>> def counter(): >>>> > ... mylist = range(3) > ... for i in mylist: > ... yield i*i > ... > >>>> counter >>>> > > >>>> gen = counter() >>>> gen >>>> > > >>>> while True: >>>> > ... i = gen.next() > ... print i > ... > 0 > 1 > 4 > Traceback (most recent call last): > File "", line 2, in ? > StopIteration > > > I thought the 'for' in counter() was supposed to catch StopIteration. > Ie, I thought that something like > > ... for i in mylist: > ... yield i*i > > Got translated to something like > >>>> try: >>>> > ... while True: > ... do some stuff > ... except StopIteration: > ... pass > ... > > > There are two separate StopIteration's here. The for statement you describe will indeed catch the StopIteration from the list's iterator. But that for statement is inside a generator function, and the generator function throws a StopIteration when it returns (as opposed to when it yields). If you had used that generator inside another for statement, it would have worked as you expect. But when you call nest() explicitly, you have to be prepared for the exception. Clearly, the while True loop cannot go forever, when your generator is finite. From andrew at acooke.org Sat Apr 4 12:05:21 2009 From: andrew at acooke.org (andrew cooke) Date: Sat, 4 Apr 2009 12:05:21 -0400 (CLT) Subject: Why doesn't StopIteration get caught in the following code? In-Reply-To: References: Message-ID: grocery_stocker wrote: >>>> while True: > ... i = gen.next() > ... print i > ... > 0 > 1 > 4 python's magic isn't as magic as you hope. roughly speaking, it only does the necessary rewriting (writing the equivalent code with next etc etc) when you define a function or a method that contains "yield". the above doesn't, so it's not rewritten and there's no magic. more exactly: http://docs.python.org/reference/datamodel.html#index-1747 andrew From mail at microcorp.co.za Sat Apr 4 12:07:25 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Sat, 4 Apr 2009 18:07:25 +0200 Subject: Hash of None varies per-machine References: <4c62537d-583b-4441-89f9-b341247d0b13@l22g2000vba.googlegroups.com><01e6a5ff$0$20654$c3e8da3@news.astraweb.com> <01e73c74$0$20654$c3e8da3@news.astraweb.com> Message-ID: <007501c9b540$4e99b380$0d00a8c0@Hendrik> "Steven D'Aprano" wrote: >Seems to me you have misunderstood the way pickling works. Yeah right - have you ever looked at the pickle code? Good to hear it "just works" :-) - Hendrik From davea at dejaviewphoto.com Sat Apr 4 12:07:44 2009 From: davea at dejaviewphoto.com (Dave Angel) Date: Sat, 04 Apr 2009 11:07:44 -0500 Subject: Cannot find text in *.py files with Windows Explorer? Message-ID: <49D785D0.80309@dejaviewphoto.com> John Doe wrote: > Tim Golden wrote: > >> Now I think about it, try searching for "xplorer2" since I think I >> mentioned that I have used that instead of explorer for some while. > > Yeah... at least by the time I move from Windows XP to Windows 7, very > likely I will be using a different file manager. If I cannot > search Python files, now might be a good time to switch. Thanks for > the search criteria, I will look for that thread. > The original thread is at http://mail.python.org/pipermail/tutor/2009-January/066765.html and the product xplorer2 is at http://zabkat.com/ Std version is $30, but there's a free version also. From dasacc22 at gmail.com Sat Apr 4 12:09:17 2009 From: dasacc22 at gmail.com (dasacc22) Date: Sat, 4 Apr 2009 09:09:17 -0700 (PDT) Subject: lxml and xslt extensions References: <4e26caff-d65b-42b7-a921-073974522dce@h28g2000yqd.googlegroups.com> Message-ID: <8597616e-196d-47a6-9fe1-40b5479f03e2@e5g2000vbe.googlegroups.com> On Apr 4, 11:31?am, dasacc22 wrote: > Hi, > > Im not sure where else to ask this. But basically Im having trouble > figuring out how to successfully apply multiple extensions in a single > transformation. So for example if i have > > > > > > > > in my xsl and my xslt extension looks like > > class TagExtension(etree.XSLTExtension): > ? def execute( ..., output_parent): > ? ? print 'executing tag_extension' > ? ? tag = etree.Element('p') > ? ? tag.text = 'Hello' > ? ? output_parent.append(tag) > > well then the transformation works for the first tag and appends it to > the root of the created doc but all subsequent calls dont append > (maybe b/c output_parent is now somewhere else for return?). And to > clarify, I know that its the first call that completes and all > subsequent calls fail b/c i have a subsequent call that performs a > different transformation. > > Thanks for any help or hints, > Daniel Oh well I found the culprit, etree.tostring seems to cut it off after the first transformation, simply doing a $> print result displays the entire document. To make use of the keyword options xml_declaration, pretty_print, encoding, I tried using the .write method of result to a StringIO but it produces the same clipped result. Guess Ill have to edit the .docinfo attributes on the result and return the string From bradleyw at gmail.com Sat Apr 4 12:22:44 2009 From: bradleyw at gmail.com (Bradley Wright) Date: Sat, 4 Apr 2009 09:22:44 -0700 (PDT) Subject: is there a way to collect twitts with python? References: <1078018b0903271054y3d4a7162w885c61bf63bc2048@mail.gmail.com> Message-ID: <78f368f8-d9d0-4a9e-888b-1b4dde42025e@o11g2000yql.googlegroups.com> Just to pimp my own wares: http://github.com/bradleywright/yatcip/tree/master A Python Twitter client. From bockman at virgilio.it Sat Apr 4 12:57:39 2009 From: bockman at virgilio.it (Francesco Bochicchio) Date: Sat, 04 Apr 2009 18:57:39 +0200 Subject: Testing dynamic languages References: <70d345df-33ca-4628-9fe9-dda113b5a2f4@n20g2000vba.googlegroups.com> Message-ID: On Sat, 04 Apr 2009 07:37:44 -0700, grkuntzmd wrote: > I am a Java developer. There, I said it :-). > > When I am writing code, I can rely on the compiler to confirm that > any methods I write will be called with parameters of the "right" > type. I do not need to test that parameter #1 really is a String > before I call some method on it that only works on Strings. > > If I am writing in Python, since it is dynamically, but strongly > typed, I really should check that each parameter is of the expected > type, or at least can respond to the method I plan on calling ("duck" > typing). Every call should be wrapped in a try/except statement to > prevent the method (and program) from crashing when my method is > called with an integer instead of the expected string. > > Is this the experience that Python programmer (of large projects) see? > Do you also write unit tests to confirm that the methods actually > check for and catch "bad" parameter types? If I am writing small one- > off scripts, I wouldn't worry about it, but if I am writing a large > system that must have 99+% uptime without constant monitoring, this > really should be verified. > > Up for discussion... Uhm. I write large bodies of code for living ... not in Python, unfortunately. I usually divide my code in two classes wrt sanity checks : inner code and boundary code. Boundary code gets paranoic checks on everything: arguments, consistency etc ... also with static typing : an 'int' parameter declaration in C/C++ make sure that your function gets an integer, but ensure nothing in the way of minimum and maximum value, so before using it - say - as an array index, it is better to check that. Inner code gets less checks, based on the assumptions that inputs have been properly checked by the boundary functions. This method is not without risks - an architectural change can move a function from an inner zone to a boundary zone, and I may forget to 'fortify' the function. However, this is a guideline that served me well. Beside that there is another guideline I apply to languages like java and python - programs in these languages do not 'crash' ... they throw exceptions. Now, supposing to add an input check : what are you going to do if you find bad data? In the answer is - as often the case - throw an exception, then maybe the check is not worth much ... There are exceptions to this guideline, as always, like if you want to generate a more meaningful exception, but this is another guideline I tend to follow. And this means that my level of checking in python is much lower than in - say - C++. And I don't worry too much about argument types, more about external inputs with the right 'semantic'. The one coding behaviour that dynamic types forced me to change, is that now I tend to build programs more incrementally, because catching typos error and logic errors at the same time on a large body of code can be frustrating and not very productive ... but I find myself to use now the same approach also when I code in statically typed languages : a bit slower at beginning, but tend to procuce more reliable results . Ciao ---- FB From tim.wintle at teamrubber.com Sat Apr 4 13:00:03 2009 From: tim.wintle at teamrubber.com (Tim Wintle) Date: Sat, 04 Apr 2009 18:00:03 +0100 Subject: Testing dynamic languages In-Reply-To: <70d345df-33ca-4628-9fe9-dda113b5a2f4@n20g2000vba.googlegroups.com> References: <70d345df-33ca-4628-9fe9-dda113b5a2f4@n20g2000vba.googlegroups.com> Message-ID: <1238864403.30065.13.camel@tim-laptop> On Sat, 2009-04-04 at 06:37 -0700, grkuntzmd at gmail.com wrote: > If I am writing in Python, since it is dynamically, but strongly > typed, I really should check that each parameter is of the expected > type, or at least can respond to the method I plan on calling ("duck" > typing). Every call should be wrapped in a try/except statement to > prevent the method (and program) from crashing when my method is > called with an integer instead of the expected string. At some point you should wrap it in a try/except block - but only at the point where you want the exception to be handled. That will normally be quite far up, and you'll just let the exception travel back up to that point. for example, in a web server type of thing you might have something vaguely like def main: for request in request_itter: try: headers = request.get_headers() dispatch = get_dispatch_fn(headers) response = dispatch(response) except: send_500() You probably then don't need to catch any exceptions in the get_headers, or actual processing methods (unless you are writing to sql or something, when you might want to wrap all statements in a try block, and then put a "ROLLBACK;" query in the except block (and then raise the caught exception so it goes back the the block above) > > Is this the experience that Python programmer (of large projects) see? > Do you also write unit tests to confirm that the methods actually > check for and catch "bad" parameter types? If I am writing small one- > off scripts, I wouldn't worry about it, but if I am writing a large > system that must have 99+% uptime without constant monitoring, this > really should be verified. I write large applications with a target of 99.9% uptime, and I don't find it a problem. occasionally I have to check parameters, but that's not very often > > Up for discussion... > -- > http://mail.python.org/mailman/listinfo/python-list From tim.wintle at teamrubber.com Sat Apr 4 13:07:23 2009 From: tim.wintle at teamrubber.com (Tim Wintle) Date: Sat, 04 Apr 2009 18:07:23 +0100 Subject: python needs leaning stuff from other language In-Reply-To: References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <73kt5jFvitcdU1@mid.uni-berlin.de> <49D54209.5090306@hotmail.com> <01e56a2f$0$20654$c3e8da3@news.astraweb.com> <87y6uie9ej.fsf@benfinney.id.au> <01e624b7$0$20654$c3e8da3@news.astraweb.com> <1238794953.8087.1.camel@tim-laptop> <1238820500.1029.69.camel@tim-laptop> Message-ID: <1238864843.30065.21.camel@tim-laptop> On Sat, 2009-04-04 at 02:03 -0500, Robert Kern wrote: > > Let's be clear: python-ideas seems positive on the idea of adding a .clear() > method. *Completely removing* slice assignment has not been broached there. Yup, sorry - I did mean to refer to the initial suggestion, rather than my comments > > > (I didn't expect such strong responses btw!) > > You are proposing the removal of a general, orthogonal feature (and breaking > code in consequence!) just because of a new syntax for a single special case of > that feature. That is quite simply ridiculous. Ok, I may have come across a little strongly (was very tired) - I'm not _actually_ saying we should remove it, I'm just pointing out why adding .clear() to lists seems to be unnecessary and slightly messy. The suggested removal of assignments to slices is a theoretical statement. > > .clear() would be non-orthogonal syntactic sugar. That's okay! Python has > syntactic sugar in a number of other places, too! Appropriate doses of syntactic > sugar and non-orthogonality are precisely what lets you implement "There should > be one-- and preferably only one --obvious way to do it." The really key word in > that sentence is "obvious", not "one". > > FWIW, removing slice assignment would be a gross form of non-orthogonality, too. > __getitem__, __setitem__ and __delitem__ should all be able to accept the same > indices (or else raise exceptions in the case of immutability). hummm - I'm sure it would be confusing behaviour if it was not available, but I'm not sure how it would be non-orthogonal From gert.cuykens at gmail.com Sat Apr 4 13:11:12 2009 From: gert.cuykens at gmail.com (gert) Date: Sat, 4 Apr 2009 10:11:12 -0700 (PDT) Subject: with open('com1', 'r') as f: References: 20090404205040.6e8c5cfc@oxygen Message-ID: <84dc1c84-27f9-40a5-890a-9760c91d3a6d@k2g2000yql.googlegroups.com> On Apr 4, 5:20?pm, Kushal Kumaran wrote: > On Fri, 03 Apr 2009 22:10:36 +0200 > > Christian Heimes wrote: > > gert wrote: > > > I do understand, and I went looking into pySerial, but it is a long > > > way from getting compatible with python3.x and involves other libs > > > that are big and non pyhton3.x compatible. > > > So don't use Python 3.0. Most people are still using Python 2.5 or > > 2.6. > > Alternatively, you could look into the pySerial source and find out > what it does. I think pywin32 is the way they do the things I want. Witch is not python3 ready and way to much work around to do it clean. Using ctypes is a option but you have to really know what you are doing and what you are looking for. From grkuntzmd at gmail.com Sat Apr 4 13:30:27 2009 From: grkuntzmd at gmail.com (grkuntzmd at gmail.com) Date: Sat, 4 Apr 2009 10:30:27 -0700 (PDT) Subject: Testing dynamic languages References: <70d345df-33ca-4628-9fe9-dda113b5a2f4@n20g2000vba.googlegroups.com> Message-ID: This may be obvious but, clearly there are (at least) two general types of errors: those caused by data external to the program and those caused by bugs in the program. For all inputs coming into the program from outside, such as user inputs and data coming over a network, the inputs must be completely checked -- always assume that they will be incorrect and are intended to crash your code -- be pleasantly surprised when they are not :-). Check for all bad inputs. If the data are from inside the program, the assumption may be that they are good and if not, it was a bug. I suppose you can write unit tests on each routine to see that the methods that routine calls are with valid arguments (using mocks). I actually tried this in a Java program using JMockit; it was tedious, but it did catch a few "potential" bugs. I would love to say that I ALWAYS remember the input and output condition of every subroutine I write, but I just finished a new feature for a company project that involved 100+ routines and classes and I admit that I don't always remember which ones can return null and which always return empty arrays, for example, even though I try to write JavaDocs at the top of each routine. By mocking these routines, I can check that the caller always handles each case gracefully. From ma.ston.99 at hotmail.com Sat Apr 4 13:34:59 2009 From: ma.ston.99 at hotmail.com (Mark) Date: Sat, 4 Apr 2009 19:34:59 +0200 Subject: django model problem References: <19a1cxf57zsli.1wmngjrxnlagf$.dlg@40tude.net> <2jd2j8x0sir0.1jje7ogy6pgpd.dlg@40tude.net> Message-ID: <6x88bzn3im9o$.1hrfbg1oggm1n.dlg@40tude.net> > > Anyway, since I don't have time to actually install and configure Django > to experiment, I'd suggest you post a query on the django-users mailing > list, at http://groups.google.com/group/django-users Yes, that's what I did - it seems my problem is either a tough one, or it's just impossible to do what I want. Anyway great thanks for your interest and help! Regards, MS From ppearson at nowhere.invalid Sat Apr 4 14:09:38 2009 From: ppearson at nowhere.invalid (Peter Pearson) Date: 4 Apr 2009 18:09:38 GMT Subject: Hash of None varies per-machine References: <4c62537d-583b-4441-89f9-b341247d0b13@l22g2000vba.googlegroups.com> <7xfxgp39ji.fsf@ruckus.brouhaha.com> Message-ID: <73pm32Fvt37qU1@mid.individual.net> On 03 Apr 2009 10:57:05 -0700, Paul Rubin wrote: > ben.taylor at email.com writes: >> 1. Is it correct that if you hash two things that are not equal they >> might give you the same hash value? > > Yes, hashes are 32 bit numbers and there are far more than 2**32 > possible Python values (think of long ints), so obviously there must > be multiple values that hash to the same slot. For example, on this machine: Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>> hash( "latox" ) == hash( "uzyky" ) True YMMV. -- To email me, substitute nowhere->spamcop, invalid->net. From gagsl-py2 at yahoo.com.ar Sat Apr 4 14:09:51 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sat, 04 Apr 2009 15:09:51 -0300 Subject: with open('com1', 'r') as f: References: <8bc55c05-19da-41c4-b916-48e0a4be49b9@p11g2000yqe.googlegroups.com> <91e09eaf-5a25-4a6b-b131-a5245970b337@f19g2000yqh.googlegroups.com> Message-ID: En Sat, 04 Apr 2009 11:29:22 -0300, gert escribi?: > On Apr 4, 12:58?am, Lawrence D'Oliveiro central.gen.new_zealand> wrote: >> In message <8bc55c05-19da-41c4- >> >> b916-48e0a4be4... at p11g2000yqe.googlegroups.com>, gert wrote: >> > ? ? with open('com1', 'r') as f: >> > ? ? ? ? for line in f: >> > ? ? ? ? ? ? ?print('line') >> >> Why bother, why not just >> >> ? ? for line in open('com1', 'r') : >> ? ? ? ? print line > > Interesting :) > So its does the same thing as with right ? > Automatic closing and finalizing stuff. No, it does not. Either use `with` o a `try...finally` block. -- Gabriel Genellina From ntwrkd at gmail.com Sat Apr 4 14:36:21 2009 From: ntwrkd at gmail.com (ntwrkd) Date: Sat, 4 Apr 2009 11:36:21 -0700 Subject: Best Compatible JS Lib for Django Message-ID: Does anyone have experience with using JS Libraries with Django? Do some work better than others and are easier to code with? TIV From aahz at pythoncraft.com Sat Apr 4 14:44:01 2009 From: aahz at pythoncraft.com (Aahz) Date: 4 Apr 2009 11:44:01 -0700 Subject: dict view to list References: <326f58f6-c888-46ce-9c38-e295e7d3c9e7@j8g2000yql.googlegroups.com> <6b4065b0-6af7-4aff-8023-40e5d521faa0@v19g2000yqn.googlegroups.com> Message-ID: In article <6b4065b0-6af7-4aff-8023-40e5d521faa0 at v19g2000yqn.googlegroups.com>, Luis Gonzalez wrote: > >Yes, I know the python approach is to use built-ins. >But wouldn't it be cool if we could do mydict.values().tolist() >instead? >It would be more regular and intuitive and readable from an OO point >of view. >In my oppinion, this would be cleaner. >Built-ins used like this look like an early decission made when >designing an imperative language. What kind of language do you think Python is? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." --Brian W. Kernighan From jjposner at snet.net Sat Apr 4 14:50:29 2009 From: jjposner at snet.net (John Posner) Date: Sat, 04 Apr 2009 14:50:29 -0400 Subject: Generators/iterators, Pythonicity, and primes Message-ID: Inspired by recent threads (and recalling my first message to Python edu-sig), I did some Internet searching on producing prime numbers using Python generators. Most algorithms I found don't go for the infinite, contenting themselves with "list all the primes below a given number". Here's a very Pythonic (IMHO) implementation that keeps going and going and going ...: from itertools import count from math import sqrt def prime_gen(): """ Generate all prime numbers """ primes = [] for n in count(2): if all(n%p for p in primes if p < sqrt(n)): primes.append(n) yield n The use of all() is particularly nifty (see http://code.activestate.com/recipes/576640/). And so is the way in which the list comprehension easily incorporates the sqrt(n) optimization. Question: Is there a way to implement this algorithm using generator expressions only -- no "yield" statements allowed? BTW -- thank you, John Machin, for the spanking ('I'd worry about "correct" before "Pythonic"') in a previous message. I *did* manage to post a correction to the needless conversion of a string to a list: b in list("\r\n\t") ... a few hours before your message arrived (Wed Apr 1 19:44:01 CEST 2009)! E-mail message checked by Spyware Doctor (6.0.0.386) Database version: 5.12110 http://www.pctools.com/en/spyware-doctor-antivirus/ From stefan_ml at behnel.de Sat Apr 4 14:55:07 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sat, 04 Apr 2009 20:55:07 +0200 Subject: lxml and xslt extensions In-Reply-To: <8597616e-196d-47a6-9fe1-40b5479f03e2@e5g2000vbe.googlegroups.com> References: <4e26caff-d65b-42b7-a921-073974522dce@h28g2000yqd.googlegroups.com> <8597616e-196d-47a6-9fe1-40b5479f03e2@e5g2000vbe.googlegroups.com> Message-ID: <49d7ad0b$0$31875$9b4e6d93@newsspool3.arcor-online.net> Hi, dasacc22 wrote: > On Apr 4, 11:31 am, dasacc22 wrote: >> Im not sure where else to ask this. The best place to ask is the lxml mailing list: http://codespeak.net/mailman/listinfo/lxml-dev >> But basically Im having trouble >> figuring out how to successfully apply multiple extensions in a single >> transformation. So for example if i have >> >> >> >> >> >> >> >> in my xsl and my xslt extension looks like >> >> class TagExtension(etree.XSLTExtension): >> def execute( ..., output_parent): >> print 'executing tag_extension' >> tag = etree.Element('p') >> tag.text = 'Hello' >> output_parent.append(tag) >> >> well then the transformation works for the first tag and appends it to >> the root of the created doc but all subsequent calls dont append >> (maybe b/c output_parent is now somewhere else for return?). And to >> clarify, I know that its the first call that completes and all >> subsequent calls fail b/c i have a subsequent call that performs a >> different transformation. > > Oh well I found the culprit, etree.tostring seems to cut it off after > the first transformation, Not sure what you mean here. Could you post your question on the lxml mailing list and add a runnable example that shows the behaviour? > simply doing a > > $> print result > > displays the entire document. To make use of the keyword options > xml_declaration, pretty_print, encoding, I tried using the .write > method of result to a StringIO but it produces the same clipped > result. Guess Ill have to edit the .docinfo attributes on the result > and return the string .docinfo is basically read-only. The result of the XSLT is serialised as requested by the xsl:output element in the stylesheet. Stefan From caseyhHAMMER_TIME at istar.ca Sat Apr 4 14:58:32 2009 From: caseyhHAMMER_TIME at istar.ca (Casey Hawthorne) Date: Sat, 04 Apr 2009 11:58:32 -0700 Subject: Can't one collect twitts and twits in any language? Message-ID: :) -- Regards, Casey From jdoe at usenetlove.invalid Sat Apr 4 14:59:23 2009 From: jdoe at usenetlove.invalid (John Doe) Date: Sat, 04 Apr 2009 18:59:23 GMT Subject: Cannot find text in *.py files with Windows Explorer? References: Message-ID: Dave Angel wrote: > John Doe wrote: >> ...at least by the time I move from Windows XP to Windows 7, >> very likely I will be using a different file manager. If I cannot >> search Python files, now might be a good time to switch. > and the product xplorer2 is at http://zabkat.com/ > Std version is $30, but there's a free version also. The free version uses Windows Explorer search, therefore it will not find text in *.py files. Same with Ultra Explorer. Another file manager, FreeCommander, does find text in Python files. From metolone+gmane at gmail.com Sat Apr 4 15:06:57 2009 From: metolone+gmane at gmail.com (Mark Tolonen) Date: Sat, 4 Apr 2009 12:06:57 -0700 Subject: Generators/iterators, Pythonicity, and primes References: Message-ID: "John Posner" wrote in message news:AF9FBCC3A7624599A6F51BAD2397EA46 at AMDUP... > Inspired by recent threads (and recalling my first message to Python > edu-sig), I did some Internet searching on producing prime numbers using > Python generators. Most algorithms I found don't go for the infinite, > contenting themselves with "list all the primes below a given number". > > Here's a very Pythonic (IMHO) implementation that keeps going and going > and > going ...: > > from itertools import count > from math import sqrt > > def prime_gen(): > """ > Generate all prime numbers > """ > primes = [] > for n in count(2): > if all(n%p for p in primes if p < sqrt(n)): > primes.append(n) > yield n p <= sqrt(n) works a little better :^) -Mark From deets at nospam.web.de Sat Apr 4 15:10:59 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sat, 04 Apr 2009 21:10:59 +0200 Subject: python needs leaning stuff from other language In-Reply-To: References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <73kt5jFvitcdU1@mid.uni-berlin.de> <49D54209.5090306@hotmail.com> <01e56a2f$0$20654$c3e8da3@news.astraweb.com> <87y6uie9ej.fsf@benfinney.id.au> <01e624b7$0$20654$c3e8da3@news.astraweb.com> <1238794953.8087.1.camel@tim-laptop> <1238820500.1029.69.camel@tim-laptop> Message-ID: <73ppm4Fs6mvlU1@mid.uni-berlin.de> Tim Wintle schrieb: > On Sat, 2009-04-04 at 02:03 -0500, Robert Kern wrote: >> Let's be clear: python-ideas seems positive on the idea of adding a .clear() >> method. *Completely removing* slice assignment has not been broached there. > > Yup, sorry - I did mean to refer to the initial suggestion, rather than > my comments > >>> (I didn't expect such strong responses btw!) >> You are proposing the removal of a general, orthogonal feature (and breaking >> code in consequence!) just because of a new syntax for a single special case of >> that feature. That is quite simply ridiculous. > > Ok, I may have come across a little strongly (was very tired) - I'm not > _actually_ saying we should remove it, I'm just pointing out why > adding .clear() to lists seems to be unnecessary and slightly messy. The > suggested removal of assignments to slices is a theoretical statement. > >> .clear() would be non-orthogonal syntactic sugar. That's okay! Python has >> syntactic sugar in a number of other places, too! Appropriate doses of syntactic >> sugar and non-orthogonality are precisely what lets you implement "There should >> be one-- and preferably only one --obvious way to do it." The really key word in >> that sentence is "obvious", not "one". >> >> FWIW, removing slice assignment would be a gross form of non-orthogonality, too. >> __getitem__, __setitem__ and __delitem__ should all be able to accept the same >> indices (or else raise exceptions in the case of immutability). > > hummm - I'm sure it would be confusing behaviour if it was not > available, but I'm not sure how it would be non-orthogonal >>> l = range(10) >>> l[3:7] = range(4) >>> l [0, 1, 2, 0, 1, 2, 3, 7, 8, 9] How do you want to do that with clear? That l[:] = [] clears a list is more of an "accident" than the intent of slice-assignment. Removing it in favor of clear() would remove an orthogonal feature of updating parts of a list with another iterable. Diez From fetchinson at googlemail.com Sat Apr 4 15:20:39 2009 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Sat, 4 Apr 2009 12:20:39 -0700 Subject: Best Compatible JS Lib for Django In-Reply-To: References: Message-ID: > Does anyone have experience with using JS Libraries with Django? > Do some work better than others and are easier to code with? You might want to ask this on the django list. Cheers, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From jjposner at snet.net Sat Apr 4 15:28:47 2009 From: jjposner at snet.net (John Posner) Date: Sat, 04 Apr 2009 15:28:47 -0400 Subject: Generators/iterators, Pythonicity, and primes In-Reply-To: References: Message-ID: <915A3093045248CA966CD1D2AC8159D1@AMDUP> Mark Tolonen said: >> p <= sqrt(n) works a little better :^) >> >> -Mark >> Right you are -- I found that bug in my last-minute check, and then I forgot to trannscribe the fix into the email message. Duh -- thanks! -John E-mail message checked by Spyware Doctor (6.0.0.386) Database version: 5.12110 http://www.pctools.com/en/spyware-doctor-antivirus/ From bearophileHUGS at lycos.com Sat Apr 4 15:57:15 2009 From: bearophileHUGS at lycos.com (bearophileHUGS at lycos.com) Date: Sat, 4 Apr 2009 12:57:15 -0700 (PDT) Subject: Testing dynamic languages References: <70d345df-33ca-4628-9fe9-dda113b5a2f4@n20g2000vba.googlegroups.com> Message-ID: <16c4d592-2acf-4e3c-8214-4624c75b00ef@y7g2000yqa.googlegroups.com> grkunt...: > If I am writing in Python, since it is dynamically, but strongly > typed, I really should check that each parameter is of the expected > type, or at least can respond to the method I plan on calling ("duck" > typing). Every call should be wrapped in a try/except statement to > prevent the method (and program) from crashing when my method is > called with an integer instead of the expected string. Others have already given you most of the answers (summary: don't do that. Don't fight the language. Use doctests). My other suggestion is to read code coming from 5+ Python programs written by other (different) people. You will see how to use Python. Bye, bearophile From deets at nospam.web.de Sat Apr 4 16:28:30 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sat, 04 Apr 2009 22:28:30 +0200 Subject: with open('com1', 'r') as f: In-Reply-To: References: <8bc55c05-19da-41c4-b916-48e0a4be49b9@p11g2000yqe.googlegroups.com> <20090403002347.163911b4@oxygen> <8de4f974-073f-41cb-9729-341a02ca4c3f@u8g2000yqn.googlegroups.com> <575f77ac-9f14-490b-a165-3ac38127b87a@l38g2000vba.googlegroups.com> Message-ID: <73pu7eFvrpokU1@mid.uni-berlin.de> gert schrieb: > On Apr 3, 10:10 pm, Christian Heimes wrote: >> gert wrote: >>> I do understand, and I went looking into pySerial, but it is a long >>> way from getting compatible with python3.x and involves other libs >>> that are big and non pyhton3.x compatible. >> So don't use Python 3.0. Most people are still using Python 2.5 or 2.6. > > With all respect but why do people in general avoid the question when > they do not know something? > I appreciate the answer and its a valid solution, never the less > useless in the answer I seek. > > I hope I did not offend anybody. When you want to do something about > the future, you have to take the hard way, so others can take the easy > way. If somebody is stuck going the hard way, to clear it for others, > you can not expect them to be satisfied with a easy answer. But your path is clearly *not* the future. It would be fixing win32 and pyserial, if anything. And the suggestion to change the set of tools if there is no reason to stick to the ones you use now certainly is a valid answer. Especially if they do know that these solutions work. Diez From robert.kern at gmail.com Sat Apr 4 16:36:07 2009 From: robert.kern at gmail.com (Robert Kern) Date: Sat, 04 Apr 2009 15:36:07 -0500 Subject: python needs leaning stuff from other language In-Reply-To: <1238864843.30065.21.camel@tim-laptop> References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <73kt5jFvitcdU1@mid.uni-berlin.de> <49D54209.5090306@hotmail.com> <01e56a2f$0$20654$c3e8da3@news.astraweb.com> <87y6uie9ej.fsf@benfinney.id.au> <01e624b7$0$20654$c3e8da3@news.astraweb.com> <1238794953.8087.1.camel@tim-laptop> <1238820500.1029.69.camel@tim-laptop> <1238864843.30065.21.camel@tim-laptop> Message-ID: On 2009-04-04 12:07, Tim Wintle wrote: > On Sat, 2009-04-04 at 02:03 -0500, Robert Kern wrote: >> Let's be clear: python-ideas seems positive on the idea of adding a .clear() >> method. *Completely removing* slice assignment has not been broached there. > > Yup, sorry - I did mean to refer to the initial suggestion, rather than > my comments > >>> (I didn't expect such strong responses btw!) >> You are proposing the removal of a general, orthogonal feature (and breaking >> code in consequence!) just because of a new syntax for a single special case of >> that feature. That is quite simply ridiculous. > > Ok, I may have come across a little strongly (was very tired) - I'm not > _actually_ saying we should remove it, I'm just pointing out why > adding .clear() to lists seems to be unnecessary and slightly messy. The > suggested removal of assignments to slices is a theoretical statement. But can you see why your wording might lead the rest of us to believe otherwise? :-) >> .clear() would be non-orthogonal syntactic sugar. That's okay! Python has >> syntactic sugar in a number of other places, too! Appropriate doses of syntactic >> sugar and non-orthogonality are precisely what lets you implement "There should >> be one-- and preferably only one --obvious way to do it." The really key word in >> that sentence is "obvious", not "one". >> >> FWIW, removing slice assignment would be a gross form of non-orthogonality, too. >> __getitem__, __setitem__ and __delitem__ should all be able to accept the same >> indices (or else raise exceptions in the case of immutability). > > hummm - I'm sure it would be confusing behaviour if it was not > available, but I'm not sure how it would be non-orthogonal I might be abusing the term, but to me, orthogonality doesn't just mean avoiding overlapping functionality. It also means not putting in special-case limitations for otherwise general features. It would be odd if you could use integer indices for __getitem__, __setitem__ and __delitem__, but slice indices would work for __getitem__ and not the others. -- 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 lists at cheimes.de Sat Apr 4 17:09:01 2009 From: lists at cheimes.de (Christian Heimes) Date: Sat, 04 Apr 2009 23:09:01 +0200 Subject: with open('com1', 'r') as f: In-Reply-To: References: <8bc55c05-19da-41c4-b916-48e0a4be49b9@p11g2000yqe.googlegroups.com> <20090403002347.163911b4@oxygen> <8de4f974-073f-41cb-9729-341a02ca4c3f@u8g2000yqn.googlegroups.com> <575f77ac-9f14-490b-a165-3ac38127b87a@l38g2000vba.googlegroups.com> Message-ID: gert wrote: > On Apr 3, 10:10 pm, Christian Heimes wrote: >> gert wrote: >>> I do understand, and I went looking into pySerial, but it is a long >>> way from getting compatible with python3.x and involves other libs >>> that are big and non pyhton3.x compatible. >> So don't use Python 3.0. Most people are still using Python 2.5 or 2.6. > > With all respect but why do people in general avoid the question when > they do not know something? > I appreciate the answer and its a valid solution, never the less > useless in the answer I seek. You are missing some background information. :) I've spent a considerable amount of time with the design and development of Python 3.0. I don't mention the fact in order to show off. I just wanna explain to you that my suggestion has a solid background. You are going throw a lot of unnecessary pain and suffering. Python 3.0 is for people, who like to play with cutting edge software and for library developers, that port their code to 3.0. I appreciate your hard work but it's really the wrong way. If you like to offer your help then help the developers of pyserial. Please don't try to come by with a hacky approach as using the old MS DOS magic files. Christian From electriclightheads at gmail.com Sat Apr 4 17:28:05 2009 From: electriclightheads at gmail.com ('2+) Date: Sun, 5 Apr 2009 06:28:05 +0900 Subject: is there a way to collect twitts with python? In-Reply-To: <78f368f8-d9d0-4a9e-888b-1b4dde42025e@o11g2000yql.googlegroups.com> References: <1078018b0903271054y3d4a7162w885c61bf63bc2048@mail.gmail.com> <78f368f8-d9d0-4a9e-888b-1b4dde42025e@o11g2000yql.googlegroups.com> Message-ID: <1078018b0904041428j701b980ft434d8f7f210f8e45@mail.gmail.com> nice info, thanx that # stalk my stalkers example look smart i won't use that one if it was for this ml ;D On Sun, Apr 5, 2009 at 1:22 AM, Bradley Wright wrote: > Just to pimp my own wares: > > http://github.com/bradleywright/yatcip/tree/master > > A Python Twitter client. > -- > 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 gagsl-py2 at yahoo.com.ar Sat Apr 4 18:24:15 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sat, 04 Apr 2009 19:24:15 -0300 Subject: with open('com1', 'r') as f: References: <84dc1c84-27f9-40a5-890a-9760c91d3a6d@k2g2000yql.googlegroups.com> Message-ID: En Sat, 04 Apr 2009 14:11:12 -0300, gert escribi?: > On Apr 4, 5:20?pm, Kushal Kumaran wrote: >> On Fri, 03 Apr 2009 22:10:36 +0200 >> Christian Heimes wrote: >> > gert wrote: >> > > I do understand, and I went looking into pySerial, but it is a long >> > > way from getting compatible with python3.x and involves other libs >> > > that are big and non pyhton3.x compatible. >> >> > So don't use Python 3.0. Most people are still using Python 2.5 or >> > 2.6. >> >> Alternatively, you could look into the pySerial source and find out >> what it does. > > I think pywin32 is the way they do the things I want. Witch is not > python3 ready and way to much work around to do it clean. Using ctypes > is a option but you have to really know what you are doing and what > you are looking for. The last pywin32 release (213) does work with Python 3. If you can wait a few days, I'm working on a proper port of pyserial. Preliminary testing shows it's working fine on Windows. Basically, I've modified the read/write methods to use bytes instead of str, and 2to3 did the rest: Python 3.0.1 (r301:69561, Feb 13 2009, 20:04:18) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. p3> import serial p3> ser = serial.Serial(2) p3> ser.write(b"ATI7\r\n") p3> for line in ser: print(line.rstrip().decode("ascii","replace")) ... ATI7 Configuration Profile... Product type US/Canada Internal Options V32bis,V.FC,V.34+ Fax Options Class 1/Class 2.0 Clock Freq 92.0Mhz Line Options Caller ID,Distinctive Ring Voice Options Speakerphone,TAD Eprom 256k Ram 64k EPROM date 5/13/96 DSP date 5/13/96 EPROM rev 2.0 DSP rev 2.0 OK ^C -- Gabriel Genellina From ericwoodworth at gmail.com Sat Apr 4 18:39:42 2009 From: ericwoodworth at gmail.com (ericwoodworth at gmail.com) Date: Sat, 4 Apr 2009 15:39:42 -0700 (PDT) Subject: Creating a session in windows to auth to remote machines Message-ID: <5662d84f-faa9-4792-94e9-9ab3add4e604@c9g2000yqm.googlegroups.com> Hi, I'm trying to auth to remote machines so I can plunder WMI to get logs and settings and the like. My script works for most of my machines because they're all in the same domain and I run the script as somebody who has enough access to get at this stuff but for machines off the domain I'm stuck. Now I'm primarily a sys/network admin and not a programmer. As a sys admin if I had this problem while trying to pull eventlogs manually with eventvwr I would simply map a drive to the remote machine. That would allow me to enter a username/password and then once I was authed I'd have a session and I'd be able to piggyback on that session to pull logs. I'm looking to do exactly that from inside my script. I could probably import os and call the net use command to map a drive and get a session that way but that feels really sloppy to me. I want to be able to explicitly close this session when I'm done with it too. So I figure there's a com object I could call to open this session for me but I can't figure out what it is. Any help would be appreciated! Thanks From nospam at nospam.invalid Sat Apr 4 18:46:11 2009 From: nospam at nospam.invalid (Rahul) Date: Sat, 4 Apr 2009 22:46:11 +0000 (UTC) Subject: cProfile.py not found. Message-ID: I need to profile a slow-running code. The problem is I cannot seem to find cProfile.py. Where can I get it? Is it not included in the normal distro? I tried googling it up and theres tons of info on how to use it but no links for where to download it from. I am using Python 2.4.4 (#3, Feb 17 2008, 15:06:10). I am aware this is kind of dated but some of our legacy codes insist on using that exact version. -- Rahul From ericwoodworth at gmail.com Sat Apr 4 18:47:43 2009 From: ericwoodworth at gmail.com (ericwoodworth at gmail.com) Date: Sat, 4 Apr 2009 15:47:43 -0700 (PDT) Subject: Creating a session in windows to auth to remote machines References: <5662d84f-faa9-4792-94e9-9ab3add4e604@c9g2000yqm.googlegroups.com> Message-ID: <627d97ea-b383-407e-b184-3f88005649d9@j39g2000yqn.googlegroups.com> On Apr 4, 6:39?pm, ericwoodwo... at gmail.com wrote: > Hi, > ? ? ?I'm trying to auth to remote machines so I can plunder WMI to get > logs and settings and the like. ?My script works for most of my > machines because they're all in the same domain and I run the script > as somebody who has enough access to get at this stuff but for > machines off the domain I'm stuck. > > ? ? ?Now I'm primarily a sys/network admin and not a programmer. ?As a > sys admin if I had this problem while trying to pull eventlogs > manually with eventvwr I would simply map a drive to the remote > machine. ?That would allow me to enter a username/password and then > once I was authed I'd have a session and I'd be able to piggyback on > that session to pull logs. > > ? ? ?I'm looking to do exactly that from inside my script. ?I could > probably import os and call the net use command to map a drive and get > a session that way but that feels really sloppy to me. ?I want to be > able to explicitly close this session when I'm done with it too. > > ? ? ?So I figure there's a com object I could call to open this > session for me but I can't figure out what it is. ?Any help would be > appreciated! > > Thanks Also I am writing this in Python. So I can use win32com if that's the way to go or I can use anything python 2.6 has built in. I mentioned com objects because I'm using a lot of those currently but if there's a more pythonic way to do what I"m after then I'm all ears. Thanks From robert.kern at gmail.com Sat Apr 4 18:57:12 2009 From: robert.kern at gmail.com (Robert Kern) Date: Sat, 04 Apr 2009 17:57:12 -0500 Subject: cProfile.py not found. In-Reply-To: References: Message-ID: On 2009-04-04 17:46, Rahul wrote: > I need to profile a slow-running code. The problem is I cannot seem to find > cProfile.py. > > Where can I get it? Is it not included in the normal distro? I tried > googling it up and theres tons of info on how to use it but no links for > where to download it from. > > I am using Python 2.4.4 (#3, Feb 17 2008, 15:06:10). > > I am aware this is kind of dated but some of our legacy codes insist on > using that exact version. What system are you on? Some Linux distributions put it into a separate package, like python-profile. The python.org Windows and Mac binaries should have it, though. -- 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 gallium.arsenide at gmail.com Sat Apr 4 19:08:27 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Sat, 4 Apr 2009 16:08:27 -0700 (PDT) Subject: cProfile.py not found. References: Message-ID: I believe cProfile was added in 2.5. Your best bet on 2.4 is probably the profile module. That is what the docs recommend. John From nospam at nospam.invalid Sat Apr 4 19:08:55 2009 From: nospam at nospam.invalid (Rahul) Date: Sat, 4 Apr 2009 23:08:55 +0000 (UTC) Subject: cProfile.py not found. References: Message-ID: Robert Kern wrote in news:mailman.3312.1238885852.11746.python-list at python.org: > What system are you on? Some Linux distributions put it into a > separate package, like python-profile. The python.org Windows and Mac > binaries should have it, though. > > THanks Robert. I'm on RHEL. Tried yum search python-profile. No hits. Also tried a locate on cProfile. No hits again. Did a yum search python. Huge list but nothing promising. Yet, I reproduce it below just in case its hidden somewhere in the list. What options do I have now? python-sqlite2.i386 : DB-API 2.0 interface for SQLite 3.x tix.i386 : A set of extension widgets for Tk tix-devel.i386 : Tk Interface eXtension development files tix-doc.i386 : Tk Interface eXtension documentation vim-enhanced.i386 : A version of the VIM editor which includes recent : enhancements. Django.noarch : A high-level Python Web framework Django-doc.noarch : Documentation for Django MySQL-python.i386 : An interface to MySQL OpenIPMI-python.i386 : OpenIPMI Python language bindings PyQt.i386 : Python bindings for Qt PyQt-examples.i386 : Examples for PyQt PyXML.i386 : XML libraries for python. PyYAML.noarch : YAML parser and emitter for Python Pyrex.noarch : A compiler/language for writing Python extension modules. PythonCard.noarch : PythonCard GUI construction toolkit R2spec.noarch : Python script to generate R spec file SOAPpy.noarch : Full-featured SOAP library for Python TurboGears.noarch : Back-to-front web development in Python ant-scripts.i386 : Additional scripts for ant archivemail.noarch : A tool for archiving and compressing old email in mailboxes audit-libs-python.i386 : Python bindings for libaudit babel.noarch : Tools for internationalizing Python applications beecrypt-python.i386 : Files needed for python applications using beecrypt. booty.noarch : simple python bootloader config lib bsf.i386 : Bean Scripting Framework cobbler.noarch : Boot server configurator ctopy.noarch : C to Python translator cvs2svn.noarch : CVS to Subversion Repository Converter db4.i386 : The Berkeley DB database library (version 4) for C. dbus-python.i386 : D-Bus Python Bindings ddd.i386 : GUI for several command-line debuggers deluge.i386 : Graphical BitTorrent client with support for DHT, UPnP, and PEX denyhosts.noarch : A script to help thwart ssh server attacks epydoc.noarch : Edward Loper's Python API documentation generation tool exaile.i386 : A music player flumotion.i386 : Flumotion - the Fluendo Streaming Server fuzzyclock.noarch : Generates fuzzy clock output gamin-python.i386 : Python bindings for the gamin library gdal.i386 : Geospatial Data Abstraction Library gdal-python.i386 : Python modules for the GDAL file format library getmail.noarch : POP3 mail retriever with reliable Maildir delivery glade2.i386 : A GTK+ GUI builder. gnome-menus.i386 : A menu system for the GNOME project gnome-python2.i386 : The sources for the PyGNOME Python extension module gnome-python2-applet.i386 : Python bindings for GNOME Panel applets. gnome-python2-bonobo.i386 : Python bindings for interacting with bonobo. gnome-python2-canvas.i386 : Python bindings for the GNOME Canvas. gnome-python2-desktop.i386 : The sources for additional PyGNOME Python extension : modules for the GNOME desktop gnome-python2-extras.i386 : The sources for additional. PyGNOME Python extension : modules. gnome-python2-gconf.i386 : Python bindings for interacting with GConf gnome-python2-gnomedesktop.i386 : Python bindings for interacting with gnome- : desktop gnome-python2-gnomekeyring.i386 : Python bindings for interacting with gnome- : keyring gnome-python2-gnomeprint.i386 : Python bindings for interacting with : libgnomeprint gnome-python2-gnomevfs.i386 : Python bindings for interacting with gnome- vfs gnome-python2-gtkhtml2.i386 : Python bindings for interacting with gtkhtml2 python-twisted-names.i386 : A Twisted DNS implementation python-twisted-web.i386 : Twisted web server, programmable in Python python-twisted-web2.i386 : Twisted web server, programmable in Python python-twisted-words.i386 : Twisted Words contains Instant Messaging implementations python-twitter.noarch : A python wrapper around the Twitter API python-tz.noarch : World Timezone Definitions for Python python-urlgrabber.noarch : High-level cross-protocol url-grabber python-urljr.noarch : Common interface to urllib2 and curl for making HTTP requests python-urwid.noarch : Console UI Library for Python python-urwid.i386 : Console UI Library for Python python-utmp.i386 : Python module for working with utmp python-vcpx.noarch : Version Control Patch eXchanger python-virtinst.noarch : Python modules for starting Xen guest installations python-vorbis.i386 : Python bindings for libvorbis python-webob.noarch : WSGI request and response object python-which.noarch : Small which replacement that can be used as a Python module python-wifi.noarch : Python binding for the wireless (wifi) extensions python-xattr.i386 : Extended attributes for python python-xdg.noarch : Python library to access freedesktop.org standards python-xlib.i386 : Complete X11R6 client-side implementation python-xlrd.noarch : Library to extract data from Microsoft Excel (tm) spreadsheet files python-yadis.noarch : Relying party support for the Yadis service discovery protocol python-yaml.noarch : Python package implementing YAML parser and emitter python-yenc.i386 : yEnc Module for Python python-zope-interface.i386 : Zope 3 Interface Infrastructure pytone.i386 : Music Jukebox with a Curses Based GUI pytraffic.i386 : Rush Hour game pytz.noarch : World Timezone Definitions for Python pywebdav.noarch : WebDAV library pyxattr.i386 : Extended attributes library wrapper for Python pyxdg.noarch : Python library to access freedesktop.org standards pyxf86config.i386 : Python wrappers for libxf86config pyzor.noarch : Pyzor collaborative spam filtering system rdiff-backup.i386 : Convenient and transparent local/remote incremental mirror/backup rhnlib.noarch : Python libraries for the RHN project rhpl.i386 : Library of python code used by programs in Red Hat Linux rhpxl.i386 : Python library for configuring and running X. rpm-python.i386 : Python bindings for apps which will manipulate RPM packages. rrdtool-python.i386 : Python RRDtool bindings sagator.noarch : SAGATOR - antivir/antispam gateway for smtp server scapy.noarch : Interactive packet manipulation tool and network scanner scipy.i386 : Scipy: Scientific Tools for Python scons.noarch : An Open Source software construction tool sip.i386 : SIP - Python/C++ Bindings Generator sip-devel.i386 : Files needed to generate Python bindings for any C++ class library solarwolf.noarch : Python SDL game where you have to collect energy cubes spacepong.noarch : An innovative Python SDL game that is controlled with the mouse stgit.noarch : StGIT provides similar functionality to Quilt on top of GIT sulk.i386 : Sulk, the hackable Space Hulk supybot.noarch : Cross-platform IRC bot written in Python swig.i386 : Simplified Wrapper and Interface Generator sympy.noarch : A Python library for symbolic mathematics system-config-bind.noarch : The Red Hat BIND DNS Configuration Tool. tkinter.i386 : A graphical user interface for the Python scripting language. trytond.noarch : Server for the Tryton application framework txt2tags.noarch : Converts text files to HTML, XHTML, sgml, LaTeX, man... wxPython.i386 : GUI toolkit for the Python programming language wxPython-devel.i386 : Development files for wxPython add-on modules yum-protect-packages.noarch : Yum plugin to prevents Yum from removing itself and other protected packages zope.i386 : Web application server for flexible content management applications -- Rahul From benjamin at python.org Sat Apr 4 19:13:40 2009 From: benjamin at python.org (Benjamin Peterson) Date: Sat, 4 Apr 2009 18:13:40 -0500 Subject: [RELEASED] Python 3.1 alpha 2 Message-ID: <1afaf6160904041613t4bb44976x65d7a4d4a90f2c47@mail.gmail.com> On behalf of the Python development team, I'm thrilled to announce the second alpha release of Python 3.1. Python 3.1 focuses on the stabilization and optimization of features and changes Python 3.0 introduced. For example, the new I/O system has been rewritten in C for speed. Other features include an ordered dictionary implementation and support for ttk Tile in Tkinter. For a more extensive list of changes in 3.1, see http://doc.python.org/dev/py3k/whatsnew/3.1.html or Misc/NEWS in the Python distribution. Please note that this is an alpha releases, and as such is not suitable for production environments. We continue to strive for a high degree of quality, but there are still some known problems and the feature sets have not been finalized. This alpha is being released to solicit feedback and hopefully discover bugs, as well as allowing you to determine how changes in 3.1 might impact you. If you find things broken or incorrect, please submit a bug report at http://bugs.python.org For more information and downloadable distributions, see the Python 3.1 website: http://www.python.org/download/releases/3.1/ See PEP 375 for release schedule details: http://www.python.org/dev/peps/pep-0375/ Regards, -- Benjamin Benjamin Peterson benjamin at python.org Release Manager (on behalf of the entire python-dev team and 3.1's contributors) From robert.kern at gmail.com Sat Apr 4 19:15:36 2009 From: robert.kern at gmail.com (Robert Kern) Date: Sat, 04 Apr 2009 18:15:36 -0500 Subject: cProfile.py not found. In-Reply-To: References: Message-ID: On 2009-04-04 18:08, John Yeung wrote: > I believe cProfile was added in 2.5. Your best bet on 2.4 is probably > the profile module. That is what the docs recommend. Oops, I missed that piece of information. Alternately, the OP can install lsprof, which was cProfile's third-party incarnation before it got integrated into the stdlib: http://codespeak.net/svn/user/arigo/hack/misc/lsprof/ -- 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 sjmachin at lexicon.net Sat Apr 4 19:17:25 2009 From: sjmachin at lexicon.net (John Machin) Date: Sat, 4 Apr 2009 16:17:25 -0700 (PDT) Subject: cProfile.py not found. References: Message-ID: <0a8400dc-b14b-4bb9-a608-7327fe88aff0@j18g2000prm.googlegroups.com> On Apr 5, 8:46?am, Rahul wrote: > I need to profile a slow-running code. The problem is I cannot seem to find ? > cProfile.py. > > Where can I get it? Is it not included in the normal distro? I tried > googling it up and theres tons of info on how to use it but no links for > where to download it from. > > I am using Python 2.4.4 (#3, Feb 17 2008, 15:06:10). Read the fantastic manual: http://docs.python.org/library/profile.html Note: this is the *first* hit when you google either "cprofile" or "cProfile.py". """ The Python standard library provides three different profilers: 1. cProfile is recommended for most users; it?s a C extension with reasonable overhead that makes it suitable for profiling long-running programs. Based on lsprof, contributed by Brett Rosen and Ted Czotter. New in version 2.5. """ From nospam at nospam.invalid Sat Apr 4 19:38:25 2009 From: nospam at nospam.invalid (Rahul) Date: Sat, 4 Apr 2009 23:38:25 +0000 (UTC) Subject: cProfile.py not found. References: Message-ID: John Yeung wrote in news:c0752f32-b0cf-4fde- 87a8-eb665252e961 at k41g2000yqh.googlegroups.com: > I believe cProfile was added in 2.5. Your best bet on 2.4 is probably > the profile module. That is what the docs recommend. > Thanks John. That works. I'll use "profile" instead. -- Rahul From nospam at nospam.invalid Sat Apr 4 19:41:02 2009 From: nospam at nospam.invalid (Rahul) Date: Sat, 4 Apr 2009 23:41:02 +0000 (UTC) Subject: cProfile.py not found. References: <0a8400dc-b14b-4bb9-a608-7327fe88aff0@j18g2000prm.googlegroups.com> Message-ID: John Machin wrote in news:0a8400dc-b14b-4bb9-a608- 7327fe88aff0 at j18g2000prm.googlegroups.com: > Read the fantastic manual: > > http://docs.python.org/library/profile.html [snip] > cProfile is recommended for most users; it's a C extension with > reasonable overhead that makes it suitable for profiling long-running > programs. Based on lsprof, contributed by Brett Rosen and Ted Czotter. > > New in version 2.5. > Thanks John; I did read the manual which is why I decided to use cProfile as it was "recommended for most users". I missed the last bit about "New in version 2.5." -- Rahul From sjmachin at lexicon.net Sat Apr 4 19:55:58 2009 From: sjmachin at lexicon.net (John Machin) Date: Sat, 4 Apr 2009 16:55:58 -0700 (PDT) Subject: cProfile.py not found. References: <0a8400dc-b14b-4bb9-a608-7327fe88aff0@j18g2000prm.googlegroups.com> Message-ID: <4c8ee09e-71e2-464a-a3c0-b630b47077cb@c18g2000prh.googlegroups.com> On Apr 5, 9:41?am, Rahul wrote: > John Machin wrote in news:0a8400dc-b14b-4bb9-a608- > 7327fe88a... at j18g2000prm.googlegroups.com: > > > > > Read the fantastic manual: > > >http://docs.python.org/library/profile.html > ?[snip] > > ? ? ? cProfile is recommended for most users; it's a C extension with > > reasonable overhead that makes it suitable for profiling long-running > > programs. Based on lsprof, contributed by Brett Rosen and Ted Czotter. > > > ? ? ? New in version 2.5. > > Thanks John; I did read the manual which is why I decided to use cProfile > as it was "recommended for most users". ?I missed the last bit about "New > in version 2.5." Looks like our definitions of "read" differ :-) From nospam at nospam.invalid Sat Apr 4 19:56:51 2009 From: nospam at nospam.invalid (Rahul) Date: Sat, 4 Apr 2009 23:56:51 +0000 (UTC) Subject: what does "execfile" mean within profiler output and why does it not have a attached line number Message-ID: "profile" tells me that most of my runtime was spent in just one part (1.28 sec cumulatively out of 1.29 secs. But what is "execfile"? I don't see this as a function call with my python code. Also what's the 0 in the snippet: ":0(execfile)"? Isn't there supposed to be a line-number? Looking up "execfile" in the python manual leads me to "exec": "This statement supports dynamic execution of Python code." But that seems pretty generic; how can I now try figuring out which part of my python file is the bottleneck? Sorry, I'm a newbiee to profiling. ###################################################################### 51651 function calls (37762 primitive calls) in 1.290 CPU seconds ncalls tottime percall cumtime percall filename:lineno(function) [snip] 1 0.010 0.010 1.280 1.280 :0(execfile) [snip] ########################################################## -- Rahul From nospam at nospam.invalid Sat Apr 4 20:00:47 2009 From: nospam at nospam.invalid (Rahul) Date: Sun, 5 Apr 2009 00:00:47 +0000 (UTC) Subject: cProfile.py not found. References: <0a8400dc-b14b-4bb9-a608-7327fe88aff0@j18g2000prm.googlegroups.com> <4c8ee09e-71e2-464a-a3c0-b630b47077cb@c18g2000prh.googlegroups.com> Message-ID: John Machin wrote in news:4c8ee09e-71e2-464a-a3c0- b630b47077cb at c18g2000prh.googlegroups.com: > Looks like our definitions of "read" differ :-) > Sorry. I ought to have said "skimmed" :) I guess I am one of those guilty lazy-bums that the manual refers to under <<>> -- Rahul From ross.jett at gmail.com Sat Apr 4 20:42:58 2009 From: ross.jett at gmail.com (Ross) Date: Sat, 4 Apr 2009 17:42:58 -0700 (PDT) Subject: possible pairings in a set Message-ID: <905f6361-089e-47d4-b4e6-88877d2ef402@k2g2000yql.googlegroups.com> I'm new to python and I'm trying to come up with a function that takes a given number of players in a game and returns all possible unique pairings. Here's the code I've come up with so far, but I'm not getting the output I'd like to: def all_pairings(players): cleanlist = [] for i in range(players): cleanlist.append(i) return cleanlist start = 0 follow = start +1 finallist = [] while follow <= len(cleanlist)-1: for player in cleanlist: mini = cleanlist[start],cleanlist[follow] finallist.append(mini) follow +=1 start+=1 return finallist If I were to execute the function with all_pairings(4), I want to get the output [[0,1],[0,2],[0,3],[1,2],[1,3],[2,3]. Instead, I get [0,1,2,3] with the code I currently have. Can you guys help me out? Also, if my code is considered ugly or redundant by this community, can you make suggestions to clean it up? From sjmachin at lexicon.net Sat Apr 4 20:43:08 2009 From: sjmachin at lexicon.net (John Machin) Date: Sat, 4 Apr 2009 17:43:08 -0700 (PDT) Subject: what does "execfile" mean within profiler output and why does it not have a attached line number References: Message-ID: <7f70c8ed-ca43-40d3-bf9d-a39baec32138@d7g2000prl.googlegroups.com> On Apr 5, 9:56?am, Rahul wrote: > "profile" tells me that most of my runtime was spent in just one part (1.28 > sec cumulatively out of 1.29 secs. But what is "execfile"? I don't see this > as a function call with my python code. Also what's the 0 in the snippet: ? > ":0(execfile)"? Isn't there supposed to be a line-number? > > Looking up "execfile" in the python manual leads me to "exec": "This > statement supports dynamic execution of Python code." > > But that seems pretty generic; how can I now try figuring out which part of > my python file is the bottleneck? > > Sorry, I'm a newbiee to profiling. > > ###################################################################### > ? ? ? ?51651 function calls (37762 primitive calls) in 1.290 CPU seconds > ?ncalls ?tottime ?percall ?cumtime ?percall filename:lineno(function) > [snip] > ? ? 1 ? ?0.010 ? ?0.010 ? ?1.280 ? ?1.280 :0(execfile) > [snip] > ########################################################## That means no more than "the profiler executed all of your code once, it took 0.01 seconds inside the execfile itself, 0.01 seconds per execution, total time spent by the execfile *and* what it called was 1.28 seconds ("cum" == "cumulative"), again 1.28 secs per execution" So ignore that and look at the figures for the app functions/methods. From benjamin at python.org Sat Apr 4 20:53:01 2009 From: benjamin at python.org (Benjamin Peterson) Date: Sun, 5 Apr 2009 00:53:01 +0000 (UTC) Subject: possible pairings in a set References: <905f6361-089e-47d4-b4e6-88877d2ef402@k2g2000yql.googlegroups.com> Message-ID: Ross gmail.com> writes: > Can you guys help me out? Do you have Python 2.6? If so, it's a solved problem. :) import itertools possible_pairings = list(itertools.combinations(players, 2)) From robert.kern at gmail.com Sat Apr 4 20:59:24 2009 From: robert.kern at gmail.com (Robert Kern) Date: Sat, 04 Apr 2009 19:59:24 -0500 Subject: what does "execfile" mean within profiler output and why does it not have a attached line number In-Reply-To: References: Message-ID: On 2009-04-04 18:56, Rahul wrote: > "profile" tells me that most of my runtime was spent in just one part (1.28 > sec cumulatively out of 1.29 secs. But what is "execfile"? I don't see this > as a function call with my python code. Also what's the 0 in the snippet: > ":0(execfile)"? Isn't there supposed to be a line-number? It's a builtin function, so it has no filename or line number. execfile is a function that reads a Python file and executes its code. This is almost certainly the execfile call from profile.py itself that is running your code. Ignore it. > Looking up "execfile" in the python manual leads me to "exec": "This > statement supports dynamic execution of Python code." > > But that seems pretty generic; how can I now try figuring out which part of > my python file is the bottleneck? To quickly find your hotspots, start by sorting by 'time' (that would be displayed as the 'tottime' column in the human-readable output). That tells you how much time is spent in each function itself, excluding the time it spends calling out to other functions. For example, per the docs under "Instant User?s Manual" (which you might want to spend a little more time with): p.sort_stats('time').print_stats(10) -- 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 jjposner at snet.net Sat Apr 4 21:01:08 2009 From: jjposner at snet.net (John Posner) Date: Sat, 04 Apr 2009 21:01:08 -0400 Subject: possible pairings in a set In-Reply-To: <905f6361-089e-47d4-b4e6-88877d2ef402@k2g2000yql.googlegroups.com> References: <905f6361-089e-47d4-b4e6-88877d2ef402@k2g2000yql.googlegroups.com> Message-ID: <47BBFD2A70384B87965C61507F1FFF6E@AMDUP> >> Also, if my code is considered ugly or redundant by this community, >> can you make suggestions to clean it up? Python is pretty mature: if you have a simple, generic problem, the chances are that someone else has already solved it, packaging the solution in a library (or "module"). For your job, the "itertools" module includes the function "combinations": import itertools for comb in itertools.combinations([0,1,2,3], 2): print comb output: (0, 1) (0, 2) (0, 3) (1, 2) (1, 3) (2, 3) Note that the output of itertools.combinations() is not a list but an iterator. If you need the entire list, use the list() function to have the iterator deliver all the goods at once: mylist = list(itertools.combinations([0,1,2,3], 2)) E-mail message checked by Spyware Doctor (6.0.0.386) Database version: 5.12110 http://www.pctools.com/en/spyware-doctor-antivirus/ From martin at v.loewis.de Sat Apr 4 21:08:40 2009 From: martin at v.loewis.de (=?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?=) Date: Sun, 05 Apr 2009 03:08:40 +0200 Subject: Python Goes Mercurial In-Reply-To: References: <49d65b62$0$30904$9b622d9e@news.freenet.de> Message-ID: <49D80498.6000101@v.loewis.de> >> I don't like git because it is too difficult for me. In many cases, >> git would refuse to do operations like updating or local committing, >> producing error messages I was not able to understand ... > > Post an example of what you were trying to do, with the exact messages, and > we can walk you through it. Unfortunately, these are many months ago, and I don't recall the exact error messages. I wasn't really asking for help, merely pointing out that I dislike git because it makes me ask for help (something I did not have to do for CVS or subversion, except for very special cases). Regards, Martin From davea at ieee.org Sat Apr 4 21:27:34 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 04 Apr 2009 20:27:34 -0500 Subject: possible pairings in a set In-Reply-To: <905f6361-089e-47d4-b4e6-88877d2ef402@k2g2000yql.googlegroups.com> References: <905f6361-089e-47d4-b4e6-88877d2ef402@k2g2000yql.googlegroups.com> Message-ID: <49D80906.7000702@ieee.org> Ross wrote: > I'm new to python and I'm trying to come up with a function that takes > a given number of players in a game and returns all possible unique > pairings. Here's the code I've come up with so far, but I'm not > getting the output I'd like to: > > def all_pairings(players): > cleanlist = [] > for i in range(players): > cleanlist.append(i) > return cleanlist > start = 0 > follow = start +1 > finallist = [] > while follow <= len(cleanlist)-1: > for player in cleanlist: > mini = cleanlist[start],cleanlist[follow] > finallist.append(mini) > follow +=1 > start+=1 > return finallist > > If I were to execute the function with all_pairings(4), I want to get > the output [[0,1],[0,2],[0,3],[1,2],[1,3],[2,3]. Instead, I get > [0,1,2,3] with the code I currently have. Can you guys help me out? > Also, if my code is considered ugly or redundant by this community, > can you make suggestions to clean it up? > > First problem is the return you have in the middle of the function, which returns cleanlist. That's why you're getting [0, 1, 2, 3] Once you fix that, you'll find you have exceptions in the code where cleanlist[follow] doesn't work if follow is equal to 4. I'm afraid the code is ugly, and therefore hard to understand or fix. Let me suggest an approach, rather than just giving you the code. Why not create a nested for which generates two subscripts between 0 and 4, then append only those pairs in which the second is strictly greater than the first. It won't be the quickest, but it will be easy to follow. No while loop, no explicit incrementing of variables, and in fact no subscripting of the lists. Just let them supply their own values. cleanlist = range(players) # no need for any loop, it's already a list (except in Python 3.0, in which case it's a sequence that still works just as well) finallist = [] for i .....: for j... if i When I visit ... http://www.python.org/doc/lib/lib.html ... I get redirected to ... http://www.python.org/doc/2.5.2/lib/lib.html ... which seems a bit old. -- Brandon Craig Rhodes brandon at rhodesmill.org http://rhodesmill.org/brandon From martin at v.loewis.de Sat Apr 4 21:32:57 2009 From: martin at v.loewis.de (=?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?=) Date: Sun, 05 Apr 2009 03:32:57 +0200 Subject: Python Goes Mercurial In-Reply-To: References: <49d65b62$0$30904$9b622d9e@news.freenet.de> Message-ID: <49d80a4a$0$30643$9b622d9e@news.freenet.de> >> I don't like git because it is too difficult for me. In many cases, >> git would refuse to do operations like updating or local committing, >> producing error messages I was not able to understand ... > > Post an example of what you were trying to do, with the exact messages, and > we can walk you through it. Unfortunately, these are many months ago, and I don't recall the exact error messages. I wasn't really asking for help, merely pointing out that I dislike git because it makes me ask for help (something I did not have to do for CVS or subversion, except for very special cases). Regards, Martin From google at mrabarnett.plus.com Sat Apr 4 21:40:30 2009 From: google at mrabarnett.plus.com (MRAB) Date: Sun, 05 Apr 2009 02:40:30 +0100 Subject: possible pairings in a set In-Reply-To: <49D80906.7000702@ieee.org> References: <905f6361-089e-47d4-b4e6-88877d2ef402@k2g2000yql.googlegroups.com> <49D80906.7000702@ieee.org> Message-ID: <49D80C0E.3010207@mrabarnett.plus.com> Dave Angel wrote: > > > Ross wrote: >> I'm new to python and I'm trying to come up with a function that takes >> a given number of players in a game and returns all possible unique >> pairings. Here's the code I've come up with so far, but I'm not >> getting the output I'd like to: >> >> def all_pairings(players): >> cleanlist = [] >> for i in range(players): >> cleanlist.append(i) >> return cleanlist >> start = 0 >> follow = start +1 >> finallist = [] >> while follow <= len(cleanlist)-1: >> for player in cleanlist: >> mini = cleanlist[start],cleanlist[follow] >> finallist.append(mini) >> follow +=1 >> start+=1 >> return finallist >> >> If I were to execute the function with all_pairings(4), I want to get >> the output [[0,1],[0,2],[0,3],[1,2],[1,3],[2,3]. Instead, I get >> [0,1,2,3] with the code I currently have. Can you guys help me out? >> Also, if my code is considered ugly or redundant by this community, >> can you make suggestions to clean it up? >> >> > First problem is the return you have in the middle of the function, > which returns cleanlist. That's why you're getting [0, 1, 2, 3] > > Once you fix that, you'll find you have exceptions in the code where > cleanlist[follow] doesn't work if follow is equal to 4. > > I'm afraid the code is ugly, and therefore hard to understand or fix. > Let me suggest an approach, rather than just giving you the code. Why > not create a nested for which generates two subscripts between 0 and 4, > then append only those pairs in which the second is strictly greater > than the first. It won't be the quickest, but it will be easy to follow. > > No while loop, no explicit incrementing of variables, and in fact no > subscripting of the lists. Just let them supply their own values. > cleanlist = range(players) # no need for any loop, it's > already a list (except in Python 3.0, in which case it's a sequence that > still works just as well) > finallist = [] > for i .....: > for j... > if i append ... > return finallist > Actually, cleanlist[i] == i, so it's pointless. You can do just: finallist = [] for i in range(players): for j in range(i + 1, players): finallist.append((i, j)) or, using list comprehension: finallist = [(i, j) for i in range(players) for j in range(i + 1, players)] From martin at v.loewis.de Sat Apr 4 21:46:30 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Sun, 05 Apr 2009 03:46:30 +0200 Subject: python docs redirect on python.org is old In-Reply-To: References: Message-ID: <49d80d87$0$30878$9b622d9e@news.freenet.de> Brandon Craig Rhodes wrote: > When I visit ... > > http://www.python.org/doc/lib/lib.html > > ... I get redirected to ... > > http://www.python.org/doc/2.5.2/lib/lib.html > > ... which seems a bit old. That is intentional. Use http://docs.python.org/library/ instead. Regards, Martin From aahz at pythoncraft.com Sat Apr 4 22:01:48 2009 From: aahz at pythoncraft.com (Aahz) Date: 4 Apr 2009 19:01:48 -0700 Subject: Python Tk Tix GUI documentation & builder overview and tips References: Message-ID: [posted & e-mailed] In article , wrote: > >Before starting I spent some effort to find >a) relevant documentation, >b) GUI Builders which might help me, >c) answers to non-obvious questions. > >The process took some time and effort so I want to share my findings: Adding this to wiki.python.org/moin/ would be very welcome! (You should probably hunt around a bit to find the best spot.) -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." --Brian W. Kernighan From skip at pobox.com Sat Apr 4 22:02:51 2009 From: skip at pobox.com (skip at pobox.com) Date: Sat, 4 Apr 2009 21:02:51 -0500 Subject: cProfile.py not found. In-Reply-To: References: Message-ID: <18904.4427.865350.837616@montanaro.dyndns.org> Rahul> I need to profile a slow-running code. The problem is I cannot Rahul> seem to find cProfile.py. You want cProfile.c (compiled to cProfile.so). Rahul> I am using Python 2.4.4 (#3, Feb 17 2008, 15:06:10). cProfile was new in 2.5, but the code backports with no problem to 2.4. Just grab it from a 2.5 distro and build/install it. -- Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/ "XML sucks, dictionaries rock" - Dave Beazley From castironpi at gmail.com Sat Apr 4 22:08:27 2009 From: castironpi at gmail.com (Aaron Brady) Date: Sat, 4 Apr 2009 19:08:27 -0700 (PDT) Subject: pygame and socket.recv References: <1c6574dd-1bfb-4c45-a744-58761e5d1f06@k2g2000yql.googlegroups.com> <2084b37d-485c-435f-be5c-8ee1bc901d70@p11g2000yqe.googlegroups.com> Message-ID: <95390b4a-da9d-4030-ba44-31528cf53e6f@y7g2000yqa.googlegroups.com> On Apr 2, 5:46?pm, Tim Wintle wrote: > On Thu, 2009-04-02 at 06:50 -0700, Aaron Brady wrote: > > It's just that if you register a collision in between the time that > > one object has changed its position and momentum, and the time you > > learn about it, you have to retroactively edit the collision, restore > > hit points, and recalculate the other object's position and momentum, > > to name a few. > > So it sounds like what you are trying to do is emulate shared RAM > between the two players! > > The problem being that you have to effectively lock the variables on one > machine while the other one reacts. > > I believe the answer normally used is the client-server architecture. > > Choose one machine to act as the "server" somehow, then let that control > the entire game state. Both players pass events to this server method > (using non-blocking sockets), and render things in the position they > believe they are in. > > I believe that is how all the current RTS games work, certainly how Red > Alert worked, and is how all FPS games work (e.g. in unreal there is > always a "server", and in single player mode the clients are simply bots > and the local client application.) > > You might find the docs for Unreal networking (c. 1999) useful to > explain what they did better than I can.http://unreal.epicgames.com/Network.htm I don't think you did the link justice in your introduction of it. I don't think I did your post justice... by ignoring it. Here is a quote from the paper: """ Player Prediction This approach is purely predictive, and it gives one the best of both worlds: In all cases, the server remains completely authoritative. Nearly all the time, the client movement simulation exactly mirrors the client movement carried out by the server, so the client's position is seldom corrected. Only in the rare case, such as a player getting hit by a rocket, or bumping into an enemy, will the client's location need to be corrected. """ Please excuse me briefly; the FBI is beating down my door for quoting it. Regardless, I think it was the 'correction', per se, step that I was interested in. Does it suffice to merely wait until recovering from the lag to notify the player s/he's dead, and proceed normally otherwise? Should the keystroke that the player hit during lag be delivered asap, or 'in time' with how many seconds elapsed since the last known frame? My game in particular, I have a hunch, relies on more precise timing than you might expect in a game. > So it sounds like what you are trying to do is emulate shared RAM > between the two players! No, that's my other project ;) From castironpi at gmail.com Sat Apr 4 22:11:59 2009 From: castironpi at gmail.com (Aaron Brady) Date: Sat, 4 Apr 2009 19:11:59 -0700 (PDT) Subject: pygame and socket.recv References: <1c6574dd-1bfb-4c45-a744-58761e5d1f06@k2g2000yql.googlegroups.com> <2084b37d-485c-435f-be5c-8ee1bc901d70@p11g2000yqe.googlegroups.com> Message-ID: <405eeda6-fe69-4b26-bec1-e34cbb6f1ec1@z9g2000yqi.googlegroups.com> On Apr 2, 4:13?am, Tim Wintle wrote: > On Wed, 2009-04-01 at 18:45 -0700, Aaron Brady wrote: > > > My game loop looks like this: > > > poll events, get 1 at most > > send to server > > wait for server reply > > render entire frame > > The look I'm suggesting is: > > poll events > write to (non-blocking) socket > render frame > check non-blocking socket and add events to the event queue > > > Yes, I am blocking for the data to come down the network. > > Unfortunately, if I use any "prediction," I will have to go back and > > un-render the previous frame, then redraw with the new information. > > Sounds like that may have to be re-factored slightly, afraid this is why > real-time networked games are tough to make. snip > a) How much to move etc. is decided based on some real-time solution > (not on the number of frames). Ideally all movement methods take a > parameter that is a delta in time. (i.e. 1/40th of a second) > > b) That time is the time that is synced across machines - if machine B > has to put it's timer forward 1/50 of a second, you call all the methods > above with a timedelta of 1/50 before continuing. Tim Wintle's link has an interesting illustration of this: """ Basic Concepts The Update Loop For example, Doom's movement physics looks like "Position += PositionIncrment" while Unreal's looks like "Position += Velocity * DeltaTime". This enables greater frame rate scalability. """ ... Perhaps you've heard of him. From skip at pobox.com Sat Apr 4 22:20:36 2009 From: skip at pobox.com (skip at pobox.com) Date: Sat, 4 Apr 2009 21:20:36 -0500 Subject: Python Goes Mercurial In-Reply-To: <49D80498.6000101@v.loewis.de> References: <49d65b62$0$30904$9b622d9e@news.freenet.de> <49D80498.6000101@v.loewis.de> Message-ID: <18904.5492.813651.140495@montanaro.dyndns.org> Martin> I wasn't really asking for help, merely pointing out that I Martin> dislike git because it makes me ask for help (something I did Martin> not have to do for CVS or subversion, except for very special Martin> cases). In fact, Martin is generally the guy answering the CVS and Subversion questions. -- Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/ "XML sucks, dictionaries rock" - Dave Beazley From tjreedy at udel.edu Sat Apr 4 22:48:18 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 04 Apr 2009 22:48:18 -0400 Subject: Why doesn't StopIteration get caught in the following code? In-Reply-To: References: Message-ID: grocery_stocker wrote: ... >>>> while True: > ... i = gen.next() > ... print i > ... > 0 > 1 > 4 > Traceback (most recent call last): > File "", line 2, in ? > StopIteration If you had written for item in gen: print(i) then StopIteration from gen would be caught. One expansion of a for loop is (in the above case) it = iter(gen) # not needed here, but is for general iterables try: while True: i = it.next() print(i) # or whatever the loop body is except StopIteration: pass In other words, 'for i in iterable' expands to several lines of boilerplate code. It is very useful syntactic sugar. You left out the try..except part. From tjreedy at udel.edu Sat Apr 4 22:53:08 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 04 Apr 2009 22:53:08 -0400 Subject: Generators/iterators, Pythonicity, and primes In-Reply-To: References: Message-ID: John Posner wrote: > Inspired by recent threads (and recalling my first message to Python > edu-sig), I did some Internet searching on producing prime numbers using > Python generators. Most algorithms I found don't go for the infinite, > contenting themselves with "list all the primes below a given number". > > Here's a very Pythonic (IMHO) implementation that keeps going and going and > going ...: > > from itertools import count > from math import sqrt > > def prime_gen(): > """ > Generate all prime numbers > """ > primes = [] > for n in count(2): > if all(n%p for p in primes if p < sqrt(n)): > primes.append(n) > yield n > > The use of all() is particularly nifty (see > http://code.activestate.com/recipes/576640/). And so is the way in which the > list comprehension easily incorporates the sqrt(n) optimization. > > Question: Is there a way to implement this algorithm using generator > expressions only -- no "yield" statements allowed? No. You refer to the list being build in the code for building the list (very cute), which requires that the list be bound to a name at the start of the process rather than just when complete (which is never ;-). tjr From davea at ieee.org Sat Apr 4 23:09:50 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 04 Apr 2009 22:09:50 -0500 Subject: possible pairings in a set In-Reply-To: <49D80C0E.3010207@mrabarnett.plus.com> References: <905f6361-089e-47d4-b4e6-88877d2ef402@k2g2000yql.googlegroups.com> <49D80906.7000702@ieee.org> <49D80C0E.3010207@mrabarnett.plus.com> Message-ID: <49D820FE.4030508@ieee.org> MRAB wrote: > Dave Angel wrote: >> >> >> Ross wrote: >>> I'm new to python and I'm trying to come up with a function that takes >>> a given number of players in a game and returns all possible unique >>> pairings. Here's the code I've come up with so far, but I'm not >>> getting the output I'd like to: >>> >>> def all_pairings(players): >>> cleanlist = [] >>> for i in range(players): >>> cleanlist.append(i) >>> return cleanlist >>> start = 0 >>> follow = start +1 >>> finallist = [] >>> while follow <= len(cleanlist)-1: >>> for player in cleanlist: >>> mini = cleanlist[start],cleanlist[follow] >>> finallist.append(mini) >>> follow +=1 >>> start+=1 >>> return finallist >>> >>> If I were to execute the function with all_pairings(4), I want to get >>> the output [[0,1],[0,2],[0,3],[1,2],[1,3],[2,3]. Instead, I get >>> [0,1,2,3] with the code I currently have. Can you guys help me out? >>> Also, if my code is considered ugly or redundant by this community, >>> can you make suggestions to clean it up? >>> >>> >> First problem is the return you have in the middle of the function, >> which returns cleanlist. That's why you're getting [0, 1, 2, 3] >> >> Once you fix that, you'll find you have exceptions in the code where >> cleanlist[follow] doesn't work if follow is equal to 4. >> >> I'm afraid the code is ugly, and therefore hard to understand or >> fix. Let me suggest an approach, rather than just giving you the >> code. Why not create a nested for which generates two subscripts >> between 0 and 4, then append only those pairs in which the second is >> strictly greater than the first. It won't be the quickest, but it >> will be easy to follow. >> >> No while loop, no explicit incrementing of variables, and in fact no >> subscripting of the lists. Just let them supply their own values. >> cleanlist = range(players) # no need for any loop, >> it's already a list (except in Python 3.0, in which case it's a >> sequence that still works just as well) >> finallist = [] >> for i .....: >> for j... >> if i> append ... >> return finallist >> > Actually, cleanlist[i] == i, so it's pointless. You can do just: > > finallist = [] > for i in range(players): > for j in range(i + 1, players): > finallist.append((i, j)) > > or, using list comprehension: > > finallist = [(i, j) for i in range(players) for j in range(i + 1, > players)] > > This looked to me like a homework assignment, or a self-assignment for a relative beginner in Python. So I wanted to outline an approach, not provide the absolute optimum answer which might not be understood. I avoided mentioning the itertools.combinations() function because you can't learn much from a single function call. I avoided list comprehensions because they are very hard for many inexperienced python programmers. I left the cleanlist in because this approach can be readily generalized to taking a list which isn't just a list of numbers. Add an enumerate to each of the for loops, and you can readily produce all the combinations of whatever might be in the first list. I debated with myself using slicing of the list for the nested for (your range(i+1, players) is equivalent to a slice), and decided that it would be harder to read. . Clearly my nested loop iterates twice as much on the average as is needed, but the if is easier to read for a beginner. Even for experienced developers, sometimes optimizing too early can make it hard to visualize generalizing a function. If the function took a list rather than an upperlimit, it would be much more general, but how many could take your list comprehension and turn it into def all_pairings(cleanlist): return [ (a, b) for i, a in enumerate(cleanlist) for b in cleanlist[i+1:] ] print all_pairings(range(2,6)) print all_pairings( ("tom", "dick", "harry", "jim") ) From ericwoodworth at gmail.com Sat Apr 4 23:43:14 2009 From: ericwoodworth at gmail.com (ericwoodworth at gmail.com) Date: Sat, 4 Apr 2009 20:43:14 -0700 (PDT) Subject: Killing threads Message-ID: <4b52f7d7-81d5-4141-9385-ee8cfb90ac1b@l1g2000yqk.googlegroups.com> Hi, I'm new to python and even newer to threading and it seems as though I'm missing something fundamental about threads. Basically I have a program that looks like this: class ThreadOne(threading.Thread): while 1: do stuff class ThreadTwo(threading.Thread): while 1: do other stuff first = ThreadOne() second = ThreadTwo() while 1: do stuff The issue that I'm having is...I don't know how to kill this app in window. I hit ctrl-c but that only seems to kill one of the threads. The rest of the app just lingers. There's got to be a more graceful way but so far I haven't googled anything up. I'm using queues to talk between these threads so I could certainly put some kind of message on the queue that causes the threads to commit suicide but I'm thinking there's a more built in way to do what I want. I'm just not sure what it is. I'd appreciate it if somebody could point me in the right direction. Thanks. From ldo at geek-central.gen.new_zealand Sat Apr 4 23:49:20 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sun, 05 Apr 2009 15:49:20 +1200 Subject: Python Goes Mercurial References: <49d65b62$0$30904$9b622d9e@news.freenet.de> <49d80a4a$0$30643$9b622d9e@news.freenet.de> Message-ID: In message <49d80a4a$0$30643$9b622d9e at news.freenet.de>, "Martin v. L?wis" wrote: >In message , Lawrence D'Oliveiro wrote: > >> Post an example of what you were trying to do, with the exact messages, >> and we can walk you through it. > > Unfortunately, these are many months ago, and I don't recall the exact > error messages. I wasn't really asking for help, merely pointing out > that I dislike git because it makes me ask for help (something I did > not have to do for CVS or subversion, except for very special cases). Everybody needs help sometime. If you're accustomed to centralized version control (CVS and SVN), it probably takes some time to get used to the way distributed systems work. It's nothing to be ashamed of. From ldo at geek-central.gen.new_zealand Sat Apr 4 23:51:31 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sun, 05 Apr 2009 15:51:31 +1200 Subject: with open('com1', 'r') as f: References: <8bc55c05-19da-41c4-b916-48e0a4be49b9@p11g2000yqe.googlegroups.com> <91e09eaf-5a25-4a6b-b131-a5245970b337@f19g2000yqh.googlegroups.com> Message-ID: In message <91e09eaf-5a25-4a6b-b131- a5245970b337 at f19g2000yqh.googlegroups.com>, gert wrote: > On Apr 4, 12:58 am, Lawrence D'Oliveiro central.gen.new_zealand> wrote: > >> In message <8bc55c05-19da-41c4- >> b916-48e0a4be4... at p11g2000yqe.googlegroups.com>, gert wrote: >> >>> with open('com1', 'r') as f: >>> for line in f: >>> print('line') >> >> Why bother, why not just >> >> for line in open('com1', 'r') : >> print line > > So its does the same thing as with right ? Why do you need a with? > Automatic closing and finalizing stuff. All Python objects are reference-counted. Once the file object becomes inaccessible, it is automatically closed. Simple. Note: I wouldn't do this for files open for writing. I'd prefer to make sure those were properly flushed and closed, if only to catch any I/O errors. From steve at REMOVE-THIS-cybersource.com.au Sat Apr 4 23:58:48 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 05 Apr 2009 03:58:48 GMT Subject: possible pairings in a set References: <905f6361-089e-47d4-b4e6-88877d2ef402@k2g2000yql.googlegroups.com> Message-ID: <01e820b2$0$20654$c3e8da3@news.astraweb.com> On Sat, 04 Apr 2009 17:42:58 -0700, Ross wrote: > I'm new to python and I'm trying to come up with a function that takes a > given number of players in a game and returns all possible unique > pairings. Here's the code I've come up with so far, but I'm not getting > the output I'd like to: Others have already given a solution for Python 2.6, using itertools.combinations(). Here's a solution for Python 2.4 and 2.5. The thing to remember about combinations is that they can be defined recursively. To find the combinations of [1, 2, 3, 4] we can do something like this: Combinations of [1, 2, 3, 4]: Start with the first item, 1. Combinations are given by [1] + combinations of what's left: [2, 3, 4]. Combinations of [2, 3, 4]: Start with the first item, 2. Combinations are given by [2] + combinations of what's left: [3, 4]. Combinations of [3, 4]: Start with the first item, 3. Combinations are given by [3] + combinations of what's left: [4]. Combinations of [4]: Start with the first item, 4. Combinations are given by [4] + combinations of what's left: []. Combinations of []: There aren't any, so we're done. Using that algorithm gives us this function: def combinations(seq, r=None): """Generator returning combinations of items from sequence taken at a time. Order is not significant. If is not given, the entire sequence is returned. """ if r == None: r = len(seq) if r <= 0: yield [] else: for i in xrange(len(seq)): for cc in combinations(seq[i+1:], r-1): yield [seq[i]]+cc def all_pairings(players): return list(combinations(range(players), 2)) > If I were to execute the function with all_pairings(4), I want to get > the output [[0,1],[0,2],[0,3],[1,2],[1,3],[2,3]. >>> all_pairings(4) [[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]] -- Steven From michele.simionato at gmail.com Sun Apr 5 00:03:15 2009 From: michele.simionato at gmail.com (Michele Simionato) Date: Sat, 4 Apr 2009 21:03:15 -0700 (PDT) Subject: Python Goes Mercurial References: <49d65b62$0$30904$9b622d9e@news.freenet.de> <49d80a4a$0$30643$9b622d9e@news.freenet.de> Message-ID: <262497db-d2fd-4217-978c-fc5571f103d5@c11g2000yqj.googlegroups.com> On Apr 5, 5:49?am, Lawrence D'Oliveiro wrote: > Everybody needs help sometime. If you're accustomed to centralized version > control (CVS and SVN), it probably takes some time to get used to the way > distributed systems work. It's nothing to be ashamed of. If Martin - which is well above the average programmer - says that he would need help with Git, I take this as meaning that most people would get lost with Git. Heck, I am getting lost even with SVN! Michele Simionato P.S. the thing I do not understand if why we are moving away from Subversion. Will all the new features entered in 1.5 and 1.6 Subversion is now not so bad as it used to be and it has the advantage of being already there and familiar to most people. From aahz at pythoncraft.com Sun Apr 5 00:22:35 2009 From: aahz at pythoncraft.com (Aahz) Date: 4 Apr 2009 21:22:35 -0700 Subject: Killing threads References: <4b52f7d7-81d5-4141-9385-ee8cfb90ac1b@l1g2000yqk.googlegroups.com> Message-ID: In article <4b52f7d7-81d5-4141-9385-ee8cfb90ac1b at l1g2000yqk.googlegroups.com>, wrote: > >I'm using queues to talk between these threads so I could certainly >put some kind of message on the queue that causes the threads to >commit suicide but I'm thinking there's a more built in way to do what >I want. I'm just not sure what it is. There isn't, you have the right idea about using queues. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." --Brian W. Kernighan From semanticist at gmail.com Sun Apr 5 00:28:17 2009 From: semanticist at gmail.com (Miles) Date: Sun, 5 Apr 2009 00:28:17 -0400 Subject: Generators/iterators, Pythonicity, and primes In-Reply-To: References: Message-ID: On Sat, Apr 4, 2009 at 2:50 PM, John Posner wrote: > Inspired by recent threads (and recalling my first message to Python > edu-sig), I did some Internet searching on producing prime numbers using > Python generators. Most algorithms I found don't go for the infinite, > contenting themselves with "list all the primes below a given number". > > Here's a very Pythonic (IMHO) implementation that keeps going and going and > going ...: > > from itertools import count > from math import sqrt > > def prime_gen(): > """ > Generate all prime numbers > """ > primes = [] > for n in count(2): > if all(n%p for p in primes if p < sqrt(n)): > primes.append(n) > yield n > > The use of all() is particularly nifty (see > http://code.activestate.com/recipes/576640/). And so is the way in which the > list comprehension easily incorporates the sqrt(n) optimization. > > Question: Is there a way to implement this algorithm using generator > expressions only -- no "yield" statements allowed? def prime_gen(): primes = [] return (primes.append(n) or n for n in count(2) if all(n%p for p in primes if p<=sqrt(n))) That version is only marginally faster than your original version. The biggest performance penalty is that the (... for p in primes ...) generator isn't aborted once p > sqrt(n). Here's a less nifty but much more efficient version: def prime_gen(): prime_list = [2] for p in prime_list: yield p for n in itertools.count(prime_list[-1] + 1): for p in prime_list: if p * p > n: prime_list.append(n) yield n break elif n % p == 0: break else: raise Exception("Shouldn't have run out of primes!") When generating the first 1000 primes, this version's approximately 20 times faster; for the first 10,000 primes, ~80x (but still much slower than a simple Sieve of Eratosthenes). To make repeated calls faster, move prime_list = [2] outside the function. -Miles P.S. Gmail shows all your messages with a blank body and a text attachment containing your message; this is perhaps because your mailer includes an invalid blank Content-disposition header. From ericwoodworth at gmail.com Sun Apr 5 00:45:23 2009 From: ericwoodworth at gmail.com (ericwoodworth at gmail.com) Date: Sat, 4 Apr 2009 21:45:23 -0700 (PDT) Subject: Killing threads References: <4b52f7d7-81d5-4141-9385-ee8cfb90ac1b@l1g2000yqk.googlegroups.com> Message-ID: <5670fce5-c106-44bf-9152-b196298aa495@c36g2000yqn.googlegroups.com> On Apr 5, 12:22?am, a... at pythoncraft.com (Aahz) wrote: > In article <4b52f7d7-81d5-4141-9385-ee8cfb90a... at l1g2000yqk.googlegroups.com>, > > ? wrote: > > >I'm using queues to talk between these threads so I could certainly > >put some kind of message on the queue that causes the threads to > >commit suicide but I'm thinking there's a more built in way to do what > >I want. ?I'm just not sure what it is. > > There isn't, you have the right idea about using queues. > -- > Aahz (a... at pythoncraft.com) ? ? ? ? ? <*> ? ? ? ?http://www.pythoncraft.com/ > > "Debugging is twice as hard as writing the code in the first place. > Therefore, if you write the code as cleverly as possible, you are, by > definition, not smart enough to debug it." ?--Brian W. Kernighan Ok good to know. I was letting the search for a really cool solution stop me from rolling out what I think I already know how to do. Thanks. From kay.schluehr at gmx.net Sun Apr 5 01:43:00 2009 From: kay.schluehr at gmx.net (Kay Schluehr) Date: Sat, 4 Apr 2009 22:43:00 -0700 (PDT) Subject: Generators/iterators, Pythonicity, and primes References: Message-ID: > Question: Is there a way to implement this algorithm using generator > expressions only -- no "yield" statements allowed? Yes. Avoiding the yield statement is easy but one might eventually end up with two statements because one has to produce a side effect on the primes list. However we can use default parameters in lambdas and finally get a single expression which is a generator expression: g = (lambda primes = []: (n for n in count(2) \ if (lambda n, primes: (n in primes if primes and n<=primes [-1] \ else (primes.append(n) or True \ if all(n%p for p in primes if p <= sqrt(n)) \ else False)))(n, primes)))() assert g.next() == 2 assert g.next() == 3 assert g.next() == 5 assert g.next() == 7 From mail at timgolden.me.uk Sun Apr 5 02:11:57 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Sun, 05 Apr 2009 07:11:57 +0100 Subject: Creating a session in windows to auth to remote machines In-Reply-To: <5662d84f-faa9-4792-94e9-9ab3add4e604@c9g2000yqm.googlegroups.com> References: <5662d84f-faa9-4792-94e9-9ab3add4e604@c9g2000yqm.googlegroups.com> Message-ID: <49D84BAD.6050109@timgolden.me.uk> ericwoodworth at gmail.com wrote: > Hi, > I'm trying to auth to remote machines so I can plunder WMI to get > logs and settings and the like. My script works for most of my > machines because they're all in the same domain and I run the script > as somebody who has enough access to get at this stuff but for > machines off the domain I'm stuck. Unless I'm missing something here, you can already specify username etc. with WMI. Just Dispatch on "WbemScripting.SWbemLocator" and call .ConnectServer. Just in case you haven't already, I (naturally :) ) recommend my wmi module [1] which wraps a fair bit of the plumbing for you, including this. Eg, import wmi c = wmi.WMI ("some-machine", user="tim", password="password") for log in c.Win32_NTLogEvent ( Logfile="Application", Type="error" ): print log.RecordNumber, log.SourceName, log.Message [1] http://timgolden.me.uk/python/wmi.html TJG From ganeshborse at gmail.com Sun Apr 5 02:20:34 2009 From: ganeshborse at gmail.com (grbgooglefan) Date: Sat, 4 Apr 2009 23:20:34 -0700 (PDT) Subject: How to free /destroy object created by PyTuple_New References: <3c15a8d6-3095-4fff-b902-8bef22542b86@a5g2000pre.googlegroups.com> <0aa96d3a-f646-4f98-b8c9-934985411917@v6g2000vbb.googlegroups.com> Message-ID: <0f67973b-80dd-47c7-926b-989dec9de358@f1g2000prb.googlegroups.com> On Apr 4, 10:27?pm, Andrew Svetlov wrote: > To destroy every python object you need to call Py_DECREF. > To call python code fron you C thread you need to use pair > PyGILState_Ensure/PyGILState_Release. In my case, my C application has multiple threads & they are accessing a single Python Interpreter which was initialized by 1st main thread. In that case also, do I need to use PyGILState_Ensure/ PyGILState_Release APIs? I am not using Python interpreter level threads. Can I have a Python interpreter per thread so that I won't have issues of some data inside Python interpreters being gettint accidently overwritten/ freed by other thread? Will Py_DECREF release the items set in that tuple using call to PyTuple_SetItem? Or do I need to separately call Py_DECREF for them also? What is correct sequence for calling Py_DECREF & Py_INCREF? If I dont call Py_DECREF, will that object (PyTuple) not get freeed at all, causing memory leak? Please guide. From steve at REMOVE-THIS-cybersource.com.au Sun Apr 5 02:24:27 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 05 Apr 2009 06:24:27 GMT Subject: with open('com1', 'r') as f: References: <8bc55c05-19da-41c4-b916-48e0a4be49b9@p11g2000yqe.googlegroups.com> <91e09eaf-5a25-4a6b-b131-a5245970b337@f19g2000yqh.googlegroups.com> Message-ID: <01e842d6$0$20654$c3e8da3@news.astraweb.com> On Sun, 05 Apr 2009 15:51:31 +1200, Lawrence D'Oliveiro wrote: > All Python objects are reference-counted. Once the file object becomes > inaccessible, it is automatically closed. Simple. If only it were so simple. Firstly, what you describe is an implementation detail of CPython, not Python the language. Jython does not close files as soon as they become inaccessible, and IronPython and CLPython may not. Secondly, even in CPython things may not be so simple. Not all file-like objects are built-in file objects. >>> class MyFile(file): ... pass ... >>> f = MyFile("test", "r") >>> n = f.fileno() >>> os.read(n, 1) 'h' >>> f.close() >>> os.read(n, 1) Traceback (most recent call last): File "", line 1, in OSError: [Errno 9] Bad file descriptor My file-like object works just like the built-in file object, but now I can do this: >>> f = MyFile("test", "r") >>> f.attr = f # make a reference cycle >>> n = f.fileno() >>> del f >>> os.read(n, 5) 'hello' And lo and behold, the file is *not* automatically closed when f becomes inaccessible. I don't believe the garbage collector will free that file descriptor, possibly not even when Python exists. But watch this: >>> os.close(n) # just to be sure >>> >>> from __future__ import with_statement >>> with MyFile("test", "r") as f: ... n = f.fileno() ... f.attr = f # make a reference cycle ... >>> os.read(n, 5) Traceback (most recent call last): File "", line 1, in OSError: [Errno 9] Bad file descriptor The with statement guarantees[1] closing, even if there are other references to the file object elsewhere. [1] Guarantee void if the file system can't actually close the file for some reason. -- Steven From asmodai at in-nomine.org Sun Apr 5 02:25:06 2009 From: asmodai at in-nomine.org (Jeroen Ruigrok van der Werven) Date: Sun, 5 Apr 2009 08:25:06 +0200 Subject: "Pythoner",Wish me luck! In-Reply-To: <19963172-7ac1-4796-abb7-3387a65ba91d@f19g2000yqo.googlegroups.com> References: <54afb932-1a42-42d2-a3b9-0151b2bd410b@q30g2000prq.googlegroups.com> <19963172-7ac1-4796-abb7-3387a65ba91d@f19g2000yqo.googlegroups.com> Message-ID: <20090405062506.GI13110@nexus.in-nomine.org> -On [20090403 20:35], barisa (bbaja42 at gmail.com) wrote: >my question is : what benefit is using interactive intrepreter ? Install ipython. It's an extension wrapper around the interactive shell and allows a lot of very nice features in additional to the standard shell, such as tab completion. The benefit is easily testing code snippets, inspecting the contents of various variables, modules, classes, and so on. -- Jeroen Ruigrok van der Werven / asmodai ????? ?????? ??? ?? ?????? http://www.in-nomine.org/ | http://www.rangaku.org/ | GPG: 2EAC625B I must be cruel, only to be kind... From asmodai at in-nomine.org Sun Apr 5 02:48:05 2009 From: asmodai at in-nomine.org (Jeroen Ruigrok van der Werven) Date: Sun, 5 Apr 2009 08:48:05 +0200 Subject: Python Goes Mercurial In-Reply-To: <262497db-d2fd-4217-978c-fc5571f103d5@c11g2000yqj.googlegroups.com> References: <49d65b62$0$30904$9b622d9e@news.freenet.de> <49d80a4a$0$30643$9b622d9e@news.freenet.de> <262497db-d2fd-4217-978c-fc5571f103d5@c11g2000yqj.googlegroups.com> Message-ID: <20090405064805.GK13110@nexus.in-nomine.org> -On [20090405 06:05], Michele Simionato (michele.simionato at gmail.com) wrote: >P.S. the thing I do not understand if why we are moving >away from Subversion. Will all the new features entered >in 1.5 and 1.6 Subversion is now not so bad as it used to >be and it has the advantage of being already there and >familiar to most people. I don't fully get the whole 'we MUST go DVCS!' meme going around the Internet either. There's situations where a centralised system like SVN works and there's situations where a DVCS works better. Unfortunately there's a whole group of rabid, zealous proponents of one system or ther other out there that are quick to denounce any of your rationale for going with one or the other if it is not their $FAVOURITE_ONE. -- Jeroen Ruigrok van der Werven / asmodai ????? ?????? ??? ?? ?????? http://www.in-nomine.org/ | http://www.rangaku.org/ | GPG: 2EAC625B Love conquers all... From tjreedy at udel.edu Sun Apr 5 02:48:20 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 05 Apr 2009 02:48:20 -0400 Subject: with open('com1', 'r') as f: In-Reply-To: References: <8bc55c05-19da-41c4-b916-48e0a4be49b9@p11g2000yqe.googlegroups.com> <91e09eaf-5a25-4a6b-b131-a5245970b337@f19g2000yqh.googlegroups.com> Message-ID: Lawrence D'Oliveiro wrote: > In message <91e09eaf-5a25-4a6b-b131- > a5245970b337 at f19g2000yqh.googlegroups.com>, gert wrote: > >> On Apr 4, 12:58 am, Lawrence D'Oliveiro > central.gen.new_zealand> wrote: >> >>> In message <8bc55c05-19da-41c4- >>> b916-48e0a4be4... at p11g2000yqe.googlegroups.com>, gert wrote: >>> >>>> with open('com1', 'r') as f: >>>> for line in f: >>>> print('line') >>> Why bother, why not just >>> >>> for line in open('com1', 'r') : >>> print line >> So its does the same thing as with right ? > > Why do you need a with? > >> Automatic closing and finalizing stuff. > > All Python objects are reference-counted. Nope. Only in CPython, and even that could change. > Once the file object becomes > inaccessible, it is automatically closed. Simple. Even in CPython, that would not be true now is the object became involved in or became a dependent of a reference cycle. > Note: I wouldn't do this for files open for writing. I'd prefer to make sure > those were properly flushed and closed, if only to catch any I/O errors. tjr From ldo at geek-central.gen.new_zealand Sun Apr 5 02:50:53 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sun, 05 Apr 2009 18:50:53 +1200 Subject: Python Goes Mercurial References: <49d65b62$0$30904$9b622d9e@news.freenet.de> <49d80a4a$0$30643$9b622d9e@news.freenet.de> <262497db-d2fd-4217-978c-fc5571f103d5@c11g2000yqj.googlegroups.com> Message-ID: In message <262497db-d2fd-4217-978c- fc5571f103d5 at c11g2000yqj.googlegroups.com>, Michele Simionato wrote: > If Martin - which is well above the average programmer - > says that he would need help with Git, I take this as > meaning that most people would get lost with Git. I don't feel lost with Git. Does that make me an above-average programmer? > P.S. the thing I do not understand if why we are moving > away from Subversion. Because it's still centralized. That means different developers cannot pursue parallel branches on their own, those branches must be represented on the server. From ldo at geek-central.gen.new_zealand Sun Apr 5 02:52:58 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sun, 05 Apr 2009 18:52:58 +1200 Subject: with open('com1', 'r') as f: References: <8bc55c05-19da-41c4-b916-48e0a4be49b9@p11g2000yqe.googlegroups.com> <91e09eaf-5a25-4a6b-b131-a5245970b337@f19g2000yqh.googlegroups.com> <01e842d6$0$20654$c3e8da3@news.astraweb.com> Message-ID: In message <01e842d6$0$20654$c3e8da3 at news.astraweb.com>, Steven D'Aprano wrote: > Firstly, what you describe is an implementation detail of CPython, not > Python the language. Jython does not close files as soon as they become > inaccessible, and IronPython and CLPython may not. That's a limitation of Java-like virtual machines, which I have no intention of condonig. > Secondly, even in CPython things may not be so simple. Not all file-like > objects are built-in file objects. Why is this relevant? From goodz158 at yahoo.com Sun Apr 5 03:17:33 2009 From: goodz158 at yahoo.com (Good Z) Date: Sun, 5 Apr 2009 00:17:33 -0700 (PDT) Subject: Python2.4 and HTTPS Message-ID: <715817.45271.qm@web35908.mail.mud.yahoo.com> Dear all, I am using Python 2.4.3 for my project. We need to use HTTPS with python2.4.3 unfortunately it seems httplib is not working fine for me. Below is small code that works well with Python2.6.1 but not with Python2.4.3. Unfortunately its not possible for me to move away from Python 2.4.3. Would request if anyone has any idea how to make this programs work in Python2.4.3. import httplib conn1 = httplib.HTTPSConnection(WEBSITE_ADDRESS) conn1.putrequest('GET', RELATIVE_ADDR) conn1.putheader('Connection', 'close') conn1.endheaders() r1 = conn1.getresponse() data1 = r1.read() print data1 conn1.close() One executing the above program, it crash while reading the response and gives following error: Traceback (most recent call last): File "", line 1, in r1 = conn1.getresponse() File "C:\Python25\lib\httplib.py", line 928, in getresponse response.begin() File "C:\Python25\lib\httplib.py", line 385, in begin version, status, reason = self._read_status() File "C:\Python25\lib\httplib.py", line 349, in _read_status raise BadStatusLine(line) BadStatusLine Any help would be appreciated. Best Regards, Mike. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ldo at geek-central.gen.new_zealand Sun Apr 5 03:30:33 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sun, 05 Apr 2009 19:30:33 +1200 Subject: Python Goes Mercurial References: <49d65b62$0$30904$9b622d9e@news.freenet.de> <49d80a4a$0$30643$9b622d9e@news.freenet.de> <262497db-d2fd-4217-978c-fc5571f103d5@c11g2000yqj.googlegroups.com> Message-ID: In message , Jeroen Ruigrok van der Werven wrote: > There's situations where a centralised system like SVN works and there's > situations where a DVCS works better. Most of the people using Subversion seem to be corporates with a centralized, top-down-controlled policy for development. Conway's Law in effect, I guess. > Unfortunately there's a whole group of rabid, zealous proponents... Tell you what, keep the ad-hominem bullshit out of this discussion, and I agree to do the same. Deal? From hniksic at xemacs.org Sun Apr 5 03:47:06 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Sun, 05 Apr 2009 09:47:06 +0200 Subject: How to free /destroy object created by PyTuple_New References: <3c15a8d6-3095-4fff-b902-8bef22542b86@a5g2000pre.googlegroups.com> <0aa96d3a-f646-4f98-b8c9-934985411917@v6g2000vbb.googlegroups.com> <0f67973b-80dd-47c7-926b-989dec9de358@f1g2000prb.googlegroups.com> Message-ID: <871vs7leyt.fsf@busola.homelinux.net> [ You can also ask questions like this on the specialized capi-sig list; see http://mail.python.org/mailman/listinfo/capi-sig ] grbgooglefan writes: > In my case, my C application has multiple threads & they are accessing > a single Python Interpreter which was initialized by 1st main thread. > In that case also, do I need to use PyGILState_Ensure/ > PyGILState_Release APIs? Yes. There is a single GIL that needs to be held even if each thread has a different interpreter. > Can I have a Python interpreter per thread so that I won't have > issues of some data inside Python interpreters being gettint > accidently overwritten/ freed by other thread? You can have a separate interpreter per thread, but they will still need to be synchronized using the GIL because even separate interpreters share some global data, such as C-level global variables. > Will Py_DECREF release the items set in that tuple using call to > PyTuple_SetItem? Or do I need to separately call Py_DECREF for them > also? A single Py_DECREF on the tuple will call Py_DECREF on all the contained objects. This principle applies to all containers. > What is correct sequence for calling Py_DECREF & Py_INCREF? If you receive a "new" reference, you don't need to initially incref it. You need to call DECREF when you're done with the object, i.e. when it stops being accessible for you (for example because you replaced it with another object or NULL in your C-level data structure). > If I dont call Py_DECREF, will that object (PyTuple) not get freeed > at all, causing memory leak? Yes, and additionally it causes objects inside it to leak as well. From michele.simionato at gmail.com Sun Apr 5 03:47:44 2009 From: michele.simionato at gmail.com (Michele Simionato) Date: Sun, 5 Apr 2009 00:47:44 -0700 (PDT) Subject: Python Goes Mercurial References: <49d65b62$0$30904$9b622d9e@news.freenet.de> <49d80a4a$0$30643$9b622d9e@news.freenet.de> <262497db-d2fd-4217-978c-fc5571f103d5@c11g2000yqj.googlegroups.com> Message-ID: <5f1038cd-b2c7-420d-bf6e-5872d556fb22@u8g2000yqn.googlegroups.com> On Apr 5, 8:50?am, Lawrence D'Oliveiro wrote: > Michele wrote: > > the thing I do not understand if why we are moving > > away from Subversion. > > Because it's still centralized. That means different developers cannot pursue > parallel branches on their own, those branches must be represented on the > server. Yes, but Python managed to work this way for nearly two decades and I am not sure the change is really required. Notice that I did use a DVCS in the past (Darcs) and in principle I like DVCS better, but in practice there are too many of them nowadays, and I would wait for a few years to see if the situation stabilizes before changing the old route. But this point is moot, of course, since the BDFL has already spoken. I take the core developers are happy with the choice. The relevance for me is that at work we use Subversion, but now that Python uses mercurial I am sure there will be coworkers wanting to switch to Mercurial and I would like to understand what are the tradeoffs involved. From robert at nowhere.invalid Sun Apr 5 04:04:28 2009 From: robert at nowhere.invalid (robert) Date: Sun, 05 Apr 2009 10:04:28 +0200 Subject: Accessing mail box tree of Thunderbird/Seamonkey? Message-ID: Is there a API/possibilty for reading&writing (live) in the mail box tree of Thunderbird/Seamonkey with Python? From clp2 at rebertia.com Sun Apr 5 04:16:28 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sun, 5 Apr 2009 01:16:28 -0700 Subject: Accessing mail box tree of Thunderbird/Seamonkey? In-Reply-To: References: Message-ID: <50697b2c0904050116h44f92b15s110b7dde338882c8@mail.gmail.com> On Sun, Apr 5, 2009 at 1:04 AM, robert wrote: > Is there a API/possibilty for reading&writing (live) in the mail box tree of > Thunderbird/Seamonkey with Python? >From what I can google, they're already in mbox format, so you can use mailbox.mbox to read/write to them. See http://docs.python.org/library/mailbox.html#mailbox.mbox Cheers, Chris -- I have a blog: http://blog.rebertia.com From cs at zip.com.au Sun Apr 5 04:21:44 2009 From: cs at zip.com.au (Cameron Simpson) Date: Sun, 5 Apr 2009 18:21:44 +1000 Subject: Accessing mail box tree of Thunderbird/Seamonkey? In-Reply-To: <50697b2c0904050116h44f92b15s110b7dde338882c8@mail.gmail.com> Message-ID: <20090405082144.GA26172@cskk.homeip.net> On 05Apr2009 01:16, Chris Rebert wrote: | On Sun, Apr 5, 2009 at 1:04 AM, robert wrote: | > Is there a API/possibilty for reading&writing (live) in the mail box tree of | > Thunderbird/Seamonkey with Python? | | >From what I can google, they're already in mbox format, so you can use | mailbox.mbox to read/write to them. | See http://docs.python.org/library/mailbox.html#mailbox.mbox But you should probably _not_ have thunderbird open when you modify them; it expects exclusive access. Also, if you modify a file you should probably remove the corresponding .sdb file, which is a cache of Thunderbird's knowledge of the mbox contents (header values and probably message offsets within the file). -- Cameron Simpson DoD#743 http://www.cskk.ezoshosting.com/cs/ ..And in all of Babylonia there was wailing and gnashing of teeth, till the prophets bade the multitudes get a grip on themselves and shape up. - Woody Allen From asmodai at in-nomine.org Sun Apr 5 04:21:55 2009 From: asmodai at in-nomine.org (Jeroen Ruigrok van der Werven) Date: Sun, 5 Apr 2009 10:21:55 +0200 Subject: Python Goes Mercurial In-Reply-To: References: <49d65b62$0$30904$9b622d9e@news.freenet.de> <49d80a4a$0$30643$9b622d9e@news.freenet.de> <262497db-d2fd-4217-978c-fc5571f103d5@c11g2000yqj.googlegroups.com> Message-ID: <20090405082155.GL13110@nexus.in-nomine.org> -On [20090405 09:35], Lawrence D'Oliveiro (ldo at geek-central.gen.new_zealand) wrote: >Most of the people using Subversion seem to be corporates with a >centralized, top-down-controlled policy for development. Not just corporations. An open source or other type of organisation might just as well have requirements that mandate a different kind of policy. >Tell you what, keep the ad-hominem bullshit out of this discussion, and I >agree to do the same. Deal? I guess I touched a nerve, so you must consider yourself one of those then. Just for the record: I meant that there's always a group who actively push one solution over all others without regard to what the current situation demands. If you cannot step back and look at the whole without trying to push your own things, there's nary a word other than rabid and zealous for that type of behaviour. -- Jeroen Ruigrok van der Werven / asmodai ????? ?????? ??? ?? ?????? http://www.in-nomine.org/ | http://www.rangaku.org/ | GPG: 2EAC625B How are the mighty fallen... From cournape at gmail.com Sun Apr 5 04:45:25 2009 From: cournape at gmail.com (David Cournapeau) Date: Sun, 5 Apr 2009 17:45:25 +0900 Subject: Python Goes Mercurial In-Reply-To: <20090405064805.GK13110@nexus.in-nomine.org> References: <49d65b62$0$30904$9b622d9e@news.freenet.de> <49d80a4a$0$30643$9b622d9e@news.freenet.de> <262497db-d2fd-4217-978c-fc5571f103d5@c11g2000yqj.googlegroups.com> <20090405064805.GK13110@nexus.in-nomine.org> Message-ID: <5b8d13220904050145m67d9821s1314c438cac512f7@mail.gmail.com> On Sun, Apr 5, 2009 at 3:48 PM, Jeroen Ruigrok van der Werven wrote: > -On [20090405 06:05], Michele Simionato (michele.simionato at gmail.com) wrote: >>P.S. the thing I do not understand if why we are moving >>away from Subversion. Will all the new features entered >>in 1.5 and 1.6 Subversion is now not so bad as it used to >>be and it has the advantage of being already there and >>familiar to most people. > > I don't fully get the whole 'we MUST go DVCS!' meme going around the > Internet either. I think it is often poorly 'marketed', and not well explained. But as someone whose first source control system which made sense was a DVCS, I can assure you that svn feels clumsy, slow, inadequate and extremely primitive. Most people justification for svn is based on the fact they are used to the workflow. Using DVCS to mimic the svn workflow is an error IMHO. What would have happened if people tried to mimic the visual sourcesafe workflow, with locks and co, with svn ? To me, defending svn against DVCS is like defending the lock-per-file approach of visual sourcesafe and the likes. Believe or not, there are some people who think this lock-based workflow is better. A simplistic analogy: svn it to CVS what C++ is to C, but try selling C++ to python developers :) David From ben+python at benfinney.id.au Sun Apr 5 05:11:22 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sun, 05 Apr 2009 19:11:22 +1000 Subject: Python Goes Mercurial References: <49d65b62$0$30904$9b622d9e@news.freenet.de> <49d80a4a$0$30643$9b622d9e@news.freenet.de> <262497db-d2fd-4217-978c-fc5571f103d5@c11g2000yqj.googlegroups.com> Message-ID: <87ab6vbh39.fsf@benfinney.id.au> Jeroen Ruigrok van der Werven writes: > -On [20090405 09:35], Lawrence D'Oliveiro (ldo at geek-central.gen.new_zealand) wrote: > >Tell you what, keep the ad-hominem bullshit out of this discussion, > >and I agree to do the same. Deal? > > I guess I touched a nerve, so you must consider yourself one of > those then. Alternative explanation: You made bullshit ad-hominem attacks instead of addressing the arguments. Please cut it out. > Just for the record: I meant that there's always a group who > actively push one solution over all others without regard to what > the current situation demands. If you cannot step back and look at > the whole without trying to push your own things, there's nary a > word other than rabid and zealous for that type of behaviour. If you can look at the process that was followed in this decision and see ?push one solution over all others without regard to what the current situation demands? you're not looking very hard. -- \ ?I am amazed, O Wall, that you have not collapsed and fallen, | `\ since you must bear the tedious stupidities of so many | _o__) scrawlers.? ?anonymous graffiti, Pompeii, 79 CE | Ben Finney From asmodai at in-nomine.org Sun Apr 5 05:27:08 2009 From: asmodai at in-nomine.org (Jeroen Ruigrok van der Werven) Date: Sun, 5 Apr 2009 11:27:08 +0200 Subject: Python Goes Mercurial In-Reply-To: <87ab6vbh39.fsf@benfinney.id.au> References: <49d65b62$0$30904$9b622d9e@news.freenet.de> <49d80a4a$0$30643$9b622d9e@news.freenet.de> <262497db-d2fd-4217-978c-fc5571f103d5@c11g2000yqj.googlegroups.com> <87ab6vbh39.fsf@benfinney.id.au> Message-ID: <20090405092708.GN13110@nexus.in-nomine.org> -On [20090405 11:15], Ben Finney (ben+python at benfinney.id.au) wrote: >If you can look at the process that was followed in this decision and >see ?push one solution over all others without regard to what the >current situation demands? you're not looking very hard. I did not argue the decision for the Python project to use a DVCS at all, which was evident from my initial email. I merely stated that I cannot understand why there's a meme and/or group of people on the 'net that vehemently declare that everything less than a DVCS sucks. -- Jeroen Ruigrok van der Werven / asmodai ????? ?????? ??? ?? ?????? http://www.in-nomine.org/ | http://www.rangaku.org/ | GPG: 2EAC625B Wisdom is the difference between knowledge and experience... From jj at objectsroot.com Sun Apr 5 05:51:17 2009 From: jj at objectsroot.com (Giovanni Giorgi) Date: Sun, 5 Apr 2009 11:51:17 +0200 Subject: Spring-like IoC in python? Message-ID: <9dfdfb090904050251u57322ff1r983041ed8a50f9db@mail.gmail.com> Hi all, I have just read the Thread "Thoughts on language-level configuration support?" started by jfager. I have worked in the past days with Java Spring. I found very valuable the ideas behind Inversion of Control (IoC). I think it should be a very nice feature to get into python too. In detail, I think the most important concept is the ability to auto-wire dependencies declared in the configuration file. It is the "added value" of such idea in my own opinion. Spring is heavily based on type declaration (in form of concrete class of interfaces) so this approach is not the preferred path in a dynamically language like Python. Do someone have tried or seen something like IoC container based on Python? -- Giovanni Giorgi Team Leader & Software Architect http://gioorgi.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From ldo at geek-central.gen.new_zealand Sun Apr 5 06:26:32 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sun, 05 Apr 2009 22:26:32 +1200 Subject: Python Goes Mercurial References: <49d65b62$0$30904$9b622d9e@news.freenet.de> <49d80a4a$0$30643$9b622d9e@news.freenet.de> <262497db-d2fd-4217-978c-fc5571f103d5@c11g2000yqj.googlegroups.com> <5f1038cd-b2c7-420d-bf6e-5872d556fb22@u8g2000yqn.googlegroups.com> Message-ID: In message <5f1038cd-b2c7-420d- bf6e-5872d556fb22 at u8g2000yqn.googlegroups.com>, Michele Simionato wrote: > The relevance for me is that at work we use Subversion, > but now that Python uses mercurial I am sure there will > be coworkers wanting to switch to Mercurial and I would > like to understand what are the tradeoffs involved. Eric Raymond has a good survey of what the important issues that they address and the differences between them . I would say that, basically, it's all about increasingly sophisticated ways of handling merging. From bockman at virgilio.it Sun Apr 5 06:54:45 2009 From: bockman at virgilio.it (Francesco Bochicchio) Date: Sun, 05 Apr 2009 12:54:45 +0200 Subject: Killing threads References: <4b52f7d7-81d5-4141-9385-ee8cfb90ac1b@l1g2000yqk.googlegroups.com> <5670fce5-c106-44bf-9152-b196298aa495@c36g2000yqn.googlegroups.com> Message-ID: On Sat, 04 Apr 2009 22:45:23 -0700, ericwoodworth wrote: > On Apr 5, 12:22?am, a... at pythoncraft.com (Aahz) wrote: >> In article <4b52f7d7-81d5-4141-9385-ee8cfb90a... at l1g2000yqk.googlegroups.com>, >> >> ? wrote: >> >> >I'm using queues to talk between these threads so I could certainly >> >put some kind of message on the queue that causes the threads to >> >commit suicide but I'm thinking there's a more built in way to do what >> >I want. ?I'm just not sure what it is. >> >> There isn't, you have the right idea about using queues. >> -- > > Ok good to know. I was letting the search for a really cool solution > stop me from rolling out what I think I already know how to do. > Thanks. If yor threads are not set as 'deamons' using Thread.setDaemon method, then your main program at its termination should call Thread.join for each of the thread spawned, otherwise the whole process will not quit. Ciao ---- FB From steve at REMOVE-THIS-cybersource.com.au Sun Apr 5 07:24:00 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 05 Apr 2009 11:24:00 GMT Subject: with open('com1', 'r') as f: References: <8bc55c05-19da-41c4-b916-48e0a4be49b9@p11g2000yqe.googlegroups.com> <91e09eaf-5a25-4a6b-b131-a5245970b337@f19g2000yqh.googlegroups.com> <01e842d6$0$20654$c3e8da3@news.astraweb.com> Message-ID: <01e88908$0$20654$c3e8da3@news.astraweb.com> On Sun, 05 Apr 2009 18:52:58 +1200, Lawrence D'Oliveiro wrote: > In message <01e842d6$0$20654$c3e8da3 at news.astraweb.com>, Steven D'Aprano > wrote: > >> Firstly, what you describe is an implementation detail of CPython, not >> Python the language. Jython does not close files as soon as they become >> inaccessible, and IronPython and CLPython may not. > > That's a limitation of Java-like virtual machines, which I have no > intention of condonig. You don't have to condone it, but you do have to live with it. >> Secondly, even in CPython things may not be so simple. Not all >> file-like objects are built-in file objects. > > Why is this relevant? File-like objects might not automatically close themselves. -- Steven From steve at REMOVE-THIS-cybersource.com.au Sun Apr 5 07:24:44 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 05 Apr 2009 11:24:44 GMT Subject: Generators/iterators, Pythonicity, and primes References: Message-ID: <01e88935$0$20654$c3e8da3@news.astraweb.com> On Sun, 05 Apr 2009 00:28:17 -0400, Miles wrote: > def prime_gen(): > primes = [] > return (primes.append(n) or n for n in count(2) if all(n%p for p > in primes if p<=sqrt(n))) > > That version is only marginally faster than your original version. The > biggest performance penalty is that the (... for p in primes ...) > generator isn't aborted once p > sqrt(n). That's a big performance penalty in practice. For n=100, it means that ninety percent of the loop iterations are pointless; for n = 10,000 it means that 99% of the loops are pointless; asymptotically, it means that almost all the work done is wasted. http://en.wikipedia.org/wiki/Almost_all Consequently, retrieving the next prime becomes slower and slower as you iterate over the generator. By my tests, it is about 40 times slower to go from the 2000th prime to the 2001st prime (17389 and 17393) than it is to go from the sixth prime to the seventh (13 and 17). In other words, this is a crappy way to generate primes. It's a trick, not a good algorithm: slow and inefficient, as well as obfuscated. > Here's a less nifty but much more efficient version: > > def prime_gen(): > prime_list = [2] > for p in prime_list: yield p > for n in itertools.count(prime_list[-1] + 1): > for p in prime_list: > if p * p > n: > prime_list.append(n) > yield n > break > elif n % p == 0: > break > else: > raise Exception("Shouldn't have run out of primes!") That's pretty sweet, but we can make it even faster. We can speed things up by incrementing by two instead of one, to avoid pointlessly testing even numbers that we know must fail. We can also speed things up a tad by making the common test first, and by storing squares rather than recalculating them each time through the loop. def prime_gen2(): yield 2 n = 3 yield n prime_list = [(2, 4), (3, 9)] it = itertools.count(4) for n in it: n = it.next() for p,p2 in prime_list: if n % p == 0: break elif p2 > n: prime_list.append((n, n*n)) yield n break else: raise RuntimeError("Shouldn't have run out of primes!") Generating the first million primes is 10% faster using this compared to your improved version. -- Steven From Andy at home.com.au Sun Apr 5 07:26:27 2009 From: Andy at home.com.au (Andy at home.com.au) Date: Sun, 5 Apr 2009 21:26:27 +1000 Subject: How Do I think of import? Message-ID: Hi: Sorry for the detail of this question but I hope its useful to others as well as myself. I'm planning to do a Python wrapper for an existing GUI environment that is not one of the standard environments. So I have complete freedom. Ive done various smaller things in Python with some success. But this is large enough to need proper planning. The current environment is C but with a C++ structure. Widgets are developed from simpler widgets, PtBasic -> PtLabel-> PtButton etc. Each widget has its own C header that defines widget parameters, callback options various functions. Each header includes the header defining the next level down. I plan to extract parameters, bit masks callbacks etc using a modified h2py, so I have all I need at my fingertips. But how do I organise it? What structure do I impose on my import files? The options seem to be: a) copy the C headers structure! "from photon.PtButton import *" photon.PtButton imports Its own definitions and imports photon.PtLabel which imports PtBasic etc. This is a simple structure. However- all users will import multiple widgets and I see a lot of wasted effort by Python trying to import PtBasic and other modules multiple times. b) Create modules by their function! photon.defititions, photon.callbacks and photon.functions then maybe a breakdown under definitions for example. photon.definitions.PtButton c) Just give up trying to be efficient and assume that the user will want to access all definitons etc. Simply create a photon.all and let that be imported. 'from photon.all import *" No matter what path I consider, I see a very crowded namespace. Is this inevitable? Should I just get on with the wrapper and not worry ? Sizing.. Currently PtBasic has 90 definitions PtLabel 54 and PtButton 11. Thanks, Andy From tim.wintle at teamrubber.com Sun Apr 5 07:38:12 2009 From: tim.wintle at teamrubber.com (Tim Wintle) Date: Sun, 05 Apr 2009 12:38:12 +0100 Subject: python needs leaning stuff from other language In-Reply-To: References: <57c1eae3-d096-43c0-acf0-a474290e82db@x29g2000prf.googlegroups.com> <73kt5jFvitcdU1@mid.uni-berlin.de> <49D54209.5090306@hotmail.com> <01e56a2f$0$20654$c3e8da3@news.astraweb.com> <87y6uie9ej.fsf@benfinney.id.au> <01e624b7$0$20654$c3e8da3@news.astraweb.com> <1238794953.8087.1.camel@tim-laptop> <1238820500.1029.69.camel@tim-laptop> <1238864843.30065.21.camel@tim-laptop> Message-ID: <1238931492.30594.5.camel@tim-laptop> On Sat, 2009-04-04 at 15:36 -0500, Robert Kern wrote: > On 2009-04-04 12:07, Tim Wintle wrote: > >>> (I didn't expect such strong responses btw!) > >> You are proposing the removal of a general, orthogonal feature (and breaking > >> code in consequence!) just because of a new syntax for a single special case of > >> that feature. That is quite simply ridiculous. > > > > Ok, I may have come across a little strongly (was very tired) - I'm not > > _actually_ saying we should remove it, I'm just pointing out why > > adding .clear() to lists seems to be unnecessary and slightly messy. The > > suggested removal of assignments to slices is a theoretical statement. > > But can you see why your wording might lead the rest of us to believe otherwise? > :-) Yes I do - sorry, I tend to check mailing lists at the end of the day when I am quite tired. I think I could argue 'till the cows come home about why adding .clear feels messy to me, bu as it's not my decision in the slightest, I think I will end this thread here ;) Tim From skip at pobox.com Sun Apr 5 08:02:08 2009 From: skip at pobox.com (skip at pobox.com) Date: Sun, 5 Apr 2009 07:02:08 -0500 (CDT) Subject: replacement for new.instancemethod in Python3? Message-ID: <20090405120208.50CB5FB4293@montanaro.dyndns.org> Is there a replacement in Python3 for new.instancemethod? That is, given an arbitrary instance (not its class) how can I add a new appropriately defined function as a method to it? Thx, -- Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/ "XML sucks, dictionaries rock" - Dave Beazley From skip at pobox.com Sun Apr 5 08:07:26 2009 From: skip at pobox.com (skip at pobox.com) Date: Sun, 5 Apr 2009 07:07:26 -0500 (CDT) Subject: replacement for new.instancemethod in Python3? Message-ID: <20090405120726.0AC50FB42C8@montanaro.dyndns.org> > Is there a replacement in Python3 for new.instancemethod? I think I can answer my own question: functools.partial: >>> class C: ... pass ... >>> def meth(self, x): ... self.x = x ... >>> c = C() >>> c.meth = meth >>> c.meth(5) Traceback (most recent call last): File "", line 1, in TypeError: meth() takes exactly 2 positional arguments (1 given) >>> import functools >>> c.meth = functools.partial(meth, c) >>> c.meth(5) >>> c.x 5 Is this the "one obvious" way to do this in Python3? -- Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/ "XML sucks, dictionaries rock" - Dave Beazley From michele.simionato at gmail.com Sun Apr 5 08:13:07 2009 From: michele.simionato at gmail.com (Michele Simionato) Date: Sun, 5 Apr 2009 05:13:07 -0700 (PDT) Subject: replacement for new.instancemethod in Python3? References: Message-ID: On Apr 5, 2:02?pm, s... at pobox.com wrote: > Is there a replacement in Python3 for new.instancemethod? ?That is, given an > arbitrary instance (not its class) how can I add a new appropriately defined > function as a method to it? There is no need for new.instancemethod for new style classes: >>> class C: pass ... >>> c=C() >>> def f(self): pass ... >>> c.f = f.__get__(c, C) >>> c.f > From jura.grozni at gmail.com Sun Apr 5 08:16:19 2009 From: jura.grozni at gmail.com (azrael) Date: Sun, 5 Apr 2009 05:16:19 -0700 (PDT) Subject: Best way to pickle functions References: <29bfcf8a-984d-47df-a116-7a1f80f171ba@z9g2000yqi.googlegroups.com> <5fce0034-3598-4d8a-9a2d-9832e8bea5ba@j39g2000yqn.googlegroups.com> <09a3664d-6cc4-4131-97fd-325af72814d8@e18g2000yqo.googlegroups.com> Message-ID: <6c20582f-afa9-4872-abfe-7b43a297647a@r37g2000yqn.googlegroups.com> As in Python everythong is an object you could use __name__. >>> import cPickle >>> def def1(): ... pass >>> def def2(): ... pass >>> def1.__name__ def1 >>> def2.__name__ def2 in this case you can combine __name__ to get the object name and then combine it with eval to pickle. >>> pickleString = "stored = cPickle.dumps(" + def1.__name__ + ")" >>> eval(pickleString) >>> restored = cPickle.loads(stored) in this way you deal with dynamic strings and you can pickle anything From jura.grozni at gmail.com Sun Apr 5 08:31:15 2009 From: jura.grozni at gmail.com (azrael) Date: Sun, 5 Apr 2009 05:31:15 -0700 (PDT) Subject: Need advise about an application Message-ID: <6b74d269-6ef5-4483-a492-de397c2e156c@f19g2000yqh.googlegroups.com> I am currently working on an application and I need a advise. I am supposed to read data from a device connected to a serial port. I am reading data using pySerial. The devise is sending signals with a time between two signals of one second. The application is supposed to collect the data and put it into a sqlite DB. wxPython GUI. I am worried about this asspect that I have two solutions how to do it but as I never tried it I wanted to ask for advise which one should be easier. solution 1): The GUI is set into a separate thread then the part of collecting and saving data. Well one thread for getting signals and saving data and the other for GUI and data presentation solution 2): create an event which is binded to pySerial read methon. Well when the devise sends a signal the event function is triggered. Or maybe combine the two? From dstanek at dstanek.com Sun Apr 5 08:45:44 2009 From: dstanek at dstanek.com (David Stanek) Date: Sun, 5 Apr 2009 08:45:44 -0400 Subject: Spring-like IoC in python? In-Reply-To: <9dfdfb090904050251u57322ff1r983041ed8a50f9db@mail.gmail.com> References: <9dfdfb090904050251u57322ff1r983041ed8a50f9db@mail.gmail.com> Message-ID: On Sun, Apr 5, 2009 at 5:51 AM, Giovanni Giorgi wrote: > Hi all, I have just read the Thread "Thoughts on language-level > configuration support?" started by jfager. > I have worked in the past days with Java Spring. > I found very valuable the ideas behind Inversion of Control (IoC). > I think it should be a very nice feature to get into python too. > In detail, I think the most important concept is the ability to auto-wire > dependencies declared in the configuration file. > It is the "added value" of such idea in my own opinion. > Spring is heavily based on type declaration (in form of concrete class of > interfaces) so this approach is not the preferred path in a dynamically > language like Python. > Do someone have tried or seen something like IoC container based on Python? > There are several projects implementing IoC in Python. The three I am most familiar with are snake-guice[0], pinsor[1] and SpringPython[2]. I think they are all worth investigating. I started snake-guice as a clone of the google-guice project (a Java IoC framework.) I liked several things that I just didn't see anywhere else: 1. Language based configuration because XML sucks 2. In code hints about what will be injected 3. Not a container - the injector injects and gets out of the way The documentation is a little lacking, but that will be changing in the next few days. Examples of using snake-guice with CherryPy, Django and TurboGears are just a few days off as well. The API tests[3] show simple clear examples. The project is going to be used on several very large websites. So you can expect it to be maintained for quite a while. I would encourage you to look at each project and leave feedback where you can. It can only help each project. 0. http://code.google.com/p/snake-guice/ 1. http://code.google.com/p/pinsor/ 2. http://springpython.webfactional.com/ 3. http://code.google.com/p/snake-guice/source/browse/trunk/snakeguice/tests/test_api_25.py -- David blog: http://www.traceback.org twitter: http://twitter.com/dstanek From newptcai at gmail.com Sun Apr 5 08:48:41 2009 From: newptcai at gmail.com (=?GB2312?B?0rvK18qr?=) Date: Sun, 5 Apr 2009 05:48:41 -0700 (PDT) Subject: Need advise about an application References: <6b74d269-6ef5-4483-a492-de397c2e156c@f19g2000yqh.googlegroups.com> Message-ID: Do you have to show these data on GUI? If so, why not use the event pattern? As far as I know, it's the standard way wxPython works. BTW : If it's not complicated, I think maybe some wysiwyg solution is better. I use python for almost every thing, except these related to GUI. On Apr 5, 8:31?pm, azrael wrote: > I am currently working on an application and I need a advise. > > I am supposed to read data from a device connected to a serial port. I > am reading data using pySerial. The devise is sending signals with a > time between two signals of one second. > > The application is supposed ?to collect the data and put it into a > sqlite DB. wxPython GUI. > I am worried about this asspect that I have two solutions how to do it > but as I never tried it I wanted to ask for advise which one should be > easier. > > solution 1): > The GUI is set into a separate thread then the part of collecting and > saving data. Well one thread for getting signals and saving data and > the other for GUI and data presentation > > solution 2): > create an event which is binded to pySerial read methon. Well when the > devise sends a signal the event function is triggered. > > Or maybe combine the two? From imageguy1206 at gmail.com Sun Apr 5 09:05:57 2009 From: imageguy1206 at gmail.com (imageguy) Date: Sun, 5 Apr 2009 06:05:57 -0700 (PDT) Subject: Killing threads References: <4b52f7d7-81d5-4141-9385-ee8cfb90ac1b@l1g2000yqk.googlegroups.com> Message-ID: <53ebfff9-448f-438f-aa93-a2187bf13056@f1g2000prb.googlegroups.com> On Apr 4, 10:43?pm, ericwoodwo... at gmail.com wrote: > Hi, > ? ? ?I'm new to python and even newer to threading and it seems as > though I'm missing something fundamental about threads. ?Basically I > have a program that looks like this: > > class ThreadOne(threading.Thread): > ? ? ?while 1: > ? ? ? ? ? do stuff > > class ThreadTwo(threading.Thread): > ? ? ?while 1: > ? ? ? ? ? do other stuff > > first = ThreadOne() > second = ThreadTwo() > > while 1: > ? ? do stuff > > The issue that I'm having is...I don't know how to kill this app in > window. ?I hit ctrl-c but that only seems to kill one of the threads. > The rest of the app just lingers. ?There's got to be a more graceful > way but so far I haven't googled anything up. > > I'm using queues to talk between these threads so I could certainly > put some kind of message on the queue that causes the threads to > commit suicide but I'm thinking there's a more built in way to do what > I want. ?I'm just not sure what it is. > > I'd appreciate it if somebody could point me in the right direction. > Thanks. I am not an expert either, however, I think the standard practice is to use and threading.Event to communicate with the threads. So the structure goes something like this; In the main thread set a threading.Event called 'keepgoing'. Pass this object into the thread at instantiation or as a parameter in the .start method Over-ride the .run method to handle the process Change the while loop's condition to check the .keepgoing.isSet() In the primary thread, when you want the threads to stop, simply .clear () the keepgoing event The while loop terminates and at the end of the .run method, the thread will terminate. In the primary program, you should then .join() the worker threads. - doing this will block the program until all of the thread have stopped. So your example would become; class ThreadOne(threading.Thread): def __init__(self, keepgoing): threading.Thread.__init__(self) self.keepgoing = keepgoing def run(self) while self.keepgoing.isSet(): do stuff thread one class ThreadTwo(threading.Thread): def __init__(self, keepgoing): threading.Thread.__init__(self) self.keepgoing = keepgoing def run(self) while self.keepgoing.isSet(): do stuff in thread two # #now start the main process # keepgoing = threading.Event() keepgoing.set() first = ThreadOne(keepgoing) second = ThreadTwo(keepgoing) # you could also pass in the variable with a thread.start() method, which will then call the thread.run() while 1: do stuff if stop_condition: #keyboard interrupt ? keepgoing.clear() for worker in threading.enumerate(): if worker.isAlive(): worker.join() FULL DISCLAIMER: I am NOT a threading expert and have limited experience, so if this doesn't work, you are on your own ! From ericwoodworth at gmail.com Sun Apr 5 09:09:12 2009 From: ericwoodworth at gmail.com (ericwoodworth at gmail.com) Date: Sun, 5 Apr 2009 06:09:12 -0700 (PDT) Subject: Creating a session in windows to auth to remote machines References: <5662d84f-faa9-4792-94e9-9ab3add4e604@c9g2000yqm.googlegroups.com> Message-ID: <854caba3-f51d-4f1e-ae47-da08d125702c@v15g2000yqn.googlegroups.com> On Apr 5, 2:11?am, Tim Golden wrote: > ericwoodwo... at gmail.com wrote: > > Hi, > > ? ? ?I'm trying to auth to remote machines so I can plunder WMI to get > > logs and settings and the like. ?My script works for most of my > > machines because they're all in the same domain and I run the script > > as somebody who has enough access to get at this stuff but for > > machines off the domain I'm stuck. > > Unless I'm missing something here, you can already specify > username etc. with WMI. Just Dispatch on "WbemScripting.SWbemLocator" > and call .ConnectServer. Just in case you haven't already, I > (naturally :) ) recommend my wmi module [1] which wraps a fair bit > of the plumbing for you, including this. Eg, > > > import wmi > > c = wmi.WMI ("some-machine", user="tim", password="password") > > for log in c.Win32_NTLogEvent ( > ? Logfile="Application", > ? Type="error" > ): > ? print log.RecordNumber, log.SourceName, log.Message > > > > [1]http://timgolden.me.uk/python/wmi.html > > TJG That's very cool. I was using win32com.client but I'll check this out. Thanks! From andrew at acooke.org Sun Apr 5 09:24:00 2009 From: andrew at acooke.org (andrew cooke) Date: Sun, 5 Apr 2009 09:24:00 -0400 (CLT) Subject: Spring-like IoC in python? In-Reply-To: References: <9dfdfb090904050251u57322ff1r983041ed8a50f9db@mail.gmail.com> Message-ID: <2050aad1c1a94ec6c192a5d5fde5f4e6.squirrel@acooke.dyndns.org> David Stanek wrote: [...] > The documentation is a little lacking, but that will be changing in > the next few days. Examples of using snake-guice with CherryPy, Django > and TurboGears are just a few days off as well. The API tests[3] show > simple clear examples. [...] > 3. > http://code.google.com/p/snake-guice/source/browse/trunk/snakeguice/tests/test_api_25.py can i make a suggestion? add comments to those tests explaining what they do in fairly boring, introductory detail (ie "we are injecting a value in ... because ..."). also, explain "ch" (class hierarchy) or, if it's just a meaningless module name, use from ... import Person because i thought it was something important and now think it's noise and if its noise it wasted my time. i know "Person" is the kind of class used in examples, but i am unsure if "ch" is something special your system needs or not. i was going to argue that ioc isn't needed in python because the motivation for it in java - that you need a more concise configuration language - doesn't exist. what's the point of using xml to simulate a scripting language when you are already in a scripting language. but maybe i have missed something, given that this stuff exists (hence me trying to understand it). andrew From dstanek at dstanek.com Sun Apr 5 09:32:00 2009 From: dstanek at dstanek.com (David Stanek) Date: Sun, 5 Apr 2009 09:32:00 -0400 Subject: Spring-like IoC in python? In-Reply-To: <2050aad1c1a94ec6c192a5d5fde5f4e6.squirrel@acooke.dyndns.org> References: <9dfdfb090904050251u57322ff1r983041ed8a50f9db@mail.gmail.com> <2050aad1c1a94ec6c192a5d5fde5f4e6.squirrel@acooke.dyndns.org> Message-ID: On Sun, Apr 5, 2009 at 9:24 AM, andrew cooke wrote: > David Stanek wrote: > [...] >> The documentation is a little lacking, but that will be changing in >> the next few days. Examples of using snake-guice with CherryPy, Django >> and TurboGears are just a few days off as well. The API tests[3] show >> simple clear examples. > [...] >> 3. >> http://code.google.com/p/snake-guice/source/browse/trunk/snakeguice/tests/test_api_25.py > > can i make a suggestion? ?add comments to those tests explaining what they > do in fairly boring, introductory detail (ie "we are injecting a value in > ... because ..."). > > also, explain "ch" (class hierarchy) or, if it's just a meaningless module > name, use > > from ... import Person > > because i thought it was something important and now think it's noise and > if its noise it wasted my time. ?i know "Person" is the kind of class used > in examples, but i am unsure if "ch" is something special your system > needs or not. > > i was going to argue that ioc isn't needed in python because the > motivation for it in java - that you need a more concise configuration > language - doesn't exist. ?what's the point of using xml to simulate a > scripting language when you are already in a scripting language. ?but > maybe i have missed something, given that this stuff exists (hence me > trying to understand it). > That is a very good point. Up until recently I had been mostly working on getting it ready to use at work. I am only beginning my attempt to get community involvement. -- David blog: http://www.traceback.org twitter: http://twitter.com/dstanek From skip at pobox.com Sun Apr 5 09:33:04 2009 From: skip at pobox.com (skip at pobox.com) Date: Sun, 5 Apr 2009 08:33:04 -0500 Subject: Python Goes Mercurial In-Reply-To: References: <49d65b62$0$30904$9b622d9e@news.freenet.de> <49d80a4a$0$30643$9b622d9e@news.freenet.de> <262497db-d2fd-4217-978c-fc5571f103d5@c11g2000yqj.googlegroups.com> Message-ID: <18904.45840.77133.911735@montanaro.dyndns.org> >> If Martin - which is well above the average programmer - says that he >> would need help with Git, I take this as meaning that most people >> would get lost with Git. Lawrence> I don't feel lost with Git. Does that make me an above-average Lawrence> programmer? No, perhaps it just means git fits your brain better than it fits Martin's. I have trouble understanding any of the DVCS tools, but I think it's mostly because I'm overwhelmed by the seemingly infinite variety of workflows. -- Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/ "XML sucks, dictionaries rock" - Dave Beazley From aahz at pythoncraft.com Sun Apr 5 09:42:46 2009 From: aahz at pythoncraft.com (Aahz) Date: 5 Apr 2009 06:42:46 -0700 Subject: Killing threads References: <4b52f7d7-81d5-4141-9385-ee8cfb90ac1b@l1g2000yqk.googlegroups.com> <53ebfff9-448f-438f-aa93-a2187bf13056@f1g2000prb.googlegroups.com> Message-ID: In article <53ebfff9-448f-438f-aa93-a2187bf13056 at f1g2000prb.googlegroups.com>, imageguy wrote: >On Apr 4, 10:43=A0pm, ericwoodwo... at gmail.com wrote: >> >> The issue that I'm having is...I don't know how to kill this app in >> window. > >I am not an expert either, however, I think the standard practice is >to use and threading.Event to communicate with the threads. Nope, Eric had the right idea the first time: use Queue.Queue. And the reason is precisely because you're not a thread expert; using Queue is the easiest way to avoid deadlocks and other threading problems. For more info, see the slides from my thread tutorial: http://pythoncraft.com/OSCON2001/ -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." --Brian W. Kernighan From nirp42 at walla.com Sun Apr 5 09:56:21 2009 From: nirp42 at walla.com (=?windows-1255?Q?=61=20=62?=) Date: Sun, 5 Apr 2009 16:56:21 +0300 Subject: =?windows-1255?Q?=4D=6F=6E=69=74=6F=72=69=6E=67=20=49=6E=74=65=72=6E=65=74=20=45=78=70=6C=6F=72=65=72?= Message-ID: <1238939780.830000-67623641-29038@walla.com> An HTML attachment was scrubbed... URL: From skip at pobox.com Sun Apr 5 09:58:16 2009 From: skip at pobox.com (skip at pobox.com) Date: Sun, 5 Apr 2009 08:58:16 -0500 Subject: replacement for new.instancemethod in Python3? In-Reply-To: References: Message-ID: <18904.47352.437165.498394@montanaro.dyndns.org> Michele> There is no need for new.instancemethod for new style classes: >>> class C: pass ... >>> c=C() >>> def f(self): pass ... >>> c.f = f.__get__(c, C) >>> c.f > Like a chimpanzee I can mimic your use of __get__ (that is, use the pattern you've defined without understanding what it means), but based on the 3.1 docs I haven't the slightest idea what it's really supposed to do: object.__get__(self, instance, owner) Called to get the attribute of the owner class (class attribute access) or of an instance of that class (instance attribute access). owner is always the owner class, while instance is the instance that the attribute was accessed through, or None when the attribute is accessed through the owner. This method should return the (computed) attribute value or raise an AttributeError exception. And what does object.__set__ do? object.__set__(self, instance, value) Called to set the attribute on an instance instance of the owner class to a new value, value. What attribute on the instance? I rather like functools.partial better than object.__get__. At least I can understand it. Skip From michele.simionato at gmail.com Sun Apr 5 10:18:01 2009 From: michele.simionato at gmail.com (Michele Simionato) Date: Sun, 5 Apr 2009 07:18:01 -0700 (PDT) Subject: replacement for new.instancemethod in Python3? References: Message-ID: On Apr 5, 3:58?pm, s... at pobox.com wrote: > ? ? Michele> There is no need for new.instancemethod for new style classes: > > ? ? >>> class C: pass > ? ? ... > ? ? >>> c=C() > ? ? >>> def f(self): pass > ? ? ... > ? ? >>> c.f = f.__get__(c, C) > ? ? >>> c.f > ? ? > > > Like a chimpanzee I can mimic your use of __get__ (that is, use the pattern > you've defined without understanding what it means), but based on the 3.1 > docs I haven't the slightest idea what it's really supposed to do: > > ? ? object.__get__(self, instance, owner) > ? ? ? ? Called to get the attribute of the owner class (class attribute > ? ? ? ? access) or of an instance of that class (instance attribute > ? ? ? ? access). owner is always the owner class, while instance is the > ? ? ? ? instance that the attribute was accessed through, or None when the > ? ? ? ? attribute is accessed through the owner. This method should return > ? ? ? ? the (computed) attribute value or raise an AttributeError exception. > > And what does object.__set__ do? > > ? ? object.__set__(self, instance, value) > ? ? ? ? Called to set the attribute on an instance instance of the owner > ? ? ? ? class to a new value, value. > > What attribute on the instance? > > I rather like functools.partial better than object.__get__. ?At least I can > understand it. > > Skip Perhaps the docstring should contain a link to Raymond Hettinger's descriptor essay: http://users.rcn.com/python/download/Descriptor.htm From fairwinds.dp at gmail.com Sun Apr 5 10:28:26 2009 From: fairwinds.dp at gmail.com (David Pratt) Date: Sun, 5 Apr 2009 11:28:26 -0300 Subject: mmap resizing macosx unix Message-ID: <48DC568D-4D92-4E55-8A03-E717666C8857@gmail.com> Hi. I have been experimenting with mmap recently. I determined how to read and write properly from it and so search and replace on large files. The problem I am having is with replaces that are larger than the mmap. In this instance I need to * rewind * resize the mmap to accomodate the text * move some part of the text to a new location on the mmap so the new text does not overwrite the old * write the replacement text When I try to use resize it gives me the following error; SystemError: mmap: resizing not available--no mremap() I pass size to mmap to begin with based on my filesize size = os.path.getsize(filename) m = mmap.mmap(f.fileno(), size) I am attempting to give it new size by doing: m.resize(size + size_diff) size_diff is difference between original and what will be new file size after the insertion. Can someone provide some hint on resizing or its this a python bug? Many thanks Regards David From philip at semanchuk.com Sun Apr 5 10:34:56 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Sun, 5 Apr 2009 10:34:56 -0400 Subject: mmap resizing macosx unix In-Reply-To: <48DC568D-4D92-4E55-8A03-E717666C8857@gmail.com> References: <48DC568D-4D92-4E55-8A03-E717666C8857@gmail.com> Message-ID: <5D97D682-32EE-470C-B0B8-B549CB1E0BE9@semanchuk.com> On Apr 5, 2009, at 10:28 AM, David Pratt wrote: > Hi. I have been experimenting with mmap recently. I determined how > to read and write properly from it and so search and replace on > large files. The problem I am having is with replaces that are > larger than the mmap. In this instance I need to > > * rewind > * resize the mmap to accomodate the text > * move some part of the text to a new location on the mmap so the > new text does not overwrite the old > * write the replacement text > > When I try to use resize it gives me the following error; > > SystemError: mmap: resizing not available--no mremap() Hi David, Based on experience with my posix_ipc module, ISTR that (a) resizing of the mmapp-ed segment happens via a call to ftruncate() (or os.truncate() in Python land) and (b) OS X only supports one call to this per segment; subsequent calls fail. Keep in mind that I was dealing with shared memory. A regular mmapped file might resize just fine so point (b) might be irrelevant for you. HTH Philip From nxrikjs at googlemail.com Sun Apr 5 10:44:16 2009 From: nxrikjs at googlemail.com (nxrikjs at googlemail.com) Date: Sun, 5 Apr 2009 07:44:16 -0700 (PDT) Subject: Empty string when reading a file Message-ID: Hi, I'm reading in a file of 250 bytes. At the 55th byte, read() will return an empty string although this isn't the end of the file. This is what I have: for i in range(os.path.getsize("inputFile")): bitsInFile = inputFile.read(1) inputFile.seek(i) byteFromFile = ord(bitsInFile) Reading in 1 byte, then moving to the next byte and converting the character read to its decimal format for later bitwise operations. I've omitted the ord() step and it does return 250 characters, a couple of these are empty strings, not just byte 55. Just to see what was going on, I changed the range to 280 and, sure enough, it returned 30 empty strings signifying the end of the file. Why does it return an empty string when it's not the end of the file? From fairwinds.dp at gmail.com Sun Apr 5 10:58:20 2009 From: fairwinds.dp at gmail.com (David Pratt) Date: Sun, 5 Apr 2009 11:58:20 -0300 Subject: mmap resizing macosx unix In-Reply-To: <5D97D682-32EE-470C-B0B8-B549CB1E0BE9@semanchuk.com> References: <48DC568D-4D92-4E55-8A03-E717666C8857@gmail.com> <5D97D682-32EE-470C-B0B8-B549CB1E0BE9@semanchuk.com> Message-ID: <4F3DAF26-900F-46A5-993C-3F80D9A3D8C0@gmail.com> Hi Phillip. I appreciate your reply. I think perhaps I will need to create a new mmap as a work around and write to locations of the second mmap based on my regex searches in the first. I should have said I am using 2.5.4 as well to be clear. I am wondering if I should recommend change to documentation for mmap if this is an issue for OSX. I have vmware installed so might try on CentOS to see what happens there. Many thanks Regards, David On 5-Apr-09, at 11:34 AM, Philip Semanchuk wrote: > > On Apr 5, 2009, at 10:28 AM, David Pratt wrote: > >> Hi. I have been experimenting with mmap recently. I determined how >> to read and write properly from it and so search and replace on >> large files. The problem I am having is with replaces that are >> larger than the mmap. In this instance I need to >> >> * rewind >> * resize the mmap to accomodate the text >> * move some part of the text to a new location on the mmap so the >> new text does not overwrite the old >> * write the replacement text >> >> When I try to use resize it gives me the following error; >> >> SystemError: mmap: resizing not available--no mremap() > > Hi David, > Based on experience with my posix_ipc module, ISTR that (a) resizing > of the mmapp-ed segment happens via a call to ftruncate() (or > os.truncate() in Python land) and (b) OS X only supports one call to > this per segment; subsequent calls fail. Keep in mind that I was > dealing with shared memory. A regular mmapped file might resize > just fine so point (b) might be irrelevant for you. > > HTH > Philip > -- > http://mail.python.org/mailman/listinfo/python-list From jjposner at snet.net Sun Apr 5 11:14:09 2009 From: jjposner at snet.net (John Posner) Date: Sun, 05 Apr 2009 11:14:09 -0400 Subject: Generators/iterators, Pythonicity, and primes Message-ID: <49D8CAC1.500@snet.net> Kay Schluehr said: > g = (lambda primes = []: > (n for n in count(2) \ > if > (lambda n, primes: (n in primes if primes and n<=primes[-1] \ > else > (primes.append(n) or True \ > if all(n%p for p in primes if p <= sqrt(n)) \ > else False)))(n, primes)))() Um ... did you say "easy"? :-) This is prodigious, and it's definitely a solution to the generator-expression-only challenge I laid down. But I think this is a case in which using a generator expression causes a loss in "Pythonicity", rather than a gain. Many, many thanks for this! Steven D'Aprano said: > In other words, this is a crappy way to generate primes. It's a trick, > not a good algorithm: slow and inefficient, as well as obfuscated. I wasn't thinking at all about efficiency, so I'm not surprised that my implementation -- a pretty simple adaptation of http://code.activestate.com/recipes/576640 -- scores low in that dimension. I was mainly looking for Pythonicity. I'm sure this means different things to different people, PEP 20 notwithstanding. To me, this is an important aspect of Pythonicity: The final Python code looks like your initial back-of-the-napkin pseudo code. In the old days, that meant using filter(), because this function mirrors the "sieve" concept in the Sieve of Eratosthenes. These days, I believe that this condition: if all(n%p for p in primes if p < sqrt(n)): ... closely resembles this pseudo code: if the number cannot be evenly divided by all the primes we've generated so far (and we can stop testing at the square root) So I believe this algorithm is the *opposite* of obfuscated. It's crystal clear (albeit a slowpoke). Miles (and others) said: > P.S. Gmail shows all your messages with a blank body and a text > attachment containing your message; this is perhaps because your > mailer includes an invalid blank Content-disposition header. It might be my ISP (AT&T Yahoo), or it might be MS-Outlook. I'm sending this message using Thunderbird 2.0.0.21. Any better? -John From google at mrabarnett.plus.com Sun Apr 5 11:28:52 2009 From: google at mrabarnett.plus.com (MRAB) Date: Sun, 05 Apr 2009 16:28:52 +0100 Subject: Empty string when reading a file In-Reply-To: References: Message-ID: <49D8CE34.2020003@mrabarnett.plus.com> nxrikjs at googlemail.com wrote: > Hi, > > I'm reading in a file of 250 bytes. At the 55th byte, read() will > return an empty string although this isn't the end of the file. This > is what I have: > > for i in range(os.path.getsize("inputFile")): > bitsInFile = inputFile.read(1) > inputFile.seek(i) > byteFromFile = ord(bitsInFile) > > Reading in 1 byte, then moving to the next byte and converting the > character read to its decimal format for later bitwise operations. > > I've omitted the ord() step and it does return 250 characters, a > couple of these are empty strings, not just byte 55. Just to see what > was going on, I changed the range to 280 and, sure enough, it returned > 30 empty strings signifying the end of the file. > > Why does it return an empty string when it's not the end of the file? > Are you using Windows and opening the file as text instead of binary? In text files a byte of value 26 indicates the end of the file, so .read() would return an empty file. Anyway, if the file is only 250 bytes long then you might as well reading it all at once, something like this: f = open("inputFile", "rb") # Open as _binary_. data = [ord(byte) for byte in f.read()] f.close() From kay.schluehr at gmx.net Sun Apr 5 11:37:20 2009 From: kay.schluehr at gmx.net (Kay Schluehr) Date: Sun, 5 Apr 2009 08:37:20 -0700 (PDT) Subject: Generators/iterators, Pythonicity, and primes References: Message-ID: <8d3b6689-2d04-4579-8be1-7fa7b2898b4e@c36g2000yqn.googlegroups.com> On 5 Apr., 17:14, John Posner wrote: > Kay Schluehr said: > > > g = (lambda primes = []: > > (n for n in count(2) \ > > if > > (lambda n, primes: (n in primes if primes and > n<=primes[-1] \ > > else > > (primes.append(n) or True \ > > if all(n%p for p in primes if p <= sqrt(n)) \ > > else False)))(n, primes)))() > > Um ... did you say "easy"? :-) This is prodigious, and it's definitely a > solution to the generator-expression-only challenge I laid down. That's because it is *one* expression. The avoidance of named functions makes it look obfuscated or prodigious. Once it is properly dissected it doesn't look that amazing anymore. Start with: (n for n in count(2) if is_prime(n, primes)) The is_prime function has following implementation: def is_prime(n, primes): if primes and n<=primes[-1]: return n in primes else: if all(n%p for p in primes if p <= sqrt(n)): primes.append(n) return True else: return False But we need a lambda-fied function expression instead of the function definition. This yields: is_prime = lambda n, primes: (n in primes \ if primes and n<=primes[-1] \ else (primes.append(n) or True \ if all(n%p for p in primes if p <= sqrt(n)) \ else False)) Finally the trick with primes definition within an expression by means of an optional argument in the lambda is applied. > But I > think this is a case in which using a generator expression causes a loss > in "Pythonicity", rather than a gain. Many, many thanks for this! I think so too. The solution with the simple generator expression and the fully defined is_prime function may be just adequate. From joe.p.cool at googlemail.com Sun Apr 5 11:39:01 2009 From: joe.p.cool at googlemail.com (Joe P. Cool) Date: Sun, 5 Apr 2009 08:39:01 -0700 (PDT) Subject: Python3: to add, remove and change References: <421c55fc-5acf-454b-830f-a7b5b579f36c@c9g2000yqm.googlegroups.com> <405e8439-382f-41e8-b177-2cb459297bd4@d19g2000prh.googlegroups.com> Message-ID: On 2 Apr., 09:39, bearophileH... at lycos.com wrote: > To change: + is a commutative operation, No, it _is_ not. It is rather _used_ frequently as a symbol for adding numbers, which happens to be a commutative operation. For me '+' means composing instances of a kind. If we are to narrow minded about generalization we'll end up with a clumsy syntax like Java's. > Unladen Swallow for official CPython 2.x/3.x: yes, thank you, if it > will be good enough. It can be what Psyco isn't able to become, and > what PyPy has so far failed to be (PyPy seems Open Souce public-money- > funded vaporwere that has tried to do too much). How about moving these lines to a separate message with a matching header? Regards, Joe. P. Cool From Scott.Daniels at Acm.Org Sun Apr 5 11:54:55 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sun, 05 Apr 2009 08:54:55 -0700 Subject: statvfs clearance In-Reply-To: References: <22347545-9b8d-4d06-88af-3aaabea41a8b@r33g2000yqn.googlegroups.com> <87iqlk7cnd.fsf@busola.homelinux.net> Message-ID: Dave Angel wrote: > The term we used to use for these is "magic numbers." Using > magic-numbers directly in your code is a good way to ensure future time > debugging. And perhaps, in these job-scarce days, future employment. I am reminded of a saying we had in the OS group at TymShare: "A wizard is someone who can fix his own mistakes quickly." Heaven save us from wizards. --Scott David Daniels Scott.Daniels at Acm.Org From nick at stinemates.org Sun Apr 5 12:40:57 2009 From: nick at stinemates.org (Nick Stinemates) Date: Sun, 5 Apr 2009 09:40:57 -0700 Subject: Generators/iterators, Pythonicity, and primes In-Reply-To: <8d3b6689-2d04-4579-8be1-7fa7b2898b4e@c36g2000yqn.googlegroups.com> References: <8d3b6689-2d04-4579-8be1-7fa7b2898b4e@c36g2000yqn.googlegroups.com> Message-ID: <007601c9b60d$4c10f6a0$e432e3e0$@org> I thought it was beautiful. Reminded me of lisp and I haven't seen that done in Python before. -----Original Message----- From: python-list-bounces+nick=stinemates.org at python.org [mailto:python-list-bounces+nick=stinemates.org at python.org] On Behalf Of Kay Schluehr Sent: Sunday, April 05, 2009 8:37 AM To: python-list at python.org Subject: Re: Generators/iterators, Pythonicity, and primes On 5 Apr., 17:14, John Posner wrote: > Kay Schluehr said: > > > g = (lambda primes = []: > > (n for n in count(2) \ > > if > > (lambda n, primes: (n in primes if primes and > n<=primes[-1] \ > > else > > (primes.append(n) or True \ > > if all(n%p for p in primes if p <= sqrt(n)) \ > > else False)))(n, primes)))() > > Um ... did you say "easy"? :-) This is prodigious, and it's definitely a > solution to the generator-expression-only challenge I laid down. That's because it is *one* expression. The avoidance of named functions makes it look obfuscated or prodigious. Once it is properly dissected it doesn't look that amazing anymore. Start with: (n for n in count(2) if is_prime(n, primes)) The is_prime function has following implementation: def is_prime(n, primes): if primes and n<=primes[-1]: return n in primes else: if all(n%p for p in primes if p <= sqrt(n)): primes.append(n) return True else: return False But we need a lambda-fied function expression instead of the function definition. This yields: is_prime = lambda n, primes: (n in primes \ if primes and n<=primes[-1] \ else (primes.append(n) or True \ if all(n%p for p in primes if p <= sqrt(n)) \ else False)) Finally the trick with primes definition within an expression by means of an optional argument in the lambda is applied. > But I > think this is a case in which using a generator expression causes a loss > in "Pythonicity", rather than a gain. Many, many thanks for this! I think so too. The solution with the simple generator expression and the fully defined is_prime function may be just adequate. -- http://mail.python.org/mailman/listinfo/python-list From jjposner at snet.net Sun Apr 5 12:47:29 2009 From: jjposner at snet.net (John Posner) Date: Sun, 05 Apr 2009 12:47:29 -0400 Subject: Generators/iterators, Pythonicity, and primes Message-ID: <49D8E0A1.30306@snet.net> Kay Schluehr wrote: > That's because it is *one* expression. The avoidance of named > functions makes it look obfuscated or prodigious. Once it is properly > dissected it doesn't look that amazing anymore. > > Start with: > > (n for n in count(2) if is_prime(n, primes)) > > The is_prime function has following implementation: > > def is_prime(n, primes): > if primes and n<=primes[-1]: > return n in primes > else: > if all(n%p for p in primes if p <= sqrt(n)): > primes.append(n) > return True > else: > return False Your explication is excellent, Kay! In the is_prime() function, can't we omit the first three lines (if ... else), because it will *always* be the case that "n" exceeds all the primes we've gathered so far. I've tested the code with these lines omitted -- both with the separate is_prime() function and with the generator-expression-only implementation. It seems to work fine. Ex: --- from itertools import count from math import sqrt g = (lambda primes = []: (n for n in count(2) if (lambda x, primes: (primes.append(x) or True if all(x%p for p in primes if p <= sqrt(x)) else False) )(n, primes) ) )() for i in range(500): print g.next(), --- Note that we don't need any backslashes, either. From kay.schluehr at gmx.net Sun Apr 5 12:57:31 2009 From: kay.schluehr at gmx.net (Kay Schluehr) Date: Sun, 5 Apr 2009 09:57:31 -0700 (PDT) Subject: Generators/iterators, Pythonicity, and primes References: Message-ID: <4f9e2776-6b4b-418d-ba00-943727db705b@z1g2000yqn.googlegroups.com> On 5 Apr., 18:47, John Posner wrote: > Kay Schluehr wrote: > > > That's because it is *one* expression. The avoidance of named > > functions makes it look obfuscated or prodigious. Once it is properly > > dissected it doesn't look that amazing anymore. > > > > Start with: > > > > (n for n in count(2) if is_prime(n, primes)) > > > > The is_prime function has following implementation: > > > > def is_prime(n, primes): > > if primes and n<=primes[-1]: > > return n in primes > > else: > > if all(n%p for p in primes if p <= sqrt(n)): > > primes.append(n) > > return True > > else: > > return False > > Your explication is excellent, Kay! In the is_prime() function, can't we > omit the first three lines (if ... else), because it will *always* be > the case that "n" exceeds all the primes we've gathered so far. Yes, sure. I developed is_prime as a standalone function with the primes list as a cache and just lambda-fied it. > I've > tested the code with these lines omitted -- both with the separate > is_prime() function and with the generator-expression-only > implementation. It seems to work fine. Ex: > > --- > from itertools import count > from math import sqrt > > g = (lambda primes = []: > (n for n in count(2) if > (lambda x, primes: > (primes.append(x) or True > if all(x%p for p in primes if p <= sqrt(x)) > else False) > )(n, primes) > ) > )() This is of course much more accessible and doesn't even look weird. Regards From jura.grozni at gmail.com Sun Apr 5 13:14:08 2009 From: jura.grozni at gmail.com (azrael) Date: Sun, 5 Apr 2009 10:14:08 -0700 (PDT) Subject: Need advise about an application References: <6b74d269-6ef5-4483-a492-de397c2e156c@f19g2000yqh.googlegroups.com> Message-ID: <2fb8b914-5302-4c13-90fb-aaf1c1ff5e8a@z1g2000yqn.googlegroups.com> On 5 tra, 14:48, ??? wrote: > Do you have to show these data on GUI? If so, why not use the event > pattern? > As far as I know, it's the standard way wxPython works. Yes, I have to show the signaled data, but also create some statistical meassurement datasets as also some graphical represenatations and graphs. wyPython works this way and it supports custom made events. I thought of something like that. But I am not sure how to combine the serial port signal to the event to call the handler. Time to google for it :D But I am still not sure if to use one thread for signal receiving and DB saving, and anotherone for statistics and other stuff because it is very important to save the signal which comes every second. if the statistics generation can use the rest of the time, then it should be done. I have absolutly no expirience with multi thread programing. I know about them only from theory I got to learn at college. I wanted to ask for an advice from something from someone with lots expirience, but also to learn new stuff by example. > > BTW : If it's not complicated, I think maybe some wysiwyg solution is > better. > I use python for almost every thing, except these related to GUI. How do you mean WYSIWYG. I know what you mean but Don't know egactly what you mean. > > On Apr 5, 8:31 pm, azrael wrote: > > > I am currently working on an application and I need a advise. > > > I am supposed to read data from a device connected to a serial port. I > > am reading data using pySerial. The devise is sending signals with a > > time between two signals of one second. > > > The application is supposed to collect the data and put it into a > > sqlite DB. wxPython GUI. > > I am worried about this asspect that I have two solutions how to do it > > but as I never tried it I wanted to ask for advise which one should be > > easier. > > > solution 1): > > The GUI is set into a separate thread then the part of collecting and > > saving data. Well one thread for getting signals and saving data and > > the other for GUI and data presentation > > > solution 2): > > create an event which is binded to pySerial read methon. Well when the > > devise sends a signal the event function is triggered. > > > Or maybe combine the two? From bbaja42 at gmail.com Sun Apr 5 13:17:04 2009 From: bbaja42 at gmail.com (barisa) Date: Sun, 5 Apr 2009 10:17:04 -0700 (PDT) Subject: python docs redirect on python.org is old References: <49d80d87$0$30878$9b622d9e@news.freenet.de> Message-ID: <49e5f821-849a-479a-949d-177c3078b655@3g2000yqk.googlegroups.com> On Apr 5, 3:46?am, "Martin v. L?wis" wrote: > Brandon Craig Rhodes wrote: > > When I visit ... > > > ? ?http://www.python.org/doc/lib/lib.html > > > ... I get redirected to ... > > > ? ?http://www.python.org/doc/2.5.2/lib/lib.html > > > ... which seems a bit old. > > That is intentional. Use > > http://docs.python.org/library/ > > instead. > > Regards, > Martin why is it intentional? From bbaja42 at gmail.com Sun Apr 5 13:22:08 2009 From: bbaja42 at gmail.com (barisa) Date: Sun, 5 Apr 2009 10:22:08 -0700 (PDT) Subject: Testing dynamic languages References: <70d345df-33ca-4628-9fe9-dda113b5a2f4@n20g2000vba.googlegroups.com> <16c4d592-2acf-4e3c-8214-4624c75b00ef@y7g2000yqa.googlegroups.com> Message-ID: <641a30b8-c659-4212-9f31-b9eb401ad901@r37g2000yqn.googlegroups.com> On Apr 4, 9:57?pm, bearophileH... at lycos.com wrote: > that. Don't fight the language. Use doctests). My other suggestion is > to read code coming from 5+ Python programs written by other > (different) people. You will see how to use Python. > > Bye, > bearophile Is there some online repository for such code? :) Regards Begginer From stef.mientki at gmail.com Sun Apr 5 13:23:27 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Sun, 05 Apr 2009 19:23:27 +0200 Subject: Need advise about an application In-Reply-To: <6b74d269-6ef5-4483-a492-de397c2e156c@f19g2000yqh.googlegroups.com> References: <6b74d269-6ef5-4483-a492-de397c2e156c@f19g2000yqh.googlegroups.com> Message-ID: <49D8E90F.20008@gmail.com> azrael wrote: > I am currently working on an application and I need a advise. > > I am supposed to read data from a device connected to a serial port. I > am reading data using pySerial. The devise is sending signals with a > time between two signals of one second. > > The application is supposed to collect the data and put it into a > sqlite DB. wxPython GUI. > I am worried about this asspect that I have two solutions how to do it > but as I never tried it I wanted to ask for advise which one should be > easier. > > solution 1): > The GUI is set into a separate thread then the part of collecting and > saving data. Well one thread for getting signals and saving data and > the other for GUI and data presentation > > solution 2): > create an event which is binded to pySerial read methon. Well when the > devise sends a signal the event function is triggered. > > Or maybe combine the two? > -- > http://mail.python.org/mailman/listinfo/python-list > I think the easiest method is to use a timer (100 ms interval), let the timer-completion check if new data from serial available, etc. I use this method with far larger throughputs ( 10k bytes / sec) and it works very good. cheers, Stef From nick at stinemates.org Sun Apr 5 13:26:52 2009 From: nick at stinemates.org (Nick Stinemates) Date: Sun, 5 Apr 2009 10:26:52 -0700 Subject: Testing dynamic languages In-Reply-To: <641a30b8-c659-4212-9f31-b9eb401ad901@r37g2000yqn.googlegroups.com> References: <70d345df-33ca-4628-9fe9-dda113b5a2f4@n20g2000vba.googlegroups.com> <16c4d592-2acf-4e3c-8214-4624c75b00ef@y7g2000yqa.googlegroups.com> <641a30b8-c659-4212-9f31-b9eb401ad901@r37g2000yqn.googlegroups.com> Message-ID: <008401c9b613$b60da070$2228e150$@org> Plenty. Try github.com for starters. -----Original Message----- From: python-list-bounces+nick=stinemates.org at python.org [mailto:python-list-bounces+nick=stinemates.org at python.org] On Behalf Of barisa Sent: Sunday, April 05, 2009 10:22 AM To: python-list at python.org Subject: Re: Testing dynamic languages On Apr 4, 9:57?pm, bearophileH... at lycos.com wrote: > that. Don't fight the language. Use doctests). My other suggestion is > to read code coming from 5+ Python programs written by other > (different) people. You will see how to use Python. > > Bye, > bearophile Is there some online repository for such code? :) Regards Begginer -- http://mail.python.org/mailman/listinfo/python-list From werner.bruhin at free.fr Sun Apr 5 13:29:54 2009 From: werner.bruhin at free.fr (Werner F. Bruhin) Date: Sun, 05 Apr 2009 19:29:54 +0200 Subject: logging - string formating problems Message-ID: <49D8EA92.9080704@free.fr> I see the following exception with a string formating problem. TypeError: not all arguments converted during string formatting Traceback (most recent call last): File "/usr/lib/python2.5/logging/__init__.py", line 744, in emit msg = self.format(record) File "/usr/lib/python2.5/logging/__init__.py", line 630, in format return fmt.format(record) File "/usr/lib/python2.5/logging/__init__.py", line 418, in format record.message = record.getMessage() File "/usr/lib/python2.5/logging/__init__.py", line 288, in getMessage msg = msg % self.args The exception does not give any information on where the problem is coming from. I am using Python 2.5.4 but I see that in 2.6 the code is still the same. Any chance that getMessage could catch this exception and provide better debugging information (i.e. content of msg and self.args). Werner From adam.the.hammer at gmail.com Sun Apr 5 13:43:22 2009 From: adam.the.hammer at gmail.com (acet) Date: Sun, 5 Apr 2009 10:43:22 -0700 (PDT) Subject: time.strptime() milliseconds Message-ID: <93e80ad7-9dfc-4493-bccd-1581349c08b9@k2g2000yql.googlegroups.com> I have a log file with hours,minutes,seconds and milliseconds, which im trying to use, so if i say for example: a = time.strptime("01:01:26:449", "%H:%M:%S:%?") is there a directive for milliseconds where the '?' should be? i cant seem to find it if there is From xarverk at gmail.com Sun Apr 5 13:48:55 2009 From: xarverk at gmail.com (Kenny x) Date: Sun, 5 Apr 2009 10:48:55 -0700 Subject: Help with wxPython program :.: return 1? Message-ID: Hello, I have a problem with my wxPython 2.8 Application. The program opens and closes and on KomodoEdit it says "wxstreamredirect.py returned 1.' What's wrong? http://paste.pocoo.org/show/111180/ P.S. I compared my source code to the source code in wxPython in Action, and it looks the same! Why does my program open, then return 1 and close? Any help Appreciated! :) ~ Xarver -------------- next part -------------- An HTML attachment was scrubbed... URL: From google at mrabarnett.plus.com Sun Apr 5 13:49:51 2009 From: google at mrabarnett.plus.com (MRAB) Date: Sun, 05 Apr 2009 18:49:51 +0100 Subject: logging - string formating problems In-Reply-To: <49D8EA92.9080704@free.fr> References: <49D8EA92.9080704@free.fr> Message-ID: <49D8EF3F.9050307@mrabarnett.plus.com> Werner F. Bruhin wrote: > I see the following exception with a string formating problem. > > TypeError: not all arguments converted during string formatting > Traceback (most recent call last): > File "/usr/lib/python2.5/logging/__init__.py", line 744, in emit > msg = self.format(record) > File "/usr/lib/python2.5/logging/__init__.py", line 630, in format > return fmt.format(record) > File "/usr/lib/python2.5/logging/__init__.py", line 418, in format > record.message = record.getMessage() > File "/usr/lib/python2.5/logging/__init__.py", line 288, in getMessage > msg = msg % self.args > > The exception does not give any information on where the problem is > coming from. > > I am using Python 2.5.4 but I see that in 2.6 the code is still the same. > > Any chance that getMessage could catch this exception and provide better > debugging information (i.e. content of msg and self.args). > The logging methods such as .debug(), etc, look like this: logging.debug(msg[, *args[, **kwargs]]) where msg is actually a format string and args are the arguments (values) for that format. Your problem is that somewhere the arguments don't match the format. From jjposner at snet.net Sun Apr 5 13:59:45 2009 From: jjposner at snet.net (John Posner) Date: Sun, 05 Apr 2009 13:59:45 -0400 Subject: Generators/iterators, Pythonicity, and primes Message-ID: <49D8F191.7000207@snet.net> >> > g = (lambda primes = []: >> > (n for n in count(2) if >> > (lambda x, primes: >> > (primes.append(x) or True >> > if all(x%p for p in primes if p <= sqrt(x)) >> > else False) >> > )(n, primes) >> > ) >> > )() >> >> This is of course much more accessible and doesn't even look weird. >> Thanks! I think the only weirdness remaining is: primes.append(x) or True ... which is a trick to force a True value after primes.append(x) returns None. BTW, for extra cleanliness, the parentheses can be omitted around lines 4-7, the inner lambda expression. From adam.the.hammer at gmail.com Sun Apr 5 14:01:18 2009 From: adam.the.hammer at gmail.com (acet) Date: Sun, 5 Apr 2009 11:01:18 -0700 (PDT) Subject: time.strptime() milliseconds References: <93e80ad7-9dfc-4493-bccd-1581349c08b9@k2g2000yql.googlegroups.com> Message-ID: On Apr 5, 6:43?pm, acet wrote: > I have a log file with hours,minutes,seconds and milliseconds, which > im trying to use, so if i say for example: > a = time.strptime("01:01:26:449", "%H:%M:%S:%?") > is there a directive for milliseconds where the '?' should be? i cant > seem to find it if there is just found : http://bugs.python.org/issue1982 From jura.grozni at gmail.com Sun Apr 5 14:14:05 2009 From: jura.grozni at gmail.com (azrael) Date: Sun, 5 Apr 2009 11:14:05 -0700 (PDT) Subject: Need advise about an application References: <6b74d269-6ef5-4483-a492-de397c2e156c@f19g2000yqh.googlegroups.com> Message-ID: <38e2ebff-cb6e-44d5-8a0f-02011a136d7d@v19g2000yqn.googlegroups.com> I guess that this is not an option because of the case that the calculation of the needed statistics takes not always the same time nad I am afraid tht using sleep() would after a couple of time periods skip a meassurement. From activescott at gmail.com Sun Apr 5 14:41:48 2009 From: activescott at gmail.com (activescott) Date: Sun, 5 Apr 2009 11:41:48 -0700 (PDT) Subject: Module Names with . (dots)? Message-ID: <43088695-c4cc-4fd6-a4b2-21d0003acaf6@z9g2000yqi.googlegroups.com> My understanding is that module names must not have a dot in them. For example, I tried to create a module like scott.appengine.util.py and put misc utility classes in there. However, python gives the error "No module named scott.appengine.util". No I've read about packages (http://docs.python.org/tutorial/modules.html#packages), but packages appear to require a specific directory structure and several package initialization files just to create a simple module with "pretty" dot syntax. Am I missing something? From Scott.Daniels at Acm.Org Sun Apr 5 15:08:04 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sun, 05 Apr 2009 12:08:04 -0700 Subject: Generators/iterators, Pythonicity, and primes In-Reply-To: <01e88935$0$20654$c3e8da3@news.astraweb.com> References: <01e88935$0$20654$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Sun, 05 Apr 2009 00:28:17 -0400, Miles wrote: >> def prime_gen(): ... > That's pretty sweet, but we can make it even faster. We can speed things > up by incrementing by two instead of one, to avoid pointlessly testing > even numbers that we know must fail. We can also speed things up a tad by > making the common test first, and by storing squares rather than > recalculating them each time through the loop. Much as I had done independently > > def prime_gen2(): > yield 2 > n = 3 > yield n > prime_list = [(2, 4), (3, 9)] > it = itertools.count(4) > for n in it: > n = it.next() > for p,p2 in prime_list: > if n % p == 0: > break > elif p2 > n: > prime_list.append((n, n*n)) > yield n > break > else: > raise RuntimeError("Shouldn't have run out of primes!") Here is another speedup (from my prime pair hunt days): Check only 2 of every 6 integers. [6N + 0, 6N + 2, 6N + 4] are divisible by 2, [6N + 0, 6N + 3] by 3. def prime_gen3(): check_list = [(5, 25)] yield 2 yield 3 yield 5 n = 5 for incr in itertools.cycle((2, 4)): n += incr for p, p_squared in check_list: if p_squared > n: check_list.append((n, n * n)) yield n break elif n % p == 0: break else: raise Exception("Shouldn't have run out of primes!") --Scott David Daniels Scott.Daniels at Acm.Org From vlastimil.brom at gmail.com Sun Apr 5 15:13:14 2009 From: vlastimil.brom at gmail.com (Vlastimil Brom) Date: Sun, 5 Apr 2009 21:13:14 +0200 Subject: Help with wxPython program :.: return 1? In-Reply-To: References: Message-ID: <9fdb569a0904051213r720f8004gba0c9d803cd914c5@mail.gmail.com> 2009/4/5 Kenny x : > Hello, I have a problem with my wxPython 2.8 Application. > The program opens and closes and on KomodoEdit it says "wxstreamredirect.py > returned 1.' > ?What's wrong? > http://paste.pocoo.org/show/111180/ > > P.S. I compared my source code to the source code in wxPython in Action, > and it looks the same! Why does my program open, then return 1 and close? > > Any help Appreciated! :) > > ~ Xarver > > -- > http://mail.python.org/mailman/listinfo/python-list > > Hi, try to change the case on line 22: self.frame.show() > self.frame.Show() There is a traceback notice about Frame not having a show() method. hth, vbr From aahz at pythoncraft.com Sun Apr 5 15:24:32 2009 From: aahz at pythoncraft.com (Aahz) Date: 5 Apr 2009 12:24:32 -0700 Subject: CGIXMLRPCRequestHandler example References: Message-ID: [posted & e-mailed, please respond on-group] In article , Phoe6 wrote: > >I have the following CGIXMLRPCRequestHandler usage example. >handler = SimpleXMLRPCServer.CGIXMLRPCRequestHandler() >handler.register_instance(Foo()) >handler.handle_request() What happens when you telnet localhost 80 If that fails, you haven't set up the server correctly. I don't know anything about SimpleXMLRPCServer, but you should be able to make progress using standard HTTP debugging tactics. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "...string iteration isn't about treating strings as sequences of strings, it's about treating strings as sequences of characters. The fact that characters are also strings is the reason we have problems, but characters are strings for other good reasons." --Aahz From aahz at pythoncraft.com Sun Apr 5 15:26:35 2009 From: aahz at pythoncraft.com (Aahz) Date: 5 Apr 2009 12:26:35 -0700 Subject: Testing dynamic languages References: <70d345df-33ca-4628-9fe9-dda113b5a2f4@n20g2000vba.googlegroups.com> <16c4d592-2acf-4e3c-8214-4624c75b00ef@y7g2000yqa.googlegroups.com> <641a30b8-c659-4212-9f31-b9eb401ad901@r37g2000yqn.googlegroups.com> Message-ID: In article <641a30b8-c659-4212-9f31-b9eb401ad901 at r37g2000yqn.googlegroups.com>, barisa wrote: >On Apr 4, 9:57=A0pm, bearophileH... at lycos.com wrote: > >> that. Don't fight the language. Use doctests). My other suggestion is >> to read code coming from 5+ Python programs written by other >> (different) people. You will see how to use Python. >> bearophile > >Is there some online repository for such code? Start with Lib/ in the source ;-) -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "...string iteration isn't about treating strings as sequences of strings, it's about treating strings as sequences of characters. The fact that characters are also strings is the reason we have problems, but characters are strings for other good reasons." --Aahz From aahz at pythoncraft.com Sun Apr 5 15:28:10 2009 From: aahz at pythoncraft.com (Aahz) Date: 5 Apr 2009 12:28:10 -0700 Subject: Re-raising exceptions with a different type and message, preserving existing information References: <87ljqnpo6o.fsf@benfinney.id.au> Message-ID: In article <87ljqnpo6o.fsf at benfinney.id.au>, Ben Finney wrote: > >What I need is to ???wrap??? the exception caught such that it has a >different type and message. But I don't want to lose the existing >type, message, and stack trace; that's all useful information for >someone trying to debug the problem. > >Python's PEP 3134 ???Exception Chaining and Embedded Tracebacks??? >discusses a change accepted in Python 3.0 for ???chaining??? exception >objects, to indicate that a new exception was raised during the >handling of an existing exception. > >What I'm trying to do is related: I need it working in earlier Python >versions, and I need it not for chaining, but only for polymorphism. >What is the right way to do this? The same way that 3.0 does it, IMO. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "...string iteration isn't about treating strings as sequences of strings, it's about treating strings as sequences of characters. The fact that characters are also strings is the reason we have problems, but characters are strings for other good reasons." --Aahz From gh at ghaering.de Sun Apr 5 15:31:50 2009 From: gh at ghaering.de (=?ISO-8859-1?Q?Gerhard_H=E4ring?=) Date: Sun, 05 Apr 2009 21:31:50 +0200 Subject: Best Compatible JS Lib for Django In-Reply-To: References: Message-ID: Daniel Fetchinson wrote: >> Does anyone have experience with using JS Libraries with Django? >> Do some work better than others and are easier to code with? > > You might want to ask this on the django list. Or on a JavaScript list ;-) It doesn't matter much in what context you use the JavaScript library (Django, Ruby on Rails, PHP, ...). FWIW I'm using JQuery with great success. It's really amazing what you can do with it with very little code. -- Gerhard From gh at ghaering.de Sun Apr 5 15:36:05 2009 From: gh at ghaering.de (=?ISO-8859-1?Q?Gerhard_H=E4ring?=) Date: Sun, 05 Apr 2009 21:36:05 +0200 Subject: Cannot find text in *.py files with Windows Explorer? In-Reply-To: <410d24a2-6280-46f5-ba87-ae5c1bcec534@y33g2000prg.googlegroups.com> References: <410d24a2-6280-46f5-ba87-ae5c1bcec534@y33g2000prg.googlegroups.com> Message-ID: John Machin wrote: > On Apr 4, 3:21 pm, John Doe wrote: >> Anybody have a solution for Windows (XP) Explorer search not finding >> ordinary text in *.py files? >> > > Get a grep on yourself! > > http://gnuwin32.sourceforge.net/packages/grep.htm There's something even better: "ack -- better than grep, a power search tool for programmers" http://betterthangrep.com/ -- Gerhard From fairwinds.dp at gmail.com Sun Apr 5 15:39:16 2009 From: fairwinds.dp at gmail.com (David Pratt) Date: Sun, 5 Apr 2009 16:39:16 -0300 Subject: mmap resizing macosx unix In-Reply-To: <5D97D682-32EE-470C-B0B8-B549CB1E0BE9@semanchuk.com> References: <48DC568D-4D92-4E55-8A03-E717666C8857@gmail.com> <5D97D682-32EE-470C-B0B8-B549CB1E0BE9@semanchuk.com> Message-ID: <9B40ED5B-9358-47A9-A51A-88068A7F33D5@gmail.com> For sake of documenting for list, I ended up opening file a second time with 'a', padding it to extend its size (previous size + additional bytes to accommodate the insertion), closing file, open file with 'r+', open a second mmap with with new size, moving text to new location, then inserting the replacement text. Not quite as smooth as doing a resize(), moving text and inserting if this had worked. Regards, David On 5-Apr-09, at 11:34 AM, Philip Semanchuk wrote: > > On Apr 5, 2009, at 10:28 AM, David Pratt wrote: > >> Hi. I have been experimenting with mmap recently. I determined how >> to read and write properly from it and so search and replace on >> large files. The problem I am having is with replaces that are >> larger than the mmap. In this instance I need to >> >> * rewind >> * resize the mmap to accomodate the text >> * move some part of the text to a new location on the mmap so the >> new text does not overwrite the old >> * write the replacement text >> >> When I try to use resize it gives me the following error; >> >> SystemError: mmap: resizing not available--no mremap() > > Hi David, > Based on experience with my posix_ipc module, ISTR that (a) resizing > of the mmapp-ed segment happens via a call to ftruncate() (or > os.truncate() in Python land) and (b) OS X only supports one call to > this per segment; subsequent calls fail. Keep in mind that I was > dealing with shared memory. A regular mmapped file might resize > just fine so point (b) might be irrelevant for you. > > HTH > Philip > -- > http://mail.python.org/mailman/listinfo/python-list From gert.cuykens at gmail.com Sun Apr 5 15:44:09 2009 From: gert.cuykens at gmail.com (gert) Date: Sun, 5 Apr 2009 12:44:09 -0700 (PDT) Subject: with open('com1', 'r') as f: References: <84dc1c84-27f9-40a5-890a-9760c91d3a6d@k2g2000yql.googlegroups.com> Message-ID: <29e7476c-fa6e-4aa4-8260-fcac0275fbf1@g20g2000vba.googlegroups.com> On Apr 5, 12:24?am, "Gabriel Genellina" wrote: > En Sat, 04 Apr 2009 14:11:12 -0300, gert escribi?: > > > On Apr 4, 5:20?pm, Kushal Kumaran wrote: > >> On Fri, 03 Apr 2009 22:10:36 +0200 > >> Christian Heimes wrote: > >> > gert wrote: > >> > > I do understand, and I went looking into pySerial, but it is a long > >> > > way from getting compatible with python3.x and involves other libs > >> > > that are big and non pyhton3.x compatible. > > >> > So don't use Python 3.0. Most people are still using Python 2.5 or > >> > 2.6. > > >> Alternatively, you could look into the pySerial source and find out > >> what it does. > > > I think pywin32 is the way they do the things I want. Witch is not > > python3 ready and way to much work around to do it clean. Using ctypes > > is a option but you have to really know what you are doing and what > > you are looking for. > > The last pywin32 release (213) does work with Python 3. > If you can wait a few days, I'm working on a proper port of pyserial. ? > Preliminary testing shows it's working fine on Windows. Basically, I've ? > modified the read/write methods to use bytes instead of str, and 2to3 did ? > the rest: > > Python 3.0.1 (r301:69561, Feb 13 2009, 20:04:18) [MSC v.1500 32 bit ? > (Intel)] on win32 > Type "help", "copyright", "credits" or "license" for more information. > p3> import serial > p3> ser = serial.Serial(2) > p3> ser.write(b"ATI7\r\n") > p3> for line in ser: print(line.rstrip().decode("ascii","replace")) > ... > ATI7 > > Configuration Profile... > > Product type ? ? ? ? ? US/Canada Internal > Options ? ? ? ? ? ? ? ?V32bis,V.FC,V.34+ > Fax Options ? ? ? ? ? ?Class 1/Class 2.0 > Clock Freq ? ? ? ? ? ? 92.0Mhz > Line Options ? ? ? ? ? Caller ID,Distinctive Ring > Voice Options ? ? ? ? ?Speakerphone,TAD > Eprom ? ? ? ? ? ? ? ? ?256k > Ram ? ? ? ? ? ? ? ? ? ?64k > > EPROM date ? ? ? ? ? ? 5/13/96 > DSP date ? ? ? ? ? ? ? 5/13/96 > > EPROM rev ? ? ? ? ? ? ?2.0 > DSP rev ? ? ? ? ? ? ? ?2.0 > > OK > ^C Great seeing evolution in action, the answer I was hoping for :) From ntwrkd at gmail.com Sun Apr 5 15:53:45 2009 From: ntwrkd at gmail.com (ntwrkd) Date: Sun, 5 Apr 2009 12:53:45 -0700 Subject: Best Compatible JS Lib for Django In-Reply-To: References: Message-ID: I didn't realize there was a Django list. I will direct my questions there. Thanks for the suggestions. On Sun, Apr 5, 2009 at 12:31 PM, Gerhard H?ring wrote: > Daniel Fetchinson wrote: >>> Does anyone have experience with using JS Libraries with Django? >>> Do some work better than others and are easier to code with? >> >> You might want to ask this on the django list. > > Or on a JavaScript list ;-) It doesn't matter much in what context you > use the JavaScript library (Django, Ruby on Rails, PHP, ...). > > FWIW I'm using JQuery with great success. It's really amazing what you > can do with it with very little code. > > -- Gerhard > > -- > http://mail.python.org/mailman/listinfo/python-list > From ericwoodworth at gmail.com Sun Apr 5 15:55:59 2009 From: ericwoodworth at gmail.com (ericwoodworth at gmail.com) Date: Sun, 5 Apr 2009 12:55:59 -0700 (PDT) Subject: Creating a session in windows to auth to remote machines References: <5662d84f-faa9-4792-94e9-9ab3add4e604@c9g2000yqm.googlegroups.com> <854caba3-f51d-4f1e-ae47-da08d125702c@v15g2000yqn.googlegroups.com> Message-ID: On Apr 5, 9:09?am, ericwoodwo... at gmail.com wrote: > On Apr 5, 2:11?am, Tim Golden wrote: > > > > > > > ericwoodwo... at gmail.com wrote: > > > Hi, > > > ? ? ?I'm trying to auth to remote machines so I can plunder WMI to get > > > logs and settings and the like. ?My script works for most of my > > > machines because they're all in the same domain and I run the script > > > as somebody who has enough access to get at this stuff but for > > > machines off the domain I'm stuck. > > > Unless I'm missing something here, you can already specify > > username etc. with WMI. Just Dispatch on "WbemScripting.SWbemLocator" > > and call .ConnectServer. Just in case you haven't already, I > > (naturally :) ) recommend my wmi module [1] which wraps a fair bit > > of the plumbing for you, including this. Eg, > > > > > import wmi > > > c = wmi.WMI ("some-machine", user="tim", password="password") > > > for log in c.Win32_NTLogEvent ( > > ? Logfile="Application", > > ? Type="error" > > ): > > ? print log.RecordNumber, log.SourceName, log.Message > > > > > > [1]http://timgolden.me.uk/python/wmi.html > > > TJG > > That's very cool. ?I was using win32com.client but I'll check this > out. ?Thanks!- Hide quoted text - > > - Show quoted text - Thanks Tim. I looked thru your code and you pointed me at just what I need: SWbemLocator.ConnectServer Method which is awesome. I already have the WMI portion of my program working so I was using SWbemLocator already...I just didn't know about that specific method. That's a big help. I've bookmarked that page...lot of good stuff there. From aahz at pythoncraft.com Sun Apr 5 16:24:16 2009 From: aahz at pythoncraft.com (Aahz) Date: 5 Apr 2009 13:24:16 -0700 Subject: CPython and C++ object GC References: <4425af5f-b188-4c3e-9114-eb7673165cd8@r33g2000yqn.googlegroups.com> Message-ID: [posted & e-mailed] In article <4425af5f-b188-4c3e-9114-eb7673165cd8 at r33g2000yqn.googlegroups.com>, wrote: > >I would like to use a C++ gui library with the following (simplified) >interface in Python. Given the lack of responses on c.l.py, try the capi-sig mailing list. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "...string iteration isn't about treating strings as sequences of strings, it's about treating strings as sequences of characters. The fact that characters are also strings is the reason we have problems, but characters are strings for other good reasons." --Aahz From sjmachin at lexicon.net Sun Apr 5 17:27:55 2009 From: sjmachin at lexicon.net (John Machin) Date: Sun, 5 Apr 2009 14:27:55 -0700 (PDT) Subject: Module Names with . (dots)? References: <43088695-c4cc-4fd6-a4b2-21d0003acaf6@z9g2000yqi.googlegroups.com> Message-ID: On Apr 6, 4:41?am, activescott wrote: > My understanding is that module names must not have a dot in them. For > example, I tried to create a module like scott.appengine.util.py ?and > put misc utility classes in there. However, python gives the error "No > module named scott.appengine.util". No I've read about packages > (http://docs.python.org/tutorial/modules.html#packages), but packages > appear to require a specific directory structure and several package > initialization files just to create a simple module with "pretty" dot > syntax. Am I missing something? Create a simple module with "pretty" underscore syntax e.g. scott_appengine_util and move on. From clp2 at rebertia.com Sun Apr 5 17:42:37 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sun, 5 Apr 2009 14:42:37 -0700 Subject: Module Names with . (dots)? In-Reply-To: <43088695-c4cc-4fd6-a4b2-21d0003acaf6@z9g2000yqi.googlegroups.com> References: <43088695-c4cc-4fd6-a4b2-21d0003acaf6@z9g2000yqi.googlegroups.com> Message-ID: <50697b2c0904051442m60ff02d9k1b0af2f9f2adf79e@mail.gmail.com> On Sun, Apr 5, 2009 at 11:41 AM, activescott wrote: > My understanding is that module names must not have a dot in them. For > example, I tried to create a module like scott.appengine.util.py ?and > put misc utility classes in there. However, python gives the error "No > module named scott.appengine.util". No I've read about packages > (http://docs.python.org/tutorial/modules.html#packages), but packages > appear to require a specific directory structure and several package > initialization files just to create a simple module with "pretty" dot > syntax. Am I missing something? I don't think so. As the Zen says, "Flat is better than nested" and "Simple is better than complex", so there's no need for your unnecessarily hierarchical name since it's not part of a package; either go with John's suggestion or rename your module to something even simpler (e.g. "saeutil"). Cheers, Chris -- I have a blog: http://blog.rebertia.com From activescott at gmail.com Sun Apr 5 18:26:44 2009 From: activescott at gmail.com (activescott) Date: Sun, 5 Apr 2009 15:26:44 -0700 (PDT) Subject: Module Names with . (dots)? References: <43088695-c4cc-4fd6-a4b2-21d0003acaf6@z9g2000yqi.googlegroups.com> Message-ID: On Apr 5, 5:42?pm, Chris Rebert wrote: > On Sun, Apr 5, 2009 at 11:41 AM, activescott wrote: > > My understanding is that module names must not have a dot in them. For > > example, I tried to create a module like scott.appengine.util.py ?and > > put misc utility classes in there. However, python gives the error "No > > module named scott.appengine.util". No I've read about packages > > (http://docs.python.org/tutorial/modules.html#packages), but packages > > appear to require a specific directory structure and several package > > initialization files just to create a simple module with "pretty" dot > > syntax. Am I missing something? > > I don't think so. As the Zen says, "Flat is better than nested" and > "Simple is better than complex", so there's no need for your > unnecessarily hierarchical name since it's not part of a package; > either go with John's suggestion or rename your module to something > even simpler (e.g. "saeutil"). > > Cheers, > Chris > > -- > I have a blog:http://blog.rebertia.com Okay, thanks for the feedback guys. From activescott at gmail.com Sun Apr 5 18:32:06 2009 From: activescott at gmail.com (activescott) Date: Sun, 5 Apr 2009 15:32:06 -0700 (PDT) Subject: Module Names with . (dots)? References: <43088695-c4cc-4fd6-a4b2-21d0003acaf6@z9g2000yqi.googlegroups.com> Message-ID: <16395d17-a978-48e6-b50a-e76ff695707c@37g2000yqp.googlegroups.com> On Apr 5, 5:42?pm, Chris Rebert wrote: > On Sun, Apr 5, 2009 at 11:41 AM, activescott wrote: > > My understanding is that module names must not have a dot in them. For > > example, I tried to create a module like scott.appengine.util.py ?and > > put misc utility classes in there. However, python gives the error "No > > module named scott.appengine.util". No I've read about packages > > (http://docs.python.org/tutorial/modules.html#packages), but packages > > appear to require a specific directory structure and several package > > initialization files just to create a simple module with "pretty" dot > > syntax. Am I missing something? > > I don't think so. As the Zen says, "Flat is better than nested" and > "Simple is better than complex", so there's no need for your > unnecessarily hierarchical name since it's not part of a package; > either go with John's suggestion or rename your module to something > even simpler (e.g. "saeutil"). > > Cheers, > Chris > > -- > I have a blog:http://blog.rebertia.com BTW: I decided to go with 'scottsappengineutil'. I doubt it will quality me as a zen master but I believe it is progress :) From bearophileHUGS at lycos.com Sun Apr 5 18:50:31 2009 From: bearophileHUGS at lycos.com (bearophileHUGS at lycos.com) Date: Sun, 5 Apr 2009 15:50:31 -0700 (PDT) Subject: Module Names with . (dots)? References: <43088695-c4cc-4fd6-a4b2-21d0003acaf6@z9g2000yqi.googlegroups.com> <16395d17-a978-48e6-b50a-e76ff695707c@37g2000yqp.googlegroups.com> Message-ID: <852d5cc4-da2a-4533-b39e-3076b46bb275@f19g2000yqh.googlegroups.com> activescott: > BTW: I decided to go with 'scottsappengineutil'. scottsappengineutil is hard to read and understand. The name split with underscores is more readable: scott_appengine_util Or just: app_engine_util Bye, bearophile From manu3d at gmail.com Sun Apr 5 18:51:46 2009 From: manu3d at gmail.com (Emanuele D'Arrigo) Date: Sun, 5 Apr 2009 15:51:46 -0700 (PDT) Subject: How to go about. On read/write locks Message-ID: <888ed340-48bd-414a-b827-8de726dc729c@h28g2000yqd.googlegroups.com> Hi everybody, I'm having a threading-related design issue and I suspect it has a name that I just don't know. Here's a description. Let's assume a resource (i.e. a dictionary) that needs to be accessed by multiple threads. A simple lock will do the job but in some circumstances it will create an unnecessary bottleneck. I.e. let's assume that most threads only need to have a -read- access to the resource, while only few threads actually change the dictionary. Ideally, the reading threads should not block each other. However, as soon as a threads intends to change the dictionary it should let all the reading threads finish, lock the access to the resource, change it, and then release the lock. I don't think it'd be difficult to implement but I'm wondering if something in this direction has been done already, if it has a name or if it's even well known design pattern. Anybody can shed some light? Manu From tdelaney at avaya.com Sun Apr 5 18:58:15 2009 From: tdelaney at avaya.com (Delaney, Timothy (Tim)) Date: Mon, 6 Apr 2009 06:58:15 +0800 Subject: Python Goes Mercurial In-Reply-To: <12cbbbfc0904020519p20d625baid0674f9ecb7cf64f@mail.gmail.com> Message-ID: As someone who has to use ClearCase UCM at work (damned politics!) I can tell you that I very much prefer creating a separate view (directory) for each branch as I used to do in Base ClearCase. All too often you end up having to deliver multiple activities together because someone else made a change that touched files that you have modified in separate activities, and because you can't deliver without rebasing you end up forced to deliver both changes together (because the rebase creates a dependency between the files). Now, I don't know if git has a similar problem, but it makes me wary of any VCS which purports to work with multiple branches, etc in a single directory. Tim Delaney ________________________________ From: python-list-bounces+tdelaney=avaya.com at python.org [mailto:python-list-bounces+tdelaney=avaya.com at python.org] On Behalf Of Jeremiah Dodds Sent: Thursday, 2 April 2009 11:19 PM To: python-list at python.org Subject: Re: Python Goes Mercurial The one thing that makes me want to use git more than any other dvcs is that you don't have to create a new directory for branches. This may be possible in other dvcs's , but git is the only one I've seen advertise the capability. -------------- next part -------------- An HTML attachment was scrubbed... URL: From tdelaney at avaya.com Sun Apr 5 19:03:45 2009 From: tdelaney at avaya.com (Delaney, Timothy (Tim)) Date: Mon, 6 Apr 2009 07:03:45 +0800 Subject: Python Goes Mercurial In-Reply-To: Message-ID: Sorry - it's early and I didn't force Outlook to not top-post. Unfortunately, I get asked to top-post here at work ... Tim Delaney From daku9999 at gmail.com Sun Apr 5 19:43:26 2009 From: daku9999 at gmail.com (daku9999 at gmail.com) Date: Sun, 5 Apr 2009 16:43:26 -0700 (PDT) Subject: tk - askopenfilenames parsing Message-ID: <0563a43d-63fd-4883-a80e-8d6dc4d23fe9@d19g2000prh.googlegroups.com> Hello, I'm having trouble with the output from askopenfilenames...: --- root = tkinter.Tk() root.withdraw() files = tkinter.filedialog.askopenfilenames(parent=root, title='Choose file(s)') if not files: tkinter.messagebox.showerror("No input file", "No input file given") sys.exit() ---- I'm using "filenames" because I want to handle the opening and closing of the files myself. But I'm having trouble with what 'files' is, as it appears to be a string with a space between each path/filename (when choosing multiple files not in the directory that it is run in, e.g. when I browse to a different directory and grab files). tryin: for filenames in files.split() works as long as there are no directories containing spaces in the names (unlikely). How does one go about parsing the return from askopenfilenames? I'm just trying to let the user select multiple files for some batch parsing and then letting the script happily loop through each one. Any help would be appreciated. pg From imageguy1206 at gmail.com Sun Apr 5 20:27:15 2009 From: imageguy1206 at gmail.com (imageguy) Date: Sun, 5 Apr 2009 17:27:15 -0700 (PDT) Subject: Killing threads References: <4b52f7d7-81d5-4141-9385-ee8cfb90ac1b@l1g2000yqk.googlegroups.com> <53ebfff9-448f-438f-aa93-a2187bf13056@f1g2000prb.googlegroups.com> Message-ID: <685a59cd-9f02-483f-bc59-b55091a181f8@u9g2000pre.googlegroups.com> > For more info, see the slides from my thread tutorial:http://pythoncraft.com/OSCON2001/ > -- > Aahz (a... at pythoncraft.com) ? ? ? ? ? <*> ? ? ? ?http://www.pythoncraft.com/ > Aahz, thanks for this reference and link to your presentation. At the risk of highjacking the OP's question, I am bit confused as to how using an Event would cause a deadlock. You presentation outlines Events, but doesn't elaborate on the specifics or usage. In threading.Event python 2.5 docs say; "This is one of the simplest mechanisms for communication between threads: one thread signals an event and other threads wait for it. " Again, I have limited experience, however, in my reading of the threading manual and review examples, Events were specifically design to be a thread safe way to communicate a 'state' to running threads ? In the OP's example 'do stuff' was open to wide interpretation, however, if within the thread's main 'while' loop the tread checks to see if the 'keepgoing' Event.isSet(), in what scenario would this create deadlock ? Thanks for your patience. I thought I had grasped the basics of using threads/threading, so hope I can learn more. g. From ldo at geek-central.gen.new_zealand Sun Apr 5 20:38:39 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 06 Apr 2009 12:38:39 +1200 Subject: with open('com1', 'r') as f: References: <8bc55c05-19da-41c4-b916-48e0a4be49b9@p11g2000yqe.googlegroups.com> <91e09eaf-5a25-4a6b-b131-a5245970b337@f19g2000yqh.googlegroups.com> Message-ID: In message , Terry Reedy wrote: > Lawrence D'Oliveiro wrote: > >> All Python objects are reference-counted. > > Nope. Only in CPython, and even that could change. Why should it? >> Once the file object becomes >> inaccessible, it is automatically closed. Simple. > > Even in CPython, that would not be true now is the object became > involved in or became a dependent of a reference cycle. And how exactly would that happen with a file object? From grante at visi.com Sun Apr 5 21:58:27 2009 From: grante at visi.com (Grant Edwards) Date: Sun, 05 Apr 2009 20:58:27 -0500 Subject: Example for readline module usage? Message-ID: I want to use the readline module to allow the user to edit input that is typed into my Python program. [Isn't that what the readline module is for? It's certainly what the readline library is used for in C programs.] I've read and re-read the readline moudle documentaion, and I still have absolutely no idea how to use it. I've read the examples, and they seem to be concerned entirely with the interaction of history files and the Python interpreter -- neither of which have anything to do with what I'm trying to do. Can anybody point me to examples/documentation for how to use the readline module? [I swear I've asked this question before, but Google can't find it.] -- Grant From semanticist at gmail.com Sun Apr 5 22:48:52 2009 From: semanticist at gmail.com (Miles) Date: Sun, 5 Apr 2009 22:48:52 -0400 Subject: Tab completion In-Reply-To: <01e4c71e$0$20654$c3e8da3@news.astraweb.com> References: <01e4c71e$0$20654$c3e8da3@news.astraweb.com> Message-ID: On Thu, Apr 2, 2009 at 10:59 AM, Steven D'Aprano wrote: > Does anyone use the tab-completion recipe in the docs? > > http://docs.python.org/library/rlcompleter.html#module-rlcompleter > > suggests using this to enable tab-completion: > > try: > ? ?import readline > except ImportError: > ? ?print "Module readline not available." > else: > ? ?import rlcompleter > ? ?readline.parse_and_bind("tab: complete") > > which is all very nice, but it makes it rather difficult to indent code > blocks: > >>>> def func(x): > ... > Display all 174 possibilities? (y or n) > > > I like tab-completion, but I'd rather not be reduced to typing spaces for > indents in the interpreter. What do other people do? Personally, I just use single spaces for indentation when typing in the interpreter. But how would something like this work for you? import readline import rlcompleter class TabCompleter(rlcompleter.Completer): def complete(self, text, state): if text == '' or text.endswith('\t'): if state == 0: return text + '\t' else: return None return rlcompleter.Completer.complete(self, text, state) readline.set_completer(TabCompleter().complete) -Miles From cs at zip.com.au Sun Apr 5 22:54:49 2009 From: cs at zip.com.au (Cameron Simpson) Date: Mon, 6 Apr 2009 12:54:49 +1000 Subject: statvfs clearance In-Reply-To: <22347545-9b8d-4d06-88af-3aaabea41a8b@r33g2000yqn.googlegroups.com> Message-ID: <20090406025449.GA16077@cskk.homeip.net> On 04Apr2009 03:56, Sreejith K wrote: | Python's statvfs module contains the following indexes to use with | os.statvfs() that contains the specified information | statvfs.F_BSIZE | Preferred file system block size. [...] | Can anyone tell me (or give me some links to know) what are these | values ? The first three I know, I need to know about the rest.... Since os.statvfs is a wrapper for the OS statvfs system call, I would consult "man statvfs" on your platform. That will tell you about the OS facility underlying the python library function. -- Cameron Simpson DoD#743 http://www.cskk.ezoshosting.com/cs/ From brian.cain at gmail.com Sun Apr 5 23:23:01 2009 From: brian.cain at gmail.com (Brian) Date: Sun, 5 Apr 2009 20:23:01 -0700 (PDT) Subject: exporting symbols with ctypes? Message-ID: I'd like to load a library that expects executables which link against it to provide a particular symbol. Is there a way to do the inverse of the in_dll() operation? I'd prefer to avoid creating a brand new library on the fly just to satisfy this one dependency. From ericwoodworth at gmail.com Sun Apr 5 23:25:39 2009 From: ericwoodworth at gmail.com (ericwoodworth at gmail.com) Date: Sun, 5 Apr 2009 20:25:39 -0700 (PDT) Subject: Killing threads References: <4b52f7d7-81d5-4141-9385-ee8cfb90ac1b@l1g2000yqk.googlegroups.com> <53ebfff9-448f-438f-aa93-a2187bf13056@f1g2000prb.googlegroups.com> <685a59cd-9f02-483f-bc59-b55091a181f8@u9g2000pre.googlegroups.com> Message-ID: On Apr 5, 11:07?pm, Dennis Lee Bieber wrote: > On Sun, 5 Apr 2009 17:27:15 -0700 (PDT), imageguy > declaimed the following in > gmane.comp.python.general: > > > In threading.Event python 2.5 docs say; > > "This is one of the simplest mechanisms for communication between > > threads: one thread signals an event and other threads wait for it. " > > > Again, I have limited experience, however, in my reading of the > > threading manual and review examples, Events were specifically design > > to be a thread safe way to communicate a 'state' to running threads ? > > In the OP's example 'do stuff' was open to wide interpretation, > > however, if within the thread's main 'while' loop the tread checks to > > see if the 'keepgoing' Event.isSet(), in what scenario would this > > create deadlock ? > > ? ? ? ? If you are going to perform a CPU intensive polling loop, there is > no sense in using the Event system in the first place... Just create a > globally accessible flag and set it to true when you want to signal the > threads (or false if you don't want to use the negation "while not > flagged: do next processing step") > > ? ? ? ? Event is optimized for the case wherein threads can WAIT (block) on > the Event object. > -- > ? ? ? ? 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/ Well it turns out my problem was with queues not with threads. I had a self.die prop in my thread object that defaults to FALSE and that I set to true when i wanted the thread to die. then my loop would be while not die: It seemed pretty simple so I didn't know why it was failing. What I didn't know, because I'm quite new to python, is that queue.get was blocking. So my producer thread why dying immediately but my worker threads were all blocking on their queue.gets. So they were never falling off the loop. I changed it to queue.get_nowait() and added a queue.empty exception and everything worked as expected. So I thought I knew what was going on and that I was having a really esoteric problem when i was actually having a pretty boring problem I didn't recognize. Thanks everybody for the help! From joseph.h.garvin at gmail.com Mon Apr 6 00:00:17 2009 From: joseph.h.garvin at gmail.com (Joseph Garvin) Date: Mon, 6 Apr 2009 00:00:17 -0400 Subject: Painful?: Using the ast module for metaprogramming Message-ID: I decided to try using the ast module to see how difficult or not it was to use for metaprogramming. So I tried writing a decorator that would perform a simple transformation of a function's code. It was certainly not as easy as I had guessed, but I did succeed so it's not impossible. The issues I encountered might suggest changes to the ast module, but since this is my first time messing with it I think it's equally likely I'm just ignorant of the best way to handle them. Questions: -If I have the source to a single function definition and I pass it to ast.parse, I get back an ast.Module. Why not an ast.FunctionDef? -An ast.Name always has an ast.Load or ast.Store "context" associated with it. This is a bit odd, since based on the actual context (what the parent of the ast.Name node is) it is always clear whether the variable needs to be loaded or stored -- loaded when the node is the child of an expression and stored when the node is the target of an assignment. Is there some weird corner case that necessitates this redundancy? It adds a lot of noise when you're trying to interpret trees, and the ast.Load and ast.Store objects themselves don't seem to contain any data. -Why can't orelse for ast.If and ast.While default to empty []? If you make an ast.While object by hand but don't specify that orelse is empty, you get an error when later trying to compile it. This seems silly since by default not specifying an else in code results in the ast module generating a node with orelse=[]. -Why can't keywords and args for ast.Call default to empty []? Same problem as with orelse. -Why can't I eval a functiondef to get back a function object? As it stands, I have to work around this by giving the functiondef a unique name, exec'ing the AST, and then doing a lookup in locals[] to get the resulting function object. This is a nasty hack. -The provided NodeTransformer class is useful, but provides no way (that I can see) to replace a single statement with multiple statements, because multiple statements would constitute multiple nodes. To work around this, I take the code I want to swap in, and wrap it in a "while 1:" block with a break at the end to ensure it only runs once and doesn't loop. Again, a kludge. -The module provides no means to convert an AST back into source code, which would be nice for debugging. -It would be nice if decorators were passed a function's AST instead of a function object. As it is I have to use inspect.getsource to retrieve the source for the function in question, and then use ast.parse, which is a bit inefficient because the cpython parser has to already have done this once before. Again, it feels like a hack. Making decorators take AST's would obviously be a compatibility breaking change, since you'd then have to compile them before returning, so alternatively you could have "ast decorators" that would use a different prefix symbol in place of @, or you could change it so that decorators got passed the AST but the AST had a __call__ method that would cause the AST to parse itself and become the resulting function object in place and then execute itself, so until the first time it was called it was an AST but after that was a function object (it would 'lazily' become a function). I think that wouldn't break most code. I have some ideas for what an easier API would look like but I want to be sure these are real issues first and not just me doing it wrong ;) Regards, Joe From ganeshborse at gmail.com Mon Apr 6 00:21:59 2009 From: ganeshborse at gmail.com (grbgooglefan) Date: Sun, 5 Apr 2009 21:21:59 -0700 (PDT) Subject: How to free /destroy object created by PyTuple_New References: <3c15a8d6-3095-4fff-b902-8bef22542b86@a5g2000pre.googlegroups.com> <0aa96d3a-f646-4f98-b8c9-934985411917@v6g2000vbb.googlegroups.com> <0f67973b-80dd-47c7-926b-989dec9de358@f1g2000prb.googlegroups.com> <871vs7leyt.fsf@busola.homelinux.net> Message-ID: <95ac8db7-23c3-4436-b786-a980a7f07c2b@d25g2000prn.googlegroups.com> Regarding PyTuple_New, when I pass this tuple with variable values set to some evaluation function like PyObject_CallObject, do I need to increment reference for this tuple & then decrement again after the call returns? If I've not configured my libpython with threads when compiling, will PyGILState_Release, etc. calls still work? For PyGILState_Release/Acquire calls do I need to create the lock explicitely or it will get created internally by Interpreter when Interpreter is initialized? Please guide. From jura.grozni at gmail.com Mon Apr 6 01:16:08 2009 From: jura.grozni at gmail.com (azrael) Date: Sun, 5 Apr 2009 22:16:08 -0700 (PDT) Subject: Need advise about an application References: <6b74d269-6ef5-4483-a492-de397c2e156c@f19g2000yqh.googlegroups.com> <2fb8b914-5302-4c13-90fb-aaf1c1ff5e8a@z1g2000yqn.googlegroups.com> Message-ID: <6ac527b0-938f-4f64-aa6a-02ff2a86aa53@k41g2000yqh.googlegroups.com> On Apr 5, 9:48?pm, Dennis Lee Bieber wrote: > On Sun, 5 Apr 2009 10:14:08 -0700 (PDT), azrael > declaimed the following in gmane.comp.python.general: > > > DB saving, and anotherone for statistics and other stuff because it is > > very important to save the signal which comes every second. if the > > statistics generation can use the rest of the time, then it should be > > done. > > ? ? ? ? In computer terms, 1 second is a very long time. > > ? ? ? ? If the source of the data is that regular (give or take a quarter > second) I'd probably code a repeating timed event with a 0.25s repeat > interval. > > ? ? ? ? > ? ? ? ? ? ? ? ? check serial port for data packet (been a long time, does > pySerial buffer stuff for one? how long are these packets, and are they > identified by a line end character or pure binary?). > ? ? ? ? ? ? ? ? if packet received: > ? ? ? ? ? ? ? ? ? ? ? ? process it > > ? ? ? ? As long as the event process takes less than 0.25s when a packet is > received (the empty packet case should be practically instantaneous: > test/return) you should get back to other GUI events in time for screen > updates, and without running into the start of the next timed check. > (The simplistic route would be to just schedule the next check 0.25s > from when the previous one returned, but that leads to creeping time > quantums. More complex is to compute ? ?next = 0.25 - > time-spent-processing ? ? ? ? ? assuming the GUI library does not > incorporate a static timed repeat call.) > > ? ? ? ? If going thread-based, the thread would contain no GUI related code > (it seems easier to make the GUI the main program, and shove non-UI work > into threads). The thread would basically loop > > ? ? ? ? wait for packet [blocking I/O, not polling loop] > ? ? ? ? compute updates statistics > ? ? ? ? do database update > > ? ? ? ? Now, how to get the statistics to the display? You either need some > GUI event (timed again) which reads the statistics values from > [global structure, from a queue with the thread PUT the > updates, etc.] and formats the display update. Or, again, some event > that can be triggered asynchronously (the timer is a synchronous update > -- you've said "every xxx update the status portion of the display") so > that the status portion is only updated when the data is changed. That's what I meant but was not sure. I guess that I will be callculating the remaining time using decorators. this should give an elegant way. The part about the threads looks like the right thing I needed. Today I will be seing the serial port device so to play a bit with it but also to see what it supports. It would be great if the meassunring signal from the device could have a function as a trigger. this way I could form it as a custom trigger for wxPython. I guess I will just have to have a look at it. Could anyone give me some good literature about custom made events in python. Either to send me the material to my email or to give me some links > -- > ? ? ? ? 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/ From http Mon Apr 6 01:16:44 2009 From: http (Paul Rubin) Date: 05 Apr 2009 22:16:44 -0700 Subject: Painful?: Using the ast module for metaprogramming References: Message-ID: <7x7i1ypdj7.fsf@ruckus.brouhaha.com> Joseph Garvin writes: > I decided to try using the ast module to see how difficult or not it > was to use for metaprogramming. Maybe you really want Lisp? ;-) From martin at v.loewis.de Mon Apr 6 01:33:33 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Mon, 06 Apr 2009 07:33:33 +0200 Subject: Painful?: Using the ast module for metaprogramming In-Reply-To: References: Message-ID: <49d9942d$0$3662$9b622d9e@news.freenet.de> > -If I have the source to a single function definition and I pass it to > ast.parse, I get back an ast.Module. Why not an ast.FunctionDef? Because it is easier for processing if you always get the same type of result. Typically, you don't know what's in the source code, so you need to parse, then inspect. > -An ast.Name always has an ast.Load or ast.Store "context" associated > with it. This is a bit odd, since based on the actual context (what > the parent of the ast.Name node is) it is always clear whether the > variable needs to be loaded or stored It simplifies the interpretation/compilation of the code, by removing the need to go up in the tree. Notice that just looking at the parent node would not be sufficient: for foo[foo] = foo = foo the various occurrences of foo take all kinds of contexts. Figuring out whether a specific occurrence is a load or store would be tricky. > It adds a lot of noise when you're trying to interpret > trees, and the ast.Load and ast.Store objects themselves don't seem to > contain any data. Yes, they are flags only. operator works the same way (taking values such as Add, Sub, ...); likewise unaryop and cmpop. > -Why can't orelse for ast.If and ast.While default to empty []? You want to store None? That would be a type error; orelse is specified as "stmt*". So it must be a list. > -Why can't keywords and args for ast.Call default to empty []? Same > problem as with orelse. Same answer. > -Why can't I eval a functiondef to get back a function object? Because a definition is not an expression. You can only eval expressions. > -The module provides no means to convert an AST back into source code, > which would be nice for debugging. See Demo/parser/unparse.py > -It would be nice if decorators were passed a function's AST instead > of a function object. How could this possibly work? If you run from a pyc file, there will be no AST available to pass. Regards, Martin From nagraj.sigma at gmail.com Mon Apr 6 01:36:49 2009 From: nagraj.sigma at gmail.com (Nagraj Rao) Date: Sun, 5 Apr 2009 22:36:49 -0700 (PDT) Subject: Python wrapper for Ebay In-Reply-To: References: Message-ID: <22902405.post@talk.nabble.com> Hey guys, I'm just wondering whether I've asked a wrong question at a wrong place, since I'm not getting any replies. Please suggest me whats the best way to talk to Ebay from Python. I just want to extract certain product info from ebay and not really perform "add item" or so. Shud I directly make use of the REST APIs and parse the xml out for info? Or is there a better way to do it? Please help. Thanks N Nagraj Rao wrote: > > Hi > > Can anyone suggest me which is the best python wrapper for Ebay APIs? > I've currently gone thru PyEbay and EasyBay. But have not seen much > activities on them since couple of years. Also I'm not able to find > any particular forums or discussion groups. I could find PyAWS for > Amazon which is really helpful. I'm wondering whether I shud write the > wrapper classes all by myself in this case? > > Please suggest. > > Thanks > N > -- > http://mail.python.org/mailman/listinfo/python-list > > -- View this message in context: http://www.nabble.com/Python-wrapper-for-Ebay-tp22847804p22902405.html Sent from the Python - python-list mailing list archive at Nabble.com. From gagsl-py2 at yahoo.com.ar Mon Apr 6 01:52:13 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 06 Apr 2009 02:52:13 -0300 Subject: Cannot find text in *.py files with Windows Explorer? References: <410d24a2-6280-46f5-ba87-ae5c1bcec534@y33g2000prg.googlegroups.com> Message-ID: En Sun, 05 Apr 2009 16:36:05 -0300, Gerhard H?ring escribi?: > John Machin wrote: >> On Apr 4, 3:21 pm, John Doe wrote: >>> Anybody have a solution for Windows (XP) Explorer search not finding >>> ordinary text in *.py files? >> >> Get a grep on yourself! >> http://gnuwin32.sourceforge.net/packages/grep.htm > > There's something even better: > http://betterthangrep.com/ Both have the same problem: they scan the "bytes contained in the file", not the "textual information available from the file". If you want to search for "a?o", grep won't find it if the file is stored in utf-8 and your terminal is latin1, or inside an HTML document written as año, and may have some trouble if the file is in utf-16. In some cases, grep may not find any text inside a PDF document (when it's compressed). The standard Windows search, on the contrary, does not have those limitations: but you have to "tell" it how to extract textual info from unknown file sources; in this case, that .py files are plain text files. This KB article [1] explains how to do that for .py files, and this [2] the underlying technology. [1] http://support.microsoft.com/?kbid=309173 [2] http://channel9.msdn.com/wiki/desktopsearchifilters/ -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Mon Apr 6 01:52:18 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 06 Apr 2009 02:52:18 -0300 Subject: Cannot find text in *.py files with Windows Explorer? References: <410d24a2-6280-46f5-ba87-ae5c1bcec534@y33g2000prg.googlegroups.com> Message-ID: En Sun, 05 Apr 2009 16:36:05 -0300, Gerhard H?ring escribi?: > John Machin wrote: >> On Apr 4, 3:21 pm, John Doe wrote: >>> Anybody have a solution for Windows (XP) Explorer search not finding >>> ordinary text in *.py files? >> >> Get a grep on yourself! >> http://gnuwin32.sourceforge.net/packages/grep.htm > > There's something even better: > http://betterthangrep.com/ Both have the same problem: they scan the "bytes contained in the file", not the "textual information available from the file". If you want to search for "a?o", grep won't find it if the file is stored in utf-8 and your terminal is latin1, or inside an HTML document written as año, and may have some trouble if the file is in utf-16. In some cases, grep may not find any text inside a PDF document (when it's compressed). The standard Windows search, on the contrary, does not have those limitations: but you have to "tell" it how to extract textual info from unknown file sources; in this case, that .py files are plain text files. This KB article [1] explains how to do that for .py files, and this [2] the underlying technology. [1] http://support.microsoft.com/?kbid=309173 [2] http://channel9.msdn.com/wiki/desktopsearchifilters/ -- Gabriel Genellina From asmodai at in-nomine.org Mon Apr 6 01:54:25 2009 From: asmodai at in-nomine.org (Jeroen Ruigrok van der Werven) Date: Mon, 6 Apr 2009 07:54:25 +0200 Subject: Python wrapper for Ebay In-Reply-To: <22902405.post@talk.nabble.com> References: <22902405.post@talk.nabble.com> Message-ID: <20090406055425.GS13110@nexus.in-nomine.org> -On [20090406 07:37], Nagraj Rao (nagraj.sigma at gmail.com) wrote: >Shud I directly make use of the REST APIs and parse the xml out for info? Or >is there a better way to do it? http://developer.ebay.com/DevZone/shopping/docs/CallRef/GetSingleItem.html#sampledescriptionItemSpecifics That's what Ebay offers, so it is best to use that route. It's quite a straightforward implementation in Python. Just use something like httplib2 with lxml or so. -- Jeroen Ruigrok van der Werven / asmodai ????? ?????? ??? ?? ?????? http://www.in-nomine.org/ | http://www.rangaku.org/ | GPG: 2EAC625B Sorrow paid for valour is too much to recall... From joseph.h.garvin at gmail.com Mon Apr 6 02:32:35 2009 From: joseph.h.garvin at gmail.com (Joseph Garvin) Date: Mon, 6 Apr 2009 02:32:35 -0400 Subject: Painful?: Using the ast module for metaprogramming In-Reply-To: <49d9942d$0$3662$9b622d9e@news.freenet.de> References: <49d9942d$0$3662$9b622d9e@news.freenet.de> Message-ID: On Mon, Apr 6, 2009 at 1:33 AM, "Martin v. L?wis" wrote: >> -If I have the source to a single function definition and I pass it to >> ast.parse, I get back an ast.Module. Why not an ast.FunctionDef? > > Because it is easier for processing if you always get the same type of > result. Typically, you don't know what's in the source code, so you > need to parse, then inspect. I see. True I'm guessing for the applications for which the module was originally intended. In a metaprogramming context you usually know though. >> -An ast.Name always has an ast.Load or ast.Store "context" associated >> with it. This is a bit odd, since based on the actual context (what >> the parent of the ast.Name node is) it is always clear whether the >> variable needs to be loaded or stored > > It simplifies the interpretation/compilation of the code, by removing > the need to go up in the tree. Notice that just looking at the parent > node would not be sufficient... I see how it avoids needing to look at the parent node in general, but if we were compiling by recursively descending through the AST, then we would know whether Name's would be loads or stores by the time we got to them (we would already had to have visited an encompassing assignment or expression) -- except in the case of your a = b = c expression, which I'm curious how Python handles. The natural answer is for assignment to be an expression (so b = c returns the new value of b). But Python doesn't do that, so then I'd expect we'd have some third ast.LoadAndStore() option for b, but examining ast.parse's behavior it looks like it chooses Store... >> -Why can't orelse for ast.If and ast.While default to empty []? > > You want to store None? That would be a type error; orelse is > specified as "stmt*". So it must be a list. A list is actually what I want, an empty one. The problem is that ast.While and ast.If's constructors default to the opposite, orelse=None. Same with keywords and args for ast.Call. Admittedly, adding orelse=[] to the constructor calls isn't terribly burdensome, but it does make already obfuscated looking AST mangling code even worse. >> -Why can't I eval a functiondef to get back a function object? > > Because a definition is not an expression. You can only eval > expressions. I understand that if function definitions were expressions, because of the whitespace syntax there wouldn't be a way to express an assignment to such an expression. But, why would it be problematic to let them be expressions anyway? >> -The module provides no means to convert an AST back into source code, >> which would be nice for debugging. > > See Demo/parser/unparse.py Thanks :) >> -It would be nice if decorators were passed a function's AST instead >> of a function object. > > How could this possibly work? If you run from a pyc file, there will > be no AST available to pass. I hadn't thought about bytecode compilation. In addition to the other suggestions you would have to change it to preserve an AST. Regards, Joe From deets at nospam.web.de Mon Apr 6 02:49:23 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 06 Apr 2009 08:49:23 +0200 Subject: How to go about. On read/write locks In-Reply-To: <888ed340-48bd-414a-b827-8de726dc729c@h28g2000yqd.googlegroups.com> References: <888ed340-48bd-414a-b827-8de726dc729c@h28g2000yqd.googlegroups.com> Message-ID: <73tmvkF10umsnU1@mid.uni-berlin.de> Emanuele D'Arrigo schrieb: > Hi everybody, > > I'm having a threading-related design issue and I suspect it has a > name that I just don't know. Here's a description. > > Let's assume a resource (i.e. a dictionary) that needs to be accessed > by multiple threads. A simple lock will do the job but in some > circumstances it will create an unnecessary bottleneck. I.e. let's > assume that most threads only need to have a -read- access to the > resource, while only few threads actually change the dictionary. > Ideally, the reading threads should not block each other. However, as > soon as a threads intends to change the dictionary it should let all > the reading threads finish, lock the access to the resource, change > it, and then release the lock. > > I don't think it'd be difficult to implement but I'm wondering if > something in this direction has been done already, if it has a name or > if it's even well known design pattern. > > Anybody can shed some light? There are two answers to this - the general, and the CPython-specific. The general is this: if you need reading to finish before you write, you need a lock that guards every access to the dict. There is no "cheap to obtain but only effective for some threads"-lock. The CPython-specific answer is that the GIL takes care of that for you right now anyway. So unless you plan for a distant future where some kind of swallows fly around that don't have a GIL, you are safe to simply read and write in threads without any locking whatsoever. Diez From deets at nospam.web.de Mon Apr 6 03:00:30 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 06 Apr 2009 09:00:30 +0200 Subject: exporting symbols with ctypes? In-Reply-To: References: Message-ID: <73tnkeF10nu3kU1@mid.uni-berlin.de> Brian schrieb: > I'd like to load a library that expects executables which link against > it to provide a particular symbol. Is there a way to do the inverse > of the in_dll() operation? I'd prefer to avoid creating a brand new > library on the fly just to satisfy this one dependency. Maybe elmer can help here: http://elmer.sourceforge.net/ It appears to be a bit dated, but maybe it shows a way how to go about this. Diez From bieffe62 at gmail.com Mon Apr 6 03:30:06 2009 From: bieffe62 at gmail.com (bieffe62 at gmail.com) Date: Mon, 6 Apr 2009 00:30:06 -0700 (PDT) Subject: Killing threads References: <4b52f7d7-81d5-4141-9385-ee8cfb90ac1b@l1g2000yqk.googlegroups.com> <5670fce5-c106-44bf-9152-b196298aa495@c36g2000yqn.googlegroups.com> Message-ID: On Apr 5, 9:48?pm, Dennis Lee Bieber wrote: > On Sun, 05 Apr 2009 12:54:45 +0200, Francesco Bochicchio > declaimed the following in > gmane.comp.python.general: > > > If yor threads are not set as 'deamons' using Thread.setDaemon method, > > then your main program at its termination should call Thread.join for > > each of the thread spawned, otherwise the whole process will not quit. > > ? ? ? ? .join() alone won't do anything but wait for the thread itself to > quit -- which means one still has to signal the threads to commit > suicide. > Yes. Mine was an 'additional suggestion' to the ones that the OP already received. I guests that was not clear enough ... .. follows a nice explanation on methods to stop threads that I was too lazy to write ... > > ? ? ? ? If the thread has the capability to become blocked on some operation > (say a socket read without timeout), none of these solutions will work. > That just leaves setting the threads daemonic at the start -- which > indicates the runtime may brutally kill them when the main program > exits. > You know, this bugger me a little. I know that killing threads is hard in any language (I'm facing now the issue in a C++ program I'm writing at work), expecially doing in a platform-independent way, but Java managed to do it. Now python is in many ways an higher level language than Java, but when it comes to threading I feel it lacks something. I know that often it is not too hard to avoid blocking reads, and you can always use subprocesses that with the new multiprocessing module are almost as easy as threads, but still ... Ciao ----- FB From bieffe62 at gmail.com Mon Apr 6 03:45:47 2009 From: bieffe62 at gmail.com (bieffe62 at gmail.com) Date: Mon, 6 Apr 2009 00:45:47 -0700 (PDT) Subject: Killing threads References: <4b52f7d7-81d5-4141-9385-ee8cfb90ac1b@l1g2000yqk.googlegroups.com> <53ebfff9-448f-438f-aa93-a2187bf13056@f1g2000prb.googlegroups.com> <685a59cd-9f02-483f-bc59-b55091a181f8@u9g2000pre.googlegroups.com> Message-ID: On 6 Apr, 05:25, ericwoodwo... at gmail.com wrote: > On Apr 5, 11:07?pm, Dennis Lee Bieber wrote: > > > > > > > On Sun, 5 Apr 2009 17:27:15 -0700 (PDT), imageguy > > declaimed the following in > > gmane.comp.python.general: > > > > In threading.Event python 2.5 docs say; > > > "This is one of the simplest mechanisms for communication between > > > threads: one thread signals an event and other threads wait for it. " > > > > Again, I have limited experience, however, in my reading of the > > > threading manual and review examples, Events were specifically design > > > to be a thread safe way to communicate a 'state' to running threads ? > > > In the OP's example 'do stuff' was open to wide interpretation, > > > however, if within the thread's main 'while' loop the tread checks to > > > see if the 'keepgoing' Event.isSet(), in what scenario would this > > > create deadlock ? > > > ? ? ? ? If you are going to perform a CPU intensive polling loop, there is > > no sense in using the Event system in the first place... Just create a > > globally accessible flag and set it to true when you want to signal the > > threads (or false if you don't want to use the negation "while not > > flagged: do next processing step") > > > ? ? ? ? Event is optimized for the case wherein threads can WAIT (block) on > > the Event object. > > -- > > ? ? ? ? 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/ > > Well it turns out my problem was with queues not with threads. ?I had > a self.die prop in my thread object that defaults to FALSE and that I > set to true when i wanted the thread to die. ?then my loop would be > while not die: ?It seemed pretty simple so I didn't know why it was > failing. ?What I didn't know, because I'm quite new to python, is that > queue.get was blocking. ?So my producer thread why dying immediately > but my worker threads were all blocking on their queue.gets. ?So they > were never falling off the loop. ?I changed it to queue.get_nowait() > and added a queue.empty exception and everything worked as expected. > > So I thought I knew what was going on and that I was having a really > esoteric problem when i was actually having a pretty boring problem I > didn't recognize. > > Thanks everybody for the help!> I've gone through that also, when I started with python threads :-) Be aware that using get_nowait may lead to your thread using too much CPU in checking a queue often empty. I tend to use Queue.get with a timeout, smaller enough to keep the thread responsive but large enough not to waste CPU in too-frequent checks. Ciao ----- FB From hyperboreean at nerdshack.com Mon Apr 6 04:01:04 2009 From: hyperboreean at nerdshack.com (hyperboreean) Date: Mon, 06 Apr 2009 11:01:04 +0300 Subject: decorators don't play nice with nose? Message-ID: <49D9B6C0.2050608@nerdshack.com> Hi, I am trying to test the business part of a web service. For this I am using unittest & nose. I wrote a decorator that should handle the xml test file retrieval, but it seems I can't get it working with nose. Here's the code: * MyApp.py -- base test class * import os import unittest from MyApp.Core import XmlParser __all__ = ['MyAppTest', 'setup'] PATH = os.path.dirname(__file__) or '' class setup(object): """Decorator to ease the use of xml files in MyApp tests. The way it works it that it decorates a test method which has a first default parameter called 'parser' and it overwrites this parameter value with a XmlParser instance. The xml file should be located under: data/testedBusinessRequest/testMethodName.xml """ def __init__(self, testedBusinessRequest = ''): self.testedBusinessRequest =\ testedBusinessRequest.lower() def _getXmlParser(self, xml): documentElement = XmlParser.parseXmlStream(xml) parser = XmlParser.getParser(documentElement) return parser def __call__(self, method): # TODO: error handling here methodName = method.func_code.co_name methodName = methodName.split('_')[1] xmlFolder = self.testedBusinessRequest xmlFile = '%s.xml' % methodName path = os.path.join(PATH, 'data', xmlFolder, xmlFile) f = open(path) xml = f.read() f.close() method.func_defaults = (self._getXmlParser(xml),) return method class MyAppTest(unittest.TestCase): def setUp(self): self.database = Database() def tearDown(self): pass * test_Login.py - test a business request * from MyAppTest import MyAppTest, setup from MyApp import Login class TestLogin(MyAppTest): testedBusinessRequest = 'Login' @setup(testedBusinessRequest) def test_validParameters(self, parser = None): response = Login(self.database, parser).run() return True Ok, so the decorator setup should fill the parser parameter with a XmlParser object. This works well if I add a __main__ and use unittest to run the tests. But if I use nose, I get the following error: *TypeError: unbound method __call__() must be called with setup instance as first argument (got module instance instead)* Any advices? Thanks! From mailing at supai.de Mon Apr 6 04:16:29 2009 From: mailing at supai.de (Wolfgang Forstmeier) Date: Mon, 06 Apr 2009 10:16:29 +0200 Subject: py2exe problem In-Reply-To: References: Message-ID: On 03.04.2009 15:58, Dave Angel wrote: > > > Wolfgang Forstmeier wrote: >> >> >> >>> >>> Ok, but do you really use idlelib for something? Or it's just some >>> random code you found somewhere and drop into your application? >> >> Ah yes, I really use this. I create some message boxes for a little >> GUI application that controls some other program with COM. >> Running my app without py2exe, just with python, there is no warning >> at all. This comes in with py2exe first. >> >> Here some piece of code that I use for tkMessageBox. >> >> from idlelib.OutputWindow import tkMessageBox >> >> ... >> # Define about message box >> def about(self): >> tkMessageBox.showinfo("About", "My little about text box.") >> # -- >> ... >> >> There is some more GUI programming arround that def in my class, but >> that uses only Tkinter, should not be interesting for that error. >> >> > So why not use tkMessageBox directly, and skip Idle's namespace wrapping? > > import tkMessageBox > > tkMessageBox.showinfo("About", "My little about text box.") > Hey Dave, thanks a lot, that did the job, because I really does not use Idle in any other place. From mail at microcorp.co.za Mon Apr 6 04:27:31 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Mon, 6 Apr 2009 10:27:31 +0200 Subject: Need advise about an application References: <6b74d269-6ef5-4483-a492-de397c2e156c@f19g2000yqh.googlegroups.com> <38e2ebff-cb6e-44d5-8a0f-02011a136d7d@v19g2000yqn.googlegroups.com> Message-ID: <010a01c9b693$ae03c460$0d00a8c0@Hendrik> "azrael" wrote: > I guess that this is not an option because of the case that the > calculation of the needed statistics takes not always the same time > nad I am afraid tht using sleep() would after a couple of time periods > skip a meassurement. If I understand correctly what you are attempting, I would use three threads: one to do the serial stuff - it is responsible for getting a slug of data in, checking for errors, doing protocol stuff like re tries, etc. One to store the slug in the database, making sure it is written. One to display the results. Glue them together with queues. If ever you need more performance, make the threads processes, and replace the queues by pipes. - Hendrik From jeremiah.dodds at gmail.com Mon Apr 6 04:46:10 2009 From: jeremiah.dodds at gmail.com (Jeremiah Dodds) Date: Mon, 6 Apr 2009 09:46:10 +0100 Subject: Python2.4 and HTTPS In-Reply-To: <715817.45271.qm@web35908.mail.mud.yahoo.com> References: <715817.45271.qm@web35908.mail.mud.yahoo.com> Message-ID: <12cbbbfc0904060146o75cf064eueda264d1ac9664cf@mail.gmail.com> On Sun, Apr 5, 2009 at 8:17 AM, Good Z wrote: > Dear all, > I am using Python 2.4.3 for my project. We need to use HTTPS with > python2.4.3 unfortunately it seems httplib is not working fine for me. Below > is small code that works well with Python2.6.1 but not with Python2.4.3. > Unfortunately its not possible for me to move away from Python 2.4.3. Would > request if anyone has any idea how to make this programs work in > Python2.4.3. > > import httplib > conn1 = httplib.HTTPSConnection(WEBSITE_ADDRESS) > conn1.putrequest('GET', RELATIVE_ADDR) > conn1.putheader('Connection', 'close') > conn1.endheaders() > > r1 = conn1.getresponse() > > data1 = r1.read() > print data1 > conn1.close() > > One executing the above program, it crash while reading the response and > gives following error: > Traceback (most recent call last): > File "", line 1, in > r1 = conn1.getresponse() > File "C:\Python25\lib\httplib.py", line 928, in getresponse > response.begin() > File "C:\Python25\lib\httplib.py", line 385, in begin > version, status, reason = self._read_status() > File "C:\Python25\lib\httplib.py", line 349, in _read_status > raise BadStatusLine(line) > BadStatusLine > > Any help would be appreciated. > > Best Regards, > Mike. > > > -- > http://mail.python.org/mailman/listinfo/python-list > > You may be running into http://bugs.python.org/issue5007 - if you could post the url of the site you're trying to open, I can test to see if it works in 2.5/2.6 (It won't if you're running into that bug). -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Mon Apr 6 05:03:16 2009 From: __peter__ at web.de (Peter Otten) Date: Mon, 06 Apr 2009 11:03:16 +0200 Subject: Example for readline module usage? References: Message-ID: Grant Edwards wrote: > [I swear I've asked this question before, but Google can't find > it.] My Google is better than yours then: http://mail.python.org/pipermail/python-list/2008-July/669582.html Peter From werner.bruhin at free.fr Mon Apr 6 05:11:37 2009 From: werner.bruhin at free.fr (Werner F. Bruhin) Date: Mon, 06 Apr 2009 11:11:37 +0200 Subject: logging - string formating problems In-Reply-To: <49D8EA92.9080704@free.fr> References: <49D8EA92.9080704@free.fr> Message-ID: <49D9C749.4060101@free.fr> Werner F. Bruhin wrote: > I see the following exception with a string formating problem. > > TypeError: not all arguments converted during string formatting > Traceback (most recent call last): > File "/usr/lib/python2.5/logging/__init__.py", line 744, in emit > msg = self.format(record) > File "/usr/lib/python2.5/logging/__init__.py", line 630, in format > return fmt.format(record) > File "/usr/lib/python2.5/logging/__init__.py", line 418, in format > record.message = record.getMessage() > File "/usr/lib/python2.5/logging/__init__.py", line 288, in getMessage > msg = msg % self.args > > The exception does not give any information on where the problem is > coming from. > > I am using Python 2.5.4 but I see that in 2.6 the code is still the same. > > Any chance that getMessage could catch this exception and provide > better debugging information (i.e. content of msg and self.args). I understand that my problem is that the arguments don't match the format. But currently the traceback is not of any help in figuring out where this in my code this is. So, I suggest that line 288 in getMessage is changed from: msg = msg % self.args To something along these lines: if self.args: try: msg = msg % self.args except: print 'msg: %s' % msg print 'args: %s' % self.args traceback.print_exception(ei[0], ei[1], ei[2]) return msg Werner From hniksic at xemacs.org Mon Apr 6 05:27:54 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Mon, 06 Apr 2009 11:27:54 +0200 Subject: How to free /destroy object created by PyTuple_New References: <3c15a8d6-3095-4fff-b902-8bef22542b86@a5g2000pre.googlegroups.com> <0aa96d3a-f646-4f98-b8c9-934985411917@v6g2000vbb.googlegroups.com> <0f67973b-80dd-47c7-926b-989dec9de358@f1g2000prb.googlegroups.com> <871vs7leyt.fsf@busola.homelinux.net> <95ac8db7-23c3-4436-b786-a980a7f07c2b@d25g2000prn.googlegroups.com> Message-ID: <87iqlif7xh.fsf@busola.homelinux.net> grbgooglefan writes: > Regarding PyTuple_New, when I pass this tuple with variable values > set to some evaluation function like PyObject_CallObject, do I need > to increment reference for this tuple & then decrement again after > the call returns? You don't. It is assumed that you already own the reference to the tuple, so you can freely pass it to functions. When you're done with the tuple (such as when you're about to leave the C scope that holds the reference to it), you're suppose to decref it. If the function you call stores it somewhere, that function is responsible for incrementing its reference count. The exception to the above are functions documented to "steal" the reference count of their arguments, such as PyList_SetItem, but those are generally quite rare and always clearly documented. Maybe you should post a question to the capi-sig list explaining what you're trying to do; perhaps there's a better way to do it. For example, maybe you don't need to create a tuple and then call PyObject_CallObject, but simply use PyObject_CallFunction, which will create the tuple for you. From scorpion032 at gmail.com Mon Apr 6 06:07:30 2009 From: scorpion032 at gmail.com (Lakshman) Date: Mon, 6 Apr 2009 03:07:30 -0700 (PDT) Subject: HTTP Authentication Message-ID: <16be85f6-2a94-4509-9f06-4982790b36f6@b7g2000pre.googlegroups.com> Whats is the python urllib2 equivallent of curl -u username:password status="abcd" http://example.com/update.json I did this: handle = urllib2.Request(url) authheader = "Basic %s" % base64.encodestring('%s:%s' % (username, password)) handle.add_header("Authorization", authheader) Is there a better / simpler way? From nigamreetesh84 at gmail.com Mon Apr 6 06:15:49 2009 From: nigamreetesh84 at gmail.com (reetesh nigam) Date: Mon, 6 Apr 2009 03:15:49 -0700 (PDT) Subject: set python default encoding Message-ID: <35d793ed-52ca-4236-bd72-8840772cec52@f41g2000pra.googlegroups.com> Hi All, I am unable to set the python default encoding. i used the following proccess to set the python encoding import sys reload(sys) sys.setdefaultencoding('latin-1') but it is giving me the same error : args = ('utf8', "MEDICINE '\xc4 ", 10, 12, 'invalid data', >) encoding = 'utf8' end = 12 message = '' object = "MEDICINE '\xc4 " reason = 'invalid data' start = 10 Please tell me how to solve this problem. Thanks and Regards Reetesh Nigam From steven at REMOVE.THIS.cybersource.com.au Mon Apr 6 06:29:01 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 06 Apr 2009 10:29:01 GMT Subject: logging - string formating problems References: <49D8EA92.9080704@free.fr> Message-ID: On Mon, 06 Apr 2009 11:11:37 +0200, Werner F. Bruhin wrote: > Werner F. Bruhin wrote: >> I see the following exception with a string formating problem. >> >> TypeError: not all arguments converted during string formatting >> Traceback (most recent call last): >> File "/usr/lib/python2.5/logging/__init__.py", line 744, in emit >> msg = self.format(record) >> File "/usr/lib/python2.5/logging/__init__.py", line 630, in format >> return fmt.format(record) >> File "/usr/lib/python2.5/logging/__init__.py", line 418, in format >> record.message = record.getMessage() >> File "/usr/lib/python2.5/logging/__init__.py", line 288, in getMessage >> msg = msg % self.args >> >> The exception does not give any information on where the problem is >> coming from. I'm pretty sure it does. Are you sure that you are including the *entire* traceback? Because judging from the four entries given, it looks like the error is originating from logging.__init__ itself, and not from your application itself. Weird huh? Oh wait... is this error occurring when you exit your application? Possibly this is the logging shutdown handler failing? I'm kind of whistling in the dark here. Perhaps this recipe can help you get more information from the tracebacks: http://code.activestate.com/recipes/52215/ Good luck, and let us know what the problem was. >> I am using Python 2.5.4 but I see that in 2.6 the code is still the >> same. >> >> Any chance that getMessage could catch this exception and provide >> better debugging information (i.e. content of msg and self.args). *shrug* You could always request a feature enhancement on the Python bug list. > I understand that my problem is that the arguments don't match the > format. But currently the traceback is not of any help in figuring out > where this in my code this is. > > So, I suggest that line 288 in getMessage is changed from: > > msg = msg % self.args > > To something along these lines: > if self.args: > try: > msg = msg % self.args > except: > print 'msg: %s' % msg > print 'args: %s' % self.args > traceback.print_exception(ei[0], ei[1], ei[2]) > return msg Patches will increase the likelihood of the enhancement being accepted, but I suspect this is too *specific* to be accepted. -- Steven From manu3d at gmail.com Mon Apr 6 06:30:28 2009 From: manu3d at gmail.com (Emanuele D'Arrigo) Date: Mon, 6 Apr 2009 03:30:28 -0700 (PDT) Subject: How to go about. On read/write locks References: <888ed340-48bd-414a-b827-8de726dc729c@h28g2000yqd.googlegroups.com> <73tmvkF10umsnU1@mid.uni-berlin.de> Message-ID: <81869878-f2cc-4468-9916-a38f498c675d@h28g2000yqd.googlegroups.com> On Apr 6, 7:49?am, "Diez B. Roggisch" wrote: > The CPython-specific answer is that the GIL takes care of that for you > right now anyway. So unless you plan for a distant future where some > kind of swallows fly around that don't have a GIL, you are safe to > simply read and write in threads without any locking whatsoever. Diez, thanks for your reply. I didn't know what the GIL is. I did some research finding an interesting article that did clarify many multi- threading related concepts and issues: http://jessenoller.com/2009/02/01/python-threads-and-the-global-interpreter-lock/ Python's approach with the GIL is both reasonable and disappointing. Reasonable because I understand how it can make things easier for its internals. Disappointing because it means that standard python cannot take advantage of the parallelism that can more and more often be afforded by today's computers. I.e. I found only recently, almost by chance, that my wife's laptop has not one but two processors, even though it isn't a particularly high-end computer. I now understand that OS-level threading does use them both, but I understand that the GIL effectively prevents parallel operations. (Am I understanding correctly?) I do not completely understand your statement in the context of my original example though, the shared dictionary. As the GIL is released every X bytecode operations surely it can happen that as the dictionary is iterated through, i.e. in a for/in loop, a different thread might change it, with potentially catastrophic consequences. The GIL wouldn't be able to prevent this, wouldn't it? Manu From jelleferinga at gmail.com Mon Apr 6 06:40:19 2009 From: jelleferinga at gmail.com (jelle) Date: Mon, 6 Apr 2009 03:40:19 -0700 (PDT) Subject: group several methods under a attribute Message-ID: Hi, I'm working on a pretty large class and I'd like to group several methods under a attribute. Its not convenient to chop up the class in several smaller classes, nor would mixins really solve the issue. So, what is a pythonic way of grouping several methods under a attribute? Many thanks in advance, -jelle From kay.schluehr at gmx.net Mon Apr 6 06:40:37 2009 From: kay.schluehr at gmx.net (Kay Schluehr) Date: Mon, 6 Apr 2009 03:40:37 -0700 (PDT) Subject: Painful?: Using the ast module for metaprogramming References: Message-ID: <483c972d-d5a3-4a2b-8e5e-110d606aee8d@o11g2000yql.googlegroups.com> > -It would be nice if decorators were passed a function's AST instead > of a function object. As it is I have to use inspect.getsource to > retrieve the source for the function in question, and then use > ast.parse, which is a bit inefficient because the cpython parser has > to already have done this once before. It doesn't matter that much though because the Python parser is very efficient and the decorator is applied only once. The PyGPU project used this approach when I remember it correctly: http://www.cs.lth.se/home/Calle_Lejdfors/pygpu/ From __peter__ at web.de Mon Apr 6 07:31:43 2009 From: __peter__ at web.de (Peter Otten) Date: Mon, 06 Apr 2009 13:31:43 +0200 Subject: logging - string formating problems References: <49D8EA92.9080704@free.fr> Message-ID: Werner F. Bruhin wrote: > Werner F. Bruhin wrote: >> I see the following exception with a string formating problem. >> >> TypeError: not all arguments converted during string formatting >> Traceback (most recent call last): >> File "/usr/lib/python2.5/logging/__init__.py", line 744, in emit >> msg = self.format(record) >> File "/usr/lib/python2.5/logging/__init__.py", line 630, in format >> return fmt.format(record) >> File "/usr/lib/python2.5/logging/__init__.py", line 418, in format >> record.message = record.getMessage() >> File "/usr/lib/python2.5/logging/__init__.py", line 288, in getMessage >> msg = msg % self.args >> >> The exception does not give any information on where the problem is >> coming from. >> >> I am using Python 2.5.4 but I see that in 2.6 the code is still the same. >> >> Any chance that getMessage could catch this exception and provide >> better debugging information (i.e. content of msg and self.args). > I understand that my problem is that the arguments don't match the > format. But currently the traceback is not of any help in figuring out > where this in my code this is. > > So, I suggest that line 288 in getMessage is changed from: > > msg = msg % self.args > > To something along these lines: > if self.args: > try: > msg = msg % self.args > except: > print 'msg: %s' % msg > print 'args: %s' % self.args > traceback.print_exception(ei[0], ei[1], ei[2]) > return msg I would be more interested in the origin of the problem in my code. For that you need the complete traceback. You might be able to find it by monkey-patching the Handler.handlError() method, e. g.: import logging def handleError(self, record): raise logging.Handler.handleError = handleError logging.critical("", 42) Of course you'd only do that while you are debugging the app. Peter From gh at ghaering.de Mon Apr 6 07:41:10 2009 From: gh at ghaering.de (=?ISO-8859-1?Q?Gerhard_H=E4ring?=) Date: Mon, 06 Apr 2009 13:41:10 +0200 Subject: HTTP Authentication In-Reply-To: <16be85f6-2a94-4509-9f06-4982790b36f6@b7g2000pre.googlegroups.com> References: <16be85f6-2a94-4509-9f06-4982790b36f6@b7g2000pre.googlegroups.com> Message-ID: Lakshman wrote: > Whats is the python urllib2 equivallent of > > curl -u username:password status="abcd" http://example.com/update.json > > I did this: > > handle = urllib2.Request(url) > authheader = "Basic %s" % base64.encodestring('%s:%s' % (username, > password)) > handle.add_header("Authorization", authheader) > > Is there a better / simpler way? Better? Yes. Simpler? No. Actually, the proper way using the urllib2 API is more code. When I need it some time ago, I googled and used this recipe: http://www.voidspace.org.uk/python/articles/urllib2.shtml#id6 # create a password manager password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() # Add the username and password. # If we knew the realm, we could use it instead of ``None``. top_level_url = "http://example.com/foo/" password_mgr.add_password(None, top_level_url, username, password) handler = urllib2.HTTPBasicAuthHandler(password_mgr) # create "opener" (OpenerDirector instance) opener = urllib2.build_opener(handler) # use the opener to fetch a URL opener.open(a_url) # Install the opener. # Now all calls to urllib2.urlopen use our opener. urllib2.install_opener(opener) -- Gerhard From piet at cs.uu.nl Mon Apr 6 07:44:37 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 06 Apr 2009 13:44:37 +0200 Subject: How to go about. On read/write locks References: <888ed340-48bd-414a-b827-8de726dc729c@h28g2000yqd.googlegroups.com> Message-ID: >>>>> "Emanuele D'Arrigo" (ED) wrote: >ED> Hi everybody, >ED> I'm having a threading-related design issue and I suspect it has a >ED> name that I just don't know. Here's a description. >ED> Let's assume a resource (i.e. a dictionary) that needs to be accessed >ED> by multiple threads. A simple lock will do the job but in some >ED> circumstances it will create an unnecessary bottleneck. I.e. let's >ED> assume that most threads only need to have a -read- access to the >ED> resource, while only few threads actually change the dictionary. >ED> Ideally, the reading threads should not block each other. However, as >ED> soon as a threads intends to change the dictionary it should let all >ED> the reading threads finish, lock the access to the resource, change >ED> it, and then release the lock. >ED> I don't think it'd be difficult to implement but I'm wondering if >ED> something in this direction has been done already, if it has a name or >ED> if it's even well known design pattern. >ED> Anybody can shed some light? This is a classical synchronization problem with a classical solution: You treat the readers as a group, and the writers individually. So you have a write lock that each writer has to acquire and release, but it is acquired only by the first reader and released by the last one. Therefore you need a counter of the number of readers, and manipulations of this counter must be protected by another lock. #------------------------------------------------------------------------ from threading import Lock mutex = Lock() writelock = Lock() numreaders = 0 #------------------------------------------------------------------------ # Reader code: mutex.acquire() numreaders += 1 if numreaders == 1: writelock.acquire() mutex.release() ## critical section for reader mutex.acquire() numreaders -= 1 if numreaders == 0: writelock.release() mutex.release() #------------------------------------------------------------------------ # Writer code: writelock.acquire() ## critical section for writer writer.release #------------------------------------------------------------------------ Notes: 1. From Python 2.6 on you can use the with statement, which makes it more robust against exceptions: with mutex: numreaders += 1 if numreaders == 1: writelock.acquire() etc. In Python 2.5 you can also use this if you use: from __future__ import with_statement 2. The code above can cause starvation for writers if there are many readers (or if new readers come in before all other readers have finished. You need at least one more lock and probably a writer counter to solve this. 3. See also http://code.activestate.com/recipes/465156/ 4. The code has not been tested, not even for syntax errors. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From sumerc at gmail.com Mon Apr 6 08:13:33 2009 From: sumerc at gmail.com (k3xji) Date: Mon, 6 Apr 2009 05:13:33 -0700 (PDT) Subject: C API String Parsing/Returning Message-ID: <73a778c4-fe3f-424c-a407-67db8cb6a766@o11g2000yql.googlegroups.com> Hi all, This might be a newbie question. I am trying to implement a simple string decoder/encoder algorithm. Just suppose I am substrcating some values from the string passed as a parameter to the function and I want the function to return encoded/decoded version of the string. Here is the call: ss= esauth.penc('s') st = esauth.pdec(ss) static PyObject * pdec(PyObject *self, PyObject *args) { unsigned char *s= NULL; unsigned int v,len,i = 0; if (!PyArg_ParseTuple(args, "s", &s)) return NULL; if (!s) return NULL; len = strlen(s); for(i=0;i 10) s[i] = s[i] - 10; } return Py_BuildValue("s",s); } This is returning the original string. I mean the parameter is changed but the Py_BuildValue is returning the original string passed in as param. have dealt with another nmore complex extension and because of the same string handling problems, I just stop implementing it. Can somebody please briefly explain the gotchas in Python's string handling and returning values, cause I am having real trouble with them. Thanks, From sumerc at gmail.com Mon Apr 6 08:18:10 2009 From: sumerc at gmail.com (k3xji) Date: Mon, 6 Apr 2009 05:18:10 -0700 (PDT) Subject: C API String Parsing/Returning References: <73a778c4-fe3f-424c-a407-67db8cb6a766@o11g2000yql.googlegroups.com> Message-ID: <48367c36-2e6e-4083-90fb-d857c3647724@y13g2000yqn.googlegroups.com> Sorry, Here is the correct output: >>> ss= esauth.penc('s') >>> print ss ? >>> esauth.pdec(ss) '\xb9' >>> print ss s --> Works fine!!! >>> ss= esauth.penc('s') >>> print ss s >>> ss = esauth.pdec(ss) >>> print ss ? --> how did this happen if the param and return values are same? I cannot understand this. Something has todo with ref counts but I don't understand the problem. >>> On Apr 6, 3:13?pm, k3xji wrote: > Hi all, > > This might be a newbie question. I am trying to implement a simple > string decoder/encoder algorithm. Just suppose I am substrcating some > values from the string passed as a parameter to the function and I > want the function to return encoded/decoded version of the string. > > Here is the call: > ss= esauth.penc('s') > st = esauth.pdec(ss) > > static PyObject * > pdec(PyObject *self, PyObject *args) > { > ? ? ? ? unsigned char *s= NULL; > > ? ? ? ? unsigned int v,len,i = 0; > > ? ? ? ? if (!PyArg_ParseTuple(args, "s", &s)) > ? ? ? ? return NULL; > ? ? ? ? if (!s) > ? ? ? ? ? ? ? ? return NULL; > > ? ? ? ? len = strlen(s); > > ? ? ? ? for(i=0;i ? ? ? ? ? ? ? ? if (s[i] > 10) > ? ? ? ? ? ? ? ? ? ? s[i] = s[i] - 10; > ? ? ? ? } > > ? ? ? ? return Py_BuildValue("s",s); > > } > > This is returning the original string. I mean the parameter is changed > but the Py_BuildValue is returning the original string passed in as > param. > > ?have dealt with another nmore complex extension and because of the > same string handling problems, I just stop implementing it. Can > somebody please briefly explain the gotchas in Python's string > handling and returning values, cause I am having real trouble with > them. > > Thanks, From ericwoodworth at gmail.com Mon Apr 6 08:23:47 2009 From: ericwoodworth at gmail.com (ericwoodworth at gmail.com) Date: Mon, 6 Apr 2009 05:23:47 -0700 (PDT) Subject: Killing threads References: <4b52f7d7-81d5-4141-9385-ee8cfb90ac1b@l1g2000yqk.googlegroups.com> <53ebfff9-448f-438f-aa93-a2187bf13056@f1g2000prb.googlegroups.com> <685a59cd-9f02-483f-bc59-b55091a181f8@u9g2000pre.googlegroups.com> Message-ID: <2bbee5f8-35cc-4acf-8011-b4144dad701b@r33g2000yqn.googlegroups.com> On Apr 6, 3:45?am, bieff... at gmail.com wrote: > On 6 Apr, 05:25, ericwoodwo... at gmail.com wrote: > > > > > On Apr 5, 11:07?pm, Dennis Lee Bieber wrote: > > > > On Sun, 5 Apr 2009 17:27:15 -0700 (PDT), imageguy > > > declaimed the following in > > > gmane.comp.python.general: > > > > > In threading.Event python 2.5 docs say; > > > > "This is one of the simplest mechanisms for communication between > > > > threads: one thread signals an event and other threads wait for it. " > > > > > Again, I have limited experience, however, in my reading of the > > > > threading manual and review examples, Events were specifically design > > > > to be a thread safe way to communicate a 'state' to running threads ? > > > > In the OP's example 'do stuff' was open to wide interpretation, > > > > however, if within the thread's main 'while' loop the tread checks to > > > > see if the 'keepgoing' Event.isSet(), in what scenario would this > > > > create deadlock ? > > > > ? ? ? ? If you are going to perform a CPU intensive polling loop, there is > > > no sense in using the Event system in the first place... Just create a > > > globally accessible flag and set it to true when you want to signal the > > > threads (or false if you don't want to use the negation "while not > > > flagged: do next processing step") > > > > ? ? ? ? Event is optimized for the case wherein threads can WAIT (block) on > > > the Event object. > > > -- > > > ? ? ? ? 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/ > > > Well it turns out my problem was with queues not with threads. ?I had > > a self.die prop in my thread object that defaults to FALSE and that I > > set to true when i wanted the thread to die. ?then my loop would be > > while not die: ?It seemed pretty simple so I didn't know why it was > > failing. ?What I didn't know, because I'm quite new to python, is that > > queue.get was blocking. ?So my producer thread why dying immediately > > but my worker threads were all blocking on their queue.gets. ?So they > > were never falling off the loop. ?I changed it to queue.get_nowait() > > and added a queue.empty exception and everything worked as expected. > > > So I thought I knew what was going on and that I was having a really > > esoteric problem when i was actually having a pretty boring problem I > > didn't recognize. > > > Thanks everybody for the help!> > > I've gone through that also, when I started with python threads :-) > Be aware that using get_nowait may lead to your thread using too much > CPU in checking a queue often empty. I tend to use ?Queue.get with a > timeout, smaller enough to keep the thread responsive but large enough > not > to waste CPU in too-frequent checks. > > Ciao > ----- > FB Ok thanks - good to now. I'm trying to throttle it with a 1/2 sec sleep statement in the loop but I might just have the "main" loop toss some stuff on that queue as another solution. I'm still kicking it around From gh at ghaering.de Mon Apr 6 08:44:28 2009 From: gh at ghaering.de (=?ISO-8859-1?Q?Gerhard_H=E4ring?=) Date: Mon, 06 Apr 2009 14:44:28 +0200 Subject: group several methods under a attribute In-Reply-To: References: Message-ID: jelle wrote: > Hi, > > I'm working on a pretty large class Can you describe what its purpose is? > and I'd like to group several methods under a attribute. That doesn't work in Python without bending the Python. > Its not convenient to chop up the class in several smaller classes, But that's almost certainly the right thing to do. > nor would mixins really solve the issue. > So, what is a pythonic way of grouping several methods under a > attribute? Whatever it is, you should find a better way instead of cramming everything into a single class. That smells of the God Object antipattern (http://en.wikipedia.org/wiki/God_object). -- Gerhard From gh at ghaering.de Mon Apr 6 08:57:01 2009 From: gh at ghaering.de (=?ISO-8859-1?Q?Gerhard_H=E4ring?=) Date: Mon, 06 Apr 2009 14:57:01 +0200 Subject: C API String Parsing/Returning In-Reply-To: <73a778c4-fe3f-424c-a407-67db8cb6a766@o11g2000yql.googlegroups.com> References: <73a778c4-fe3f-424c-a407-67db8cb6a766@o11g2000yql.googlegroups.com> Message-ID: k3xji wrote: > Hi all, > > This might be a newbie question. I am trying to implement a simple > string decoder/encoder algorithm. Just suppose I am substrcating some > values from the string passed as a parameter to the function and I > want the function to return encoded/decoded version of the string. > > Here is the call: > ss= esauth.penc('s') > st = esauth.pdec(ss) > > static PyObject * > pdec(PyObject *self, PyObject *args) > { > unsigned char *s= NULL; > > unsigned int v,len,i = 0; > > if (!PyArg_ParseTuple(args, "s", &s)) > return NULL; > if (!s) > return NULL; These two lines are superfluous. s now points to the contents of the Python string (which must not contain any 0 characters, else a TypeError is raised instead). Python strings are immutable, so you should *not modify this C string*. > len = strlen(s); > > for(i=0;i if (s[i] > 10) > s[i] = s[i] - 10; > } > > return Py_BuildValue("s",s); > } > > > This is returning the original string. I mean the parameter is changed > but the Py_BuildValue is returning the original string passed in as > param. [...] Yes, that's because you're returning a Python string from the string passed in ;-) You should do something else instead: char* buf = strdup(s); if (!buf) { PyErr_SetString(PyExc_MemoryError, "Out of memory: strdup failed"); return NULL; } /* TODO: your string manipulation */ return PyString_FromString(buf); /* return Py_BuildValue("s", buf); */ If you want to cope with Python strings that may contain 0 bytes, parse them with "s#" instead. This should normally be better because you avoid the strlen() this way. HTH -- Gerhard From werner.bruhin at free.fr Mon Apr 6 08:58:34 2009 From: werner.bruhin at free.fr (Werner F. Bruhin) Date: Mon, 06 Apr 2009 14:58:34 +0200 Subject: logging - string formating problems In-Reply-To: <49D8EA92.9080704@free.fr> References: <49D8EA92.9080704@free.fr> Message-ID: <49D9FC7A.8050105@free.fr> I am fully aware that the problem is in my code, however as getMessage in logging.__init__.py does not catch the exception it is pretty difficult to find the problem without manually inspecting any logging.something statements. My hack of logging.py is really a hack and I know that this can not be used a patch but I hope that someone with more know how then me knows how to improve the situation. BTW, using the below hack was enough for me to mind the bad code in my stuff, but it shouldn't have been this difficult. Traceback (most recent call last): File "C:\Python25\lib\logging\__init__.py", line 750, in emit msg = self.format(record) File "C:\Python25\lib\logging\__init__.py", line 636, in format return fmt.format(record) File "C:\Python25\lib\logging\__init__.py", line 424, in format record.message = record.getMessage() File "C:\Python25\lib\logging\__init__.py", line 288, in getMessage msg = msg % self.args TypeError: not all arguments converted during string formatting Then I hack logging.py and change line 288 in getMessage from: msg = msg % self.args to: try: msg = msg % self.args except: print 'msg: %s' % msg print 'args: %s' % self.args print traceback.format_exc() I get the following which helps a lot more in finding were in my code I have the problem: msg: ping_min: 1 args: replace Traceback (most recent call last): File "C:\Python25\lib\logging\__init__.py", line 290, in getMessage msg = msg % self.args TypeError: not all arguments converted during string formatting As mentioned above, I know the above code is not good enough at all, but I hope that maybe Vinay Sajip or someone else with more know how then I have can come up with a patch which will make this type of situation easier. Werner From chris at simplistix.co.uk Mon Apr 6 09:00:18 2009 From: chris at simplistix.co.uk (Chris Withers) Date: Mon, 06 Apr 2009 14:00:18 +0100 Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: <49D669AA.6080001@v.loewis.de> References: <49D4DA72.60401@v.loewis.de> <49D51A16.70804@simplistix.co.uk> <49D669AA.6080001@v.loewis.de> Message-ID: <49D9FCE2.4070805@simplistix.co.uk> Martin v. L?wis wrote: > Chris Withers wrote: >> Martin v. L?wis wrote: >>> I propose the following PEP for inclusion to Python 3.1. >>> Please comment. >> Would this support the following case: >> >> I have a package called mortar, which defines useful stuff: >> >> from mortar import content, ... >> >> I now want to distribute large optional chunks separately, but ideally >> so that the following will will work: >> >> from mortar.rbd import ... >> from mortar.zodb import ... >> from mortar.wsgi import ... >> >> Does the PEP support this? > > That's the primary purpose of the PEP. Are you sure? Does the pep really allow for: from mortar import content from mortar.rdb import something ...where 'content' is a function defined in mortar/__init__.py and 'something' is a function defined in mortar/rdb/__init__.py *and* the following are separate distributions on PyPI: - mortar - mortar.rdb ...where 'mortar' does not contain 'mortar.rdb'. > You can do this today already > (see the zope package, No, they have nothing but a (functionally) empty __init__.py in the zope package. cheers, Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk From manu3d at gmail.com Mon Apr 6 09:20:29 2009 From: manu3d at gmail.com (Emanuele D'Arrigo) Date: Mon, 6 Apr 2009 06:20:29 -0700 (PDT) Subject: How to go about. On read/write locks References: <888ed340-48bd-414a-b827-8de726dc729c@h28g2000yqd.googlegroups.com> Message-ID: <34977ea2-4d72-43ce-b3cb-a1b605b22840@e5g2000vbe.googlegroups.com> On Apr 6, 12:44?pm, Piet van Oostrum wrote: > 3. See also http://code.activestate.com/recipes/465156/ Thank you for the useful suggestions Piet. In particular I just had a look at the SharedLock class provided through the link above and it seems to fit the bill quite nicely. I'll give it a go! Thank you again! Manu From jnoller at gmail.com Mon Apr 6 09:21:06 2009 From: jnoller at gmail.com (Jesse Noller) Date: Mon, 6 Apr 2009 09:21:06 -0400 Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: <49D52115.6020001@egenix.com> References: <49D4DA72.60401@v.loewis.de> <49D52115.6020001@egenix.com> Message-ID: <4222a8490904060621y36285ca4g73fafe85d4b9c146@mail.gmail.com> On Thu, Apr 2, 2009 at 4:33 PM, M.-A. Lemburg wrote: > On 2009-04-02 17:32, Martin v. L?wis wrote: >> I propose the following PEP for inclusion to Python 3.1. > > Thanks for picking this up. > > I'd like to extend the proposal to Python 2.7 and later. > -1 to adding it to the 2.x series. There was much discussion around adding features to 2.x *and* 3.0, and the consensus seemed to *not* add new features to 2.x and use those new features as carrots to help lead people into 3.0. jesse From barry at python.org Mon Apr 6 09:26:24 2009 From: barry at python.org (Barry Warsaw) Date: Mon, 6 Apr 2009 09:26:24 -0400 Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: <4222a8490904060621y36285ca4g73fafe85d4b9c146@mail.gmail.com> References: <49D4DA72.60401@v.loewis.de> <49D52115.6020001@egenix.com> <4222a8490904060621y36285ca4g73fafe85d4b9c146@mail.gmail.com> Message-ID: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Apr 6, 2009, at 9:21 AM, Jesse Noller wrote: > On Thu, Apr 2, 2009 at 4:33 PM, M.-A. Lemburg wrote: >> On 2009-04-02 17:32, Martin v. L?wis wrote: >>> I propose the following PEP for inclusion to Python 3.1. >> >> Thanks for picking this up. >> >> I'd like to extend the proposal to Python 2.7 and later. >> > > -1 to adding it to the 2.x series. There was much discussion around > adding features to 2.x *and* 3.0, and the consensus seemed to *not* > add new features to 2.x and use those new features as carrots to help > lead people into 3.0. Actually, isn't the policy just that nothing can go into 2.7 that isn't backported from 3.1? Whether the actual backport happens or not is up to the developer though. OTOH, we talked about a lot of things and my recollection is probably fuzzy. Barry -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (Darwin) iQCVAwUBSdoDAXEjvBPtnXfVAQIrPgQAse7BXQfPYHJJ/g3HNEtc0UmZZ9MCNtGc sIoZ2EHRVz+pylZT9fmSmorJdIdFvAj7E43tKsV2bQpo/am9XlL10SMn3k0KLxnF vNCi39nB1B7Uktbnrlpnfo4u93suuEqYexEwrkDhJuTMeye0Cxg0os5aysryuPza mKr5jsqkV5c= =Y9iP -----END PGP SIGNATURE----- From google at mrabarnett.plus.com Mon Apr 6 09:34:13 2009 From: google at mrabarnett.plus.com (MRAB) Date: Mon, 06 Apr 2009 14:34:13 +0100 Subject: logging - string formating problems In-Reply-To: <49D9FC7A.8050105@free.fr> References: <49D8EA92.9080704@free.fr> <49D9FC7A.8050105@free.fr> Message-ID: <49DA04D5.1000802@mrabarnett.plus.com> Werner F. Bruhin wrote: > I am fully aware that the problem is in my code, however as getMessage > in logging.__init__.py does not catch the exception it is pretty > difficult to find the problem without manually inspecting any > logging.something statements. > > My hack of logging.py is really a hack and I know that this can not be > used a patch but I hope that someone with more know how then me knows > how to improve the situation. > > BTW, using the below hack was enough for me to mind the bad code in my > stuff, but it shouldn't have been this difficult. > > Traceback (most recent call last): > File "C:\Python25\lib\logging\__init__.py", line 750, in emit > msg = self.format(record) > File "C:\Python25\lib\logging\__init__.py", line 636, in format > return fmt.format(record) > File "C:\Python25\lib\logging\__init__.py", line 424, in format > record.message = record.getMessage() > File "C:\Python25\lib\logging\__init__.py", line 288, in getMessage > msg = msg % self.args > TypeError: not all arguments converted during string formatting > > > Then I hack logging.py and change line 288 in getMessage from: > msg = msg % self.args > > to: > try: > msg = msg % self.args > except: > print 'msg: %s' % msg > print 'args: %s' % self.args > print traceback.format_exc() > > I get the following which helps a lot more in finding were in my code I > have the problem: > > msg: ping_min: 1 > args: replace > Traceback (most recent call last): > File "C:\Python25\lib\logging\__init__.py", line 290, in getMessage > msg = msg % self.args > TypeError: not all arguments converted during string formatting > > > As mentioned above, I know the above code is not good enough at all, but > I hope that maybe Vinay Sajip or someone else with more know how then I > have can come up with a patch which will make this type of situation > easier. > It looks like what you need is for the module to record the exception in the log you're generating as a "logging error" or some such. BTW, you should catch only TypeError and not use an empty "except": try: msg = msg % self.args except TypeError: msg = 'Logging error: msg is %s, args is %s' % (repr(msg), repr(self.args)) msg += traceback.format_exc() (needs tidying up, of course!) From Hermann at fitzanne.co.za Mon Apr 6 09:42:24 2009 From: Hermann at fitzanne.co.za (Hermann Wehrmeyer) Date: Mon, 6 Apr 2009 15:42:24 +0200 Subject: Newbie: Development plaform Message-ID: Hi, I am looking for an old school friend of mine, Demos Economacos. Are you perhaps the Demos who completed schooling 1979 at Kroonstad SA. Groete/Greetings Hermann Wehrmeyer Tel: 012 342 3710 Fax: 012 342 3775 -------------- next part -------------- An HTML attachment was scrubbed... URL: From lists at cheimes.de Mon Apr 6 09:43:30 2009 From: lists at cheimes.de (Christian Heimes) Date: Mon, 06 Apr 2009 15:43:30 +0200 Subject: C API String Parsing/Returning In-Reply-To: References: <73a778c4-fe3f-424c-a407-67db8cb6a766@o11g2000yql.googlegroups.com> Message-ID: Gerhard H?ring wrote: > char* buf = strdup(s); > if (!buf) { > PyErr_SetString(PyExc_MemoryError, "Out of memory: strdup failed"); > return NULL; > } > > /* TODO: your string manipulation */ Don't forget to free(buf). ;) Christian From sundol at sfc.keio.ac.jp Mon Apr 6 09:48:51 2009 From: sundol at sfc.keio.ac.jp (Hyunchul Kim) Date: Mon, 06 Apr 2009 22:48:51 +0900 Subject: speed of string chunks file parsing Message-ID: <49DA0843.2060906@sfc.keio.ac.jp> Hi, all I have a simple script. Can you improve algorithm of following 10 line script, with a view point of speed ? Following script do exactly what I want but I want to improve the speed. This parse a file and accumulate lines till a line match a given regular expression. Then, when a line match a given regular expression, this function yield lines before the matched lines. **************** import re resultlist = [] cp_regularexpression = re.compile('^a complex regular expression here$) for line in file(inputfile): if cp_regularexpression.match(line): if resultlist != []: yield resultlist resultlist = [] resultlist.append(line) yield resultlist **************** Thank you in advance, Hyunchul From google at mrabarnett.plus.com Mon Apr 6 09:57:38 2009 From: google at mrabarnett.plus.com (MRAB) Date: Mon, 06 Apr 2009 14:57:38 +0100 Subject: speed of string chunks file parsing In-Reply-To: <49DA0843.2060906@sfc.keio.ac.jp> References: <49DA0843.2060906@sfc.keio.ac.jp> Message-ID: <49DA0A52.1080701@mrabarnett.plus.com> Hyunchul Kim wrote: > Hi, all > > I have a simple script. > Can you improve algorithm of following 10 line script, with a view point > of speed ? > Following script do exactly what I want but I want to improve the speed. > > This parse a file and accumulate lines till a line match a given regular > expression. > Then, when a line match a given regular expression, this function yield > lines before the matched lines. > > **************** > import re > resultlist = [] > cp_regularexpression = re.compile('^a complex regular expression here$) > for line in file(inputfile): > if cp_regularexpression.match(line): > if resultlist != []: > yield resultlist > resultlist = [] > resultlist.append(line) > yield resultlist > **************** > > Thank you in advance, > It looks OK to me. Of course, it could be the regular expression that's the slowest part. Have you tried timing it? From james at agentultra.com Mon Apr 6 10:08:03 2009 From: james at agentultra.com (J Kenneth King) Date: Mon, 06 Apr 2009 10:08:03 -0400 Subject: decorators don't play nice with nose? References: Message-ID: <85ljqdg9j0.fsf@agentultra.com> hyperboreean writes: > From: hyperboreean > Subject: decorators don't play nice with nose? > Newsgroups: comp.lang.python > To: python-list at python.org > Date: Mon, 06 Apr 2009 11:01:04 +0300 > > Hi, I am trying to test the business part of a web service. For this I > am using unittest & nose. > I wrote a decorator that should handle the xml test file retrieval, > but it seems I can't get it working with nose. > Here's the code: > > > * MyApp.py -- base test class * > > import os > import unittest > > from MyApp.Core import XmlParser > > > __all__ = ['MyAppTest', 'setup'] > > > PATH = os.path.dirname(__file__) or '' > > > class setup(object): > """Decorator to ease the use of xml files in MyApp tests. > > The way it works it that it decorates a test method which has a first > default parameter called 'parser' and it overwrites this parameter value > with a XmlParser instance. > > The xml file should be located under: > data/testedBusinessRequest/testMethodName.xml > """ > def __init__(self, testedBusinessRequest = ''): > self.testedBusinessRequest =\ > testedBusinessRequest.lower() > > > def _getXmlParser(self, xml): > documentElement = XmlParser.parseXmlStream(xml) > parser = XmlParser.getParser(documentElement) > return parser > > > def __call__(self, method): > > # TODO: error handling here > methodName = method.func_code.co_name > methodName = methodName.split('_')[1] > > xmlFolder = self.testedBusinessRequest > xmlFile = '%s.xml' % methodName > > path = os.path.join(PATH, 'data', > xmlFolder, xmlFile) > > f = open(path) > xml = f.read() > f.close() > method.func_defaults = (self._getXmlParser(xml),) > return method > > > class MyAppTest(unittest.TestCase): > > def setUp(self): > self.database = Database() > > def tearDown(self): > pass > > > * test_Login.py - test a business request * > from MyAppTest import MyAppTest, setup > > from MyApp import Login > > > class TestLogin(MyAppTest): > testedBusinessRequest = 'Login' > > @setup(testedBusinessRequest) > def test_validParameters(self, parser = None): FWIW, nose and unittest both provide methods for setting up and tearing down tests. You should probably look at those first before rolling your own. At the very least it will give you an idea of how yours should work. Cheers From aahz at pythoncraft.com Mon Apr 6 10:32:06 2009 From: aahz at pythoncraft.com (Aahz) Date: 6 Apr 2009 07:32:06 -0700 Subject: Killing threads References: <4b52f7d7-81d5-4141-9385-ee8cfb90ac1b@l1g2000yqk.googlegroups.com> Message-ID: In article , wrote: > >I know that killing threads is hard in any language (I'm facing now >the issue in a C++ program I'm writing at work), expecially doing in a >platform-independent way, but Java managed to do it. That's not my understanding: http://www.roseindia.net/javatutorials/shutting_down_threads_cleanly.shtml -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "...string iteration isn't about treating strings as sequences of strings, it's about treating strings as sequences of characters. The fact that characters are also strings is the reason we have problems, but characters are strings for other good reasons." --Aahz From aahz at pythoncraft.com Mon Apr 6 10:36:00 2009 From: aahz at pythoncraft.com (Aahz) Date: 6 Apr 2009 07:36:00 -0700 Subject: Killing threads References: <4b52f7d7-81d5-4141-9385-ee8cfb90ac1b@l1g2000yqk.googlegroups.com> <53ebfff9-448f-438f-aa93-a2187bf13056@f1g2000prb.googlegroups.com> <685a59cd-9f02-483f-bc59-b55091a181f8@u9g2000pre.googlegroups.com> Message-ID: In article <685a59cd-9f02-483f-bc59-b55091a181f8 at u9g2000pre.googlegroups.com>, imageguy wrote: >Aahz: >> >>For more info, see the slides from my thread tutorial: >>http://pythoncraft.com/OSCON2001/ > >Aahz, thanks for this reference and link to your presentation. At the >risk of highjacking the OP's question, I am bit confused as to how >using an Event would cause a deadlock. You presentation outlines >Events, but doesn't elaborate on the specifics or usage. >In threading.Event python 2.5 docs say; >"This is one of the simplest mechanisms for communication between >threads: one thread signals an event and other threads wait for it. " > >Again, I have limited experience, however, in my reading of the >threading manual and review examples, Events were specifically design >to be a thread safe way to communicate a 'state' to running threads ? >In the OP's example 'do stuff' was open to wide interpretation, >however, if within the thread's main 'while' loop the tread checks to >see if the 'keepgoing' Event.isSet(), in what scenario would this >create deadlock ? Consider two threads trying to pass information to each other. You need two Event objects; unless you are very careful, they can both block on waiting for each other. It's easier if Queue is the only construct you use, it can do everything that the lower-level constructs can do, without needing to figure out the little gotchas -- and you'll need a Queue eventually, so just learn that one. It's all about simplifying. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "...string iteration isn't about treating strings as sequences of strings, it's about treating strings as sequences of characters. The fact that characters are also strings is the reason we have problems, but characters are strings for other good reasons." --Aahz From grkuntzmd at gmail.com Mon Apr 6 10:37:19 2009 From: grkuntzmd at gmail.com (grkuntzmd at gmail.com) Date: Mon, 6 Apr 2009 07:37:19 -0700 (PDT) Subject: Q: "Best" book for teaching Message-ID: <8e3d0032-5e9f-44c2-9380-1d2383552046@u5g2000vbc.googlegroups.com> I am considering teaching an "introduction to programming" course for continuing education adults at a local community college. These would people with no programming experience, but I will require a reasonable facility with computers. What would be a good book to use as the text for the course? Thanks. From invalid at invalid Mon Apr 6 10:38:11 2009 From: invalid at invalid (Grant Edwards) Date: Mon, 06 Apr 2009 09:38:11 -0500 Subject: Example for readline module usage? References: Message-ID: On 2009-04-06, Peter Otten <__peter__ at web.de> wrote: > Grant Edwards wrote: > >> [I swear I've asked this question before, but Google can't find >> it.] > > My Google is better than yours then: > > http://mail.python.org/pipermail/python-list/2008-July/669582.html It certainly is. All I could come up with were tons of hits on the file-object's readline. What I don't understand is how/where the readline module is told what file descriptor it's supposed to be reading from. Is that just implicit in the "import" operation? -- Grant Edwards grante Yow! I'm having BEAUTIFUL at THOUGHTS about the INSIPID visi.com WIVES of smug and wealthy CORPORATE LAWYERS ... From eric at trueblade.com Mon Apr 6 10:40:56 2009 From: eric at trueblade.com (Eric Smith) Date: Mon, 6 Apr 2009 10:40:56 -0400 (EDT) Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: References: <49D4DA72.60401@v.loewis.de> <49D52115.6020001@egenix.com> <4222a8490904060621y36285ca4g73fafe85d4b9c146@mail.gmail.com> Message-ID: <39936.63.251.87.214.1239028856.squirrel@mail.trueblade.com> > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > On Apr 6, 2009, at 9:21 AM, Jesse Noller wrote: > >> On Thu, Apr 2, 2009 at 4:33 PM, M.-A. Lemburg wrote: >>> On 2009-04-02 17:32, Martin v. L?wis wrote: >>>> I propose the following PEP for inclusion to Python 3.1. >>> >>> Thanks for picking this up. >>> >>> I'd like to extend the proposal to Python 2.7 and later. >>> >> >> -1 to adding it to the 2.x series. There was much discussion around >> adding features to 2.x *and* 3.0, and the consensus seemed to *not* >> add new features to 2.x and use those new features as carrots to help >> lead people into 3.0. > > Actually, isn't the policy just that nothing can go into 2.7 that > isn't backported from 3.1? Whether the actual backport happens or not > is up to the developer though. OTOH, we talked about a lot of things > and my recollection is probably fuzzy. I believe Barry is correct. The official policy is "no features in 2.7 that aren't also in 3.1". I personally think I'm not going to put anything else in 2.7, specifically the ',' formatter stuff from PEP 378. 3.1 has diverged too far from 2.7 in this regard to make the backport easy to do. But this decision is left up to the individual committer. From reckoner at gmail.com Mon Apr 6 10:50:13 2009 From: reckoner at gmail.com (Reckoner) Date: Mon, 6 Apr 2009 07:50:13 -0700 (PDT) Subject: Q: "Best" book for teaching References: <8e3d0032-5e9f-44c2-9380-1d2383552046@u5g2000vbc.googlegroups.com> Message-ID: <9614e27e-6faa-457a-a0c9-3e04601467f9@g19g2000yql.googlegroups.com> On Apr 6, 7:37 am, grkunt... at gmail.com wrote: > I am considering teaching an "introduction to programming" course for > continuing education adults at a local community college. These would > people with no programming experience, but I will require a reasonable > facility with computers. > > What would be a good book to use as the text for the course? > > Thanks. QuickPython is pretty good, but might be somewhat above the level you're looking for. Depends on the class. You might want to use it to guide a selection of topics. From reckoner at gmail.com Mon Apr 6 10:53:55 2009 From: reckoner at gmail.com (Reckoner) Date: Mon, 6 Apr 2009 07:53:55 -0700 (PDT) Subject: object knows which object called it? Message-ID: <662de4f7-d8a3-4ed8-bb02-5571e33a3304@k8g2000yqn.googlegroups.com> hi, I have the following problem: I have two objects, say, A and B, which are both legitimate stand-alone objects with lives of their own. A contains B as a property, so I often do A.B.foo() the problem is that some functions inside of B actually need A (remember I said they were both standalone objects), so I have to often do: A.B.foo_func(A) Which is kind of awkward. Is there some way that B.foo_func() could somehow know that it was called as a property of A in this way? Note that I'm looking for the calling object and NOT the calling function. Thanks in advance. From jelleferinga at gmail.com Mon Apr 6 11:03:48 2009 From: jelleferinga at gmail.com (jelle) Date: Mon, 6 Apr 2009 08:03:48 -0700 (PDT) Subject: group several methods under a attribute References: Message-ID: > Whatever it is, you should find a better way instead of cramming > everything into a single class. That smells of the God Object > antipattern (http://en.wikipedia.org/wiki/God_object). Thanks Gerard, I'll take your advice. -jelle From bearophileHUGS at lycos.com Mon Apr 6 11:20:52 2009 From: bearophileHUGS at lycos.com (bearophileHUGS at lycos.com) Date: Mon, 6 Apr 2009 08:20:52 -0700 (PDT) Subject: speed of string chunks file parsing References: Message-ID: Hyunchul Kim: > Following script do exactly what I want but I want to improve the speed. This may be a bit faster, especially if sequences are long (code untested): import re from collections import deque def scanner1(deque=deque): result_seq = deque() cp_regular_expression = re.compile("^a complex regular expression here$") for line in file(inputfile): if cp_regular_expression.match(line) and result_seq: yield result_list result_seq = deque() result_seq.append(line) yield result_seq If the sequences are processed on the fly then you don't need to create new ones and you can clear old ones, this may be a bit faster: def scanner2(deque=deque): result_seq = deque() cp_regular_expression = re.compile("^a complex regular expression here$") for line in file(inputfile): if cp_regular_expression.match(line) and result_seq: yield result_list result_seq.clear() result_seq.append(line) yield result_seq Note that most of the time may be used by the regular expression, often there are ways to speed it up using string methods, even as a first faster approximate match too. Bye, bearophile From pje at telecommunity.com Mon Apr 6 11:21:42 2009 From: pje at telecommunity.com (P.J. Eby) Date: Mon, 06 Apr 2009 11:21:42 -0400 Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: <49D9FCE2.4070805@simplistix.co.uk> References: <49D4DA72.60401@v.loewis.de> <49D51A16.70804@simplistix.co.uk> <49D669AA.6080001@v.loewis.de> <49D9FCE2.4070805@simplistix.co.uk> Message-ID: <20090406151915.5D4F93A406A@sparrow.telecommunity.com> At 02:00 PM 4/6/2009 +0100, Chris Withers wrote: >Martin v. L?wis wrote: >>Chris Withers wrote: >>>Would this support the following case: >>> >>>I have a package called mortar, which defines useful stuff: >>> >>>from mortar import content, ... >>> >>>I now want to distribute large optional chunks separately, but ideally >>>so that the following will will work: >>> >>>from mortar.rbd import ... >>>from mortar.zodb import ... >>>from mortar.wsgi import ... >>> >>>Does the PEP support this? >>That's the primary purpose of the PEP. > >Are you sure? > >Does the pep really allow for: > >from mortar import content >from mortar.rdb import something > >...where 'content' is a function defined in mortar/__init__.py and >'something' is a function defined in mortar/rdb/__init__.py *and* >the following are separate distributions on PyPI: > >- mortar >- mortar.rdb > >...where 'mortar' does not contain 'mortar.rdb'. See the third paragraph of http://www.python.org/dev/peps/pep-0382/#discussion From bearophileHUGS at lycos.com Mon Apr 6 11:24:20 2009 From: bearophileHUGS at lycos.com (bearophileHUGS at lycos.com) Date: Mon, 6 Apr 2009 08:24:20 -0700 (PDT) Subject: speed of string chunks file parsing References: bb82b9ac-ea72-4a59-bdd2-0fd901e59a67@v15g2000yqn.googlegroups.com Message-ID: <664007ec-9cff-4bd2-bc92-640749b3cf73@g19g2000yql.googlegroups.com> bearophile: > ? ? cp_regular_expression = re.compile("^a complex regular expression > here$") > ? ? for line in file(inputfile): > ? ? ? ? if cp_regular_expression.match(line) and result_seq: Sorry, you can replace that with: cp_regular_expression = re.compile("^a complex regular expression here$").match for line in file(inputfile): if cp_regular_expression(line) and result_seq: That is a bit faster. Bye From chris at simplistix.co.uk Mon Apr 6 11:57:59 2009 From: chris at simplistix.co.uk (Chris Withers) Date: Mon, 06 Apr 2009 16:57:59 +0100 Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: <20090406151915.5D4F93A406A@sparrow.telecommunity.com> References: <49D4DA72.60401@v.loewis.de> <49D51A16.70804@simplistix.co.uk> <49D669AA.6080001@v.loewis.de> <49D9FCE2.4070805@simplistix.co.uk> <20090406151915.5D4F93A406A@sparrow.telecommunity.com> Message-ID: <49DA2687.6050508@simplistix.co.uk> P.J. Eby wrote: > See the third paragraph of > http://www.python.org/dev/peps/pep-0382/#discussion Indeed, I guess the PEP could be made more explanatory then 'cos, as a packager, I don't see what I'd put in the various setup.py and __init__.py to make this work... That said, I'm delighted to hear it's going to be possible and wholeheartedly support the PEP and it's backporting to 2.7 as a result... cheers, Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk From jnoller at gmail.com Mon Apr 6 12:00:46 2009 From: jnoller at gmail.com (Jesse Noller) Date: Mon, 6 Apr 2009 12:00:46 -0400 Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: References: <49D4DA72.60401@v.loewis.de> <49D52115.6020001@egenix.com> <4222a8490904060621y36285ca4g73fafe85d4b9c146@mail.gmail.com> Message-ID: <4222a8490904060900s349180a5k952b32b35274df73@mail.gmail.com> On Mon, Apr 6, 2009 at 9:26 AM, Barry Warsaw wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > On Apr 6, 2009, at 9:21 AM, Jesse Noller wrote: > >> On Thu, Apr 2, 2009 at 4:33 PM, M.-A. Lemburg wrote: >>> >>> On 2009-04-02 17:32, Martin v. L?wis wrote: >>>> >>>> I propose the following PEP for inclusion to Python 3.1. >>> >>> Thanks for picking this up. >>> >>> I'd like to extend the proposal to Python 2.7 and later. >>> >> >> -1 to adding it to the 2.x series. There was much discussion around >> adding features to 2.x *and* 3.0, and the consensus seemed to *not* >> add new features to 2.x and use those new features as carrots to help >> lead people into 3.0. > > Actually, isn't the policy just that nothing can go into 2.7 that isn't > backported from 3.1? ?Whether the actual backport happens or not is up to > the developer though. ?OTOH, we talked about a lot of things and my > recollection is probably fuzzy. > > Barry That *is* the official policy, but there was discussions around no further backporting of features from 3.1 into 2.x, therefore providing more of an upgrade incentive From andrew at acooke.org Mon Apr 6 12:09:42 2009 From: andrew at acooke.org (andrew cooke) Date: Mon, 6 Apr 2009 12:09:42 -0400 (CLT) Subject: speed of string chunks file parsing In-Reply-To: <49DA0843.2060906@sfc.keio.ac.jp> References: <49DA0843.2060906@sfc.keio.ac.jp> Message-ID: [disclaimer - this is just guessing from general knowledge of regular expressions; i don't know any details of python's regexp engine] if your regular expression is the bottleneck rewrite it to avoid lazy matching, references, groups, lookbacks, and perhaps even counted repeats. with a little thought you can do almost everything using just choices '(a|b)' and repeat 'a*'. even if the expression is longer, it will probably be faster. character ranges - either explicit '[a-z]' or predefined '\w' (even '.') - should be fine, but try to avoid having multiple occurrences of ".*". see the timeit package for testing the speed of small chunks of code. andrew Hyunchul Kim wrote: > Hi, all > > I have a simple script. > Can you improve algorithm of following 10 line script, with a view point > of speed ? > Following script do exactly what I want but I want to improve the speed. > > This parse a file and accumulate lines till a line match a given regular > expression. > Then, when a line match a given regular expression, this function yield > lines before the matched lines. > > **************** > import re > resultlist = [] > cp_regularexpression = re.compile('^a complex regular expression here$) > for line in file(inputfile): > if cp_regularexpression.match(line): > if resultlist != []: > yield resultlist > resultlist = [] > resultlist.append(line) > yield resultlist > **************** > > Thank you in advance, > > Hyunchul > > > -- > http://mail.python.org/mailman/listinfo/python-list > > From __peter__ at web.de Mon Apr 6 12:46:16 2009 From: __peter__ at web.de (Peter Otten) Date: Mon, 06 Apr 2009 18:46:16 +0200 Subject: Example for readline module usage? References: Message-ID: Grant Edwards wrote: > On 2009-04-06, Peter Otten <__peter__ at web.de> wrote: >> Grant Edwards wrote: >> >>> [I swear I've asked this question before, but Google can't find >>> it.] >> >> My Google is better than yours then: >> >> http://mail.python.org/pipermail/python-list/2008-July/669582.html > > It certainly is. All I could come up with were tons of hits on > the file-object's readline. What I don't understand is > how/where the readline module is told what file descriptor it's > supposed to be reading from. Is that just implicit in the > "import" operation? What I've gleaned from the source: On import it sets the PyOS_ReadlineFunctionPointer in Parser/myreadline.c. If both stdin and stdout are a tty (GNU) readline is invoked via this pointer. Peter From rdmurray at bitdance.com Mon Apr 6 12:53:40 2009 From: rdmurray at bitdance.com (R. David Murray) Date: Mon, 6 Apr 2009 16:53:40 +0000 (UTC) Subject: object knows which object called it? References: <662de4f7-d8a3-4ed8-bb02-5571e33a3304@k8g2000yqn.googlegroups.com> Message-ID: Reckoner wrote: > hi, > > I have the following problem: I have two objects, say, A and B, which > are both legitimate stand-alone objects with lives of their own. > > A contains B as a property, so I often do > > A.B.foo() > > the problem is that some functions inside of B actually need A > (remember I said they were both standalone objects), so I have to > often do: > > A.B.foo_func(A) > > Which is kind of awkward. > > Is there some way that B.foo_func() could somehow know that it was > called as a property of A in this way? > > Note that I'm looking for the calling object and NOT the calling > function. You could probably do this by creating a custom __getattr__ method (or maybe even just a property) on A that would recognize B as an object and return it wrapped in a class that would pick up the __getattr__ call on B and translate it into a real call on B passing A as the first argument. But that kind of magic is not considered good Python practice ("explicit is better than implicit"). And it would be quite inefficient :) I think the OO way to do this is to provide a method on A that does the right thing: def Bfoo_func(self): self.B.foo_func(self) Or maybe you could look at generic methods, which provide a way to do multiple dispatch. -- R. David Murray http://www.bitdance.com From kernel at tekno-soft.it Mon Apr 6 12:58:12 2009 From: kernel at tekno-soft.it (Roberto Fichera) Date: Mon, 06 Apr 2009 18:58:12 +0200 Subject: Injecting python function in an embedded python engine Message-ID: <49DA34A4.1000202@tekno-soft.it> Hi All in the list, I've embedded python v2.6.x engine into my application without any problem. Now I would like to inject some additional functions after importing a python module. So, basically I'm importing a python module via PyImport_ImportModule() function. The python module is a simple set of functions and I would check if some functions does exist or not, than if doesn't I would add it from my C application. Checking if a function does exist or not isn't an issue, it works without any problem, my problem is only how to inject a new function in an already imported module. Thanks in advance, Roberto Fichera. From faniryharijaona at yahoo.fr Mon Apr 6 13:00:08 2009 From: faniryharijaona at yahoo.fr (razafindrazaka faniry harijaona) Date: Mon, 6 Apr 2009 17:00:08 +0000 (GMT) Subject: Randomized Incremental Decision Tree Message-ID: <1262.2197.qm@web23305.mail.ird.yahoo.com> Hello, How to model this problem as a python code: Starting with a general condition A, we enter a statement 'p' , if p satisfy A which is always the case, then split A to three sub-conditions A1,A2,A3. And we enter again a statement p1: if p1 satisfy A: if p1 satisfy A1: split A1 to A1_1,A1_2,A1_3 if p1 satisfy A2: split A2 to A2_1,A2_2,A2_3 if p1 satisfy A3: split A3 to A3_1,A3_2,A3_3 we enter a third statement p2 and test from A-->A_i-->Ai_j and split the corresponding satisfied condition and so one.... Since we add incrementally the statement, the condition will be generated according to the truth of the statement. Regards -------------- next part -------------- An HTML attachment was scrubbed... URL: From castironpi at gmail.com Mon Apr 6 13:02:20 2009 From: castironpi at gmail.com (Aaron Brady) Date: Mon, 6 Apr 2009 10:02:20 -0700 (PDT) Subject: group several methods under a attribute References: Message-ID: <746bd28c-9ea6-44d0-80ea-1292f60ede73@r37g2000yqn.googlegroups.com> On Apr 6, 5:40?am, jelle wrote: > Hi, > > I'm working on a pretty large class and I'd like to group several > methods under a attribute. > Its not convenient to chop up the class in several smaller classes, > nor would mixins really solve the issue. > So, what is a pythonic way of grouping several methods under a > attribute? > > Many thanks in advance, > > -jelle Hi jelle, I disagree with Gerhard that you 'should find a better way' and another way 'almost certainly the right thing', unless his reason is merely that it's an advanced technique that will get you into problems down the road. There's nothing 'un-object-oriented' about it. The link to the God pattern alleges that it fails to 'divide and conquer', but you are dividing. I think Python opens some beautiful doors in this regard. Here is some code which I understand accomplishes what you sought. class ClsA( object ): def __init__( self ): self.inst= None def __get__( self, instance, owner ): self.inst= instance return self def submethA( self, arg ): print( 'submethA %r, instance %r'% ( arg, self.inst ) ) class ClsB( object ): A= ClsA( ) def methA( self, arg ): print( 'methA %r'% arg ) b= ClsB( ) b.methA( 'this' ) b.A.submethA( 'that' ) #Output: ''' methA 'this' submethA 'that', instance <__main__.ClsB object...> ''' In Python 3, you don't need 'ClsA' and 'ClsB' to inherit from 'object' explicitly. If you call 'b.A' from a class, 'ClsB.A', you will get a 'ClsA' object with a None 'inst' attribute, which is a big hole waiting for you to step in. Perhaps you want a wrapper for your methods of 'ClsA' to check that 'inst' is set, or merely throw an exception in '__get__' if 'instance' is 'None'. Python after all requires that a method's first argument be an instance of an improper subclass of the class it was defined for. From castironpi at gmail.com Mon Apr 6 13:04:56 2009 From: castironpi at gmail.com (Aaron Brady) Date: Mon, 6 Apr 2009 10:04:56 -0700 (PDT) Subject: object knows which object called it? References: <662de4f7-d8a3-4ed8-bb02-5571e33a3304@k8g2000yqn.googlegroups.com> Message-ID: On Apr 6, 9:53?am, Reckoner wrote: > hi, > > I have the following problem: I have two objects, say, A and B, which > are both legitimate stand-alone objects with lives of their own. > > A contains B as a property, so I often do > > A.B.foo() > > the problem is that some functions inside of B actually need A > (remember I said they were both standalone objects), so I have to > often do: > > A.B.foo_func(A) > > Which is kind of awkward. > > Is there some way that B.foo_func() could somehow know that it was > called as a property of A in this way? > > Note that I'm looking for the calling object and NOT the calling > function. > > Thanks in advance. Hi Reckoner, I believe this does what you want. It's an advanced technique and not available in all OO languages. class ClsA( object ): def __init__( self ): self.inst= None def __get__( self, instance, owner ): self.inst= instance return self def submethA( self, arg ): print( 'submethA %r, instance %r'% ( arg, self.inst ) ) class ClsB( object ): A= ClsA( ) def methA( self, arg ): print( 'methA %r'% arg ) b= ClsB( ) b.methA( 'this' ) b.A.submethA( 'that' ) #Output: ''' methA 'this' submethA 'that', instance <__main__.ClsB object...> ''' There's a small discussion in another today's thread, 'group several methods under an attribute'. From duncan.booth at invalid.invalid Mon Apr 6 13:09:55 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 6 Apr 2009 17:09:55 GMT Subject: object knows which object called it? References: <662de4f7-d8a3-4ed8-bb02-5571e33a3304@k8g2000yqn.googlegroups.com> Message-ID: Reckoner wrote: > hi, > > I have the following problem: I have two objects, say, A and B, which > are both legitimate stand-alone objects with lives of their own. > > A contains B as a property, so I often do > > A.B.foo() > > the problem is that some functions inside of B actually need A > (remember I said they were both standalone objects), so I have to > often do: > > A.B.foo_func(A) > > Which is kind of awkward. > > Is there some way that B.foo_func() could somehow know that it was > called as a property of A in this way? > > Note that I'm looking for the calling object and NOT the calling > function. > You can do something like this if you really want (Zope does), but it is complex and confusing (Zope 3 has dropped implicit acquisition as being a bad idea). Have a look at http://www.zope.org/Documentation/Books/ZDG/current/Acquisition.stx which has the following example: import ExtensionClass, Acquisition class C(ExtensionClass.Base): color='red' class A(Acquisition.Implicit): def report(self): print self.color a=A() c=C() c.a=A() c.a.report() # prints 'red' d=C() d.color='green' d.a=a d.a.report() # prints 'green' a.report() # raises an attribute error and what you actually asked for: in the example above 'c.a.aq_parent is c' If you want to confuse yourself you can install Zope's ExtensionClass and Acquisition modules from PyPi: http://pypi.python.org/pypi/Acquisition/2.12.0a1 From rdmurray at bitdance.com Mon Apr 6 13:17:15 2009 From: rdmurray at bitdance.com (R. David Murray) Date: Mon, 6 Apr 2009 17:17:15 +0000 (UTC) Subject: set python default encoding References: <35d793ed-52ca-4236-bd72-8840772cec52@f41g2000pra.googlegroups.com> Message-ID: reetesh nigam wrote: > Hi All, > I am unable to set the python default encoding. > i used the following proccess to set the python encoding > > import sys > reload(sys) > sys.setdefaultencoding('latin-1') > > but it is giving me the same error : > > args = ('utf8', "MEDICINE '\xc4 ", 10, 12, 'invalid data', > 0x036FFE90>>) > encoding = 'utf8' > end = 12 > message = '' > object = "MEDICINE '\xc4 " > reason = 'invalid data' > start = 10 > > > Please tell me how to solve this problem. That doesn't look anything like a python traceback. I'm guessing you are using some sort of web framework? Perhaps you should try asking in the forum for the framework. -- R. David Murray http://www.bitdance.com From castironpi at gmail.com Mon Apr 6 13:30:44 2009 From: castironpi at gmail.com (Aaron Brady) Date: Mon, 6 Apr 2009 10:30:44 -0700 (PDT) Subject: group several methods under a attribute References: <746bd28c-9ea6-44d0-80ea-1292f60ede73@r37g2000yqn.googlegroups.com> Message-ID: On Apr 6, 12:02?pm, Aaron Brady wrote: > On Apr 6, 5:40?am, jelle wrote: > > > Hi, > > > I'm working on a pretty large class and I'd like to group several > > methods under a attribute. > > Its not convenient to chop up the class in several smaller classes, > > nor would mixins really solve the issue. > > So, what is a pythonic way of grouping several methods under a > > attribute? > > > Many thanks in advance, > > > -jelle > > Hi jelle, > > I disagree with Gerhard that you 'should find a better way' and > another way 'almost certainly the right thing', unless his reason is > merely that it's an advanced technique that will get you into problems > down the road. snip > class ClsA( object ): > ? ? def __init__( self ): > ? ? ? ? self.inst= None > ? ? def __get__( self, instance, owner ): > ? ? ? ? self.inst= instance > ? ? ? ? return self snip There's a potential conflict in the above, which may have contributed to Gerhard's advice. If you have: bA= ClsB( ) bB= ClsB( ) bA.A.submethA( 'what' ) then the ClsA instance will think it's being accessed from 'bA' until it's accessed again! This may cause problems such as if you store a reference to it, because bB could access it, and it won't think it's being accessed from bA anymore. To get around this, you probably want '__get__' to create a new object that is specifically dedicated to the instance of ClsB that's calling it. bA.A and bB.A return new objects, separate from each other. However, then two calls to bA.A won't return the same object; they'll return new every time. Another detail is that ClsA can't store instance information. It should be stored in ClsB instances, and the methods merely go somewhere else. Otherwise, the right place for 'A= ClsA( )' is in the constructor: class clsB: def __init__( self ): self.A= ClsA( ) Then, every ClsB instance has its own instance of ClsA, and it can store instance information, and doesn't create new instances when you call it. The creation of 'ClsA' merely goes in the initializer, not the class statement. From anthra.norell at bluewin.ch Mon Apr 6 13:44:44 2009 From: anthra.norell at bluewin.ch (Anthra Norell) Date: Mon, 06 Apr 2009 19:44:44 +0200 Subject: object knows which object called it? In-Reply-To: <662de4f7-d8a3-4ed8-bb02-5571e33a3304@k8g2000yqn.googlegroups.com> References: <662de4f7-d8a3-4ed8-bb02-5571e33a3304@k8g2000yqn.googlegroups.com> Message-ID: <49DA3F8C.7060407@bluewin.ch> Reckoner wrote: > hi, > > I have the following problem: I have two objects, say, A and B, which > are both legitimate stand-alone objects with lives of their own. > > A contains B as a property, so I often do > > A.B.foo() > > the problem is that some functions inside of B actually need A > (remember I said they were both standalone objects), so I have to > often do: > > A.B.foo_func(A) > > Which is kind of awkward. > > Is there some way that B.foo_func() could somehow know that it was > called as a property of A in this way? > > Note that I'm looking for the calling object and NOT the calling > function. > > Thanks in advance. > -- > http://mail.python.org/mailman/listinfo/python-list > > B has to get into A somehow. Providing an installation method is no inconvenience if one remembers later not to bypass it. class A: something_for_contained_to_show = 99 def install_contained (self, B): B.container = self # Provides B with a reference to A self.contained = B class B: def method_with_container_access (self): print self.container # Show container object print self.container.something_for_contained_to_show >>> a = A (); b = B (); a.install_contained (b) >>> b.method_with_container_access () <__main__.A instance at 0x019D5788> 99 Does this look like a good idea? Frederic From vishakha406 at yahoo.com Mon Apr 6 13:56:03 2009 From: vishakha406 at yahoo.com (vishakha vaibhav) Date: Mon, 6 Apr 2009 10:56:03 -0700 (PDT) Subject: Executing a C program from Python Message-ID: <769712.9895.qm@web53703.mail.re2.yahoo.com> Hi, I am very new to python. I have my cgi script written in Python. My CGI script should call a C program and collect the value returned by this program and pass it to the browser. ? Can anyone help me out in this. How can I execute the c program and collect the return value. ? I tried, import os a=os.system("my-app")//my-app is the C program executable print a ? But the variable a prints 0 everytime.?It should return a non-zero value as per my C program. ?Is there any method to do what I need. ? Regards, vish -------------- next part -------------- An HTML attachment was scrubbed... URL: From piet at cs.uu.nl Mon Apr 6 14:14:01 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 06 Apr 2009 20:14:01 +0200 Subject: An executable operational semantics for Python References: <01cf8180-87e2-472a-9891-eb687fdd5e6c@j8g2000yql.googlegroups.com> Message-ID: >>>>> bearophileHUGS at lycos.com (b) wrote: >b> gideon: >>> I've recently finished my Master's thesis on the semantics of Python. >>> In my thesis I define the semantics of Python by rewriting an abstract >>> machine. The sources that are used to produce my thesis can also be >>> compiled into a working interpreter. Hence I call it an 'executable' >>> semantics. >b> Can it be used for some useful purpose? I wanted to play with Python's MRO algorithm in the last weeks. I just took the MRO description from Gideon's thesis, put it in a small Haskell program and now I can easily try it out. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From antroy at gmail.com Mon Apr 6 14:16:45 2009 From: antroy at gmail.com (Ant) Date: Mon, 6 Apr 2009 11:16:45 -0700 (PDT) Subject: Cannot find text in *.py files with Windows Explorer? References: Message-ID: <00e7a65c-0541-4a90-8ee9-529245e44756@l22g2000vba.googlegroups.com> On Apr 4, 7:09?am, Tim Golden wrote: > ... Now I think about it, try searching > for "xplorer2" ... I'll second that. It's one of the few non-open source bits of software that I'll willingly pay a license for. Have used it for around 5 or 6 years now. It's by a little 1 man company called Zabkat. -- Ant. From trentm at activestate.com Mon Apr 6 14:26:25 2009 From: trentm at activestate.com (Trent Mick) Date: Mon, 06 Apr 2009 11:26:25 -0700 Subject: Help with wxPython program :.: return 1? In-Reply-To: References: Message-ID: <49DA4951.1040405@activestate.com> Dennis Lee Bieber wrote: > I don't know what Komodo is coded in, but if it is using wx, you may > be failing from having two "mainloop" processes... (same problem as > trying to run a Tkinter application from inside IDLE, and probably > trying to run a win32gui application from PythonWin) No, Komodo is not a wx app. Trent -- Trent Mick trentm at activestate.com From bsagert at gmail.com Mon Apr 6 14:29:43 2009 From: bsagert at gmail.com (Bill) Date: Mon, 6 Apr 2009 11:29:43 -0700 (PDT) Subject: Delicious API and urllib2 Message-ID: <3a6e324f-bf5f-4ac9-969a-882fdd341c91@r3g2000vbp.googlegroups.com> The delicious api requires http authorization (actually https). A generic delicious api post url is "https:// username:password at api.api.del.icio.us/v1/posts/add?url=http:// example.com/&description=interesting&tags=whatever". This works fine when entered in the Firefox address bar. However urllib2.urlopen(delicious_post_url) chokes and returns "httplib.InvalidURL: nonnumeric port: 'password at api.del.icio.us". Delicious really wants that authorization stuff embedded in the url as it also rejected my attempts at using urllib2.HTTPBasicAuthHandler(), etc. Anybody have any hints? From Eric_Dexter at msn.com Mon Apr 6 14:30:57 2009 From: Eric_Dexter at msn.com (Eric_Dexter at msn.com) Date: Mon, 6 Apr 2009 11:30:57 -0700 (PDT) Subject: pycap (popcap gaming lib w python 2.5) mouse question Message-ID: <1f24c44d-cf0d-4f61-8f38-e706d1ad88c5@k41g2000yqh.googlegroups.com> anyone use pycap based on popcap gaming lib.. http://www.farbs.org/pycap.html?? (not to be confused with the other pycap) I was trying to figure out why the mouse works in the example I didn't see any python code for it but It seem to have an effect in the example.. From vinay_sajip at yahoo.co.uk Mon Apr 6 14:35:08 2009 From: vinay_sajip at yahoo.co.uk (Vinay Sajip) Date: Mon, 6 Apr 2009 11:35:08 -0700 (PDT) Subject: logging - string formating problems References: <49D8EA92.9080704@free.fr> Message-ID: <75cbee2e-6824-4e1a-9999-2ae105e5e21a@l38g2000vba.googlegroups.com> On Apr 6, 1:58?pm, "Werner F. Bruhin" wrote: > I am fully aware that the problem is in my code, however as getMessage > inlogging.__init__.py does not catch the exception it is pretty > difficult to find the problem without manually inspecting anylogging.something statements. > > My hack oflogging.py is really a hack and I know that this can not be > used a patch but I hope that someone with more know how then me knows > how to improve the situation. > > BTW, using the below hack was enough for me to mind the bad code in my > stuff, but it shouldn't have been this difficult. > > Traceback (most recent call last): > ? ?File "C:\Python25\lib\logging\__init__.py", line 750, in emit > ? ? ?msg = self.format(record) > ? ?File "C:\Python25\lib\logging\__init__.py", line 636, in format > ? ? ?return fmt.format(record) > ? ?File "C:\Python25\lib\logging\__init__.py", line 424, in format > ? ? ?record.message = record.getMessage() > ? ?File "C:\Python25\lib\logging\__init__.py", line 288, in getMessage > ? ? ?msg = msg % self.args > TypeError: not all arguments converted during string formatting > > Then I hacklogging.py and change line 288 in getMessage from: > ? ? ? ? ? ? ?msg = msg % self.args > > to: > ? ? ? ? ? ? ?try: > ? ? ? ? ? ? ? ? ?msg = msg % self.args > ? ? ? ? ? ? ?except: > ? ? ? ? ? ? ? ? ?print 'msg: %s' % msg > ? ? ? ? ? ? ? ? ?print 'args: %s' % self.args > ? ? ? ? ? ? ? ? ?print traceback.format_exc() > > I get the following which helps a lot more in finding were in my code I > have the problem: > > msg: ping_min: 1 > args: replace > Traceback (most recent call last): > ? ?File "C:\Python25\lib\logging\__init__.py", line 290, in getMessage > ? ? ?msg = msg % self.args > TypeError: not all arguments converted during string formatting > > As mentioned above, I know the above code is not good enough at all, but > I hope that maybe Vinay Sajip or someone else with more know how then I > have can come up with a patch which will make this type of situation easier. > > Werner Handlers should direct any exceptions raised during emit() to handleError(), except for SystemExit and KeyboardInterrupt which are re-raised. So, you should be able to redefine handleError to print the relevant information if a TypeError is raised, either by subclassing or by monkey-patching as Peter Otten has suggested. However, handleError normally swallows the exceptions if raiseExceptions is false (by default, it's set to true. It's meant to be set to false only in production environments where exceptions raised in logging should have no influence on the application's behaviour). Regards, Vinay Sajip From robert at nowhere.invalid Mon Apr 6 14:39:05 2009 From: robert at nowhere.invalid (robert) Date: Mon, 06 Apr 2009 20:39:05 +0200 Subject: Accessing mail box tree of Thunderbird/Seamonkey? In-Reply-To: References: Message-ID: Chris Rebert wrote: > On Sun, Apr 5, 2009 at 1:04 AM, robert wrote: >> Is there a API/possibilty for reading&writing (live) in the mail box tree of >> Thunderbird/Seamonkey with Python? > >>From what I can google, they're already in mbox format, so you can use > mailbox.mbox to read/write to them. > See http://docs.python.org/library/mailbox.html#mailbox.mbox > I was already aware of. those mbox file just grow and grow until one does a 'Compress Folder'. There are additional complicated index files and caching and all. So it doesn't reflect the current state nor allows for practical modifying. I found only SimpleMAPI for the inbox&send only. the XUL api seems to be usable only from inside. Robert From brentb at beanfield.com Mon Apr 6 14:44:52 2009 From: brentb at beanfield.com (Brent Bloxam) Date: Mon, 06 Apr 2009 14:44:52 -0400 Subject: Delicious API and urllib2 In-Reply-To: <3a6e324f-bf5f-4ac9-969a-882fdd341c91@r3g2000vbp.googlegroups.com> References: <3a6e324f-bf5f-4ac9-969a-882fdd341c91@r3g2000vbp.googlegroups.com> Message-ID: <49DA4DA4.50606@beanfield.com> Bill wrote: > The delicious api requires http authorization (actually https). A > generic delicious api post url is "https:// > username:password at api.api.del.icio.us/v1/posts/add?url=http:// > example.com/&description=interesting&tags=whatever". > > This works fine when entered in the Firefox address bar. However > urllib2.urlopen(delicious_post_url) chokes and returns > "httplib.InvalidURL: nonnumeric port: 'password at api.del.icio.us". > > Delicious really wants that authorization stuff embedded in the url as > it also rejected my attempts at using urllib2.HTTPBasicAuthHandler(), > etc. > Anybody have any hints? > -- > http://mail.python.org/mailman/listinfo/python-list > What failure were you experiencing when you were using the HTTPBasicAuthHandler? Did you follow the sample code from the docs? > import urllib2 > # Create an OpenerDirector with support for Basic HTTP Authentication... > auth_handler = urllib2.HTTPBasicAuthHandler() > auth_handler.add_password(realm='PDQ Application', > uri='https://mahler:8092/site-updates.py', > user='klem', > passwd='kadidd!ehopper') > opener = urllib2.build_opener(auth_handler) > # ...and install it globally so it can be used with urlopen. > urllib2.install_opener(opener) > urllib2.urlopen('http://www.example.com/login.html' From brentb at beanfield.com Mon Apr 6 14:49:29 2009 From: brentb at beanfield.com (Brent Bloxam) Date: Mon, 06 Apr 2009 14:49:29 -0400 Subject: Executing a C program from Python In-Reply-To: <769712.9895.qm@web53703.mail.re2.yahoo.com> References: <769712.9895.qm@web53703.mail.re2.yahoo.com> Message-ID: <49DA4EB9.80208@beanfield.com> vishakha vaibhav wrote: > Hi, > I am very new to python. I have my cgi script written in Python. My > CGI script should call a C program and collect the value returned by > this program and pass it to the browser. > > Can anyone help me out in this. How can I execute the c program and > collect the return value. > > I tried, > import os > a=os.system("my-app")//my-app is the C program executable > print a > > But the variable *a* prints 0 everytime. It should return a non-zero > value as per my C program. Is there any method to do what I need. > > Regards, > vish > > > ------------------------------------------------------------------------ > > -- > http://mail.python.org/mailman/listinfo/python-list > Executing from os.system returns the return code from the application. Take a look at the subprocess module (http://docs.python.org/library/subprocess.html#module-subprocess), as recommended by the os docs when you need to deal with capturing output from stdout. -------------- next part -------------- An HTML attachment was scrubbed... URL: From avinashrude at gmail.com Mon Apr 6 15:08:31 2009 From: avinashrude at gmail.com (Avi) Date: Mon, 6 Apr 2009 12:08:31 -0700 (PDT) Subject: Best way to start Message-ID: <0e991af0-52f4-4849-9587-914bf38b3505@j39g2000yqn.googlegroups.com> Hi, What is a good way to learn Python? Do you recommend going by a book (suggestions welcome) or learning with tutorials? Both? Thanks in advance, Avi From victorsubervi at gmail.com Mon Apr 6 15:11:35 2009 From: victorsubervi at gmail.com (Victor Subervi) Date: Mon, 6 Apr 2009 15:11:35 -0400 Subject: Eval Problem Message-ID: <4dc0cfea0904061211u44ba605bx8638414144025bc0@mail.gmail.com> Hi: I have this code: x = 1 while x <= bitties: file = open(p + str(x) + ".txt") for line in file: print line print eval(bits[x - 1]) x += 1 which throws this error: [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] PythonHandler mod_python.cgihandler: Traceback (most recent call last): [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] PythonHandler mod_python.cgihandler: File "/usr/lib64/python2.4/site-packages/mod_python/apache.py", line 299, in HandlerDispatch\n result = object(req) [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] PythonHandler mod_python.cgihandler: File "/usr/lib64/python2.4/site-packages/mod_python/cgihandler.py", line 96, in handler\n imp.load_module(module_name, fd, path, desc) [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] PythonHandler mod_python.cgihandler: File "/var/www/vhosts/ articles.13gems.com/httpdocs/index_frame.py", line 89, in ?\n print eval(bits[1]) [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] PythonHandler mod_python.cgihandler: File "", line 1 [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] PythonHandler mod_python.cgihandler: tableBottom(348,180) [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] PythonHandler mod_python.cgihandler: ^ [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] PythonHandler mod_python.cgihandler: SyntaxError: invalid syntax However, if I edit out the offending line and add: print tableBottom(348,180) it prints! What?s wrong? TIA, Victor -------------- next part -------------- An HTML attachment was scrubbed... URL: From clp2 at rebertia.com Mon Apr 6 15:19:40 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 6 Apr 2009 12:19:40 -0700 Subject: Best way to start In-Reply-To: <0e991af0-52f4-4849-9587-914bf38b3505@j39g2000yqn.googlegroups.com> References: <0e991af0-52f4-4849-9587-914bf38b3505@j39g2000yqn.googlegroups.com> Message-ID: <50697b2c0904061219i3c3379c1l84597b7308612eca@mail.gmail.com> On Mon, Apr 6, 2009 at 12:08 PM, Avi wrote: > Hi, > > What is a good way to learn Python? > > Do you recommend going by a book (suggestions welcome) or learning > with tutorials? Both? The official Python tutorial is pretty darn good: http://docs.python.org/tutorial/ If you want a book as well, I personally liked "Python in a Nutshell"; it's not in tutorial format, but you can definitely learn Python from it. Cheers, Chris -- I have a blog: http://blog.rebertia.com From shannon.timothy at gmail.com Mon Apr 6 15:37:41 2009 From: shannon.timothy at gmail.com (Tim Shannon) Date: Mon, 6 Apr 2009 14:37:41 -0500 Subject: Weird Tk Canvas coordinate issue Message-ID: I'm new to python, so keep that in mind. I have a tk Canvas that I'm trying to draw on, and I want to start my drawing at an offset (from 0) location. So I can tweak this as I code, I set this offset as a class level variable: def ClassName: OFFSET = 20 def __init__(self, master)): self.canvas = Canvas(master) self.canvas.create_rectangle(self.OFFSET, self.OFFSET, 300 + self.OFFSET, 300 + self.OFFSET, width=2) The weird thing is, it doesn't offset. If I set my offset to 100000, it still starts drawing at 0,0. Here's the really weird part (at least to me), if I put a print line right about my drawing statements to print the value of the offset, it works like it should, and offsets the drawing. If I remove the print line, the offset goes away. This makes no sense to me. -------------- next part -------------- An HTML attachment was scrubbed... URL: From davidj411 at gmail.com Mon Apr 6 15:41:50 2009 From: davidj411 at gmail.com (davidj411) Date: Mon, 6 Apr 2009 12:41:50 -0700 (PDT) Subject: cgi file limit size? Message-ID: I am wondering where the limitation of filesize comes from when i upload a large file. it uploads when the filesize is less than 20 MB (but not if larger). the script does not limit the filesize so it is either an HTTP specification or a webserver limit, right? maybe my connection to the server is timing out during the upload? web server is IIS 6.0. python is 2.5.2. IIS webmapping does not use "-u" b/c nothing works when that option is used. From jelleferinga at gmail.com Mon Apr 6 15:52:50 2009 From: jelleferinga at gmail.com (jelle) Date: Mon, 6 Apr 2009 12:52:50 -0700 (PDT) Subject: group several methods under a attribute References: <746bd28c-9ea6-44d0-80ea-1292f60ede73@r37g2000yqn.googlegroups.com> Message-ID: Hi Aaron, Thanks a lot for your suggestions. I wasnt familiar with the __get__ magic, which seems interesting. So, finally it seems that the cleanest pattern is: class ClsA( object ): def __init__( self, other ): self.inst= other def submethA( self, arg ): print( 'submethA %r, instance %r'% ( arg, self.inst ) ) class ClsB( object ): def methA( self, arg ): self.A= ClsA( self ) print( 'methA %r'% arg ) b= ClsB( ) b.methA( 'this' ) b.A.submethA( 'that' ) Many thanks, -jelle From george.sakkis at gmail.com Mon Apr 6 15:55:17 2009 From: george.sakkis at gmail.com (George Sakkis) Date: Mon, 6 Apr 2009 12:55:17 -0700 (PDT) Subject: object knows which object called it? References: <662de4f7-d8a3-4ed8-bb02-5571e33a3304@k8g2000yqn.googlegroups.com> Message-ID: On Apr 6, 10:53?am, Reckoner wrote: > hi, > > I have the following problem: I have two objects, say, A and B, which > are both legitimate stand-alone objects with lives of their own. > > A contains B as a property, so I often do > > A.B.foo() > > the problem is that some functions inside of B actually need A > (remember I said they were both standalone objects), so I have to > often do: > > A.B.foo_func(A) > > Which is kind of awkward. > > Is there some way that B.foo_func() could somehow know that it was > called as a property of A in this way? > > Note that I'm looking for the calling object and NOT the calling > function. Read up on descriptors [1], it seems that's what you're looking for. HTH, George [1] http://users.rcn.com/python/download/Descriptor.htm From andrew at acooke.org Mon Apr 6 16:10:07 2009 From: andrew at acooke.org (andrew cooke) Date: Mon, 6 Apr 2009 16:10:07 -0400 (CLT) Subject: Best way to start In-Reply-To: <0e991af0-52f4-4849-9587-914bf38b3505@j39g2000yqn.googlegroups.com> References: <0e991af0-52f4-4849-9587-914bf38b3505@j39g2000yqn.googlegroups.com> Message-ID: Avi wrote: > What is a good way to learn Python? > > Do you recommend going by a book (suggestions welcome) or learning > with tutorials? Both? how do you like to learn and how much experience do you have programming in other languages? andrew From namekuseijin at gmail.com Mon Apr 6 16:14:37 2009 From: namekuseijin at gmail.com (namekuseijin) Date: Mon, 06 Apr 2009 17:14:37 -0300 Subject: Best way to start In-Reply-To: <0e991af0-52f4-4849-9587-914bf38b3505@j39g2000yqn.googlegroups.com> References: <0e991af0-52f4-4849-9587-914bf38b3505@j39g2000yqn.googlegroups.com> Message-ID: I was able to get a friend into Python over a Google Chat. I pointed him to the downloads page, waited for him to install, then covered the basics in quite a few steps (syntax, conditionals, loops, function definition and application, classes and methods, lists, dicts and comprehensions). He loved the language and used it to solve some problems in the Euler Project. :) -- a game sig: http://tinyurl.com/d3rxz9 From shannon.timothy at gmail.com Mon Apr 6 16:14:44 2009 From: shannon.timothy at gmail.com (Tim Shannon) Date: Mon, 6 Apr 2009 15:14:44 -0500 Subject: Best way to start In-Reply-To: <0e991af0-52f4-4849-9587-914bf38b3505@j39g2000yqn.googlegroups.com> References: <0e991af0-52f4-4849-9587-914bf38b3505@j39g2000yqn.googlegroups.com> Message-ID: I personally learned a lot from www.diveintopython.org On Mon, Apr 6, 2009 at 2:08 PM, Avi wrote: > Hi, > > What is a good way to learn Python? > > Do you recommend going by a book (suggestions welcome) or learning > with tutorials? Both? > > Thanks in advance, > Avi > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From sxn02 at yahoo.com Mon Apr 6 17:01:46 2009 From: sxn02 at yahoo.com (Sorin Schwimmer) Date: Mon, 6 Apr 2009 14:01:46 -0700 (PDT) Subject: Some test fail on my new Python 2.6 Message-ID: <689852.94417.qm@web56005.mail.re3.yahoo.com> Hi All, I just downloaded and compiled Python 2.6 on a Gentoo Linux, IBM NetVista. After going through the usual steps (./configure, make), I ran a test (make test), and got some unexpected issues, which are detailed here: # ./python Lib/test/test_tcl.py Traceback (most recent call last): File "Lib/test/test_tcl.py", line 6, in from Tkinter import Tcl File "/install/Python-2.6.1/Lib/lib-tk/Tkinter.py", line 39, in import _tkinter # If this fails your Python may not be configured for Tk # ./python Lib/test/test_dbm.py Traceback (most recent call last): File "Lib/test/test_dbm.py", line 3, in import dbm ImportError: No module named dbm # ./python Lib/test/test_multiprocessing.py Traceback (most recent call last): File "Lib/test/test_multiprocessing.py", line 1825, in main() File "Lib/test/test_multiprocessing.py", line 1822, in main test_main(unittest.TextTestRunner(verbosity=2).run) File "Lib/test/test_multiprocessing.py", line 1788, in test_main raise TestSkipped("OSError raises on RLock creation, see issue 3111!") test.test_support.TestSkipped: OSError raises on RLock creation, see issue 3111! What is issue 3111? Couldn't find any reference to it. Two more: # ./python Lib/test/test_httpservers.py test_command (__main__.BaseHTTPServerTestCase) ... ok test_handler (__main__.BaseHTTPServerTestCase) ... ok test_head_keep_alive (__main__.BaseHTTPServerTestCase) ... ok test_header_close (__main__.BaseHTTPServerTestCase) ... ok test_internal_key_error (__main__.BaseHTTPServerTestCase) ... ok test_request_line_trimming (__main__.BaseHTTPServerTestCase) ... ok test_return_custom_status (__main__.BaseHTTPServerTestCase) ... ok test_return_header_keep_alive (__main__.BaseHTTPServerTestCase) ... ok test_send_blank (__main__.BaseHTTPServerTestCase) ... ok test_version_bogus (__main__.BaseHTTPServerTestCase) ... ok test_version_digits (__main__.BaseHTTPServerTestCase) ... ok test_version_invalid (__main__.BaseHTTPServerTestCase) ... ok test_version_none (__main__.BaseHTTPServerTestCase) ... ok test_version_none_get (__main__.BaseHTTPServerTestCase) ... ok test_get (__main__.SimpleHTTPServerTestCase) ... FAIL test_head (__main__.SimpleHTTPServerTestCase) ... ok test_invalid_requests (__main__.SimpleHTTPServerTestCase) ... ok test_authorization (__main__.CGIHTTPServerTestCase) ... FAIL test_headers_and_content (__main__.CGIHTTPServerTestCase) ... FAIL test_invaliduri (__main__.CGIHTTPServerTestCase) ... ok test_post (__main__.CGIHTTPServerTestCase) ... FAIL ====================================================================== FAIL: test_get (__main__.SimpleHTTPServerTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "Lib/test/test_httpservers.py", line 243, in test_get self.check_status_and_reason(response, 404) File "Lib/test/test_httpservers.py", line 219, in check_status_and_reason self.assertEquals(response.status, status) AssertionError: 200 != 404 ====================================================================== FAIL: test_authorization (__main__.CGIHTTPServerTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "Lib/test/test_httpservers.py", line 340, in test_authorization (res.read(), res.getheader('Content-type'), res.status)) AssertionError: ('Hello World\n', 'text/html', 200) != ('', None, 200) ====================================================================== FAIL: test_headers_and_content (__main__.CGIHTTPServerTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "Lib/test/test_httpservers.py", line 321, in test_headers_and_content (res.read(), res.getheader('Content-type'), res.status)) AssertionError: ('Hello World\n', 'text/html', 200) != ('', None, 200) ====================================================================== FAIL: test_post (__main__.CGIHTTPServerTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "Lib/test/test_httpservers.py", line 328, in test_post self.assertEquals(res.read(), '1, python, 123456\n') AssertionError: '' != '1, python, 123456\n' ---------------------------------------------------------------------- Ran 21 tests in 3.149s FAILED (failures=4) # ./python Lib/test/test_socket.py testCrucialConstants (__main__.GeneralModuleTests) ... ok testDefaultTimeout (__main__.GeneralModuleTests) ... ok testGetServBy (__main__.GeneralModuleTests) ... ok testGetSockOpt (__main__.GeneralModuleTests) ... ok testHostnameRes (__main__.GeneralModuleTests) ... ok testIPv4toString (__main__.GeneralModuleTests) ... ok testIPv6toString (__main__.GeneralModuleTests) ... ok testInterpreterCrash (__main__.GeneralModuleTests) ... ok testNewAttributes (__main__.GeneralModuleTests) ... ok testNtoH (__main__.GeneralModuleTests) ... ok testNtoHErrors (__main__.GeneralModuleTests) ... ok testRefCountGetNameInfo (__main__.GeneralModuleTests) ... ok testSendAfterClose (__main__.GeneralModuleTests) ... ok testSetSockOpt (__main__.GeneralModuleTests) ... ok testSockName (__main__.GeneralModuleTests) ... ERROR testSocketError (__main__.GeneralModuleTests) ... ok testStringToIPv4 (__main__.GeneralModuleTests) ... ok testStringToIPv6 (__main__.GeneralModuleTests) ... ok test_sock_ioctl (__main__.GeneralModuleTests) ... ok test_weakref (__main__.GeneralModuleTests) ... ok testFromFd (__main__.BasicTCPTest) ... ok testOverFlowRecv (__main__.BasicTCPTest) ... ok testOverFlowRecvFrom (__main__.BasicTCPTest) ... ok testRecv (__main__.BasicTCPTest) ... ok testRecvFrom (__main__.BasicTCPTest) ... ok testSendAll (__main__.BasicTCPTest) ... ok testShutdown (__main__.BasicTCPTest) ... ok testClose (__main__.TCPCloserTest) ... ok testInterruptedTimeout (__main__.TCPTimeoutTest) ... ok testTCPTimeout (__main__.TCPTimeoutTest) ... ok testTimeoutZero (__main__.TCPTimeoutTest) ... ok testExceptionTree (__main__.TestExceptions) ... ok testRecvFromInto (__main__.BufferIOTest) ... ok testRecvInto (__main__.BufferIOTest) ... ok testFromFd (__main__.BasicTCPTest2) ... ok testOverFlowRecv (__main__.BasicTCPTest2) ... ok testOverFlowRecvFrom (__main__.BasicTCPTest2) ... ok testRecv (__main__.BasicTCPTest2) ... ok testRecvFrom (__main__.BasicTCPTest2) ... ok testSendAll (__main__.BasicTCPTest2) ... ok testShutdown (__main__.BasicTCPTest2) ... ok testRecvFrom (__main__.BasicUDPTest) ... ok testRecvFromNegative (__main__.BasicUDPTest) ... ok testSendtoAndRecv (__main__.BasicUDPTest) ... ok testTimeoutZero (__main__.UDPTimeoutTest) ... ok testUDPTimeout (__main__.UDPTimeoutTest) ... ok testAccept (__main__.NonBlockingTCPTests) ... ok testConnect (__main__.NonBlockingTCPTests) ... ok testRecv (__main__.NonBlockingTCPTests) ... ok testSetBlocking (__main__.NonBlockingTCPTests) ... ok testClosedAttr (__main__.FileObjectClassTestCase) ... ok testFullRead (__main__.FileObjectClassTestCase) ... ok testReadline (__main__.FileObjectClassTestCase) ... ok testReadlineAfterRead (__main__.FileObjectClassTestCase) ... ok testReadlineAfterReadNoNewline (__main__.FileObjectClassTestCase) ... ok testSmallRead (__main__.FileObjectClassTestCase) ... ok testUnbufferedRead (__main__.FileObjectClassTestCase) ... ok testClosedAttr (__main__.UnbufferedFileObjectClassTestCase) ... ok testFullRead (__main__.UnbufferedFileObjectClassTestCase) ... ok testReadline (__main__.UnbufferedFileObjectClassTestCase) ... ok testReadlineAfterRead (__main__.UnbufferedFileObjectClassTestCase) ... ok testReadlineAfterReadNoNewline (__main__.UnbufferedFileObjectClassTestCase) ... ok testSmallRead (__main__.UnbufferedFileObjectClassTestCase) ... ok testUnbufferedRead (__main__.UnbufferedFileObjectClassTestCase) ... ok testUnbufferedReadline (__main__.UnbufferedFileObjectClassTestCase) ... ok testClosedAttr (__main__.LineBufferedFileObjectClassTestCase) ... ok testFullRead (__main__.LineBufferedFileObjectClassTestCase) ... ok testReadline (__main__.LineBufferedFileObjectClassTestCase) ... ok testReadlineAfterRead (__main__.LineBufferedFileObjectClassTestCase) ... ok testReadlineAfterReadNoNewline (__main__.LineBufferedFileObjectClassTestCase) ... ok testSmallRead (__main__.LineBufferedFileObjectClassTestCase) ... ok testUnbufferedRead (__main__.LineBufferedFileObjectClassTestCase) ... ok testClosedAttr (__main__.SmallBufferedFileObjectClassTestCase) ... ok testFullRead (__main__.SmallBufferedFileObjectClassTestCase) ... ok testReadline (__main__.SmallBufferedFileObjectClassTestCase) ... ok testReadlineAfterRead (__main__.SmallBufferedFileObjectClassTestCase) ... ok testReadlineAfterReadNoNewline (__main__.SmallBufferedFileObjectClassTestCase) ... ok testSmallRead (__main__.SmallBufferedFileObjectClassTestCase) ... ok testUnbufferedRead (__main__.SmallBufferedFileObjectClassTestCase) ... ok testClose (__main__.Urllib2FileobjectTest) ... ok testWithoutServer (__main__.NetworkConnectionNoServer) ... ok testFamily (__main__.NetworkConnectionAttributesTest) ... ok testTimeoutDefault (__main__.NetworkConnectionAttributesTest) ... ok testTimeoutNone (__main__.NetworkConnectionAttributesTest) ... ok testTimeoutValueNamed (__main__.NetworkConnectionAttributesTest) ... ok testTimeoutValueNonamed (__main__.NetworkConnectionAttributesTest) ... ok testInsideTimeout (__main__.NetworkConnectionBehaviourTest) ... ok testOutsideTimeout (__main__.NetworkConnectionBehaviourTest) ... ok testRecv (__main__.BasicSocketPairTest) ... ok testSend (__main__.BasicSocketPairTest) ... ok testLinuxAbstractNamespace (__main__.TestLinuxAbstractNamespace) ... ok testMaxName (__main__.TestLinuxAbstractNamespace) ... ok testNameOverflow (__main__.TestLinuxAbstractNamespace) ... ok ====================================================================== ERROR: testSockName (__main__.GeneralModuleTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "Lib/test/test_socket.py", line 474, in testSockName my_ip_addr = socket.gethostbyname(socket.gethostname()) gaierror: [Errno -2] Name or service not known ---------------------------------------------------------------------- Ran 93 tests in 11.488s FAILED (errors=1) Anybody has a quick fix? Thanks for your advise, SxN __________________________________________________________________ Instant Messaging, free SMS, sharing photos and more... Try the new Yahoo! Canada Messenger at http://ca.beta.messenger.yahoo.com/ From george.sakkis at gmail.com Mon Apr 6 17:02:26 2009 From: george.sakkis at gmail.com (George Sakkis) Date: Mon, 6 Apr 2009 14:02:26 -0700 (PDT) Subject: Returning different types based on input parameters Message-ID: That's more of a general API design question but I'd like to get an idea if and how things are different in Python context. AFAIK it's generally considered bad form (or worse) for functions/methods to return values of different "type" depending on the number, type and/or values of the passed parameters. I'm using "type" loosely in a duck- typing sense, not necessarily as a concrete class and its descendants, although I'm not sure if even duck-typing is endorsed for return values (as opposed to input parameters). For example, it is common for a function f(x) to expect x to be simply iterable, without caring of its exact type. Is it ok though for f to return a list for some types/values of x, a tuple for others and a generator for everything else (assuming it's documented), or it should always return the most general (iterator in this example) ? To take it further, what if f wants to return different types, differing even in a duck-type sense? That's easier to illustrate in a API-extension scenario. Say that there is an existing function `solve (x)` that returns `Result` instances. Later someone wants to extend f by allowing an extra optional parameter `foo`, making the signature `solve(x, foo=None)`. As long as the return value remains backward compatible, everything's fine. However, what if in the extended case, solve() has to return some *additional* information apart from `Result`, say the confidence that the result is correct ? In short, the extended API would be: def solve(x, foo=None): ''' @rtype: `Result` if foo is None; (`Result`, confidence) otherwise. ''' Strictly speaking, the extension is backwards compatible; previous code that used `solve(x)` will still get back `Result`s. The problem is that in new code you can't tell what `solve(x,y)` returns unless you know something about `y`. My question is, is this totally unacceptable and should better be replaced by a new function `solve2 (x, foo=None)` that always returns (`Result`, confidence) tuples, or it might be a justifiable cost ? Any other API extension approaches that are applicable to such situations ? George From avinashrude at gmail.com Mon Apr 6 17:09:07 2009 From: avinashrude at gmail.com (Avi) Date: Mon, 6 Apr 2009 14:09:07 -0700 (PDT) Subject: Best way to start References: <0e991af0-52f4-4849-9587-914bf38b3505@j39g2000yqn.googlegroups.com> Message-ID: <8028aa3c-09d6-4f0d-967e-055b226ce8f0@g19g2000yql.googlegroups.com> A BIG Thanks to Chris and Andrew for suggestions. This is an awesome place. namekuseijin: haha...got a friend hooked to Python on chat? hilarious! From deets at nospam.web.de Mon Apr 6 17:11:08 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 06 Apr 2009 23:11:08 +0200 Subject: decorators don't play nice with nose? In-Reply-To: References: Message-ID: <73v9fdF10v3a8U1@mid.uni-berlin.de> hyperboreean schrieb: > Hi, I am trying to test the business part of a web service. For this I > am using unittest & nose. > I wrote a decorator that should handle the xml test file retrieval, but > it seems I can't get it working with nose. > Here's the code: > > > * MyApp.py -- base test class * > > import os > import unittest > > from MyApp.Core import XmlParser > > > __all__ = ['MyAppTest', 'setup'] > > > PATH = os.path.dirname(__file__) or '' > > > class setup(object): > """Decorator to ease the use of xml files in MyApp tests. > > The way it works it that it decorates a test method which has a first > default parameter called 'parser' and it overwrites this parameter value > with a XmlParser instance. > > The xml file should be located under: > data/testedBusinessRequest/testMethodName.xml > """ > def __init__(self, testedBusinessRequest = ''): > self.testedBusinessRequest =\ > testedBusinessRequest.lower() > > > def _getXmlParser(self, xml): > documentElement = XmlParser.parseXmlStream(xml) > parser = XmlParser.getParser(documentElement) > return parser > > > def __call__(self, method): > > # TODO: error handling here > methodName = method.func_code.co_name > methodName = methodName.split('_')[1] > > xmlFolder = self.testedBusinessRequest > xmlFile = '%s.xml' % methodName > > path = os.path.join(PATH, 'data', > xmlFolder, xmlFile) > > f = open(path) > xml = f.read() > f.close() > method.func_defaults = (self._getXmlParser(xml),) > return method > > > class MyAppTest(unittest.TestCase): > > def setUp(self): > self.database = Database() > > def tearDown(self): > pass > > > * test_Login.py - test a business request * > from MyAppTest import MyAppTest, setup > > from MyApp import Login > > > class TestLogin(MyAppTest): > testedBusinessRequest = 'Login' > > @setup(testedBusinessRequest) > def test_validParameters(self, parser = None): > response = Login(self.database, parser).run() > return True > > > > Ok, so the decorator setup should fill the parser parameter with a > XmlParser object. This works well if I add a __main__ and use unittest > to run the tests. But if I use nose, I get the following error: > > *TypeError: unbound method __call__() must be called with setup instance > as first argument (got module instance instead)* > > Any advices? Nose works via the func_name parameter of a method/function. So when you decorate it, you need to make sure that is set properly. One option is to do something like this: from functools import wraps def my_decorator(f): @wraps(f) def _d(*args, **kwargs): return f(*args, **kwargs) return _d Diez From deets at nospam.web.de Mon Apr 6 17:23:47 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 06 Apr 2009 23:23:47 +0200 Subject: How to go about. On read/write locks In-Reply-To: References: <888ed340-48bd-414a-b827-8de726dc729c@h28g2000yqd.googlegroups.com> Message-ID: <73va73F10v3a8U3@mid.uni-berlin.de> > This is a classical synchronization problem with a classical solution: > You treat the readers as a group, and the writers individually. So you > have a write lock that each writer has to acquire and release, but it is > acquired only by the first reader and released by the last one. > Therefore you need a counter of the number of readers, and manipulations > of this counter must be protected by another lock. > I was going to suggest a similar approach but refused to because of a problem I see with your code as well - if the readers are reading very fast (the OP didn't state what his actual application is, so it might not be a consumer-producer scheme which I don't think a dict would be the natural choice anyway) they can block the writer from writing alltogether. Or do I miss something here? Diez From ronn.ross at gmail.com Mon Apr 6 17:24:08 2009 From: ronn.ross at gmail.com (Ronn Ross) Date: Mon, 6 Apr 2009 17:24:08 -0400 Subject: print from a python script. Message-ID: <9c8c445f0904061424p2b90968gb48aa2d777ebd05b@mail.gmail.com> I'm trying to print a simple string to a network printer. This is what I have so far: import os printer_path = "192.168.200.139" p = os.popen(printer_path, 'w') p.write("this is a printer test") p.close() I'm trying to call the printer from its IP address. When I run the script I get: sh: 192.168.200.139: not found thanks for you help in advance. -------------- next part -------------- An HTML attachment was scrubbed... URL: From deets at nospam.web.de Mon Apr 6 17:27:13 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 06 Apr 2009 23:27:13 +0200 Subject: How to go about. On read/write locks In-Reply-To: <81869878-f2cc-4468-9916-a38f498c675d@h28g2000yqd.googlegroups.com> References: <888ed340-48bd-414a-b827-8de726dc729c@h28g2000yqd.googlegroups.com> <73tmvkF10umsnU1@mid.uni-berlin.de> <81869878-f2cc-4468-9916-a38f498c675d@h28g2000yqd.googlegroups.com> Message-ID: <73vadiF11a1foU1@mid.uni-berlin.de> > Python's approach with the GIL is both reasonable and disappointing. > Reasonable because I understand how it can make things easier for its > internals. Disappointing because it means that standard python cannot > take advantage of the parallelism that can more and more often be > afforded by today's computers. I.e. I found only recently, almost by > chance, that my wife's laptop has not one but two processors, even > though it isn't a particularly high-end computer. I now understand > that OS-level threading does use them both, but I understand that the > GIL effectively prevents parallel operations. (Am I understanding > correctly?) Not entirely. Yes, if your application is CPU-bound. No if it's IO-bound. And a lot of people think that threads are actually the wrong approach for concurrency anyway, so with python2.6 there comes the multiprocessing-module that lets you use the full capacity of your CPUs. > I do not completely understand your statement in the context of my > original example though, the shared dictionary. As the GIL is released > every X bytecode operations surely it can happen that as the > dictionary is iterated through, i.e. in a for/in loop, a different > thread might change it, with potentially catastrophic consequences. > The GIL wouldn't be able to prevent this, wouldn't it? You didn't give a concrete usage scenario for your shared dict - but I assumed that by reading and writing you meant mydict[key] = value value = mydict[key] which are both atomic through the GIL. More complex operations - such as iteration - might need more coarse grained locking. Diez From clp2 at rebertia.com Mon Apr 6 17:28:57 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 6 Apr 2009 14:28:57 -0700 Subject: print from a python script. In-Reply-To: <9c8c445f0904061424p2b90968gb48aa2d777ebd05b@mail.gmail.com> References: <9c8c445f0904061424p2b90968gb48aa2d777ebd05b@mail.gmail.com> Message-ID: <50697b2c0904061428m6c8e564by10661388b8c009ed@mail.gmail.com> On Mon, Apr 6, 2009 at 2:24 PM, Ronn Ross wrote: > I'm trying to print a simple string to a network printer. This is what I > have so far: > > import os > > printer_path = "192.168.200.139" > p = os.popen(printer_path, 'w') > p.write("this is a printer test") > p.close() > > I'm trying to call the printer from its IP address. When I run the script I > get: > sh: 192.168.200.139: not found Note that os.popen() is for running /commands/, not opening files or IP addresses. I think you want the httplib or urllib modules instead. Cheers, Chris -- I have a blog: http://blog.rebertia.com From namekuseijin at gmail.com Mon Apr 6 17:35:44 2009 From: namekuseijin at gmail.com (namekuseijin) Date: Mon, 06 Apr 2009 18:35:44 -0300 Subject: Best way to start In-Reply-To: <8028aa3c-09d6-4f0d-967e-055b226ce8f0@g19g2000yql.googlegroups.com> References: <0e991af0-52f4-4849-9587-914bf38b3505@j39g2000yqn.googlegroups.com> <8028aa3c-09d6-4f0d-967e-055b226ce8f0@g19g2000yql.googlegroups.com> Message-ID: Avi escreveu: > A BIG Thanks to Chris and Andrew for suggestions. > > This is an awesome place. > > namekuseijin: haha...got a friend hooked to Python on chat? hilarious! True story. But he was already a programmer. Only Pascal Delphi though. -- a game sig: http://tinyurl.com/d3rxz9 From google at mrabarnett.plus.com Mon Apr 6 17:56:11 2009 From: google at mrabarnett.plus.com (MRAB) Date: Mon, 06 Apr 2009 22:56:11 +0100 Subject: Returning different types based on input parameters In-Reply-To: References: Message-ID: <49DA7A7B.1080408@mrabarnett.plus.com> George Sakkis wrote: > That's more of a general API design question but I'd like to get an > idea if and how things are different in Python context. AFAIK it's > generally considered bad form (or worse) for functions/methods to > return values of different "type" depending on the number, type and/or > values of the passed parameters. I'm using "type" loosely in a duck- > typing sense, not necessarily as a concrete class and its descendants, > although I'm not sure if even duck-typing is endorsed for return > values (as opposed to input parameters). > > For example, it is common for a function f(x) to expect x to be simply > iterable, without caring of its exact type. Is it ok though for f to > return a list for some types/values of x, a tuple for others and a > generator for everything else (assuming it's documented), or it should > always return the most general (iterator in this example) ? > > To take it further, what if f wants to return different types, > differing even in a duck-type sense? That's easier to illustrate in a > API-extension scenario. Say that there is an existing function `solve > (x)` that returns `Result` instances. Later someone wants to extend f > by allowing an extra optional parameter `foo`, making the signature > `solve(x, foo=None)`. As long as the return value remains backward > compatible, everything's fine. However, what if in the extended case, > solve() has to return some *additional* information apart from > `Result`, say the confidence that the result is correct ? In short, > the extended API would be: > > def solve(x, foo=None): > ''' > @rtype: `Result` if foo is None; (`Result`, confidence) > otherwise. > ''' > > Strictly speaking, the extension is backwards compatible; previous > code that used `solve(x)` will still get back `Result`s. The problem > is that in new code you can't tell what `solve(x,y)` returns unless > you know something about `y`. My question is, is this totally > unacceptable and should better be replaced by a new function `solve2 > (x, foo=None)` that always returns (`Result`, confidence) tuples, or > it might be a justifiable cost ? Any other API extension approaches > that are applicable to such situations ? > I don't like the sound of this. :-) In your example I would possibly suggest returning a 'Result' object and then later subclassing to give 'ConfidenceResult' which has the additional 'confidence' attribute. I think the only time when it's OK to return instances of different classes is when one of them is None, for example the re module where match() returns either a MatchObject (if successful) or None (if unsuccessful); apart from that, a function should always return an instance of the same class (or perhaps a subclass) or, if a collection then the same type of collection (eg always a list and never sometimes a list, sometimes a tuple). From rdmurray at bitdance.com Mon Apr 6 18:08:03 2009 From: rdmurray at bitdance.com (R. David Murray) Date: Mon, 6 Apr 2009 22:08:03 +0000 (UTC) Subject: print from a python script. References: <9c8c445f0904061424p2b90968gb48aa2d777ebd05b@mail.gmail.com> <50697b2c0904061428m6c8e564by10661388b8c009ed@mail.gmail.com> Message-ID: Chris Rebert wrote: > On Mon, Apr 6, 2009 at 2:24 PM, Ronn Ross wrote: > > I'm trying to print a simple string to a network printer. This is what I > > have so far: > > > > import os > > > > printer_path = "192.168.200.139" > > p = os.popen(printer_path, 'w') > > p.write("this is a printer test") > > p.close() > > > > I'm trying to call the printer from its IP address. When I run the script I > > get: > > sh: 192.168.200.139: not found > > Note that os.popen() is for running /commands/, not opening files or > IP addresses. I think you want the httplib or urllib modules instead. Actually in this case (_writing_ text to a printer) I think you want the Linux CUPS package for access to your remote printer, and then popen on the 'lp' command (but use subprocess.Popen instead, that's better). By the way, when you get closer to getting the above to work, you're going to want to add a '\n' to the end of that string you are writing. -- R. David Murray http://www.bitdance.com From jjposner at snet.net Mon Apr 6 18:11:14 2009 From: jjposner at snet.net (John Posner) Date: Mon, 06 Apr 2009 18:11:14 -0400 Subject: Weird Tk Canvas coordinate issue In-Reply-To: References: Message-ID: <49DA7E02.4070502@snet.net> Tim Shannon wrote: > I'm new to python, so keep that in mind. > > I have a tk Canvas that I'm trying to draw on, and I want to start my > drawing at an offset (from 0) location. So I can tweak this as I > code, I set this offset as a class level variable: > > def ClassName: > OFFSET = 20 > > def __init__(self, master)): > self.canvas = Canvas(master) > self.canvas.create_rectangle(self.OFFSET, > self.OFFSET, > 300 + self.OFFSET, > 300 + self.OFFSET, > width=2) > > > The weird thing is, it doesn't offset. If I set my offset to 100000, > it still starts drawing at 0,0. > Here's the really weird part (at least to me), if I put a print line > right about my drawing statements to print the value of the offset, it > works like it should, and offsets the drawing. > If I remove the print line, the offset goes away. > > This makes no sense to me. Tim Shannon wrote: > I'm new to python, so keep that in mind. > > I have a tk Canvas that I'm trying to draw on, and I want to start my > drawing at an offset (from 0) location. So I can tweak this as I > code, I set this offset as a class level variable: > > def ClassName: > OFFSET = 20 > > def __init__(self, master)): > self.canvas = Canvas(master) > self.canvas.create_rectangle(self.OFFSET, > self.OFFSET, > 300 + self.OFFSET, > 300 + self.OFFSET, > width=2) > The above code wouldn't even compile. Please be careful to cut-and-paste working code into your email message. (I've made this mistake myself!) Changes to make: 1. first line: change "def" to "class" 2. def __init__(self, master)): <--- get rid of extra ")" 3. Make sure to "pack" the canvas into the overall Tk window: self.canvas = Canvas(master) self.canvas.pack() <--- add this line From anishchapagain at gmail.com Mon Apr 6 18:12:14 2009 From: anishchapagain at gmail.com (Anish Chapagain) Date: Mon, 6 Apr 2009 15:12:14 -0700 (PDT) Subject: extract Infobox contents Message-ID: Hi, I was trying to extract wikipedia Infobox contents which is in format like given below, from the opened URL page in Python. {{ Infobox Software | name = Bash | logo = [[Image:bash-org.png|165px]] | screenshot = [[Image:Bash demo.png|250px]] | caption = Screenshot of bash and [[Bourne shell|sh]] sessions demonstrating some features | developer = [[Chet Ramey]] | latest release version = 4.0 | latest release date = {{release date|mf=yes|2009|02|20}} | programming language = [[C (programming language)|C]] | operating system = [[Cross-platform]] | platform = [[GNU]] | language = English, multilingual ([[gettext]]) | status = Active | genre = [[Unix shell]] | source model = [[Free software]] | license = [[GNU General Public License]] | website = [http://tiswww.case.edu/php/chet/bash/ bashtop.html Home page] }} //upto this line I need to extract all data between {{ Infobox ...to }} Thank's if anyone can help, am trying with s1='{{ Infobox' s2=len(s1) pos1=data.find("{{ Infobox") pos2=data.find("\n",pos2) pat1=data.find("}}") but am ending up getting one line at top only. thank you, From namekuseijin at gmail.com Mon Apr 6 18:17:26 2009 From: namekuseijin at gmail.com (namekuseijin) Date: Mon, 06 Apr 2009 19:17:26 -0300 Subject: Best way to start In-Reply-To: References: <0e991af0-52f4-4849-9587-914bf38b3505@j39g2000yqn.googlegroups.com> <8028aa3c-09d6-4f0d-967e-055b226ce8f0@g19g2000yql.googlegroups.com> Message-ID: Google's automatic chat logging is nice too. My first online python tutorial for someone who never saw it before (sorry for not being in english): 14/09/08 00:50 KALEL: I'm on Phyton Shell 00:52 me: cool let's go type it: 2 just to get rid of your fears... :) KALEL: Hah hah hah hah me: really 1+2 00:53 KALEL: Stupendous AND??? me: type( 1 ) type( "foobar" ) 00:54 KALEL: Good me: Python is dinamically typed, you don't declare types: a = 2 a+3 KALEL: Ok... how do I refer to the values???? 00:55 me: a ? KALEL: I mean, define some constant??? me: no constants, all variable. of course, there are scope rules 00:56 modules, classes and functions let's go by steps KALEL: Ok Morpheus me: type there: dir() to know what it is in the current scope KALEL: ['__builtins__', '__doc__', '__name__', 'a'] me: exact it's a list: [] 00:57 KALEL: I see me: type dir( [] ) to know the methods of a list object KALEL: Cool me: show 00:58 KALEL: Waitaminute: dir is a list, right? me: it's a function returning a list ls = dir() KALEL: OK. me: ls.sort 00:59 KALEL: Traceback (most recent call last): File "", line 1, in ls.sort NameError: name 'ls' is not defined me: you did not define it? ls = dir() 01:00 KALEL: me: yes ls.sort() forgot the parentheses to call ;) ls.index('a') 01:01 for i in dir(): print i 01:02 KALEL: stopped 01:03 me: one more ENTER BTW: blocks in python are idented by tabs KALEL: ok me: no {} or begin end wait 01:04 KALEL: wonderful 01:05 Ok, but when you have a single command, you don't need {} nor begin end 01:06 me: true, for that same reason I put print in the same line for and if end in : KALEL: 0x0 me: if foo: this else: 01:07 that not forgetting tab indenting KALEL: good me: if you get another nested if, more indenting KALEL: This is very good 01:08 The guy does not get lost me: let's define a function KALEL: And you don't need to worry about non-practical details me: def fact( n ): if n < 2: return 1 else: return n+fact(n-1) 01:10 you can, of course, write a program in a text file and import it 01:11 KALEL: good Guess I can already solve an Euler 01:12 me: say type def fact( n ): """Factorial of n""" if n < 2: return 1 else: return n*fact(n-1) into a text file and save as euler.py 01:13 notice documentation as part of a function 01:14 KALEL: Just did it. Opened a python instance 01:15 me: still open? KALEL: Yes me: so, type this euler.fact( 5 ) 01:16 KALEL: FAIL me: what? KALEL: typed on the shell and didnt recongnize me: import euler now should go euler.fact(5) 01:22 KALEL: Now it works! me: I forgot the import, no? :P hahahaha KALEL: yep me: sorry KALEL: Will get more coffee me: huhauhua, javaman 01:24 KALEL: Got coke me: good KALEL: while is in the same vein??? 01:26 me: i = 1 while i<10: ... print i ... i += 1 without the dots, sure 01:30 KALEL: any mod function?????? 01:32 me: % 3%2 KALEL: Yes, thanks... I looked up on help 01:33 Any easy way to sum var to itself + new result??? say: sp = sp + p1 01:34 me: sp += 3 KALEL: sp +=p1 That????? me: yep KALEL: Awesome. 01:35 me: converting a previous problem to Python? :) KALEL: No... Solving the second one me: ok 01:39 KALEL: how to end while??? indenting??? I mean, how do I close the block? 01:40 Forget me: ident all commands inside while ok ending it is newline, plainly 01:43 KALEL: finally!!!! Very good Phyton GOT IT!!!!!! me: way to go very easy, ain't it? 01:44 nothing like removing away everything non-essential KALEL: while sp<=4000000: p1=p1+p2 if p1%2 == 0: sp += p1 p2=p2+p1 if p2%2 == 0: sp += p2 me: hehe KALEL: of course, defining p1 = 2 p1 = 1 and p2 = 2 me: I was worried you would use = to compare KALEL: sp = 2 me: exactly KALEL: a bug 01:45 me: you don't even need a fib function KALEL: Then I remembered = was used for attribution me: hehe common mistake among Pascal programmers KALEL: COOL me: not even in need for OO too! :O haha 01:46 KALEL: too much... Used OOPS!! me: OOPS? KALEL: OOPS 01:47 you try, if it fails: OOPS! 01:50 KALEL: Thank you. I'll continue studying... you wait for me... now the force if with me too. me: we're right here huhauhuha 01:51 nice, young Padawan 01:52 KALEL: Thanks, Jedi Master!!!! 01:55 me: BTW, my solution will make you go nuts: def p2(): print "Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed 4 million." sum = 0 fib = 2 pfib = 1 while (fib<4000000): if fib%2==0: sum+=fib fib, pfib = fib+pfib,fib return sum 01:56 check out the excellent variables swap near the last line... :) 01:57 KALEL: AWESOME! me: yeah, no need for intermediary temp variables 01:58 I see you have a strange gleaming in the eyes, young disciple :) 01:59 KALEL: huah huah huah 02:01 me: you may also reload a module after each change reload( euler ) -- a game sig: http://tinyurl.com/d3rxz9 From rdmurray at bitdance.com Mon Apr 6 18:20:50 2009 From: rdmurray at bitdance.com (R. David Murray) Date: Mon, 6 Apr 2009 22:20:50 +0000 (UTC) Subject: Some test fail on my new Python 2.6 References: <689852.94417.qm@web56005.mail.re3.yahoo.com> Message-ID: Sorin Schwimmer wrote: > I just downloaded and compiled Python 2.6 on a Gentoo Linux, IBM NetVista. > > After going through the usual steps (./configure, make), I ran a test (make test), and got some unexpected issues, which are detailed here: > > # ./python Lib/test/test_tcl.py > Traceback (most recent call last): > File "Lib/test/test_tcl.py", line 6, in > from Tkinter import Tcl > File "/install/Python-2.6.1/Lib/lib-tk/Tkinter.py", line 39, in > import _tkinter # If this fails your Python may not be configured for Tk [...] > Anybody has a quick fix? Yep. Run 'make test' instead of running them one by one. make test will run them under regrtest, which has mechanisms for detecting tests that are expected to or may reasonably fail on a given platform. Then if you still have errors, report back :) -- R. David Murray From cosmo_general at yahoo.com Mon Apr 6 18:29:41 2009 From: cosmo_general at yahoo.com (Muddy Coder) Date: Mon, 6 Apr 2009 15:29:41 -0700 (PDT) Subject: How can I change size of GUI? Message-ID: <6dd057ac-9fb9-4979-861f-59f36e3561e7@s20g2000yqh.googlegroups.com> Hi Folks, I copied code from book: class ScrolledText(Frame): def __init__(self, parent=None, text='', file=None): Frame.__init__(self, parent) self.pack(expand=YES, fill=BOTH) self.makeWidgets() self.settext(text, file) def makeWidgets(self): sbar = Scrollbar(self) text = Text(self, relief=SUNKEN, width=120) sbar.config(command=text.yview) text.config(yscrollcommand=sbar.set) sbar.pack(side=RIGHT, fill=Y) text.pack(side=LEFT, expand=YES, fill=BOTH) self.text = text It works, of course. But, the GUI is small, and I want to enlarge it. I tried to add in options of width=120 for Text(), but it did not work. Can somebody drop me a couple of lines for help? Thanks! Muddy Coder From jhg at acm.org Mon Apr 6 18:33:54 2009 From: jhg at acm.org (Jim Garrison) Date: Mon, 06 Apr 2009 17:33:54 -0500 Subject: New online docs broken? Message-ID: <49da837c$0$29401$4c5ecfc7@fe1.usenet.com> I notice the online docs (at docs.python.org/3.0/index.html) were updated today. It seems some of the top-level pages, like Tutorial, "Using Python", "Language Reference" are truncated after the first few paragraphs. From jcd at sdf.lonestar.org Mon Apr 6 18:37:28 2009 From: jcd at sdf.lonestar.org (J. Clifford Dyer) Date: Mon, 06 Apr 2009 18:37:28 -0400 Subject: Eval Problem In-Reply-To: <4dc0cfea0904061211u44ba605bx8638414144025bc0@mail.gmail.com> References: <4dc0cfea0904061211u44ba605bx8638414144025bc0@mail.gmail.com> Message-ID: <1239057448.25384.9.camel@mctell> On Mon, 2009-04-06 at 15:11 -0400, Victor Subervi wrote: > Hi: > I have this code: > > x = 1 > while x <= bitties: > file = open(p + str(x) + ".txt") > for line in file: > print line > print eval(bits[x - 1]) > x += 1 > > which throws this error: > > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] > PythonHandler mod_python.cgihandler: Traceback (most recent call > last): > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] > PythonHandler mod_python.cgihandler: File > "/usr/lib64/python2.4/site-packages/mod_python/apache.py", line 299, > in HandlerDispatch\n result = object(req) > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] > PythonHandler mod_python.cgihandler: File > "/usr/lib64/python2.4/site-packages/mod_python/cgihandler.py", line > 96, in handler\n imp.load_module(module_name, fd, path, desc) > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] > PythonHandler mod_python.cgihandler: File > "/var/www/vhosts/articles.13gems.com/httpdocs/index_frame.py", line > 89, in ?\n print eval(bits[1]) > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] > PythonHandler mod_python.cgihandler: File "", line 1 > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] > PythonHandler mod_python.cgihandler: tableBottom(348,180) > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] > PythonHandler mod_python.cgihandler: ^ > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] > PythonHandler mod_python.cgihandler: SyntaxError: invalid syntax > Hmm. That's not the problem I get. For me, your code raises: "NameError: name 'bitties' is not defined" It's easier for us to help you if you present a self-contained piece of code that exhibits the problem you've encountered. Creating that code will often reveal the problem with the original code, and you will have solved your own problem. The other problem with your code is that you are using eval. Eval leads to difficult-to-debug errors, and is usually unnecessary, given the dynamic nature of python. If you need to access a class method using a string, you can use getattr. If you need to dynamically select a function based on some condition known in another variable, you can use a dictionary to hash into the function. It's hard to say what might be appropriate for your situation, because your code fragment is so... fragmentary. Cheers, Cliff From rhodri at wildebst.demon.co.uk Mon Apr 6 18:41:48 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Mon, 06 Apr 2009 23:41:48 +0100 Subject: extract Infobox contents In-Reply-To: References: Message-ID: On Mon, 06 Apr 2009 23:12:14 +0100, Anish Chapagain wrote: > Hi, > I was trying to extract wikipedia Infobox contents which is in format > like given below, from the opened URL page in Python. > > {{ Infobox Software > | name = Bash > | logo = [[Image:bash-org.png|165px]] > | screenshot = [[Image:Bash demo.png|250px]] > | caption = Screenshot of bash and [[Bourne shell|sh]] > sessions demonstrating some features > | developer = [[Chet Ramey]] > | latest release version = 4.0 > | latest release date = {{release date|mf=yes|2009|02|20}} > | programming language = [[C (programming language)|C]] > | operating system = [[Cross-platform]] > | platform = [[GNU]] > | language = English, multilingual ([[gettext]]) > | status = Active > | genre = [[Unix shell]] > | source model = [[Free software]] > | license = [[GNU General Public License]] > | website = [http://tiswww.case.edu/php/chet/bash/ > bashtop.html Home page] > }} //upto this line > > I need to extract all data between {{ Infobox ...to }} > > Thank's if anyone can help, > am trying with > > s1='{{ Infobox' > s2=len(s1) > pos1=data.find("{{ Infobox") > pos2=data.find("\n",pos2) > > pat1=data.find("}}") > > but am ending up getting one line at top only. How are you getting your data? Assuming that you can arrange to get it one line at a time, here's a quick and dirty way to extract the infoboxes on a page. infoboxes = [] infobox = [] reading_infobox = False for line in feed_me_lines_somehow(): if line.startswith("{{ Infobox"): reading_infobox = True if reading_infobox: infobox.append(line) if line.startswith("}}"): reading_infobox = False infoboxes.append(infobox) infobox = [] You end up with 'infoboxes' containing a list of all the infoboxes on the page, each held as a list of the lines of their content. For safety's sake you really should be using regular expressions rather than 'startswith', but I leave that as an exercise for the reader :-) -- Rhodri James *-* Wildebeeste Herder to the Masses From r-w at inet.net.au Mon Apr 6 18:44:17 2009 From: r-w at inet.net.au (r-w) Date: Tue, 07 Apr 2009 08:44:17 +1000 Subject: Web validation Message-ID: <49DA85C1.4090506@inet.net.au> If no internet connection: if have files: run anyway, with warning else: ERROR else: if error getting hash/files: if have files: run anyway, with warning else: ERROR else: run From steve at REMOVE-THIS-cybersource.com.au Mon Apr 6 18:50:00 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 06 Apr 2009 22:50:00 GMT Subject: Returning different types based on input parameters References: Message-ID: <01ea7b46$0$20654$c3e8da3@news.astraweb.com> On Mon, 06 Apr 2009 14:02:26 -0700, George Sakkis wrote: > That's more of a general API design question but I'd like to get an idea > if and how things are different in Python context. AFAIK it's generally > considered bad form (or worse) for functions/methods to return values of > different "type" depending on the number, type and/or values of the > passed parameters. I'm using "type" loosely in a duck- typing sense, not > necessarily as a concrete class and its descendants, although I'm not > sure if even duck-typing is endorsed for return values (as opposed to > input parameters). > > For example, it is common for a function f(x) to expect x to be simply > iterable, without caring of its exact type. Is it ok though for f to > return a list for some types/values of x, a tuple for others and a > generator for everything else (assuming it's documented), or it should > always return the most general (iterator in this example) ? Arguably, if the only promise you make is that f() returns an iterable, then you could return any of list, tuple etc and still meet that promise. I'd consider that acceptable but eccentric. However, I'd consider it bad form to *not* warn that the actual type returned is an implementation detail that may vary. Alternatively, I'm very fond of what the built-in filter function does: it tries to match the return type to the input type, so that if you pass a string as input, it returns a string, and if you pass it a tuple, it returns a tuple. > To take it further, what if f wants to return different types, differing > even in a duck-type sense? That's easier to illustrate in a > API-extension scenario. Say that there is an existing function `solve > (x)` that returns `Result` instances. Later someone wants to extend f > by allowing an extra optional parameter `foo`, making the signature > `solve(x, foo=None)`. As long as the return value remains backward > compatible, everything's fine. However, what if in the extended case, > solve() has to return some *additional* information apart from `Result`, > say the confidence that the result is correct ? In short, the extended > API would be: > > def solve(x, foo=None): > ''' > @rtype: `Result` if foo is None; (`Result`, confidence) > otherwise. > ''' > > Strictly speaking, the extension is backwards compatible; previous code > that used `solve(x)` will still get back `Result`s. The problem is that > in new code you can't tell what `solve(x,y)` returns unless you know > something about `y`. My question is, is this totally unacceptable and > should better be replaced by a new function `solve2 (x, foo=None)` that > always returns (`Result`, confidence) tuples, or it might be a > justifiable cost ? Any other API extension approaches that are > applicable to such situations ? I dislike that, although I've been tempted to write functions like that myself. Better, I think, to create a second function, xsolve() which takes a second argument, and refactor the common parts of solve/xsolve out into a third private function so you avoid code duplication. -- Steven From rhodri at wildebst.demon.co.uk Mon Apr 6 18:56:36 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Mon, 06 Apr 2009 23:56:36 +0100 Subject: group several methods under a attribute In-Reply-To: References: <746bd28c-9ea6-44d0-80ea-1292f60ede73@r37g2000yqn.googlegroups.com> Message-ID: On Mon, 06 Apr 2009 20:52:50 +0100, jelle wrote: > Hi Aaron, > > Thanks a lot for your suggestions. > I wasnt familiar with the __get__ magic, which seems interesting. > > So, finally it seems that the cleanest pattern is: > > class ClsA( object ): > def __init__( self, other ): > self.inst= other > > def submethA( self, arg ): > print( 'submethA %r, instance %r'% ( arg, self.inst ) ) > > class ClsB( object ): > def methA( self, arg ): > self.A= ClsA( self ) > print( 'methA %r'% arg ) > > b= ClsB( ) > b.methA( 'this' ) > b.A.submethA( 'that' ) You'll note that this is, all Aaron's protests to the contrary, splitting your class up into multiple cooperating classes. If you're set on doing it like this, doing it this way avoids polluting your namespace so much: class ClsB(object): class ClsA(object): def do_something(self): print "Here's A doing something" def __init__(self): self.A = ClsB.ClsA() def do_something(self): print "Here's B doing something" b = ClsB() b.do_something() b.A.do_something() -- Rhodri James *-* Wildebeeste Herder to the Masses From george.sakkis at gmail.com Mon Apr 6 18:56:45 2009 From: george.sakkis at gmail.com (George Sakkis) Date: Mon, 6 Apr 2009 15:56:45 -0700 (PDT) Subject: Returning different types based on input parameters References: Message-ID: <63ca016a-9b5a-466b-b083-30fc66cbe14d@y7g2000yqa.googlegroups.com> On Apr 6, 5:56?pm, MRAB wrote: > In your example I would possibly suggest returning a 'Result' object and > then later subclassing to give 'ConfidenceResult' which has the > additional 'confidence' attribute. That's indeed one option, but not very appealing if `Result` happens to be a builtin (e.g. float or list). Technically you can subclass builtins but I think, in this case at least, the cure is worse than the disease. George From sjmachin at lexicon.net Mon Apr 6 19:23:58 2009 From: sjmachin at lexicon.net (John Machin) Date: Mon, 6 Apr 2009 16:23:58 -0700 (PDT) Subject: speed of string chunks file parsing References: Message-ID: <8980d334-d499-43ad-8bdb-80df71183116@j18g2000prm.googlegroups.com> On Apr 6, 11:48?pm, Hyunchul Kim wrote: > Hi, all > > I have a simple script. > Can you improve algorithm of following 10 line script, with a view point > of speed ? > Following script do exactly what I want but I want to improve the speed. So the first thing to do is to try to work out where it is spending the time. Step 1: read your code: uh-oh: re.compile('^a complex regular expression here$) By the way, you are using match() so the "^" is redundant. Step 2: Run some timing tests: (a) script as written (b) script modified to do nothing else but count the lines in the file. Is the time difference worth any further work? If so: (c) script modified to ignore the regex and pretend the match fails on each line (d) script modified to ignore the regex and pretend the match succeeds on each line (e) script modified to ignore the appending and yielding; just count how many matches So where is most of the time(a) - time(b) difference being taken: matching or appending/yielding? > > This parse a file and accumulate lines till a line match a given regular > expression. > Then, when a line match a given regular expression, this function yield > lines before the matched lines. I think your code documents the requirement slightly better. Before you start worrying about speed, it's a good idea to ensure that the requirements are correct. It would be useful to include explicit statements to this effect: Given N matching lines, there will be (N+1) yields. The last yield will contain the lines (if any) after the last matching line (if any). No other yield will be empty. Consequently: an empty file will yield [], and a file with no matching lines will yield [all the lines in the file]. Presuming of course that is what you want; consider this: first yield: [line1, line2] second and final yield: [line3, line4] The caller has no way of knowing (without using the regex!) if the last line in the file matches the regex or not. Does this matter? > > **************** > import re > resultlist = [] > cp_regularexpression = re.compile('^a complex regular expression here$) > for line in file(inputfile): > ? ? ? ? if cp_regularexpression.match(line): > ? ? ? ? ? ? ? ? if resultlist != []: if resultlist: # slightly faster and much less ugly > ? ? ? ? ? ? ? ? ? ? ? ? yield resultlist > ? ? ? ? ? ? ? ? ? ? ? ? resultlist = [] > ? ? ? ? resultlist.append(line) > yield resultlist If it turns out that you don't want lines after the last matching line, you need to remove the above yield statement. HTH, John From vicye6 at gmail.com Mon Apr 6 19:40:32 2009 From: vicye6 at gmail.com (Ye Liu) Date: Mon, 6 Apr 2009 16:40:32 -0700 (PDT) Subject: New online docs broken? References: <49da837c$0$29401$4c5ecfc7@fe1.usenet.com> Message-ID: <94ae55e9-265c-4444-8bba-316001db83e1@r37g2000yqn.googlegroups.com> On Apr 6, 6:33?pm, Jim Garrison wrote: > I notice the online docs (at docs.python.org/3.0/index.html) were > updated today. ?It seems some of the top-level pages, like > Tutorial, "Using Python", "Language Reference" are truncated > after the first few paragraphs. Yea, same here. Hope it's going to be fixed soon. From andrew at acooke.org Mon Apr 6 19:53:01 2009 From: andrew at acooke.org (andrew cooke) Date: Mon, 6 Apr 2009 19:53:01 -0400 (CLT) Subject: Returning different types based on input parameters In-Reply-To: References: Message-ID: <43aa610b30145a92914205dbb7e43078.squirrel@localhost> George Sakkis wrote: > That's more of a general API design question but I'd like to get an > idea if and how things are different in Python context. AFAIK it's > generally considered bad form (or worse) for functions/methods to > return values of different "type" depending on the number, type and/or > values of the passed parameters. I'm using "type" loosely in a duck- > typing sense, not necessarily as a concrete class and its descendants, > although I'm not sure if even duck-typing is endorsed for return > values (as opposed to input parameters). [...] you probably want to look up substitutability: http://www.google.cl/search?q=substitutability+principle andrew From andrew at acooke.org Mon Apr 6 19:57:22 2009 From: andrew at acooke.org (andrew cooke) Date: Mon, 6 Apr 2009 19:57:22 -0400 (CLT) Subject: Returning different types based on input parameters In-Reply-To: <43aa610b30145a92914205dbb7e43078.squirrel@localhost> References: <43aa610b30145a92914205dbb7e43078.squirrel@localhost> Message-ID: <79295db462df0c79bce82d939e41ad58.squirrel@localhost> andrew cooke wrote: > George Sakkis wrote: >> That's more of a general API design question but I'd like to get an >> idea if and how things are different in Python context. AFAIK it's >> generally considered bad form (or worse) for functions/methods to >> return values of different "type" depending on the number, type and/or >> values of the passed parameters. I'm using "type" loosely in a duck- >> typing sense, not necessarily as a concrete class and its descendants, >> although I'm not sure if even duck-typing is endorsed for return >> values (as opposed to input parameters). > [...] > > you probably want to look up substitutability: > http://www.google.cl/search?q=substitutability+principle actually, this is better: http://www.google.cl/search?q=substitution+principle the idea being that if the "contract" for your function is that it returns a certain type, then any subclass should also be ok (alternatively, that subclasses should be written so that they can be returned when a caller was expecting the superclass) andrew From tdelaney at avaya.com Mon Apr 6 20:00:09 2009 From: tdelaney at avaya.com (Delaney, Timothy (Tim)) Date: Tue, 7 Apr 2009 08:00:09 +0800 Subject: with open('com1', 'r') as f: In-Reply-To: Message-ID: Lawrence D'Oliveiro wrote: > In message , > Terry Reedy wrote: > >> Lawrence D'Oliveiro wrote: >> >>> All Python objects are reference-counted. >> >> Nope. Only in CPython, and even that could change. > > Why should it? Because Guido has said it might some time in the future. >>> Once the file object becomes >>> inaccessible, it is automatically closed. Simple. >> >> Even in CPython, that would not be true now is the object became >> involved in or became a dependent of a reference cycle. > > And how exactly would that happen with a file object? Create a cycle of objects, and have one of them reference a file object. Tim Delaney From ndbecker2 at gmail.com Mon Apr 6 20:05:51 2009 From: ndbecker2 at gmail.com (Neal Becker) Date: Mon, 06 Apr 2009 20:05:51 -0400 Subject: more fun with iterators (mux, demux) Message-ID: I'm trying to make a multiplexor and demultiplexor, using generators. The multiplexor will multiplex N sequences -> 1 sequence (assume equal length). The demultiplexor will do the inverse. The mux seems easy enough: ----------------------- def mux (*ranges): iterables = [iter (r) for r in ranges] while (True): for i in (iterables): yield i.next() def test_mux (): a = xrange (10) b = xrange (10,20) return mux (a, b) x = test_mux() print [e for e in x] ------------------------ The demux has me stumped. The demux should return a tuple of N generators. The signature should be: def demux (sequence, N): Not sure about this one. From clp2 at rebertia.com Mon Apr 6 20:08:04 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 6 Apr 2009 17:08:04 -0700 Subject: Web validation In-Reply-To: <49DA85C1.4090506@inet.net.au> References: <49DA85C1.4090506@inet.net.au> Message-ID: <50697b2c0904061708h430afd72h9b67115a7224cb8b@mail.gmail.com> On Mon, Apr 6, 2009 at 3:44 PM, r-w wrote: > If no internet connection: Try loading http://example.com using urllib (http://docs.python.org/library/urllib.html). If an exception gets raised, you're not connected (properly). Cheers, Chris -- I have a blog: http://blog.rebertia.com From nospam at nospam.invalid Mon Apr 6 20:09:21 2009 From: nospam at nospam.invalid (Rahul) Date: Tue, 7 Apr 2009 00:09:21 +0000 (UTC) Subject: what does "execfile" mean within profiler output and why does it not have a attached line number References: Message-ID: Robert Kern wrote in news:mailman.3316.1238893185.11746.python-list at python.org: > To quickly find your hotspots, start by sorting by 'time' (that would > be displayed as the 'tottime' column in the human-readable output). > That tells you how much time is spent in each function itself, > excluding the time it spends calling out to other functions. For > example, per the docs under "Instant User? Ts Manual" (which you might > want to spend a little more time with): > > p.sort_stats('time').print_stats(10) > > Thanks Robert. I was executing the profiler on the commandline like so: python -m profile ~/bin/visualize.py *.nc Is there any way to pass further options of the form sort etc. via this invocation. The manual did not specify usage of this form. -- Rahul From fetchinson at googlemail.com Mon Apr 6 20:41:33 2009 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Mon, 6 Apr 2009 17:41:33 -0700 Subject: decorator module in stdlib? Message-ID: The decorator module [1] written by Michele Simionato is a very useful tool for maintaining function signatures while applying a decorator. Many different projects implement their own versions of the same functionality, for example turbogears has its own utility for this, I guess others do something similar too. Was the issue whether to include this module in the stdlib raised? If yes, what were the arguments against it? If not, what do you folks think, shouldn't it be included? I certainly think it should be. Cheers, Daniel [1] http://pypi.python.org/pypi/decorator -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From clp2 at rebertia.com Mon Apr 6 20:50:05 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 6 Apr 2009 17:50:05 -0700 Subject: decorator module in stdlib? In-Reply-To: References: Message-ID: <50697b2c0904061750j3fc3fb5bwcab248cc82dbf839@mail.gmail.com> On Mon, Apr 6, 2009 at 5:41 PM, Daniel Fetchinson wrote: > The decorator module [1] written by Michele Simionato is a very useful > tool for maintaining function signatures while applying a decorator. > Many different projects implement their own versions of the same > functionality, for example turbogears has its own utility for this, I > guess others do something similar too. > > Was the issue whether to include this module in the stdlib raised? If > yes, what were the arguments against it? If not, what do you folks > think, shouldn't it be included? I certainly think it should be. Similar functionality is already provided by functools.update_wrapper() and functools.wraps(). See http://docs.python.org/library/functools.html You might consider proposing the modification of these functions instead. Cheers, Chris -- I have a blog: http://blog.rebertia.com From pavlovevidence at gmail.com Mon Apr 6 21:44:15 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Mon, 6 Apr 2009 18:44:15 -0700 (PDT) Subject: How to go about. On read/write locks References: <888ed340-48bd-414a-b827-8de726dc729c@h28g2000yqd.googlegroups.com> <73tmvkF10umsnU1@mid.uni-berlin.de> <81869878-f2cc-4468-9916-a38f498c675d@h28g2000yqd.googlegroups.com> Message-ID: On Apr 6, 3:30?am, "Emanuele D'Arrigo" wrote: > Python's approach with the GIL is both reasonable and disappointing. > Reasonable because I understand how it can make things easier for its > internals. Disappointing because it means that standard python cannot > take advantage of the parallelism that can more and more often be > afforded by today's computers. I.e. I found only recently, almost by > chance, that my wife's laptop has not one but two processors, even > though it isn't a particularly high-end computer. I now understand > that OS-level threading does use them both, but I understand that the > GIL effectively prevents parallel operations. (Am I understanding > correctly?) Mostly, but keep in mind that non-Python code can run on a different core at the same time. This could be stuff like I/O or numerical calcuations written in C. > I do not completely understand your statement in the context of my > original example though, the shared dictionary. As the GIL is released > every X bytecode operations surely it can happen that as the > dictionary is iterated through, i.e. in a for/in loop, a different > thread might change it, with potentially catastrophic consequences. > The GIL wouldn't be able to prevent this, wouldn't it? It'll prevent catastrophic consequences such as segfaults, yes. It won't prevent incorrect results, or some other thread changing something under your feet, though. Also, I believe dicts know when they're being iterated over and will raise an exception on any attempt to add or remove a new key, so at worst you might get a value changed under your feet. I would say, therefore, that as long as you are only modifying values, and not adding or removing them, Diez is correct. (Someone more familiar with dict internals might want to verify.) Carl Banks From steven at REMOVE.THIS.cybersource.com.au Mon Apr 6 22:05:59 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 07 Apr 2009 02:05:59 GMT Subject: more fun with iterators (mux, demux) References: Message-ID: On Mon, 06 Apr 2009 20:05:51 -0400, Neal Becker wrote: > I'm trying to make a multiplexor and demultiplexor, using generators. > The multiplexor will multiplex N sequences -> 1 sequence (assume equal > length). The demultiplexor will do the inverse. > > The mux seems easy enough: > > ----------------------- > def mux (*ranges): > iterables = [iter (r) for r in ranges] while (True): > for i in (iterables): > yield i.next() This is like a zip, and can be re-written using itertools.izip. def mux(*iterables): for i in itertools.izip(*iterables): for item in i: yield item The demuxer can't be an iterator, since it needs to run through the entire collection. def demux(it, n): collectors = [[] for i in xrange(n)] i = 0 for item in it: collectors[i].append(item) i = (i+1) % n return tuple([iter(x) for x in collectors]) -- Steven From pavlovevidence at gmail.com Mon Apr 6 22:09:02 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Mon, 6 Apr 2009 19:09:02 -0700 (PDT) Subject: How to go about. On read/write locks References: <888ed340-48bd-414a-b827-8de726dc729c@h28g2000yqd.googlegroups.com> <73va73F10v3a8U3@mid.uni-berlin.de> Message-ID: <523a2f2e-5ae4-417e-909b-0dec94a4ee56@k41g2000yqh.googlegroups.com> On Apr 6, 2:23?pm, "Diez B. Roggisch" wrote: > > This is a classical synchronization problem with a classical solution: > > You treat the readers as a group, and the writers individually. So you > > have a write lock that each writer has to acquire and release, but it is > > acquired only by the first reader and released by the last one. > > Therefore you need a counter of the number of readers, and manipulations > > of this counter must be protected by another lock. > > I was going to suggest a similar approach but refused to because of a > problem I see with your code as well - if the readers are reading very > fast (the OP didn't state what his actual application is, so it might > not be a consumer-producer scheme which I don't think a dict would be > the natural choice anyway) they can block the writer from writing > alltogether. You could implement some kind of fair ordering where whoever requests a lock first is guaranteed to get it first, but I can't think of a way to do that without requiring all readers to acquire two locks. Carl Banks From steven at REMOVE.THIS.cybersource.com.au Mon Apr 6 22:19:42 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 07 Apr 2009 02:19:42 GMT Subject: object knows which object called it? References: <662de4f7-d8a3-4ed8-bb02-5571e33a3304@k8g2000yqn.googlegroups.com> Message-ID: On Mon, 06 Apr 2009 07:53:55 -0700, Reckoner wrote: > hi, > > I have the following problem: I have two objects, say, A and B, which > are both legitimate stand-alone objects with lives of their own. > > A contains B as a property, so I often do > > A.B.foo() > > the problem is that some functions inside of B actually need A (remember > I said they were both standalone objects), You contradict yourself. If the methods inside B *need* A, then B is NOT a standalone object, it has a dependency, namely A. > so I have to often do: > > A.B.foo_func(A) > > Which is kind of awkward. > > Is there some way that B.foo_func() could somehow know that it was > called as a property of A in this way? Not in any nice way. There's probably some sort of deep evil black magic that would work, for some definition of "work" that includes the phrase "fragile, incomprehensible and risky". The solution is to admit your dependency instead of living in denial, and tell B who owns it: B.owner = A Now B.foo_func() can refer to B.owner and all is good. For advanced work, you can make B.owner a weakref to A, and avoid creating a reference cycle which can sometimes be tricky to deal with. -- Steven From ldo at geek-central.gen.new_zealand Mon Apr 6 22:49:18 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Tue, 07 Apr 2009 14:49:18 +1200 Subject: with open('com1', 'r') as f: References: Message-ID: In message , Delaney, Timothy (Tim) wrote: > Lawrence D'Oliveiro wrote: > >> In message , >> Terry Reedy wrote: >> >>> Lawrence D'Oliveiro wrote: >>> >>>> All Python objects are reference-counted. >>> >>> Nope. Only in CPython, and even that could change. >> >> Why should it? > > Because Guido has said it might some time in the future. It's still a matter of efficiency, not correctness. >>>> Once the file object becomes >>>> inaccessible, it is automatically closed. Simple. >>> >>> Even in CPython, that would not be true now is the object became >>> involved in or became a dependent of a reference cycle. >> >> And how exactly would that happen with a file object? > > Create a cycle of objects, and have one of them reference a file object. Which was not what was happening in the actual situation at hand. From semanticist at gmail.com Mon Apr 6 22:58:53 2009 From: semanticist at gmail.com (Miles) Date: Mon, 6 Apr 2009 22:58:53 -0400 Subject: more fun with iterators (mux, demux) In-Reply-To: References: Message-ID: On Mon, Apr 6, 2009 at 8:05 PM, Neal Becker wrote: > I'm trying to make a multiplexor and demultiplexor, using generators. ?The > multiplexor will multiplex N sequences -> 1 sequence ?(assume equal length). > The demultiplexor will do the inverse. > > The demux has me stumped. ?The demux should return a tuple of N generators. from itertools import islice, tee def demux(iterable, n): return tuple(islice(it, i, None, n) for (i, it) in enumerate(tee(iterable, n))) -Miles From sxn02 at yahoo.com Mon Apr 6 23:14:52 2009 From: sxn02 at yahoo.com (Sorin Schwimmer) Date: Mon, 6 Apr 2009 20:14:52 -0700 (PDT) Subject: Some test fail on my new Python 2.6 Message-ID: <344374.9276.qm@web56005.mail.re3.yahoo.com> > Run 'make test' instead of running them one by one. I did it *before* going one by one. I then tried individual tests in hope of getting hints of what to look for. Actualy, for the first three tests, the "make test" script commented that it is unusual for them to fail on my platform (linux2). SxN __________________________________________________________________ Instant Messaging, free SMS, sharing photos and more... Try the new Yahoo! Canada Messenger at http://ca.beta.messenger.yahoo.com/ From ntv1534 at gmail.com Mon Apr 6 23:17:43 2009 From: ntv1534 at gmail.com (MooMaster) Date: Mon, 6 Apr 2009 20:17:43 -0700 (PDT) Subject: in place list modification necessary? What's a better idiom? Message-ID: <7790d731-109b-4373-986f-c28699e29a6b@b7g2000pre.googlegroups.com> A similar discussion has already occurred, over 4 years ago: http://groups.google.com/group/comp.lang.python/browse_thread/thread/b806ada0732643d/5dff55826a199928?lnk=gst&q=list+in+place#5dff55826a199928 Nevertheless, I have a use-case where such a discussion comes up. For my data mining class I'm writing an implementation of the bisecting KMeans clustering algorithm (if you're not familiar with clustering and are interested, this gives a decent example based overview: http://rakaposhi.eas.asu.edu/cse494/notes/f02-clustering.ppt). Given a CSV dataset of n records, we are to cluster them accordingly. The dataset is generalizable enough to have any kind of data-type (strings, floats, booleans, etc) for each of the record's columnar values, for example here's a couple of records from the famous iris dataset: 5.1,3.5,1.4,0.2,Iris-setosa 6.4,3.2,4.5,1.5,Iris-versicolor Now we can't calculate a meaningful Euclidean distance for something like "Iris-setosa" and "Iris-versicolor" unless we use string-edit distance or something overly complicated, so instead we'll use a simple quantization scheme of enumerating the set of values within the column domain and replacing the strings with numbers (i.e. Iris-setosa = 1, iris-versicolor=2). So I'm reading in values from a file, and for each column I need to dynamically discover the range of possible values it can take and quantize if necessary. This is the solution I've come up with: def createInitialCluster(fileName): #get the data from the file points = [] with open(fileName, 'r') as f: for line in f: points.append(line.rstrip('\n')) #clean up the data fixedPoints = [] for point in points: dimensions = [quantize(i, points, point.split(",").index(i)) for i in point.split(",")] print dimensions fixedPoints.append(Point(dimensions)) #return an initial cluster of all the points return Cluster(fixedPoints) def quantize(stringToQuantize, pointList, columnIndex): #if it's numeric, no need to quantize if(isNumeric(stringToQuantize)): return float(stringToQuantize) #first we need to discover all the possible values of this column domain = [] for point in pointList: domain.append(point.split(",")[columnIndex]) #make it a set to remove duplicates domain = list(Set(domain)) #use the index into the domain as the number representing this value return float(domain.index(stringToQuantize)) #harvested from http://www.rosettacode.org/wiki/IsNumeric#Python def isNumeric(string): try: i = float(string) except ValueError: return False return True It works, but it feels a little ugly, and not exactly Pythonic. Using two lists I need the original point list to read in the data, then the dimensions one to hold the processed point, and a fixedPoint list to make objects out of the processed data. If my dataset is in the order of millions, this'll nuke the memory. I tried something like: for point in points: point = Point([quantize(i, points, point.split(",").index(i)) for i in point.split(",")]) but when I print out the points afterward, it doesn't keep the changes. What's a more efficient way of doing this? From robert.kern at gmail.com Mon Apr 6 23:19:47 2009 From: robert.kern at gmail.com (Robert Kern) Date: Mon, 06 Apr 2009 22:19:47 -0500 Subject: what does "execfile" mean within profiler output and why does it not have a attached line number In-Reply-To: References: Message-ID: On 2009-04-06 19:09, Rahul wrote: > Robert Kern wrote in > news:mailman.3316.1238893185.11746.python-list at python.org: > >> To quickly find your hotspots, start by sorting by 'time' (that would >> be displayed as the 'tottime' column in the human-readable output). >> That tells you how much time is spent in each function itself, >> excluding the time it spends calling out to other functions. For >> example, per the docs under "Instant User? Ts Manual" (which you might >> want to spend a little more time with): >> >> p.sort_stats('time').print_stats(10) > > Thanks Robert. I was executing the profiler on the commandline like so: > > python -m profile ~/bin/visualize.py *.nc > > Is there any way to pass further options of the form sort etc. via this > invocation. The manual did not specify usage of this form. $ python -m profile --help Usage: profile.py [-o output_file_path] [-s sort] scriptfile [arg] ... Options: -h, --help show this help message and exit -o OUTFILE, --outfile=OUTFILE Save stats to -s SORT, --sort=SORT Sort order when printing to stdout, based on pstats.Stats class Additionally, you can run pstats on the outfile for an interactive viewer: $ python -m pstats visualize.prof -- 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 semanticist at gmail.com Mon Apr 6 23:35:25 2009 From: semanticist at gmail.com (Miles) Date: Mon, 6 Apr 2009 23:35:25 -0400 Subject: more fun with iterators (mux, demux) In-Reply-To: References: Message-ID: On Mon, Apr 6, 2009 at 10:05 PM, Steven D'Aprano wrote: > On Mon, 06 Apr 2009 20:05:51 -0400, Neal Becker wrote: > >> I'm trying to make a multiplexor and demultiplexor, using generators. >> The multiplexor will multiplex N sequences -> 1 sequence ?(assume equal >> length). The demultiplexor will do the inverse. >> >> The mux seems easy enough: >> >> ----------------------- >> def mux (*ranges): >> ? ? iterables = [iter (r) for r in ranges] while (True): >> ? ? ? ? for i in (iterables): >> ? ? ? ? ? ? yield i.next() > > > This is like a zip, and can be re-written using itertools.izip. > > def mux(*iterables): > ? ?for i in itertools.izip(*iterables): > ? ? ? ?for item in i: > ? ? ? ? ? ?yield item In Python 2.6, you could also do this: def mux(*iterables): return itertools.chain.from_iterable(itertools.izip(*iterables)) -Miles From michele.simionato at gmail.com Tue Apr 7 00:20:02 2009 From: michele.simionato at gmail.com (Michele Simionato) Date: Mon, 6 Apr 2009 21:20:02 -0700 (PDT) Subject: decorator module in stdlib? References: Message-ID: On Apr 7, 2:50?am, Chris Rebert wrote: > Similar functionality is already provided by > functools.update_wrapper() and functools.wraps(). > Seehttp://docs.python.org/library/functools.html > You might consider proposing the modification of these functions instead. Unfortunately functools.update_wrapper() and functools.wraps() are NOT equivalent to the decorator module. My original plan when I wrote the decorator module was to make aware people that the corrent support for decorators in the language is insufficient. I have always seen the decorator module as a temporary hack waiting for a proper solution at the language level. I wanted the possibility to modify the signature of a function. Everybody more or less agreed that this was a good idea on python-dev, and there is also a PEP: http://www.python.org/dev/peps/pep-0362/ However, AFAIK, the PEP is still in draft status. Perhaps people wanting this should make some noise on python-dev. Michele Simionato From michele.simionato at gmail.com Tue Apr 7 00:23:36 2009 From: michele.simionato at gmail.com (Michele Simionato) Date: Mon, 6 Apr 2009 21:23:36 -0700 (PDT) Subject: decorators don't play nice with nose? References: <73v9fdF10v3a8U1@mid.uni-berlin.de> Message-ID: <73baf7d3-c193-4d4a-bb84-1114a24bad51@a7g2000yqk.googlegroups.com> On Apr 6, 11:11?pm, "Diez B. Roggisch" wrote: > Nose works via the func_name parameter of a method/function. > > So when you decorate it, you need to make sure that is set properly. One > option is to do something like this: > > from functools import wraps > > def my_decorator(f): > ? ? @wraps(f) > ? ? def _d(*args, **kwargs): > ? ? ? ? return f(*args, **kwargs) > > ? ? return _d Or you can use the decorator module: http://pypi.python.org/pypi/decorator From pavlovevidence at gmail.com Tue Apr 7 01:43:45 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Mon, 6 Apr 2009 22:43:45 -0700 (PDT) Subject: in place list modification necessary? What's a better idiom? References: <7790d731-109b-4373-986f-c28699e29a6b@b7g2000pre.googlegroups.com> Message-ID: <688a5e3d-38f5-45d9-a115-ca43435fa8e9@i28g2000prd.googlegroups.com> MooMaster wrote: > So I'm reading in values from a file, and for each column I need to > dynamically discover the range of possible values it can take and > quantize if necessary. This is the solution I've come up with: > > > def createInitialCluster(fileName): > #get the data from the file > points = [] > with open(fileName, 'r') as f: > for line in f: > points.append(line.rstrip('\n')) > #clean up the data > fixedPoints = [] > for point in points: > dimensions = [quantize(i, points, point.split(",").index(i)) > for i in point.split(",")] > print dimensions > fixedPoints.append(Point(dimensions)) > #return an initial cluster of all the points > return Cluster(fixedPoints) > > def quantize(stringToQuantize, pointList, columnIndex): > #if it's numeric, no need to quantize > if(isNumeric(stringToQuantize)): > return float(stringToQuantize) > #first we need to discover all the possible values of this column > domain = [] > for point in pointList: > domain.append(point.split(",")[columnIndex]) > #make it a set to remove duplicates > domain = list(Set(domain)) > #use the index into the domain as the number representing this > value > return float(domain.index(stringToQuantize)) > > #harvested from http://www.rosettacode.org/wiki/IsNumeric#Python > def isNumeric(string): > try: > i = float(string) > except ValueError: > return False > return True Big problem with this. I'm guessing you never ran it on a really big file yet. Your quantize function does a lot of unnecessary work: it rebuilds the list of indices for every single line, every single non- numeric entry, in fact. (Tech-speak: that is N^2 behavior in both the number of lines and number of non-numeric columns. Not good.) This will work ok for a small file, but will take forever on a large file (perhaps literally). So, forgetting about column indexing for a moment, we can improve this vastly simply by generating the list of indices once. Do that in a separete function, have it return the list, and then pass that list to the quantize function. So replace the midportion of your createInitialCluster function with something like this: .... for i in xrange(len(points[0])): # number of columns column_indices.append(quantize_column(points,i)) fixedPoints = [] for point in points: dimensions = [quantize(s, column_indices[i], point.split (",").index(i)) for (i,s) in enumerate(point.split(","))] # need index as well as entry here print dimensions fixedPoints.append(Point(dimensions)) .... And the two functions would be something like this: def quantize_columns(point_list,column_index): # this assumes if the first column is numeric the whole column would be if(isNumeric(point_list[0][column_index])): return None # don't quantize this column #first we need to discover all the possible values of this column domain = [] for point in point_list: domain.append(point.split(",")[column_index]) #make it a set to remove duplicates return list(set(domain)) def quantize(i,domain,s): if domain is None: return float(s) return float(domain.index(s)) This (once debugged :) will run much, much faster on a large dataset. Now back to your question. > It works, but it feels a little ugly, and not exactly Pythonic. Using > two lists I need the original point list to read in the data, then the > dimensions one to hold the processed point, and a fixedPoint list to > make objects out of the processed data. If my dataset is in the order > of millions, this'll nuke the memory. I tried something like: > > for point in points: > point = Point([quantize(i, points, point.split(",").index(i)) for i > in point.split(",")]) > but when I print out the points afterward, it doesn't keep the > changes. It's because the order of items in a set is undefined. The order of the items in list(set(["a","b","c","d"])) might be very different from the order in list(set(["a","b","c","d","e"])). You were passing quantize incomplete an incomplete list of points, so as the points grew, the items in the set changed, and it messed up the order. In fact, you should never rely on the order being the same, even if you created the set with the very same arguments. What you are trying to do should be done with dictionaries: create a dict that maps a value to a number. Now, based on your quantize function, it would seem that the number associated with the value is arbitrary (doesn't matter what it is, as long as it's distinct), so it isn't necessary to read the whole csv file in before assigning numbers; just build the dict as you go. I suggest collections.defaultdict for this task. It's like a regular dict, but it creates a new value any time you access a key that doesn't exist, a perfect solution to your task. We'll pass it a function that generates a different index each time it's called. (This is probably too advanced for you, but oh well, it's such a cool trick.) import collections import itertools def createInitialCluster(fileName): fixedPoints = [] # quantization is a dict that assigns sequentially-increasing numbers # to values when reading keys that don't yet exit quantization = defaultdict.collections(itertools.count().next) with open(fileName, 'r') as f: for line in f: dimensions = [] for s in line.rstrip('\n').split(","): if isNumeric(s): dimensions.append(float(s)) else: dimensions.append(float(quantization[s])) fixedPoints.append(Point(dimensions)) return Cluster(fixedPoints) A couple general pointers: * Don't ever use i to represent a string. Programmers expect i to be an integer. i,j,k,l,m, and n should be integers, in fact. u,v,w,x,y, and z should be floats. You should stick to this convention whenever possible, but definitely never use i for anything but an integer. * set is built into Python now; unless you're using an older version (2.3 I think) you should use set instead of Set. * The Python style guide (q.g.) recommends that variables use names such as column_index rather than columnIndex. The world won't end if you don't follow it but if you want to be Pythonic that's how. Carl Banks From george.sakkis at gmail.com Tue Apr 7 01:43:54 2009 From: george.sakkis at gmail.com (George Sakkis) Date: Mon, 6 Apr 2009 22:43:54 -0700 (PDT) Subject: Returning different types based on input parameters References: <43aa610b30145a92914205dbb7e43078.squirrel@localhost> Message-ID: On Apr 6, 7:57?pm, "andrew cooke" wrote: > andrew cooke wrote: > > George Sakkis wrote: > >> That's more of a general API design question but I'd like to get an > >> idea if and how things are different in Python context. AFAIK it's > >> generally considered bad form (or worse) for functions/methods to > >> return values of different "type" depending on the number, type and/or > >> values of the passed parameters. I'm using "type" loosely in a duck- > >> typing sense, not necessarily as a concrete class and its descendants, > >> although I'm not sure if even duck-typing is endorsed for return > >> values (as opposed to input parameters). > > [...] > > > you probably want to look up substitutability: > >http://www.google.cl/search?q=substitutability+principle > > actually, this is better:http://www.google.cl/search?q=substitution+principle > > the idea being that if the "contract" for your function is that it returns > a certain type, then any subclass should also be ok (alternatively, that > subclasses should be written so that they can be returned when a caller > was expecting the superclass) I'm not sure if Liskov substitution addresses the same problem. The question here is, what's the scope of the contract ? Does it apply to the original signature only or any future extended version of it ? In the former case, the contract is still valid: whenever someone calls "solve(x)" gets the promised type. The original contract didn't specify what should the result be when the function is called as "solve (x, y)" (since the function didn't support a second argument originally). Only if one interprets the contract as applicable to the current plus all future extensions, then Liskov substitution comes into play. Perhaps that's more obvious in statically typed languages that allow overloading. IIRC the presence of a method with the signature float foo(float x); does not preclude its overloading (in the same or a descendant class) with a method char* foo(float x, int y); The two methods just happen to share the same name but other than that they are separate, their return value doesn't have to be substitutable. Is this considered bad practice ? George From fetchinson at googlemail.com Tue Apr 7 01:57:26 2009 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Mon, 6 Apr 2009 22:57:26 -0700 Subject: decorator module in stdlib? In-Reply-To: References: Message-ID: >> Similar functionality is already provided by >> functools.update_wrapper() and functools.wraps(). >> Seehttp://docs.python.org/library/functools.html >> You might consider proposing the modification of these functions instead. > > Unfortunately functools.update_wrapper() and functools.wraps() > are NOT equivalent to the decorator module. Exactly, that's why I think the decorator module would be a useful addition to functools. > My original > plan when I wrote the decorator module was to make aware > people that the corrent support for decorators in the language > is insufficient. I have always seen the decorator module > as a temporary hack waiting for a proper solution > at the language level. I wanted the possibility to modify the > signature of a function. Everybody more or less agreed > that this was a good idea on python-dev, and there is > also a PEP: > > http://www.python.org/dev/peps/pep-0362/ > > However, AFAIK, the PEP is still in draft status. > Perhaps people wanting this should make some noise > on python-dev. I've posted it to python-dev: http://mail.python.org/pipermail/python-dev/2009-April/088387.html Cheers, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From michele.simionato at gmail.com Tue Apr 7 02:12:58 2009 From: michele.simionato at gmail.com (Michele Simionato) Date: Mon, 6 Apr 2009 23:12:58 -0700 (PDT) Subject: decorator module in stdlib? References: Message-ID: <93a12bfb-9981-4998-8541-65c9b7fea9ee@z9g2000yqi.googlegroups.com> On Apr 7, 7:57?am, Daniel Fetchinson wrote: > > have always seen the decorator module > > as a temporary hack waiting for a proper solution > > at the language level. I wanted the possibility to modify the > > signature of a function. Everybody more or less agreed > > that this was a good idea on python-dev, and there is > > also a PEP: > > >http://www.python.org/dev/peps/pep-0362/ > > > However, AFAIK, the PEP is still in draft status. > > Perhaps people wanting this should make some noise > > on python-dev. > > I've posted it to python-dev: > > http://mail.python.org/pipermail/python-dev/2009-April/088387.html If the PEP is implemented and it becomes possible to fiddle with the function signature directly, then the decorator module would become trivial to implement and actually nearly useless. I would happy with that, less code to maintain ;) From aleksandar27 at BRISIOVOnet.hr Tue Apr 7 02:39:29 2009 From: aleksandar27 at BRISIOVOnet.hr (alejandro) Date: Tue, 7 Apr 2009 08:39:29 +0200 Subject: Anyone mannaged to access parallel port on windows xp? Message-ID: I have a switch that I should connect to the parallel port, but had no luck with it. Tha guy that made it for me told me that it would be easyer to connect via parallel instead the USB.... So did anyone have success? I only get suckess!! :-)) tryed giveio.sys but it doesn't wort (can't figure out what is it) From sumerc at gmail.com Tue Apr 7 02:46:13 2009 From: sumerc at gmail.com (k3xji) Date: Mon, 6 Apr 2009 23:46:13 -0700 (PDT) Subject: C API String Parsing/Returning References: <73a778c4-fe3f-424c-a407-67db8cb6a766@o11g2000yql.googlegroups.com> Message-ID: <0aa88c27-262c-46f4-9bcf-5b67c440bd82@u8g2000yqn.googlegroups.com> Whan I run the following function, I see a mem leak, a 20 mb of memory is allocated and is not freed. Here is the code I run: >>> import esauth >>> for i in range(1000000): ... ss = esauth.penc('sumer') ... >>> for i in range(1000000): ... ss = esauth.penc('sumer') ... And here is the penc() function. static PyObject * penc(PyObject *self, PyObject *args) { unsigned char *s= NULL; unsigned char *buf = NULL; PyObject * result = NULL; unsigned int v,len,i = 0; if (!PyArg_ParseTuple(args, "s#", &s, &len)) return NULL; buf = strdup(s); if (!buf) { PyErr_SetString(PyExc_MemoryError, "Out of memory: strdup failed"); return NULL; } /*string manipulation*/ result = PyString_FromString(buf); free(buf); return result; } Am I doing something wrong? Thanks, From sumerc at gmail.com Tue Apr 7 03:01:39 2009 From: sumerc at gmail.com (k3xji) Date: Tue, 7 Apr 2009 00:01:39 -0700 (PDT) Subject: Python C API String Memory Consumption Message-ID: <41d95468-8f7a-4647-83e1-6df147744467@u8g2000yqn.googlegroups.com> When I run the following function, I seem to have a mem leak, a 20 mb of memory is allocated and is not freed. Here is the code I run: >>> import esauth >>> for i in range(1000000): ... ss = esauth.penc('sumer') ... >>> for i in range(1000000): ... ss = esauth.penc('sumer') ... And here is the penc() function. static PyObject * penc(PyObject *self, PyObject *args) { unsigned char *s= NULL; unsigned char *buf = NULL; PyObject * result = NULL; unsigned int v,len,i = 0; if (!PyArg_ParseTuple(args, "s#", &s, &len)) return NULL; buf = strdup(s); if (!buf) { PyErr_SetString(PyExc_MemoryError, "Out of memory: strdup failed"); return NULL; } /*string manipulation*/ result = PyString_FromString(buf); free(buf); return result; } Am I doing something wrong? Thanks, From eric.brunel at nospam-pragmadev.com Tue Apr 7 03:30:26 2009 From: eric.brunel at nospam-pragmadev.com (Eric Brunel) Date: 07 Apr 2009 07:30:26 GMT Subject: How can I change size of GUI? References: <6dd057ac-9fb9-4979-861f-59f36e3561e7@s20g2000yqh.googlegroups.com> Message-ID: Muddy Coder wrote: > Hi Folks, > > I copied code from book: > > class ScrolledText(Frame): > def __init__(self, parent=None, text='', file=None): > Frame.__init__(self, parent) > self.pack(expand=YES, fill=BOTH) > self.makeWidgets() > self.settext(text, file) > def makeWidgets(self): > sbar = Scrollbar(self) > text = Text(self, relief=SUNKEN, width=120) > sbar.config(command=text.yview) > text.config(yscrollcommand=sbar.set) > sbar.pack(side=RIGHT, fill=Y) > text.pack(side=LEFT, expand=YES, fill=BOTH) > self.text = text > > It works, of course. But, the GUI is small, and I want to enlarge it. > I tried to add in options of width=120 for Text(), but it did not > work. Can somebody drop me a couple of lines for help? Thanks! For what definition of 'did not work'? Seems perfectly fine to me. Just try to add the lines: root = Tk() frm = ScrolledFrame(root) frm.pack() root.mainloop() to the end of your code above, remove the line self.settext(text, file) which calls a method that doesn't exist, then run it with your option width=120, then without, and I can guarantee you'll see a difference (I do anyway...). So I guess this code is part of a much bigger one, and that the problem lies in the other part... HTH anyway... - Eric - From __peter__ at web.de Tue Apr 7 03:38:02 2009 From: __peter__ at web.de (Peter Otten) Date: Tue, 07 Apr 2009 09:38:02 +0200 Subject: in place list modification necessary? What's a better idiom? References: <7790d731-109b-4373-986f-c28699e29a6b@b7g2000pre.googlegroups.com> Message-ID: MooMaster wrote: > Now we can't calculate a meaningful Euclidean distance for something > like "Iris-setosa" and "Iris-versicolor" unless we use string-edit > distance or something overly complicated, so instead we'll use a > simple quantization scheme of enumerating the set of values within the > column domain and replacing the strings with numbers (i.e. Iris-setosa > = 1, iris-versicolor=2). I'd calculate the distance as def string_dist(x, y, weight=1): return weight * (x == y) You don't get a high resolution in that dimension, but you don't introduce an element of randomness, either. Peter From __peter__ at web.de Tue Apr 7 03:40:37 2009 From: __peter__ at web.de (Peter Otten) Date: Tue, 07 Apr 2009 09:40:37 +0200 Subject: in place list modification necessary? What's a better idiom? References: <7790d731-109b-4373-986f-c28699e29a6b@b7g2000pre.googlegroups.com> Message-ID: Peter Otten wrote: > MooMaster wrote: > >> Now we can't calculate a meaningful Euclidean distance for something >> like "Iris-setosa" and "Iris-versicolor" unless we use string-edit >> distance or something overly complicated, so instead we'll use a >> simple quantization scheme of enumerating the set of values within the >> column domain and replacing the strings with numbers (i.e. Iris-setosa >> = 1, iris-versicolor=2). > > I'd calculate the distance as > > def string_dist(x, y, weight=1): > return weight * (x == y) oops, this must of course be (x != y). > You don't get a high resolution in that dimension, but you don't introduce > an element of randomness, either. > > Peter > From fetchinson at googlemail.com Tue Apr 7 03:44:55 2009 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Tue, 7 Apr 2009 00:44:55 -0700 Subject: Anyone mannaged to access parallel port on windows xp? In-Reply-To: References: Message-ID: > I have a switch that I should connect to the parallel port, but had no luck > with it. Tha guy that made it for me told me that it would be easyer to > connect via parallel instead the USB.... > So did anyone have success? I only get suckess!! :-)) > tryed giveio.sys but it doesn't wort (can't figure out what is it) Have you tried http://pyserial.wiki.sourceforge.net/pyParallel ? Cheers, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From pavlovevidence at gmail.com Tue Apr 7 04:10:56 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Tue, 7 Apr 2009 01:10:56 -0700 (PDT) Subject: in place list modification necessary? What's a better idiom? References: <7790d731-109b-4373-986f-c28699e29a6b@b7g2000pre.googlegroups.com> Message-ID: On Apr 7, 12:38?am, Peter Otten <__pete... at web.de> wrote: > MooMaster wrote: > > Now we can't calculate a meaningful Euclidean distance for something > > like "Iris-setosa" and "Iris-versicolor" unless we use string-edit > > distance or something overly complicated, so instead we'll use a > > simple quantization scheme of enumerating the set of values within the > > column domain and replacing the strings with numbers (i.e. Iris-setosa > > = 1, iris-versicolor=2). > > I'd calculate the distance as > > def string_dist(x, y, weight=1): > ? ? return weight * (x == y) > > You don't get a high resolution in that dimension, but you don't introduce > an element of randomness, either. Does the algorithm require well-ordered data along the dimensions? Though I've never heard of it, the fact that it's called "bisecting Kmeans" suggests to me that it does, which means this wouldn't work. However, the OP better be sure to set the scales for the quantized dimensions high enough so that no clusters form containing points with different discrete values. That, in turn, suggests he might as well not even bother sending the discrete values to the clustering algorithm, but instead to call it for each unique set of discretes. (However, I could imagine the marginal cost of more dimensions is less than that of multiple runs; I've been dealing with such a case at work.) I'll leave it to the OP to decide. Carl Banks From aleksandar27 at BRISIOVOnet.hr Tue Apr 7 04:22:04 2009 From: aleksandar27 at BRISIOVOnet.hr (alejandro) Date: Tue, 7 Apr 2009 10:22:04 +0200 Subject: Anyone mannaged to access parallel port on windows xp? References: Message-ID: I have a PC at work and at home. I wanted to write the errors it throws out when i try to use the module but now it work on this PC(the one at work). I don't get it?!?! I tryed so many things and now I can't remember what I have done :-))) This is worse than before! I will post the errors when i get home. Thanks for the reply! From aleksandar27 at BRISIOVOnet.hr Tue Apr 7 04:23:13 2009 From: aleksandar27 at BRISIOVOnet.hr (alejandro) Date: Tue, 7 Apr 2009 10:23:13 +0200 Subject: Anyone mannaged to access parallel port on windows xp? References: Message-ID: Yes that module is the one to blame for my headache! :-)) > Have you tried http://pyserial.wiki.sourceforge.net/pyParallel ? > > Cheers, > Daniel > > -- > Psss, psss, put it down! - http://www.cafepress.com/putitdown From pavlovevidence at gmail.com Tue Apr 7 04:40:27 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Tue, 7 Apr 2009 01:40:27 -0700 (PDT) Subject: Python C API String Memory Consumption References: <41d95468-8f7a-4647-83e1-6df147744467@u8g2000yqn.googlegroups.com> Message-ID: On Apr 7, 12:01?am, k3xji wrote: > When I run the following function, I seem to have a mem leak, a 20 mb > of memory > is allocated and is not freed. Here is the code I run: > > >>> import esauth > >>> for i in range(1000000): > > ... ? ? ss = esauth.penc('sumer') > ... > > >>> for i in range(1000000): > > ... ? ? ss = esauth.penc('sumer') > ... > > And here is the penc() function. > > static PyObject * > penc(PyObject *self, PyObject *args) > { > ? ? ? ? unsigned char *s= NULL; > ? ? ? ? unsigned char *buf = NULL; > ? ? ? ? PyObject * result = NULL; > ? ? ? ? unsigned int v,len,i = 0; > > ? ? ? ? if (!PyArg_ParseTuple(args, "s#", &s, &len)) > ? ? ? ? return NULL; > > ? ? ? ? buf = strdup(s); > ? ? ? ? if (!buf) { > ? ? ? ? ? ? ? ? PyErr_SetString(PyExc_MemoryError, > ? ? ? ? ? ? ? ? ? ? ? ? "Out of memory: strdup failed"); > ? ? ? ? ? ? ? ? return NULL; > ? ? ? ? } > > ? ? ? ? /*string manipulation*/ > > ? ? ? ? result = PyString_FromString(buf); > ? ? ? ? free(buf); > ? ? ? ? return result; > > } > > Am I doing something wrong? It might just be an unfortunate case where malloc keeps allocating memory higher and higher on the heap even though it frees all the memory. And since it doesn't give it back to the OS, it runs out. However, Python apparently does leak a reference if passed a Unicode object; PyArg_ParseTuple automatically creates an encoded string but never decrefs it. (That might be necessary evil to preserve compatibility, though. PyString_AS_STRING does it too.) Carl Banks From sumerc at gmail.com Tue Apr 7 04:43:41 2009 From: sumerc at gmail.com (k3xji) Date: Tue, 7 Apr 2009 01:43:41 -0700 (PDT) Subject: Python C API String Memory Consumption References: <41d95468-8f7a-4647-83e1-6df147744467@u8g2000yqn.googlegroups.com> Message-ID: <6d8e5740-0ee9-46b6-97df-9e73be81c24b@e18g2000yqo.googlegroups.com> Interestaing I changed malloc()/free() usage with PyMem_xx APIs and the problem resolved. However, I really cannot understand why the first version does not work. Here is the latest code that has no problems at all: static PyObject * penc(PyObject *self, PyObject *args) { PyObject * result = NULL; unsigned char *s= NULL; unsigned char *buf = NULL; unsigned int v,len,i = 0; if (!PyArg_ParseTuple(args, "s#", &s, &len)) return NULL; buf = (unsigned char *) PyMem_Malloc(len); if (buf == NULL) { PyErr_NoMemory(); return NULL; } /* string manipulation. */ result = PyString_FromStringAndSize((char *)buf, len); PyMem_Free(buf); return result; } From r-w at inet.net.au Tue Apr 7 05:00:30 2009 From: r-w at inet.net.au (r-w) Date: Tue, 07 Apr 2009 19:00:30 +1000 Subject: pseudo-code Message-ID: <49DB162E.2020309@inet.net.au> If no internet connection: if have files: run anyway, with warning else: ERROR else: if error getting hash/files: if have files: run anyway, with warning else: ERROR else: run From __peter__ at web.de Tue Apr 7 05:11:30 2009 From: __peter__ at web.de (Peter Otten) Date: Tue, 07 Apr 2009 11:11:30 +0200 Subject: in place list modification necessary? What's a better idiom? References: <7790d731-109b-4373-986f-c28699e29a6b@b7g2000pre.googlegroups.com> Message-ID: Carl Banks wrote: > On Apr 7, 12:38?am, Peter Otten <__pete... at web.de> wrote: >> MooMaster wrote: >> > Now we can't calculate a meaningful Euclidean distance for something >> > like "Iris-setosa" and "Iris-versicolor" unless we use string-edit >> > distance or something overly complicated, so instead we'll use a >> > simple quantization scheme of enumerating the set of values within the >> > column domain and replacing the strings with numbers (i.e. Iris-setosa >> > = 1, iris-versicolor=2). >> >> I'd calculate the distance as >> >> def string_dist(x, y, weight=1): >> return weight * (x == y) >> >> You don't get a high resolution in that dimension, but you don't >> introduce an element of randomness, either. > > Does the algorithm require well-ordered data along the dimensions? > Though I've never heard of it, the fact that it's called "bisecting > Kmeans" suggests to me that it does, which means this wouldn't work. I've read about K-Means in Segaran's "Collective Intelligence" which describes it: "K-Means clustering begins with k randomly placed centroids (points in space that represent the center of the cluster), and assigns every item to the nearest one. After the assignment, the centroids are moved to the average location of all the nodes assigned to them, and the assignments are redone. This process repeats until the assignments stop changing." The book doesn't go into the theory, and "any distance would do" was my assumption which may be wrong. Peter From clp2 at rebertia.com Tue Apr 7 05:23:15 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Tue, 7 Apr 2009 02:23:15 -0700 Subject: pseudo-code In-Reply-To: <49DB162E.2020309@inet.net.au> References: <49DB162E.2020309@inet.net.au> Message-ID: <50697b2c0904070223n65c19aefha68645b06e609795@mail.gmail.com> On Tue, Apr 7, 2009 at 2:00 AM, r-w wrote: > If no internet connection: > ? ?if have files: > ? ? ? ?run anyway, with warning > ? ?else: > ? ? ? ?ERROR > else: > ? ?if error getting hash/files: > ? ? ? ?if have files: > ? ? ? ? ? ?run anyway, with warning > ? ? ? ?else: > ? ? ? ? ? ?ERROR > ? ?else: > ? ? ? ?run Please refrain from re-posting the /exact same thing/ without waiting for a reasonable amount of time to pass, and then only when you don't receive any useful replies, neither of which is the case. It's basic netiquette. See my reply to your original post. Cheers, Chris -- I have a blog: http://blog.rebertia.com From ganeshborse at gmail.com Tue Apr 7 05:30:00 2009 From: ganeshborse at gmail.com (grbgooglefan) Date: Tue, 7 Apr 2009 02:30:00 -0700 (PDT) Subject: building release - assert-free python library Message-ID: <121dcfb3-1ffe-40bf-b6dc-d2bacc073c3f@k19g2000prh.googlegroups.com> How can I build a release and not the debug version of libpython.a? I have seen that there are assert, abort statements in lot many functions in Python code. I would like to avoid those when compiling the libpython.a library because when this libpython gets used for production purpose and it aborts, the application goes down without any means of graceful handling that error condition. How can we handle this? Please help From notvalid2 at sbcglobal.net Tue Apr 7 05:58:27 2009 From: notvalid2 at sbcglobal.net (W. eWatson) Date: Tue, 07 Apr 2009 02:58:27 -0700 Subject: PIL\Tkinter and Transparencies, Rubber Lines, and Dragging Image Objects Message-ID: <_sFCl.3740$im1.3071@nlpi061.nbdc.sbc.com> Basically, I'd like to know how one (broadly, e.g., references in Win-land) does IP (image processing) and drawing techniques such as rubber lines, and dragging image objects across the canvas. I know there are some pretty powerful toolkits out there, but I'd like to limit this to PIL and Tkinter. If it can't be done with them, then I'll consider other possibilities. As a starter, on the topic of transparencies, consider this program that I pulled off the web and was posted in 1999. It purports to illustrate how one might produce a transparency. #!/usr/bin/python # see http://mail.python.org/pipermail/python-list/1999-May/003388.html from Tkinter import * import Image, ImageTk import tkFileDialog class Transparency: def __init__(self, parent): self.canvas = Canvas(parent, bg='green') self.canvas.pack() b = Button(parent, command=self.open, text="Select graphics file") b.pack() def open(self): self.canvas.delete(ALL) filename = tkFileDialog.askopenfilename() if filename != '': im = Image.open(filename) if im.mode != "RGBA": im = Image.open(filename).convert("RGBA") source = im.split() R, G, B, A = 0, 1, 2, 3 mask = im.point(lambda i: i > 0 and 255) # use black as transparent source[A].paste(mask) im = Image.merge(im.mode, source) # build a new multiband image self.graphic = ImageTk.PhotoImage(image=im) self.canvas.create_image(100, 100, image=self.graphic) if __name__ == "__main__": root = Tk() test = Transparency(root) root.mainloop() It colors the canvas green, and produces a black background. An image is merged with the background. I tried out the program. It executes, but I do not see where the transparency is apparent. I used a gif with a picture of a telescope on a white background, and the result is what I would see if I pasted the telescope and white background onto the green canvas. If there's something missing in my observation, I'd like to know what it is. To further explore "drawing graphics", what roughly is the capability of Tkinter or PIL to allow one to place a transparent layer (mode, I guess in PIL may be roughly equivalent to a layer in tools like Photoshop) on top of an image and then move the transparency around over the image with a mouse? -- 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 Tue Apr 7 06:22:53 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 07 Apr 2009 10:22:53 GMT Subject: pseudo-code References: <49DB162E.2020309@inet.net.au> Message-ID: <01eb1da7$0$20654$c3e8da3@news.astraweb.com> On Tue, 07 Apr 2009 19:00:30 +1000, r-w wrote: > If no internet connection: > if have files: > run anyway, with warning > else: > ERROR > else: > if error getting hash/files: > if have files: > run anyway, with warning > else: > ERROR > else: > run Very pretty. Did you have a question, or did you just want to show off your L33T pseudo-code writing 5K1LL5? -- Steven From ben+python at benfinney.id.au Tue Apr 7 06:30:53 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 07 Apr 2009 20:30:53 +1000 Subject: building release - assert-free python library References: <121dcfb3-1ffe-40bf-b6dc-d2bacc073c3f@k19g2000prh.googlegroups.com> Message-ID: <87tz507o2q.fsf@benfinney.id.au> grbgooglefan writes: > I would like to avoid [exceptions] when compiling the libpython.a > library because when this libpython gets used for production purpose > and it aborts, the application goes down without any means of > graceful handling that error condition. How can we handle this? Exceptions are a fundamental control flow mechanism in Python, and Python programs need them enabled in order to function properly. What you actually want is (as you say) to properly handle any un-caught exceptions. This is done by writing an exception handler function and then binding the name ?sys.excepthook? to your function as the default exception handler. -- \ ?Somebody told me how frightening it was how much topsoil we | `\ are losing each year, but I told that story around the campfire | _o__) and nobody got scared.? ?Jack Handey | Ben Finney From andrew at acooke.org Tue Apr 7 06:37:01 2009 From: andrew at acooke.org (andrew cooke) Date: Tue, 7 Apr 2009 06:37:01 -0400 (CLT) Subject: in place list modification necessary? What's a better idiom? In-Reply-To: <688a5e3d-38f5-45d9-a115-ca43435fa8e9@i28g2000prd.googlegroups.com> References: <7790d731-109b-4373-986f-c28699e29a6b@b7g2000pre.googlegroups.com> <688a5e3d-38f5-45d9-a115-ca43435fa8e9@i28g2000prd.googlegroups.com> Message-ID: <495e3949bce027f09eee99beaacb22fc.squirrel@localhost> Carl Banks wrote: > import collections > import itertools > > def createInitialCluster(fileName): > fixedPoints = [] > # quantization is a dict that assigns sequentially-increasing > numbers > # to values when reading keys that don't yet exit > quantization = defaultdict.collections(itertools.count().next) > with open(fileName, 'r') as f: > for line in f: > dimensions = [] > for s in line.rstrip('\n').split(","): > if isNumeric(s): > dimensions.append(float(s)) > else: > dimensions.append(float(quantization[s])) > fixedPoints.append(Point(dimensions)) > return Cluster(fixedPoints) nice reply (i didn't know defaultdict worked like that - very neat). two small things i noticed: 1 - do you need a separate quantization for each column? the code above might give, for example, non-contiguous ranges of integers for a particular column if a string occurs ("by accident" perhaps) in more than one. 2 - don't bother with isNumeric. just return the cast value or catch the exception: [...] try: dimensions.append(float(s)) except: dimensions.append(float(quantization[s])) (not sure float() is needed there either if you're using a recent version of python - only reason i can think of is to avoid integer division in older versions). andrew From eric.brunel at nospam-pragmadev.com Tue Apr 7 06:58:18 2009 From: eric.brunel at nospam-pragmadev.com (Eric Brunel) Date: 07 Apr 2009 10:58:18 GMT Subject: PIL\Tkinter and Transparencies, Rubber Lines, and Dragging Image Objects References: <_sFCl.3740$im1.3071@nlpi061.nbdc.sbc.com> Message-ID: W. eWatson wrote: > Basically, I'd like to know how one (broadly, e.g., references in Win-land) > does IP (image processing) and drawing techniques such as rubber lines, and > dragging image objects across the canvas. I know there are some pretty > powerful toolkits out there, but I'd like to limit this to PIL and Tkinter. > If it can't be done with them, then I'll consider other possibilities. As a > starter, on the topic of transparencies, consider this program that I pulled > off the web and was posted in 1999. It purports to illustrate how one might > produce a transparency. OK, maybe I'm dumb but: > #!/usr/bin/python > # see http://mail.python.org/pipermail/python-list/1999-May/003388.html > from Tkinter import * > import Image, ImageTk > import tkFileDialog > > class Transparency: > def __init__(self, parent): > self.canvas = Canvas(parent, bg='green') > self.canvas.pack() > b = Button(parent, command=self.open, text="Select graphics file") > b.pack() > > def open(self): > self.canvas.delete(ALL) > filename = tkFileDialog.askopenfilename() > if filename != '': > im = Image.open(filename) > if im.mode != "RGBA": > im = Image.open(filename).convert("RGBA") > source = im.split() > R, G, B, A = 0, 1, 2, 3 > mask = im.point(lambda i: i > 0 and 255) # use black as transparent ^^^^^^^^^^^^^^^^^^^^^^^^ > source[A].paste(mask) > im = Image.merge(im.mode, source) # build a new multiband image > > self.graphic = ImageTk.PhotoImage(image=im) > self.canvas.create_image(100, 100, image=self.graphic) > if __name__ == "__main__": > root = Tk() > test = Transparency(root) > root.mainloop() > > It colors the canvas green, and produces a black background. An image is > merged with the background. I tried out the program. It executes, but I > do not see where the transparency is apparent. I used a gif with a > picture of a telescope on a white background, and the result is what I ^^^^^^^^^^^^^^^^ > would see if I pasted the telescope and white background onto the green > canvas. I have neither PIL, nor the image you're using so I can just guess. But if you want to use a white background, maybe you should use a mask defined with: mask = im.point(lambda i: 255 if i >= 255 else 0) (if I understood the mask construction correctly...). HTH - Eric - From eckhardt at satorlaser.com Tue Apr 7 06:58:31 2009 From: eckhardt at satorlaser.com (Ulrich Eckhardt) Date: Tue, 07 Apr 2009 12:58:31 +0200 Subject: building release - assert-free python library References: <121dcfb3-1ffe-40bf-b6dc-d2bacc073c3f@k19g2000prh.googlegroups.com> Message-ID: grbgooglefan wrote: > How can I build a release and not the debug version of libpython.a? > I have seen that there are assert, abort statements in lot many > functions in Python code. I would like to avoid those when compiling > the libpython.a library because when this libpython gets used for > production purpose and it aborts, the application goes down without > any means of graceful handling that error condition. You are misunderstanding assertions. If an assertion fires, you always have a programming error that can not be recovered from. So, your program will go down anyway, the difference is a defined way from an assertion compared to an undefined one that can mean random corruption sometime sooner or later. That said, the only reason to go without assertions is to improve performance. Uli -- Sator Laser GmbH Gesch?ftsf?hrer: Thorsten F?cking, Amtsgericht Hamburg HR B62 932 From tarundevnani at gmail.com Tue Apr 7 07:04:50 2009 From: tarundevnani at gmail.com (tarun) Date: Tue, 7 Apr 2009 16:34:50 +0530 Subject: Issue with subprocess Module Message-ID: Hello All, I've a batch file to be invoke using a python script. The batch file has pause, and the time, I need to send some command to the batch file from my scripts. I placed both, the batch file (test.bat) and the python script (test.py) in the same folder. And executed 'test.py' (Please find the source files and error below). *I get the following error:* Traceback (most recent call last): File "", line 74, in run_nodebug File "D:\test.py", line 4, in proc = subprocess.Popen(my_bat,stdin=subprocess.PIPE) File "C:\Python25\lib\subprocess.py", line 588, in __init__ errread, errwrite) = self._get_handles(stdin, stdout, stderr) File "C:\Python25\lib\subprocess.py", line 717, in _get_handles c2pwrite = self._make_inheritable(c2pwrite) File "C:\Python25\lib\subprocess.py", line 746, in _make_inheritable DUPLICATE_SAME_ACCESS) WindowsError: [Error 6] The handle is invalid *Python Script:* *test.py* import subprocess,os my_bat = os.getcwd()+'\\test.bat' proc = subprocess.Popen(my_bat,stdin=subprocess.PIPE) input = '\n' proc.communicate(input) *Batch File* *test.bat* echo "START' pause echo 'END' Please help me with this issue. Thanks In Advance, Tarun -------------- next part -------------- An HTML attachment was scrubbed... URL: From google at mrabarnett.plus.com Tue Apr 7 07:12:20 2009 From: google at mrabarnett.plus.com (MRAB) Date: Tue, 07 Apr 2009 12:12:20 +0100 Subject: in place list modification necessary? What's a better idiom? In-Reply-To: <688a5e3d-38f5-45d9-a115-ca43435fa8e9@i28g2000prd.googlegroups.com> References: <7790d731-109b-4373-986f-c28699e29a6b@b7g2000pre.googlegroups.com> <688a5e3d-38f5-45d9-a115-ca43435fa8e9@i28g2000prd.googlegroups.com> Message-ID: <49DB3514.7010703@mrabarnett.plus.com> Carl Banks wrote: > MooMaster wrote: >> So I'm reading in values from a file, and for each column I need to >> dynamically discover the range of possible values it can take and >> quantize if necessary. This is the solution I've come up with: >> [snip] >> #harvested from http://www.rosettacode.org/wiki/IsNumeric#Python >> def isNumeric(string): >> try: >> i = float(string) >> except ValueError: >> return False >> return True > [snip] > > import collections > import itertools > > def createInitialCluster(fileName): > fixedPoints = [] > # quantization is a dict that assigns sequentially-increasing > numbers > # to values when reading keys that don't yet exit > quantization = defaultdict.collections(itertools.count().next) > with open(fileName, 'r') as f: > for line in f: > dimensions = [] > for s in line.rstrip('\n').split(","): > if isNumeric(s): > dimensions.append(float(s)) > else: > dimensions.append(float(quantization[s])) > fixedPoints.append(Point(dimensions)) > return Cluster(fixedPoints) > I'd also remove the isNumeric() function. You're just converting to a float if you can successfully convert to a float! try: dimensions.append(float(s)) except ValueError: dimensions.append(float(quantization[s])) From google at mrabarnett.plus.com Tue Apr 7 07:19:29 2009 From: google at mrabarnett.plus.com (MRAB) Date: Tue, 07 Apr 2009 12:19:29 +0100 Subject: Python C API String Memory Consumption In-Reply-To: <6d8e5740-0ee9-46b6-97df-9e73be81c24b@e18g2000yqo.googlegroups.com> References: <41d95468-8f7a-4647-83e1-6df147744467@u8g2000yqn.googlegroups.com> <6d8e5740-0ee9-46b6-97df-9e73be81c24b@e18g2000yqo.googlegroups.com> Message-ID: <49DB36C1.3040700@mrabarnett.plus.com> k3xji wrote: > Interestaing I changed malloc()/free() usage with PyMem_xx APIs and > the problem resolved. However, I really cannot understand why the > first version does not work. Here is the latest code that has no > problems at all: > > static PyObject * > penc(PyObject *self, PyObject *args) > { > PyObject * result = NULL; > unsigned char *s= NULL; > unsigned char *buf = NULL; > unsigned int v,len,i = 0; > > if (!PyArg_ParseTuple(args, "s#", &s, &len)) > return NULL; > > buf = (unsigned char *) PyMem_Malloc(len); > if (buf == NULL) { > PyErr_NoMemory(); > return NULL; > } > > /* string manipulation. */ > > result = PyString_FromStringAndSize((char *)buf, len); > PyMem_Free(buf); > return result; > } > In general I'd say don't mix your memory allocators. I don't know whether CPython implements PyMem_Malloc using malloc, but it's better to stick with CPython's memory allocators when writing for CPython. From piet at cs.uu.nl Tue Apr 7 07:29:40 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 07 Apr 2009 13:29:40 +0200 Subject: How to go about. On read/write locks References: <888ed340-48bd-414a-b827-8de726dc729c@h28g2000yqd.googlegroups.com> <73va73F10v3a8U3@mid.uni-berlin.de> Message-ID: >>>>> "Diez B. Roggisch" (DBR) wrote: >>> This is a classical synchronization problem with a classical solution: >>> You treat the readers as a group, and the writers individually. So you >>> have a write lock that each writer has to acquire and release, but it is >>> acquired only by the first reader and released by the last one. >>> Therefore you need a counter of the number of readers, and manipulations >>> of this counter must be protected by another lock. >>> >DBR> I was going to suggest a similar approach but refused to because of a >DBR> problem I see with your code as well - if the readers are reading very fast >DBR> (the OP didn't state what his actual application is, so it might not be a >DBR> consumer-producer scheme which I don't think a dict would be the natural >DBR> choice anyway) they can block the writer from writing alltogether. >DBR> Or do I miss something here? Yes, you missed note 2 at the end of my posting. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From jcd at sdf.lonestar.org Tue Apr 7 07:46:18 2009 From: jcd at sdf.lonestar.org (J. Clifford Dyer) Date: Tue, 07 Apr 2009 07:46:18 -0400 Subject: extract Infobox contents In-Reply-To: References: Message-ID: <1239104778.10734.2.camel@mctell> On Mon, 2009-04-06 at 23:41 +0100, Rhodri James wrote: > On Mon, 06 Apr 2009 23:12:14 +0100, Anish Chapagain > wrote: > > > Hi, > > I was trying to extract wikipedia Infobox contents which is in format > > like given below, from the opened URL page in Python. > > > > {{ Infobox Software > > | name = Bash > > | logo = [[Image:bash-org.png|165px]] > > | screenshot = [[Image:Bash demo.png|250px]] > > | caption = Screenshot of bash and [[Bourne shell|sh]] > > sessions demonstrating some features > > | developer = [[Chet Ramey]] > > | latest release version = 4.0 > > | latest release date = {{release date|mf=yes|2009|02|20}} > > | programming language = [[C (programming language)|C]] > > | operating system = [[Cross-platform]] > > | platform = [[GNU]] > > | language = English, multilingual ([[gettext]]) > > | status = Active > > | genre = [[Unix shell]] > > | source model = [[Free software]] > > | license = [[GNU General Public License]] > > | website = [http://tiswww.case.edu/php/chet/bash/ > > bashtop.html Home page] > > }} //upto this line > > > > I need to extract all data between {{ Infobox ...to }} > > > > Thank's if anyone can help, > > am trying with > > > > s1='{{ Infobox' > > s2=len(s1) > > pos1=data.find("{{ Infobox") > > pos2=data.find("\n",pos2) > > > > pat1=data.find("}}") > > > > but am ending up getting one line at top only. > > How are you getting your data? Assuming that you can arrange to get > it one line at a time, here's a quick and dirty way to extract the > infoboxes on a page. > > infoboxes = [] > infobox = [] > reading_infobox = False > > for line in feed_me_lines_somehow(): > if line.startswith("{{ Infobox"): > reading_infobox = True > if reading_infobox: > infobox.append(line) > if line.startswith("}}"): > reading_infobox = False > infoboxes.append(infobox) > infobox = [] > > You end up with 'infoboxes' containing a list of all the infoboxes > on the page, each held as a list of the lines of their content. > For safety's sake you really should be using regular expressions > rather than 'startswith', but I leave that as an exercise for the > reader :-) > I agree that startswith isn't the right option, but for matching two constant characters, I don't think re is necessary. I'd just do: if '}}' in line: pass Then, as the saying goes, you only have one problem. Cheers, Cliff From rdmurray at bitdance.com Tue Apr 7 07:47:26 2009 From: rdmurray at bitdance.com (R. David Murray) Date: Tue, 7 Apr 2009 11:47:26 +0000 (UTC) Subject: in place list modification necessary? What's a better idiom? References: <7790d731-109b-4373-986f-c28699e29a6b@b7g2000pre.googlegroups.com> <688a5e3d-38f5-45d9-a115-ca43435fa8e9@i28g2000prd.googlegroups.com> <495e3949bce027f09eee99beaacb22fc.squirrel@localhost> Message-ID: "andrew cooke" wrote: > Carl Banks wrote: > > import collections > > import itertools > > > > def createInitialCluster(fileName): > > fixedPoints = [] > > # quantization is a dict that assigns sequentially-increasing > > numbers > > # to values when reading keys that don't yet exit > > quantization = defaultdict.collections(itertools.count().next) > > with open(fileName, 'r') as f: > > for line in f: > > dimensions = [] > > for s in line.rstrip('\n').split(","): > > if isNumeric(s): > > dimensions.append(float(s)) > > else: > > dimensions.append(float(quantization[s])) > > fixedPoints.append(Point(dimensions)) > > return Cluster(fixedPoints) > > nice reply (i didn't know defaultdict worked like that - very neat). > > two small things i noticed: > > 1 - do you need a separate quantization for each column? the code above > might give, for example, non-contiguous ranges of integers for a > particular column if a string occurs ("by accident" perhaps) in more than > one. > > 2 - don't bother with isNumeric. just return the cast value or catch the > exception: > > [...] > try: > dimensions.append(float(s)) > except: > dimensions.append(float(quantization[s])) No, no, no; never use a bare except! :) Do it MRAB's way and catch ValueError. -- R. David Murray http://www.bitdance.com From jcd at sdf.lonestar.org Tue Apr 7 07:53:33 2009 From: jcd at sdf.lonestar.org (J. Clifford Dyer) Date: Tue, 07 Apr 2009 07:53:33 -0400 Subject: Web validation In-Reply-To: <49DA85C1.4090506@inet.net.au> References: <49DA85C1.4090506@inet.net.au> Message-ID: <1239105213.10734.7.camel@mctell> On Tue, 2009-04-07 at 08:44 +1000, r-w wrote: > If no internet connection: > if have files: > run anyway, with warning > else: > ERROR > else: > if error getting hash/files: > if have files: > run anyway, with warning > else: > ERROR > else: > run > -- > http://mail.python.org/mailman/listinfo/python-list > import logging def run_with_files(files): if files: logging.warn("warning") run() else: logging.error("error") raise IOError if internet_connection() and got_hash(): run() else: run_with_files(files) From davea at ieee.org Tue Apr 7 07:53:40 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 07 Apr 2009 07:53:40 -0400 Subject: Issue with subprocess Module In-Reply-To: References: Message-ID: <49DB3EC4.4040004@ieee.org> tarun wrote: > Hello All, > > I've a batch file to be invoke using a python script. The batch file has > pause, and the time, I need to send some command to the batch file from my > scripts. I placed both, the batch file (test.bat) and the python script > (test.py) in the same folder. And executed 'test.py' > > (Please find the source files and error below). > > *I get the following error:* > Traceback (most recent call last): > File "", line 74, in run_nodebug > File "D:\test.py", line 4, in > proc = subprocess.Popen(my_bat,stdin=subprocess.PIPE) > File "C:\Python25\lib\subprocess.py", line 588, in __init__ > errread, errwrite) = self._get_handles(stdin, stdout, stderr) > File "C:\Python25\lib\subprocess.py", line 717, in _get_handles > c2pwrite = self._make_inheritable(c2pwrite) > File "C:\Python25\lib\subprocess.py", line 746, in _make_inheritable > DUPLICATE_SAME_ACCESS) > WindowsError: [Error 6] The handle is invalid > > *Python Script:* > *test.py* > import subprocess,os > my_bat = os.getcwd()+'\\test.bat' > proc = subprocess.Popen(my_bat,stdin=subprocess.PIPE) > input = '\n' > proc.communicate(input) > > *Batch File* > *test.bat* > echo "START' > pause > echo 'END' > Please help me with this issue. > > Thanks In Advance, > Tarun > > subprocess.Popen() is expecting the name of a program, which should normally have an extension of .exe You're handing it a .bat file, which is not executable. It only executes in the context of a command interpreter (shell), such as cmd.exe You can probably do what you want by running "cmd.exe" and passing it "test.bat" as a parameter From rdmurray at bitdance.com Tue Apr 7 07:59:00 2009 From: rdmurray at bitdance.com (R. David Murray) Date: Tue, 7 Apr 2009 11:59:00 +0000 (UTC) Subject: Some test fail on my new Python 2.6 References: <344374.9276.qm@web56005.mail.re3.yahoo.com> Message-ID: Sorin Schwimmer wrote: > > > Run 'make test' instead of running them one by one. > > I did it *before* going one by one. I then tried individual tests in hope of getting hints of what to look for. Actualy, for the first three tests, the "make test" script commented that it is unusual for them to fail on my platform (linux2). Oh, yeah, I forgot that bit. The _tkinter error means you don't have TCL/TK installed. Not a problem unless you want to use tkinter. Unfortunately I forget what your other errors were. Can you just post the Skip messages (and any real errors) from the make test run? I can run through them quickly for you then, and then we can focus on the ones that are the real errors. -- R. David Murray http://www.bitdance.com From andrew at acooke.org Tue Apr 7 08:01:51 2009 From: andrew at acooke.org (andrew cooke) Date: Tue, 7 Apr 2009 08:01:51 -0400 (CLT) Subject: in place list modification necessary? What's a better idiom? In-Reply-To: <495e3949bce027f09eee99beaacb22fc.squirrel@localhost> References: <7790d731-109b-4373-986f-c28699e29a6b@b7g2000pre.googlegroups.com> <688a5e3d-38f5-45d9-a115-ca43435fa8e9@i28g2000prd.googlegroups.com> <495e3949bce027f09eee99beaacb22fc.squirrel@localhost> Message-ID: <1b055a9b896c788cedb0e40b339b1b70.squirrel@localhost> just playing around - doesn't work with 3.0 due to lack of pattern binding (which i think is coming back in 3.1?) >>> from collections import defaultdict >>> from itertools import count >>> def mkdict(): return defaultdict(count().next) ... >>> converters = defaultdict(mkdict) >>> def to_float((i, s)): ... try: return float(s) ... except: return converters[i][s] ... >>> def convert(line): ... return map(to_float, zip(count(), line.split(','))) ... >>> convert('1,2,red') [1.0, 2.0, 0] >>> convert('1,2,red') [1.0, 2.0, 0] >>> convert('1,2,blue') [1.0, 2.0, 1] >>> convert('1,2,blue,blue') [1.0, 2.0, 1, 0] andrew cooke wrote: > Carl Banks wrote: >> import collections >> import itertools >> >> def createInitialCluster(fileName): >> fixedPoints = [] >> # quantization is a dict that assigns sequentially-increasing >> numbers >> # to values when reading keys that don't yet exit >> quantization = defaultdict.collections(itertools.count().next) >> with open(fileName, 'r') as f: >> for line in f: >> dimensions = [] >> for s in line.rstrip('\n').split(","): >> if isNumeric(s): >> dimensions.append(float(s)) >> else: >> dimensions.append(float(quantization[s])) >> fixedPoints.append(Point(dimensions)) >> return Cluster(fixedPoints) > > nice reply (i didn't know defaultdict worked like that - very neat). > > two small things i noticed: > > 1 - do you need a separate quantization for each column? the code above > might give, for example, non-contiguous ranges of integers for a > particular column if a string occurs ("by accident" perhaps) in more than > one. > > 2 - don't bother with isNumeric. just return the cast value or catch the > exception: > > [...] > try: > dimensions.append(float(s)) > except: > dimensions.append(float(quantization[s])) > > (not sure float() is needed there either if you're using a recent version > of python - only reason i can think of is to avoid integer division in > older versions). > > andrew > > > -- > http://mail.python.org/mailman/listinfo/python-list > > From mal at egenix.com Tue Apr 7 08:02:54 2009 From: mal at egenix.com (M.-A. Lemburg) Date: Tue, 07 Apr 2009 14:02:54 +0200 Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: <20090403004135.B76443A40A7@sparrow.telecommunity.com> References: <49D4DA72.60401@v.loewis.de> <49D52115.6020001@egenix.com> <20090403004135.B76443A40A7@sparrow.telecommunity.com> Message-ID: <49DB40EE.60004@egenix.com> On 2009-04-03 02:44, P.J. Eby wrote: > At 10:33 PM 4/2/2009 +0200, M.-A. Lemburg wrote: >> Alternative Approach: >> --------------------- >> >> Wouldn't it be better to stick with a simpler approach and look for >> "__pkg__.py" files to detect namespace packages using that O(1) check ? > >> One of the namespace packages, the defining namespace package, will have >> to include a __init__.py file. > > Note that there is no such thing as a "defining namespace package" -- > namespace package contents are symmetrical peers. That was a definition :-) Definition namespace package := the namespace package having the __pkg__.py file This is useful to have since packages allowing integration of other sub-packages typically come as a base package with some basic infra-structure in place which is required by all other namespace packages. If the __init__.py file is not found among the namespace directories, the importer will have to raise an exception, since the result would not be a proper Python package. >> * It's possible to have a defining package dir and add-one package >> dirs. > > Also possible in the PEP, although the __init__.py must be in the first > such directory on sys.path. (However, such "defining" packages are not > that common now, due to tool limitations.) That's a strange limitation of the PEP. Why should the location of the __init__.py file depend on the order of sys.path ? -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Apr 03 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/ ________________________________________________________________________ 2009-03-19: Released mxODBC.Connect 1.0.1 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 manu3d at gmail.com Tue Apr 7 08:04:05 2009 From: manu3d at gmail.com (Emanuele D'Arrigo) Date: Tue, 7 Apr 2009 05:04:05 -0700 (PDT) Subject: On Integration Testing Message-ID: <1766c243-8b0a-4557-b0ef-7cb9d974cd5c@f19g2000vbf.googlegroups.com> Hi everybody, I just finished unit-testing a couple of tightly related modules and it's now time to test them together to a) check if they do talk to each other in the ways they should, b) check how fast they can talk to each other. The problem? Well, the problem is that potentially they can have some pretty varied conversations!! There are a lot of different messages that they can exchange, from simple question-answer messages to more complex [initial request, confirmation requested, confirmation sent, final response, public announce] sequences of messages. All this in a one-to-many context, that is one module is effectively a server and can be connected to zero to N clients. Right now I do not have a particularly cohesive, consistent approach to this type of testing. In short, I've never done it before. I'll start with some simple tests, i.e. adding and removing clients and sending some simple sequences of messages, maybe pushing up the number of clients to see how much gets done in, say, a second. But it feels fairly "unscientific". =) I've already googled "integration testing" and I'm in the process of reading a variety of articles. Is there any one that the people of the python community would like to recommend? Manu From marduk at letterboxes.org Tue Apr 7 08:07:01 2009 From: marduk at letterboxes.org (Albert Hopkins) Date: Tue, 07 Apr 2009 08:07:01 -0400 Subject: Issue with subprocess Module In-Reply-To: <49DB3EC4.4040004@ieee.org> References: <49DB3EC4.4040004@ieee.org> Message-ID: <1239106021.26233.9.camel@blackwidow.nbk> On Tue, 2009-04-07 at 07:53 -0400, Dave Angel wrote: > > > subprocess.Popen() is expecting the name of a program, which should > normally have an extension of .exe You're handing it a .bat file, > which is not executable. It only executes in the context of a command > interpreter (shell), such as cmd.exe > > You can probably do what you want by running "cmd.exe" and passing it > "test.bat" as a parameter > It seems to me that most people who have trouble with the subprocess module are Windows users. And indeed it does have a more *nix-like interface. But I'm wondering if there is another module that is more Windows-friendly (thinking out loud)? From ronn.ross at gmail.com Tue Apr 7 08:08:16 2009 From: ronn.ross at gmail.com (Ronn Ross) Date: Tue, 7 Apr 2009 08:08:16 -0400 Subject: Scraping a web page Message-ID: <9c8c445f0904070508p52e6011vaee4c5ce2b66d406@mail.gmail.com> I'm using the following to scrape a web page: import urllib f = urllib.urlopen("http://www.google.com") s = f.read() It is working, but it's returning the source of the page. Is there anyway I can get almost a screen capture of the page? Dankon -------------- next part -------------- An HTML attachment was scrubbed... URL: From mail at timgolden.me.uk Tue Apr 7 08:21:43 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Tue, 07 Apr 2009 13:21:43 +0100 Subject: Issue with subprocess Module In-Reply-To: <49DB3EC4.4040004@ieee.org> References: <49DB3EC4.4040004@ieee.org> Message-ID: <49DB4557.90903@timgolden.me.uk> Dave Angel wrote: > tarun wrote: >> Hello All, >> >> I've a batch file to be invoke using a python script. The batch file has >> pause, and the time, I need to send some command to the batch file >> from my >> scripts. I placed both, the batch file (test.bat) and the python script >> (test.py) in the same folder. And executed 'test.py' >> >> (Please find the source files and error below). >> >> *I get the following error:* >> Traceback (most recent call last): >> File "", line 74, in run_nodebug >> File "D:\test.py", line 4, in >> proc = subprocess.Popen(my_bat,stdin=subprocess.PIPE) >> File "C:\Python25\lib\subprocess.py", line 588, in __init__ >> errread, errwrite) = self._get_handles(stdin, stdout, stderr) >> File "C:\Python25\lib\subprocess.py", line 717, in _get_handles >> c2pwrite = self._make_inheritable(c2pwrite) >> File "C:\Python25\lib\subprocess.py", line 746, in _make_inheritable >> DUPLICATE_SAME_ACCESS) >> WindowsError: [Error 6] The handle is invalid >> >> *Python Script:* >> *test.py* >> import subprocess,os >> my_bat = os.getcwd()+'\\test.bat' >> proc = subprocess.Popen(my_bat,stdin=subprocess.PIPE) >> input = '\n' >> proc.communicate(input) >> >> *Batch File* >> *test.bat* >> echo "START' >> pause >> echo 'END' >> Please help me with this issue. >> >> Thanks In Advance, >> Tarun >> >> > subprocess.Popen() is expecting the name of a program, which should > normally have an extension of .exe You're handing it a .bat file, > which is not executable. It only executes in the context of a command > interpreter (shell), such as cmd.exe > > You can probably do what you want by running "cmd.exe" and passing it > "test.bat" as a parameter Sounds reasonable, but isn't actually true. This works fine: import subprocess open ("t.bat", "w").write ("echo hello") subprocess.Popen ("t.bat") TJG From mal at egenix.com Tue Apr 7 08:30:19 2009 From: mal at egenix.com (M.-A. Lemburg) Date: Tue, 07 Apr 2009 14:30:19 +0200 Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: <49D66C6E.3090602@v.loewis.de> References: <49D4DA72.60401@v.loewis.de> <49D52115.6020001@egenix.com> <49D66C6E.3090602@v.loewis.de> Message-ID: <49DB475B.8060504@egenix.com> [Resent due to a python.org mail server problem] On 2009-04-03 22:07, Martin v. L?wis wrote: >> I'd like to extend the proposal to Python 2.7 and later. > > I don't object, but I also don't want to propose this, so > I added it to the discussion. > > My (and perhaps other people's) concern is that 2.7 might > well be the last release of the 2.x series. If so, adding > this feature to it would make 2.7 an odd special case for > users and providers of third party tools. I certainly hope that we'll see more useful features backported from 3.x to the 2.x series or forward ported from 2.x to 3.x (depending on what the core developer preferences are). Regarding this particular PEP, it is well possible to implement an importer that provides the functionality for Python 2.3-2.7 versions, so it doesn't have to be an odd special case. >> That's going to slow down Python package detection a lot - you'd >> replace an O(1) test with an O(n) scan. > > I question that claim. In traditional Unix systems, the file system > driver performs a linear search of the directory, so it's rather > O(n)-in-kernel vs. O(n)-in-Python. Even for advanced file systems, > you need at least O(log n) to determine whether a specific file is > in a directory. For all practical purposes, the package directory > will fit in a single disk block (containing a single .pkg file, and > one or few subpackages), making listdir complete as fast as stat. On second thought, you're right, it won't be that costly. It requires an os.listdir() scan due to the wildcard approach and in some cases, such a scan may not be possible, e.g. when using frozen packages. Indeed, the freeze mechanism would not even add the .pkg files - it only handles .py file content. The same is true for distutils, MANIFEST generators and other installer mechanisms - it would have to learn to package the .pkg files along with the Python files. Another problem with the .pkg file approach is that the file extension is already in use for e.g. Mac OS X installers. You don't have those issues with the __pkg__.py file approach I suggested. >> Wouldn't it be better to stick with a simpler approach and look for >> "__pkg__.py" files to detect namespace packages using that O(1) check ? > > Again - this wouldn't be O(1). More importantly, it breaks system > packages, which now again have to deal with the conflicting file names > if they want to install all portions into a single location. True, but since that means changing the package infrastructure, I think it's fair to ask distributors who want to use that approach to also take care of looking into the __pkg__.py files and merging them if necessary. Most of the time the __pkg__.py files will be empty, so that's not really much to ask for. >> This would also avoid any issues you'd otherwise run into if you want >> to maintain this scheme in an importer that doesn't have access to a list >> of files in a package directory, but is well capable for the checking >> the existence of a file. > > Do you have a specific mechanism in mind? Yes: frozen modules and imports straight from a web resource. The .pkg file approach requires a directory scan and additional support from all importers. The __pkg__.py approach I suggested can use existing importers without modifications by checking for the existence of such a Python module in an importer managed resource. -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Apr 07 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/ ________________________________________________________________________ 2009-03-19: Released mxODBC.Connect 1.0.1 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 rdmurray at bitdance.com Tue Apr 7 08:32:01 2009 From: rdmurray at bitdance.com (R. David Murray) Date: Tue, 7 Apr 2009 12:32:01 +0000 (UTC) Subject: cgi file limit size? References: Message-ID: davidj411 wrote: > I am wondering where the limitation of filesize comes from when i > upload a large file. > it uploads when the filesize is less than 20 MB (but not if larger). > the script does not limit the filesize so it is either an HTTP > specification or a webserver limit, right? > maybe my connection to the server is timing out during the upload? > web server is IIS 6.0. > python is 2.5.2. > IIS webmapping does not use "-u" b/c nothing works when that option is > used. What are you using to do the upload? What error message do you get? -- R. David Murray http://www.bitdance.com From kay.schluehr at gmx.net Tue Apr 7 08:37:29 2009 From: kay.schluehr at gmx.net (Kay Schluehr) Date: Tue, 7 Apr 2009 05:37:29 -0700 (PDT) Subject: nonlocal in Python 2.6 Message-ID: <9ce7652b-6e76-4e54-b515-8272fa82cc06@l1g2000yqk.googlegroups.com> I always wondered about the decision to omit the nonlocal statement from the Python 2.X series because it seems to be orthogonal to Python 2.5. Are there any chances for it to be back ported? From victorsubervi at gmail.com Tue Apr 7 08:38:19 2009 From: victorsubervi at gmail.com (Victor Subervi) Date: Tue, 7 Apr 2009 08:38:19 -0400 Subject: Eval Problem In-Reply-To: <1239057448.25384.9.camel@mctell> References: <4dc0cfea0904061211u44ba605bx8638414144025bc0@mail.gmail.com> <1239057448.25384.9.camel@mctell> Message-ID: <4dc0cfea0904070538s4680d67ayf5ff874f438640b6@mail.gmail.com> Yes, Python is dynamic, but my server farm, in this era of big business screwing the client and cutting all services and customer support, uses such crappy hardware that all normal code breaks, so I am forced to find work-arounds. Here is more complete code: ourFile = string.split(__file__, "/") p = ourFile[len(ourFile) - 1] p = p[: - 9] site = ourFile[4] bitsFile = p + ".bits" bitties = 0 bits = [] Above, I define a bits file with bits of code that the broken python interpreter will not evaluate with normal text, so must be separated into its own file and called line by line and blended with separated files that quote only text. Pain in the hind end ;) try: file = open(bitsFile, "r") for line in file: if len(line) > 2: bits.append(line) bitties += 1 except: pass x = 1 while x <= bitties: file = open(p + str(x) + ".txt") for line in file: print line print eval(bits[x - 1]) x += 1 I have excluded the code where I call the separate text files for printing normal text. They work. It's my code that I cannot get to work. For example, if I take out the "eval" part of the above, it will nicely print the commands, such as this: tableTop(123,456) which is supposed to call said fn. If I place that line in the file calling the text files and the bits file it will execute just fine, but that inevitably makes my job harder. Ideas? TIA, Victor On Mon, Apr 6, 2009 at 6:37 PM, J. Clifford Dyer wrote: > On Mon, 2009-04-06 at 15:11 -0400, Victor Subervi wrote: > > Hi: > > I have this code: > > > > x = 1 > > while x <= bitties: > > file = open(p + str(x) + ".txt") > > for line in file: > > print line > > print eval(bits[x - 1]) > > x += 1 > > > > which throws this error: > > > > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] > > PythonHandler mod_python.cgihandler: Traceback (most recent call > > last): > > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] > > PythonHandler mod_python.cgihandler: File > > "/usr/lib64/python2.4/site-packages/mod_python/apache.py", line 299, > > in HandlerDispatch\n result = object(req) > > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] > > PythonHandler mod_python.cgihandler: File > > "/usr/lib64/python2.4/site-packages/mod_python/cgihandler.py", line > > 96, in handler\n imp.load_module(module_name, fd, path, desc) > > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] > > PythonHandler mod_python.cgihandler: File > > "/var/www/vhosts/articles.13gems.com/httpdocs/index_frame.py", line > > 89, in ?\n print eval(bits[1]) > > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] > > PythonHandler mod_python.cgihandler: File "", line 1 > > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] > > PythonHandler mod_python.cgihandler: tableBottom(348,180) > > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] > > PythonHandler mod_python.cgihandler: ^ > > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] > > PythonHandler mod_python.cgihandler: SyntaxError: invalid syntax > > > > Hmm. That's not the problem I get. For me, your code raises: > "NameError: name 'bitties' is not defined" > > It's easier for us to help you if you present a self-contained piece of > code that exhibits the problem you've encountered. Creating that code > will often reveal the problem with the original code, and you will have > solved your own problem. > > The other problem with your code is that you are using eval. Eval leads > to difficult-to-debug errors, and is usually unnecessary, given the > dynamic nature of python. If you need to access a class method using a > string, you can use getattr. If you need to dynamically select a > function based on some condition known in another variable, you can use > a dictionary to hash into the function. > > It's hard to say what might be appropriate for your situation, because > your code fragment is so... fragmentary. > > Cheers, > Cliff > > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From python.list at tim.thechases.com Tue Apr 7 08:44:56 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Tue, 07 Apr 2009 07:44:56 -0500 Subject: Scraping a web page In-Reply-To: <9c8c445f0904070508p52e6011vaee4c5ce2b66d406@mail.gmail.com> References: <9c8c445f0904070508p52e6011vaee4c5ce2b66d406@mail.gmail.com> Message-ID: <49DB4AC8.8050404@tim.thechases.com> > f = urllib.urlopen("http://www.google.com") > s = f.read() > > It is working, but it's returning the source of the page. Is there anyway I > can get almost a screen capture of the page? This is the job of a browser -- to render the source HTML. As such, you'd want to look into any of the browser-automation libraries to hook into IE, FireFox, Opera, or maybe using the WebKit/KHTML control. You may then be able to direct it to render the HTML into a canvas you can then treat as an image. Another alternative might be provided by some web-services that will render a page as HTML with various browsers and then send you the result. However, these are usually either (1) asynchronous or (2) paid services (or both). -tkc From andrew at acooke.org Tue Apr 7 09:01:04 2009 From: andrew at acooke.org (andrew cooke) Date: Tue, 7 Apr 2009 09:01:04 -0400 (CLT) Subject: in place list modification necessary? What's a better idiom? In-Reply-To: References: <7790d731-109b-4373-986f-c28699e29a6b@b7g2000pre.googlegroups.com> <688a5e3d-38f5-45d9-a115-ca43435fa8e9@i28g2000prd.googlegroups.com> <495e3949bce027f09eee99beaacb22fc.squirrel@localhost> Message-ID: <43b0c55c69d0d09c321dc2fabbdf8d5d.squirrel@acooke.dyndns.org> R. David Murray wrote: >> [...] >> try: >> dimensions.append(float(s)) >> except: >> dimensions.append(float(quantization[s])) > > No, no, no; never use a bare except! :) can you explain why? i can't think of any reason why the code would be better catching a specific exception. as a general rule, maybe, but in this particular case i can't see a reason - so i'm not sure if you're just pedantically following rules or if i've missed something i should know. andrew From mail at timgolden.me.uk Tue Apr 7 09:05:03 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Tue, 07 Apr 2009 14:05:03 +0100 Subject: Issue with subprocess Module In-Reply-To: References: Message-ID: <49DB4F7F.1030104@timgolden.me.uk> tarun wrote: > Hello All, > > I've a batch file to be invoke using a python script. The batch file has > pause, and the time, I need to send some command to the batch file from my > scripts. I placed both, the batch file (test.bat) and the python script > (test.py) in the same folder. And executed 'test.py' I can't reproduce this in Python 2.6.1. The following is the result of cut-and-pasting your test.py & test.bat and running them from the command line: C:\temp>test.py C:\temp>echo "START' "START' C:\temp>pause Press any key to continue . . . C:\temp>echo 'END' 'END' C:\temp> which is pretty much which I'd expected. I know there have been quite a few changes to the subprocess module between Python 2.5 & 2.6 so maybe you need to upgrade. (Failing that, we'd have to work a bit harder to pin down a specific bug in the 2.5 code since 2.5 is now in bugfix-release mode only, I think). BTW, echo simply echoes whatever follows it, regardless of (or including) quotes of any sort. So just do: echo START. TJG From jjposner at snet.net Tue Apr 7 09:07:51 2009 From: jjposner at snet.net (John Posner) Date: Tue, 07 Apr 2009 09:07:51 -0400 Subject: How can I change size of GUI? In-Reply-To: References: <6dd057ac-9fb9-4979-861f-59f36e3561e7@s20g2000yqh.googlegroups.com> Message-ID: <49DB5027.40801@snet.net> > For what definition of 'did not work'? Seems perfectly fine to me. Just try to > add the lines: > > root = Tk() > Try adding a line like this: root.geometry("400x300+100+75") ... which means: "make the window 400 pixels wide and 300 pixels high, with the upperleft corner at point (100,75)" > frm = ScrolledFrame(root) > frm.pack() > root.mainloop() > From sjmachin at lexicon.net Tue Apr 7 09:10:45 2009 From: sjmachin at lexicon.net (John Machin) Date: Tue, 7 Apr 2009 06:10:45 -0700 (PDT) Subject: Python C API String Memory Consumption References: <41d95468-8f7a-4647-83e1-6df147744467@u8g2000yqn.googlegroups.com> <6d8e5740-0ee9-46b6-97df-9e73be81c24b@e18g2000yqo.googlegroups.com> Message-ID: <602f5fb3-84c8-4347-8a8f-dd6777197551@s22g2000prg.googlegroups.com> On Apr 7, 9:19?pm, MRAB wrote: > k3xji wrote: > > Interestaing I changed malloc()/free() usage with PyMem_xx APIs and > > the problem resolved. However, I really cannot understand why the > > first version does not work. Here is the latest code that has no > > problems at all: > > > static PyObject * > > penc(PyObject *self, PyObject *args) > > { > > ? ?PyObject * result = NULL; > > ? ?unsigned char *s= NULL; > > ? ?unsigned char *buf = NULL; > > ? ?unsigned int v,len,i = 0; > > > ? ?if (!PyArg_ParseTuple(args, "s#", &s, &len)) > > ? ? ? ? return NULL; > > > ? ?buf = (unsigned char *) PyMem_Malloc(len); > > ? ?if (buf == NULL) { > > ? ? ? ? ? ?PyErr_NoMemory(); > > ? ? ? ? ? ?return NULL; > > ? ?} > > > ? ? ? ? /* string manipulation. */ > > > ? ?result = PyString_FromStringAndSize((char *)buf, len); > > ? ?PyMem_Free(buf); > > ? ?return result; > > } > > In general I'd say don't mix your memory allocators. I don't know > whether CPython implements PyMem_Malloc using malloc, The fantastic manual (http://docs.python.org/c-api/ memory.html#overview) says: """the C allocator and the Python memory manager ... implement different algorithms and operate on different heaps""". > but it's better to > stick with CPython's memory allocators when writing for CPython. for the reasons given in the last paragraph of the above reference. HTH, John From google at mrabarnett.plus.com Tue Apr 7 09:15:55 2009 From: google at mrabarnett.plus.com (MRAB) Date: Tue, 07 Apr 2009 14:15:55 +0100 Subject: Issue with subprocess Module In-Reply-To: <49DB4F7F.1030104@timgolden.me.uk> References: <49DB4F7F.1030104@timgolden.me.uk> Message-ID: <49DB520B.3080807@mrabarnett.plus.com> Tim Golden wrote: > tarun wrote: >> Hello All, >> >> I've a batch file to be invoke using a python script. The batch file has >> pause, and the time, I need to send some command to the batch file >> from my >> scripts. I placed both, the batch file (test.bat) and the python script >> (test.py) in the same folder. And executed 'test.py' > > I can't reproduce this in Python 2.6.1. The following is the > result of cut-and-pasting your test.py & test.bat and running > them from the command line: > > > C:\temp>test.py > > C:\temp>echo "START' > "START' > > C:\temp>pause > Press any key to continue . . . > > C:\temp>echo 'END' > 'END' > > C:\temp> > > > > which is pretty much which I'd expected. I know there have been quite > a few changes to the subprocess module between Python 2.5 & 2.6 so > maybe you need to upgrade. (Failing that, we'd have to work a bit > harder to pin down a specific bug in the 2.5 code since 2.5 is now > in bugfix-release mode only, I think). > > BTW, echo simply echoes whatever follows it, regardless of > (or including) quotes of any sort. So just do: echo START. > I couldn't reproduce it either. I wonder whether one or both of the files is open in an editor and whether that could cause the problem. From google at mrabarnett.plus.com Tue Apr 7 09:36:54 2009 From: google at mrabarnett.plus.com (MRAB) Date: Tue, 07 Apr 2009 14:36:54 +0100 Subject: in place list modification necessary? What's a better idiom? In-Reply-To: <43b0c55c69d0d09c321dc2fabbdf8d5d.squirrel@acooke.dyndns.org> References: <7790d731-109b-4373-986f-c28699e29a6b@b7g2000pre.googlegroups.com> <688a5e3d-38f5-45d9-a115-ca43435fa8e9@i28g2000prd.googlegroups.com> <495e3949bce027f09eee99beaacb22fc.squirrel@localhost> <43b0c55c69d0d09c321dc2fabbdf8d5d.squirrel@acooke.dyndns.org> Message-ID: <49DB56F6.9090705@mrabarnett.plus.com> andrew cooke wrote: > R. David Murray wrote: >>> [...] >>> try: >>> dimensions.append(float(s)) >>> except: >>> dimensions.append(float(quantization[s])) >> No, no, no; never use a bare except! :) > > can you explain why? i can't think of any reason why the code would be > better catching a specific exception. > > as a general rule, maybe, but in this particular case i can't see a reason > - so i'm not sure if you're just pedantically following rules or if i've > missed something i should know. > A bare exception will catch _any_ exception, even those you didn't expect (technical term: "bug" :-)); for example, if you had written: try: dimension.append(float(s)) except: dimensions.append(float(quantization[s])) it would silently catch "NameError: name 'dimension' is not defined" and perform the fallback action. From castironpi at gmail.com Tue Apr 7 09:43:11 2009 From: castironpi at gmail.com (Aaron Brady) Date: Tue, 7 Apr 2009 06:43:11 -0700 (PDT) Subject: hashing and collision detection Message-ID: <39de850f-0695-4501-b996-b712d921a1df@j8g2000yql.googlegroups.com> Hi group, You are making good on your promise from a year ago: "This is a helpful group. Give us more to go on, and you are likely to receive thousands of dollars worth of consulting for free." http://groups.google.com/group/comp.lang.python/msg/2e2906eaa804812c - Bryan Olson I have an idea, which has probably been invented a thousand times, and is probably a nice smooth round wheel by now, but I didn't know how to websearch for it, and didn't try. I am trying to floating-point collide rectangles, axis-oriented, on a large board. I thought about having every rectangle participate in a hash, several units big, and merely testing collisions within high population hashes. The pathological case is where rectangles are very small, and thus will be able to fit too many in a bucket. I'm willing to rule it out for now... for-ever. Most hash slots will only have zero or one members; therefore, I only need to check collision in a small fraction of slots: those that have two or more members. If slots are too small, moving an object is an expensive operation, because it will need to add and remove itself from many buckets. If slots are too large, too many objects can fit in them, and a simple rectangle collision will check too many. But for my purposes, the balance should be a fairly large plateau. I am thinking of a simple dictionary, which maps tuples to a list of members in that slot: { ( 0, 0 ), ( 0, 3 ), ( 0, 6 ), ( 3, 0 ), ... }. Where space becomes a concern, empty slots can be removed and re-added to and from the dictionary. Last note: the entire rectangles participate in the list of members of a slot, not just their subsegments which reside in the particular slot. What are your thoughts? From shannon.timothy at gmail.com Tue Apr 7 09:47:31 2009 From: shannon.timothy at gmail.com (Tim Shannon) Date: Tue, 7 Apr 2009 08:47:31 -0500 Subject: Weird Tk Canvas coordinate issue In-Reply-To: <49DA7E02.4070502@snet.net> References: <49DA7E02.4070502@snet.net> Message-ID: Here's my actual code, but I've tried to strip out the irrelevant parts, but it should compile. FYI, I'm using 2.6 not 3. The code is basically drawing a grid with points in it. I'm fairly new to Python, so constructive criticism is appreciated. class Editor: GSPACE = 80 OFFSET = 50 def __init__(self, master): #global variables self.grid = ex_Grid.Grid("", 10, 10) self.showPaths = IntVar() self.pathValid = False self.master = master self.isSaved = False master.title("ex_hack Editor") master.columnconfigure(0, weight=1) master.rowconfigure(0, weight=1) vscroll = Scrollbar(master) hscroll = Scrollbar(master) self.canvas = Canvas(master, height=768, width=1024, yscrollcommand=vscroll.set, xscrollcommand=hscroll.set) self.canvas.grid(column=0, row=0, sticky="wens") vscroll.config(command=self.canvas.yview) hscroll.config(command=self.canvas.xview, orient="horizontal") vscroll.grid(column=1, row=0, stick="ns") hscroll.grid(column=0, row=1, sticky="we") #truncated for brevity def draw_grid(self): """Loads the current grid on the application canvas as well as into the other fields""" #print self.OFFSET self.canvas.delete(ALL) #draw Border self.canvas.create_rectangle(self.OFFSET, self.OFFSET, (self.grid.height * self.GSPACE) + self.OFFSET, (self.grid.width * self.GSPACE) + self.OFFSET, width=2) self.canvas.create_rectangle(self.OFFSET-4, self.OFFSET-4, (self.grid.height * self.GSPACE) + self.OFFSET+4, (self.grid.width * self.GSPACE) + self.OFFSET+4, width=2) #draw limit areas for l in self.grid.limitAreas: self.canvas.create_rectangle(self.g2c(l["x1"]) - (self.GSPACE/4), self.g2c(l["y1"]) - (self.GSPACE/4), self.g2c(l["x2"]) + (self.GSPACE/4), self.g2c(l["y2"]) + (self.GSPACE/4), outline="gray", fill="gray", stipple="gray12") #draw spaces for space in self.grid.grid.values(): self.canvas.create_line(self.g2c(space.x), self.g2c(space.y), self.g2c(space.x)+1, self.g2c(space.y)+1, tags="space") if not space.is_empty(): self.draw_space(space) self.canvas.config(scrollregion=self.canvas.bbox(ALL)) def g2c(self, coord): """Converts grid locations to their actual coordinates on the drawing canvas""" return (coord * self.GSPACE) + self.OFFSET + (self.GSPACE / 2) On Mon, Apr 6, 2009 at 5:11 PM, John Posner wrote: > Tim Shannon wrote: > >> I'm new to python, so keep that in mind. >> I have a tk Canvas that I'm trying to draw on, and I want to start my >> drawing at an offset (from 0) location. So I can tweak this as I code, I >> set this offset as a class level variable: >> def ClassName: >> OFFSET = 20 >> >> def __init__(self, master)): >> self.canvas = Canvas(master) >> self.canvas.create_rectangle(self.OFFSET, >> self.OFFSET, >> 300 + self.OFFSET, >> 300 + self.OFFSET, >> width=2) >> The weird thing is, it doesn't offset. If I set my offset to 100000, it >> still starts drawing at 0,0. Here's the really weird part (at least to me), >> if I put a print line right about my drawing statements to print the value >> of the offset, it works like it should, and offsets the drawing. >> If I remove the print line, the offset goes away. >> This makes no sense to me. >> > Tim Shannon wrote: > >> I'm new to python, so keep that in mind. >> I have a tk Canvas that I'm trying to draw on, and I want to start my >> drawing at an offset (from 0) location. So I can tweak this as I code, I >> set this offset as a class level variable: >> def ClassName: >> OFFSET = 20 >> >> def __init__(self, master)): >> self.canvas = Canvas(master) >> self.canvas.create_rectangle(self.OFFSET, >> self.OFFSET, >> 300 + self.OFFSET, >> 300 + self.OFFSET, >> width=2) >> >> > The above code wouldn't even compile. Please be careful to cut-and-paste > working code into your email message. (I've made this mistake myself!) > Changes to make: > > 1. first line: change "def" to "class" > > 2. def __init__(self, master)): <--- get rid of extra ")" > > 3. Make sure to "pack" the canvas into the overall Tk window: > > self.canvas = Canvas(master) > self.canvas.pack() <--- add this line > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From jcd at sdf.lonestar.org Tue Apr 7 09:49:17 2009 From: jcd at sdf.lonestar.org (J. Cliff Dyer) Date: Tue, 07 Apr 2009 09:49:17 -0400 Subject: Eval Problem In-Reply-To: <4dc0cfea0904070538s4680d67ayf5ff874f438640b6@mail.gmail.com> References: <4dc0cfea0904061211u44ba605bx8638414144025bc0@mail.gmail.com> <1239057448.25384.9.camel@mctell> <4dc0cfea0904070538s4680d67ayf5ff874f438640b6@mail.gmail.com> Message-ID: <1239112157.5733.10.camel@aalcdl07> OK. You still haven't shown the code where tableTop gets defined, so your code is unrunnable. However, I think your problem is that wherever tableTop lives, it isn't part of your globals or locals in eval. See the documentation on evals here: http://www.python.org/doc/1.4/lib/node26.html Something like the following might work: print eval(line, {'tableTop': tableTop}) Cheers, Cliff On Tue, 2009-04-07 at 08:38 -0400, Victor Subervi wrote: > > I have excluded the code where I call the separate text files for > printing normal text. They work. It's my code that I cannot get to > work. For example, if I take out the "eval" part of the above, it will > nicely print the commands, such as this: > > tableTop(123,456) > > which is supposed to call said fn. If I place that line in the file > calling the text files and the bits file it will execute just fine, > but that inevitably makes my job harder. Ideas? > TIA, > Victor From rdmurray at bitdance.com Tue Apr 7 09:51:21 2009 From: rdmurray at bitdance.com (R. David Murray) Date: Tue, 7 Apr 2009 09:51:21 -0400 (EDT) Subject: in place list modification necessary? What's a better idiom? In-Reply-To: <43b0c55c69d0d09c321dc2fabbdf8d5d.squirrel@acooke.dyndns.org> References: <7790d731-109b-4373-986f-c28699e29a6b@b7g2000pre.googlegroups.com> <688a5e3d-38f5-45d9-a115-ca43435fa8e9@i28g2000prd.googlegroups.com> <495e3949bce027f09eee99beaacb22fc.squirrel@localhost> <43b0c55c69d0d09c321dc2fabbdf8d5d.squirrel@acooke.dyndns.org> Message-ID: On Tue, 7 Apr 2009 at 09:01, andrew cooke wrote: > R. David Murray wrote: >>> [...] >>> try: >>> dimensions.append(float(s)) >>> except: >>> dimensions.append(float(quantization[s])) >> >> No, no, no; never use a bare except! :) > > can you explain why? i can't think of any reason why the code would be > better catching a specific exception. > > as a general rule, maybe, but in this particular case i can't see a reason > - so i'm not sure if you're just pedantically following rules or if i've > missed something i should know. What if the user pressed ctl-c right when the float was being converted and appended? Never use a bare except unless you have a specific reason to do so, and there are very few of those. (Yes, I should have said it that way to start with, my apologies for going hyperbolic.) Using because it doesn't _look_ like it will cause issues is just asking for hard to track down bugs :). -- R. David Murray http://www.bitdance.com From notvalid2 at sbcglobal.net Tue Apr 7 09:56:00 2009 From: notvalid2 at sbcglobal.net (W. eWatson) Date: Tue, 07 Apr 2009 06:56:00 -0700 Subject: PIL\Tkinter and Transparencies, Rubber Lines, and Dragging Image Objects In-Reply-To: References: <_sFCl.3740$im1.3071@nlpi061.nbdc.sbc.com> Message-ID: You got it. That lamda did look a little odd. The white background is opaque and the telescope is seen as green. The program will ask for a file. I didn't write the code. Eric Brunel wrote: > W. eWatson wrote: >> Basically, I'd like to know how one (broadly, e.g., references in Win-land) >> does IP (image processing) and drawing techniques such as rubber lines, and >> dragging image objects across the canvas. I know there are some pretty >> powerful toolkits out there, but I'd like to limit this to PIL and Tkinter. >> If it can't be done with them, then I'll consider other possibilities. As a >> starter, on the topic of transparencies, consider this program that I pulled >> off the web and was posted in 1999. It purports to illustrate how one might >> produce a transparency. > > OK, maybe I'm dumb but: > >> #!/usr/bin/python >> # see http://mail.python.org/pipermail/python-list/1999-May/003388.html >> from Tkinter import * >> import Image, ImageTk ... > HTH > - Eric - -- 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 support.desk.ipg at gmail.com Tue Apr 7 09:58:54 2009 From: support.desk.ipg at gmail.com (Support Desk) Date: Tue, 7 Apr 2009 08:58:54 -0500 Subject: Scraping a web page In-Reply-To: <49DB4AC8.8050404@tim.thechases.com> References: <9c8c445f0904070508p52e6011vaee4c5ce2b66d406@mail.gmail.com> <49DB4AC8.8050404@tim.thechases.com> Message-ID: <80F56C542AB34789A90BA91D86D9FBC7@office.ipglobal.net> You could do something like below to get the rendered page. Import os site = 'website.com' X = os.popen('lynx --dump %s' % site).readlines() -----Original Message----- From: Tim Chase [mailto:python.list at tim.thechases.com] Sent: Tuesday, April 07, 2009 7:45 AM To: Ronn Ross Cc: python-list at python.org Subject: Re: Scraping a web page > f = urllib.urlopen("http://www.google.com") > s = f.read() > > It is working, but it's returning the source of the page. Is there anyway I > can get almost a screen capture of the page? This is the job of a browser -- to render the source HTML. As such, you'd want to look into any of the browser-automation libraries to hook into IE, FireFox, Opera, or maybe using the WebKit/KHTML control. You may then be able to direct it to render the HTML into a canvas you can then treat as an image. Another alternative might be provided by some web-services that will render a page as HTML with various browsers and then send you the result. However, these are usually either (1) asynchronous or (2) paid services (or both). -tkc From andrew at mackeith.net Tue Apr 7 10:01:51 2009 From: andrew at mackeith.net (Andrew MacKeith) Date: Tue, 07 Apr 2009 10:01:51 -0400 Subject: PyXML and Python-2.6 Message-ID: <49db5ccf$1@news.abaqus.com> The Python.org "SIG for XML Processing in Python" page indicates that "The SIG, through the mailing list and the PyXML project hosted on SourceForge...". The PyXML project on SourceForge " is no longer maintained. ", so perhaps the SIG page could be updated. Is there a guide to porting projects that depend on PyXML to Python-2.6? Andrew MacKeith From pje at telecommunity.com Tue Apr 7 10:05:45 2009 From: pje at telecommunity.com (P.J. Eby) Date: Tue, 07 Apr 2009 10:05:45 -0400 Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: <49DB475B.8060504@egenix.com> References: <49D4DA72.60401@v.loewis.de> <49D52115.6020001@egenix.com> <49D66C6E.3090602@v.loewis.de> <49DB475B.8060504@egenix.com> Message-ID: <20090407140317.EBD383A4063@sparrow.telecommunity.com> At 02:30 PM 4/7/2009 +0200, M.-A. Lemburg wrote: > >> Wouldn't it be better to stick with a simpler approach and look for > >> "__pkg__.py" files to detect namespace packages using that O(1) check ? > > > > Again - this wouldn't be O(1). More importantly, it breaks system > > packages, which now again have to deal with the conflicting file names > > if they want to install all portions into a single location. > >True, but since that means changing the package infrastructure, I think >it's fair to ask distributors who want to use that approach to also take >care of looking into the __pkg__.py files and merging them if >necessary. > >Most of the time the __pkg__.py files will be empty, so that's not >really much to ask for. This means your proposal actually doesn't add any benefit over the status quo, where you can have an __init__.py that does nothing but declare the package a namespace. We already have that now, and it doesn't need a new filename. Why would we expect OS vendors to start supporting it, just because we name it __pkg__.py instead of __init__.py? From benjamin at python.org Tue Apr 7 10:06:42 2009 From: benjamin at python.org (Benjamin Peterson) Date: Tue, 7 Apr 2009 14:06:42 +0000 (UTC) Subject: nonlocal in Python 2.6 References: <9ce7652b-6e76-4e54-b515-8272fa82cc06@l1g2000yqk.googlegroups.com> Message-ID: Kay Schluehr gmx.net> writes: > > I always wondered about the decision to omit the nonlocal statement > from the Python 2.X series because it seems to be orthogonal to Python > 2.5. Are there any chances for it to be back ported? The only reason it was not backported was that either no one provided a patch or it never got reviewed and applied. From benjamin at python.org Tue Apr 7 10:12:24 2009 From: benjamin at python.org (Benjamin Peterson) Date: Tue, 7 Apr 2009 14:12:24 +0000 (UTC) Subject: Python C API String Memory Consumption References: <41d95468-8f7a-4647-83e1-6df147744467@u8g2000yqn.googlegroups.com> Message-ID: Carl Banks gmail.com> writes: > However, Python apparently does leak a reference if passed a Unicode > object; PyArg_ParseTuple automatically creates an encoded string but > never decrefs it. (That might be necessary evil to preserve > compatibility, though. PyString_AS_STRING does it too.) Unicode objects cache a copy of themselves as default encoded strings. It is deallocated when the unicode object its self is. From davea at ieee.org Tue Apr 7 10:18:11 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 07 Apr 2009 10:18:11 -0400 Subject: Issue with subprocess Module In-Reply-To: <49DB4557.90903@timgolden.me.uk> References: <49DB3EC4.4040004@ieee.org> <49DB4557.90903@timgolden.me.uk> Message-ID: <49DB60A3.4070707@ieee.org> Tim Golden wrote: >
Dave > Angel wrote: >> tarun wrote: >>> Hello All, >>> >>> I've a batch file to be invoke using a python script. The batch >>> file has >>> pause, and the time, I need to send some command to the batch file >>> from my >>> scripts. I placed both, the batch file (test.bat) and the python script >>> (test.py) in the same folder. And executed 'test.py' >>> >>> (Please find the source files and error below). >>> >>> *I get the following error:* >>> Traceback (most recent call last): >>> File "", line 74, in run_nodebug >>> File "D:\test.py", line 4, in >>> proc = subprocess.Popen(my_bat,stdin=subprocess.PIPE) >>> File "C:\Python25\lib\subprocess.py", line 588, in __init__ >>> errread, errwrite) = self._get_handles(stdin, stdout, stderr) >>> File "C:\Python25\lib\subprocess.py", line 717, in _get_handles >>> c2pwrite = self._make_inheritable(c2pwrite) >>> File "C:\Python25\lib\subprocess.py", line 746, in _make_inheritable >>> DUPLICATE_SAME_ACCESS) >>> WindowsError: [Error 6] The handle is invalid >>> >>> *Python Script:* >>> *test.py* >>> import subprocess,os >>> my_bat = os.getcwd()+'\\test.bat' >>> proc = subprocess.Popen(my_bat,stdin=subprocess.PIPE) >>> input = '\n' >>> proc.communicate(input) >>> >>> *Batch File* >>> *test.bat* >>> echo "START' >>> pause >>> echo 'END' >>> Please help me with this issue. >>> >>> Thanks In Advance, >>> Tarun >>> >>> >> subprocess.Popen() is expecting the name of a program, which should >> normally have an extension of .exe You're handing it a .bat file, >> which is not executable. It only executes in the context of a >> command interpreter (shell), such as cmd.exe >> >> You can probably do what you want by running "cmd.exe" and passing it >> "test.bat" as a parameter > > Sounds reasonable, but isn't actually true. This works fine: > > > import subprocess > > open ("t.bat", "w").write ("echo hello") > subprocess.Popen ("t.bat") > > > > TJG > >
> The docs of Popen() state that it uses CreateProcess() on Windows, so I didn't even try it. Thanks for informing me it works. I see now the COMSPEC manipulation in _execute_child(), but I'm still puzzled. When I step through, it skips that part because we didn't specify shell= as an argument. It still had not put the cmd.exe /c into the args variable when it called CreateProcess(). So is the Python CreateProcess more than a thin wrapper around Windows version? Another bug in this program is that it does not include quotes around the program name. If the current directory has a space in it, and an appropriately named executable happens to be in the parent directory of the one with the space, it'll get run instead of the batch. For example, if the current directory is c:\source code\test and there's a file in the route called c:\source.exe then it'll get run. But what about the OP problem? I now see it runs okay for me, both stand-alone and inside Komodo. But he's getting an exception inside make_inheritable(), which is apparently being passed an invalid handle. Runs OK for me, in Python 2.6 running under XP, SP3. 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)] From lordgabriel14 at gmail.com Tue Apr 7 10:35:00 2009 From: lordgabriel14 at gmail.com (janus99) Date: Tue, 7 Apr 2009 07:35:00 -0700 (PDT) Subject: Python syntax Message-ID: <7dd228af-e549-444d-8623-11e95185123c@y9g2000yqg.googlegroups.com> well i'm pretty much a newb on python, it's my first comp lang. and i've been having some diffuclties, i want to get the hang of it and i'm wondering if u could help me understand the syntax structure. I messed around with my own comp (windos xp) command prompt and i currently play a mud, but the python syntax is completly diffrent, i was wondering if u could expalin how to syntax goes, i've tried the "hello world" but it keep displaying syntax error, i've tried entering random commands, e.g find,search, collect,... From andrew at acooke.org Tue Apr 7 10:37:06 2009 From: andrew at acooke.org (andrew cooke) Date: Tue, 7 Apr 2009 10:37:06 -0400 (CLT) Subject: in place list modification necessary? What's a better idiom? In-Reply-To: <49DB56F6.9090705@mrabarnett.plus.com> References: <7790d731-109b-4373-986f-c28699e29a6b@b7g2000pre.googlegroups.com> <688a5e3d-38f5-45d9-a115-ca43435fa8e9@i28g2000prd.googlegroups.com> <495e3949bce027f09eee99beaacb22fc.squirrel@localhost> <43b0c55c69d0d09c321dc2fabbdf8d5d.squirrel@acooke.dyndns.org> <49DB56F6.9090705@mrabarnett.plus.com> Message-ID: <70dce5b96d3e096c5c84af7245821007.squirrel@localhost> MRAB wrote: > andrew cooke wrote: >> R. David Murray wrote: >>>> [...] >>>> try: >>>> dimensions.append(float(s)) >>>> except: >>>> dimensions.append(float(quantization[s])) >>> No, no, no; never use a bare except! :) >> >> can you explain why? i can't think of any reason why the code would be >> better catching a specific exception. >> >> as a general rule, maybe, but in this particular case i can't see a >> reason >> - so i'm not sure if you're just pedantically following rules or if i've >> missed something i should know. >> > A bare exception will catch _any_ exception, even those you didn't > expect (technical term: "bug" :-)); for example, if you had written: > > try: > dimension.append(float(s)) > except: > dimensions.append(float(quantization[s])) > > it would silently catch "NameError: name 'dimension' is not defined" and > perform the fallback action. true, i hadn't considered coding errors (i was thinking that something global, like out of memory, would fail again anyway). andrew From invalid at invalid Tue Apr 7 10:37:35 2009 From: invalid at invalid (Grant Edwards) Date: Tue, 07 Apr 2009 09:37:35 -0500 Subject: Adding method to class at run-time: bad style? Message-ID: I realize that technically all methods are added to classes at "run-time", but what I'm talking about is this: import ClientForm def controlEqual(self,other): return (self.type == other.type) and \ (self.name == other.name) and \ (self.value == other.value) and \ (self.disabled == other.disabled) and\ (self.readonly == self.readonly) def controlNotEqual(self,other): return not (self==other) ClientForm.Control.__eq__ = controlEqual ClientForm.Control.__ne__ = controlNotEqual def formEqual(self,other): if len(self.controls) != len(other.controls): return False for (c1,c2) in zip(self.controls,other.controls): if c1 != c2: return False return True def formNotEqual(self,other): return not (self==other) ClientForm.HTMLForm.__eq__ = formEqual ClientForm.HTMLForm.__ne__ = formNotEqual It works fine, but it seems like it might be dangerous (or just bad form) to insert methods into existing classes like that. Perhaps it would be safer to make sure that __eq__, __ne__, and __cmp__ don't exist before adding my own? -- Grant Edwards grante Yow! NEWARK has been at REZONED!! DES MOINES has visi.com been REZONED!! From michele.simionato at gmail.com Tue Apr 7 10:43:48 2009 From: michele.simionato at gmail.com (Michele Simionato) Date: Tue, 7 Apr 2009 07:43:48 -0700 (PDT) Subject: Adding method to class at run-time: bad style? References: Message-ID: On Apr 7, 4:37?pm, Grant Edwards wrote: > I realize that technically all methods are added to classes at > "run-time", but what I'm talking about is this: Raymond Hettinger solves this problem nicely with a class decorator: http://code.activestate.com/recipes/576685/ From dorzey at googlemail.com Tue Apr 7 10:47:02 2009 From: dorzey at googlemail.com (dorzey) Date: Tue, 7 Apr 2009 07:47:02 -0700 (PDT) Subject: Python syntax References: <7dd228af-e549-444d-8623-11e95185123c@y9g2000yqg.googlegroups.com> Message-ID: <4e80071a-01bb-4297-a90e-77a79b53ac0b@n17g2000vba.googlegroups.com> http://wiki.python.org/moin/BeginnersGuide is probably a good place to start. I found lots of useful info from the links on this page when I started programming in Python. Dorzey From paul at boddie.org.uk Tue Apr 7 10:49:46 2009 From: paul at boddie.org.uk (Paul Boddie) Date: Tue, 7 Apr 2009 07:49:46 -0700 (PDT) Subject: PyXML and Python-2.6 References: <49db5ccf$1@news.abaqus.com> Message-ID: On 7 Apr, 16:01, Andrew MacKeith wrote: > The Python.org "SIG for XML Processing in Python" page indicates that > "The SIG, through the mailing list and the PyXML project hosted on > SourceForge...". > > The PyXML project on SourceForge " is no longer maintained. ", so > perhaps the SIG page could be updated. I think the SIG pages should be migrated to the Wiki. There seems to be some work proceeding on this: http://wiki.python.org/moin/Special_Interest_Groups > Is there a guide to porting projects that depend on PyXML to Python-2.6? You could start here: http://wiki.python.org/moin/PythonXml Look under "W3C DOM-like libraries" for some candidates. I've maintained libxml2dom for quite some time because I started out with PyXML and wanted something whose maintenance was guaranteed, even if it was me who was going to be doing it. Paul From mal at egenix.com Tue Apr 7 10:58:39 2009 From: mal at egenix.com (M.-A. Lemburg) Date: Tue, 07 Apr 2009 16:58:39 +0200 Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: <20090407140317.EBD383A4063@sparrow.telecommunity.com> References: <49D4DA72.60401@v.loewis.de> <49D52115.6020001@egenix.com> <49D66C6E.3090602@v.loewis.de> <49DB475B.8060504@egenix.com> <20090407140317.EBD383A4063@sparrow.telecommunity.com> Message-ID: <49DB6A1F.50801@egenix.com> On 2009-04-07 16:05, P.J. Eby wrote: > At 02:30 PM 4/7/2009 +0200, M.-A. Lemburg wrote: >> >> Wouldn't it be better to stick with a simpler approach and look for >> >> "__pkg__.py" files to detect namespace packages using that O(1) >> check ? >> > >> > Again - this wouldn't be O(1). More importantly, it breaks system >> > packages, which now again have to deal with the conflicting file names >> > if they want to install all portions into a single location. >> >> True, but since that means changing the package infrastructure, I think >> it's fair to ask distributors who want to use that approach to also take >> care of looking into the __pkg__.py files and merging them if >> necessary. >> >> Most of the time the __pkg__.py files will be empty, so that's not >> really much to ask for. > > This means your proposal actually doesn't add any benefit over the > status quo, where you can have an __init__.py that does nothing but > declare the package a namespace. We already have that now, and it > doesn't need a new filename. Why would we expect OS vendors to start > supporting it, just because we name it __pkg__.py instead of __init__.py? I lost you there. Since when do we support namespace packages in core Python without the need to add some form of magic support code to __init__.py ? My suggestion basically builds on the same idea as Martin's PEP, but uses a single __pkg__.py file as opposed to some non-Python file yaddayadda.pkg. Here's a copy of the proposal, with some additional discussion bullets added: """ Alternative Approach: --------------------- Wouldn't it be better to stick with a simpler approach and look for "__pkg__.py" files to detect namespace packages using that O(1) check ? This would also avoid any issues you'd otherwise run into if you want to maintain this scheme in an importer that doesn't have access to a list of files in a package directory, but is well capable for the checking the existence of a file. Mechanism: ---------- If the import mechanism finds a matching namespace package (a directory with a __pkg__.py file), it then goes into namespace package scan mode and scans the complete sys.path for more occurrences of the same namespace package. The import loads all __pkg__.py files of matching namespace packages having the same package name during the search. One of the namespace packages, the defining namespace package, will have to include a __init__.py file. After having scanned all matching namespace packages and loading the __pkg__.py files in the order of the search, the import mechanism then sets the packages .__path__ attribute to include all namespace package directories found on sys.path and finally executes the __init__.py file. (Please let me know if the above is not clear, I will then try to follow up on it.) Discussion: ----------- The above mechanism allows the same kind of flexibility we already have with the existing normal __init__.py mechanism. * It doesn't add yet another .pth-style sys.path extension (which are difficult to manage in installations). * It always uses the same naive sys.path search strategy. The strategy is not determined by some file contents. * The search is only done once - on the first import of the package. * It's possible to have a defining package dir and add-one package dirs. * The search does not depend on the order of directories in sys.path. There's no requirement for the defining package to appear first on sys.path. * Namespace packages are easy to recognize by testing for a single resource. * There's no conflict with existing files using the .pkg extension such as Mac OS X installer files or Solaris packages. * Namespace __pkg__.py modules can provide extra meta-information, logging, etc. to simplify debugging namespace package setups. * It's possible to freeze such setups, to put them into ZIP files, or only have parts of it in a ZIP file and the other parts in the file-system. * There's no need for a package directory scan, allowing the mechanism to also work with resources that do not permit to (easily and efficiently) scan the contents of a package "directory", e.g. frozen packages or imports from web resources. Caveats: * Changes to sys.path will not result in an automatic rescan for additional namespace packages, if the package was already loaded. However, we could have a function to make such a rescan explicit. """ -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Apr 07 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/ ________________________________________________________________________ 2009-03-19: Released mxODBC.Connect 1.0.1 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 cssaua at hotmail.com Tue Apr 7 11:00:08 2009 From: cssaua at hotmail.com (Lydia) Date: Tue, 7 Apr 2009 09:00:08 -0600 Subject: Expression Message-ID: I am working on calculating one of the fields in a feature class based on other 2 fields. The logic is, A (the resulting field) is calculated from B, but C and D have part of the value that could fill the blank of B, which meaning that combine three fields of values can make A. Field A is what I need. The data looks like: . A B C D 2 2 5 5 4 4 6 6 cur = gp.UpdateCursor(data) row = cur.Next() gp.CalculateField_management(data, "A", "[B]", "VB", "") while row: cur.UpdateRow(row) if not(row.GetValue("C") == 'NULL'): row.SetValue("A",row.GetValue("C")); elif not(row.GetValue("D") == 'NULL'): row.SetValue("A",row.GetValue("D")); row = cur.Next() del cur del row But the out looks like only B was calculated to A successfully. C&D are not in A. I guess there must be something wrong with the code, but I am very new to Python, and not familiar with the expression. Could anybody help ? PS. I am coding Python with ARCGIS. Thanks a lot. Lydia -------------- next part -------------- An HTML attachment was scrubbed... URL: From support.desk.ipg at gmail.com Tue Apr 7 11:02:15 2009 From: support.desk.ipg at gmail.com (Support Desk) Date: Tue, 7 Apr 2009 10:02:15 -0500 Subject: Scraping a web page In-Reply-To: <9c8c445f0904070737u33545112y1a3351390f47d6d@mail.gmail.com> References: <9c8c445f0904070508p52e6011vaee4c5ce2b66d406@mail.gmail.com> <49DB4AC8.8050404@tim.thechases.com> <80F56C542AB34789A90BA91D86D9FBC7@office.ipglobal.net> <9c8c445f0904070737u33545112y1a3351390f47d6d@mail.gmail.com> Message-ID: <8B3132230D04431BB285D86104B3C067@office.ipglobal.net> If your only interested in the Images, perhaps you want to use wget like: wget -r --accept=jpg,jpeg www.xyz.org or maybe this http://www.vex.net/~x/python_stuff.html BackCrawler 1.1 A crude web spider with only one purpose: mercilessly suck the background images from all web pages it can find. Understands frames and redirects, uses MD5 to elimate duplicates. Need web page backgrounds? This'll get lots of them. Sadly, most are very tacky, and Backcrawler can't help with that. Requires Threads. _____ From: Ronn Ross [mailto:ronn.ross at gmail.com] Sent: Tuesday, April 07, 2009 9:37 AM To: Support Desk Subject: Re: Scraping a web page This works great, but is there a way to do this with firefox or something similar so I can also print the images from the site? On Tue, Apr 7, 2009 at 9:58 AM, Support Desk wrote: You could do something like below to get the rendered page. Import os site = 'website.com' X = os.popen('lynx --dump %s' % site).readlines() -----Original Message----- From: Tim Chase [mailto:python.list at tim.thechases.com] Sent: Tuesday, April 07, 2009 7:45 AM To: Ronn Ross Cc: python-list at python.org Subject: Re: Scraping a web page > f = urllib.urlopen("http://www.google.com") > s = f.read() > > It is working, but it's returning the source of the page. Is there anyway I > can get almost a screen capture of the page? This is the job of a browser -- to render the source HTML. As such, you'd want to look into any of the browser-automation libraries to hook into IE, FireFox, Opera, or maybe using the WebKit/KHTML control. You may then be able to direct it to render the HTML into a canvas you can then treat as an image. Another alternative might be provided by some web-services that will render a page as HTML with various browsers and then send you the result. However, these are usually either (1) asynchronous or (2) paid services (or both). -tkc -------------- next part -------------- An HTML attachment was scrubbed... URL: From cgoldberg at gmail.com Tue Apr 7 11:12:03 2009 From: cgoldberg at gmail.com (cgoldberg) Date: Tue, 7 Apr 2009 08:12:03 -0700 (PDT) Subject: Scraping a web page References: <9c8c445f0904070508p52e6011vaee4c5ce2b66d406@mail.gmail.com> Message-ID: <784b6eda-e0b4-436a-9072-fe0450b27a16@f19g2000yqo.googlegroups.com> > Is there anyway I > can get almost a screen capture of the page? I'm not sure exactly what you mean by "screen capture". But the webbrowser module in the standard lib might be of some help. You can use it to drive a web browser from Python. to load a page in your browser, you can do something like this: ----------------------------------- #! /usr/bin/env python import webbrowser url = 'http://www.google.com' webbrowser.open(url) ----------------------------------- -Corey From jhg at acm.org Tue Apr 7 11:22:59 2009 From: jhg at acm.org (Jim Garrison) Date: Tue, 07 Apr 2009 10:22:59 -0500 Subject: New online docs broken? In-Reply-To: <94ae55e9-265c-4444-8bba-316001db83e1@r37g2000yqn.googlegroups.com> References: <49da837c$0$29401$4c5ecfc7@fe1.usenet.com> <94ae55e9-265c-4444-8bba-316001db83e1@r37g2000yqn.googlegroups.com> Message-ID: <49db6ffe$0$29393$4c5ecfc7@fe1.usenet.com> Ye Liu wrote: > On Apr 6, 6:33 pm, Jim Garrison wrote: >> I notice the online docs (at docs.python.org/3.0/index.html) were >> updated today. It seems some of the top-level pages, like >> Tutorial, "Using Python", "Language Reference" are truncated >> after the first few paragraphs. > > Yea, same here. Hope it's going to be fixed soon. Still broken. I've emailed webmaster(at)python.org From cournape at gmail.com Tue Apr 7 11:29:02 2009 From: cournape at gmail.com (David Cournapeau) Date: Wed, 8 Apr 2009 00:29:02 +0900 Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: <49DB6A1F.50801@egenix.com> References: <49D4DA72.60401@v.loewis.de> <49D52115.6020001@egenix.com> <49D66C6E.3090602@v.loewis.de> <49DB475B.8060504@egenix.com> <20090407140317.EBD383A4063@sparrow.telecommunity.com> <49DB6A1F.50801@egenix.com> Message-ID: <5b8d13220904070829j416b2536u885cd79a33ebefb5@mail.gmail.com> On Tue, Apr 7, 2009 at 11:58 PM, M.-A. Lemburg wrote: >> >> This means your proposal actually doesn't add any benefit over the >> status quo, where you can have an __init__.py that does nothing but >> declare the package a namespace. ?We already have that now, and it >> doesn't need a new filename. ?Why would we expect OS vendors to start >> supporting it, just because we name it __pkg__.py instead of __init__.py? > > I lost you there. > > Since when do we support namespace packages in core Python without > the need to add some form of magic support code to __init__.py ? I think P. Eby refers to the problem that most packaging systems don't like several packages to have the same file - be it empty or not. That's my main personal grip against namespace packages, and from this POV, I think it is fair to say the proposal does not solve anything. Not that I have a solution, of course :) cheers, David > > My suggestion basically builds on the same idea as Martin's PEP, > but uses a single __pkg__.py file as opposed to some non-Python > file yaddayadda.pkg. > > Here's a copy of the proposal, with some additional discussion > bullets added: > > """ > Alternative Approach: > --------------------- > > Wouldn't it be better to stick with a simpler approach and look for > "__pkg__.py" files to detect namespace packages using that O(1) check ? > > This would also avoid any issues you'd otherwise run into if you want > to maintain this scheme in an importer that doesn't have access to a list > of files in a package directory, but is well capable for the checking > the existence of a file. > > Mechanism: > ---------- > > If the import mechanism finds a matching namespace package (a directory > with a __pkg__.py file), it then goes into namespace package scan mode and > scans the complete sys.path for more occurrences of the same namespace > package. > > The import loads all __pkg__.py files of matching namespace packages > having the same package name during the search. > > One of the namespace packages, the defining namespace package, will have > to include a __init__.py file. > > After having scanned all matching namespace packages and loading > the __pkg__.py files in the order of the search, the import mechanism > then sets the packages .__path__ attribute to include all namespace > package directories found on sys.path and finally executes the > __init__.py file. > > (Please let me know if the above is not clear, I will then try to > follow up on it.) > > Discussion: > ----------- > > The above mechanism allows the same kind of flexibility we already > have with the existing normal __init__.py mechanism. > > * It doesn't add yet another .pth-style sys.path extension (which are > difficult to manage in installations). > > * It always uses the same naive sys.path search strategy. The strategy > is not determined by some file contents. > > * The search is only done once - on the first import of the package. > > * It's possible to have a defining package dir and add-one package > dirs. > > * The search does not depend on the order of directories in sys.path. > There's no requirement for the defining package to appear first > on sys.path. > > * Namespace packages are easy to recognize by testing for a single > resource. > > * There's no conflict with existing files using the .pkg extension > such as Mac OS X installer files or Solaris packages. > > * Namespace __pkg__.py modules can provide extra meta-information, > logging, etc. to simplify debugging namespace package setups. > > * It's possible to freeze such setups, to put them into ZIP files, > or only have parts of it in a ZIP file and the other parts in the > file-system. > > * There's no need for a package directory scan, allowing the > mechanism to also work with resources that do not permit to > (easily and efficiently) scan the contents of a package "directory", > e.g. frozen packages or imports from web resources. > > Caveats: > > * Changes to sys.path will not result in an automatic rescan for > additional namespace packages, if the package was already loaded. > However, we could have a function to make such a rescan explicit. > """ > > -- > Marc-Andre Lemburg > eGenix.com > > Professional Python Services directly from the Source ?(#1, Apr 07 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/ > ________________________________________________________________________ > 2009-03-19: Released mxODBC.Connect 1.0.1 ? ? ?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/ > _______________________________________________ > Python-Dev mailing list > Python-Dev at python.org > http://mail.python.org/mailman/listinfo/python-dev > Unsubscribe: http://mail.python.org/mailman/options/python-dev/cournape%40gmail.com > From dullrich at sprynet.com Tue Apr 7 11:40:56 2009 From: dullrich at sprynet.com (David C. Ullrich) Date: Tue, 07 Apr 2009 10:40:56 -0500 Subject: SoHo Book Problem Message-ID: Just curious - has anyone else bought the printed Python 3 Reference Manual published by SoHo Books? Talking about what they call "Part 2" of their Python Documentation. I haven't looked in detail - Part 1 seems fine, but the typesetting in Part 2 is totally screwed up. I mean totally - on more or less every page there are lines that are unreadable because there are words printed on top of each other instead of next to each other! On the one hand I don't see how the problem could be with just my copy. On the other hand I don't see how they could _all_ be like mine - that would mean nobody even _glanced_ at what was coming out of the press. So I'm curious whether anyone else has a copy. (I know it's all online. Some people like _books_...) DU. -- David C. Ullrich From alanmf at gmail.com Tue Apr 7 11:59:37 2009 From: alanmf at gmail.com (Tairic) Date: Tue, 7 Apr 2009 08:59:37 -0700 (PDT) Subject: Best way to start References: <0e991af0-52f4-4849-9587-914bf38b3505@j39g2000yqn.googlegroups.com> <8028aa3c-09d6-4f0d-967e-055b226ce8f0@g19g2000yql.googlegroups.com> Message-ID: I was looking for a language that seemed easier to learn and that was powerful, so I just started learning Python myself. I'm using some online tutorials and The book "Python Power!" by Matt Telles. Some of the O'Reilley books looked ok (I spent a while in the bookstore deciding), but this one was a bit cheaper, and seemed to fit the bill; it covers most of what I wanted to know, and I found it pretty easy to read. My only complaint is it doesn't have you spend much time coding, so I scoured the internet for CS classes with python 'homework' and did that. Anyways, good luck! From floris.bruynooghe at gmail.com Tue Apr 7 12:04:06 2009 From: floris.bruynooghe at gmail.com (Floris Bruynooghe) Date: Tue, 7 Apr 2009 09:04:06 -0700 (PDT) Subject: Python C API String Memory Consumption References: <41d95468-8f7a-4647-83e1-6df147744467@u8g2000yqn.googlegroups.com> <6d8e5740-0ee9-46b6-97df-9e73be81c24b@e18g2000yqo.googlegroups.com> <602f5fb3-84c8-4347-8a8f-dd6777197551@s22g2000prg.googlegroups.com> Message-ID: <63ab6e58-4b41-4c9c-80ab-8456354e1116@q2g2000vbr.googlegroups.com> On Apr 7, 2:10?pm, John Machin wrote: > On Apr 7, 9:19?pm, MRAB wrote: > > > > > k3xji wrote: > > > Interestaing I changed malloc()/free() usage with PyMem_xx APIs and > > > the problem resolved. However, I really cannot understand why the > > > first version does not work. Here is the latest code that has no > > > problems at all: > > > > static PyObject * > > > penc(PyObject *self, PyObject *args) > > > { > > > ? ?PyObject * result = NULL; > > > ? ?unsigned char *s= NULL; > > > ? ?unsigned char *buf = NULL; > > > ? ?unsigned int v,len,i = 0; > > > > ? ?if (!PyArg_ParseTuple(args, "s#", &s, &len)) > > > ? ? ? ? return NULL; > > > > ? ?buf = (unsigned char *) PyMem_Malloc(len); > > > ? ?if (buf == NULL) { > > > ? ? ? ? ? ?PyErr_NoMemory(); > > > ? ? ? ? ? ?return NULL; > > > ? ?} > > > > ? ? ? ? /* string manipulation. */ > > > > ? ?result = PyString_FromStringAndSize((char *)buf, len); > > > ? ?PyMem_Free(buf); > > > ? ?return result; > > > } I assume you're doing a memcpy() somewhere in there... This is also safer then your first version since the python string can contain an embeded \0 and the strdup() of the first version would not copy that. But maybe you're sure your input doesn't have NULLs in them so it might be fine. > > > In general I'd say don't mix your memory allocators. I don't know > > whether CPython implements PyMem_Malloc using malloc, > > The fantastic manual (http://docs.python.org/c-api/ > memory.html#overview) says: """the C allocator and the Python memory > manager ... implement different algorithms and operate on different > heaps""". > > > but it's better to > > stick with CPython's memory allocators when writing for CPython. > > for the reasons given in the last paragraph of the above reference. That document explictly says you're allowed to use malloc() and free() in extensions. There is nothing wrong with allocating things on different heaps, I've done and seen it many times and never had trouble. Why the original problem ocurred I don't understand either tough. Regards Floris From maxerickson at gmail.com Tue Apr 7 12:37:10 2009 From: maxerickson at gmail.com (Max Erickson) Date: Tue, 7 Apr 2009 16:37:10 +0000 (UTC) Subject: Delicious API and urllib2 References: <3a6e324f-bf5f-4ac9-969a-882fdd341c91@r3g2000vbp.googlegroups.com> Message-ID: Bill wrote: > The delicious api requires http authorization (actually https). A > generic delicious api post url is "https:// > username:password at api.api.del.icio.us/v1/posts/add?url=http:// > example.com/&description=interesting&tags=whatever". > The simplest way is probably to manually add the authentication header, as shown here: http://code.activestate.com/recipes/267197/#c2 This article discusses the above strategy, and a strategy using HTTPBasicAuthHandler with a password manager default realm (I think getting the realm correct is what has stymied me in my attempts to use handlers, rather than injecting the header): http://www.voidspace.org.uk/python/articles/authentication.shtml Max From rdmurray at bitdance.com Tue Apr 7 12:43:44 2009 From: rdmurray at bitdance.com (R. David Murray) Date: Tue, 7 Apr 2009 16:43:44 +0000 (UTC) Subject: Issue with subprocess Module References: <49DB4F7F.1030104@timgolden.me.uk> Message-ID: Tim Golden wrote: > tarun wrote: > > Hello All, > > > > I've a batch file to be invoke using a python script. The batch file has > > pause, and the time, I need to send some command to the batch file from my > > scripts. I placed both, the batch file (test.bat) and the python script > > (test.py) in the same folder. And executed 'test.py' > > I can't reproduce this in Python 2.6.1. The following is the > result of cut-and-pasting your test.py & test.bat and running > them from the command line: > > > C:\temp>test.py > > C:\temp>echo "START' > "START' > > C:\temp>pause > Press any key to continue . . . > > C:\temp>echo 'END' > 'END' > > C:\temp> > > > > which is pretty much which I'd expected. I know there have been quite > a few changes to the subprocess module between Python 2.5 & 2.6 so > maybe you need to upgrade. (Failing that, we'd have to work a bit > harder to pin down a specific bug in the 2.5 code since 2.5 is now > in bugfix-release mode only, I think). 2.5 is in security-fix-only mode. -- R. David Murray http://www.bitdance.com From Scott.Daniels at Acm.Org Tue Apr 7 12:48:11 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Tue, 07 Apr 2009 09:48:11 -0700 Subject: On Integration Testing In-Reply-To: <1766c243-8b0a-4557-b0ef-7cb9d974cd5c@f19g2000vbf.googlegroups.com> References: <1766c243-8b0a-4557-b0ef-7cb9d974cd5c@f19g2000vbf.googlegroups.com> Message-ID: Emanuele D'Arrigo wrote: > Hi everybody, > > I just finished unit-testing a couple of tightly related modules and > it's now time to test them together to a) check if they do talk to > each other in the ways they should, b) check how fast they can talk to > each other. The problem? Well, the problem is that potentially they > can have some pretty varied conversations!! There are a lot of > different messages that they can exchange, from simple question-answer > messages to more complex [initial request, confirmation requested, > confirmation sent, final response, public announce] sequences of > messages. All this in a one-to-many context, that is one module is > effectively a server and can be connected to zero to N clients. > > Right now I do not have a particularly cohesive, consistent approach > to this type of testing. In short, I've never done it before. I'll > start with some simple tests, i.e. adding and removing clients and > sending some simple sequences of messages, maybe pushing up the number > of clients to see how much gets done in, say, a second. But it feels > fairly "unscientific". =) > > I've already googled "integration testing" and I'm in the process of > reading a variety of articles. Is there any one that the people of the > python community would like to recommend? Seems like what you want here is Function tests. At this point you are testing the whole system behavior. I presume the modules are meant to conspire to produce a result; check that that result is produced. If you really have a nice set of unit tests, the integration is testing against a misunderstanding of the interface. Your functional tests begin with simple straightforward tests, then push at definitional boundaries. One thing to vary is client-server interaction lifetime. Make some clients slow and others fast in your mix. Performance testing (seeing how much you can accomplish in a second) is, I'd argue, a later phase (where you need a good model of actual use). First, go for correctness under "strange" interactions. --Scott David Daniels Scott.Daniels at Acm.Org From akineko at gmail.com Tue Apr 7 13:08:37 2009 From: akineko at gmail.com (akineko) Date: Tue, 7 Apr 2009 10:08:37 -0700 (PDT) Subject: named pipe and Linux Message-ID: <5b5157dd-ca70-4c6d-8adb-8a4d322fbd4a@x31g2000prc.googlegroups.com> Hello everyone, I'm trying to use named pipes to fuse a Python program and a C program. One side creates pipes using os.mkfifo() and both sides use the same named pipes (one side reads, another side writes). The read side uses select.select() to wait for incoming messages and read the message when select.select() says it is ready. The length of the message is unknown to the read side. I cannot use file.read() because it will block waiting for an EOF. I cannot use file.readline() because how many lines have arrived is unknown. So, I needed to use os.read() with the exact number of characters to read. Under Solaris environment, os.fstat() provides the exact size of the message that has arrived. Thus, two processes can communicate each other through the named pipes without blocking. However, the above scheme didn't work under Linux. Linux os.fstat() returns size=0 even the message is pending. (I think Linux buffers the message in memory while Solaris buffers the message in a file system) My question is, how can I make the named pipe scheme work under Linux? Is there any way to read the message without getting blocked? I know this is more Linux question than Python question but I believe many Python programmers are strong Linux programmers. Any suggestions will be appreciated. Best regards, Aki Niimura From Scott.Daniels at Acm.Org Tue Apr 7 13:09:10 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Tue, 07 Apr 2009 10:09:10 -0700 Subject: Adding method to class at run-time: bad style? In-Reply-To: References: Message-ID: Grant Edwards wrote: > I realize that technically all methods are added to classes at > "run-time", but what I'm talking about is this: > > import ClientForm > > def controlEqual(self,other): > return (self.type == other.type) and \ > (self.name == other.name) and \ > (self.value == other.value) and \ > (self.disabled == other.disabled) and\ > (self.readonly == self.readonly) Note: Here you are checking that self.readonly == self.readonly! I also dislike the trailing backslashes and over-parenthesizing. So, I'd do it like this (using the pare to avoid the backslash): def controlEqual(self,other): return (self.type == other.type and self.name == other.name and self.value == other.value and self.disabled == other.disabled and self.readonly == self.readonly) ... > ClientForm.Control.__eq__ = controlEqual > ClientForm.Control.__ne__ = controlNotEqual > > def formEqual(self,other): > if len(self.controls) != len(other.controls): > return False > for (c1,c2) in zip(self.controls,other.controls): > if c1 != c2: > return False > return True > > def formNotEqual(self,other): > return not (self==other) > > ClientForm.HTMLForm.__eq__ = formEqual > ClientForm.HTMLForm.__ne__ = formNotEqual > It works fine, but it seems like it might be dangerous (or just > bad form) to insert methods into existing classes like that. > Perhaps it would be safer to make sure that __eq__, __ne__, > and __cmp__ don't exist before adding my own? Well, a more standard approach would be: class FancyControl(ClientForm.Control): def __eq__(self, other): return (self.type == other.type and self.name == other.name and self.value == other.value and self.disabled == other.disabled and self.readonly == self.readonly) def __ne__(self, other): return not self.__eq__(other) class FancyHTMLForm(ClientForm.HTMLForm): def __eq__(self,other): ... def __ne__(self, other): return not self.__eq__(other) You'd have to make sure FancyControl and FancyClientForm get used in the app. The latter could be insured by monkey-patching: ... ClientForm.Control = FancyControl ClientForm.HTMLForm = FancyHTMLForm But substituting monkey-patching for class method insertion seems like you are going from one quick and dirty technique to a slightly nicer quick and dirty technique. --Scott David Daniels Scott.Daniels at Acm.Org From stef.mientki at gmail.com Tue Apr 7 13:28:12 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Tue, 07 Apr 2009 19:28:12 +0200 Subject: extracting plain text from RTF-file ? Message-ID: <49DB8D2C.1090005@gmail.com> hello, I'm looking for a library to extract plain text from RTF-files. I found these only RTF generation http://pyrtf.sourceforge.net/ should be able to parse, but no download files http://code.google.com/p/pyrtf-ng/ any suggestions ? thanks, Stef Mientki From aahz at pythoncraft.com Tue Apr 7 13:35:26 2009 From: aahz at pythoncraft.com (Aahz) Date: 7 Apr 2009 10:35:26 -0700 Subject: Call to PyEval_EvalCodeEx crashes in PyFrame_New with abort References: <72593fd7-4500-4ea9-b54e-1637d07b5972@s12g2000prc.googlegroups.com> Message-ID: [posted and e-mailed] In article <72593fd7-4500-4ea9-b54e-1637d07b5972 at s12g2000prc.googlegroups.com>, grbgooglefan wrote: > >I've emabedded Python(2.6) in my C++ application and using on Solaris >5.10. When the application calls Py_Eval, it causes an abort & >application core dumps. But this is at random times & no pattern for >this. Given the lack of response, I suggest asking on the capi-sig. The likeliest problem is some kind of refcount error when the embedded code calls back into your application. Do you ever get problems when you use pure Python code? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "...string iteration isn't about treating strings as sequences of strings, it's about treating strings as sequences of characters. The fact that characters are also strings is the reason we have problems, but characters are strings for other good reasons." --Aahz From brentb at beanfield.com Tue Apr 7 13:35:32 2009 From: brentb at beanfield.com (Brent Bloxam) Date: Tue, 07 Apr 2009 13:35:32 -0400 Subject: extracting plain text from RTF-file ? In-Reply-To: <49DB8D2C.1090005@gmail.com> References: <49DB8D2C.1090005@gmail.com> Message-ID: <49DB8EE4.8080000@beanfield.com> Stef Mientki wrote: > hello, > > I'm looking for a library to extract plain text from RTF-files. > I found these > > only RTF generation > http://pyrtf.sourceforge.net/ > > should be able to parse, but no download files > http://code.google.com/p/pyrtf-ng/ > > > any suggestions ? > thanks, > Stef Mientki > -- > http://mail.python.org/mailman/listinfo/python-list Checkout from SVN, there's a release tagged as 0.45, but the trunk is newer by ~4 months From aahz at pythoncraft.com Tue Apr 7 13:38:46 2009 From: aahz at pythoncraft.com (Aahz) Date: 7 Apr 2009 10:38:46 -0700 Subject: UnknownTimeZoneError References: <7c101864-308c-468a-9053-819af2f575c9@o6g2000yql.googlegroups.com> Message-ID: [posted & e-mailed] In article <7c101864-308c-468a-9053-819af2f575c9 at o6g2000yql.googlegroups.com>, Brian wrote: > >I'm running App Engine with Django. I'm having troubles executing >timezone conversion via pytz. What version of Python are you using? IIRC, GAE uses Python 2.3. Do other timezones work? I suspect you might get more responses from a GAE forum. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "...string iteration isn't about treating strings as sequences of strings, it's about treating strings as sequences of characters. The fact that characters are also strings is the reason we have problems, but characters are strings for other good reasons." --Aahz From aahz at pythoncraft.com Tue Apr 7 13:45:36 2009 From: aahz at pythoncraft.com (Aahz) Date: 7 Apr 2009 10:45:36 -0700 Subject: python for loop References: <0rc5t4l6lqethggahj6auupb5vj67j78gk@4ax.com> <5c92e9bd-1fb4-4c01-a928-04d7f673329a@e21g2000yqb.googlegroups.com> Message-ID: In article , Lou Pecora wrote: >In article ><5c92e9bd-1fb4-4c01-a928-04d7f673329a at e21g2000yqb.googlegroups.com>, > Aaron Brady wrote: >> >> Did I tell you guys that 'natural' has 38 definitions at >> dictionary.com? > >Amazing. I suggest you pick the one that fits best. "The wonderful thing about standards is that there are so many to choose from." -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "...string iteration isn't about treating strings as sequences of strings, it's about treating strings as sequences of characters. The fact that characters are also strings is the reason we have problems, but characters are strings for other good reasons." --Aahz From pje at telecommunity.com Tue Apr 7 13:46:21 2009 From: pje at telecommunity.com (P.J. Eby) Date: Tue, 07 Apr 2009 13:46:21 -0400 Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: <49DB6A1F.50801@egenix.com> References: <49D4DA72.60401@v.loewis.de> <49D52115.6020001@egenix.com> <49D66C6E.3090602@v.loewis.de> <49DB475B.8060504@egenix.com> <20090407140317.EBD383A4063@sparrow.telecommunity.com> <49DB6A1F.50801@egenix.com> Message-ID: <20090407174355.B62983A4063@sparrow.telecommunity.com> At 04:58 PM 4/7/2009 +0200, M.-A. Lemburg wrote: >On 2009-04-07 16:05, P.J. Eby wrote: > > At 02:30 PM 4/7/2009 +0200, M.-A. Lemburg wrote: > >> >> Wouldn't it be better to stick with a simpler approach and look for > >> >> "__pkg__.py" files to detect namespace packages using that O(1) > >> check ? > >> > > >> > Again - this wouldn't be O(1). More importantly, it breaks system > >> > packages, which now again have to deal with the conflicting file names > >> > if they want to install all portions into a single location. > >> > >> True, but since that means changing the package infrastructure, I think > >> it's fair to ask distributors who want to use that approach to also take > >> care of looking into the __pkg__.py files and merging them if > >> necessary. > >> > >> Most of the time the __pkg__.py files will be empty, so that's not > >> really much to ask for. > > > > This means your proposal actually doesn't add any benefit over the > > status quo, where you can have an __init__.py that does nothing but > > declare the package a namespace. We already have that now, and it > > doesn't need a new filename. Why would we expect OS vendors to start > > supporting it, just because we name it __pkg__.py instead of __init__.py? > >I lost you there. > >Since when do we support namespace packages in core Python without >the need to add some form of magic support code to __init__.py ? > >My suggestion basically builds on the same idea as Martin's PEP, >but uses a single __pkg__.py file as opposed to some non-Python >file yaddayadda.pkg. Right... which completely obliterates the primary benefit of the original proposal compared to the status quo. That is, that the PEP 382 way is more compatible with system packaging tools. Without that benefit, there's zero gain in your proposal over having __init__.py files just call pkgutil.extend_path() (in the stdlib since 2.3, btw) or pkg_resources.declare_namespace() (similar functionality, but with zipfile support and some other niceties). IOW, your proposal doesn't actually improve the status quo in any way that I am able to determine, except that it calls for loading all the __pkg__.py modules, rather than just the first one. (And the setuptools implementation of namespace packages actually *does* load multiple __init__.py's, so that's still no change over the status quo for setuptools-using packages.) From stefan_ml at behnel.de Tue Apr 7 13:50:46 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 07 Apr 2009 19:50:46 +0200 Subject: PyXML and Python-2.6 In-Reply-To: <49db5ccf$1@news.abaqus.com> References: <49db5ccf$1@news.abaqus.com> Message-ID: <49db9276$0$31875$9b4e6d93@newsspool3.arcor-online.net> Andrew MacKeith wrote: > The Python.org "SIG for XML Processing in Python" page indicates that > "The SIG, through the mailing list and the PyXML project hosted on > SourceForge...". > > The PyXML project on SourceForge " is no longer maintained. ", so > perhaps the SIG page could be updated. > > Is there a guide to porting projects that depend on PyXML to Python-2.6? Depending on how large your project is (and how well you encapsulated the XML code), a partial rewrite using ElementTree or lxml might be worth it. You'd be rewarded by simple and very fast code. Stefan From stevescripts at gmail.com Tue Apr 7 14:36:53 2009 From: stevescripts at gmail.com (Steve Offutt) Date: Tue, 7 Apr 2009 11:36:53 -0700 (PDT) Subject: Tkinter, tkFileDialog - how to select multiple files ? Message-ID: <6d3e4405-c44f-4d11-9210-68d92a1ba019@c9g2000yqm.googlegroups.com> Let me preface by saying that I don't do much Python, however, I am in my spare time, attempting to become somewhat comfortable with Python/ Tkinter. I just can't seem to understand what is the problem with this: ------ import Tkinter, tkFileDialog root = Tkinter.Tk() files = tkFileDialog.askopenfilenames(parent = root, filetypes = [('JPEG', '*.jpg'), ('ICON', '*.ico'), ('GIF', '*.gif')], title = "Select files...", multiple = 1 ) print files ----- Any help greatly appreciated! Steve From invalid at invalid Tue Apr 7 14:38:11 2009 From: invalid at invalid (Grant Edwards) Date: Tue, 07 Apr 2009 13:38:11 -0500 Subject: Adding method to class at run-time: bad style? References: Message-ID: On 2009-04-07, Scott David Daniels wrote: > Grant Edwards wrote: >> I realize that technically all methods are added to classes at >> "run-time", but what I'm talking about is this: >> >> import ClientForm >> >> def controlEqual(self,other): >> return (self.type == other.type) and \ >> (self.name == other.name) and \ >> (self.value == other.value) and \ >> (self.disabled == other.disabled) and\ >> (self.readonly == self.readonly) > Note: > Here you are checking that self.readonly == self.readonly! Doh! > I also dislike the trailing backslashes and over-parenthesizing. > So, I'd do it like this (using the pare to avoid the backslash): The paren to avoid the backslashes is a good tip. > def controlEqual(self,other): > return (self.type == other.type > and self.name == other.name > and self.value == other.value > and self.disabled == other.disabled > and self.readonly == self.readonly) I don't really like to rely on my memory of operator precidence, but that's a personal problem. > ... >> ClientForm.Control.__eq__ = controlEqual >> ClientForm.Control.__ne__ = controlNotEqual >> >> def formEqual(self,other): >> if len(self.controls) != len(other.controls): >> return False >> for (c1,c2) in zip(self.controls,other.controls): >> if c1 != c2: >> return False >> return True >> >> def formNotEqual(self,other): >> return not (self==other) >> >> ClientForm.HTMLForm.__eq__ = formEqual >> ClientForm.HTMLForm.__ne__ = formNotEqual > >> It works fine, but it seems like it might be dangerous (or just >> bad form) to insert methods into existing classes like that. >> Perhaps it would be safer to make sure that __eq__, __ne__, >> and __cmp__ don't exist before adding my own? > > Well, a more standard approach would be: > class FancyControl(ClientForm.Control): > def __eq__(self, other): > return (self.type == other.type > and self.name == other.name > and self.value == other.value > and self.disabled == other.disabled > and self.readonly == self.readonly) > > def __ne__(self, other): > return not self.__eq__(other) > > class FancyHTMLForm(ClientForm.HTMLForm): > def __eq__(self,other): > ... > > def __ne__(self, other): > return not self.__eq__(other) > > You'd have to make sure FancyControl and FancyClientForm get used in > the app. The problem is that my app never instanciates either the Control or the HTMLForm class: that's all done by module functions that the app calls, and they don't know about the new classes without some trickery (e.g. below) > The latter could be insured by monkey-patching: > ... > ClientForm.Control = FancyControl > ClientForm.HTMLForm = FancyHTMLForm > > But substituting monkey-patching :) How did I not know that phrase until today? > for class method insertion seems like you are going from one > quick and dirty technique to a slightly nicer quick and dirty > technique. I think like your monkey-patching solution better. It feels a bit cleaner. -- Grant Edwards grante Yow! ! Up ahead! It's a at DONUT HUT!! visi.com From kyosohma at gmail.com Tue Apr 7 14:40:19 2009 From: kyosohma at gmail.com (Mike Driscoll) Date: Tue, 7 Apr 2009 11:40:19 -0700 (PDT) Subject: Q: "Best" book for teaching References: <8e3d0032-5e9f-44c2-9380-1d2383552046@u5g2000vbc.googlegroups.com> Message-ID: <7ce404f3-22f3-46f9-a4fe-a61280219433@s19g2000vbp.googlegroups.com> On Apr 6, 9:37?am, grkunt... at gmail.com wrote: > I am considering teaching an "introduction to programming" course for > continuing education adults at a local ?community college. These would > people with no programming experience, but I will require a reasonable > facility with computers. > > What would be a good book to use as the text for the course? > > Thanks. If they ever release it, this book might be good: http://www.amazon.com/Hello-World-Computer-Programming-Beginners/dp/1933988495/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1239129498&sr=8-1 Zelle's Python Programming book is pretty good (and was written by a college professor) and I've heard good things about ORielly's Learning Python by Lutz. - Mike From invalid at invalid Tue Apr 7 14:48:58 2009 From: invalid at invalid (Grant Edwards) Date: Tue, 07 Apr 2009 13:48:58 -0500 Subject: Adding method to class at run-time: bad style? References: Message-ID: On 2009-04-07, Grant Edwards wrote: > On 2009-04-07, Scott David Daniels wrote: >> Grant Edwards wrote: >>> I realize that technically all methods are added to classes at >>> "run-time", but what I'm talking about is this: > ... >> >>> ClientForm.Control.__eq__ = controlEqual >>> ClientForm.Control.__ne__ = controlNotEqual > ... > >> Well, a more standard approach would be: >> class FancyControl(ClientForm.Control): >> def __eq__(self, other): >> return (self.type == other.type >> and self.name == other.name >> and self.value == other.value >> and self.disabled == other.disabled >> and self.readonly == self.readonly) >> >> def __ne__(self, other): >> return not self.__eq__(other) I like that, except it doesn't work. Objects instantiated from sub-classes of ClientForm.Control break. I get errors like this: Traceback (most recent call last): File "./testit.py", line 141, in f1 = getSocketForm(0) File "./testit.py", line 99, in getSocketForm return getForm("Socket",n) File "./testit.py", line 88, in getForm forms = ClientForm.ParseResponse(response,backwards_compat=False) File "/usr/lib/python2.5/site-packages/ClientForm.py", line 1057, in ParseResponse return _ParseFileEx(response, response.geturl(), *args, **kwds)[1:] File "/usr/lib/python2.5/site-packages/ClientForm.py", line 1128, in _ParseFileEx type, name, attrs, select_default=select_default, index=ii*10) File "/usr/lib/python2.5/site-packages/ClientForm.py", line 2843, in new_control control.add_to_form(self) File "/usr/lib/python2.5/site-packages/ClientForm.py", line 2016, in add_to_form Control.add_to_form(self, form) TypeError: unbound method add_to_form() must be called with FancyControl instance as first argument (got CheckboxControl instance instead) -- Grant Edwards grante Yow! I want a WESSON OIL at lease!! visi.com From andrew at acooke.org Tue Apr 7 14:51:06 2009 From: andrew at acooke.org (andrew cooke) Date: Tue, 7 Apr 2009 14:51:06 -0400 (CLT) Subject: Creating Linked Lists in Python In-Reply-To: References: Message-ID: <2870723299c0fea7e8fd2bff7aa86dd2.squirrel@localhost> J-Burns wrote: > How can I do this? And if I could do this by some other way than using > linked lists than do tell me about that as well. replying to this dead thread mainly for anyone using google. there's now a python regular expression engine in lepl and the source code can be seen via http://www.acooke.org/lepl/api/lepl.regexp-module.html in particular, i found that the critical collection was a map from interval to values which automatically fragments (i think discussion of this thread ended up looking at maps). so, for example, if (1,10) maps to 'foo' then adding (4,5) -> 'bar' gives: (1,3) -> foo (4,5) -> foo, bar (6,10) -> foo in simple terms that lets you construct transitions for ranges of values - typically a range of character with a mapped value that is the new state in the machine. so if you have a-d going to state 5, and then add d-f going to state 6, you want d (ie ('d','d')) to go to both 5 and 6 when constructing a nfa. that structure is here - http://www.acooke.org/lepl/api/lepl.regexp.interval-pysrc.html#TaggedFragments (minimal docs at http://www.acooke.org/lepl/api/lepl.regexp.interval.TaggedFragments-class.html) hope that makes sense. it could probably be more efficient (does an O(n) scan of all intervals each time a new interval is added so is O(n^2)), but since this is used in the "compile" part of a regexp, speed may not be as important as in the "match" phase. andrew From demirbaris at gmail.com Tue Apr 7 14:54:26 2009 From: demirbaris at gmail.com (Baris Demir) Date: Tue, 07 Apr 2009 20:54:26 +0200 Subject: 3D plotting in a GUI Message-ID: <49DBA162.3010907@gmail.com> Hi all, I need to develop a GUI for some scientific data processing operations and this GUI should work well with a 3D plotting module, also with NumPy and SciPy for sure. I made a search about packages but, there are plenty of these modules available. What kind of a package or a package of packages might be the best way for this work. BTW I do not want to use MayaVi. It is too much actually. Thanks in advance. BD From aahz at pythoncraft.com Tue Apr 7 14:57:46 2009 From: aahz at pythoncraft.com (Aahz) Date: 7 Apr 2009 11:57:46 -0700 Subject: Request For Comment Message-ID: How RFC1 got created: http://www.nytimes.com/2009/04/07/opinion/07crocker.html -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "...string iteration isn't about treating strings as sequences of strings, it's about treating strings as sequences of characters. The fact that characters are also strings is the reason we have problems, but characters are strings for other good reasons." --Aahz From mail at timgolden.me.uk Tue Apr 7 15:17:07 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Tue, 07 Apr 2009 20:17:07 +0100 Subject: Issue with subprocess Module In-Reply-To: <49DB60A3.4070707@ieee.org> References: <49DB3EC4.4040004@ieee.org> <49DB4557.90903@timgolden.me.uk> <49DB60A3.4070707@ieee.org> Message-ID: <49DBA6B3.9050904@timgolden.me.uk> > Tim Golden wrote: >> Sounds reasonable, but isn't actually true. This works fine: >> >> >> import subprocess >> >> open ("t.bat", "w").write ("echo hello") >> subprocess.Popen ("t.bat") >> >> >> >> TJG >> >>
Dave Angel wrote: > The docs of Popen() state that it uses CreateProcess() on Windows, so I > didn't even try it. I only knew because I'd answered a similar question recently and had simply tried it out! > Thanks for informing me it works. I see now the > COMSPEC manipulation in _execute_child(), but I'm still puzzled. When I > step through, it skips that part because we didn't specify shell= as an > argument. It still had not put the cmd.exe /c into the args variable > when it called CreateProcess(). So is the Python CreateProcess more > than a thin wrapper around Windows version? Doesn't look like it. And using win32process.CreateProcess from pywin32 does exactly the same. (Haven't checked their source but I doubt they're finicking around with the parameters): import win32process open ("t.bat", "w").write ("echo hello") win32process.CreateProcess ( None, "t.bat", None, None, 1, 0, None, None, win32process.STARTUPINFO () ) gives (, , 6208, 5732) >>> >>> c:\temp>echo hello hello >>> So it looks as though the MS docs are simply wrong? I haven't tried it with ctypes or a native C program, but I can't see that they should be any different. If I get a chance later I'll knock something up in C. > > But what about the OP problem? I now see it runs okay for me, both > stand-alone and inside Komodo. But he's getting an exception inside > make_inheritable(), which is apparently being passed an invalid handle. > > Runs OK for me, in Python 2.6 running under XP, SP3. 2.6.1 > (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)] Works under python 26/25/24 and the current subversion trunk, and on 3.1 once I'd modified the input to be a bytes string. TJG From rhamph at gmail.com Tue Apr 7 15:18:26 2009 From: rhamph at gmail.com (Adam Olsen) Date: Tue, 7 Apr 2009 12:18:26 -0700 (PDT) Subject: Returning different types based on input parameters References: Message-ID: On Apr 6, 3:02?pm, George Sakkis wrote: > For example, it is common for a function f(x) to expect x to be simply > iterable, without caring of its exact type. Is it ok though for f to > return a list for some types/values of x, a tuple for others and a > generator for everything else (assuming it's documented), or it should > always return the most general (iterator in this example) ? For list/tuple/iterable the correlation with the argument's type is purely superficial, *because* they're so compatible. Why should only tuples and lists get special behaviour? Why shouldn't every other argument type return a list as well? A counter example is python 3.0's str/bytes functions. They're mutually incompatible and there's no default. > To take it further, what if f wants to return different types, > differing even in a duck-type sense? That's easier to illustrate in a > API-extension scenario. Say that there is an existing function `solve > (x)` that returns `Result` instances. ?Later someone wants to extend f > by allowing an extra optional parameter `foo`, making the signature > `solve(x, foo=None)`. As long as the return value remains backward > compatible, everything's fine. However, what if in the extended case, > solve() has to return some *additional* information apart from > `Result`, say the confidence that the result is correct ? In short, > the extended API would be: > > ? ? def solve(x, foo=None): > ? ? ? ? ''' > ? ? ? ? @rtype: `Result` if foo is None; (`Result`, confidence) > otherwise. > ? ? ? ? ''' > > Strictly speaking, the extension is backwards compatible; previous > code that used `solve(x)` will still get back `Result`s. The problem > is that in new code you can't tell what `solve(x,y)` returns unless > you know something about `y`. My question is, is this totally > unacceptable and should better be replaced by a new function `solve2 > (x, foo=None)` that always returns (`Result`, confidence) tuples, or > it might be a justifiable cost ? Any other API extension approaches > that are applicable to such situations ? At a minimum it's highly undesirable. You lose a lot of readability/ maintainability. solve2/solve_ex is a little ugly, but that's less overall, so it's the better option. If your tuple gets to 3 or more I'd start wondering if you should return a single instance, with the return values as attributes. If Result is already such a thing I'd look even with a tuple of 2 to see if that's appropriate. From stevescripts at gmail.com Tue Apr 7 15:33:21 2009 From: stevescripts at gmail.com (Steve Offutt) Date: Tue, 7 Apr 2009 12:33:21 -0700 (PDT) Subject: Tkinter, tkFileDialog - how to select multiple files ? References: <6d3e4405-c44f-4d11-9210-68d92a1ba019@c9g2000yqm.googlegroups.com> Message-ID: <5cf9dbcc-c959-4ba5-987a-45f0e8721f72@b16g2000yqb.googlegroups.com> Hmm ... sorry folks - this works for me everywhere except on the machine I wrote it on ... Must be some sort of configuration problem... Steve From mail at timgolden.me.uk Tue Apr 7 15:35:24 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Tue, 07 Apr 2009 20:35:24 +0100 Subject: Issue with subprocess Module In-Reply-To: <49DBA6B3.9050904@timgolden.me.uk> References: <49DB3EC4.4040004@ieee.org> <49DB4557.90903@timgolden.me.uk> <49DB60A3.4070707@ieee.org> <49DBA6B3.9050904@timgolden.me.uk> Message-ID: <49DBAAFC.9040304@timgolden.me.uk> Tim Golden wrote: > So it looks as though the MS docs are simply wrong? I haven't > tried it with ctypes or a native C program, but I can't > see that they should be any different. If I get a chance > later I'll knock something up in C. OK, copying the standard MS example for creating a process: http://msdn.microsoft.com/en-us/library/ms682512(VS.85).aspx and changing only enough to substitute "c:/temp/t.bat" for the argv[1], it runs perfectly well. Doesn't answer the OP's question, but does suggest the docs are wrong. TJG From davea at ieee.org Tue Apr 7 15:41:16 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 07 Apr 2009 15:41:16 -0400 Subject: Python syntax In-Reply-To: <7dd228af-e549-444d-8623-11e95185123c@y9g2000yqg.googlegroups.com> References: <7dd228af-e549-444d-8623-11e95185123c@y9g2000yqg.googlegroups.com> Message-ID: <49DBAC5C.2050900@ieee.org> janus99 wrote: > well i'm pretty much a newb on python, it's my first comp lang. and > i've been having some diffuclties, i want to get the hang of it and > i'm wondering if u could help me understand the syntax structure. > > I messed around with my own comp (windos xp) command prompt and i > currently play a mud, but the python syntax is completly diffrent, i > was wondering if u could expalin how to syntax goes, i've tried the > "hello world" but it keep displaying syntax error, i've tried entering > random commands, e.g find,search, collect,... > > > dorzey's suggestion is good, as is the tutorial available with your install. Look for the file python-docs-pdf-letter.zip, and inside it tutorial.pdf If you didn't download that yet, you can get it from http://docs.python.org/download I prefer the pdf form, but your preferences may be different. Just try to get one that matches the version of the python you downloaded. The other thing that you need to learn how to do is to cut & paste in your command window. If you drag the mouse around the command window it should highlight some text. Then you right click to put that on the clipboard. Now you can go to some other program (like your mail program) and Ctrl-V to paste it. If that mode isn't enabled on your command windows, fix it, although you can manually use the system menu to accomplish the same thing (which I do when I'm using someone else's machine). When you ask for help here, please be specific. What did you try, and exactly what was the error. For all we know, you weren't even successful in getting the python.exe program to run at all. So it could be a path problem, not a language syntax problem. Here's a sample session. If you can't get this far, show us exactly what you did get. E:\>c:\ProgFiles\Python26\python.exe 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. >>> "hello world" 'hello world' >>> 3*12 36 >>> import sys >>> sys.exit(0) E:\> From alan.franzoni.xyz at gmail.com Tue Apr 7 15:52:53 2009 From: alan.franzoni.xyz at gmail.com (Alan Franzoni) Date: Tue, 07 Apr 2009 19:52:53 GMT Subject: PyCon Italy 2009: Early Bird Deadline Message-ID: The early-bird registration deadline for PyCon Tre (PyCon Italy 2009) is April 13th, just a few days from now. The conference will take place in Florence from May 8th till May 10th 2009, and features guests like Guido Van Rossum, Alex Martelli, Raymond Hettinger, Fredrik Lundh and Antonio Cangiano. Feel free to take a look at the schedule: http://www.pycon.it/pycon3/schedule/ A simultaneous interpretation service is available for the main track: http://www.pycon.it/pycon3/non-italians See you in Florence! -- Alan Franzoni - Remove .xyzz from my email in order to contact me. - GPG Key Fingerprint: 5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E From alan.franzoni.xyz at gmail.com Tue Apr 7 15:54:17 2009 From: alan.franzoni.xyz at gmail.com (Alan Franzoni) Date: Tue, 07 Apr 2009 19:54:17 GMT Subject: PyCon Italy 2009: Early Bird Deadline Message-ID: The early-bird registration deadline for PyCon Tre (PyCon Italy 2009) is April 13th, just a few days from now. The conference will take place in Florence from May 8th till May 10th 2009, and features guests like Guido Van Rossum, Alex Martelli, Raymond Hettinger, Fredrik Lundh and Antonio Cangiano. Feel free to take a look at the schedule: http://www.pycon.it/pycon3/schedule/ A simultaneous interpretation service is available for the main track: http://www.pycon.it/pycon3/non-italians See you in Florence! -- Alan Franzoni - Remove .xyzz from my email in order to contact me. - GPG Key Fingerprint: 5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E From leolozanne at yahoo.com Tue Apr 7 16:22:48 2009 From: leolozanne at yahoo.com (Leonardo lozanne) Date: Tue, 7 Apr 2009 13:22:48 -0700 (PDT) Subject: xml.dom.minidom getElementsByTagName white space issue Message-ID: <656946.72976.qm@web110406.mail.gq1.yahoo.com> Hi, ? I'm getting some XML tags with white spaces from a web service and when I try to get them with the getElements ByTagName I'm not able to do so. I'm getting an empty list. What I'm doing is: ? #XML_response is an xml string xml_msg = xml.dom.minidom.parseString(XML_response) ? nodes = xml_msg.getElementsByTagName("tag ten")? #tag name is "tag ten" with a whitespace ? It all works fine with tags like tag_seven but NOT for tag names with a white space. I've tried some escape chars but it doesnt seems to work. ? Does anybody has the escape char sequence I should be using or a work around for this? Thanks in advanced for your replies. -------------- next part -------------- An HTML attachment was scrubbed... URL: From garrickp at gmail.com Tue Apr 7 17:07:58 2009 From: garrickp at gmail.com (Falcolas) Date: Tue, 7 Apr 2009 14:07:58 -0700 (PDT) Subject: Request For Comment References: Message-ID: <9309874f-a3ea-4ba1-ac77-bf20f0d54064@r37g2000yqn.googlegroups.com> On Apr 7, 12:57?pm, a... at pythoncraft.com (Aahz) wrote: > How RFC1 got created: > > http://www.nytimes.com/2009/04/07/opinion/07crocker.html Thanks for bring that forward; I thought it was an interesting read and a nice polite (yet much needed) little jab at status-quo. ~G From stef.mientki at gmail.com Tue Apr 7 17:34:05 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Tue, 07 Apr 2009 23:34:05 +0200 Subject: extracting plain text from RTF-file ? In-Reply-To: <49DB8EE4.8080000@beanfield.com> References: <49DB8D2C.1090005@gmail.com> <49DB8EE4.8080000@beanfield.com> Message-ID: <49DBC6CD.8040405@gmail.com> Brent Bloxam wrote: > Stef Mientki wrote: >> hello, >> >> I'm looking for a library to extract plain text from RTF-files. >> I found these >> >> only RTF generation >> http://pyrtf.sourceforge.net/ >> >> should be able to parse, but no download files >> http://code.google.com/p/pyrtf-ng/ >> >> >> any suggestions ? >> thanks, >> Stef Mientki >> -- >> http://mail.python.org/mailman/listinfo/python-list > > Checkout from SVN, there's a release tagged as 0.45, but the trunk is > newer by ~4 months thanks Brent, I got some code and examples, but no example of parsing, only generating ? Anyone has an example perhaps ? thnaks, Stef From stef.mientki at gmail.com Tue Apr 7 17:37:24 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Tue, 07 Apr 2009 23:37:24 +0200 Subject: 3D plotting in a GUI In-Reply-To: <49DBA162.3010907@gmail.com> References: <49DBA162.3010907@gmail.com> Message-ID: <49DBC794.6070005@gmail.com> Baris Demir wrote: > Hi all, > > I need to develop a GUI for some scientific data processing operations > and this GUI should work well with a 3D plotting module, also with > NumPy and SciPy for sure. I made a search about packages but, there > are plenty of these modules available. What kind of a package or a > package of packages might be the best way for this work. BTW I do > not want to use MayaVi. It is too much actually. What do you mean by "too much actually" ? What's the GUI you had in mind ? What kind of scientific data processing do you've in mind ? cheers, Stef > > Thanks in advance. > BD > > -- > http://mail.python.org/mailman/listinfo/python-list From r-w at inet.net.au Tue Apr 7 17:49:08 2009 From: r-w at inet.net.au (r-w) Date: Wed, 08 Apr 2009 07:49:08 +1000 Subject: pseudo-code In-Reply-To: <49DB162E.2020309@inet.net.au> References: <49DB162E.2020309@inet.net.au> Message-ID: <49dbca5a$0$29865$5a62ac22@per-qv1-newsreader-01.iinet.net.au> r-w wrote: > If no internet connection: > if have files: > run anyway, with warning > else: > ERROR > else: > if error getting hash/files: > if have files: > run anyway, with warning > else: > ERROR > else: > run Sorry, guys. This should have gone to work. Ross From tjreedy at udel.edu Tue Apr 7 17:51:11 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 07 Apr 2009 17:51:11 -0400 Subject: with open('com1', 'r') as f: In-Reply-To: References: Message-ID: Lawrence D'Oliveiro wrote: > In message , Delaney, > Timothy (Tim) wrote: > >> Lawrence D'Oliveiro wrote: >> >>> In message , >>> Terry Reedy wrote: >>> >>>> Lawrence D'Oliveiro wrote: >>>> >>>>> All Python objects are reference-counted. >>>> Nope. Only in CPython, and even that could change. >>> Why should it? >> Because Guido has said it might some time in the future. > > It's still a matter of efficiency, not correctness. If one wants to *ensure* that the file is closed as soon as it is not needed (perhaps because it is one of 100s of files being processed), in all versions of Python current and future, then 'with ...' is an easy way to do it, while 'open ...' is incorrect. If one *does not care* about quick closure (perhaps because it is one of only a few files and they are all just read or the program will soon end (and cause closure anyway), then 'open ...' is both easy and correct. The latter has typically applied to my work, but I know that is not true for everyone. >>>>>> Once the file object becomes >>>>> inaccessible, it is automatically closed. Simple. >>>> Even in CPython, that would not be true now is the object became >>>> involved in or became a dependent of a reference cycle. >>> And how exactly would that happen with a file object? >> Create a cycle of objects, and have one of them reference a file object. > > Which was not what was happening in the actual situation at hand. For the simple toy example, sure. But in general, anything could happen after a file is opened within 'with' block or any other chunk of code. tjr From Scott.Daniels at Acm.Org Tue Apr 7 18:02:14 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Tue, 07 Apr 2009 15:02:14 -0700 Subject: Adding method to class at run-time: bad style? In-Reply-To: References: Message-ID: Grant Edwards wrote: > On 2009-04-07, Grant Edwards wrote: >> On 2009-04-07, Scott David Daniels wrote: >>> Grant Edwards wrote: >>>> I realize that technically all methods are added to classes at >>>> "run-time", but what I'm talking about is this: >> ... >>>> ClientForm.Control.__eq__ = controlEqual >>>> ClientForm.Control.__ne__ = controlNotEqual >> ... >> >>> Well, a more standard approach would be: >>> class FancyControl(ClientForm.Control): >>> def __eq__(self, other): >>> return (self.type == other.type >>> and self.name == other.name >>> and self.value == other.value >>> and self.disabled == other.disabled >>> and self.readonly == self.readonly) >>> >>> def __ne__(self, other): >>> return not self.__eq__(other) > > I like that, except it doesn't work. Objects instantiated from > sub-classes of ClientForm.Control break. I get errors like > this: > > Traceback (most recent call last): > File "./testit.py", line 141, in > f1 = getSocketForm(0) > File "./testit.py", line 99, in getSocketForm > return getForm("Socket",n) > File "./testit.py", line 88, in getForm > forms = ClientForm.ParseResponse(response,backwards_compat=False) > File "/usr/lib/python2.5/site-packages/ClientForm.py", line 1057, in ParseResponse > return _ParseFileEx(response, response.geturl(), *args, **kwds)[1:] > File "/usr/lib/python2.5/site-packages/ClientForm.py", line 1128, in _ParseFileEx > type, name, attrs, select_default=select_default, index=ii*10) > File "/usr/lib/python2.5/site-packages/ClientForm.py", line 2843, in new_control > control.add_to_form(self) > File "/usr/lib/python2.5/site-packages/ClientForm.py", line 2016, in add_to_form > Control.add_to_form(self, form) > TypeError: unbound method add_to_form() must be called with FancyControl instance as first argument (got CheckboxControl instance instead) > OK, that is one of the problems with monkey-patching. What happened is ClientForm apparently defines subclasses of Control such as CheckboxControl. The monkey-patching only happens after the ClientForm module has been executed (first import), and the monkey-patching happens after all of that. So now the "tack it into the class" method looks a bit better if you cannot simply add your requirement to the ClientForm source. --Scott David Daniels Scott.Daniels at Acm.Org From Scott.Daniels at Acm.Org Tue Apr 7 18:11:33 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Tue, 07 Apr 2009 15:11:33 -0700 Subject: 3D plotting in a GUI In-Reply-To: References: Message-ID: Baris Demir wrote: > Hi all, > > I need to develop a GUI for some scientific data processing operations > and this GUI should work well with a 3D plotting module, also with NumPy > and SciPy for sure. I made a search about packages but, there are plenty > of these modules available.... Look into VPython. Especially if MayaVi is too much. It has provisions for running with cross-eyed and wall-eyed 3-D as well as the cheapo colored paper glasses and the seriously not cheapo shuttered-glasses / special display card option. --Scott David Daniels Scott.Daniels at Acm.Org From tjreedy at udel.edu Tue Apr 7 18:22:01 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 07 Apr 2009 18:22:01 -0400 Subject: Expression In-Reply-To: References: Message-ID: Lydia wrote: > I am working on calculating one of the fields in a feature class based > on other 2 fields. The logic is, > A (the resulting field) is calculated from B, but C and D have part of > the value that could fill the blank of B, which meaning that combine > three fields of values can make A. > > Field A is what I need. > > The data looks like: . > > A B C D > 2 2 > 5 5 > 4 4 > 6 6 > > > cur = gp.UpdateCursor(data) > row = cur.Next() > gp.CalculateField_management(data, "A", "[B]", "VB", "") Those indents are wrong, and would cause a syntax error, so this must not be the code you actually ran. When posting, please copy and paste instead of retyping. Does the last line set A from B? Should it be inside the loop? > while row: > > cur.UpdateRow(row) Or does this magically set A from B? > > if not(row.GetValue("C") == 'NULL'): This should be the same as if row.GetValue("C") != 'NULL': which I find easier to read. From what you said before, you only want to set A from C or D if B is blank. If so, this section should be indented under if row.GetValue("B") == 'NULL': > row.SetValue("A",row.GetValue("C")); > > elif not(row.GetValue("D") == 'NULL'): > row.SetValue("A",row.GetValue("D")); > > row = cur.Next() > > del cur > del row Again, indent is erroneous. > But the out looks like only B was calculated to A successfully. C&D are > not in A. I have no idea why not. A main way to debug is to insert print statements into the code in appropriate places and see if what is printed matched what you expect. > I guess there must be something wrong with the code, but I am very new > to Python, and not familiar with the expression. Could anybody help ? > PS. I am coding Python with ARCGIS. I am not familiar with ARCGIS. tjr From cs at zip.com.au Tue Apr 7 18:26:28 2009 From: cs at zip.com.au (Cameron Simpson) Date: Wed, 8 Apr 2009 08:26:28 +1000 Subject: named pipe and Linux In-Reply-To: <5b5157dd-ca70-4c6d-8adb-8a4d322fbd4a@x31g2000prc.googlegroups.com> Message-ID: <20090407222628.GA29089@cskk.homeip.net> On 07Apr2009 10:08, akineko wrote: | I'm trying to use named pipes to fuse a Python program and a C | program. | One side creates pipes using os.mkfifo() and both sides use the same | named pipes (one side reads, another side writes). The read side uses | select.select() to wait for incoming messages and read the message | when select.select() says it is ready. | The length of the message is unknown to the read side. That's a serious flaw in the message protocol. | I cannot use file.read() because it will block waiting for an EOF. | I cannot use file.readline() because how many lines have arrived is | unknown. | So, I needed to use os.read() with the exact number of characters to | read. No! You should use os.read() with the maximum size of a message. It _should_ return with the number of bytes in the message, provided the C program writes messages with a single OS-level write() call. Forget all the fstat() stuff - it's racy. Personally, I'd use a thread to just do continuous blocking os.read()s of the pipe, and putting the resulting messages on a Queue for collection by your main program. If you're the only consumer of a Queue it's safe to poll it for emptiness or not, or to use a no-wait get(). All the above is untested, but absent a size in the protocol or other ways of parsing message boundaries in data stream, you can only rely on the C program writing messages with a single write() and collect using a large os.read(), which should return with what is there. Cheers, -- Cameron Simpson DoD#743 http://www.cskk.ezoshosting.com/cs/ Language... has created the word "loneliness" to express the pain of being alone. And it has created the word "solitude" to express the glory of being alone. - Paul Johannes Tillich From ldo at geek-central.gen.new_zealand Tue Apr 7 18:39:06 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Wed, 08 Apr 2009 10:39:06 +1200 Subject: Python syntax References: <7dd228af-e549-444d-8623-11e95185123c@y9g2000yqg.googlegroups.com> Message-ID: In message <7dd228af- e549-444d-8623-11e95185123c at y9g2000yqg.googlegroups.com>, janus99 wrote: > I messed around with my own comp (windos xp) command prompt ... Try a Linux command prompt. You'll learn a bit more that way. From invalid at invalid Tue Apr 7 18:44:23 2009 From: invalid at invalid (Grant Edwards) Date: Tue, 07 Apr 2009 17:44:23 -0500 Subject: Adding method to class at run-time: bad style? References: Message-ID: On 2009-04-07, Scott David Daniels wrote: >> File "/usr/lib/python2.5/site-packages/ClientForm.py", line 2016, in add_to_form >> Control.add_to_form(self, form) >> TypeError: unbound method add_to_form() must be called with FancyControl instance as first argument (got CheckboxControl instance instead) > > OK, that is one of the problems with monkey-patching. What happened > is ClientForm apparently defines subclasses of Control such as > CheckboxControl. Right. Control is just a base class, and changing it to a "different" class after sub-classes have been defined pulls the rug out from under things. Had I thought about it for a minute, I should have known that would happen. The working patch tweaks the class in-place, so it works OK. > The monkey-patching only happens after the ClientForm module > has been executed (first import), and the monkey-patching > happens after all of that. So now the "tack it into the > class" method looks a bit better if you cannot simply add your > requirement to the ClientForm source. That's obviously the "right thing", but it makes portability more of an issue (I would have to archive/distribute ClientForm source and users would have to install the customized version of ClientForm). Of course there's always the chance that my version of monkey-patching will stop working with a different version of ClientForm. We'll burn that bridge when we come to it. -- Grant Edwards grante Yow! BARBARA STANWYCK makes at me nervous!! visi.com From ldo at geek-central.gen.new_zealand Tue Apr 7 18:44:26 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Wed, 08 Apr 2009 10:44:26 +1200 Subject: named pipe and Linux References: <5b5157dd-ca70-4c6d-8adb-8a4d322fbd4a@x31g2000prc.googlegroups.com> Message-ID: In message <5b5157dd- ca70-4c6d-8adb-8a4d322fbd4a at x31g2000prc.googlegroups.com>, akineko wrote: > The length of the message is unknown to the read side. I think you want a message-based, not a stream-based, IPC mechanism. Look at the docs on msgctl, msgget, msgop and so on. From akineko at gmail.com Tue Apr 7 18:47:17 2009 From: akineko at gmail.com (akineko) Date: Tue, 7 Apr 2009 15:47:17 -0700 (PDT) Subject: named pipe and Linux References: Message-ID: Hello Cameron Simpson, Thank you for your reply. I now think the way I did (using fstat()) was a very bad idea (as you pointed out). I haven't decided how to fix this yet. I also considered attaching the message length to the head of the message. It will work, too. I need to a bit more experiment. Thank you for providing me directions to solve this problem. Best regards, Aki Niimura On Apr 7, 3:26 pm, Cameron Simpson wrote: > On 07Apr2009 10:08, akineko wrote: > | I'm trying to use named pipes to fuse a Python program and a C > | program. > | One side creates pipes using os.mkfifo() and both sides use the same > | named pipes (one side reads, another side writes). The read side uses > | select.select() to wait for incoming messages and read the message > | when select.select() says it is ready. > | The length of the message is unknown to the read side. > > That's a serious flaw in the message protocol. > > | I cannot use file.read() because it will block waiting for an EOF. > | I cannot use file.readline() because how many lines have arrived is > | unknown. > | So, I needed to use os.read() with the exact number of characters to > | read. > > No! > > You should use os.read() with the maximum size of a message. > It _should_ return with the number of bytes in the message, provided the > C program writes messages with a single OS-level write() call. > > Forget all the fstat() stuff - it's racy. > > Personally, I'd use a thread to just do continuous blocking os.read()s of > the pipe, and putting the resulting messages on a Queue for collection > by your main program. If you're the only consumer of a Queue it's safe > to poll it for emptiness or not, or to use a no-wait get(). > > All the above is untested, but absent a size in the protocol or other > ways of parsing message boundaries in data stream, you can only rely on > the C program writing messages with a single write() and collect using a > large os.read(), which should return with what is there. > > Cheers, > -- > Cameron Simpson DoD#743http://www.cskk.ezoshosting.com/cs/ > > Language... has created the word "loneliness" to express the pain of > being alone. And it has created the word "solitude" to express the glory > of being alone. - Paul Johannes Tillich From rfritz at u.washingon.edu Tue Apr 7 18:56:21 2009 From: rfritz at u.washingon.edu (R Fritz) Date: Tue, 07 Apr 2009 15:56:21 -0700 Subject: 3D plotting in a GUI References: Message-ID: I've been doing 3D modeling for lighting simulation and I found two reasonably well-supported solutions: VTK and OpenSceneGraph. VTK generally has a research slant and has what I believe are reasonably strong Python bindings. OSG is closer to VR/AR/Flight Simulation applications and the Python bindings are pre-alpha--you'll probably have to roll your own for some purposes. Both are OpenGL based; if you need an easy Windows install you'll have to go with something that supports DirectX. If your app is simple, you might just be able to write OpenGL code. Anyone who knows of other reasonably current libraries, please follow up--there are a lot of libraries that don't seem to see much activity, and I'm not sure if this is because they are complete or because they are not much used. From Scott.Daniels at Acm.Org Tue Apr 7 19:09:20 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Tue, 07 Apr 2009 16:09:20 -0700 Subject: Adding method to class at run-time: bad style? In-Reply-To: References: Message-ID: Grant Edwards wrote: > On 2009-04-07, Scott David Daniels wrote: > >>> File "/usr/lib/python2.5/site-packages/ClientForm.py", line 2016, in add_to_form >>> Control.add_to_form(self, form) >>> TypeError: unbound method add_to_form() must be called with FancyControl instance as first argument (got CheckboxControl instance instead) >> OK, that is one of the problems with monkey-patching. What happened >> is ClientForm apparently defines subclasses of Control such as >> CheckboxControl. > > Right. Control is just a base class, and changing it to a > "different" class after sub-classes have been defined pulls the > rug out from under things. Had I thought about it for a > minute, I should have known that would happen. The working > patch tweaks the class in-place, so it works OK. > >> The monkey-patching only happens after the ClientForm module >> has been executed (first import), and the monkey-patching >> happens after all of that. So now the "tack it into the >> class" method looks a bit better if you cannot simply add your >> requirement to the ClientForm source. > > That's obviously the "right thing", but it makes portability > more of an issue (I would have to archive/distribute ClientForm > source and users would have to install the customized version > of ClientForm). > > Of course there's always the chance that my version of > monkey-patching will stop working with a different version of > ClientForm. We'll burn that bridge when we come to it. > What you might use as a half-way measure: class Mixin: # or class Mixin(object) if new-style: def __eq__(self, other): return (self.type == other.type and self.name == other.name and self.value == other.value and self.disabled == other.disabled and self.readonly == self.readonly) def __ne__(self, other): return not self.__eq__(other) ...(including FancyHTMLForm) ... class FancyControl(MixIn, ClientForm.Control): pass class FancyCheckboxControl(MixIn, ClientForm.CheckboxControl): pass ... ClientForm.HTMLForm = FancyHTMLForm ClientForm.Control = FancyControl ClientForm.CheckboxControl = FancyCheckboxControl --Scott David Daniels Scott.Daniels at Acm.Org From gallium.arsenide at gmail.com Tue Apr 7 19:34:55 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Tue, 7 Apr 2009 16:34:55 -0700 (PDT) Subject: Q: "Best" book for teaching References: <8e3d0032-5e9f-44c2-9380-1d2383552046@u5g2000vbc.googlegroups.com> Message-ID: <57e72bda-450b-4bf0-b204-7049ddac1dc2@z14g2000yqa.googlegroups.com> On Apr 6, 10:37?am, grkunt... at gmail.com wrote: > I am considering teaching an "introduction to programming" > course for continuing education adults at a local?community > college. These would people with no programming experience, > but I will require a reasonable facility with computers. > > What would be a good book to use as the text for the course? For an extremely gentle introduction, perhaps take a look at _Python Programming for the Absolute Beginner_ by Michael Dawson: http://www.amazon.com/Python-Programming-Absolute-Beginner-Michael/dp/1592000738 A coworker of mine recently bought this book, which is how I found out about it. Besides assuming no programming experience, it tries to stay interesting through the use of simple games for its examples (eventually building up to the use of the popular pygame library). The writing style is definitely more "fun" than "academic". John From grante at visi.com Tue Apr 7 19:45:15 2009 From: grante at visi.com (Grant Edwards) Date: Tue, 07 Apr 2009 18:45:15 -0500 Subject: Adding method to class at run-time: bad style? References: Message-ID: On 2009-04-07, Scott David Daniels wrote: > Grant Edwards wrote: >> On 2009-04-07, Scott David Daniels wrote: >> >>>> File "/usr/lib/python2.5/site-packages/ClientForm.py", line 2016, in add_to_form >>>> Control.add_to_form(self, form) >>>> TypeError: unbound method add_to_form() must be called with FancyControl instance as first argument (got CheckboxControl instance instead) >>> OK, that is one of the problems with monkey-patching. What happened >>> is ClientForm apparently defines subclasses of Control such as >>> CheckboxControl. >> >> Right. Control is just a base class, and changing it to a >> "different" class after sub-classes have been defined pulls the >> rug out from under things. Had I thought about it for a >> minute, I should have known that would happen. The working >> patch tweaks the class in-place, so it works OK. >> >>> The monkey-patching only happens after the ClientForm module >>> has been executed (first import), and the monkey-patching >>> happens after all of that. So now the "tack it into the >>> class" method looks a bit better if you cannot simply add your >>> requirement to the ClientForm source. >> >> That's obviously the "right thing", but it makes portability >> more of an issue (I would have to archive/distribute ClientForm >> source and users would have to install the customized version >> of ClientForm). >> >> Of course there's always the chance that my version of >> monkey-patching will stop working with a different version of >> ClientForm. We'll burn that bridge when we come to it. >> > > What you might use as a half-way measure: > > class Mixin: # or class Mixin(object) if new-style: > def __eq__(self, other): > return (self.type == other.type > and self.name == other.name > and self.value == other.value > and self.disabled == other.disabled > and self.readonly == self.readonly) > > def __ne__(self, other): > return not self.__eq__(other) > ...(including FancyHTMLForm) ... > class FancyControl(MixIn, ClientForm.Control): pass > class FancyCheckboxControl(MixIn, ClientForm.CheckboxControl): pass > ... > ClientForm.HTMLForm = FancyHTMLForm > ClientForm.Control = FancyControl > ClientForm.CheckboxControl = FancyCheckboxControl That would work -- but there are probably 8 or 10 different Control subclasses. It's a bit tedious mixing them all one at a time, and you need more "inside" information (the names of all the different subclasses). -- Grant From ricaraoz at gmail.com Tue Apr 7 20:28:28 2009 From: ricaraoz at gmail.com (=?ISO-8859-1?Q?Ricardo_Ar=E1oz?=) Date: Tue, 07 Apr 2009 21:28:28 -0300 Subject: object knows which object called it? In-Reply-To: <49DA3F8C.7060407@bluewin.ch> References: <662de4f7-d8a3-4ed8-bb02-5571e33a3304@k8g2000yqn.googlegroups.com> <49DA3F8C.7060407@bluewin.ch> Message-ID: <49DBEFAC.2070002@gmail.com> Reckoner wrote: >> hi, >> >> I have the following problem: I have two objects, say, A and B, which >> are both legitimate stand-alone objects with lives of their own. >> >> A contains B as a property, so I often do >> >> A.B.foo() >> >> the problem is that some functions inside of B actually need A >> (remember I said they were both standalone objects), so I have to >> often do: >> >> A.B.foo_func(A) >> >> Which is kind of awkward. >> >> Is there some way that B.foo_func() could somehow know that it was >> called as a property of A in this way? >> >> Note that I'm looking for the calling object and NOT the calling >> function. >> >> Thanks in advance. >> -- >> http://mail.python.org/mailman/listinfo/python-list >> >> Maybe this would work for you? >>> class B(object) : ... def __init__(self, owner=None) : ... self.owner = owner ... def someMethod(self) : ... print self.owner.name ... >>> class A(object) : ... def __init__(self, name='class A') : ... self.B = B(owner=self) ... self.name = name ... >>> instA1 = A('one') >>> instA2 = A('two') >>> instA1.B.someMethod() one >>> instA2.B.someMethod() two >>> From robert.oschler at gmail.com Tue Apr 7 20:49:02 2009 From: robert.oschler at gmail.com (roschler) Date: Tue, 7 Apr 2009 17:49:02 -0700 (PDT) Subject: Lock down Internet Access from Python script? Message-ID: <97995159-9dfd-44e5-8c00-441c0699a9bd@f19g2000yqo.googlegroups.com> I know it's not possible to "sandbox" the Python Interpreter like you can the Java VM, but I'm wondering if there is a way to at least lock down internet access? Trying to do it at the O/S service level on Windows like ZoneAlarm does isn't feasible for me, but I thought there might be a way to do it at the Python Interpreter level in a way that would be hard to get around through clever code forming. My application will be running several out of process Python Interpreters in a server pool. My desire is to at least keep authors from creating a situation where they are downloading Python scripts from a web server and running them, unless the owner of that station trusts a particular author enough to grant that kind of access. Is there any way to do this that's more bulletproof than trying to restrict access to known Socket based libraries? In other words, something that could stop things at the Socket level rather than trying to shepherd the situation by preventing the use of certain modules? Thanks, Robert From rhodri at wildebst.demon.co.uk Tue Apr 7 20:57:35 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Wed, 08 Apr 2009 01:57:35 +0100 Subject: extract Infobox contents In-Reply-To: <1239104778.10734.2.camel@mctell> References: <1239104778.10734.2.camel@mctell> Message-ID: On Tue, 07 Apr 2009 12:46:18 +0100, J. Clifford Dyer wrote: > On Mon, 2009-04-06 at 23:41 +0100, Rhodri James wrote: >> On Mon, 06 Apr 2009 23:12:14 +0100, Anish Chapagain >> wrote: >> >> > Hi, >> > I was trying to extract wikipedia Infobox contents which is in format >> > like given below, from the opened URL page in Python. >> > >> > {{ Infobox Software >> > | name = Bash [snip] >> > | latest release date = {{release date|mf=yes|2009|02|20}} >> > | programming language = [[C (programming language)|C]] >> > | operating system = [[Cross-platform]] >> > | platform = [[GNU]] >> > | language = English, multilingual ([[gettext]]) >> > | status = Active [snip some more] >> > }} //upto this line >> > >> > I need to extract all data between {{ Infobox ...to }} [snip still more] >> You end up with 'infoboxes' containing a list of all the infoboxes >> on the page, each held as a list of the lines of their content. >> For safety's sake you really should be using regular expressions >> rather than 'startswith', but I leave that as an exercise for the >> reader :-) >> > > I agree that startswith isn't the right option, but for matching two > constant characters, I don't think re is necessary. I'd just do: > > if '}}' in line: > pass > > Then, as the saying goes, you only have one problem. That would be the problem of matching lines like: | latest release date = {{release date|mf=yes|2009|02|20}} would it? :-) A quick bit of timing suggests that: if line.lstrip().startswith("}}"): pass is what we actually want. -- Rhodri James *-* Wildebeeste Herder to the Masses From e.yunak at gmail.com Tue Apr 7 21:10:46 2009 From: e.yunak at gmail.com (Eugene Yunak) Date: Tue, 7 Apr 2009 18:10:46 -0700 (PDT) Subject: cross platform ping module Message-ID: Hi all! I'm interested in developing a cross platform module that will allow a simple 'ping' functionality, i mean sending icmp type 8 code 0 and receiving appropriate answers. This sound like a simple work, but i ask for your help with architectural decision. I can implement icmp communications using raw sockets (possibly, as an extension coded in C), or i can use subproccess module, ask system ping executable to do the work, and just parse it's output. There are problems with both solutions. First one will require root (or equivalent) privileges for the interpreter to work. This is unavoidable, we can just make the program suid, but sometimes (as to my mind, always) it is not an option. Security matters, i can't allow each and every untested application to run with root privileges. Besides, this will require the user to actually have root access to make program suid. I'm unsure about the way something similar to suid can be done on windows, too. Second one just plain looks ugly to me. I don't like the approach at all. But, this would avoid all security troubles with suid, because if the user can run ping, we can too. And this also mean sysadmin actually allowed the user to run ping. On the other hand, this will limit us to icmp types that concrete system's ping executable can work with. This means that on some systems we can use only type 8/ type 0 (i would like to use few others too). If i just needed a tool for specific environment, i would have easily chosen. But i'm going to create a module that everyone and everywhere could use. >From this point of view, calling system ping looks better. Can you please help me choose the correct way to handle this problem? Maybe, there are some other possibilities, witch i haven't taken into account? Is someone here interested in this functionality, anyway? Sorry for my poor English. From afriere at yahoo.co.uk Tue Apr 7 21:16:07 2009 From: afriere at yahoo.co.uk (afriere at yahoo.co.uk) Date: Tue, 7 Apr 2009 18:16:07 -0700 (PDT) Subject: object knows which object called it? References: <662de4f7-d8a3-4ed8-bb02-5571e33a3304@k8g2000yqn.googlegroups.com> Message-ID: <20ffa088-e402-47fb-b953-ef8a4a652bbb@l1g2000yqk.googlegroups.com> On Apr 7, 2:53?am, "R. David Murray" wrote: > I think the OO way to do this is to provide a method on A that does the > right thing: > > ? ? def Bfoo_func(self): > ? ? ? ? self.B.foo_func(self) > > Or maybe you could look at generic methods, which provide a way > to do multiple dispatch. +1 Which dispatch would itself be using methods to pass 'self' to the receiving object. Yes this would seem to be the obvious way to establish two-way communication between objects. I would be so bold as to suggest that if the design the OP has adopted is not amenable to this straightfoward approach, the design needs to be reassessed. From ecarlson at eng.ua.edu Tue Apr 7 21:51:20 2009 From: ecarlson at eng.ua.edu (Eric Carlson) Date: Tue, 07 Apr 2009 20:51:20 -0500 Subject: 3D plotting in a GUI In-Reply-To: References: Message-ID: Baris Demir wrote: > Hi all, > > I need to develop a GUI for some scientific data processing operations > and this GUI should work well with a 3D plotting module, also with NumPy > and SciPy for sure. I made a search about packages but, there are plenty > of these modules available. What kind of a package or a package of > packages might be the best way for this work. BTW I do not want to use > MayaVi. It is too much actually. > > Thanks in advance. > BD > The attached example should give you some idea of embedding mayavi (and then using the very high-level mlab) and vtk in wxpython apps. This example requires wxpython, vtk, numpy, and ETS. If you are really after visualization of data, you will need to spend years to get even a fraction of the capability of mayavi. On the other hand, rolling out a py2exe'd version of a program is currently a major challenge if you need ETS. The mayavi.mlab module is so easy to use and embed that I would recommend using it to create a rapid software prototype to compare against whatever system you decide to go with. It is designed with a seamless interface to numpy/scipy, and provides high-level routines to modify low-level properties. Cheers, Eric -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: wx_mayavi_embed_in_notebook.py URL: From ldo at geek-central.gen.new_zealand Tue Apr 7 22:39:29 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Wed, 08 Apr 2009 14:39:29 +1200 Subject: Lock down Internet Access from Python script? References: <97995159-9dfd-44e5-8c00-441c0699a9bd@f19g2000yqo.googlegroups.com> Message-ID: In message <97995159-9dfd-44e5-8c00-441c0699a9bd at f19g2000yqo.googlegroups.com>, roschler wrote: > I know it's not possible to "sandbox" the Python Interpreter ... The Google App Engine does exactly that. How could you do it? My guess is, use some kind of virtualization (if you have the hardware) or paravirtualization (if you don't). From aahz at pythoncraft.com Tue Apr 7 23:22:16 2009 From: aahz at pythoncraft.com (Aahz) Date: 7 Apr 2009 20:22:16 -0700 Subject: is there a way to collect twitts with python? References: <1078018b0903271054y3d4a7162w885c61bf63bc2048@mail.gmail.com> <49D67874.8080209@gmail.com> <1238820981.1029.76.camel@tim-laptop> Message-ID: In article , Nick Stinemates wrote: > >You mean you don't want to read every detail of someone's life? Damn.. Someone has probably done this already, but I've been tempted to set up a Twitter feed that lists every time I visit the loo. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "...string iteration isn't about treating strings as sequences of strings, it's about treating strings as sequences of characters. The fact that characters are also strings is the reason we have problems, but characters are strings for other good reasons." --Aahz From barry at python.org Tue Apr 7 23:28:15 2009 From: barry at python.org (Barry Warsaw) Date: Tue, 7 Apr 2009 23:28:15 -0400 Subject: RELEASED Python 2.6.2 candidate 1 Message-ID: <67987D03-6D96-4601-A0C5-08B987A81F3B@python.org> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I'm happy to announce the release of Python 2.6.2 candidate 1. This release contains dozens of bug fixes since Python 2.6.1. Please see the NEWS file for a detailed list of changes. Barring unforeseen problems, Python 2.6.2 final will be released within a few days. http://www.python.org/download/releases/2.6.2/NEWS.txt For more information on Python 2.6 please see http://docs.python.org/dev/whatsnew/2.6.html Source tarballs and Windows installers for this release candidate can be downloaded from the Python 2.6.2 page: http://www.python.org/download/releases/2.6.2/ Bugs can be reported in the Python bug tracker: http://bugs.python.org Enjoy, Barry Barry Warsaw barry at python.org Python 2.6/3.0 Release Manager (on behalf of the entire python-dev team) -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (Darwin) iQCVAwUBSdwZ0HEjvBPtnXfVAQJTsAP+Krt1F6qGjuk9a7q8HwF2oAWr/peIAfDf 7HGjOpieoyyAKO1ZNqWvxZ1Ftx+I0YHjfk5OKz/1FN9H3eteFU/L5EEbJD1iTSmK LAOycWWtWJp+OPatqveHZbGr4ap4XON05yMrzlewnnIH0iGnYjMAgxKkwVKA7MwN BiXDeBPba1A= =HdKG -----END PGP SIGNATURE----- From steve at holdenweb.com Tue Apr 7 23:32:51 2009 From: steve at holdenweb.com (Steve Holden) Date: Tue, 07 Apr 2009 23:32:51 -0400 Subject: Python syntax In-Reply-To: References: <7dd228af-e549-444d-8623-11e95185123c@y9g2000yqg.googlegroups.com> Message-ID: Lawrence D'Oliveiro wrote: > In message <7dd228af- > e549-444d-8623-11e95185123c at y9g2000yqg.googlegroups.com>, janus99 wrote: > >> I messed around with my own comp (windos xp) command prompt ... > > Try a Linux command prompt. You'll learn a bit more that way. > Great advice when Linux is available, but somewhat fatuous for a Windows user. regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ Watch PyCon on video now! http://pycon.blip.tv/ From sxn02 at yahoo.com Tue Apr 7 23:42:18 2009 From: sxn02 at yahoo.com (Sorin Schwimmer) Date: Tue, 7 Apr 2009 20:42:18 -0700 (PDT) Subject: Some test fail on my new Python 2.6 Message-ID: <777745.16818.qm@web56006.mail.re3.yahoo.com> I wanted to replace my old Python 2.4 and tcl/tk 8.4 with tcl/tk 8.5.6 and Python 2.6, mainly so that I can enjoy ttk. tcl/tk was installed from sources without any problem, I started a wish and worked. Now, for the Python, here are all the issues signaled by make test: running build running build_ext building 'dbm' extension gcc -pthread -fPIC -fno-strict-aliasing -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -DHAVE_GDBM_NDBM_H -I. -I/install/Python-2.6.1/./Include -I. -IInclude -I./Include -I/usr/local/include -I/install/Python-2.6.1/Include -I/install/Python-2.6.1 -c /install/Python-2.6.1/Modules/dbmmodule.c -o build/temp.linux-i686-2.6/install/Python-2.6.1/Modules/dbmmodule.o gcc -pthread -shared build/temp.linux-i686-2.6/install/Python-2.6.1/Modules/dbmmodule.o -L/usr/local/lib -lgdbm -o build/lib.linux-i686-2.6/dbm.so *** WARNING: renaming "dbm" since importing it failed: build/lib.linux-i686-2.6/dbm.so: undefined symbol: dbm_firstkey building '_tkinter' extension gcc -pthread -fPIC -fno-strict-aliasing -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -DWITH_APPINIT=1 -DWITH_BLT=1 -I/usr/X11R6/include -I. -I/install/Python-2.6.1/./Include -I. -IInclude -I./Include -I/usr/local/include -I/install/Python-2.6.1/Include -I/install/Python-2.6.1 -c /install/Python-2.6.1/Modules/_tkinter.c -o build/temp.linux-i686-2.6/install/Python-2.6.1/Modules/_tkinter.o gcc -pthread -fPIC -fno-strict-aliasing -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -DWITH_APPINIT=1 -DWITH_BLT=1 -I/usr/X11R6/include -I. -I/install/Python-2.6.1/./Include -I. -IInclude -I./Include -I/usr/local/include -I/install/Python-2.6.1/Include -I/install/Python-2.6.1 -c /install/Python-2.6.1/Modules/tkappinit.c -o build/temp.linux-i686-2.6/install/Python-2.6.1/Modules/tkappinit.o gcc -pthread -shared build/temp.linux-i686-2.6/install/Python-2.6.1/Modules/_tkinter.o build/temp.linux-i686-2.6/install/Python-2.6.1/Modules/tkappinit.o -L/usr/X11R6/lib64 -L/usr/X11R6/lib -L/usr/local/lib -lBLT -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 dbm running build_scripts find ./Lib -name '*.py[co]' -print | xargs rm -f ./python -E -tt ./Lib/test/regrtest.py -l test_aepack test_aepack skipped -- No module named aepack test_al test_al skipped -- No module named al test_applesingle test_applesingle skipped -- No module named macostools test_bsddb185 test_bsddb185 skipped -- No module named bsddb185 test_bsddb3 test_bsddb3 skipped -- Use of the `bsddb' resource not enabled test_cd test_cd skipped -- No module named cd test_cl test_cl skipped -- No module named cl test_codecmaps_cn test_codecmaps_cn skipped -- Use of the `urlfetch' resource not enabled test_codecmaps_hk test_codecmaps_hk skipped -- Use of the `urlfetch' resource not enabled test_codecmaps_jp test_codecmaps_jp skipped -- Use of the `urlfetch' resource not enabled test_codecmaps_kr test_codecmaps_kr skipped -- Use of the `urlfetch' resource not enabled test_codecmaps_tw test_codecmaps_tw skipped -- Use of the `urlfetch' resource not enabled test_curses test_curses skipped -- Use of the `curses' resource not enabled test_dbm test_dbm skipped -- No module named dbm test_gl test_gl skipped -- No module named gl test_httpservers test test_httpservers failed -- errors occurred; run in verbose mode for details test_imgfile test_imgfile skipped -- No module named imgfile test_kqueue test_kqueue skipped -- test works only on BSD test_linuxaudiodev test_linuxaudiodev skipped -- Use of the `audio' resource not enabled test_macos test_macos skipped -- No module named MacOS test_macostools test_macostools skipped -- No module named macostools test_multiprocessing test_multiprocessing skipped -- OSError raises on RLock creation, see issue 3111! test_normalization test_normalization skipped -- Use of the `urlfetch' resource not enabled test_ossaudiodev test_ossaudiodev skipped -- Use of the `audio' resource not enabled test_pep277 test_pep277 skipped -- test works only on NT+ test_py3kwarn test_py3kwarn skipped -- test.test_py3kwarn must be run with the -3 flag test_scriptpackages test_scriptpackages skipped -- No module named aetools test_socket test test_socket failed -- Traceback (most recent call last): File "/install/Python-2.6.1/Lib/test/test_socket.py", line 474, in testSockName my_ip_addr = socket.gethostbyname(socket.gethostname()) gaierror: [Errno -2] Name or service not known test_socketserver test_socketserver skipped -- Use of the `network' resource not enabled test_sqlite test_sqlite skipped -- no sqlite available test_startfile test_startfile skipped -- cannot import name startfile test_sunaudiodev test_sunaudiodev skipped -- No module named sunaudiodev test_sys test test_sys failed -- Traceback (most recent call last): File "/install/Python-2.6.1/Lib/test/test_sys.py", line 354, in test_43581 self.assert_(sys.__stdout__.encoding == sys.__stderr__.encoding) AssertionError test_tcl test_tcl skipped -- No module named _tkinter test_timeout test_timeout skipped -- Use of the `network' resource not enabled test_unicode_file test_unicode_file skipped -- No Unicode filesystem semantics on this platform. test_urllib2net test_urllib2net skipped -- Use of the `network' resource not enabled test_urllibnet test_urllibnet skipped -- Use of the `network' resource not enabled test_uuid WARNING: uuid.getnode is unreliable on many platforms. It is disabled until the code and/or test can be fixed properly. WARNING: uuid._ifconfig_getnode is unreliable on many platforms. It is disabled until the code and/or test can be fixed properly. WARNING: uuid._unixdll_getnode is unreliable on many platforms. It is disabled until the code and/or test can be fixed properly. test_winreg test_winreg skipped -- No module named _winreg test_winsound test_winsound skipped -- No module named winsound test_zipfile64 test_zipfile64 skipped -- test requires loads of disk-space bytes and a long time to run 320 tests OK. 3 tests failed: test_httpservers test_socket test_sys 38 tests skipped: test_aepack test_al test_applesingle test_bsddb185 test_bsddb3 test_cd test_cl test_codecmaps_cn test_codecmaps_hk test_codecmaps_jp test_codecmaps_kr test_codecmaps_tw test_curses test_dbm test_gl test_imgfile test_kqueue test_linuxaudiodev test_macos test_macostools test_multiprocessing test_normalization test_ossaudiodev test_pep277 test_py3kwarn test_scriptpackages test_socketserver test_sqlite test_startfile test_sunaudiodev test_tcl test_timeout test_unicode_file test_urllib2net test_urllibnet test_winreg test_winsound test_zipfile64 3 skips unexpected on linux2: test_tcl test_dbm test_multiprocessing ./python -E -tt ./Lib/test/regrtest.py -l test_aepack test_aepack skipped -- No module named aepack test_al test_al skipped -- No module named al test_applesingle test_applesingle skipped -- No module named macostools test_bsddb185 test_bsddb185 skipped -- No module named bsddb185 test_bsddb3 test_bsddb3 skipped -- Use of the `bsddb' resource not enabled test_cd test_cd skipped -- No module named cd test_cl test_cl skipped -- No module named cl test_codecmaps_cn test_codecmaps_cn skipped -- Use of the `urlfetch' resource not enabled test_codecmaps_hk test_codecmaps_hk skipped -- Use of the `urlfetch' resource not enabled test_codecmaps_jp test_codecmaps_jp skipped -- Use of the `urlfetch' resource not enabled test_codecmaps_kr test_codecmaps_kr skipped -- Use of the `urlfetch' resource not enabled test_codecmaps_tw test_codecmaps_tw skipped -- Use of the `urlfetch' resource not enabled test_curses test_curses skipped -- Use of the `curses' resource not enabled test_dbm test_dbm skipped -- No module named dbm test_gl test_gl skipped -- No module named gl test_httpservers test test_httpservers failed -- errors occurred; run in verbose mode for details test_imgfile test_imgfile skipped -- No module named imgfile test_kqueue test_kqueue skipped -- test works only on BSD test_linuxaudiodev test_linuxaudiodev skipped -- Use of the `audio' resource not enabled test_macos test_macos skipped -- No module named MacOS test_macostools test_macostools skipped -- No module named macostools test_multiprocessing test_multiprocessing skipped -- OSError raises on RLock creation, see issue 3111! test_normalization test_normalization skipped -- Use of the `urlfetch' resource not enabled test_ossaudiodev test_ossaudiodev skipped -- Use of the `audio' resource not enabled test_pep277 test_pep277 skipped -- test works only on NT+ test_py3kwarn test_py3kwarn skipped -- test.test_py3kwarn must be run with the -3 flag test_scriptpackages test_scriptpackages skipped -- No module named aetools test_socket test test_socket failed -- Traceback (most recent call last): File "/install/Python-2.6.1/Lib/test/test_socket.py", line 474, in testSockName my_ip_addr = socket.gethostbyname(socket.gethostname()) gaierror: [Errno -2] Name or service not known test_socketserver test_socketserver skipped -- Use of the `network' resource not enabled test_sqlite test_sqlite skipped -- no sqlite available test_startfile test_startfile skipped -- cannot import name startfile test_sunaudiodev test_sunaudiodev skipped -- No module named sunaudiodev test_sys test test_sys failed -- Traceback (most recent call last): File "/install/Python-2.6.1/Lib/test/test_sys.py", line 354, in test_43581 self.assert_(sys.__stdout__.encoding == sys.__stderr__.encoding) AssertionError test_tcl test_tcl skipped -- No module named _tkinter test_timeout test_timeout skipped -- Use of the `network' resource not enabled test_unicode_file test_unicode_file skipped -- No Unicode filesystem semantics on this platform. test_urllib2net test_urllib2net skipped -- Use of the `network' resource not enabled test_urllibnet test_urllibnet skipped -- Use of the `network' resource not enabled test_uuid WARNING: uuid.getnode is unreliable on many platforms. It is disabled until the code and/or test can be fixed properly. WARNING: uuid._ifconfig_getnode is unreliable on many platforms. It is disabled until the code and/or test can be fixed properly. WARNING: uuid._unixdll_getnode is unreliable on many platforms. It is disabled until the code and/or test can be fixed properly. test_winreg test_winreg skipped -- No module named _winreg test_winsound test_winsound skipped -- No module named winsound test_zipfile64 test_zipfile64 skipped -- test requires loads of disk-space bytes and a long time to run 320 tests OK. 3 tests failed: test_httpservers test_socket test_sys 38 tests skipped: test_aepack test_al test_applesingle test_bsddb185 test_bsddb3 test_cd test_cl test_codecmaps_cn test_codecmaps_hk test_codecmaps_jp test_codecmaps_kr test_codecmaps_tw test_curses test_dbm test_gl test_imgfile test_kqueue test_linuxaudiodev test_macos test_macostools test_multiprocessing test_normalization test_ossaudiodev test_pep277 test_py3kwarn test_scriptpackages test_socketserver test_sqlite test_startfile test_sunaudiodev test_tcl test_timeout test_unicode_file test_urllib2net test_urllibnet test_winreg test_winsound test_zipfile64 3 skips unexpected on linux2: test_tcl test_dbm test_multiprocessing SxN __________________________________________________________________ Instant Messaging, free SMS, sharing photos and more... Try the new Yahoo! Canada Messenger at http://ca.beta.messenger.yahoo.com/ From ldo at geek-central.gen.new_zealand Wed Apr 8 00:39:05 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Wed, 08 Apr 2009 16:39:05 +1200 Subject: Python syntax References: <7dd228af-e549-444d-8623-11e95185123c@y9g2000yqg.googlegroups.com> Message-ID: In message , Steve Holden wrote: > Lawrence D'Oliveiro wrote: > >> In message <7dd228af- >> e549-444d-8623-11e95185123c at y9g2000yqg.googlegroups.com>, janus99 wrote: >> >>> I messed around with my own comp (windos xp) command prompt ... >> >> Try a Linux command prompt. You'll learn a bit more that way. >> > Great advice when Linux is available, but somewhat fatuous for a Windows > user. How hard is it to install Cygwin or download a live CD? From gagsl-py2 at yahoo.com.ar Wed Apr 8 01:09:29 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Wed, 08 Apr 2009 02:09:29 -0300 Subject: Adding method to class at run-time: bad style? References: Message-ID: En Tue, 07 Apr 2009 20:45:15 -0300, Grant Edwards escribi?: > On 2009-04-07, Scott David Daniels wrote: >> Grant Edwards wrote: >>> On 2009-04-07, Scott David Daniels wrote: >>> >>>>> File "/usr/lib/python2.5/site-packages/ClientForm.py", line >>>>> 2016, in add_to_form >>>>> Control.add_to_form(self, form) >>>>> TypeError: unbound method add_to_form() must be called with >>>>> FancyControl instance as first argument (got CheckboxControl >>>>> instance instead) >>> >>>> The monkey-patching only happens after the ClientForm module >>>> has been executed (first import), and the monkey-patching >>>> happens after all of that. So now the "tack it into the >>>> class" method looks a bit better if you cannot simply add your >>>> requirement to the ClientForm source. >>> >>> That's obviously the "right thing", but it makes portability >>> more of an issue (I would have to archive/distribute ClientForm >>> source and users would have to install the customized version >>> of ClientForm). >>> >>> Of course there's always the chance that my version of >>> monkey-patching will stop working with a different version of >>> ClientForm. We'll burn that bridge when we come to it. >> >> What you might use as a half-way measure: >> >> class Mixin: # or class Mixin(object) if new-style: >> def __eq__(self, other): >> return (self.type == other.type ... >> def __ne__(self, other): >> return not self.__eq__(other) >> class FancyControl(MixIn, ClientForm.Control): pass >> class FancyCheckboxControl(MixIn, ClientForm.CheckboxControl): pass >> .. >> ClientForm.Control = FancyControl >> ClientForm.CheckboxControl = FancyCheckboxControl > > That would work -- but there are probably 8 or 10 different > Control subclasses. It's a bit tedious mixing them all one at a > time, and you need more "inside" information (the names of all > the different subclasses). New style classes have a __subclasses__() method that could be used to find all of them (*at a certain moment*) -- but considering all the issues, I think that monkey-patching the base class is the "less bad" option in this case... -- Gabriel Genellina From aleksandar27 at brisiovonet.hr Wed Apr 8 01:38:26 2009 From: aleksandar27 at brisiovonet.hr (alejandro) Date: Wed, 8 Apr 2009 07:38:26 +0200 Subject: Anyone mannaged to access parallel port on windows xp? References: Message-ID: Now it doesn't throw exceptions but it doesn't work. I tryed to connect a LED light to the port and it wan't glow. Do you know a way to check if the port work? From gagsl-py2 at yahoo.com.ar Wed Apr 8 01:52:29 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Wed, 08 Apr 2009 02:52:29 -0300 Subject: Converting a PIL image object to a buffer References: <79ddf944-2fee-46c5-aed7-e6276dce88ed@s19g2000vbp.googlegroups.com> Message-ID: En Wed, 01 Apr 2009 19:20:43 -0300, Simon Hibbs escribi?: > On 1 Apr, 21:43, Gary Herron wrote: >> Simon Hibbs wrote: >> > I'm trying to dump a snapshot of my application window to the >> > clipboard. I can use ImageGrab in PIL to get the screen data into a >> > PIL image object, which i have converted to a bitmap using ImageWin, >> > but when I try to pass this to the clipboard using - >> >> > win32clipboard.SetClipboardData(win32clipboard.CF_BITMAP, img) >> >> > It fails, telling be that "The object must support the buffer >> > interface". The second argument to SetClipboardData should be a handle to a bitmap resource, not a string. See win32\test\test_clipboard.py for an example. PS: Hmm, looking at SetClipboardData, seems that a string containing the data in the right format *might* work too. But it's easier to use LoadImage than building the resource by hand, I think. -- Gabriel Genellina From prakash.stack at gmail.com Wed Apr 8 02:00:38 2009 From: prakash.stack at gmail.com (prakash jp) Date: Wed, 8 Apr 2009 11:30:38 +0530 Subject: Anyone mannaged to access parallel port on windows xp? In-Reply-To: References: Message-ID: <805f59d50904072300o6f321764m4c17546c5fca584d@mail.gmail.com> Hi all, just would like to say that most of the parallel port preexistant code is usually blinking leds, which is the not the true reprsentation of the paralle port behaviour. Here one needs to think that data is coming out byte after byte. Now plz look out for the sequence to push data byte after byte. One pin to pulse(programatically) for a byte of data to come out is the Pin 0, strobe pin, then reset the same to push the next byte of data. Latare u r expected to send these data byte in the sequnce that the target board requirement. I have done this in c - dos based regards Prakash On Tue, Apr 7, 2009 at 12:09 PM, alejandro wrote: > I have a switch that I should connect to the parallel port, but had no > luck > with it. Tha guy that made it for me told me that it would be easyer to > connect via parallel instead the USB.... > So did anyone have success? I only get suckess!! :-)) > tryed giveio.sys but it doesn't wort (can't figure out what is it) > > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From gagsl-py2 at yahoo.com.ar Wed Apr 8 02:41:22 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Wed, 08 Apr 2009 03:41:22 -0300 Subject: Incomplete exception tracebacks when importing from zipped modules References: Message-ID: En Thu, 02 Apr 2009 09:35:53 -0300, arve.knudsen at gmail.com escribi?: > I can't seem to get complete tracebacks when modules imported from zip > archives raise exceptions. [...] > As you can see, the code for each stack entry is omitted. Is this > normal?? A known problem, at least... You should be able to find it reported somewhere at http://bugs.python.org -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Wed Apr 8 02:49:36 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Wed, 08 Apr 2009 03:49:36 -0300 Subject: Injecting python function in an embedded python engine References: <49DA34A4.1000202@tekno-soft.it> Message-ID: En Mon, 06 Apr 2009 13:58:12 -0300, Roberto Fichera escribi?: > I've embedded python v2.6.x engine into my application without any > problem. > Now I would like to inject some additional functions after importing a > python module. > So, basically I'm importing a python module via PyImport_ImportModule() > function. > The python module is a simple set of functions and I would check if some > functions > does exist or not, than if doesn't I would add it from my C application. > Checking if > a function does exist or not isn't an issue, it works without any > problem, my problem > is only how to inject a new function in an already imported module. Easy (so I wonder whether I misunderstood your question): some_module.function_name = new_function_object or setattr(some_module, function_name, new_function_object) or use PyObject_SetAttr/PyObject_SetAttrString in C code. -- Gabriel Genellina From ldo at geek-central.gen.new_zealand Wed Apr 8 03:14:55 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Wed, 08 Apr 2009 19:14:55 +1200 Subject: Scraping a web page References: <9c8c445f0904070508p52e6011vaee4c5ce2b66d406@mail.gmail.com> <49DB4AC8.8050404@tim.thechases.com> Message-ID: In message , Support Desk wrote: > You could do something like below to get the rendered page. > > Import os > site = 'website.com' > X = os.popen('lynx --dump %s' % site).readlines() I wonder how easy it would be to get the page image in SVG format? I believe the Gecko HTML engine in Firefox already uses Cairo for its rendering, and Cairo supports SVG as one of its surface types. From prakash.stack at gmail.com Wed Apr 8 03:41:38 2009 From: prakash.stack at gmail.com (prakash jp) Date: Wed, 8 Apr 2009 13:11:38 +0530 Subject: Injecting python function in an embedded python engine In-Reply-To: <49DA34A4.1000202@tekno-soft.it> References: <49DA34A4.1000202@tekno-soft.it> Message-ID: <805f59d50904080041s43fed8a0h9bf8dd45f5436d18@mail.gmail.com> import os ch = os.system("import -window root temp.png") print ch after that no way to store the screen shot regards Prakash On Mon, Apr 6, 2009 at 10:28 PM, Roberto Fichera wrote: > Hi All in the list, > > I've embedded python v2.6.x engine into my application without any problem. > Now I would like to inject some additional functions after importing a > python module. > So, basically I'm importing a python module via PyImport_ImportModule() > function. > The python module is a simple set of functions and I would check if some > functions > does exist or not, than if doesn't I would add it from my C application. > Checking if > a function does exist or not isn't an issue, it works without any > problem, my problem > is only how to inject a new function in an already imported module. > > Thanks in advance, > Roberto Fichera. > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From tleeuwenburg at gmail.com Wed Apr 8 03:46:06 2009 From: tleeuwenburg at gmail.com (tleeuwenburg at gmail.com) Date: Wed, 8 Apr 2009 00:46:06 -0700 (PDT) Subject: TypeError: object cannot be interpreted as an index Message-ID: It started with this error message... "TypeError: object cannot be used as an index" foo = {} someObject = someClass() foo[someObject] = "hello" Obviously, there are some known reasons why objects may not be indexes, such as if they are not hashable (as in the case of lists). However, I'm not getting that error message. I tested this out with some minimal Python code, and the error did not occur. It only occurs in my big codebase, not in a neat minimal example using a very small class. The type of this object is listed as 'instance'. My interpreter is 2.5.1 So I tried looking for more information inside types, and found types.InstanceType. Running help(types.InstanceType) gave me: >>> help(types.InstanceType) hello Traceback (most recent call last): File "", line 1, in File "/usr/local/python-2.5.1/lib/python2.5/site.py", line 345, in __call__ import pydoc File "/usr/local/python-2.5.1/lib/python2.5/pydoc.py", line 56, in from repr import Repr ImportError: cannot import name Repr There is a good chance that this object is build from C code, instantiated through Swig. I'll have to do a bit more work to trace back where the problem object is being created... What, exactly, needs to be in place for an object to be a valid dictionary key? -- -------------------------------------------------- Tennessee Leeuwenburg http://myownhat.blogspot.com/ "Don't believe everything you think" From pnsmyth at gmail.com Wed Apr 8 04:15:23 2009 From: pnsmyth at gmail.com (Eclipse) Date: Wed, 8 Apr 2009 01:15:23 -0700 (PDT) Subject: Help with setting up Tkinter Message-ID: <65dfacfd-3940-4c4b-86bb-a6463f5f36df@y13g2000yqn.googlegroups.com> >>> import _tkinter >>> import Tkinter >>> Tkinter._test >>> From pnsmyth at gmail.com Wed Apr 8 04:20:30 2009 From: pnsmyth at gmail.com (Eclipse) Date: Wed, 8 Apr 2009 01:20:30 -0700 (PDT) Subject: Help with setting up Tkinter Message-ID: <772fe5d3-0dc5-4cfb-881d-1e75f54d1136@r33g2000yqn.googlegroups.com> G'day All I was following the instructions (listed at bottom of post) from the PythonInfo Wiki which says to run three tests. I ran the tests and test 1 and 2 worked Test 3 gave me an error - can anyone help ??? Tks in advance Pete >>> import _tkinter >>> import Tkinter >>> Tkinter._test >>> _________________________________________________________________________ Checking your Tkinter support A good way to systematically check whether your Tkinter support is working is the following. Enter an interactive Python interpreter in a shell on an X console. Step 1 - can _tkinter be imported? Try the following command at the Python prompt: >>> import _tkinter # with underscore, and lowercase 't' * If it works, go to step 2. * If it fails with "No module named _tkinter", your Python configuration needs to be modified to include this module (which is an extension module implemented in C). Do **not** edit Modules/Setup (it is out of date). You may have to install Tcl and Tk (when using RPM, install the -devel RPMs as well) and/or edit the setup.py script to point to the right locations where Tcl/Tk is installed. If you install Tcl/Tk in the default locations, simply rerunning "make" should build the _tkinter extension. * If it fails with an error from the dynamic linker, see above (for Unix, check for a header/library file mismatch; for Windows, check that the TCL/TK DLLs can be found). Step 2 - can Tkinter be imported? Try the following command at the Python prompt: >>> import Tkinter # no underscore, uppercase 'T' * If it works, go to step 3. * If it fails with "No module named Tkinter", your Python configuration need to be changed to include the directory that contains Tkinter.py in its default module search path. You have probably forgotten to define TKPATH in the Modules/Setup file. A temporary workaround would be to find that directory and add it to your PYTHONPATH environment variable. It is the subdirectory named "lib-tk" of the Python library directory (when using Python 1.4 or before, it is named "tkinter"). Step 3 - does Tkinter work? Try the following command at the Python prompt: >>> Tkinter._test( ) # note underscore in _test( ) * This should pop up a small window with two buttons. Clicking the "Quit" button makes it go away and the command return. If this works, you're all set. (When running this test on Windows, from Python run in a MS-DOS console, the new window somehow often pops up *under* the console window. Move it aside or locate the Tk window in the Taskbar.) * If this doesn't work, study the error message you get; if you can't see how to fix the problem, ask for help. From koranthala at gmail.com Wed Apr 8 04:27:50 2009 From: koranthala at gmail.com (koranthala) Date: Wed, 8 Apr 2009 01:27:50 -0700 (PDT) Subject: Help with setting up Tkinter References: <772fe5d3-0dc5-4cfb-881d-1e75f54d1136@r33g2000yqn.googlegroups.com> Message-ID: <62e254c6-8c09-4f1d-9c72-1de5c305ad0b@z14g2000yqa.googlegroups.com> On Apr 8, 1:20?pm, Eclipse wrote: > G'day All > > I was following the instructions (listed at bottom of post) from the > PythonInfo Wiki which says to run three tests. > > I ran the tests and test 1 and 2 worked > > Test 3 gave me an error - > > can anyone help ??? > > Tks in advance > > Pete > > >>> import _tkinter > >>> import Tkinter > >>> Tkinter._test > > > > _________________________________________________________________________ > > Checking your Tkinter support > > A good way to systematically check whether your Tkinter support is > working is the following. > > Enter an interactive Python interpreter in a shell on an X console. > > Step 1 - can _tkinter be imported? > > Try the following command at the Python prompt: > > >>> import _tkinter # with underscore, and lowercase 't' > > ? ? * If it works, go to step 2. > ? ? * If it fails with "No module named _tkinter", your Python > configuration needs to be modified to include this module (which is an > extension module implemented in C). Do **not** edit Modules/Setup (it > is out of date). You may have to install Tcl and Tk (when using RPM, > install the -devel RPMs as well) and/or edit the setup.py script to > point to the right locations where Tcl/Tk is installed. If you install > Tcl/Tk in the default locations, simply rerunning "make" should build > the _tkinter extension. > ? ? * If it fails with an error from the dynamic linker, see above > (for Unix, check for a header/library file mismatch; for Windows, > check that the TCL/TK DLLs can be found). > > Step 2 - can Tkinter be imported? > > Try the following command at the Python prompt: > > >>> import Tkinter # no underscore, uppercase 'T' > > ? ? * If it works, go to step 3. > ? ? * If it fails with "No module named Tkinter", your Python > configuration need to be changed to include the directory that > contains Tkinter.py in its default module search path. You have > probably forgotten to define TKPATH in the Modules/Setup file. A > temporary workaround would be to find that directory and add it to > your PYTHONPATH environment variable. It is the subdirectory named > "lib-tk" of the Python library directory (when using Python 1.4 or > before, it is named "tkinter"). > > Step 3 - does Tkinter work? > > Try the following command at the Python prompt: > > >>> Tkinter._test( ) # note underscore in _test( ) > > ? ? * This should pop up a small window with two buttons. Clicking the > "Quit" button makes it go away and the command return. If this works, > you're all set. (When running this test on Windows, from Python run in > a MS-DOS console, the new window somehow often pops up *under* the > console window. Move it aside or locate the Tk window in the Taskbar.) > ? ? * > > ? ? ? If this doesn't work, study the error message you get; if you > can't see how to fix the problem, ask for help. Can you try Tkinter._test() instead of Tkinter._test Just Tkinter._test will go and hit its __repr__ code - i.e. representation I guess - which displays the function data instead of running actual function. From clp2 at rebertia.com Wed Apr 8 04:29:08 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 8 Apr 2009 01:29:08 -0700 Subject: Help with setting up Tkinter In-Reply-To: <65dfacfd-3940-4c4b-86bb-a6463f5f36df@y13g2000yqn.googlegroups.com> References: <65dfacfd-3940-4c4b-86bb-a6463f5f36df@y13g2000yqn.googlegroups.com> Message-ID: <50697b2c0904080129g6f2a85eeq2474c78602725cc@mail.gmail.com> On Wed, Apr 8, 2009 at 1:15 AM, Eclipse wrote: >>>> import _tkinter >>>> import Tkinter >>>> Tkinter._test > That last input line should be: Tkinter._test() Note the parens, which do matter (Python != Ruby/Smalltalk). Try again and see if you get an actual error. Cheers, Chris -- I have a blog: http://blog.rebertia.com From pataphor at gmail.com Wed Apr 8 05:06:52 2009 From: pataphor at gmail.com (pataphor) Date: Wed, 8 Apr 2009 11:06:52 +0200 Subject: more fun with iterators (mux, demux) References: Message-ID: <20090408110652.52931472@hyperspace> On 07 Apr 2009 02:05:59 GMT Steven D'Aprano wrote: > The demuxer can't be an iterator, since it needs to run through the > entire collection. Then your demuxer obviously cannot handle infinite sequences. > def demux(it, n): > collectors = [[] for i in xrange(n)] > i = 0 > for item in it: > collectors[i].append(item) > i = (i+1) % n > return tuple([iter(x) for x in collectors]) But this one can: from collections import deque from itertools import cycle, izip, count, islice def mux(*iterables): for it in izip(*iterables): for item in it: yield item def demux(seq,n): it = iter(seq) Q = [deque() for i in xrange(n)] CQ = cycle(Q) def gen(D): for x,C in izip(it,CQ): C.appendleft(x) while D: yield D.pop() while D: yield D.pop() return map(gen,Q) def test(): a = count(10) b = count(20) c = count (30) x = demux(mux(a,b,c),3) for e in x: print list(islice(e,0,10)) if __name__=='__main__': test() P. From ldo at geek-central.gen.new_zealand Wed Apr 8 05:19:33 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Wed, 08 Apr 2009 21:19:33 +1200 Subject: Q: "Best" book for teaching References: <8e3d0032-5e9f-44c2-9380-1d2383552046@u5g2000vbc.googlegroups.com> Message-ID: In message <8e3d0032-5e9f-44c2-9380-1d2383552046 at u5g2000vbc.googlegroups.com>, grkuntzmd at gmail.com wrote: > I am considering teaching an "introduction to programming" course for > continuing education adults at a local community college. These would > people with no programming experience, but I will require a reasonable > facility with computers. I thought that a good introduction might be to show them how HTML works, and progress from there to embedding little bits of JavaScript. Nothing to do with Python I know, but might be a possibility. From __peter__ at web.de Wed Apr 8 05:27:00 2009 From: __peter__ at web.de (Peter Otten) Date: Wed, 08 Apr 2009 11:27:00 +0200 Subject: TypeError: object cannot be interpreted as an index References: Message-ID: tleeuwenburg at gmail.com wrote: > It started with this error message... "TypeError: object cannot be > used as an index" > > foo = {} > someObject = someClass() > foo[someObject] = "hello" "interpreted" or "used"? If the former, 'foo' may be a list rather than a dict, and someClass a classic class: >>> class A: pass ... >>> {A(): 42} {<__main__.A instance at 0x2ac2a6d35bd8>: 42} >>> [][A()] Traceback (most recent call last): File "", line 1, in TypeError: object cannot be interpreted as an index To find out the actual the actual type of a classic class look into the __class__ attribute: >>> type(A()) >>> A().__class__ Peter PS: Please cut and paste in the future. From nospam at nospam.com Wed Apr 8 05:56:34 2009 From: nospam at nospam.com (Gilles Ganault) Date: Wed, 08 Apr 2009 11:56:34 +0200 Subject: Why does Python show the whole array? Message-ID: Hello I'd like to go through a list of e-mail addresses, and extract those that belong to well-known ISP's. For some reason I can't figure out, Python shows the whole list instead of just e-mails that match: ======= script test = "toto at gmail.com" isp = ["gmail.com", "yahoo.com"] for item in isp: if test.find(item): print item ======= output gmail.com yahoo.com ======= Any idea why I'm also getting "yahoo.com"? Thank you. From __peter__ at web.de Wed Apr 8 06:01:35 2009 From: __peter__ at web.de (Peter Otten) Date: Wed, 08 Apr 2009 12:01:35 +0200 Subject: Why does Python show the whole array? References: Message-ID: Gilles Ganault wrote: > I'd like to go through a list of e-mail addresses, and extract those > that belong to well-known ISP's. For some reason I can't figure out, > Python shows the whole list instead of just e-mails that match: > > ======= script > test = "toto at gmail.com" > isp = ["gmail.com", "yahoo.com"] > for item in isp: > ????????if test.find(item): > ????????????????print item > ======= output > gmail.com > yahoo.com > ======= > > Any idea why I'm also getting "yahoo.com"? Because str.find() returns the position of the search string if found and -1 if it is not found: >>> "abc".find("bc") 1 >>> "abc".find("ab") 0 >>> "abc".find("x") -1 Use if test.find(item) != -1: ... or if item in test: ... to make your example work. Peter From eckhardt at satorlaser.com Wed Apr 8 06:11:55 2009 From: eckhardt at satorlaser.com (Ulrich Eckhardt) Date: Wed, 08 Apr 2009 12:11:55 +0200 Subject: Why does Python show the whole array? References: Message-ID: Gilles Ganault wrote: > test = "toto at gmail.com" > isp = ["gmail.com", "yahoo.com"] > for item in isp: > if test.find(item): > print item > ======= output > gmail.com > yahoo.com > ======= > > Any idea why I'm also getting "yahoo.com"? find() returns the index where it is found or -1 if it is not found. Both an index>0 or a -1 evaluate to True when used as conditional expression. Uli -- Sator Laser GmbH Gesch?ftsf?hrer: Thorsten F?cking, Amtsgericht Hamburg HR B62 932 From marduk at letterboxes.org Wed Apr 8 06:28:38 2009 From: marduk at letterboxes.org (Albert Hopkins) Date: Wed, 08 Apr 2009 06:28:38 -0400 Subject: Why does Python show the whole array? In-Reply-To: References: Message-ID: <1239186518.7120.9.camel@blackwidow.nbk> On Wed, 2009-04-08 at 12:01 +0200, Peter Otten wrote: > Gilles Ganault wrote: > > > I'd like to go through a list of e-mail addresses, and extract those > > that belong to well-known ISP's. For some reason I can't figure out, > > Python shows the whole list instead of just e-mails that match: > > > > ======= script > > test = "toto at gmail.com" > > isp = ["gmail.com", "yahoo.com"] > > for item in isp: > > if test.find(item): > > print item > > ======= output > > gmail.com > > yahoo.com > > ======= > > > > Any idea why I'm also getting "yahoo.com"? > > Because str.find() returns the position of the search string if found and -1 > if it is not found: > > >>> "abc".find("bc") > 1 > >>> "abc".find("ab") > 0 > >>> "abc".find("x") > -1 > > Use > > if test.find(item) != -1: ... > > or > > if item in test: ... > > to make your example work. Or you could also use the .endswith() method if test[test.find('@')+1:].endswith(item): From ben+python at benfinney.id.au Wed Apr 8 06:29:54 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Wed, 08 Apr 2009 20:29:54 +1000 Subject: Why does Python show the whole array? References: Message-ID: <87d4bn8ml9.fsf@benfinney.id.au> Gilles Ganault writes: > I'd like to go through a list of e-mail addresses, and extract those > that belong to well-known ISP's. For some reason I can't figure out, > Python shows the whole list instead of just e-mails that match: > > ======= script > test = "toto at gmail.com" > isp = ["gmail.com", "yahoo.com"] > for item in isp: > if test.find(item): > print item > ======= output > gmail.com > yahoo.com > ======= > > Any idea why I'm also getting "yahoo.com"? You've had answers on the ?why? question. Here's a suggestion for a better way that doesn't involve ?find?: >>> known_domains = ["example.com", "example.org"] >>> test_address = "toto at example.com" >>> for domain in known_domains: ... if test_address.endswith("@" + domain): ... print domain ... example.com >>> If all you want is a boolean ?do any of these domains match the address?, it's quicker and simpler to feed an iterator to ?any? (first introduced in Python 2.5), which short-cuts by exiting on the first item that produces a True result: >>> known_domains = ["example.com", "example.org"] >>> test_address = "toto at example.com" >>> any(test_address.endswith("@" + domain) for domain in known_domains) True >>> test_address = "tinman at example.net" >>> any(test_address.endswith("@" + domain) for domain in known_domains) False -- \ ?For my birthday I got a humidifier and a de-humidifier. I put | `\ them in the same room and let them fight it out.? ?Steven Wright | _o__) | Ben Finney From piet at cs.uu.nl Wed Apr 8 06:30:01 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 08 Apr 2009 12:30:01 +0200 Subject: How to go about. On read/write locks References: <888ed340-48bd-414a-b827-8de726dc729c@h28g2000yqd.googlegroups.com> <73va73F10v3a8U3@mid.uni-berlin.de> <523a2f2e-5ae4-417e-909b-0dec94a4ee56@k41g2000yqh.googlegroups.com> Message-ID: >>>>> Carl Banks (CB) wrote: >CB> On Apr 6, 2:23?pm, "Diez B. Roggisch" wrote: >>> > This is a classical synchronization problem with a classical solution: >>> > You treat the readers as a group, and the writers individually. So you >>> > have a write lock that each writer has to acquire and release, but it is >>> > acquired only by the first reader and released by the last one. >>> > Therefore you need a counter of the number of readers, and manipulations >>> > of this counter must be protected by another lock. >>> >>> I was going to suggest a similar approach but refused to because of a >>> problem I see with your code as well - if the readers are reading very >>> fast (the OP didn't state what his actual application is, so it might >>> not be a consumer-producer scheme which I don't think a dict would be >>> the natural choice anyway) they can block the writer from writing >>> alltogether. >CB> You could implement some kind of fair ordering where whoever requests >CB> a lock first is guaranteed to get it first, but I can't think of a way >CB> to do that without requiring all readers to acquire two locks. The original implementation (with writers starvation) comes from [1]. They also describe a solution where witers have priority, but it needs 5 semaphores and 2 counters (one for writers and one for readers). It can cause starvation for readers, however. For the OP this wouldn't be a problem because writers are rare in his situation. However, I found a solution [2] with just one additional counter for the number of writers and no additional semaphores. The manipulations of the writers counter are also protected by the same mutex. This solution is fair for both readers and writers. Translated in Python this would be: #------------------------------------------------------------------------ from threading import Lock mutex = Lock() writelock = Lock() numreaders = 0 numwriters = 0 #------------------------------------------------------------------------ # Reader code: mutex.acquire() if numwriters > 0 or numreaders == 0: mutex.release() writelock.acquire() mutex.acquire() numreaders += 1 mutex.release() ## critical section for reader mutex.acquire() numreaders -= 1 if numreaders == 0: writelock.release() mutex.release() #------------------------------------------------------------------------ # Writer code: mutex.acquire() numwriters += 1 mutex.release() writelock.acquire() ## critical section for writer mutex.acquire() numwriters -= 1 mutex.release() writelock.release() #------------------------------------------------------------------------ I am going to put this in a class, with a context manager so that it can be use with the 'with' statement like the normal locks. I also found a solution with no additional counters but an additional semaphore, but I found it only in lecture notes [3,4]; I couldn't find any scientific publications about it. So I don't know for sure if it is correct, fair etc. [1] P. J. Courtois , F. Heymans , D. L. Parnas, Concurrent control with ?readers? and ?writers?, Communications of the ACM, v.14 n.10, p.667-668, Oct. 1971 [2] Jalal Kawash, Process Synchronization with Readers and Writers Revisited Journal of Computing and Information Technology - CIT 13, 2005, 1, 43?51 [3] http://pages.cs.wisc.edu/~haryadi/537/slides/lec18-semaphores.ppt [4] http://vorlon.case.edu/~jrh23/338/HW3.pdf -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From davea at ieee.org Wed Apr 8 06:44:10 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 08 Apr 2009 06:44:10 -0400 Subject: Why does Python show the whole array? In-Reply-To: References: Message-ID: <49DC7FFA.2070103@ieee.org> Gilles Ganault wrote: > Hello > > I'd like to go through a list of e-mail addresses, and extract those > that belong to well-known ISP's. For some reason I can't figure out, > Python shows the whole list instead of just e-mails that match: > > ======= script > test = "toto at gmail.com" > isp = ["gmail.com", "yahoo.com"] > for item in isp: > if test.find(item): > print item > ======= output > gmail.com > yahoo.com > ======= > > Any idea why I'm also getting "yahoo.com"? > > Thank you. > > Look up the definition of string.find(). It returns a -1 for failure, not 0. So your test should presumably be if test.find(item) != -1: From piet at cs.uu.nl Wed Apr 8 06:44:54 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 08 Apr 2009 12:44:54 +0200 Subject: TypeError: object cannot be interpreted as an index References: Message-ID: >>>>> "tleeuwenburg at gmail.com" (tc) wrote: >tc> What, exactly, needs to be in place for an object to be a valid >tc> dictionary key? It must have __hash__ and __cmp__ or __eq__ methods. Newstyle classes inherit these from object. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From qilong.ren at gmail.com Wed Apr 8 08:19:58 2009 From: qilong.ren at gmail.com (Qilong Ren) Date: Wed, 8 Apr 2009 20:19:58 +0800 Subject: Why does Python show the whole array? In-Reply-To: References: Message-ID: <0EF4E3E0-104D-462C-861C-CA22FBEBBBBE@gmail.com> > Remeber the return value of find function of a string is -1 when it fails, which is True. Try: for item in isp: if item in test: print item > From: Gilles Ganault > Date: April 8, 2009 5:56:34 PM CST > To: python-list at python.org > Subject: Why does Python show the whole array? > > > Hello > > I'd like to go through a list of e-mail addresses, and extract those > that belong to well-known ISP's. For some reason I can't figure out, > Python shows the whole list instead of just e-mails that match: > > ======= script > test = "toto at gmail.com" > isp = ["gmail.com", "yahoo.com"] > for item in isp: > if test.find(item): > print item > ======= output > gmail.com > yahoo.com > ======= > > Any idea why I'm also getting "yahoo.com"? > > Thank you. > > > > -- > http://mail.python.org/mailman/listinfo/python-list -------------- next part -------------- An HTML attachment was scrubbed... URL: From python-url at phaseit.net Wed Apr 8 08:23:40 2009 From: python-url at phaseit.net (Gabriel Genellina) Date: Wed, 8 Apr 2009 12:23:40 +0000 (UTC) Subject: Python-URL! - weekly Python news and links (Apr 8) Message-ID: QOTW: "Those who show promise can advance to our Winter Improve Python to Expert program, for an additional fee, and, be given expert tutoring to help you gain our exemplary A.R.S.E./W.I.P.E certification which is guaranteed to attract certain types of employers by its name alone." - Paddy3118 http://groups.google.com/group/comp.lang.python/msg/f86e314251d94d96 Two new releases this week: * Python 3.1 alpha 2: http://mail.python.org/pipermail/python-list/2009-April/707817.html * and Python 2.6.2 candidate 1: http://mail.python.org/pipermail/python-list/2009-April/708214.html Dynamically adding new methods to a class hierarchy: http://groups.google.com/group/comp.lang.python/t/a6805cc39de3746a/ How to handle infinite generators (in the example, generating *all* prime numbers): http://groups.google.com/group/comp.lang.python/t/77cc9764f9d5a072/ Refactoring a giant class into smaller parts: http://groups.google.com/group/comp.lang.python/t/581d6b8dac0360d6/ People coming from static languages fear that Python code requires stronger unit tests - is that true? http://groups.google.com/group/comp.lang.python/t/8a3be7bb5ee1d1b4/ Synchronizing concurrent access to a dictionary: http://groups.google.com/group/comp.lang.python/t/d2156aa4aaf95bd5/ Can an object know whose attribute it is called upon? http://groups.google.com/group/comp.lang.python/t/1b43c60954e0a7a3/ Usually, returning different types based on input parameters is a bad idea: http://groups.google.com/group/comp.lang.python/t/b9fbef4368cf9981/ ======================================================================== 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 enthusiats": 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/ The somewhat older Vaults of Parnassus ambitiously collects references to all sorts of Python resources. http://www.vex.net/~x/parnassus/ 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 *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 nicogrubert at gmail.com Wed Apr 8 08:54:33 2009 From: nicogrubert at gmail.com (Nico Grubert) Date: Wed, 08 Apr 2009 14:54:33 +0200 Subject: List of paths In-Reply-To: References: Message-ID: <49DC9E89.6000101@gmail.com> > Here's a tricky case that doesn't show up in your example: > In each case above, the directory names are distinct. > how about: >['/desk', '/desk/ethanallen', '/desk/ikea', > '/desktop', /desktop/pc', '/desktop/mac'] >Should the answer be ['/desk'] or ['/desk', '/desktop'] ? Hi Scott good point. It should be: ['/desk', '/desktop'] From nospam at nospam.com Wed Apr 8 09:06:07 2009 From: nospam at nospam.com (Gilles Ganault) Date: Wed, 08 Apr 2009 15:06:07 +0200 Subject: Why does Python show the whole array? References: Message-ID: <388pt4lqguqukl7kddnejhob3kkv04fs20@4ax.com> On Wed, 08 Apr 2009 12:11:55 +0200, Ulrich Eckhardt wrote: >find() returns the index where it is found or -1 if it is not found. Both an >index>0 or a -1 evaluate to True when used as conditional expression. Thanks everyone. I shouldn't have assumed that "if test.find(item):" was necessarily enough to mean True. for item in isp: #GOOD if item in test: if test.find(item) > 0: print test From andrew at acooke.org Wed Apr 8 09:24:02 2009 From: andrew at acooke.org (andrew cooke) Date: Wed, 8 Apr 2009 09:24:02 -0400 (CLT) Subject: Why does Python show the whole array? In-Reply-To: <388pt4lqguqukl7kddnejhob3kkv04fs20@4ax.com> References: <388pt4lqguqukl7kddnejhob3kkv04fs20@4ax.com> Message-ID: Gilles Ganault wrote: > On Wed, 08 Apr 2009 12:11:55 +0200, Ulrich Eckhardt > wrote: >>find() returns the index where it is found or -1 if it is not found. Both >> an >>index>0 or a -1 evaluate to True when used as conditional expression. > > Thanks everyone. I shouldn't have assumed that "if test.find(item):" > was necessarily enough to mean True. > > for item in isp: > #GOOD if item in test: > if test.find(item) > 0: the post you quote has an error. you probably want > -1 there. 0 is returned if the two strings match at the start. the problem is that the method return isn't consistent with implicit conversion to boolean; python does convert non-zero to True. andrew > print test > -- > http://mail.python.org/mailman/listinfo/python-list > > From ebonak at hotmail.com Wed Apr 8 09:25:37 2009 From: ebonak at hotmail.com (Esmail) Date: Wed, 08 Apr 2009 09:25:37 -0400 Subject: genetic algorithms in Python? Message-ID: Hello, Anyone using Python for coding up genetic algorithms? If so, would you share your favorite modules/libraries/tools? Thanks, Esmail From icanbob at gmail.com Wed Apr 8 09:33:11 2009 From: icanbob at gmail.com (bobicanprogram) Date: Wed, 8 Apr 2009 06:33:11 -0700 (PDT) Subject: named pipe and Linux References: <5b5157dd-ca70-4c6d-8adb-8a4d322fbd4a@x31g2000prc.googlegroups.com> Message-ID: <505b86ca-9a3f-4882-a6e1-9c1b9b74d525@g19g2000yql.googlegroups.com> On Apr 7, 1:08 pm, akineko wrote: > Hello everyone, > > I'm trying to use named pipes to fuse a Python program and a C > program. > One side creates pipes using os.mkfifo() and both sides use the same > named pipes (one side reads, another side writes). The read side uses > select.select() to wait for incoming messages and read the message > when select.select() says it is ready. > > The length of the message is unknown to the read side. > I cannot use file.read() because it will block waiting for an EOF. > I cannot use file.readline() because how many lines have arrived is > unknown. > So, I needed to use os.read() with the exact number of characters to > read. > > Under Solaris environment, os.fstat() provides the exact size of the > message that has arrived. > Thus, two processes can communicate each other through the named pipes > without blocking. > > However, the above scheme didn't work under Linux. > Linux os.fstat() returns size=0 even the message is pending. > (I think Linux buffers the message in memory while Solaris buffers the > message in a file system) > > My question is, how can I make the named pipe scheme work under Linux? > Is there any way to read the message without getting blocked? > > I know this is more Linux question than Python question but I believe > many Python programmers are strong Linux programmers. > > Any suggestions will be appreciated. > > Best regards, > Aki Niimura The SIMPL open source project (http://www.icanprogram.com/simpl) provides an ultra lightweight toolkit useful for joining Python programs to C programs using a Send/Receive/Reply mechanism first pioneered by QNX. SIMPL uses a fifo synchronized shared memory scheme for the local message pass. Through the use of generic surrogate pairs SIMPL processes can be distributed across TCP/IP or RS232 (think radio modem) networks often times without any changes or recompiles. Through the use of another type of generic surrogate a Python module running on a nonLinux OS can communicate transparently with a module running on a Linux box. A SIMPL application consists of two or more SIMPL modules collaborating in this way. SIMPL modules can be written in any number of languages including Python, C, C++, Tcl/Tk or JAVA. More importantly SIMPL modules written in different languages can be mixed in a given SIMPL application. There is a Sudoku puzzle solver example here: http://www.icanprogram.com/simplBook/simplBook.self.html bob SIMPL project facilitator From rdmurray at bitdance.com Wed Apr 8 09:46:16 2009 From: rdmurray at bitdance.com (R. David Murray) Date: Wed, 8 Apr 2009 13:46:16 +0000 (UTC) Subject: Some test fail on my new Python 2.6 References: <777745.16818.qm@web56006.mail.re3.yahoo.com> Message-ID: Sorin Schwimmer wrote: > > I wanted to replace my old Python 2.4 and tcl/tk 8.4 with tcl/tk 8.5.6 and Python 2.6, mainly so that I can enjoy ttk. tcl/tk was installed from sources without any problem, I started a wish and worked. > > Now, for the Python, here are all the issues signaled by make test: > > running build > running build_ext > building 'dbm' extension > gcc -pthread -fPIC -fno-strict-aliasing -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -DHAVE_GDBM_NDBM_H -I. -I/install/Python-2.6.1/./Include -I. -IInclude -I./Include -I/usr/local/include -I/install/Python-2.6.1/Include -I/install/Python-2.6.1 -c /install/Python-2.6.1/Modules/dbmmodule.c -o build/temp.linux-i686-2.6/install/Python-2.6.1/Modules/dbmmodule.o > gcc -pthread -shared build/temp.linux-i686-2.6/install/Python-2.6.1/Modules/dbmmodule.o -L/usr/local/lib -lgdbm -o build/lib.linux-i686-2.6/dbm.so > *** WARNING: renaming "dbm" since importing it failed: build/lib.linux-i686-2.6/dbm.so: undefined symbol: dbm_firstkey Ok, so DBM wasn't built because it couldn't find the external symbol 'dbm_firstkey'. I have no idea off the top of my head why that would happen, but I don't think you really care at the moment since you are trying to get tkinter working. If you do care we can come back to that. > building '_tkinter' extension > gcc -pthread -fPIC -fno-strict-aliasing -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -DWITH_APPINIT=1 -DWITH_BLT=1 -I/usr/X11R6/include -I. -I/install/Python-2.6.1/./Include -I. -IInclude -I./Include -I/usr/local/include -I/install/Python-2.6.1/Include -I/install/Python-2.6.1 -c /install/Python-2.6.1/Modules/_tkinter.c -o build/temp.linux-i686-2.6/install/Python-2.6.1/Modules/_tkinter.o > gcc -pthread -fPIC -fno-strict-aliasing -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -DWITH_APPINIT=1 -DWITH_BLT=1 -I/usr/X11R6/include -I. -I/install/Python-2.6.1/./Include -I. -IInclude -I./Include -I/usr/local/include -I/install/Python-2.6.1/Include -I/install/Python-2.6.1 -c /install/Python-2.6.1/Modules/tkappinit.c -o build/temp.linux-i686-2.6/install/Python-2.6.1/Modules/tkappinit.o > gcc -pthread -shared build/temp.linux-i686-2.6/install/Python-2.6.1/Modules/_tkinter.o build/temp.linux-i686-2.6/install/Python-2.6.1/Modules/tkappinit.o -L/usr/X11R6/lib64 -L/usr/X11R6/lib -L/usr/local/lib -lBLT -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 Now, this one is clear. It couldn't find libtk8.5.so when it tried to use _tkinter.so. So either that symlink doesn't exist, or points to a nonexistent library, or there's something wrong with your load library path, so that python isn't finding it when it tries to do the load. If you don't know what that last one means, check out 'man ldconfig'. > Failed to find the necessary bits to build these modules: > _sqlite3 bsddb185 sunaudiodev Here you presumably don't have these libraries installed, so no real problem. > To find the necessary bits, look in setup.py in detect_modules() for the module's name. > > > Failed to build these modules: > _tkinter dbm These we covered above. Most of the stuff you show from the test run is expected output for one reason or another. The ones that aren't... > test_multiprocessing > test_multiprocessing skipped -- OSError raises on RLock creation, see issue 3111! According to the referenced bug, this may be a result of your not having /dev/shm mounted, but in any case it is more a bug in the tests than a bug in your system or setup. > test_socket > test test_socket failed -- Traceback (most recent call last): > File "/install/Python-2.6.1/Lib/test/test_socket.py", line 474, in testSockName > my_ip_addr = socket.gethostbyname(socket.gethostname()) > gaierror: [Errno -2] Name or service not known This probably means that you haven't properly defined 'localhost' in your /etc/hosts file. > test_sys > test test_sys failed -- Traceback (most recent call last): > File "/install/Python-2.6.1/Lib/test/test_sys.py", line 354, in test_43581 > self.assert_(sys.__stdout__.encoding == sys.__stderr__.encoding) > AssertionError This one is puzzling. Did you modify 'site.py'? Or perhaps you redirected stdout and stderr to different places and they wound up with different encodings for some reason. In which case that one would be a bug in the tests as well. > test_tcl > test_tcl skipped -- No module named _tkinter This is a result of the build failure. > 320 tests OK. > 3 tests failed: > test_httpservers test_socket test_sys So we understand test_socket and maybe test_sys. You'll have to show me the output of test_httpsservers run in verbose mode again to see if I can make sense of that one, if you care. > 38 tests skipped: > test_aepack test_al test_applesingle test_bsddb185 test_bsddb3 > test_cd test_cl test_codecmaps_cn test_codecmaps_hk > test_codecmaps_jp test_codecmaps_kr test_codecmaps_tw test_curses > test_dbm test_gl test_imgfile test_kqueue test_linuxaudiodev > test_macos test_macostools test_multiprocessing test_normalization > test_ossaudiodev test_pep277 test_py3kwarn test_scriptpackages > test_socketserver test_sqlite test_startfile test_sunaudiodev > test_tcl test_timeout test_unicode_file test_urllib2net > test_urllibnet test_winreg test_winsound test_zipfile64 These as it implies are all normal. > 3 skips unexpected on linux2: > test_tcl test_dbm test_multiprocessing And these we've covered. So your biggest barrier to getting your python working for your purposes is getting python to find the libtk8.5.so. Everything else you can ignore, unless you want to try to track them down. -- R. David Murray http://www.bitdance.com From invalid at invalid Wed Apr 8 09:57:01 2009 From: invalid at invalid (Grant Edwards) Date: Wed, 08 Apr 2009 08:57:01 -0500 Subject: Adding method to class at run-time: bad style? References: Message-ID: On 2009-04-08, Gabriel Genellina wrote: >>> ClientForm.Control = FancyControl >>> ClientForm.CheckboxControl = FancyCheckboxControl >> >> That would work -- but there are probably 8 or 10 different >> Control subclasses. It's a bit tedious mixing them all one at a >> time, and you need more "inside" information (the names of all >> the different subclasses). > > New style classes have a __subclasses__() method that could be > used to find all of them (*at a certain moment*) -- but > considering all the issues, I think that monkey-patching the > base class is the "less bad" option in this case... Of course I could just -- Grant Edwards grante Yow! Th' MIND is the Pizza at Palace of th' SOUL visi.com From invalid at invalid Wed Apr 8 10:02:25 2009 From: invalid at invalid (Grant Edwards) Date: Wed, 08 Apr 2009 09:02:25 -0500 Subject: Adding method to class at run-time: bad style? References: Message-ID: On 2009-04-08, Gabriel Genellina wrote: >>> class Mixin: # or class Mixin(object) if new-style: >>> def __eq__(self, other): >>> return (self.type == other.type ... >>> def __ne__(self, other): >>> return not self.__eq__(other) >>> class FancyControl(MixIn, ClientForm.Control): pass >>> class FancyCheckboxControl(MixIn, ClientForm.CheckboxControl): pass >>> .. >>> ClientForm.Control = FancyControl >>> ClientForm.CheckboxControl = FancyCheckboxControl >> >> That would work -- but there are probably 8 or 10 different >> Control subclasses. It's a bit tedious mixing them all one at a >> time, and you need more "inside" information (the names of all >> the different subclasses). > > New style classes have a __subclasses__() method that could be used to > find all of them (*at a certain moment*) -- but considering all the > issues, I think that monkey-patching the base class is the "less bad" > option in this case... I agree. Inserting methods into the base class seems to be working fine, and it's the least messy of the alternatives. I'd submit a patch for ClientForm, but I don't know if my definition of "equal" for a form/control is generally useful enough to warrant being added to the package. FWIW, I'm using it for automated regression-testing on a product containing a web server. Basically, I perform an operation on the product, and then check to make sure that only certain web pages/form/controls changed in the manner they should have. -- Grant Edwards grante Yow! ... the HIGHWAY is at made out of LIME JELLO and visi.com my HONDA is a barbequeued OYSTER! Yum! From george.sakkis at gmail.com Wed Apr 8 10:09:30 2009 From: george.sakkis at gmail.com (George Sakkis) Date: Wed, 8 Apr 2009 07:09:30 -0700 (PDT) Subject: Returning different types based on input parameters References: Message-ID: On Apr 7, 3:18?pm, Adam Olsen wrote: > On Apr 6, 3:02?pm, George Sakkis wrote: > > > For example, it is common for a function f(x) to expect x to be simply > > iterable, without caring of its exact type. Is it ok though for f to > > return a list for some types/values of x, a tuple for others and a > > generator for everything else (assuming it's documented), or it should > > always return the most general (iterator in this example) ? > > For list/tuple/iterable the correlation with the argument's type is > purely superficial, *because* they're so compatible. ?Why should only > tuples and lists get special behaviour? ?Why shouldn't every other > argument type return a list as well? That's easy; because the result might be infinite. In which case you may ask "why shouldn't every argument type return an iterator then", and the reason is usually performance; if you already need to store the whole result sequence (e.g. sorted()), why return just an iterator to it and force the client to copy it to another list if he needs anything more than iterating once over it ? > A counter example is python 3.0's str/bytes functions. ?They're > mutually incompatible and there's no default. As already mentioned, another example is filter() that tries to match the input sequence type and falls back to list if it fails. > > To take it further, what if f wants to return different types, > > differing even in a duck-type sense? That's easier to illustrate in a > > API-extension scenario. Say that there is an existing function `solve > > (x)` that returns `Result` instances. ?Later someone wants to extend f > > by allowing an extra optional parameter `foo`, making the signature > > `solve(x, foo=None)`. As long as the return value remains backward > > compatible, everything's fine. However, what if in the extended case, > > solve() has to return some *additional* information apart from > > `Result`, say the confidence that the result is correct ? In short, > > the extended API would be: > > > ? ? def solve(x, foo=None): > > ? ? ? ? ''' > > ? ? ? ? @rtype: `Result` if foo is None; (`Result`, confidence) > > otherwise. > > ? ? ? ? ''' > > > Strictly speaking, the extension is backwards compatible; previous > > code that used `solve(x)` will still get back `Result`s. The problem > > is that in new code you can't tell what `solve(x,y)` returns unless > > you know something about `y`. My question is, is this totally > > unacceptable and should better be replaced by a new function `solve2 > > (x, foo=None)` that always returns (`Result`, confidence) tuples, or > > it might be a justifiable cost ? Any other API extension approaches > > that are applicable to such situations ? > > At a minimum it's highly undesirable. ?You lose a lot of readability/ > maintainability. ?solve2/solve_ex is a little ugly, but that's less > overall, so it's the better option. That's my feeling too, at least in a dynamic language. For a static language that allows overloading, that should be a smaller (or perhaps no) issue. George From dullrich at sprynet.com Wed Apr 8 10:43:03 2009 From: dullrich at sprynet.com (David C. Ullrich) Date: Wed, 08 Apr 2009 09:43:03 -0500 Subject: SoHo Book Problem References: Message-ID: Never mind (at least tentatively). Later in the day I got an email from the publisher, saying they're going to send me a corrected copy "free". Evidently if you get a bad copy you don't have to complain to the publisher or the retailer, you just have to post a complaint somewhere where Google can see it... DU. In article , "David C. Ullrich" wrote: > Just curious - has anyone else bought the printed > Python 3 Reference Manual published by SoHo Books? > > Talking about what they call "Part 2" of their Python > Documentation. I haven't looked in detail - Part 1 > seems fine, but the typesetting in Part 2 is totally > screwed up. > > I mean totally - on more or less every page there are lines > that are unreadable because there are words printed on top > of each other instead of next to each other! > > On the one hand I don't see how the problem could be with > just my copy. On the other hand I don't see how they could > _all_ be like mine - that would mean nobody even _glanced_ > at what was coming out of the press. So I'm curious whether > anyone else has a copy. > > (I know it's all online. Some people like _books_...) > > DU. -- David C. Ullrich From ndbecker2 at gmail.com Wed Apr 8 10:51:19 2009 From: ndbecker2 at gmail.com (Neal Becker) Date: Wed, 08 Apr 2009 10:51:19 -0400 Subject: more fun with iterators (mux, demux) References: <20090408110652.52931472@hyperspace> Message-ID: pataphor wrote: > On 07 Apr 2009 02:05:59 GMT > Steven D'Aprano wrote: > >> The demuxer can't be an iterator, since it needs to run through the >> entire collection. > > Then your demuxer obviously cannot handle infinite sequences. > >> def demux(it, n): >> collectors = [[] for i in xrange(n)] >> i = 0 >> for item in it: >> collectors[i].append(item) >> i = (i+1) % n >> return tuple([iter(x) for x in collectors]) > > But this one can: > > from collections import deque > from itertools import cycle, izip, count, islice > > def mux(*iterables): > for it in izip(*iterables): > for item in it: > yield item > > def demux(seq,n): > it = iter(seq) > Q = [deque() for i in xrange(n)] > CQ = cycle(Q) > def gen(D): > for x,C in izip(it,CQ): > C.appendleft(x) > while D: > yield D.pop() > while D: > yield D.pop() > return map(gen,Q) > > def test(): > a = count(10) > b = count(20) > c = count (30) > x = demux(mux(a,b,c),3) > for e in x: > print list(islice(e,0,10)) > > if __name__=='__main__': > test() > > P. > -- > http://mail.python.org/mailman/listinfo/python-list What was wrong with this one? def demux(iterable, n): return tuple(islice(it, i, None, n) for (i, it) in enumerate(tee(iterable, n))) From orsenthil at gmail.com Wed Apr 8 10:51:58 2009 From: orsenthil at gmail.com (O.R.Senthil Kumaran) Date: Wed, 8 Apr 2009 07:51:58 -0700 (PDT) Subject: CGIXMLRPCRequestHandler example References: Message-ID: On Apr 5, 12:24?pm, a... at pythoncraft.com (Aahz) wrote: > [posted & e-mailed, please respond on-group] There was some problem with the CGIXMLRPCRequestHandler code in the SimpleXMLRPC Server. It was not getting the length to read from the CONTENT_LENGTH environement variable ( as it the CGI server was doing). It was fixed during PyCon sprints: http://svn.python.org/view?view=rev&revision=70940 From gregcorradini at gmail.com Wed Apr 8 10:52:24 2009 From: gregcorradini at gmail.com (Greg Corradini) Date: Wed, 8 Apr 2009 07:52:24 -0700 (PDT) Subject: Simple CGI request and Python reply Message-ID: <22952274.post@talk.nabble.com> Hello, I'm trying to implement something very simple without using a Python WebFramework and I need some advice. I want to send a comma delimited string from the client to a server-side Python script. My initial plan was to use a JavaScript function (see below) called "makerequest" that creates a XMLHttpRequest object and GETs the output from a Python script. I've used this function before to field requests (in those cases the parameter "data" passed in the XMLHttpRequest.send() method is null). So I thought I could just pass some data in and be able to retrieve it with Python. I'm not sure how to do this. I've used forms with cgi/Python before. However, I don't want to use a form here. I want Python to handle the "data" variable being passed without looking for field names using .FieldStorage(). Can that be done? Maybe making a request is not the quickest route to do what I want. Ideas? function makerequest(serverPage,objID,data) { var obj = document.getElementById(objID); xmlhttp.open("GET",serverPage); xmlhttp.onreadystatechange = function() { if(xmlhttp.readyState == 4 && xmlhttp.status == 200) { obj.innerHTML = xmlhttp.responseText; } } xmlhttp.send(data); } -- View this message in context: http://www.nabble.com/Simple-CGI-request-and-Python-reply-tp22952274p22952274.html Sent from the Python - python-list mailing list archive at Nabble.com. From medmediani at gmail.com Wed Apr 8 11:08:48 2009 From: medmediani at gmail.com (Mohammed Mediani) Date: Wed, 8 Apr 2009 17:08:48 +0200 Subject: genetic algorithms in Python? In-Reply-To: References: Message-ID: <1aa8f3840904080808o405de471u908d768e86b1ff2c@mail.gmail.com> I have done something in this direction. I will be happy to share my experience. However, my code is not generic and needs many things to be manually introduced. My GA is standard (selection by roulette wheel or tournament, single point cross). Let me know if you are interested! On Wed, Apr 8, 2009 at 3:25 PM, Esmail wrote: > Hello, > > Anyone using Python for coding up genetic algorithms? If > so, would you share your favorite modules/libraries/tools? > > Thanks, > Esmail > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From sxn02 at yahoo.com Wed Apr 8 11:14:36 2009 From: sxn02 at yahoo.com (Sorin Schwimmer) Date: Wed, 8 Apr 2009 08:14:36 -0700 (PDT) Subject: Some test fail on my new Python 2.6 Message-ID: <428673.49902.qm@web56002.mail.re3.yahoo.com> Thanks for the pointers. Here are some answers: > Ok, so DBM wasn't built because it couldn't find the external symbol > 'dbm_firstkey'. I have no idea off the top of my head why that would > happen, but I don't think you really care at the moment since you are > trying to get tkinter working. If you do care we can come back to that. Although I can live without it, I do use sometime dbm. And then, again, there are other ways to accomplish the same, so it's not critical. >> building '_tkinter' extension >> gcc -pthread -fPIC -fno-strict-aliasing -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -DWITH_APPINIT=1 -DWITH_BLT=1 -I/usr/X11R6/include -I. -I/install/Python-2.6.1/./Include -I. -IInclude -I./Include -I/usr/local/include -I/install/Python-2.6.1/Include -I/install/Python-2.6.1 -c /install/Python-2.6.1/Modules/_tkinter.c -o build/temp.linux-i686-2.6/install/Python-2.6.1/Modules/_tkinter.o >> gcc -pthread -fPIC -fno-strict-aliasing -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -DWITH_APPINIT=1 -DWITH_BLT=1 -I/usr/X11R6/include -I. -I/install/Python-2.6.1/./Include -I. -IInclude -I./Include -I/usr/local/include -I/install/Python-2.6.1/Include -I/install/Python-2.6.1 -c /install/Python-2.6.1/Modules/tkappinit.c -o build/temp.linux-i686-2.6/install/Python-2.6.1/Modules/tkappinit.o >> gcc -pthread -shared build/temp.linux-i686-2.6/install/Python-2.6.1/Modules/_tkinter.o build/temp.linux-i686-2.6/install/Python-2.6.1/Modules/tkappinit.o -L/usr/X11R6/lib64 -L/usr/X11R6/lib -L/usr/local/lib -lBLT -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 > Now, this one is clear. It couldn't find libtk8.5.so when it tried to > use _tkinter.so. So either that symlink doesn't exist, or points to a > nonexistent library, or there's something wrong with your load library > path, so that python isn't finding it when it tries to do the load. > If you don't know what that last one means, check out 'man ldconfig'. # pwd /usr/local/lib # ls libpq.so.4 libtcl8.4.so libtclstub8.4.a libtk8.4.so libtkstub8.4.a python2.4 tcl8.4 tclConfig.sh tk8.5 libpq.so.4.0 libtcl8.5.so libtclstub8.5.a libtk8.5.so libtkstub8.5.a tcl8 tcl8.5 tk8.4 tkConfig.sh It's there! >> test_multiprocessing >> test_multiprocessing skipped -- OSError raises on RLock creation, see issue 3111! > According to the referenced bug, this may be a result of your not having > /dev/shm mounted, but in any case it is more a bug in the tests than a > bug in your system or setup. # mount /dev/hda3 on / type reiserfs (rw,noatime) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) udev on /dev type tmpfs (rw,nosuid) devpts on /dev/pts type devpts (rw) shm on /dev/shm type tmpfs (rw,noexec,nosuid,nodev) usbfs on /proc/bus/usb type usbfs (rw,devmode=0664,devgid=85) It's there, but if it is about a known bug in the test, maybe we should just ignore it? >> test_socket >> test test_socket failed -- Traceback (most recent call last): >> File "/install/Python-2.6.1/Lib/test/test_socket.py", line 474, in testSockName >> my_ip_addr = socket.gethostbyname(socket.gethostname()) >> gaierror: [Errno -2] Name or service not known > This probably means that you haven't properly defined 'localhost' > in your /etc/hosts file. # cat /etc/hosts # /etc/hosts # # This file describes a number of hostname-to-address # mappings for the TCP/IP subsystem. It is mostly # used at boot time, when no name servers are running. # On small systems, this file can be used instead of a # "named" name server. Just add the names, addresses # and any aliases to this file... # 127.0.0.1 localhost # IPV6 versions of localhost and co ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters ff02::3 ip6-allhosts There is a localhost definition. Is it not correct? >> test_sys >> test test_sys failed -- Traceback (most recent call last): >> File "/install/Python-2.6.1/Lib/test/test_sys.py", line 354, in test_43581 >> self.assert_(sys.__stdout__.encoding == sys.__stderr__.encoding) >> AssertionError > This one is puzzling. Did you modify 'site.py'? Or perhaps you > redirected stdout and stderr to different places and they > wound up with different encodings for some reason. In which > case that one would be a bug in the tests as well. No, I didn't, nor have I done anything with stdout, stderror. When I ran the test, I did a make test | tee test.rez, which in my mind should have been harmless. The output for individual test_httpservers was posted here: http://mail.python.org/pipermail/python-list/2009-April/708041.html Thanks again SxN __________________________________________________________________ Yahoo! Canada Toolbar: Search from anywhere on the web, and bookmark your favourite sites. Download it now http://ca.toolbar.yahoo.com. From iainking at gmail.com Wed Apr 8 11:14:46 2009 From: iainking at gmail.com (Iain King) Date: Wed, 8 Apr 2009 08:14:46 -0700 (PDT) Subject: Scraping a web page References: <9c8c445f0904070508p52e6011vaee4c5ce2b66d406@mail.gmail.com> Message-ID: <8bebfc39-5cd5-4be4-94a0-5c11db379906@t11g2000vbc.googlegroups.com> On Apr 7, 1:44?pm, Tim Chase wrote: > > f = urllib.urlopen("http://www.google.com") > > s = f.read() > > > It is working, but it's returning the source of the page. Is there anyway I > > can get almost a screen capture of the page? > > This is the job of a browser -- to render the source HTML. ?As > such, you'd want to look into any of the browser-automation > libraries to hook into IE, FireFox, Opera, or maybe using the > WebKit/KHTML control. ?You may then be able to direct it to > render the HTML into a canvas you can then treat as an image. > > Another alternative might be provided by some web-services that > will render a page as HTML with various browsers and then send > you the result. ?However, these are usually either (1) > asynchronous or (2) paid services (or both). > > -tkc WX can render html. From jhg at acm.org Wed Apr 8 11:28:49 2009 From: jhg at acm.org (Jim Garrison) Date: Wed, 08 Apr 2009 10:28:49 -0500 Subject: Python 3.0 online docs broken In-Reply-To: <49db6ffe$0$29393$4c5ecfc7@fe1.usenet.com> References: <49da837c$0$29401$4c5ecfc7@fe1.usenet.com> <94ae55e9-265c-4444-8bba-316001db83e1@r37g2000yqn.googlegroups.com> <49db6ffe$0$29393$4c5ecfc7@fe1.usenet.com> Message-ID: <49dcc2df$0$29421$4c5ecfc7@fe1.usenet.com> Jim Garrison wrote: > Ye Liu wrote: >> On Apr 6, 6:33 pm, Jim Garrison wrote: >>> I notice the online docs (at docs.python.org/3.0/index.html) were >>> updated today. It seems some of the top-level pages, like >>> Tutorial, "Using Python", "Language Reference" are truncated >>> after the first few paragraphs. >> >> Yea, same here. Hope it's going to be fixed soon. > > Still broken. I've emailed webmaster(at)python.org I'm still seeing only stubs for many of the doc pages at docs.python.org. For example http://docs.python.org/3.0/tutorial/index.html This page stops at the sentence "The Glossary is also worth going through." whereas it used to be followed by a hyperlinked table of contents for all the tutorial pages. I emailed the webmaster but have received no response. Am I the only one seeing this? From Tribulations at Paralleles.invalid Wed Apr 8 11:46:11 2009 From: Tribulations at Paralleles.invalid (TP) Date: Wed, 08 Apr 2009 17:46:11 +0200 Subject: how to get back an object from its id() value Message-ID: <3ftua6-s5k.ln1@rama.fbx.proxad.net> Hi everybody, I have a data structure (a tree) that has one constraint: I can only store strings in this data structure. To know if an object foo already exists in memory, I store "str(id(foo))" in the data structure. OK. But how do I get a usable reference from the id value? For example, if "foo" has a method "bar()", how can I call "foo.bar()" from "str(id(foo))" (say, 149466208). Thanks in advance, Julien -- python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.\ 9&1+,\'Z4(55l4('])" "When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong." (first law of AC Clarke) From esmail.js at gmail.com Wed Apr 8 11:49:57 2009 From: esmail.js at gmail.com (Esmail) Date: Wed, 08 Apr 2009 11:49:57 -0400 Subject: genetic algorithms in =?windows-1252?Q?Python=3F=3F?= Message-ID: <49DCC7A5.8070202@gmail.com> Hello Mohammed, Yes, that would great. While I am comfortable with GAs, I'm still rather inexperienced with Python so seeing some implementation examples would be very useful. Thanks, Esmail -- Date: Wed, 8 Apr 2009 17:08:48 +0200 Subject: Re: genetic algorithms in Python? From: medmediani at gmail.com To: ebonak at hotmail.com CC: python-list at python.org I have done something in this direction. I will be happy to share my experience. However, my code is not generic and needs many things to be manually introduced. My GA is standard (selection by roulette wheel or tournament, single point cross). Let me know if you are interested! On Wed, Apr 8, 2009 at 3:25 PM, Esmail wrote: Hello, Anyone using Python for coding up genetic algorithms? If so, would you share your favorite modules/libraries/tools? Thanks, Esmail From invalid at invalid Wed Apr 8 11:56:03 2009 From: invalid at invalid (Grant Edwards) Date: Wed, 08 Apr 2009 10:56:03 -0500 Subject: How to ignore white space changes using difflib? Message-ID: I'm trying to use difflib to compare strings ignoring changes to white-space (space/tab). According to the doc page, you can do this by specifying a "charjunk" parameter to filter out characters: charjunk: A function that accepts a character (a string of length 1), and returns if the character is junk, or false if not. The default is module-level function IS_CHARACTER_JUNK(), which filters out whitespace characters (a blank or tab; note: bad idea to include newline in this!). But, I simply can't get it to work. I get exactly the same results with or without white-space filtering: Here's my test program: #!/usr/bin/python import difflib d1 = ["this is string one","this is string two","this is string three"] d2 = ["this is string one","this is string two","this is string three"] def iswhite(c): return c in " \t" print "--------------------no filtering--------------------" delta = difflib.ndiff(d1,d2) for line in delta: print line print "----------------------------------------------------" print print "--------------------IS_CHARACTER_JUNK--------------------" delta = difflib.ndiff(d1,d2,charjunk=difflib.IS_CHARACTER_JUNK) for line in delta: print line print "----------------------------------------------------" print print "--------------------iswhite--------------------" delta = difflib.ndiff(d1,d2,charjunk=iswhite) for line in delta: print line print "----------------------------------------------------" And here's the output: --------------------no filtering-------------------- this is string one - this is string two ? -- + this is string two ? + + this is string three ---------------------------------------------------- --------------------IS_CHARACTER_JUNK-------------------- this is string one - this is string two ? -- + this is string two ? + + this is string three ---------------------------------------------------- --------------------iswhite-------------------- this is string one - this is string two ? -- + this is string two ? + + this is string three ---------------------------------------------------- What am I doing wrong? -- Grant Edwards grante Yow! I'll show you MY at telex number if you show me visi.com YOURS ... From avinashrude at gmail.com Wed Apr 8 12:03:20 2009 From: avinashrude at gmail.com (Avi) Date: Wed, 8 Apr 2009 09:03:20 -0700 (PDT) Subject: Floor value in math operators Message-ID: Hi, This will be a very simple question to ask all the awesome programmers here: How can I get answer in in decimals for such a math operator: 3/2 I get 1. I want to get 1.5 Thanks in advance, Avi From google at mrabarnett.plus.com Wed Apr 8 12:05:30 2009 From: google at mrabarnett.plus.com (MRAB) Date: Wed, 08 Apr 2009 17:05:30 +0100 Subject: how to get back an object from its id() value In-Reply-To: <3ftua6-s5k.ln1@rama.fbx.proxad.net> References: <3ftua6-s5k.ln1@rama.fbx.proxad.net> Message-ID: <49DCCB4A.10104@mrabarnett.plus.com> TP wrote: > Hi everybody, > > I have a data structure (a tree) that has one constraint: I can only store > strings in this data structure. > > To know if an object foo already exists in memory, I store "str(id(foo))" in > the data structure. > OK. > > But how do I get a usable reference from the id value? > For example, if "foo" has a method "bar()", how can I call "foo.bar()" > from "str(id(foo))" (say, 149466208). > You could create a dict with the string as the key and the object as the value. From clp2 at rebertia.com Wed Apr 8 12:08:40 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 8 Apr 2009 09:08:40 -0700 Subject: Floor value in math operators In-Reply-To: References: Message-ID: <50697b2c0904080908x60115a7ahadc5abbf4efc8191@mail.gmail.com> On Wed, Apr 8, 2009 at 9:03 AM, Avi wrote: > Hi, > > This will be a very simple question to ask all the awesome programmers > here: > > How can I get answer in in decimals for such a math operator: > > 3/2 > > I get 1. I want to get 1.5 Add the following line to the top of your program (or use Python 3.0+): from __future__ import division To get integer division when this is enabled, use the integer division operator, // e.g. x = 3//2 #x=1 Cheers, Chris -- I have a blog: http://blog.rebertia.com From debatem1 at gmail.com Wed Apr 8 12:09:03 2009 From: debatem1 at gmail.com (CTO) Date: Wed, 8 Apr 2009 09:09:03 -0700 (PDT) Subject: how to get back an object from its id() value References: <3ftua6-s5k.ln1@rama.fbx.proxad.net> Message-ID: <34bc23cf-7e13-4f07-95c4-8eb70d9a9c97@r33g2000yqn.googlegroups.com> > But how do I get a usable reference from the id value? > For example, if "foo" has a method "bar()", how can I call "foo.bar()" > from "str(id(foo))" (say, 149466208). can you just use a weakref instead? It is certainly cleaner than trying to store id's, since an id is only guaranteed to be unique for the lifetime of the object in question, and the only way I know of correlating id's to objects involves some C- level trickery. for completeness- the trickery: http://www.friday.com/bbum/2007/08/24/python-di/ From invalid at invalid Wed Apr 8 12:11:27 2009 From: invalid at invalid (Grant Edwards) Date: Wed, 08 Apr 2009 11:11:27 -0500 Subject: How to ignore white space changes using difflib? References: Message-ID: On 2009-04-08, Grant Edwards wrote: > I'm trying to use difflib to compare strings ignoring changes > to white-space (space/tab). According to the doc page, you > can do this by specifying a "charjunk" parameter to filter out > characters: > > charjunk: A function that accepts a character (a string of > length 1), and returns if the character is junk, or false if > not. The default is module-level function > IS_CHARACTER_JUNK(), which filters out whitespace characters > (a blank or tab; note: bad idea to include newline in > this!). Apparently that "filtering out" characters doesn't mean that they're ignored when doing the comparison. (A bit of a "WTF?" if you ask me). After some more googling, it appears that I'm far from the first person who interpreted "filtered out" as "ignored when comparing lines". I'd submit a fix for the doc page, but you apparently have to be a lot smarter than me to figure out what "filters out" means in this context. I guess I can collapse all whitespace sequences, do the diff on the collapsed lines, and them map the results back to the original lines. :/ > What am I doing wrong? Reading the doc page, apparently. ;) -- Grant Edwards grante Yow! Not SENSUOUS ... only at "FROLICSOME" ... and in visi.com need of DENTAL WORK ... in PAIN!!! From deets at nospam.web.de Wed Apr 8 12:13:13 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 08 Apr 2009 18:13:13 +0200 Subject: Floor value in math operators References: Message-ID: <7440n2F10r0ajU1@mid.uni-berlin.de> Avi wrote: > Hi, > > This will be a very simple question to ask all the awesome programmers > here: > > How can I get answer in in decimals for such a math operator: > > 3/2 > > I get 1. I want to get 1.5 You don't say which python-version you have. Depending on that, the answer is different. In python2.5 and before, dividing two integers will always return an integer. So for your problem, you need to do 3.0 / 2.0 At least one of the involved numbers must be a float. However, from python2.5 the division operator behavior changed if desired - if you do >>> from __future__ import division >>> 3 / 2 1.5 >>> 3 // 2 1 you get what you expect. The old division operator is still available, as // In python2.6 and 3.x, the new behavior is standard. Diez From bellman at lysator.liu.se Wed Apr 8 12:13:38 2009 From: bellman at lysator.liu.se (Thomas Bellman) Date: Wed, 8 Apr 2009 16:13:38 +0000 (UTC) Subject: named pipe and Linux References: Message-ID: Cameron Simpson wrote: > On 07Apr2009 10:08, akineko wrote: >| I'm trying to use named pipes to fuse a Python program and a C >| program. >| One side creates pipes using os.mkfifo() and both sides use the same >| named pipes (one side reads, another side writes). The read side uses >| select.select() to wait for incoming messages and read the message >| when select.select() says it is ready. >| The length of the message is unknown to the read side. > That's a serious flaw in the message protocol. >| I cannot use file.read() because it will block waiting for an EOF. >| I cannot use file.readline() because how many lines have arrived is >| unknown. >| So, I needed to use os.read() with the exact number of characters to >| read. > No! > You should use os.read() with the maximum size of a message. > It _should_ return with the number of bytes in the message, provided the > C program writes messages with a single OS-level write() call. No! That's still broken. You can't know if the writer has managed to write one or several messages onto the pipe. And if the messages are large, you aren't guaranteed that the OS won't split up the data into multiple segments that only become available to the reader one or a few at a time. You *need* to use a protocol where it is possible to determine the message boundaries. You can do that by: - Using messages of a fixed size. - Terminate each message with an octet sequence that cannot occur within a message. For example, a linefeed without a backslash before it (and you would probably want a way to escape the backslash, in case you want to end a message with a backslash). - Have small header of a fixed size at the start of each message, that includes the length of the message in octets. -- Thomas Bellman, Lysator Computer Club, Link??ping University, Sweden "Life IS pain, highness. Anyone who tells ! bellman @ lysator.liu.se differently is selling something." ! Make Love -- Nicht Wahr! From gherron at islandtraining.com Wed Apr 8 12:14:38 2009 From: gherron at islandtraining.com (Gary Herron) Date: Wed, 08 Apr 2009 09:14:38 -0700 Subject: how to get back an object from its id() value In-Reply-To: <3ftua6-s5k.ln1@rama.fbx.proxad.net> References: <3ftua6-s5k.ln1@rama.fbx.proxad.net> Message-ID: <49DCCD6E.90108@islandtraining.com> TP wrote: > Hi everybody, > > I have a data structure (a tree) that has one constraint: I can only store > strings in this data structure. > > To know if an object foo already exists in memory, I store "str(id(foo))" in > the data structure. > OK. > > But how do I get a usable reference from the id value? > For example, if "foo" has a method "bar()", how can I call "foo.bar()" > from "str(id(foo))" (say, 149466208). > Short answer: You can't! Longer answer: You still can't, but you may be able to work around it: (1) Build up a dictionary in parallel to the structure: idMapper[id(foo)] = foo and later, index with idMapper[int(idString)] to get foo back (2) Get a different data structure. This one is clearly not satisfying your needs. (3) Pickle (or marshal or serialize as it's also called) your object into a (perhaps) long string to store. When the string is retrieved, unpickle to reconstruct an equivalent object. This new version of the original foo may or may not be adequate for your use. Gary Herron > Thanks in advance, > > Julien > > From debatem1 at gmail.com Wed Apr 8 12:17:07 2009 From: debatem1 at gmail.com (CTO) Date: Wed, 8 Apr 2009 09:17:07 -0700 (PDT) Subject: how to get back an object from its id() value References: <3ftua6-s5k.ln1@rama.fbx.proxad.net> Message-ID: > You could create a dict with the string as the key and the object as the > value. This will create a strong reference to the object, which is (I'm assuming) undesired behavior. From Tribulations at Paralleles.invalid Wed Apr 8 12:20:56 2009 From: Tribulations at Paralleles.invalid (TP) Date: Wed, 08 Apr 2009 18:20:56 +0200 Subject: how to get back an object from its id() value References: <3ftua6-s5k.ln1@rama.fbx.proxad.net> Message-ID: <8gvua6-8u9.ln1@rama.fbx.proxad.net> MRAB wrote: > You could create a dict with the string as the key and the object as the > value. Thanks. But it implies an additional data structure: a dictionnary. I don't know what is the best: * using an additional dict and maintaining it * or using the "di" module proposed by CTO If "di" is reliable, it seems a good solution for my initial constraint which is the impossibility to store anything but strings in my data structure. -- python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.\ 9&1+,\'Z4(55l4('])" "When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong." (first law of AC Clarke) From rdmurray at bitdance.com Wed Apr 8 12:22:54 2009 From: rdmurray at bitdance.com (R. David Murray) Date: Wed, 8 Apr 2009 16:22:54 +0000 (UTC) Subject: genetic algorithms in =?windows-1252?Q?Python=3F=3F?= References: <49DCC7A5.8070202@gmail.com> Message-ID: Esmail wrote: > Hello Mohammed, > > Yes, that would great. While I am comfortable with GAs, > I'm still rather inexperienced with Python so seeing some > implementation examples would be very useful. A google for 'python genetic algorithms' turns up a number of interesting hits. I also remember seeing at least one package announced on python-announce that referred to genetic algorithms, so you might check the archives of that mailing list as well. -- R. David Murray http://www.bitdance.com From mail at microcorp.co.za Wed Apr 8 12:28:50 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Wed, 8 Apr 2009 18:28:50 +0200 Subject: Python 3.0 online docs broken References: <49da837c$0$29401$4c5ecfc7@fe1.usenet.com><94ae55e9-265c-4444-8bba-316001db83e1@r37g2000yqn.googlegroups.com><49db6ffe$0$29393$4c5ecfc7@fe1.usenet.com> <49dcc2df$0$29421$4c5ecfc7@fe1.usenet.com> Message-ID: <004001c9b867$1a075440$0d00a8c0@Hendrik> "Jim Garrison" > Jim Garrison wrote: > > Ye Liu wrote: > >> On Apr 6, 6:33 pm, Jim Garrison wrote: > >>> I notice the online docs (at docs.python.org/3.0/index.html) were > >>> updated today. It seems some of the top-level pages, like > >>> Tutorial, "Using Python", "Language Reference" are truncated > >>> after the first few paragraphs. > >> > >> Yea, same here. Hope it's going to be fixed soon. > > > > Still broken. I've emailed webmaster(at)python.org > > I'm still seeing only stubs for many of the doc pages at > docs.python.org. For example > > http://docs.python.org/3.0/tutorial/index.html > > This page stops at the sentence "The Glossary is also worth going > through." whereas it used to be followed by a hyperlinked table of > contents for all the tutorial pages. > When I looked now (6:30 pm in South Africa) the links were back. The last one is representation error and it was all right when I clicked on it. - Hendrik From deets at nospam.web.de Wed Apr 8 12:29:12 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 08 Apr 2009 18:29:12 +0200 Subject: Floor value in math operators References: <7440n2F10r0ajU1@mid.uni-berlin.de> Message-ID: <7441l1F1234c4U1@mid.uni-berlin.de> > > In python2.6 and 3.x, the new behavior is standard. Apparently that is nonsense - it seems to be not standard for 2.6. Which Makes sense I guess. Diez From debatem1 at gmail.com Wed Apr 8 13:02:46 2009 From: debatem1 at gmail.com (CTO) Date: Wed, 8 Apr 2009 10:02:46 -0700 (PDT) Subject: how to get back an object from its id() value References: <3ftua6-s5k.ln1@rama.fbx.proxad.net> <8gvua6-8u9.ln1@rama.fbx.proxad.net> Message-ID: <36521bb8-6e23-44c8-b64a-ba147e62d528@k38g2000yqh.googlegroups.com> > I don't know what is the best: > * using an additional dict and maintaining it It works, but as you say, is somewhat inelegant. > * or using the "di" module proposed by CTO Let me be clear: I am not proposing that you use it. It *does* do what you ask- but what you are asking is, all by itself, not a good idea. The dict is a vastly superior- and standard- solution to the problem of mapping one object onto another. > If "di" is reliable, it seems a good solution for my initial constraint > which is the impossibility to store anything but strings in my data > structure. di is not reliable. Its author says so, and took a lot of heat for not saying so in Hollywood-sign letters, lit on fire and a thousand feet tall. If all you are worried about is storing a string, how about UserString? Notice the difference: >>> from collections import UserString >>> import weakref >>> weakref.ref("ABC") Traceback (most recent call last): File "", line 1, in TypeError: cannot create weak reference to 'str' object >>> weakref.ref(UserString("ABC")) This way you don't have to maintain a dictionary, only store your string once, and don't have to hack and kludge your way around id mappings. From joeygartin at gmail.com Wed Apr 8 13:03:58 2009 From: joeygartin at gmail.com (joeygartin) Date: Wed, 8 Apr 2009 10:03:58 -0700 (PDT) Subject: asynchronous python call Message-ID: <637028a0-58b9-4912-896d-2b17e13414fe@q16g2000yqg.googlegroups.com> Complete newbie, so forgive my improper use of Python terminology. I am working with Django and I have to send off a number of emails after a person has filled out a form. What I want is to make a call that goes off to form and send the emails and redirect the user to another view. I have this working now, but I noticed it is a bit slow. So I was hoping there was a way to spin off the process and redirect the user to the page while the emails are being formed and sent vs. making the page hang while it is done. Thank you! From mensanator at aol.com Wed Apr 8 13:04:39 2009 From: mensanator at aol.com (Mensanator) Date: Wed, 8 Apr 2009 10:04:39 -0700 (PDT) Subject: Python syntax References: <7dd228af-e549-444d-8623-11e95185123c@y9g2000yqg.googlegroups.com> Message-ID: On Apr 7, 11:39?pm, Lawrence D'Oliveiro wrote: > In message , Steve > > Holden wrote: > > Lawrence D'Oliveiro wrote: > > >> In message <7dd228af- > >> e549-444d-8623-11e951851... at y9g2000yqg.googlegroups.com>, janus99 wrote: > > >>> I messed around with my own comp (windos xp) command prompt ... > > >> Try a Linux command prompt. You'll learn a bit more that way. > > > Great advice when Linux is available, but somewhat fatuous for a Windows > > user. > > How hard is it to install Cygwin It's actually quite difficult. Even if you think ahead and ask that the various compilers are to be installed, that won't include various utilities like "make" (which are part of a different install component). It takes a lot of trial and error trying to get, say, GMP to compile. > or download a live CD? From debatem1 at gmail.com Wed Apr 8 13:04:51 2009 From: debatem1 at gmail.com (CTO) Date: Wed, 8 Apr 2009 10:04:51 -0700 (PDT) Subject: how to get back an object from its id() value References: <3ftua6-s5k.ln1@rama.fbx.proxad.net> <8gvua6-8u9.ln1@rama.fbx.proxad.net> <36521bb8-6e23-44c8-b64a-ba147e62d528@k38g2000yqh.googlegroups.com> Message-ID: Correction: the UserString will be dead on the final line. When I typed it in I had a strong reference still hanging around. From demirbaris at gmail.com Wed Apr 8 13:05:41 2009 From: demirbaris at gmail.com (Baris Demir) Date: Wed, 08 Apr 2009 19:05:41 +0200 Subject: 3D plotting in a GUI In-Reply-To: <49DBC794.6070005@gmail.com> References: <49DBA162.3010907@gmail.com> <49DBC794.6070005@gmail.com> Message-ID: <49DCD965.7050405@gmail.com> Stef Mientki wrote: > Baris Demir wrote: >> Hi all, >> >> I need to develop a GUI for some scientific data processing >> operations and this GUI should work well with a 3D plotting module, >> also with NumPy and SciPy for sure. I made a search about packages >> but, there are plenty of these modules available. What kind of a >> package or a package of packages might be the best way for this >> work. BTW I do not want to use MayaVi. It is too much actually. > What do you mean by "too much actually" ? > What's the GUI you had in mind ? > What kind of scientific data processing do you've in mind ? > > cheers, > Stef I am quite experienced about python programming but this is going to be my first GUI design. I need to draw some 2D graphs (also capability for 3D graphs might be very delicious) and do some mathematical operations on them like adding, substracting, smoothing, integration, detecting peak points and marking them, fourier transformations and etc. on WindowsXP. And then I want to convert this python code into a .exe by using py2exe. I said Mayavi is too much for me because I thought that it would be more likeyl to have problems while using a big package like MayaVi with py2exe. >> >> Thanks in advance. >> BD >> >> -- >> http://mail.python.org/mailman/listinfo/python-list > > -- > http://mail.python.org/mailman/listinfo/python-list > From dns4 at cornell.edu Wed Apr 8 13:08:32 2009 From: dns4 at cornell.edu (David Smith) Date: Wed, 08 Apr 2009 13:08:32 -0400 Subject: Floor value in math operators In-Reply-To: References: Message-ID: Avi wrote: > Hi, > > This will be a very simple question to ask all the awesome programmers > here: > > How can I get answer in in decimals for such a math operator: > > 3/2 > > I get 1. I want to get 1.5 > > Thanks in advance, > Avi I'm going to assume your operands are variables instead of numeric literals. Why not consider doing a type conversion to float or Decimal and then perform the division? --David From rdmurray at bitdance.com Wed Apr 8 13:15:26 2009 From: rdmurray at bitdance.com (R. David Murray) Date: Wed, 8 Apr 2009 17:15:26 +0000 (UTC) Subject: Some test fail on my new Python 2.6 References: <428673.49902.qm@web56002.mail.re3.yahoo.com> Message-ID: Sorin Schwimmer wrote: > > Thanks for the pointers. > > Here are some answers: > > > Ok, so DBM wasn't built because it couldn't find the external symbol > > 'dbm_firstkey'. I have no idea off the top of my head why that would > > happen, but I don't think you really care at the moment since you are > > trying to get tkinter working. If you do care we can come back to that. > > Although I can live without it, I do use sometime dbm. And then, again, > there are other ways to accomplish the same, so it's not critical. OK, so we'll come back to it later. > >> *** WARNING: renaming "_tkinter" since importing it failed: libtk8.5.so: cannot open shared object file: No such file or directory > > > Now, this one is clear. It couldn't find libtk8.5.so when it tried to > > use _tkinter.so. So either that symlink doesn't exist, or points to a > > nonexistent library, or there's something wrong with your load library > > path, so that python isn't finding it when it tries to do the load. > > If you don't know what that last one means, check out 'man ldconfig'. > > # pwd > /usr/local/lib > # ls > libpq.so.4 libtcl8.4.so libtclstub8.4.a libtk8.4.so libtkstub8.4.a python2.4 tcl8.4 tclConfig.sh tk8.5 > libpq.so.4.0 libtcl8.5.so libtclstub8.5.a libtk8.5.so libtkstub8.5.a tcl8 tcl8.5 tk8.4 tkConfig.sh > > It's there! Well, there is _something_ there. Maybe it's a symbolic link that doesn't point anywhere? Do ls -l, and/or 'file' on it just to check. The above message is what you would get if it were a symbolic link that didn't point anywhere valid. Oh, and make sure /usr/local/lib is in your /etc/ld.so.conf file. If it isn't, then that's your problem. If you need to update that file I'm pretty sure you need to run ldconfig afterwards. > >> test_multiprocessing > >> test_multiprocessing skipped -- OSError raises on RLock creation, see issue 3111! > > According to the referenced bug, this may be a result of your not having > > /dev/shm mounted, but in any case it is more a bug in the tests than a > > bug in your system or setup. > > # mount > /dev/hda3 on / type reiserfs (rw,noatime) > proc on /proc type proc (rw) > sysfs on /sys type sysfs (rw) > udev on /dev type tmpfs (rw,nosuid) > devpts on /dev/pts type devpts (rw) > shm on /dev/shm type tmpfs (rw,noexec,nosuid,nodev) > usbfs on /proc/bus/usb type usbfs (rw,devmode=0664,devgid=85) > > It's there, but if it is about a known bug in the test, maybe we should just ignore it? Yeah. Or download 2.6.2c1 and see if that has the same problem. The bug doesn't say that it was fixed in 2.6.2, and the conditional skip is still there in the test, but I know Jesse was doing work in that area, so it might get past it on 2.6.2. On the other hand, if you aren't using multiprocessing you don't need to worry about it. > >> test_socket > >> test test_socket failed -- Traceback (most recent call last): > >> File "/install/Python-2.6.1/Lib/test/test_socket.py", line 474, in testSockName > >> my_ip_addr = socket.gethostbyname(socket.gethostname()) > >> gaierror: [Errno -2] Name or service not known > > This probably means that you haven't properly defined 'localhost' > > in your /etc/hosts file. > > # cat /etc/hosts > # /etc/hosts > # > # This file describes a number of hostname-to-address > # mappings for the TCP/IP subsystem. It is mostly > # used at boot time, when no name servers are running. > # On small systems, this file can be used instead of a > # "named" name server. Just add the names, addresses > # and any aliases to this file... > # > > 127.0.0.1 localhost > # IPV6 versions of localhost and co > ::1 ip6-localhost ip6-loopback > fe00::0 ip6-localnet > ff00::0 ip6-mcastprefix > ff02::1 ip6-allnodes > ff02::2 ip6-allrouters > ff02::3 ip6-allhosts > > > There is a localhost definition. Is it not correct? Yes, but...I just did the functions on my own machine and got this: Python 2.6.1 (r261:67515, Mar 26 2009, 08:03:17) [GCC 4.1.2 (Gentoo 4.1.2)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import socket >>> socket.gethostname() 'partner' >>> socket.gethostbyname('partner') '127.0.0.1' So I suspect that you need to add your hostname to the 'localhost' line in order to get that test to pass. Mine looks like this: 127.0.0.1 partner.bitdance.com partner localhost I'm inclined to call that a bug in the tests. You could file a bug report if you feel so moved :) > >> test_sys > >> test test_sys failed -- Traceback (most recent call last): > >> File "/install/Python-2.6.1/Lib/test/test_sys.py", line 354, in test_43581 > >> self.assert_(sys.__stdout__.encoding == sys.__stderr__.encoding) > >> AssertionError > > This one is puzzling. Did you modify 'site.py'? Or perhaps you > > redirected stdout and stderr to different places and they > > wound up with different encodings for some reason. In which > > case that one would be a bug in the tests as well. > > No, I didn't, nor have I done anything with stdout, stderror. When I ran the test, I did a make test | tee test.rez, which in my mind should have been harmless. Ah, but that does redirect stdout and not stderr. stdout goes into the pipe, and thence to 'tee', which writes one copy of the input lines to stdout and one to the file. Not sure what python would see as stdout at that point, but I wouldn't be surprised if that was the source of the problem (and that means that the test is a little fragile). If you rerun that test in verbose mode without the pipe into tee, does it pass? > The output for individual test_httpservers was posted here: > http://mail.python.org/pipermail/python-list/2009-April/708041.html Hmm. These puzzle me. The first one is failing because for some reason the code is able to read a file that has been chmoded to have no access. Did you run the tests as root, by any chance? The rest of them might be failing because 'sys.executable' isn't pointing at the actual instance of the python command for some reason. If you do 'import sys; print sys.executable' what does it say? But I'd think you'd see other failures in that case, so it might be something else. The bottom line is that the little script files that the test writes are not returning anything for some reason. If you want to debug this one further we may have to start sticking prints into the tests and things like that. -- R. David Murray http://www.bitdance.com PS: 'issue 3111' means issue number 3111 on the Python bug tracker at http://bugs.python.org. From duncan.booth at invalid.invalid Wed Apr 8 13:16:13 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 8 Apr 2009 17:16:13 GMT Subject: How to ignore white space changes using difflib? References: Message-ID: Grant Edwards wrote: > Apparently that "filtering out" characters doesn't mean that > they're ignored when doing the comparison. (A bit of a "WTF?" > if you ask me). After some more googling, it appears that I'm > far from the first person who interpreted "filtered out" as > "ignored when comparing lines". I'd submit a fix for the doc > page, but you apparently have to be a lot smarter than me to > figure out what "filters out" means in this context. So far as I can see from looking at the code: Once if you have identified one block of lines as having been replaced by another the matcher can then give you additional information by marking up the changes within each line. However it only makes sense to do that if the lines are still somewhat similar. 'charjunk' is used to remove junk characters before scanning the lines within a replacement block and the most similar lines (if they are sufficiently similar) are then chosen for this extra step of comparing the character changes within the line. Here's an example. If I do this: >>> print ''.join(Differ().compare('one\ntwo\nthree\n'.splitlines(1), 'one\nwot\ntoo\nthree\n'.splitlines(1))) one - two ? - + wot ? + + too three The comparison detected that "two" was replaced by 2 lines "wot" and "too". It decided the first of these was the best match for the original line so it shows character level difference between the original and the first replacement line. >>> print ''.join(Differ(charjunk=lambda c:c=='w') .compare('one\ntwo\nthree\n'.splitlines(1), 'one\nwot\ntoo\nthree\n'.splitlines(1))) one + wot - two ? ^ + too ? ^ three This time we told the system that we don't care about 'w' in either the original or replacement text. That means instead of seeing which of "wot" and "too" is closest to "two" it looks to see which of "ot" and "too" is closest to "to". "ot" has two changes but "too" only has one, so this time it does the detailed comparison between the original line and the second line of the output. N.B. The junk function is only used to decide which lines to use for the detailed comparison: the original lines are still used for the comparison itself. From pataphor at gmail.com Wed Apr 8 13:21:27 2009 From: pataphor at gmail.com (pataphor) Date: Wed, 8 Apr 2009 19:21:27 +0200 Subject: more fun with iterators (mux, demux) References: <20090408110652.52931472@hyperspace> Message-ID: <20090408192127.3c7ad871@hyperspace> On Wed, 08 Apr 2009 10:51:19 -0400 Neal Becker wrote: > What was wrong with this one? > > def demux(iterable, n): > return tuple(islice(it, i, None, n) for (i, it) in > enumerate(tee(iterable, n))) Nothing much, I only noticed after posting that this one handles infinite sequences too. For smallish values of n it is acceptable. P. From stef.mientki at gmail.com Wed Apr 8 13:28:29 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Wed, 08 Apr 2009 19:28:29 +0200 Subject: 3D plotting in a GUI In-Reply-To: <49DCD965.7050405@gmail.com> References: <49DBA162.3010907@gmail.com> <49DBC794.6070005@gmail.com> <49DCD965.7050405@gmail.com> Message-ID: <49DCDEBD.50305@gmail.com> Baris Demir wrote: > Stef Mientki wrote: >> Baris Demir wrote: >>> Hi all, >>> >>> I need to develop a GUI for some scientific data processing >>> operations and this GUI should work well with a 3D plotting module, >>> also with NumPy and SciPy for sure. I made a search about packages >>> but, there are plenty of these modules available. What kind of a >>> package or a package of packages might be the best way for this >>> work. BTW I do not want to use MayaVi. It is too much actually. >> What do you mean by "too much actually" ? >> What's the GUI you had in mind ? >> What kind of scientific data processing do you've in mind ? >> >> cheers, >> Stef > > I am quite experienced about python programming but this is going to > be my first GUI design. I need to draw some 2D graphs (also capability > for 3D graphs might be very delicious) and do some mathematical > operations on them like adding, substracting, smoothing, integration, > detecting peak points and marking them, fourier transformations and > etc. on WindowsXP. And then I want to convert this python code into a > .exe by using py2exe. I said Mayavi is too much for me because I > thought that it would be more likeyl to have problems while using a > big package like MayaVi with py2exe. Two obvious choices are wxPython + MatPlotLib and/or VPython or PyQt, depends on what license you're after. Maybe interesting to know that a number of guys are creating MatLab-like environements, search the scipy newsgroup. You can also find some m-file to numpy converters (in case you might have m-files already). And I can assure you, there are 2 packages which will give you trouble with py2exe: numpy and matplotlib. btw if you choose wxPython, download the demo first, and you might even be interested in a simpler way to create GUIs, http://mientki.ruhosting.nl/data_www/pylab_works/pw_gui_support.html good luck, Stef From aaron.hildebrandt at gmail.com Wed Apr 8 13:40:28 2009 From: aaron.hildebrandt at gmail.com (Aaron Scott) Date: Wed, 8 Apr 2009 10:40:28 -0700 (PDT) Subject: Module caching References: <8350f62e-c620-4abc-90dc-ef3af60494f6@3g2000yqk.googlegroups.com> <3a0704ea-703a-445f-804d-73f363ca9376@c11g2000yqj.googlegroups.com> <657ed616-ca5f-4f45-b056-9db877f76beb@v15g2000yqn.googlegroups.com> <3b9b538a-0048-4730-9681-618b540c3926@z15g2000yqm.googlegroups.com> <64d56ba2-7ce5-42f3-a59a-f94f4cadda7b@f41g2000pra.googlegroups.com> Message-ID: <9719c687-3de8-42cb-960c-56f70469a2d6@v6g2000vbb.googlegroups.com> > Anyway, this person also posted on mod_python list. One of the things > I highlighted there was that mod_python for some configurations is > multithreaded and as such they may not be properly protecting > variables if they are storing them at global scope. They haven't > responded to any comments about it on mod_python list. They were also > told to read: > > ?http://www.dscpl.com.au/wiki/ModPython/Articles/TheProcessInterpreter... > > Graham Thanks, Graham. Sorry I didn't respond -- it's crunch time here, so I was pulled off the project for a couple days to help with something else. I just didn't have time to catch up. In the end, we decided to convert everything from mod_python to CGI, which ended up getting us the functionality we were looking for. For development, MaxRequestsPerChild was already set to 1, but this didn't solve any of the variable caching issues. Aaron From lists at cheimes.de Wed Apr 8 14:03:31 2009 From: lists at cheimes.de (Christian Heimes) Date: Wed, 08 Apr 2009 20:03:31 +0200 Subject: how to get back an object from its id() value In-Reply-To: <8gvua6-8u9.ln1@rama.fbx.proxad.net> References: <3ftua6-s5k.ln1@rama.fbx.proxad.net> <8gvua6-8u9.ln1@rama.fbx.proxad.net> Message-ID: TP wrote: > If "di" is reliable, it seems a good solution for my initial constraint > which is the impossibility to store anything but strings in my data > structure. 'di' is dangerous and not reliable. When the original object is freed, then the memory address may be used by another Python object, point to an invalid Python object or some random bytes. You can easily mess up or seg fault the Python interpreter. 'di' is an evil debugging hack that may proof useful as last resort. Do *not* use anything like 'di' in production code. Christian From tino at wildenhain.de Wed Apr 8 14:08:16 2009 From: tino at wildenhain.de (Tino Wildenhain) Date: Wed, 08 Apr 2009 20:08:16 +0200 Subject: asynchronous python call In-Reply-To: <637028a0-58b9-4912-896d-2b17e13414fe@q16g2000yqg.googlegroups.com> References: <637028a0-58b9-4912-896d-2b17e13414fe@q16g2000yqg.googlegroups.com> Message-ID: <49DCE810.2000204@wildenhain.de> Hi, joeygartin wrote: > Complete newbie, so forgive my improper use of Python terminology. > > I am working with Django and I have to send off a number of emails > after a person has filled out a form. What I want is to make a call > that goes off to form and send the emails and redirect the user to > another view. I have this working now, but I noticed it is a bit > slow. So I was hoping there was a way to spin off the process and > redirect the user to the page while the emails are being formed and > sent vs. making the page hang while it is done. You best bet would be something like Zope's asynchronous mail handler which is simply a queue Folder where you write the mail to be send and having an outside process completely independend to scan for new mail to be send out from time to time. If you want to speed up the pickup time you could have the mail sender process open a socket (UDP for example) where you can ping once you wrote out the mails to be send. Regards Tino From zapwireDASHgroups at yahoo.com Wed Apr 8 14:32:33 2009 From: zapwireDASHgroups at yahoo.com (Joel Koltner) Date: Wed, 8 Apr 2009 11:32:33 -0700 Subject: Q: "Best" book for teaching References: <8e3d0032-5e9f-44c2-9380-1d2383552046@u5g2000vbc.googlegroups.com> Message-ID: <756Dl.102831$UR4.35979@en-nntp-09.dc1.easynews.com> "Lawrence D'Oliveiro" wrote in message news:grhq75$ebn$2 at lust.ihug.co.nz... > I thought that a good introduction might be to show them how HTML works, and > progress from there to embedding little bits of JavaScript. > > Nothing to do with Python I know, but might be a possibility. If you want to emphasize web usage, I'd be tempted to show them a bit of HTML and then introduce them to CGI-bin scripts... written in Python. Something like Django might be fun to show as well, although such "full featured" content management systems are really a course unto themselves. (And if you're used Python scripts on the server and played with, e.g., MySQL a little, it'll be clear that things like Django are just greatly expanded versions of what they're doing themselves.) ---Joel From amon at vnl.com Wed Apr 8 14:47:46 2009 From: amon at vnl.com (Dale Amon) Date: Wed, 8 Apr 2009 19:47:46 +0100 Subject: Computed attribute names Message-ID: <20090408184745.GM17975@vnl.com> There are a number of things which I have been used to doing in other OO languages which I have not yet figured out how to do in Python, the most important of which is passing method names as args and inserting them into method calls. Here are two cases I have been trying to figure out for a current project. The first is passing methods to dispatcher methods. In pseudocode, something like this: def dispatcher(self,methodname): self.obj1.methodname() self.obj2.methodname() and another case is selecting behavior of an object by setting a type string, with pseudo code like this: self.IBM029 = re.compile([^acharset] self.IBM026 = re.compile([^anothercharset] self.type = "IBM029" errs = self.(self.type).findall(aCardImage) I have yet to find any way to do either, although it appears I could do some of it using a long and roundabout call string using __dict__. What is the Python dialect for this sort of runtime OO? -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 196 bytes Desc: Digital signature URL: From heidihannah at mac.com Wed Apr 8 14:56:12 2009 From: heidihannah at mac.com (heidi taynton) Date: Wed, 08 Apr 2009 14:56:12 -0400 Subject: numpy.where Message-ID: <148739635947045949601137627602034810513-Webmail@me.com> Hi, I'm fairly new to programming and am having a probably cutting my arrays. I have two different 1d arrays, one of time, and the second energy. I want to cut both arrays of time min<=time<=max . I've created a 2d array with [time,energy] and I believe numpy.where is what I am looking for, but haven't been able to get the conditions in right for it to work. all help appreciated hi From marduk at letterboxes.org Wed Apr 8 14:59:20 2009 From: marduk at letterboxes.org (Albert Hopkins) Date: Wed, 08 Apr 2009 14:59:20 -0400 Subject: Computed attribute names In-Reply-To: <20090408184745.GM17975@vnl.com> References: <20090408184745.GM17975@vnl.com> Message-ID: <1239217160.6698.47.camel@centar> On Wed, 2009-04-08 at 19:47 +0100, Dale Amon wrote: > There are a number of things which I have been used > to doing in other OO languages which I have not yet > figured out how to do in Python, the most important > of which is passing method names as args and inserting > them into method calls. Here are two cases I have been > trying to figure out for a current project. > > The first is passing methods to dispatcher methods. In > pseudocode, something like this: > > def dispatcher(self,methodname): > self.obj1.methodname() > self.obj2.methodname() The built-in getattr() function: def dispatcher(self, methodname): getattr(self.obj1, methodname)() getattr(self.obj2, methodname)() > and another case is selecting behavior of an object by > setting a type string, with pseudo code like this: > > self.IBM029 = re.compile([^acharset] > self.IBM026 = re.compile([^anothercharset] > self.type = "IBM029" > errs = self.(self.type).findall(aCardImage) Same: errs = getattr(self, self.type).findall(aCardImage) From paul at subsignal.org Wed Apr 8 15:03:00 2009 From: paul at subsignal.org (paul) Date: Wed, 08 Apr 2009 21:03:00 +0200 Subject: Computed attribute names In-Reply-To: <20090408184745.GM17975@vnl.com> References: <20090408184745.GM17975@vnl.com> Message-ID: Dale Amon schrieb: > There are a number of things which I have been used > to doing in other OO languages which I have not yet > figured out how to do in Python, the most important > of which is passing method names as args and inserting > them into method calls. Here are two cases I have been > trying to figure out for a current project. > > The first is passing methods to dispatcher methods. In > pseudocode, something like this: > > def dispatcher(self,methodname): > self.obj1.methodname() > self.obj2.methodname() > I'd say you can use: method = getattr(self.obj1, 'methodname') method() It will raise AttributeError if 'methodname' is not found or the (optional) third argument to getattr() > and another case is selecting behavior of an object by > setting a type string, with pseudo code like this: > > self.IBM029 = re.compile([^acharset] > self.IBM026 = re.compile([^anothercharset] > self.type = "IBM029" > errs = self.(self.type).findall(aCardImage) same here, or use a class/module-level dict like symbol_table = { 'IBM029': re.compile([^...]), 'IBM026': re.compile([^...]) } and symbol_table[self.type].findall(something) cheers Paul > > -- > http://mail.python.org/mailman/listinfo/python-list From tkpmep at hotmail.com Wed Apr 8 15:11:25 2009 From: tkpmep at hotmail.com (tkpmep at hotmail.com) Date: Wed, 8 Apr 2009 12:11:25 -0700 (PDT) Subject: Q: "Best" book for teaching References: <8e3d0032-5e9f-44c2-9380-1d2383552046@u5g2000vbc.googlegroups.com> Message-ID: <50d671e1-3755-484c-998f-c9a3562457fa@n20g2000vba.googlegroups.com> I taught myself Python from Python Programming for the Absolute Beginner by Michael Dawson (which has been mentioned above) and with lots of help from the friendly members of this group, but there's now a free e-book titled Snake Wrangling for Kids by Jason Briggs. You can view it at http://www.briggs.net.nz/log/writing/snake-wrangling-for-kids/. There are versions for Windows, Mac and Linux, though it is focused on Python 2.x. Thomas Philips From amon at vnl.com Wed Apr 8 15:12:41 2009 From: amon at vnl.com (Dale Amon) Date: Wed, 8 Apr 2009 20:12:41 +0100 Subject: Computed attribute names In-Reply-To: References: <20090408184745.GM17975@vnl.com> Message-ID: <20090408191241.GN17975@vnl.com> On Wed, Apr 08, 2009 at 09:03:00PM +0200, paul wrote: > I'd say you can use: Thanks. I could hardly ask for a faster response on a HowTo than this! -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 196 bytes Desc: Digital signature URL: From martin at v.loewis.de Wed Apr 8 15:32:30 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Wed, 08 Apr 2009 21:32:30 +0200 Subject: Painful?: Using the ast module for metaprogramming In-Reply-To: References: <49d9942d$0$3662$9b622d9e@news.freenet.de> Message-ID: <49dcfbce$0$2354$9b622d9e@news.freenet.de> > I see how it avoids needing to look at the parent node in general, but > if we were compiling by recursively descending through the AST, then > we would know whether Name's would be loads or stores by the time we > got to them (we would already had to have visited an encompassing > assignment or expression) Python's compiler does recursively descend. However, it is still more convenient to allow uniform processing of expressions, rather than having parameters passed down into the visitor functions. > except in the case of your a = b = c > expression, which I'm curious how Python handles. The natural answer > is for assignment to be an expression (so b = c returns the new value > of b). But Python doesn't do that, so then I'd expect we'd have some > third ast.LoadAndStore() option for b, but examining ast.parse's > behavior it looks like it chooses Store... No, the value of b is never read in this assignment. I'm not quite sure whether you are aware what the *actual* semantics of this assignment is; it is equivalent to tmp = c a = tmp b = tmp (so the assignment to a happens first), and neither a nor b is being read. So it's simple stores into both a and b. >>> -Why can't orelse for ast.If and ast.While default to empty []? >> You want to store None? That would be a type error; orelse is >> specified as "stmt*". So it must be a list. > > A list is actually what I want, an empty one. The problem is that > ast.While and ast.If's constructors default to the opposite, > orelse=None. Same with keywords and args for ast.Call. Admittedly, > adding orelse=[] to the constructor calls isn't terribly burdensome, > but it does make already obfuscated looking AST mangling code even > worse. Ah, that could be fixed. Please contribute a patch. >>> -Why can't I eval a functiondef to get back a function object? >> Because a definition is not an expression. You can only eval >> expressions. > > I understand that if function definitions were expressions, because of > the whitespace syntax there wouldn't be a way to express an assignment > to such an expression. But, why would it be problematic to let them be > expressions anyway? This would be a significant change to the compiler, which would have to learn what bytecodes to compile it to. Feel free to discuss this on python-ideas. Regards, Martin From bazwal at googlemail.com Wed Apr 8 15:46:20 2009 From: bazwal at googlemail.com (Baz Walter) Date: Wed, 08 Apr 2009 20:46:20 +0100 Subject: Testing the new version of Psyco Message-ID: <49DCFF0C.1070103@googlemail.com> hello i recently tried out this new version of psyco... http://www.voidspace.org.uk/python/weblog/arch_d7_2009_03_14.shtml#e1063 ...because of the new support for generators. the above link says "To use and test generators, create preferences.py, following the instructions in setup.py" - except there's nothing obvious in setup.py that refers to generators. anyway, i created a preferences.py file (with PSYCO_DEBUG = 1) and ran psycobench against python-2.5/psyco-1.6 and python-2.6/psyco-2.0 with the following results (output has been cropped slightly): # start output [benchmark]$ python2.5 psycobench.py -m time_generators time_anyall Running new timings with original psyco send call loop 1000 plain: 3.30 psyco: 3.29 ratio: 1.00 send and loop 1000 plain: 3.28 psyco: 3.27 ratio: 1.00 send just many times plain: 1.28 psyco: 0.56 ratio: 2.29 * iterate just many times plain: 0.67 psyco: 0.57 ratio: 1.19 * call next just many times plain: 0.85 psyco: 0.63 ratio: 1.35 * all_bool_genexp plain: 2.03 psyco: 2.31 ratio: 0.88 all_bool_listcomp plain: 2.94 psyco: 1.01 ratio: 2.91 all_genexp plain: 1.71 psyco: 1.97 ratio: 0.87 all_listcomp plain: 2.70 psyco: 0.73 ratio: 3.71 all_loop plain: 1.09 psyco: 0.08 ratio: 13.15 any_bool_genexp plain: 2.03 psyco: 2.45 ratio: 0.83 any_bool_listcomp plain: 2.89 psyco: 0.97 ratio: 2.99 any_genexp plain: 1.74 psyco: 1.84 ratio: 0.95 any_listcomp plain: 2.65 psyco: 0.72 ratio: 3.69 any_loop plain: 1.08 psyco: 0.08 ratio: 13.10 [benchmark]$ python2.6 psycobench.py -m time_generators time_anyall Running new timings with python2.6/site-packages/psyco/_psyco.so send call loop 1000 plain: 2.85 psyco: 0.04 ratio: 67.90 send and loop 1000 plain: 2.85 psyco: 0.04 ratio: 65.00 send just many times plain: 1.17 psyco: 0.87 ratio: 1.35 iterate just many times plain: 0.64 psyco: 0.86 ratio: 0.74 call next just many times plain: 0.77 psyco: 0.87 ratio: 0.89 all_bool_genexp plain: 1.87 psyco: 1.98 ratio: 0.95 all_bool_listcomp plain: 2.54 psyco: 0.66 ratio: 3.84 all_genexp plain: 1.69 psyco: 1.77 ratio: 0.95 all_listcomp plain: 2.38 psyco: 0.52 ratio: 4.55 all_loop plain: 1.07 psyco: 0.08 ratio: 14.24 any_bool_genexp plain: 1.87 psyco: 1.99 ratio: 0.94 any_bool_listcomp plain: 2.57 psyco: 0.68 ratio: 3.76 any_genexp plain: 1.74 psyco: 1.82 ratio: 0.95 any_listcomp plain: 2.47 psyco: 0.53 ratio: 4.66 any_loop plain: 1.07 psyco: 0.07 ratio: 14.65 # end output with the obvious exception of the the first two tests for psyco v2, the results (for generators) seem a little underwhelming. in fact, psyco v1 does significantly better on three of the tests (marked with an asterisk) and for the others there's not much difference. this makes me wonder whether i enabled generators properly in psyco v2 when i compiled it. has anybody else tried out this new version of psyco and got better overall results for generators? also, those first two results for v2 look really odd - what could explain the huge difference relative to the other generator tests for v2? (p.s. i re-ran the tests and got very similar results) -- Baz Walter From aahz at pythoncraft.com Wed Apr 8 15:52:11 2009 From: aahz at pythoncraft.com (Aahz) Date: 8 Apr 2009 12:52:11 -0700 Subject: asynchronous python call References: <637028a0-58b9-4912-896d-2b17e13414fe@q16g2000yqg.googlegroups.com> Message-ID: In article <637028a0-58b9-4912-896d-2b17e13414fe at q16g2000yqg.googlegroups.com>, joeygartin wrote: > >I am working with Django and I have to send off a number of emails >after a person has filled out a form. What I want is to make a call >that goes off to form and send the emails and redirect the user to >another view. I have this working now, but I noticed it is a bit >slow. So I was hoping there was a way to spin off the process and >redirect the user to the page while the emails are being formed and >sent vs. making the page hang while it is done. The simplest solution is to create an external program and use the subprocess module to call it. You could also use fork() directly, but that's more complicated. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "...string iteration isn't about treating strings as sequences of strings, it's about treating strings as sequences of characters. The fact that characters are also strings is the reason we have problems, but characters are strings for other good reasons." --Aahz From benjamin.kaplan at case.edu Wed Apr 8 15:54:12 2009 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Wed, 8 Apr 2009 15:54:12 -0400 Subject: Python 3.0 online docs broken In-Reply-To: <49dcc2df$0$29421$4c5ecfc7@fe1.usenet.com> References: <49da837c$0$29401$4c5ecfc7@fe1.usenet.com> <94ae55e9-265c-4444-8bba-316001db83e1@r37g2000yqn.googlegroups.com> <49db6ffe$0$29393$4c5ecfc7@fe1.usenet.com> <49dcc2df$0$29421$4c5ecfc7@fe1.usenet.com> Message-ID: On Wed, Apr 8, 2009 at 11:28 AM, Jim Garrison wrote: > Jim Garrison wrote: > >> Ye Liu wrote: >> >>> On Apr 6, 6:33 pm, Jim Garrison wrote: >>> >>>> I notice the online docs (at docs.python.org/3.0/index.html) were >>>> updated today. It seems some of the top-level pages, like >>>> Tutorial, "Using Python", "Language Reference" are truncated >>>> after the first few paragraphs. >>>> >>> >>> Yea, same here. Hope it's going to be fixed soon. >>> >> >> Still broken. I've emailed webmaster(at)python.org >> > > I'm still seeing only stubs for many of the doc pages at > docs.python.org. For example > > http://docs.python.org/3.0/tutorial/index.html > > This page stops at the sentence "The Glossary is also worth going through." > whereas it used to be followed by a hyperlinked table of > contents for all the tutorial pages. > > I emailed the webmaster but have received no response. > > Am I the only one seeing this? Nope. I have the same problem. > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From sxn02 at yahoo.com Wed Apr 8 16:04:18 2009 From: sxn02 at yahoo.com (Sorin Schwimmer) Date: Wed, 8 Apr 2009 13:04:18 -0700 (PDT) Subject: Some test fail on my new Python 2.6 Message-ID: <895637.13039.qm@web56003.mail.re3.yahoo.com> Hello again, /usr/local/lib is in /etc/ld.so.conf. The files are real. However, creating symlinks in /usr/lib targeting the libraries in /usr/local/lib, then recompiling, solved the tkinter problem. So, the conclusion is that only /usr/lib is consulted for the tcl/tk libraries. multiprocessing - do I need it? Probably not. I can fork() a new process, so I can manage. I don't know what am I loosing without multiprocessing. socket was solved as suggested, by adding the host name in /etc/hosts, like this: 127.0.0.1 localhost dsr-1 sys doesn't fail anymore after recompilation (have no clue why). htttpservers is still failing the same. Everything is done as root. I'll move on with make install. Thanks for your help; I'm progressing :-) SxN __________________________________________________________________ Instant Messaging, free SMS, sharing photos and more... Try the new Yahoo! Canada Messenger at http://ca.beta.messenger.yahoo.com/ From wilson at afn.org Wed Apr 8 16:52:09 2009 From: wilson at afn.org (JBW) Date: Wed, 08 Apr 2009 20:52:09 GMT Subject: Q: "Best" book for teaching References: <8e3d0032-5e9f-44c2-9380-1d2383552046@u5g2000vbc.googlegroups.com> Message-ID: On Mon, 06 Apr 2009 07:37:19 -0700, grkuntzmd wrote: > What would be a good book to use as the text for the course? "Python Programming: An Introduction to Computer Science" Franklin, Beedle & Associates, by little Johny Zelle. Accept no substitues! Jim From jcd at sdf.lonestar.org Wed Apr 8 17:02:30 2009 From: jcd at sdf.lonestar.org (J. Cliff Dyer) Date: Wed, 08 Apr 2009 17:02:30 -0400 Subject: extract Infobox contents In-Reply-To: References: <1239104778.10734.2.camel@mctell> Message-ID: <1239224550.31419.8.camel@aalcdl07> On Wed, 2009-04-08 at 01:57 +0100, Rhodri James wrote: > On Tue, 07 Apr 2009 12:46:18 +0100, J. Clifford Dyer > wrote: > > > On Mon, 2009-04-06 at 23:41 +0100, Rhodri James wrote: > >> On Mon, 06 Apr 2009 23:12:14 +0100, Anish Chapagain > >> wrote: > >> > >> > Hi, > >> > I was trying to extract wikipedia Infobox contents which is in format > >> > like given below, from the opened URL page in Python. > >> > > >> > {{ Infobox Software > >> > | name = Bash > [snip] > >> > | latest release date = {{release date|mf=yes|2009|02|20}} > >> > | programming language = [[C (programming language)|C]] > >> > | operating system = [[Cross-platform]] > >> > | platform = [[GNU]] > >> > | language = English, multilingual ([[gettext]]) > >> > | status = Active > [snip some more] > >> > }} //upto this line > >> > > >> > I need to extract all data between {{ Infobox ...to }} > > [snip still more] > > >> You end up with 'infoboxes' containing a list of all the infoboxes > >> on the page, each held as a list of the lines of their content. > >> For safety's sake you really should be using regular expressions > >> rather than 'startswith', but I leave that as an exercise for the > >> reader :-) > >> > > > > I agree that startswith isn't the right option, but for matching two > > constant characters, I don't think re is necessary. I'd just do: > > > > if '}}' in line: > > pass > > > > Then, as the saying goes, you only have one problem. > > That would be the problem of matching lines like: > > | latest release date = {{release date|mf=yes|2009|02|20}} > > would it? :-) > That's the one. > A quick bit of timing suggests that: > > if line.lstrip().startswith("}}"): > pass > > is what we actually want. > Indeed. Thanks. From venkatsanaka at gmail.com Wed Apr 8 17:11:37 2009 From: venkatsanaka at gmail.com (venkat sanaka) Date: Thu, 9 Apr 2009 02:41:37 +0530 Subject: "str object is not callable" error Message-ID: HIi everyone. i was using python/c api to call a python function from c and I know the name of the function which i want to call.Is there any way to do that?? This is the method i tried... for eg:This is the python function i wants to call. >>>def add(x): ... return x+10 This is my code in C: PyObject *result = NULL; int arg; PyObject *arglist; arg = 123; my_callback = "add"; arglist = Py_BuildValue("(i)", arg); result = PyObject_CallObject(my_callback, arglist); Py_DECREF(arglist); return result; I was getting a error like "str object is not callable".From the error i came to know that i was assigning "add" as a string which caused this error.Then how to make it a callable object?? Regards Venkat -------------- next part -------------- An HTML attachment was scrubbed... URL: From semanticist at gmail.com Wed Apr 8 17:12:56 2009 From: semanticist at gmail.com (Miles) Date: Wed, 8 Apr 2009 17:12:56 -0400 Subject: more fun with iterators (mux, demux) In-Reply-To: <20090408192127.3c7ad871@hyperspace> References: <20090408110652.52931472@hyperspace> <20090408192127.3c7ad871@hyperspace> Message-ID: On Wed, Apr 8, 2009 at 1:21 PM, pataphor wrote: > On Wed, 08 Apr 2009 10:51:19 -0400 Neal Becker wrote: > >> What was wrong with this one? >> >> def demux(iterable, n): >> ? ? return tuple(islice(it, i, None, n) for (i, it) in >> enumerate(tee(iterable, n))) > > Nothing much, I only noticed after posting that this one handles > infinite sequences too. For smallish values of n it is acceptable. I assume that "smallish values of n" refers to the fact that itertools.tee places items into every generator's internal deque, which islice then skips over, whereas your version places items only into the deque of the generator that needs it. However, for small n, the tee-based solution has the advantage of having most of the work done in C instead of in Python generator functions; in my limited benchmarking, the point where your version becomes faster is somewhere around n=65. -Miles From superprad at gmail.com Wed Apr 8 17:15:58 2009 From: superprad at gmail.com (PK) Date: Wed, 8 Apr 2009 17:15:58 -0400 Subject: nested looping Message-ID: <5b0681910904081415j5e260f53t77b00d70e720a44e@mail.gmail.com> So I'm trying to see whats the cleanest way to do this: I have a checklist = [ax, bx, by, cy ..] (a combination of a,b,c with x and y, either both on one) allist = [a,b,c,....] xlist = [x, y, ..] now I wanna loop through alist and xlist and see if the combination exists in checklist so something like, for alpha in alist: for xy in xlist: if alpha+xy not in checklist: missing.append(alpha) now the problem is I want to include alpha in missing list only if none of the combinations from xlist with alpha are in checklist. The above will exclude the possibility where ax doesn't exist but ay or az does exist. Hope There is a cleaner way to accomplish this. Thanks in advance, PK From rch.astra at googlemail.com Wed Apr 8 17:28:36 2009 From: rch.astra at googlemail.com (rch.astra at googlemail.com) Date: Wed, 8 Apr 2009 14:28:36 -0700 (PDT) Subject: Character strings / Python 3.0 C API Message-ID: Hi everybody, I'm doing some experiments with the python 3.0 C API, and I'm having some problems for obtaining character strings. In python 2.X APIs, a function called PyString_AsString was available. This function provided a C string (char*) given a PyObject. But, in python 3.0 API there is no such a function, and they mention that it has been substituted by PyUnicode* functions. The issue is that I still don't get how to use these functions to obtain char strings in C! This is one of the things I try to do: PyThreadState* state = PyThreadState_GET(); PyInterpreterState* interp = state->interp; int numModules = PyDict_Size(interp->modules); PyObject* moduleKeys = PyDict_Keys(interp->modules); int idx=0; for(idx=0; idxmodules, key ); char* theKeyName = PySys_WriteStdout("Module '%s'\n", theKeyName); } I was not able to obtain theKeyName, knowing that the "key" PyObject is in fact a unicode string containing the character string I want... Could anybody help me? How can I, in general, obtain char* strings from a corresponding PyObject? What should I use for ""? Thank you very much in advance, Cheers RC From tjreedy at udel.edu Wed Apr 8 17:33:36 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 08 Apr 2009 17:33:36 -0400 Subject: genetic algorithms in Python? In-Reply-To: References: Message-ID: Esmail wrote: > Hello, > > Anyone using Python for coding up genetic algorithms? If > so, would you share your favorite modules/libraries/tools? Search 'Python genetic algorithm' on Google or elsewhere. From martin at v.loewis.de Wed Apr 8 17:35:41 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Wed, 08 Apr 2009 23:35:41 +0200 Subject: Character strings / Python 3.0 C API In-Reply-To: References: Message-ID: <49dd18ae$0$30901$9b622d9e@news.freenet.de> > PyObject* key = PyList_GetItem(moduleKeys,idx); > PyObject* module = PyDict_GetItem( interp->modules, key ); > char* theKeyName = > PySys_WriteStdout("Module '%s'\n", theKeyName); > } > I was not able to obtain theKeyName, knowing that the "key" PyObject > is in fact a unicode string containing the character string I want... Use PyUnicode_AsUTF8String. > Could anybody help me? How can I, in general, obtain char* strings > from a corresponding PyObject? What should I use for ""? In general, this is much more difficult. If the object is an integer (say), or a file object - what is the char* that you want to get? You would need to call PyObject_Str first, getting str() (or repr()) of the object, and then use PyUnicode_AsUTF8String (releasing the _Str result afterwards). Regards, Martin From ebonak at hotmail.com Wed Apr 8 17:36:07 2009 From: ebonak at hotmail.com (Esmail) Date: Wed, 08 Apr 2009 17:36:07 -0400 Subject: genetic algorithms in Python?? In-Reply-To: References: <49DCC7A5.8070202@gmail.com> Message-ID: R. David Murray wrote: > Esmail wrote: >> Hello Mohammed, >> >> Yes, that would great. While I am comfortable with GAs, >> I'm still rather inexperienced with Python so seeing some >> implementation examples would be very useful. > > A google for 'python genetic algorithms' turns up a number > of interesting hits. Agreed .. I did that and came up with a number of hits, but the "problem" :-) with Python is a plethora of choices, so I wasn't quite sure which of the finds might be the most promising/useful. Nothing like a testimony from someone who uses or has used a tool. > I also remember seeing at least one package announced on > python-announce that referred to genetic algorithms, > so you might check the archives of that mailing list > as well. Yes, that's a good idea, thanks, Esmail From ebonak at hotmail.com Wed Apr 8 17:38:04 2009 From: ebonak at hotmail.com (Esmail) Date: Wed, 08 Apr 2009 17:38:04 -0400 Subject: genetic algorithms in Python? In-Reply-To: References: Message-ID: Terry Reedy wrote: > Esmail wrote: >> Hello, >> >> Anyone using Python for coding up genetic algorithms? If >> so, would you share your favorite modules/libraries/tools? > > Search 'Python genetic algorithm' on Google or elsewhere. Hi Terry, I did that first, and I came up with a number of hits. The "problem" with Python is that there's so much available and it's sometimes hard to decide what to select, so I was looking for some folks who had use some of the tools and could share their experiences. Esmail ps: the "problem" is a good thing to have all things considered :-) From emma.li at student.uni-tuebingen.de Wed Apr 8 17:51:22 2009 From: emma.li at student.uni-tuebingen.de (Emma Li) Date: Wed, 08 Apr 2009 23:51:22 +0200 Subject: Decompression with zlib Message-ID: <20090408235122.42079qs39fsz25p6@webmail.uni-tuebingen.de> Hello, I'm trying to do compression/decompression of stuff with zlib, and I just don't get it... Here is an example. I assume that dec should be "a", but it isn't. dec turns out to be an empty string, and I don't understand why... =================== import zlib compressor = zlib.compressobj(6) # level decompressor = zlib.decompressobj() # wbits cmpr = compressor.compress("a") print "compressed", len(cmpr) dec = decompressor.decompress(cmpr) print "decompressed", len(dec) # unconsumed tail and unused data print "decompressor.unconsumed_tail", decompressor.unconsumed_tail ##print "decompressor.unused_data", decompressor.unused_data dec += decompressor.flush() print "after flushing", len(dec) =================== Any help is appreciated. Emma From tjreedy at udel.edu Wed Apr 8 17:51:53 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 08 Apr 2009 17:51:53 -0400 Subject: nested looping In-Reply-To: <5b0681910904081415j5e260f53t77b00d70e720a44e@mail.gmail.com> References: <5b0681910904081415j5e260f53t77b00d70e720a44e@mail.gmail.com> Message-ID: PK wrote: > So I'm trying to see whats the cleanest way to do this: > > I have a > > checklist = [ax, bx, by, cy ..] (a combination of a,b,c with x and y, > either both on one) Since you will be repeatedly looking for items in checklist, I suggest making it a set instead. > allist = [a,b,c,....] > xlist = [x, y, ..] > > now I wanna loop through alist and xlist and see if the combination > exists in checklist > > so something like, > > for alpha in alist: > for xy in xlist: > if alpha+xy not in checklist: > missing.append(alpha) This will append alpha multiple times. Not what you want. > now the problem is I want to include alpha in missing list only if > none of the combinations from xlist with alpha are in checklist. for xy in xlist: if alpha+xy in checklist: break else: missing.append(alpha) # or missing.add(alpha) if missing a set tjr From python at rcn.com Wed Apr 8 17:58:12 2009 From: python at rcn.com (Raymond Hettinger) Date: Wed, 8 Apr 2009 14:58:12 -0700 (PDT) Subject: more fun with iterators (mux, demux) References: <20090408110652.52931472@hyperspace> <20090408192127.3c7ad871@hyperspace> Message-ID: <032559d5-7d83-4464-afcb-17e9fd010cbe@c36g2000yqn.googlegroups.com> [Miles] > I assume that "smallish values of n" refers to the fact that > itertools.tee places items into every generator's internal deque, > which islice then skips over, whereas your version places items only > into the deque of the generator that needs it. The pure python equivalent listed in the docs uses separate deques for each subiterator, but the actual C implementation of itertools.tee() only has one internal deque that is shared by all of the sub-iterators. Raymond From rch.astra at googlemail.com Wed Apr 8 17:58:36 2009 From: rch.astra at googlemail.com (rch.astra at googlemail.com) Date: Wed, 8 Apr 2009 14:58:36 -0700 (PDT) Subject: Character strings / Python 3.0 C API References: <49dd18ae$0$30901$9b622d9e@news.freenet.de> Message-ID: <6d0c5aed-a220-4287-8e36-b7f0ed20dd81@v6g2000vbb.googlegroups.com> Sorry but it did not help... I tried this: PyObject* key = PyList_GetItem(moduleKeys,idx); char* theKeyName = PyUnicode_AsUTF8String( key ); And this, just in case: PyObject* key = PyList_GetItem(moduleKeys,idx); char* theKeyName = PyUnicode_AsUTF8String( PyObject_Str (key) ); (Although it should work in the first case since key is suposed to be a string already, the module name). In all the cases, I obtained an empty string when using theKeyName: PySys_WriteStdout("Module '%s'\n", theKeyName); The issue is that all PyUnicode* functions are returning PyObjects. PyString_AsString return value was char*. Is there any real equivalent of this function? Thanks again. Best regards, RC On 8 abr, 23:35, "Martin v. L?wis" wrote: > > ? ? ? ? PyObject* key = PyList_GetItem(moduleKeys,idx); > > ? ? ? ? PyObject* module = PyDict_GetItem( interp->modules, key ); > > ? ? ? ? char* theKeyName = > > ? ? ? ? PySys_WriteStdout("Module '%s'\n", theKeyName); > > ? ? } > > I was not able to obtain theKeyName, knowing that the "key" PyObject > > is in fact a unicode string containing the character string I want... > > Use PyUnicode_AsUTF8String. > > > Could anybody help me? How can I, in general, obtain char* strings > > from a corresponding PyObject? What should I use for ""? > > In general, this is much more difficult. If the object is an integer > (say), or a file object - what is the char* that you want to get? > You would need to call PyObject_Str first, getting str() (or repr()) > of the object, and then use PyUnicode_AsUTF8String (releasing > the _Str result afterwards). > > Regards, > Martin From martin at v.loewis.de Wed Apr 8 18:05:35 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Thu, 09 Apr 2009 00:05:35 +0200 Subject: Character strings / Python 3.0 C API In-Reply-To: <6d0c5aed-a220-4287-8e36-b7f0ed20dd81@v6g2000vbb.googlegroups.com> References: <49dd18ae$0$30901$9b622d9e@news.freenet.de> <6d0c5aed-a220-4287-8e36-b7f0ed20dd81@v6g2000vbb.googlegroups.com> Message-ID: <49dd1faf$0$30649$9b622d9e@news.freenet.de> > The issue is that all PyUnicode* functions are returning PyObjects. > PyString_AsString return value was char*. Is there any real equivalent > of this function? Ah, right. PyString_AsUTF8String returns a bytes object, to which you need to apply PyBytes_AsString to. At the end, you need to decref the bytes object. Regards, Martin From mccredie at gmail.com Wed Apr 8 18:07:04 2009 From: mccredie at gmail.com (Matimus) Date: Wed, 8 Apr 2009 15:07:04 -0700 (PDT) Subject: nested looping References: Message-ID: <59988f8d-17bb-4833-b0af-ff4148bcc022@v28g2000vbb.googlegroups.com> On Apr 8, 2:15?pm, PK wrote: > So I'm trying to see whats the cleanest way to do this: > > I have a > > checklist = [ax, bx, by, cy ?..] (a combination of a,b,c with x and y, > either both on one) > > allist = [a,b,c,....] > xlist = [x, y, ..] > > now I wanna loop through alist and xlist and see if the combination > exists in checklist > > so something like, > > for alpha in alist: > ? ? for xy in xlist: > ? ? ? ? if alpha+xy not in checklist: > ? ? ? ? ? ? missing.append(alpha) > > now the problem is I want to include alpha in missing list only if > none of the combinations from xlist with alpha are in checklist. > > The above will exclude the possibility where ax doesn't exist but ay > or az does exist. > > Hope There is a cleaner way to accomplish this. > > Thanks in advance, > PK That code doesn't look too bad. It is fairly readable. There are several ways to accomplish this though. If you aren't interested in duplicate values or order then sets would be a good option. something like this: missing = set(alpha + xy for alpha in alist for xy in xlist).difference (checklist) if you are using 2.6 you could also make use of the product generator in itertools: from itertools import product missing = set(''.join(p) for p in product(alist, xlist)).difference (checklist) If you could adapt your code to use tuples instead of strings you could do it like this: missing = set(product(alist, xlist)).difference(checklist) Matt From rch.astra at googlemail.com Wed Apr 8 18:08:59 2009 From: rch.astra at googlemail.com (rch.astra at googlemail.com) Date: Wed, 8 Apr 2009 15:08:59 -0700 (PDT) Subject: Character strings / Python 3.0 C API References: <49dd18ae$0$30901$9b622d9e@news.freenet.de> <6d0c5aed-a220-4287-8e36-b7f0ed20dd81@v6g2000vbb.googlegroups.com> <49dd1faf$0$30649$9b622d9e@news.freenet.de> Message-ID: Thank you very much for your help Martin, now I got it. Cheers, RC On 9 abr, 00:05, "Martin v. L?wis" wrote: > > The issue is that all PyUnicode* functions are returning PyObjects. > > PyString_AsString return value was char*. Is there any real equivalent > > of this function? > > Ah, right. PyString_AsUTF8String returns a bytes object, to which you > need to apply PyBytes_AsString to. At the end, you need to decref the > bytes object. > > Regards, > Martin From kyrie at uh.cu Wed Apr 8 18:12:50 2009 From: kyrie at uh.cu (Luis Alberto Zarrabeitia Gomez) Date: Wed, 08 Apr 2009 18:12:50 -0400 Subject: nested looping In-Reply-To: <5b0681910904081415j5e260f53t77b00d70e720a44e@mail.gmail.com> References: <5b0681910904081415j5e260f53t77b00d70e720a44e@mail.gmail.com> Message-ID: <1239228770.49dd21623769c@mail.uh.cu> Quoting PK : > So I'm trying to see whats the cleanest way to do this: > > I have a > > checklist = [ax, bx, by, cy ..] (a combination of a,b,c with x and y, > either both on one) > > allist = [a,b,c,....] > xlist = [x, y, ..] > [...] > now the problem is I want to include alpha in missing list only if > none of the combinations from xlist with alpha are in checklist. This is untested: for alpha in alist: for xy in xlist: if alpha+xy in checklist: break else: missing.append(alpha) (note that the "else" is for the "for", not the "if") You could also try the any/all functions from python2.5: for alpha in alist: if not any(alpha + xy in checklist for xy in xlist): missing.append(alpha) Or the really not recommended one-liner: missing = [alpha for alpha in alist if not any(alpha + xy in checklist for xy in xlist)] (remember, all of this is untested) -- 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 ldo at geek-central.gen.new_zealand Wed Apr 8 18:42:13 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Thu, 09 Apr 2009 10:42:13 +1200 Subject: Q: "Best" book for teaching References: <8e3d0032-5e9f-44c2-9380-1d2383552046@u5g2000vbc.googlegroups.com> <756Dl.102831$UR4.35979@en-nntp-09.dc1.easynews.com> Message-ID: In message <756Dl.102831$UR4.35979 at en-nntp-09.dc1.easynews.com>, Joel Koltner wrote: > If you want to emphasize web usage, I'd be tempted to show them a bit of > HTML and then introduce them to CGI-bin scripts... written in Python. Yes, and show them how to watch /var/log/apache2/error.log to find their errors. :) mod_python could also be fun. From superprad at gmail.com Wed Apr 8 18:45:37 2009 From: superprad at gmail.com (PK) Date: Wed, 8 Apr 2009 18:45:37 -0400 Subject: nested looping In-Reply-To: <1239228770.49dd21623769c@mail.uh.cu> References: <5b0681910904081415j5e260f53t77b00d70e720a44e@mail.gmail.com> <1239228770.49dd21623769c@mail.uh.cu> Message-ID: <5b0681910904081545w7f04ec95p69b9586d743bc194@mail.gmail.com> Excellent! Thanks all. Since I need to have this running on python 2.4 as well, I think I'll choose, for alpha in alist: for xy in xlist: if alpha+xy in checklist: break else: missing.append(alpha) jus tested and worked like a charm. Appreciate your help!! On Wed, Apr 8, 2009 at 6:12 PM, Luis Alberto Zarrabeitia Gomez wrote: > > Quoting PK : > > > So I'm trying to see whats the cleanest way to do this: > > > > I have a > > > > checklist = [ax, bx, by, cy ..] (a combination of a,b,c with x and y, > > either both on one) > > > > allist = [a,b,c,....] > > xlist = [x, y, ..] > > > [...] > > now the problem is I want to include alpha in missing list only if > > none of the combinations from xlist with alpha are in checklist. > > This is untested: > > for alpha in alist: > for xy in xlist: > if alpha+xy in checklist: > break > else: > missing.append(alpha) > > (note that the "else" is for the "for", not the "if") > > You could also try the any/all functions from python2.5: > > for alpha in alist: > if not any(alpha + xy in checklist for xy in xlist): > missing.append(alpha) > > > Or the really not recommended one-liner: > > missing = [alpha for alpha in alist if not any(alpha + xy in checklist for > xy in > xlist)] > > (remember, all of this is untested) > > -- > 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 > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From tleeuwenburg at gmail.com Wed Apr 8 18:54:49 2009 From: tleeuwenburg at gmail.com (TennesseeLeeuwenburg) Date: Wed, 8 Apr 2009 15:54:49 -0700 (PDT) Subject: TypeError: object cannot be interpreted as an index References: Message-ID: On Apr 8, 8:44 pm, Piet van Oostrum wrote: > >>>>> "tleeuwenb... at gmail.com" (tc) wrote: > >tc> What, exactly, needs to be in place for an object to be a valid > >tc> dictionary key? > > It must have __hash__ and __cmp__ or __eq__ methods. Newstyle classes > inherit these from object. > -- > Piet van Oostrum > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4] > Private email: p... at vanoostrum.org Awesome, thanks. I wonder if this is an 'old-style' class, something I've never used myself... I'll have to do some research. -T From marduk at letterboxes.org Wed Apr 8 19:08:12 2009 From: marduk at letterboxes.org (Albert Hopkins) Date: Wed, 08 Apr 2009 19:08:12 -0400 Subject: Decompression with zlib In-Reply-To: <20090408235122.42079qs39fsz25p6@webmail.uni-tuebingen.de> References: <20090408235122.42079qs39fsz25p6@webmail.uni-tuebingen.de> Message-ID: <1239232092.32689.5.camel@blackwidow.nbk> On Wed, 2009-04-08 at 23:51 +0200, Emma Li wrote: > Hello, > > I'm trying to do compression/decompression of stuff with zlib, and I > just don't get it... > Here is an example. I assume that dec should be "a", but it isn't. dec > turns out to be an empty string, and I don't understand why... > > =================== > import zlib > compressor = zlib.compressobj(6) # level > decompressor = zlib.decompressobj() # wbits > > cmpr = compressor.compress("a") > print "compressed", len(cmpr) > dec = decompressor.decompress(cmpr) > print "decompressed", len(dec) > > # unconsumed tail and unused data > print "decompressor.unconsumed_tail", decompressor.unconsumed_tail > ##print "decompressor.unused_data", decompressor.unused_data > > dec += decompressor.flush() > print "after flushing", len(dec) > =================== > It looks like you forgot to flush the compressor object. Try insead: import zlib compressor = zlib.compressobj(6) decompressor = zlib.decompressobj() cmpr = compressor.compress("a") cmpr += compressor.flush() print "compressed", len(cmpr) dec = decompressor.decompress(cmpr) dec += decompressor.flush() print "decompressed", len(dec) From rdmurray at bitdance.com Wed Apr 8 19:22:28 2009 From: rdmurray at bitdance.com (R. David Murray) Date: Wed, 8 Apr 2009 23:22:28 +0000 (UTC) Subject: Some test fail on my new Python 2.6 References: <895637.13039.qm@web56003.mail.re3.yahoo.com> Message-ID: Sorin Schwimmer wrote: > /usr/local/lib is in /etc/ld.so.conf. The files are real. However, > creating symlinks in /usr/lib targeting the libraries in /usr/local/lib, > then recompiling, solved the tkinter problem. So, the conclusion is that > only /usr/lib is consulted for the tcl/tk libraries. Hmm. That's an issue with your linux setup, then, since python uses system facilities to load the shared libraries. I'm wondering if you just needed to run ldconfig...it might be that it hadn't been run since you installed the libraries into /usr/local/lib...though I would think the install process would have done that. > multiprocessing - do I need it? Probably not. I can fork() a new process, > so I can manage. I don't know what am I loosing without multiprocessing. What you loose is the nice interprocesses communication facilities provided by multiprocessing. You might append your system details (including the fact that you have /dev/shm) to that ticket, especially if you think you are going to want to be running cooperating processes that need to do more than just communicate via stdin/out. > htttpservers is still failing the same. > > Everything is done as root. I got failures (different failures) when I tried running that test as root. You might try running it as a regular user, both before and after the install. > I'll move on with make install. > > Thanks for your help; I'm progressing :-) You are welcome. Glad to be able to help out. -- R. David Murray http://www.bitdamce.com From andrew at acooke.org Wed Apr 8 19:32:30 2009 From: andrew at acooke.org (andrew cooke) Date: Wed, 8 Apr 2009 19:32:30 -0400 (CLT) Subject: nested looping In-Reply-To: <5b0681910904081545w7f04ec95p69b9586d743bc194@mail.gmail.com> References: <5b0681910904081415j5e260f53t77b00d70e720a44e@mail.gmail.com> <1239228770.49dd21623769c@mail.uh.cu> <5b0681910904081545w7f04ec95p69b9586d743bc194@mail.gmail.com> Message-ID: <4397ce7db623443fb756e2d3c796e4a2.squirrel@localhost> bit late here, but if it's as simple as you say, i think it would be much more efficient (because you only scan checklist and alist once each) to do: known = set() for check in checklist: known.add(check[0:-1]) missing = filter(lambda alpha: alpha not in known, alist) andrew PK wrote: > Excellent! Thanks all. Since I need to have this running on python 2.4 as > well, I think I'll choose, > > for alpha in alist: > for xy in xlist: > if alpha+xy in checklist: > break > else: > missing.append(alpha) > > jus tested and worked like a charm. > > Appreciate your help!! > > > > On Wed, Apr 8, 2009 at 6:12 PM, Luis Alberto Zarrabeitia Gomez > wrote: > >> >> Quoting PK : >> >> > So I'm trying to see whats the cleanest way to do this: >> > >> > I have a >> > >> > checklist = [ax, bx, by, cy ..] (a combination of a,b,c with x and y, >> > either both on one) >> > >> > allist = [a,b,c,....] >> > xlist = [x, y, ..] >> > >> [...] >> > now the problem is I want to include alpha in missing list only if >> > none of the combinations from xlist with alpha are in checklist. >> >> This is untested: >> >> for alpha in alist: >> for xy in xlist: >> if alpha+xy in checklist: >> break >> else: >> missing.append(alpha) >> >> (note that the "else" is for the "for", not the "if") >> >> You could also try the any/all functions from python2.5: >> >> for alpha in alist: >> if not any(alpha + xy in checklist for xy in xlist): >> missing.append(alpha) >> >> >> Or the really not recommended one-liner: >> >> missing = [alpha for alpha in alist if not any(alpha + xy in checklist >> for >> xy in >> xlist)] >> >> (remember, all of this is untested) >> >> -- >> 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 >> >> > -- > http://mail.python.org/mailman/listinfo/python-list > From Scott.Daniels at Acm.Org Wed Apr 8 19:34:56 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Wed, 08 Apr 2009 16:34:56 -0700 Subject: Decompression with zlib In-Reply-To: References: Message-ID: Emma Li wrote: > Hello, > > I'm trying to do compression/decompression of stuff with zlib, and I > just don't get it... Here is an example.... Try this: import zlib compressor = zlib.compressobj(6) # level cmpr = compressor.compress("a") print "compressed: %d %r" % (len(cmpr), cmpr) cmpr_rest = compressor.flush() print "compressed: %d %r" % (len(cmpr_rest), cmpr_rest) decompressor = zlib.decompressobj() # wbits a = decompressor.decompress(cmpr) b = decompressor.decompress(cmpr_rest) c = decompressor.flush() print "decompressed: %d+%d+%d: %r %r %r" % ( len(a), len(b), len(c), a, b, c) --Scott David Daniels Scott.Daniels at Acm.Org > decompressor = zlib.decompressobj() # wbits From rdmurray at bitdance.com Wed Apr 8 21:13:55 2009 From: rdmurray at bitdance.com (R. David Murray) Date: Thu, 9 Apr 2009 01:13:55 +0000 (UTC) Subject: is there a way to collect twitts with python? References: <1078018b0903271054y3d4a7162w885c61bf63bc2048@mail.gmail.com> <49D67874.8080209@gmail.com> <1238820981.1029.76.camel@tim-laptop> <49D6F0B7.2050608@gmail.com> Message-ID: Michael Torrie wrote: > Tim Wintle wrote: > > On Fri, 2009-04-03 at 14:58 -0600, Michael Torrie wrote: > >> Oh wow. If this is what Twitter does to one's ability to articulate > >> clearly, I hope Twitter dies a horrible death and any APIs and Python > >> bindings with it! > > > > Thank you, thank you, thank you .... > > > > everyone around me seems to love that thing (twitter), and I still can't > > work out why (apart from hacks such as using it as a hosted queue for > > cross-server comms, or receiving cheap sms to your app) > > Yeah. Of course I always thought that IRC was a good fit for this > purpose. Virus writers seem to think so. I wonder how Twitter would > deal with viruses and worms using Twitter and a command and control > communications mechanism. > > People who love Twitter are also people who SMS a lot it seems. Twitter > probably is a natural evolution of SMS, melding the IRC idea with > ridiculously short, hard-to-read, cryptic, lol-speak, messages. I think > the Japanese just might be on to something as no one over there uses > SMS. It's all e-mail to them. Granted the scourge of abbreviated words > and lol-speak is just as bad there. Sigh. lol-speak originated on IRC. (Or probably even earlier on the original bitnet relay chat.) -- R. David Murray http://www.bitdance.com From xie.kenneth at gmail.com Wed Apr 8 23:10:00 2009 From: xie.kenneth at gmail.com (Ken) Date: Thu, 9 Apr 2009 11:10:00 +0800 Subject: is there a rwlock implementation in python library? Message-ID: <8af39e160904082010k1ba3ab6la0b1aee67d34bbee@mail.gmail.com> I need a read-write-lock, is there already an implementation in the standard library? -------------- next part -------------- An HTML attachment was scrubbed... URL: From semanticist at gmail.com Wed Apr 8 23:44:36 2009 From: semanticist at gmail.com (Miles) Date: Wed, 8 Apr 2009 23:44:36 -0400 Subject: is there a rwlock implementation in python library? In-Reply-To: <8af39e160904082010k1ba3ab6la0b1aee67d34bbee@mail.gmail.com> References: <8af39e160904082010k1ba3ab6la0b1aee67d34bbee@mail.gmail.com> Message-ID: On Wed, Apr 8, 2009 at 11:10 PM, Ken wrote: > I need a read-write-lock, is there already an implementation in the standard > library? No, but there are several recipes on ActiveState: http://code.activestate.com/recipes/413393/ http://code.activestate.com/recipes/502283/ http://code.activestate.com/recipes/465156/ -Miles From aahz at pythoncraft.com Wed Apr 8 23:53:26 2009 From: aahz at pythoncraft.com (Aahz) Date: 8 Apr 2009 20:53:26 -0700 Subject: Python 3.0 online docs broken References: <49da837c$0$29401$4c5ecfc7@fe1.usenet.com> <94ae55e9-265c-4444-8bba-316001db83e1@r37g2000yqn.googlegroups.com> <49db6ffe$0$29393$4c5ecfc7@fe1.usenet.com> <49dcc2df$0$29421$4c5ecfc7@fe1.usenet.com> Message-ID: In article <49dcc2df$0$29421$4c5ecfc7 at fe1.usenet.com>, Jim Garrison wrote: > >I emailed the webmaster but have received no response. Several people have sent e-mail about this; sorry, we haven't had time for individual responses. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ Why is this newsgroup different from all other newsgroups? From ntv1534 at gmail.com Thu Apr 9 00:17:41 2009 From: ntv1534 at gmail.com (MooMaster) Date: Wed, 8 Apr 2009 21:17:41 -0700 (PDT) Subject: in place list modification necessary? What's a better idiom? References: <7790d731-109b-4373-986f-c28699e29a6b@b7g2000pre.googlegroups.com> <688a5e3d-38f5-45d9-a115-ca43435fa8e9@i28g2000prd.googlegroups.com> Message-ID: <18313bfb-e89d-4a3c-8303-8568d1322a83@y13g2000yqn.googlegroups.com> On Apr 6, 10:43?pm, Carl Banks wrote: > MooMaster wrote: > > So I'm reading in values from a file, and for each column I need to > > dynamically discover the range of possible values it can take and > > quantize if necessary. This is the solution I've come up with: > > > > > def createInitialCluster(fileName): > > ? ? #get the data from the file > > ? ? points = [] > > ? ? with open(fileName, 'r') as f: > > ? ? ? ? for line in f: > > ? ? ? ? ? ? points.append(line.rstrip('\n')) > > ? ? #clean up the data > > ? ? fixedPoints = [] > > ? ? for point in points: > > ? ? ? ? dimensions = [quantize(i, points, point.split(",").index(i)) > > for i in point.split(",")] > > ? ? ? ? print dimensions > > ? ? ? ? fixedPoints.append(Point(dimensions)) > > ? ? #return an initial cluster of all the points > > ? ? return Cluster(fixedPoints) > > > def quantize(stringToQuantize, pointList, columnIndex): > > ? ? #if it's numeric, no need to quantize > > ? ? if(isNumeric(stringToQuantize)): > > ? ? ? ? return float(stringToQuantize) > > ? ? #first we need to discover all the possible values of this column > > ? ? domain = [] > > ? ? for point in pointList: > > ? ? ? ? domain.append(point.split(",")[columnIndex]) > > ? ? #make it a set to remove duplicates > > ? ? domain = list(Set(domain)) > > ? ? #use the index into the domain as the number representing this > > value > > ? ? return float(domain.index(stringToQuantize)) > > > #harvested fromhttp://www.rosettacode.org/wiki/IsNumeric#Python > > def isNumeric(string): > > ? ? try: > > ? ? ? ? i = float(string) > > ? ? except ValueError: > > ? ? ? ? return False > > ? ? return True > > Big problem with this. ?I'm guessing you never ran it on a really big > file yet. ?Your quantize function does a lot of unnecessary work: it > rebuilds the list of indices for every single line, every single non- > numeric entry, in fact. ?(Tech-speak: that is N^2 behavior in both the > number of lines and number of non-numeric columns. ?Not good.) ?This > will work ok for a small file, but will take forever on a large file > (perhaps literally). > > So, forgetting about column indexing for a moment, we can improve this > vastly simply by generating the list of indices once. ?Do that in a > separete function, have it return the list, and then pass that list to > the quantize function. ?So replace the midportion of your > createInitialCluster function with something like this: > > ? ? .... > ? ? for i in xrange(len(points[0])): # number of columns > ? ? ? ? column_indices.append(quantize_column(points,i)) > ? ? fixedPoints = [] > ? ? for point in points: > ? ? ? ? dimensions = [quantize(s, column_indices[i], point.split > (",").index(i)) > ? ? ? ? ? ? ? ? for (i,s) in enumerate(point.split(","))] # need index > as well as entry here > ? ? ? ? print dimensions > ? ? ? ? fixedPoints.append(Point(dimensions)) > ? ? .... > > And the two functions would be something like this: > > def quantize_columns(point_list,column_index): > ? ? # this assumes if the first column is numeric the whole column > would be > ? ? if(isNumeric(point_list[0][column_index])): > ? ? ? ? return None # don't quantize this column > ? ? #first we need to discover all the possible values of this column > ? ? domain = [] > ? ? for point in point_list: > ? ? ? ? domain.append(point.split(",")[column_index]) > ? ? #make it a set to remove duplicates > ? ? return list(set(domain)) > > def quantize(i,domain,s): > ? ? if domain is None: > ? ? ? ? return float(s) > ? ? return float(domain.index(s)) > > This (once debugged :) will run much, much faster on a large dataset. > > Now back to your question. > > > It works, but it feels a little ugly, and not exactly Pythonic. Using > > two lists I need the original point list to read in the data, then the > > dimensions one to hold the processed point, and a fixedPoint list to > > make objects out of the processed data. If my dataset is in the order > > of millions, this'll nuke the memory. I tried something like: > > > for point in points: > > ? ?point = Point([quantize(i, points, point.split(",").index(i)) for i > > in point.split(",")]) > > but when I print out the points afterward, it doesn't keep the > > changes. > > It's because the order of items in a set is undefined. ?The order of > the items in list(set(["a","b","c","d"])) might be very different from > the order in list(set(["a","b","c","d","e"])). ?You were passing > quantize incomplete an incomplete list of points, so as the points > grew, the items in the set changed, and it messed up the order. ?In > fact, ?you should never rely on the order being the same, even if you > created the set with the very same arguments. > > What you are trying to do should be done with dictionaries: create a > dict that maps a value to a number. > > Now, based on your quantize function, it would seem that the number > associated with the value is arbitrary (doesn't matter what it is, as > long as it's distinct), so it isn't necessary to read the whole csv > file in before assigning numbers; just build the dict as you go. > > I suggest collections.defaultdict for this task. ?It's like a regular > dict, but it creates a new value any time you access a key that > doesn't exist, a perfect solution to your task. ?We'll pass it a > function that generates a different index each time it's called. > (This is probably too advanced for you, but oh well, it's such a cool > trick.) > > import collections > import itertools > > def createInitialCluster(fileName): > ? ? fixedPoints = [] > ? ? # quantization is a dict that assigns sequentially-increasing > numbers > ? ? # to values when reading keys that don't yet exit > ? ? quantization = defaultdict.collections(itertools.count().next) > ? ? with open(fileName, 'r') as f: > ? ? ? ? for line in f: > ? ? ? ? ? ? dimensions = [] > ? ? ? ? ? ? for s in line.rstrip('\n').split(","): > ? ? ? ? ? ? ? ? if isNumeric(s): > ? ? ? ? ? ? ? ? ? ? dimensions.append(float(s)) > ? ? ? ? ? ? ? ? else: > ? ? ? ? ? ? ? ? ? ? dimensions.append(float(quantization[s])) > ? ? ? ? ? ? fixedPoints.append(Point(dimensions)) > ? ? return Cluster(fixedPoints) > > A couple general pointers: > > * Don't ever use i to represent a string. ?Programmers expect i to be > an integer. ?i,j,k,l,m, and n should be integers, in fact. ?u,v,w,x,y, > and z should be floats. ?You should stick to this convention whenever > possible, but definitely never use i for anything but an integer. > > * set is built into Python now; unless you're using an older version > (2.3 I think) you should use set instead of Set. > > * The Python style guide (q.g.) recommends that variables use names > such as column_index rather than columnIndex. ?The world won't end if > you don't follow it but if you want to be Pythonic that's how. > > Carl Banks Nice explanation! Very detailed and thorough, thanks! I haven't used defaultdict before, so that was a very useful and efficient trick! From dmw at coder.cl Thu Apr 9 00:29:53 2009 From: dmw at coder.cl (Daniel Molina Wegener) Date: Thu, 09 Apr 2009 00:29:53 -0400 Subject: [ANN] pyxser-0.2rc1, python xml serialization Message-ID: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA512 pxyser --- python xml serialization (release candidate 0.2). pyxser is a C written extension that serializes/deserializes python objects in XML format. pyxser is licensed under LGPLv3 and tries to conform a normalized form of object serialization. the project web page is at: http://coder.cl/software/pyxser/ the project repository and download: http://sourceforge.net/projects/pyxser/ the package name is: pyxser-0.2rc1.tar.gz the download page is at: http://sourceforge.net/project/showfiles.php?group_id=256386 please, read the INSTALLING file for installing instructions. Well, I need some feedback and testers, I think that I've made just small test, but if I can get some feedback from this group it would be great... thanks in advance and best regards, - -- .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) iQIcBAEBCgAGBQJJ3XnBAAoJEHxqfq6Y4O5NcBgQAIiOzNsvToyLxGkoyR3mXqQe K6WsiYgdM3gHFr6KTTeY7NbCn1p5XGvZw2bSo/cv9yQCfh+kEN+Me9SN8IzQX+Tl IcmLlSalHvHXiW79INHEyKI1GUZxb9rKA76zWDU0PW56Fa3rxnI4aHNZEFqvVOxo MFafQXPAJE5TKRMzf68ZvMLwIyTIV5w+WqR476L1Sd6rxQXBwW31RGVVywro4aIC d6BQhfL0IhV91eXCop9zcnIXZvYOw6pZE98s6Atmda34WsyzvLMYQN6YppL4Bd4l ADsi8/7Y66CTuLaUHe5O+2lbmkSquxORQF7/hqrAnzkNaQjLuULQydhwNbdQO6Wf 3fipJ0H0XDzPpMg/CusejWuqNCYzMwYL1M6M9mo0/Sy3M2AYr9ocIh58KFmMHMM2 50E/r18cwtxZ7y7kPiu3LIWWesGIUfkpwpN1PfB2oV2aJD2jFi/XGtUrHpQbTqUA aO03CJVJiXPdQb2yCexqsp+r8+Psh8E3u2xfRGCqcNCTriBD3JrO4+XFMPnw506E K3BtdKn+1Cj8PhOyMUB9CQvG0+y8SWL8DQR1n8PDAHZ3UHigGhY+r3WNd8qoYddk 7chXG3TIa+2O3UVbDYneI1XEsBMKhE+X8mI8LinL3Tk/CuQcUl3536//oKvjLLGu 4GGlC0ni0oOoWQoJm2bw =bdNT -----END PGP SIGNATURE----- From ntv1534 at gmail.com Thu Apr 9 01:09:49 2009 From: ntv1534 at gmail.com (MooMaster) Date: Wed, 8 Apr 2009 22:09:49 -0700 (PDT) Subject: in place list modification necessary? What's a better idiom? References: <7790d731-109b-4373-986f-c28699e29a6b@b7g2000pre.googlegroups.com> Message-ID: <786ea75b-685d-4478-92b5-bb9a9cd61856@u8g2000yqn.googlegroups.com> On Apr 7, 12:40?am, Peter Otten <__pete... at web.de> wrote: > Peter Otten wrote: > > MooMaster wrote: > > >> Now we can't calculate a meaningful Euclidean distance for something > >> like "Iris-setosa" and "Iris-versicolor" unless we use string-edit > >> distance or something overly complicated, so instead we'll use a > >> simple quantization scheme of enumerating the set of values within the > >> column domain and replacing the strings with numbers (i.e. Iris-setosa > >> = 1, iris-versicolor=2). > > > I'd calculate the distance as > > > def string_dist(x, y, weight=1): > > ? ? return weight * (x == y) > > oops, this must of course be (x != y). > > > You don't get a high resolution in that dimension, but you don't introduce > > an element of randomness, either. > > > Peter > > The randomness doesn't matter too much, all K-means cares about is a distance between two points in a coordinate space and as long as that space is invariant it doesn't matter too much (i.e. we don't want (1,1) becoming (3,1) on the next iteration, or the value for a quantized column changing). With that in mind, I was hoping to be lazy and just go with an enumeration approach... Nevertheless, it does introduce a subtle ordering for nominal data, as if Iris-Setosa =1, Iris-Versicolor=2, and Iris-Virginica=3 then on that scale Iris-Versicolor is intuitively "closer" to virginica than setosa is, when in fact such distances don't mean anything on a nominal scale. I hadn't thought about a function like that, but it makes a lot of sense. Thanks! From ldo at geek-central.gen.new_zealand Thu Apr 9 01:32:00 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Thu, 09 Apr 2009 17:32 +1200 Subject: asynchronous python call References: <637028a0-58b9-4912-896d-2b17e13414fe@q16g2000yqg.googlegroups.com> Message-ID: In message , Aahz wrote: > In article > <637028a0-58b9-4912-896d-2b17e13414fe at q16g2000yqg.googlegroups.com>, > joeygartin wrote: >> >> So I was hoping there was a way to spin off the process and >>redirect the user to the page while the emails are being formed and >>sent vs. making the page hang while it is done. > > The simplest solution is to create an external program and use the > subprocess module to call it. You'll probably want to call os.setsid, so that the child process keeps running after the parent terminates. From danb_83 at yahoo.com Thu Apr 9 02:18:55 2009 From: danb_83 at yahoo.com (AggieDan04) Date: Wed, 8 Apr 2009 23:18:55 -0700 (PDT) Subject: Floor value in math operators References: Message-ID: <84f92182-9af6-445a-8ce0-18ee0bde44ce@w40g2000yqd.googlegroups.com> On Apr 8, 12:08?pm, David Smith wrote: > Avi wrote: > > Hi, > > > This will be a very simple question to ask all the awesome programmers > > here: > > > How can I get answer in in decimals for such a math operator: > > > 3/2 > > > I get 1. I want to get 1.5 > > > Thanks in advance, > > Avi > > I'm going to assume your operands are variables instead of numeric > literals. ?Why not consider doing a type conversion to float or Decimal > and then perform the division? Because float(x) and Decimal(x) fail for complex numbers and lose precision if x is a rational or a multi-precision float. From ldo at geek-central.gen.new_zealand Thu Apr 9 02:53:13 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Thu, 09 Apr 2009 18:53:13 +1200 Subject: Why does Python show the whole array? References: Message-ID: In message , Gilles Ganault wrote: > test = "toto at gmail.com" > isp = ["gmail.com", "yahoo.com"] > for item in isp: > if test.find(item): > print item > ======= output > gmail.com > yahoo.com > ======= This is why conditional constructs should not accept any values other than True and False. From __peter__ at web.de Thu Apr 9 02:59:30 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 09 Apr 2009 08:59:30 +0200 Subject: Why does Python show the whole array? References: Message-ID: Lawrence D'Oliveiro wrote: > In message , Gilles Ganault > wrote: > >> test = "toto at gmail.com" >> isp = ["gmail.com", "yahoo.com"] >> for item in isp: >> if test.find(item): >> print item >> ======= output >> gmail.com >> yahoo.com >> ======= > > This is why conditional constructs should not accept any values other than > True and False. So you think if test.find(item) == True: ... would have been better? From sri_annauni at yahoo.co.in Thu Apr 9 03:05:35 2009 From: sri_annauni at yahoo.co.in (srinivasan srinivas) Date: Thu, 9 Apr 2009 12:35:35 +0530 (IST) Subject: Can someone explain about the usage of unittest.TestSuite? Message-ID: <61448.25339.qm@web7903.mail.in.yahoo.com> Hi, I would like to know about the unittest.TestSuite clearly like at what situations i can use this TestSuite? I am not getting the clear difference between this and unittest.TestCase. Thanks, Srini Cricket on your mind? Visit the ultimate cricket website. Enter http://beta.cricket.yahoo.com From timr at probo.com Thu Apr 9 03:13:01 2009 From: timr at probo.com (Tim Roberts) Date: Thu, 09 Apr 2009 00:13:01 -0700 Subject: Simple CGI request and Python reply References: Message-ID: Greg Corradini wrote: > >I'm trying to implement something very simple without using a Python >WebFramework and I need some advice. I want to send a comma delimited string >from the client to a server-side Python script. My initial plan was to use a >JavaScript function (see below) called "makerequest" that creates a >XMLHttpRequest object and GETs the output from a Python script. I've used >this function before to field requests (in those cases the parameter "data" >passed in the XMLHttpRequest.send() method is null). So I thought I could >just pass some data in and be able to retrieve it with Python. I'm not sure >how to do this. I've used forms with cgi/Python before. However, I don't >want to use a form here. I want Python to handle the "data" variable being >passed without looking for field names using .FieldStorage(). Can that be >done? Well, sort of, but why wouldn't you want to use the tools that are designed to handle this? You are sending an HTTP GET request, which is exactly the same as entering the URL in your browser's command line. EXACTLY the same. That request will be read by your web server and converted to a CGI request for your Python program (assuming the serverPage points to a Python script). If you pass parameters with the URL, you will need to parse those parameters, and the "cgi" module already knows how to do that. You don't actually have to use FieldStorage. You can just say this: import cgi q = cgi.parse() which returns the parameters as a simple dictionary. >Maybe making a request is not the quickest route to do what I want. Ideas? There are few other mechanisms available in Javascript to do this. -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From semanticist at gmail.com Thu Apr 9 03:49:26 2009 From: semanticist at gmail.com (Miles) Date: Thu, 9 Apr 2009 03:49:26 -0400 Subject: Why does Python show the whole array? In-Reply-To: References: Message-ID: On Thu, Apr 9, 2009 at 2:59 AM, Peter Otten wrote: > Lawrence D'Oliveiro wrote: >> This is why conditional constructs should not accept any values other than >> True and False. > > So you think > > if test.find(item) == True: ... > > would have been better? Clearly, any comparison with a boolean literal should be illegal. ;) -Miles P.S. ... really, though. From sjmachin at lexicon.net Thu Apr 9 04:15:30 2009 From: sjmachin at lexicon.net (John Machin) Date: Thu, 9 Apr 2009 01:15:30 -0700 (PDT) Subject: Why does Python show the whole array? References: Message-ID: <7e7a386f-d336-4186-822d-c6af0a581751@e38g2000vbe.googlegroups.com> On Apr 9, 4:53?pm, Lawrence D'Oliveiro wrote: > In message , Gilles Ganault > wrote: > > > test = "t... at gmail.com" > > isp = ["gmail.com", "yahoo.com"] > > for item in isp: > > if test.find(item): > > print item > > ======= output > > gmail.com > > yahoo.com > > ======= > > This is why conditional constructs should not accept any values other than > True and False. An alternative viewpoint: This is what happens when one user doesn't understand a long-standing idiom and/or doesn't read the manual. That's all. It doesn't constitute evidence that such a restriction would be generally beneficial. From niklas.norrthon at hotmail.com Thu Apr 9 04:16:31 2009 From: niklas.norrthon at hotmail.com (Niklas Norrthon) Date: Thu, 9 Apr 2009 01:16:31 -0700 (PDT) Subject: Why does Python show the whole array? References: Message-ID: <755a15dc-8b5f-4f54-86b5-7c1f44ebc958@g20g2000vba.googlegroups.com> On 9 Apr, 09:49, Miles wrote: > On Thu, Apr 9, 2009 at 2:59 AM, Peter Otten wrote: > > Lawrence D'Oliveiro wrote: > >> This is why conditional constructs should not accept any values other than > >> True and False. > > > So you think > > > if test.find(item) == True: ... > > > would have been better? > > Clearly, any comparison with a boolean literal should be illegal. ?;) > So you think truth_value = True if test.find(item) == truth_value: ... would have been better? :-) (couldn't resist...) /Niklas Norrthon From sjmachin at lexicon.net Thu Apr 9 04:26:10 2009 From: sjmachin at lexicon.net (John Machin) Date: Thu, 9 Apr 2009 01:26:10 -0700 (PDT) Subject: Floor value in math operators References: <84f92182-9af6-445a-8ce0-18ee0bde44ce@w40g2000yqd.googlegroups.com> Message-ID: On Apr 9, 4:18?pm, AggieDan04 wrote: > On Apr 8, 12:08?pm, David Smith wrote: > > > > > Avi wrote: > > > Hi, > > > > This will be a very simple question to ask all the awesome programmers > > > here: > > > > How can I get answer in in decimals for such a math operator: > > > > 3/2 > > > > I get 1. I want to get 1.5 > > > > Thanks in advance, > > > Avi > > > I'm going to assume your operands are variables instead of numeric > > literals. ?Why not consider doing a type conversion to float or Decimal > > and then perform the division? > > Because float(x) and Decimal(x) fail for complex numbers and lose > precision if x is a rational or a multi-precision float. What is the likelihood that the OP would get 3 / 2 -> 1 if he was working with rationals or multi-precision floats? What is the likelihood that the OP would ask such a question if he was working with rationals or multi-precision floats? From steven at REMOVE.THIS.cybersource.com.au Thu Apr 9 04:29:21 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 09 Apr 2009 08:29:21 GMT Subject: Why does Python show the whole array? References: Message-ID: On Thu, 09 Apr 2009 18:53:13 +1200, Lawrence D'Oliveiro wrote: > This is why conditional constructs should not accept any values other > than True and False. I once tried this: for i in alist.sort(): and got an error I didn't understand because I failed to read the docs. Clearly for loops shouldn't take sequences but only an integer count, because that would have prevented my error. -- Steven From neilcrighton at gmail.com Thu Apr 9 04:33:42 2009 From: neilcrighton at gmail.com (Neil Crighton) Date: Thu, 9 Apr 2009 08:33:42 +0000 (UTC) Subject: numpy.where References: <148739635947045949601137627602034810513-Webmail@me.com> Message-ID: heidi taynton mac.com> writes: > > Hi, > > I'm fairly new to programming and am having a probably cutting my arrays. I have two different 1d arrays, one > of time, and the second energy. I want to cut both arrays of time min<=time<=max . I've created a 2d array > with [time,energy] and I believe numpy.where is what I am looking for, but haven't been able to get the > conditions in right for it to work. I'm not sure exactly what you're trying to do, but maybe you want a boolean array to select the right elements? So if time and energy are 1-d numpy arrays of the same length: >>> condition = (min_time <= time) & (time <= max_time) >>> new_time = time[condition] >>> new_energy = energy[condition] There's also a Numpy list for these kind of questions: http://dir.gmane.org/gmane.comp.python.numeric.general Neil From email at kait.de Thu Apr 9 04:36:17 2009 From: email at kait.de (Kai Timmer) Date: Thu, 9 Apr 2009 01:36:17 -0700 (PDT) Subject: Get the ipv6 address from a interface Message-ID: <86176ef7-c2e0-4c5d-b883-d91672e3eb0b@w40g2000yqd.googlegroups.com> Hello, i need a function that returns the ipv6 address from a given interface name. For ipv4 i use this one: def get_ip_address(ifname): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) return socket.inet_ntoa(fcntl.ioctl( s.fileno(), 0x8915, # SIOCGIFADDR struct.pack('256s', ifname[:15]) )[20:24]) which works great. But i am not enough into python to port that to ipv6. It has to work under linux only. Any help is appreciated. Greets, Kai From ldo at geek-central.gen.new_zealand Thu Apr 9 04:58:55 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Thu, 09 Apr 2009 20:58:55 +1200 Subject: Why does Python show the whole array? References: <7e7a386f-d336-4186-822d-c6af0a581751@e38g2000vbe.googlegroups.com> Message-ID: In message <7e7a386f-d336-4186-822d- c6af0a581751 at e38g2000vbe.googlegroups.com>, John Machin wrote: > On Apr 9, 4:53 pm, Lawrence D'Oliveiro central.gen.new_zealand> wrote: > >> This is why conditional constructs should not accept any values other >> than True and False. > > An alternative viewpoint: This is what happens when one user doesn't > understand a long-standing idiom and/or doesn't read the manual. > That's all. It doesn't constitute evidence that such a restriction > would be generally beneficial. Fine if it only happened once. But it's a commonly-made mistake. At some point you have to conclude that not all those people are stupid, there really is something wrong with the design. From pavlovevidence at gmail.com Thu Apr 9 05:07:24 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 9 Apr 2009 02:07:24 -0700 (PDT) Subject: numpy.where References: <148739635947045949601137627602034810513-Webmail@me.com> Message-ID: <1d1b090a-8046-4bb2-aba9-7c935aa64735@e18g2000yqo.googlegroups.com> On Apr 9, 1:33?am, Neil Crighton wrote: > heidi taynton mac.com> writes: > > > > > Hi, ? > > > I'm fairly new to programming and am having a probably cutting my arrays. ?I > > have two different 1d arrays, one> of time, and the second energy. ? ?I want to cut both arrays of time > > min<=time<=max ?. ? I've created a 2d array> with [time,energy] and I believe numpy.where is what I am looking for, but > > haven't been able to get the > > > conditions in right for it to work. ? > > I'm not sure exactly what you're trying to do, but maybe you want a boolean > array to select the right elements? So if time and energy are 1-d numpy arrays > of the same length: > > >>> condition = (min_time <= time) & (time <= max_time) > >>> new_time = time[condition] > >>> new_energy = energy[condition] Won't work: condition is an array of ones and zeros, but you need to index the arrays with indices. So, add a call to nonzero to get the indices of the elements. elements = nonzero(logical_and(min_time<=time,max_time>=time)) new_time = time[elements] new_energy = energy[elements] Carl Banks From rhamph at gmail.com Thu Apr 9 05:25:09 2009 From: rhamph at gmail.com (Adam Olsen) Date: Thu, 9 Apr 2009 02:25:09 -0700 (PDT) Subject: Returning different types based on input parameters References: Message-ID: <1e0fbcec-c4a0-48c3-8fa8-a2a2837beda5@h28g2000yqd.googlegroups.com> On Apr 8, 8:09?am, George Sakkis wrote: > On Apr 7, 3:18?pm, Adam Olsen wrote: > > > On Apr 6, 3:02?pm, George Sakkis wrote: > > > > For example, it is common for a function f(x) to expect x to be simply > > > iterable, without caring of its exact type. Is it ok though for f to > > > return a list for some types/values of x, a tuple for others and a > > > generator for everything else (assuming it's documented), or it should > > > always return the most general (iterator in this example) ? > > > For list/tuple/iterable the correlation with the argument's type is > > purely superficial, *because* they're so compatible. ?Why should only > > tuples and lists get special behaviour? ?Why shouldn't every other > > argument type return a list as well? > > That's easy; because the result might be infinite. In which case you > may ask "why shouldn't every argument type return an iterator then", > and the reason is usually performance; if you already need to store > the whole result sequence (e.g. sorted()), why return just an iterator > to it and force the client to copy it to another list if he needs > anything more than iterating once over it ? You've got two different use cases here. sorted() clearly cannot be infinite, so it might as well always return a list. Other functions that can be infinite should always return an iterator. > > A counter example is python 3.0's str/bytes functions. ?They're > > mutually incompatible and there's no default. > > As already mentioned, another example is filter() that tries to match > the input sequence type and falls back to list if it fails. That's fixed in 3.0. It's always an iterator now. > > > To take it further, what if f wants to return different types, > > > differing even in a duck-type sense? > > > > At a minimum it's highly undesirable. ?You lose a lot of readability/ > > maintainability. ?solve2/solve_ex is a little ugly, but that's less > > overall, so it's the better option. > > That's my feeling too, at least in a dynamic language. For a static > language that allows overloading, that should be a smaller (or perhaps > no) issue. Standard practices may encourage it in a static language, but it's still fairly confusing. Personally, I consider python's switch to a different operator for floor division (//) to be a major step forward over C-like languages. From tjreedy at udel.edu Thu Apr 9 05:29:53 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 09 Apr 2009 05:29:53 -0400 Subject: Why does Python show the whole array? In-Reply-To: References: Message-ID: Lawrence D'Oliveiro wrote: > In message , Gilles Ganault > wrote: > >> test = "toto at gmail.com" >> isp = ["gmail.com", "yahoo.com"] >> for item in isp: >> if test.find(item): >> print item >> ======= output >> gmail.com >> yahoo.com >> ======= > > This is why conditional constructs should not accept any values other than > True and False. Nonesense. Just use 'if item in test:' which tells you all the info you are using. str.find returns an index that you ignore, so don't use it. From bmdavll at gmail.com Thu Apr 9 05:56:00 2009 From: bmdavll at gmail.com (David Liang) Date: Thu, 9 Apr 2009 02:56:00 -0700 (PDT) Subject: Pathological regular expression Message-ID: <967fe248-0e15-4247-9d36-6f3f5e0c5eb7@a7g2000yqk.googlegroups.com> Hi all, I'm having a weird problem with a regular expression (tested in 2.6 and 3.0): Basically, any of these: _re_comments = re.compile(r'^(([^\\]+|\\.|"([^"\\]+|\\.)*")*)#.*$') _re_comments = re.compile(r'^(([^#]+|\\.|"([^"\\]+|\\.)*")*)#.*$') _re_comments = re.compile(r'^(([^"]+|\\.|"([^"\\]+|\\.)*")*)#.*$') followed by for example, line = r'~/.[m]ozilla/firefox/*.default/chrome' print(_re_comments.sub(r'\1', line)) ...hangs the interpreter. For reference, if the first command had been _re_comments = re.compile(r'^(([^z]+|\\.|"([^"\\]+|\\.)*")*)#.*$') (off by one character z) it works fine, and all the equivalent operations work in sed and awk. Am I missing something about Python RE's? -David From neilcrighton at gmail.com Thu Apr 9 05:58:49 2009 From: neilcrighton at gmail.com (Neil Crighton) Date: Thu, 9 Apr 2009 09:58:49 +0000 (UTC) Subject: numpy.where References: <148739635947045949601137627602034810513-Webmail@me.com> <1d1b090a-8046-4bb2-aba9-7c935aa64735@e18g2000yqo.googlegroups.com> Message-ID: Carl Banks gmail.com> writes: > > >>> condition = (min_time <= time) & (time <= max_time) > > >>> new_time = time[condition] > > >>> new_energy = energy[condition] > > Won't work: condition is an array of ones and zeros, but you need to > index the arrays with indices. So, add a call to nonzero to get the > indices of the elements. > > elements = nonzero(logical_and(min_time<=time,max_time>=time)) > > new_time = time[elements] > new_energy = energy[elements] > It will work - try it :) You can index numpy arrays with either an array of indices (dtype integer) ,or a boolean array (dtype bool). In the case above condition is a boolean array. In general using a boolean array is faster, because you skip the call to nonzero(). I find indexing with boolean arrays mindbogglingly useful, and one of the great things about Numpy. You can do things like this (age, height and people are all arrays of the same length): c0 = age < 15 c1 = height > 1.5 c2 = height < 1.8 people_subset1 = people[c0 & c1] people_subset2 = people[(c1 & c2) | c0 ] people_subset3 = people[(c1 & c2) & ~c0 ] It gets even more intuitive if you use record arrays (aka structured arrays). Neil From s.selvamsiva at gmail.com Thu Apr 9 06:13:08 2009 From: s.selvamsiva at gmail.com (S.Selvam) Date: Thu, 9 Apr 2009 15:43:08 +0530 Subject: Reading only headers Message-ID: Hi all, I want to read headers from web page and check whether its Content-Type is xml or not.I used the following code ... request = urllib2.Request(url, None, USER_AGENT) opener = urllib2.build_opener() datastream = opener.open(request) if datastream.headers.get('Content-Type','').find('xml') == -1: raise "Its not xml!" else: """ Read the content and process """ ... Is this the good way to read headers ? ,as i do not want the content ,unless it is xml. Please suggest me,if there are some other good methods to read only the headers. -- Yours, S.Selvam -------------- next part -------------- An HTML attachment was scrubbed... URL: From bmdavll at gmail.com Thu Apr 9 06:17:52 2009 From: bmdavll at gmail.com (David Liang) Date: Thu, 9 Apr 2009 03:17:52 -0700 (PDT) Subject: Pathological regular expression References: <967fe248-0e15-4247-9d36-6f3f5e0c5eb7@a7g2000yqk.googlegroups.com> Message-ID: <586673fc-10ff-49eb-96cf-a4a754166533@o11g2000yql.googlegroups.com> On Apr 9, 2:56?am, David Liang wrote: > Hi all, > I'm having a weird problem with a regular expression (tested in 2.6 > and 3.0): > > Basically, any of these: > _re_comments = re.compile(r'^(([^\\]+|\\.|"([^"\\]+|\\.)*")*)#.*$') > _re_comments = re.compile(r'^(([^#]+|\\.|"([^"\\]+|\\.)*")*)#.*$') > _re_comments = re.compile(r'^(([^"]+|\\.|"([^"\\]+|\\.)*")*)#.*$') > > followed by for example, > line = r'~/.[m]ozilla/firefox/*.default/chrome' > print(_re_comments.sub(r'\1', line)) > > ...hangs the interpreter. For reference, if the first command had been > _re_comments = re.compile(r'^(([^z]+|\\.|"([^"\\]+|\\.)*")*)#.*$') > > (off by one character z) it works fine, and all the equivalent > operations work in sed and awk. Am I missing something about Python > RE's? > > -David The problem was the redundant +'s; the fixed RE is _re_comments = re.compile(r'^(([^#"\\]|\\.|"([^"\\]|\\.)*")*)#.*') From bmdavll at gmail.com Thu Apr 9 06:17:59 2009 From: bmdavll at gmail.com (David Liang) Date: Thu, 9 Apr 2009 03:17:59 -0700 (PDT) Subject: Pathological regular expression References: <967fe248-0e15-4247-9d36-6f3f5e0c5eb7@a7g2000yqk.googlegroups.com> Message-ID: <7650367c-ab8c-4730-b3e3-30272153401c@e18g2000yqo.googlegroups.com> On Apr 9, 2:56?am, David Liang wrote: > Hi all, > I'm having a weird problem with a regular expression (tested in 2.6 > and 3.0): > > Basically, any of these: > _re_comments = re.compile(r'^(([^\\]+|\\.|"([^"\\]+|\\.)*")*)#.*$') > _re_comments = re.compile(r'^(([^#]+|\\.|"([^"\\]+|\\.)*")*)#.*$') > _re_comments = re.compile(r'^(([^"]+|\\.|"([^"\\]+|\\.)*")*)#.*$') > > followed by for example, > line = r'~/.[m]ozilla/firefox/*.default/chrome' > print(_re_comments.sub(r'\1', line)) > > ...hangs the interpreter. For reference, if the first command had been > _re_comments = re.compile(r'^(([^z]+|\\.|"([^"\\]+|\\.)*")*)#.*$') > > (off by one character z) it works fine, and all the equivalent > operations work in sed and awk. Am I missing something about Python > RE's? > > -David The problem was the redundant +'s; the fixed RE is _re_comments = re.compile(r'^(([^#"\\]|\\.|"([^"\\]|\\.)*")*)#.*') From notvalid2 at sbcglobal.net Thu Apr 9 06:22:46 2009 From: notvalid2 at sbcglobal.net (W. eWatson) Date: Thu, 09 Apr 2009 03:22:46 -0700 Subject: Problem with PIL/Tkinter Program Example Message-ID: Something is amiss here. The program produces a canvas in which one can move an object around. The input file is hard coded (see open). If you want to try it, you'll need to provide a file. Python error below. Name space difficulty? #Mouse movement from Tkinter import * import PIL import Image class data: startx=0 starty=0 def startmotioncallback(event): data.startx=event.x data.starty=event.y def motioncallback(event): deltax=event.x-data.startx deltay=event.y-data.starty data.startx=event.x data.starty=event.y # should put some limits on where the cirle is moved # left as exercise..... data.cnv.move(data.ring,deltax,deltay) def Demo(root): # resize window root.geometry('400x400+0+0') data.root=root # make a canvas cnv=Canvas(root) cnv.pack(expand=1,fill=BOTH) data.cnv=cnv img=Image.open('jupa9810.jpg') # some image you have..... raw_input("Hello") data.img=ImageTk.PhotoImage(img) data.photo=cnv.create_image(0,0,image=data.img,anchor='nw') data.ring=cnv.create_oval((100,100,300,300)) cnv.bind("",motioncallback) cnv.bind("",startmotioncallback) root.mainloop() Traceback (most recent call last): File "C:\Sandia_Meteors\Sentinel_Development\Development_Sentuser-Utilities\Playground\fun-move_object.py", line 45, in Demo(root) File "C:\Sandia_Meteors\Sentinel_Development\Development_Sentuser-Utilities\Playground\fun-move_object.py", line 35, in Demo data.img=ImageTk.PhotoImage(img) NameError: global name 'ImageTk' is not defined From jeremiah.dodds at gmail.com Thu Apr 9 06:42:48 2009 From: jeremiah.dodds at gmail.com (Jeremiah Dodds) Date: Thu, 9 Apr 2009 11:42:48 +0100 Subject: Retrieving a specific object from a list? Message-ID: <12cbbbfc0904090342n211dfa39gc0632fc818a48178@mail.gmail.com> I've been looking over some of my code, and I've found something I do that has a bit of a smell to it. I've searched the group and docs, and haven't found much of anything that solves this particular problem, although I may just not be searching correctly. Anyhow, I find that often I'll have a list of objects of some sort that I want to operate on. Most of the time, I'll want to operate on the entire list, but sometimes I'll want to operate on just one element, or retrieve just one element, and I end up with code something like the following: items = [Foo(), Foo(), ... Foo()] a_item = [x for x in items if x.bar == some_value][0] another_item = [x for x in items if x.baz == some_other_value][0] This doesn't seem correct at all, looping over the entire list to create a list of one element and then pulling that item out. Any advice? -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Thu Apr 9 07:01:31 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 09 Apr 2009 13:01:31 +0200 Subject: Problem with PIL/Tkinter Program Example References: Message-ID: W. eWatson wrote: > Something is amiss here. The program produces a canvas in which one can > move an object around. The input file is hard coded (see open). If you > want to try it, you'll need to provide a file. Python error below. Name > space difficulty? > Traceback (most recent call last): > File > "C \Sandia_Meteors\Sentinel_Development\Development_Sentuser-Utilities\Playground\fun-move_object.py", > line 45, in > Demo(root) > File > "C \Sandia_Meteors\Sentinel_Development\Development_Sentuser-Utilities\Playground\fun-move_object.py", > line 35, in Demo > data.img=ImageTk.PhotoImage(img) > NameError: global name 'ImageTk' is not defined If you want to use the ImageTk module you have to import it first: > #Mouse movement > from Tkinter import * > import PIL > import Image import ImageTk [rest of your code here] Peter From andrew at acooke.org Thu Apr 9 07:16:01 2009 From: andrew at acooke.org (andrew cooke) Date: Thu, 9 Apr 2009 07:16:01 -0400 (CLT) Subject: Retrieving a specific object from a list? In-Reply-To: <12cbbbfc0904090342n211dfa39gc0632fc818a48178@mail.gmail.com> References: <12cbbbfc0904090342n211dfa39gc0632fc818a48178@mail.gmail.com> Message-ID: <1e8cc02a53d768af1fa9a899b4ec32ef.squirrel@acooke.dyndns.org> Jeremiah Dodds wrote: > I've been looking over some of my code, and I've found something I do that > has a bit of a smell to it. I've searched the group and docs, and haven't > found much of anything that solves this particular problem, although I may > just not be searching correctly. > > Anyhow, I find that often I'll have a list of objects of some sort that I > want to operate on. Most of the time, I'll want to operate on the entire > list, but sometimes I'll want to operate on just one element, or retrieve > just one element, and I end up with code something like the following: > > items = [Foo(), Foo(), ... Foo()] > > a_item = [x for x in items if x.bar == some_value][0] > another_item = [x for x in items if x.baz == some_other_value][0] > > This doesn't seem correct at all, looping over the entire list to create a > list of one element and then pulling that item out. Any advice? [some/all of this may be obvious, but since you asked...] i think the most common solution (at least in my code) is to use a dict and store the data as a map from value to instance. you can still operate on all the instances via .values(), but you also have O(1) access via the key. another approach is to use a sorted list. then you can access things via the bisect module in O(log(n)) time. related to sorted lists, you may be able to reframe the algorithm as something that operates on a queue or stack (if your "some_value" is to find the "next" item according to an order that is fixed by earlier processing). in that case you want deque from collections. but when you need to access instances by more than one value (.bar and .baz) then typically that's a hard problem, and there's a trade-off somewhere. you might find writing a special container that contains two dicts is useful. if so, you might want to use weak references - see weakref module). all the above are relatively direct solutions. in my experience this kind of issue often comes from not thinking at a high enough level about the algorithm - even though my third suggestion (deque) sounds rather obscure you may find that once you look at you algorithm more carefully it can be rewritten in that way. i think i've seen this in my own code as i improve at integrating what might be more "functional" idioms into python in a "natural" (pythonic) way. andrew From jeremiah.dodds at gmail.com Thu Apr 9 07:24:28 2009 From: jeremiah.dodds at gmail.com (Jeremiah Dodds) Date: Thu, 9 Apr 2009 12:24:28 +0100 Subject: Retrieving a specific object from a list? In-Reply-To: <1e8cc02a53d768af1fa9a899b4ec32ef.squirrel@acooke.dyndns.org> References: <12cbbbfc0904090342n211dfa39gc0632fc818a48178@mail.gmail.com> <1e8cc02a53d768af1fa9a899b4ec32ef.squirrel@acooke.dyndns.org> Message-ID: <12cbbbfc0904090424u6d7e560jdb5764ea0c1f8279@mail.gmail.com> On Thu, Apr 9, 2009 at 12:16 PM, andrew cooke wrote: > but when you need to access instances by more than one value (.bar and > .baz) then typically that's a hard problem, and there's a trade-off > somewhere. you might find writing a special container that contains two > dicts is useful. if so, you might want to use weak references - see > weakref module). > > all the above are relatively direct solutions. in my experience this kind > of issue often comes from not thinking at a high enough level about the > algorithm - even though my third suggestion (deque) sounds rather obscure > you may find that once you look at you algorithm more carefully it can be > rewritten in that way. i think i've seen this in my own code as i improve > at integrating what might be more "functional" idioms into python in a > "natural" (pythonic) way. > > andrew > > Thanks, I have used a dict for doing what I was talking about in cases where I only ever want to be able to access by one property of the objects, but as you know, that doesn't work too well when I want to access by more than one property. It looks like I need to write a special class that acts as an iterable, and as a map on values of (more-or-less) arbitrary properties of the objects contained in it. I could probably also use with stepping back and looking at my problem from a higher level to make sure I'm not barking up an entirely wrong tree. I'll take a look at weakref and dequeue, and see if they help. -------------- next part -------------- An HTML attachment was scrubbed... URL: From andrew at acooke.org Thu Apr 9 07:53:46 2009 From: andrew at acooke.org (andrew cooke) Date: Thu, 9 Apr 2009 07:53:46 -0400 (CLT) Subject: Retrieving a specific object from a list? In-Reply-To: <1e8cc02a53d768af1fa9a899b4ec32ef.squirrel@acooke.dyndns.org> References: <12cbbbfc0904090342n211dfa39gc0632fc818a48178@mail.gmail.com> <1e8cc02a53d768af1fa9a899b4ec32ef.squirrel@acooke.dyndns.org> Message-ID: <98e20ea83cd34c563ef0a999be47fc11.squirrel@acooke.dyndns.org> andrew cooke wrote: [...] > but when you need to access instances by more than one value (.bar and > .baz) then typically that's a hard problem, and there's a trade-off > somewhere. you might find writing a special container that contains two > dicts is useful. if so, you might want to use weak references - see > weakref module). thinking a bit more about when this has occurred in my own programs, and it tends to be when i've ended up with a monster object/collection that is trying to do too many different things (typically because i am worrying about performance when i shouldn't be). in that case the fix has been to break things up and use simpler collections for each step, converting as necessary between steps. andrew From roy at panix.com Thu Apr 9 08:17:29 2009 From: roy at panix.com (Roy Smith) Date: Thu, 09 Apr 2009 08:17:29 -0400 Subject: Get the ipv6 address from a interface References: <86176ef7-c2e0-4c5d-b883-d91672e3eb0b@w40g2000yqd.googlegroups.com> Message-ID: In article <86176ef7-c2e0-4c5d-b883-d91672e3eb0b at w40g2000yqd.googlegroups.com>, Kai Timmer wrote: > Hello, > i need a function that returns the ipv6 address from a given interface > name. For ipv4 i use this one: > def get_ip_address(ifname): > s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) > return socket.inet_ntoa(fcntl.ioctl( > s.fileno(), > 0x8915, # SIOCGIFADDR > struct.pack('256s', ifname[:15]) > )[20:24]) > > which works great. But i am not enough into python to port that to > ipv6. It has to work under linux only. Any help is appreciated. I'm not 100% sure what you're trying to do, but the above is horribly non-portable. You probably want to be looking at socket.getpeername() and socket.getsockname(). In general, concepts like "the address of an interface" are difficult. In many OS's, a given interface may have multiple addresses. This is especially true in IPv6 where you've have both link local and global unicast addresses on the same interface. Can you back up a few steps and describe what it is that you're trying to do, i.e. the use case? From dns4 at cornell.edu Thu Apr 9 08:26:06 2009 From: dns4 at cornell.edu (David Smith) Date: Thu, 09 Apr 2009 08:26:06 -0400 Subject: Floor value in math operators In-Reply-To: <84f92182-9af6-445a-8ce0-18ee0bde44ce@w40g2000yqd.googlegroups.com> References: <84f92182-9af6-445a-8ce0-18ee0bde44ce@w40g2000yqd.googlegroups.com> Message-ID: AggieDan04 wrote: > On Apr 8, 12:08 pm, David Smith wrote: >> Avi wrote: >>> Hi, >>> This will be a very simple question to ask all the awesome programmers >>> here: >>> How can I get answer in in decimals for such a math operator: >>> 3/2 >>> I get 1. I want to get 1.5 >>> Thanks in advance, >>> Avi >> I'm going to assume your operands are variables instead of numeric >> literals. Why not consider doing a type conversion to float or Decimal >> and then perform the division? > > Because float(x) and Decimal(x) fail for complex numbers and lose > precision if x is a rational or a multi-precision float. The OP didn't ask for anything complicated or high precision -- just wanted to divide two integer values and get a float/Decimal output. --David From aahz at pythoncraft.com Thu Apr 9 09:06:17 2009 From: aahz at pythoncraft.com (Aahz) Date: 9 Apr 2009 06:06:17 -0700 Subject: group several methods under a attribute References: Message-ID: In article , Rhodri James wrote: > >You'll note that this is, all Aaron's protests to the contrary, >splitting your class up into multiple cooperating classes. Ayup. >If you're set on doing it like this, doing it this way avoids polluting >your namespace so much: > >class ClsB(object): > class ClsA(object): > def do_something(self): > print "Here's A doing something" > > def __init__(self): > self.A = ClsB.ClsA() > > def do_something(self): > print "Here's B doing something" Blech. The pollution caused by a separate class is minimal, and nested class definitions are likely to have problems (the most obvious is that pickling will break). -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ Why is this newsgroup different from all other newsgroups? From pecora at anvil.nrl.navy.mil Thu Apr 9 09:09:18 2009 From: pecora at anvil.nrl.navy.mil (Lou Pecora) Date: Thu, 09 Apr 2009 09:09:18 -0400 Subject: numpy.where References: <148739635947045949601137627602034810513-Webmail@me.com> Message-ID: In article , Neil Crighton wrote: > > I'm not sure exactly what you're trying to do, but maybe you want a boolean > array to select the right elements? So if time and energy are 1-d numpy arrays > of the same length: > > >>> condition = (min_time <= time) & (time <= max_time) > >>> new_time = time[condition] > >>> new_energy = energy[condition] > > There's also a Numpy list for these kind of questions: > > http://dir.gmane.org/gmane.comp.python.numeric.general > > Neil Thanks, Neil. Always something to learn. I've used Numpy for several years, but still have not plumbed the depths. Just tried this script and, yep, it works. arr=array([-1,1.0,2.2,-10.0,1.1, 0.9,-0.9]) cond= arr < 1.0 print cond brr=arr[cond] print brr Output: [ True False False True False True True] [ -1. -10. 0.9 -0.9] Really, I've gotta RTFM. :-) -- -- Lou Pecora From michael at stroeder.com Thu Apr 9 09:12:19 2009 From: michael at stroeder.com (=?ISO-8859-1?Q?Michael_Str=F6der?=) Date: Thu, 09 Apr 2009 15:12:19 +0200 Subject: ANN: python-ldap-2.3.7 Message-ID: Find a new release of python-ldap: http://python-ldap.sourceforge.net/ 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). Note that the download page has changed recently. You can now find the source distribution at PyPI: http://pypi.python.org/pypi/python-ldap/ Ciao, Michael. -- Michael Str?der E-Mail: michael at stroeder.com http://www.stroeder.com ---------------------------------------------------------------- Released 2.3.7 2009-04-09 Changes since 2.3.6: Lib/ * urllib.quote() is now used in LDAPUrlExtension.unparse() to quote all special URL characters in extension values Modules/ * Fixed ldapcontrol.c not to raise ldap.ENCODING_ERROR in function encode_rfc2696() on 64-bit systems * Fixed seg fault if error code in a LDAP response was outside the known error codes and could not be mapped to a specific exception class (thanks to Sean) * errors.c: LDAP_ERROR_MAX set to LDAP_PROXIED_AUTHORIZATION_DENIED if available in OpenLDAP header * new exception class ldap.PROXIED_AUTHORIZATION_DENIED if available in OpenLDAP header * Fixed functions.c not to raise ldap.ENCODING_ERROR in function l_ldap_str2dn() on 64-bit systems (see SF#2725356) ---------------------------------------------------------------- Released 2.3.6 2009-02-22 Changes since 2.3.5: Lib/ * Importing ldap.str2dn() which directly imported _ldap.str2dn() is prohibited now (see SF#2181141) Modules/ * get_option(): Added support for reading more SASL options. (OPT_X_SASL_MECH, OPT_X_SASL_REALM, OPT_X_SASL_AUTHCID and OPT_X_SASL_AUTHZID) * Added some explicit type casts to fix issues while building with SunStudio * Fixed compiling issue with GCC 4.4 (see SF#2555793, thanks to Matej and Martin) Doc/ * Clarified not to use ldap_get_dn() directly * Fixed description of ldap.SASL_AVAIL and ldap.TLS_AVAIL (see SF#2555804, thanks to Matej and Martin) From nigamreetesh84 at gmail.com Thu Apr 9 09:24:02 2009 From: nigamreetesh84 at gmail.com (reetesh nigam) Date: Thu, 9 Apr 2009 06:24:02 -0700 (PDT) Subject: Unicode Decode Error Message-ID: <0c3fe035-0321-4d76-a00c-2fb9e52079e1@u16g2000vbb.googlegroups.com> Hi All, There is some special character in my database. and when try to show on my UI it says return codecs.utf_8_decode(input, errors, True) UnicodeDecodeError: 'utf8' codec can't decode byte 0xc4 in position 10: unexpected end of data Can any one help to solve this problem From benjamin at python.org Thu Apr 9 09:27:04 2009 From: benjamin at python.org (Benjamin Peterson) Date: Thu, 9 Apr 2009 13:27:04 +0000 (UTC) Subject: Can someone explain about the usage of unittest.TestSuite? References: <61448.25339.qm@web7903.mail.in.yahoo.com> Message-ID: srinivasan srinivas yahoo.co.in> writes: > > > Hi, > I would like to know about the unittest.TestSuite clearly like at what situations i can use this TestSuite? > I am not getting the clear difference between this and unittest.TestCase. You write your actual tests with TestCase. A TestSuite stores many TestCases. From sxn02 at yahoo.com Thu Apr 9 10:16:10 2009 From: sxn02 at yahoo.com (Sorin Schwimmer) Date: Thu, 9 Apr 2009 07:16:10 -0700 (PDT) Subject: Autocompletion Message-ID: <569472.4801.qm@web56004.mail.re3.yahoo.com> Hi All, Does anybody knows about a module to assist with text autocompletion for a given dictionary? Thanks, SxN __________________________________________________________________ Ask a question on any topic and get answers from real people. Go to Yahoo! Answers and share what you know at http://ca.answers.yahoo.com From jjposner at snet.net Thu Apr 9 10:35:01 2009 From: jjposner at snet.net (John Posner) Date: Thu, 09 Apr 2009 10:35:01 -0400 Subject: Why does Python show the whole array? In-Reply-To: References: <7e7a386f-d336-4186-822d-c6af0a581751@e38g2000vbe.googlegroups.com> Message-ID: <49DE0795.2090704@snet.net> Lawrence D'Oliveiro wrote: > Fine if it only happened once. But it's a commonly-made mistake. At some > point you have to conclude that not all those people are stupid, there > really is something wrong with the design. I think "something wrong with the design" is overstating the case a bit, and is likely to elicit some negative reactions in this forum. But I agree with your point, to this extent: this situation illustrates the way in which Python "overloads" the number zero: Realm A: "0" indicates the first position in a sequence Realm B: "0" indicates the Boolean value "False" You just need to remember that the find() function works in Realm A, and the "in" operator works in Realm B. Q: Has anyone on the python-dev list ever proposed a "string"-module function that does the job of the "in" operator? Maybe this: if test.contains(item) # would return a Boolean value From google at mrabarnett.plus.com Thu Apr 9 10:40:54 2009 From: google at mrabarnett.plus.com (MRAB) Date: Thu, 09 Apr 2009 15:40:54 +0100 Subject: Unicode Decode Error In-Reply-To: <0c3fe035-0321-4d76-a00c-2fb9e52079e1@u16g2000vbb.googlegroups.com> References: <0c3fe035-0321-4d76-a00c-2fb9e52079e1@u16g2000vbb.googlegroups.com> Message-ID: <49DE08F6.9070206@mrabarnett.plus.com> reetesh nigam wrote: > Hi All, > There is some special character in my database. > and when try to show on my UI it says > > return codecs.utf_8_decode(input, errors, True) > UnicodeDecodeError: 'utf8' codec can't decode byte 0xc4 in position > 10: unexpected end of data > > > Can any one help to solve this problem > Perhaps it isn't valid UTF-8, but 8-bit characters in some encoding. From heidihannah at mac.com Thu Apr 9 10:46:01 2009 From: heidihannah at mac.com (heidi taynton) Date: Thu, 09 Apr 2009 10:46:01 -0400 Subject: writing array Message-ID: <119768862180497088975118472279546657608-Webmail@me.com> Hey guys, I'm trying to write a new array from the one i already have.... but here is the catch: I want an array where n=# of data points used in previous point (rounded down) in every spot, I want to take 1.1**n data points and then take the mean of the data points. (i know there is a numpy function mean() ).. put that is spot 0 and so on... any suggestions? for clarification: so the first spot takes 1 data point, b/c 1.1^0 =1 and the average of that is just the data point the second spot takes 2 data points b/c 1.1^1=1.1 which rounds up to 2.... then the average of those two the third spot takes 4 data points b/c 1.1^2 = 1.21 .... and then that will repeat.... make sense? hi From sjmachin at lexicon.net Thu Apr 9 10:46:52 2009 From: sjmachin at lexicon.net (John Machin) Date: Thu, 9 Apr 2009 07:46:52 -0700 (PDT) Subject: Why does Python show the whole array? References: <7e7a386f-d336-4186-822d-c6af0a581751@e38g2000vbe.googlegroups.com> Message-ID: <6cf1aebd-5afa-45e1-a4d3-061381218d9b@m24g2000vbp.googlegroups.com> On Apr 10, 12:35?am, John Posner wrote: > Lawrence D'Oliveiro wrote: > > ?> Fine if it only happened once. But it's a commonly-made mistake. At some > ?> point you have to conclude that not all those people are stupid, there > ?> really is something wrong with the design. > > I think "something wrong with the design" is overstating the case a bit, > and is likely to elicit some negative reactions in this forum. But I > agree with your point, to this extent: this situation illustrates the > way in which Python "overloads" the number zero: > > ? Realm A: ?"0" indicates the first position in a sequence > ? Realm B: ? "0" indicates the Boolean value "False" > > You just need to remember that the find() function works in Realm A, and > the "in" operator works in Realm B. > > Q: Has anyone on the python-dev list ever proposed a "string"-module > function that does the job of the "in" operator? Maybe this: > > ? if test.contains(item) ? ? # would return a Boolean value Yes. command_prompt> \python23\python -c "print 'frobozz'.__contains_ _('obo')" True Was first introduced in Python 2.2, with only a single character allowed in the arg. From digitig at gmail.com Thu Apr 9 10:54:05 2009 From: digitig at gmail.com (Tim Rowe) Date: Thu, 9 Apr 2009 15:54:05 +0100 Subject: Why does Python show the whole array? In-Reply-To: References: Message-ID: 2009/4/9 Miles : > Clearly, any comparison with a boolean literal should be illegal. ?;) Hey, we could have strict type checking at compile time of /all/ operations, couldn't we? Anybody care to join me over at the Ada list? ;-) -- Tim Rowe From sjmachin at lexicon.net Thu Apr 9 10:59:59 2009 From: sjmachin at lexicon.net (John Machin) Date: Thu, 9 Apr 2009 07:59:59 -0700 (PDT) Subject: Unicode Decode Error References: <0c3fe035-0321-4d76-a00c-2fb9e52079e1@u16g2000vbb.googlegroups.com> Message-ID: On Apr 10, 12:40?am, MRAB wrote: > reetesh nigam wrote: > > Hi All, > > There is some special character in my database. > > and when try to show on my UI it says > > > ? ? return codecs.utf_8_decode(input, errors, True) > > UnicodeDecodeError: 'utf8' codec can't decode byte 0xc4 in position > > 10: unexpected end of data > > > Can any one help to solve this problem > > Perhaps it isn't valid UTF-8, but 8-bit characters in some encoding. Or perhaps it is valid UTF-8, but the OP's code is picking up strg [:nchars] instead of strg[:nbytes] or somehow otherwise separating the two (or maybe even 3) bytes that UTF-8 needs to make up the character. Perhaps the OP might print repr(all_the_undecoded_bytes_he_has) then we might have a clue about how to give him a clue ... From rkmr.em at gmail.com Thu Apr 9 11:05:09 2009 From: rkmr.em at gmail.com (rkmr.em at gmail.com) Date: Thu, 9 Apr 2009 08:05:09 -0700 Subject: moving to processing from threading, global variables Message-ID: hi i am trying to move from threading to processing package. this is the controller i used to spawn new threads, and it used the global variable done to check if it needs to spawn more threads or not. it worked great for me. it checks if there is new data to be processed every 30 seconds, and spawns of new threads to do the work, till all work is done. but now in processing, anychange made to global variable done in sendalert method, is not reflected in the controller method. can please point out what is wrong? thanks a lot! code done = False def sendalert(): global done users = q.get('xx') if not users: done = True return done = False for u in users: do stuff def control(number_threads_min=3, number_threads_max = 100): global done while True: number_threads = len(processing.activeChildren()) if not done and number_threads From rdmurray at bitdance.com Thu Apr 9 11:17:23 2009 From: rdmurray at bitdance.com (R. David Murray) Date: Thu, 9 Apr 2009 15:17:23 +0000 (UTC) Subject: communication between objects - help References: Message-ID: Murali kumar wrote: > hi all.. > > > please see my attached document.. I think you'll get more help if you post in plain text. -- R. David Murray http://www.bitdance.com From akitada at gmail.com Thu Apr 9 11:22:37 2009 From: akitada at gmail.com (Akira Kitada) Date: Fri, 10 Apr 2009 00:22:37 +0900 Subject: Why is it that *dbm modules don't provide an iterator? (Language design question) Message-ID: <90bb445a0904090822h63ee44e1g1f57ad303593f1cd@mail.gmail.com> Hi, I was wondering why *dbm modules in Python do not give us an iterable interface? Take a look at an example below """ # Python 2.6 >>> import gdbm >>> d = gdbm.open("spam.db", "n") >>> d["key1"] = "ham" >>> d["key2"] = "spam" >>> >>> for k in d: ... print k ... Traceback (most recent call last): File "", line 1, in TypeError: 'gdbm.gdbm' object is not iterable """ The loop has to be: """ >>> k = d.firstkey() >>> while k != None: ... print k ... k = d.nextkey(k) key2 key1 """ I would like to know the background of this design decision. Some research on this led me to this ticket: http://bugs.python.org/issue662923 It looks I'm not the only one who wondered on this. Thanks in advance. From rdmurray at bitdance.com Thu Apr 9 11:51:47 2009 From: rdmurray at bitdance.com (R. David Murray) Date: Thu, 9 Apr 2009 15:51:47 +0000 (UTC) Subject: xml.dom.minidom getElementsByTagName white space issue References: <656946.72976.qm@web110406.mail.gq1.yahoo.com> Message-ID: Leonardo lozanne wrote: > Hi, > ? > I'm getting some XML tags with white spaces from a web service and when I try to get them with the getElements ByTagName I'm not able to do so. I'm getting an empty list. What I'm doing is: > ? > #XML_response is an xml string > xml_msg = xml.dom.minidom.parseString(XML_response) > ? > nodes = xml_msg.getElementsByTagName("tag ten")? #tag name is "tag ten" with a whitespace > ? > It all works fine with tags like tag_seven but NOT for tag names with a white space. I've tried some escape chars but it doesnt seems to work. > ? > Does anybody has the escape char sequence I should be using or a work around for this? Thanks in advanced for your replies. As far as I can tell what you are getting is invalid XML. So I think the answer is "you can't do that". (cf: http://www.w3.org/TR/2004/REC-xml11-20040204/#NT-NameChar) -- R. David Murray http://www.bitdance.com From notvalid2 at sbcglobal.net Thu Apr 9 11:52:33 2009 From: notvalid2 at sbcglobal.net (W. eWatson) Date: Thu, 09 Apr 2009 08:52:33 -0700 Subject: Problem with PIL/Tkinter Program Example In-Reply-To: References: Message-ID: <4RoDl.22919$c45.1342@nlpi065.nbdc.sbc.com> Peter Otten wrote: > W. eWatson wrote: > >> Something is amiss here. The program produces a canvas in which one can >> move an object around. The input file is hard coded (see open). If you >> want to try it, you'll need to provide a file. Python error below. Name >> space difficulty? > >> Traceback (most recent call last): >> File >> "C > \Sandia_Meteors\Sentinel_Development\Development_Sentuser-Utilities\Playground\fun-move_object.py", >> line 45, in >> Demo(root) >> File >> "C > \Sandia_Meteors\Sentinel_Development\Development_Sentuser-Utilities\Playground\fun-move_object.py", >> line 35, in Demo >> data.img=ImageTk.PhotoImage(img) >> NameError: global name 'ImageTk' is not defined > > If you want to use the ImageTk module you have to import it first: > >> #Mouse movement >> from Tkinter import * >> import PIL >> import Image > > import ImageTk > > [rest of your code here] > > Peter Very good. Thanks. -- 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 python.list at tim.thechases.com Thu Apr 9 11:53:29 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 09 Apr 2009 10:53:29 -0500 Subject: Autocompletion In-Reply-To: <569472.4801.qm@web56004.mail.re3.yahoo.com> References: <569472.4801.qm@web56004.mail.re3.yahoo.com> Message-ID: <49DE19F9.4020002@tim.thechases.com> > Does anybody knows about a module to assist with text > autocompletion for a given dictionary? On machines with the GNU readline library available, Python provides the "readline" module which should allow for auto-completion. -tkc From hniksic at xemacs.org Thu Apr 9 11:54:27 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Thu, 09 Apr 2009 17:54:27 +0200 Subject: Why does Python show the whole array? References: <7e7a386f-d336-4186-822d-c6af0a581751@e38g2000vbe.googlegroups.com> Message-ID: <87myapkeks.fsf@busola.homelinux.net> John Posner writes: > Q: Has anyone on the python-dev list ever proposed a "string"-module > function that does the job of the "in" operator? Maybe this: > > if test.contains(item) # would return a Boolean value That's a string method, not a function in the string module. If you want a function, use operator.contains: >>> import operator >>> operator.contains('foo', 'o') True From joel.hedlund at gmail.com Thu Apr 9 11:56:49 2009 From: joel.hedlund at gmail.com (Joel Hedlund) Date: Thu, 09 Apr 2009 17:56:49 +0200 Subject: safe eval of moderately simple math expressions Message-ID: Hi all! I'm writing a program that presents a lot of numbers to the user, and I want to let the user apply moderately simple arithmentics to these numbers. One possibility that comes to mind is to use the eval function, but since that sends up all kinds of warning flags in my head, I thought I'd put my idea out here first so you guys can tell me if I'm insane. :-) This is the gist of it: ---------------------------------------------------------- import math globals = dict((s, getattr(math, s)) for s in dir(math) if '_' not in s) globals.update(__builtins__=None, divmod=divmod, round=round) def calc(expr, x): if '_' in expr: raise ValueError("expr must not contain '_' characters") try: return eval(expr, globals, dict(x=x)) except: raise ValueError("bad expr or x") print calc('cos(x*pi)', 1.33) ---------------------------------------------------------- This lets the user do stuff like "exp(-0.01*x)" or "round(100*x)" but prevents malevolent stuff like "__import__('os').system('del *.*')" or "(t for t in (42).__class__.__base__.__subclasses__() if t.__name__ == 'file').next()" from messing things up. I assume there's lots of nasty and absolutely lethal stuff that I've missed, and I kindly request you show me the error of my ways. Thank you for your time! /Joel Hedlund From alanmk at hotmail.com Thu Apr 9 12:12:02 2009 From: alanmk at hotmail.com (Alan Kennedy) Date: Thu, 9 Apr 2009 09:12:02 -0700 (PDT) Subject: Jython on Google AppEngine. Message-ID: <537ac6bf-dda0-4509-a0b6-6f67551f1015@r37g2000yqn.googlegroups.com> Hi all, You may be interested to know that you can now run jython 2.2 out of the box on Google AppEngine, thanks to their new java support. A patch is required for jython 2.5, but we will be folding this in before the jython 2.5 RC release over the next few weeks. More details here http://jython.xhaus.com Regards, Alan. From python at bdurham.com Thu Apr 9 12:18:27 2009 From: python at bdurham.com (python at bdurham.com) Date: Thu, 09 Apr 2009 12:18:27 -0400 Subject: Recommendations on Pythonic tree data structure design techniques Message-ID: <1239293907.1795.1309794163@webmail.messagingengine.com> Any recommendations on Python based tree data structures that I can study? I'm working on an application that will model a basic outline structure (simple tree) and am looking for ideas on Pythonic implementation techniques. By outline I mean a traditional hierarchical document outline (section, chapter, sub-chapter, ...). I will be building this structure as I parse my customer's internally designed (proprietary) publishing markup language. My initial thought is to implement a generic node container class with attributes for parent, next, previous, and child 'pointers'. The node objects will be stored in a dictionary where node 'pointers' correspond to incrementally assigned numeric keys. Thanks! Malcolm -------------- next part -------------- An HTML attachment was scrubbed... URL: From mnordhoff at mattnordhoff.com Thu Apr 9 12:25:51 2009 From: mnordhoff at mattnordhoff.com (Matt Nordhoff) Date: Thu, 09 Apr 2009 16:25:51 +0000 Subject: safe eval of moderately simple math expressions In-Reply-To: References: Message-ID: <49DE218F.7010607@mattnordhoff.com> Joel Hedlund wrote: > Hi all! > > I'm writing a program that presents a lot of numbers to the user, and I > want to let the user apply moderately simple arithmentics to these > numbers. One possibility that comes to mind is to use the eval function, > but since that sends up all kinds of warning flags in my head, I thought > I'd put my idea out here first so you guys can tell me if I'm insane. :-) > > This is the gist of it: > ---------------------------------------------------------- > import math > > globals = dict((s, getattr(math, s)) for s in dir(math) if '_' not in s) > globals.update(__builtins__=None, divmod=divmod, round=round) > > def calc(expr, x): > if '_' in expr: > raise ValueError("expr must not contain '_' characters") > try: > return eval(expr, globals, dict(x=x)) > except: > raise ValueError("bad expr or x") > > print calc('cos(x*pi)', 1.33) > ---------------------------------------------------------- > > This lets the user do stuff like "exp(-0.01*x)" or "round(100*x)" but > prevents malevolent stuff like "__import__('os').system('del *.*')" or > "(t for t in (42).__class__.__base__.__subclasses__() if t.__name__ == > 'file').next()" from messing things up. > > I assume there's lots of nasty and absolutely lethal stuff that I've > missed, and I kindly request you show me the error of my ways. > > Thank you for your time! > /Joel Hedlund I'm way too dumb and lazy to provide a working example, but someone could work around the _ restriction by obfuscating them a bit, like this: >>> '\x5f' '_' >>> getattr(42, '\x5f\x5fclass\x5f\x5f') # __class__ Is that enough to show you the error of your ways? :-D Cuz seriously, it's a bad idea. I'm sorry, but I don't know a good solution. The simplicity of eval is definitely very attractive, but it's just not safe. (BTW: What if a user tries to do some ridiculously large calculation to DoS the app? Is that a problem?) -- From jjposner at snet.net Thu Apr 9 12:36:24 2009 From: jjposner at snet.net (John Posner) Date: Thu, 09 Apr 2009 12:36:24 -0400 Subject: Why does Python show the whole array? In-Reply-To: <87myapkeks.fsf@busola.homelinux.net> References: <7e7a386f-d336-4186-822d-c6af0a581751@e38g2000vbe.googlegroups.com> <87myapkeks.fsf@busola.homelinux.net> Message-ID: <49DE2408.4030105@snet.net> Hrvoje Niksic wrote: > if test.contains(item) # would return a Boolean value > >> That's a string method, not a function in the string module. Oops, of course. >>>> import operator >>>> operator.contains('foo', 'o') That's pretty good, and IMHO a bit better than John Machin's suggestion to use the __contains__() method. (I have this prejudice that using the __XXX__ methods in "everyday code" is cheating.) Given how common string maniuplations are, I guess I'm surprised that Python hasn't yet made "contains()" into both a "string"-module function *and* a string-object method. From __peter__ at web.de Thu Apr 9 12:46:16 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 09 Apr 2009 18:46:16 +0200 Subject: Why does Python show the whole array? References: <7e7a386f-d336-4186-822d-c6af0a581751@e38g2000vbe.googlegroups.com> <87myapkeks.fsf@busola.homelinux.net> Message-ID: John Posner wrote: > Given how common string maniuplations are, I guess I'm surprised that > Python hasn't yet made "contains()" into both a "string"-module function > *and* a string-object method. Could you explain why you prefer 'contains(belly, beer)' or 'belly.contains(beer)' over 'beer in belly'? The last form may be a bit harder to find in the documentation, but once a newbie has learned about it he'll find it easy to remember. Peter From castironpi at gmail.com Thu Apr 9 12:55:02 2009 From: castironpi at gmail.com (Aaron Brady) Date: Thu, 9 Apr 2009 09:55:02 -0700 (PDT) Subject: safe eval of moderately simple math expressions References: Message-ID: <041f540c-1f2a-48fd-9329-245fe746b11d@k41g2000yqh.googlegroups.com> On Apr 9, 10:56?am, Joel Hedlund wrote: > Hi all! > > I'm writing a program that presents a lot of numbers to the user, and I > want to let the user apply moderately simple arithmentics to these > numbers. One possibility that comes to mind is to use the eval function, > but since that sends up all kinds of warning flags in my head, I thought > I'd put my idea out here first so you guys can tell me if I'm insane. :-) > > This is the gist of it: snip > def calc(expr, x): > ? ? ?if '_' in expr: > ? ? ? ? ?raise ValueError("expr must not contain '_' characters") snip > I assume there's lots of nasty and absolutely lethal stuff that I've > missed, and I kindly request you show me the error of my ways. > > Thank you for your time! > /Joel Hedlund Would you be willing to examine a syntax tree to determine if there are any class accesses? Would it work? From tjreedy at udel.edu Thu Apr 9 13:06:28 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 09 Apr 2009 13:06:28 -0400 Subject: Retrieving a specific object from a list? In-Reply-To: <12cbbbfc0904090342n211dfa39gc0632fc818a48178@mail.gmail.com> References: <12cbbbfc0904090342n211dfa39gc0632fc818a48178@mail.gmail.com> Message-ID: Jeremiah Dodds wrote: > I've been looking over some of my code, and I've found something I do > that has a bit of a smell to it. I've searched the group and docs, and > haven't found much of anything that solves this particular problem, > although I may just not be searching correctly. > > Anyhow, I find that often I'll have a list of objects of some sort that > I want to operate on. Most of the time, I'll want to operate on the > entire list, but sometimes I'll want to operate on just one element, or > retrieve just one element, and I end up with code something like the > following: > > items = [Foo(), Foo(), ... Foo()] > > a_item = [x for x in items if x.bar == some_value][0] > another_item = [x for x in items if x.baz == some_other_value][0] > > This doesn't seem correct at all, looping over the entire list to create > a list of one element and then pulling that item out. Any advice? for x in items: if x.baz == some_other_value: break From andrew at acooke.org Thu Apr 9 13:11:22 2009 From: andrew at acooke.org (andrew cooke) Date: Thu, 9 Apr 2009 13:11:22 -0400 (CLT) Subject: Why does Python show the whole array? In-Reply-To: References: <7e7a386f-d336-4186-822d-c6af0a581751@e38g2000vbe.googlegroups.com> <87myapkeks.fsf@busola.homelinux.net> Message-ID: Peter Otten wrote: > John Posner wrote: > >> Given how common string maniuplations are, I guess I'm surprised that >> Python hasn't yet made "contains()" into both a "string"-module function >> *and* a string-object method. > > Could you explain why you prefer 'contains(belly, beer)' > or 'belly.contains(beer)' over 'beer in belly'? The last form may be a bit > harder to find in the documentation, but once a newbie has learned about > it > he'll find it easy to remember. i don't know why i get involved in this type of discussion, but.... while i agree that "in" is the correct answer, i am not sure it's obvious or easy to remember once you know it. perhaps to a newbie with a sweet, innocent and unsullied mind. but for an already corrupted programmer like myself, it's not easy to remember - i keep forgetting it! i think it's because i associate "in" with iteration, and assume everything else will be method calls or functions (and from earlier discussions here, it's clear some people are even more blinkered, and think everything should be methods....) so yeah, "in" is right, but arguments about what is natural and easy aren't really worth wasting bits over... andrew From tjreedy at udel.edu Thu Apr 9 13:13:50 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 09 Apr 2009 13:13:50 -0400 Subject: safe eval of moderately simple math expressions In-Reply-To: References: Message-ID: Joel Hedlund wrote: > Hi all! > > I'm writing a program that presents a lot of numbers to the user, and I > want to let the user apply moderately simple arithmentics to these > numbers. One possibility that comes to mind is to use the eval function, > but since that sends up all kinds of warning flags in my head, Where does the program execute? If on the user's own machine, no problem. Eval is no more dangerous than Python itself. From avinashrude at gmail.com Thu Apr 9 13:14:52 2009 From: avinashrude at gmail.com (Avi) Date: Thu, 9 Apr 2009 10:14:52 -0700 (PDT) Subject: Floor value in math operators References: <84f92182-9af6-445a-8ce0-18ee0bde44ce@w40g2000yqd.googlegroups.com> Message-ID: <1f39041a-d066-4ceb-b500-099cf6b74faa@z1g2000yqn.googlegroups.com> On Apr 9, 8:26?am, David Smith wrote: > AggieDan04 wrote: > > On Apr 8, 12:08 pm, David Smith wrote: > >> Avi wrote: > >>> Hi, > >>> This will be a very simple question to ask all the awesome programmers > >>> here: > >>> How can I get answer in in decimals for such a math operator: > >>> 3/2 > >>> I get 1. I want to get 1.5 > >>> Thanks in advance, > >>> Avi > >> I'm going to assume your operands are variables instead of numeric > >> literals. ?Why not consider doing a type conversion to float or Decimal > >> and then perform the division? > > > Because float(x) and Decimal(x) fail for complex numbers and lose > > precision if x is a rational or a multi-precision float. > > The OP didn't ask for anything complicated or high precision -- just > wanted to divide two integer values and get a float/Decimal output. > > --David Wow! I am overwhelmed by how much support I have at my finger tips. I was hesitant to ask such a silly question and was worrying of being ridiculed. Clearly, I have gained a lot by reading everyone's responses. Thanks! Avi From avinashrude at gmail.com Thu Apr 9 13:19:17 2009 From: avinashrude at gmail.com (Avi) Date: Thu, 9 Apr 2009 10:19:17 -0700 (PDT) Subject: Scrap Posts Message-ID: Hey Folks, I love this group and all the awesome and python savvy people who post here. However I also see some dumb posts like 'shoes' or something related to sex :( What can we do about crap like this? Can we clean it up? Or atleast flag some for removal. Moderators? From andrew at acooke.org Thu Apr 9 13:28:26 2009 From: andrew at acooke.org (andrew cooke) Date: Thu, 9 Apr 2009 13:28:26 -0400 (CLT) Subject: Scrap Posts In-Reply-To: References: Message-ID: are you on the mailing list (python-list at python.org) or reading via google groups? groups is full of junk, but the list is filtered. the (filtered) list is also available via gmane and similar. (disclaimer - i also use spamassasin so it's possible that is cleaning the mail up, but this discussion has happened before, here and on -dev, and people have generally acclaimed the list filtering). andrew Avi wrote: > Hey Folks, > > I love this group and all the awesome and python savvy people who post > here. However I also see some dumb posts like 'shoes' or something > related to sex :( > > What can we do about crap like this? Can we clean it up? Or atleast > flag some for removal. > > Moderators? > -- > http://mail.python.org/mailman/listinfo/python-list > > From mnordhoff at mattnordhoff.com Thu Apr 9 13:29:06 2009 From: mnordhoff at mattnordhoff.com (Matt Nordhoff) Date: Thu, 09 Apr 2009 17:29:06 +0000 Subject: Scrap Posts In-Reply-To: References: Message-ID: <49DE3062.6090005@mattnordhoff.com> Avi wrote: > Hey Folks, > > I love this group and all the awesome and python savvy people who post > here. However I also see some dumb posts like 'shoes' or something > related to sex :( > > What can we do about crap like this? Can we clean it up? Or atleast > flag some for removal. > > Moderators? FWIW, this newsgroup is mirrored (both ways) as a mailing list. The list server runs some pretty effective anti-spam software, and you could subscribe to it: -- From pavlovevidence at gmail.com Thu Apr 9 13:33:37 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 9 Apr 2009 10:33:37 -0700 (PDT) Subject: numpy.where References: <148739635947045949601137627602034810513-Webmail@me.com> <1d1b090a-8046-4bb2-aba9-7c935aa64735@e18g2000yqo.googlegroups.com> Message-ID: <185e64a2-628c-4814-a88f-a3b3b603c654@e2g2000vbe.googlegroups.com> On Apr 9, 2:58?am, Neil Crighton wrote: > Carl Banks gmail.com> writes: > > > > >>> condition = (min_time <= time) & (time <= max_time) > > > >>> new_time = time[condition] > > > >>> new_energy = energy[condition] > > > Won't work: condition is an array of ones and zeros, but you need to > > index the arrays with indices. ?So, add a call to nonzero to get the > > indices of the elements. > > > elements = nonzero(logical_and(min_time<=time,max_time>=time)) > > > new_time = time[elements] > > new_energy = energy[elements] > > It will work - try it :) > > You can index numpy arrays with either an array of indices (dtype integer) > ,or a boolean array (dtype bool). In the case above condition is a boolean > array. In general using a boolean array is faster, because you skip the call > to nonzero(). New to me. Thanks for the tip. Carl Banks From aahz at pythoncraft.com Thu Apr 9 14:10:46 2009 From: aahz at pythoncraft.com (Aahz) Date: 9 Apr 2009 11:10:46 -0700 Subject: Returning different types based on input parameters References: Message-ID: In article , George Sakkis wrote: > >To take it further, what if f wants to return different types, >differing even in a duck-type sense? That's easier to illustrate in a >API-extension scenario. Say that there is an existing function `solve >(x)` that returns `Result` instances. Later someone wants to extend f >by allowing an extra optional parameter `foo`, making the signature >`solve(x, foo=None)`. As long as the return value remains backward >compatible, everything's fine. However, what if in the extended case, >solve() has to return some *additional* information apart from >`Result`, say the confidence that the result is correct ? In short, >the extended API would be: > > def solve(x, foo=None): > ''' > @rtype: `Result` if foo is None; (`Result`, confidence) >otherwise. > ''' > >Strictly speaking, the extension is backwards compatible; previous >code that used `solve(x)` will still get back `Result`s. The problem >is that in new code you can't tell what `solve(x,y)` returns unless >you know something about `y`. My question is, is this totally >unacceptable and should better be replaced by a new function `solve2 >(x, foo=None)` that always returns (`Result`, confidence) tuples, or >it might be a justifiable cost ? Any other API extension approaches >that are applicable to such situations ? For this particular trick, I would always use a unique sentinel value so that *only* passing an argument would change the result signature: sentinel = object() def solve(x, foo=sentinel): ''' @rtype: `Result` if foo is sentinel; (`Result`, confidence) otherwise. ''' But I agree with other respondents that this is a code stink. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ Why is this newsgroup different from all other newsgroups? From fetchinson at googlemail.com Thu Apr 9 14:19:39 2009 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Thu, 9 Apr 2009 11:19:39 -0700 Subject: Recommendations on Pythonic tree data structure design techniques In-Reply-To: <1239293907.1795.1309794163@webmail.messagingengine.com> References: <1239293907.1795.1309794163@webmail.messagingengine.com> Message-ID: > Any recommendations on Python based tree data structures that I > can study? I'm working on an application that will model a basic > outline structure (simple tree) and am looking for ideas on > Pythonic implementation techniques. By outline I mean a > traditional hierarchical document outline (section, chapter, > sub-chapter, ...). I will be building this structure as I parse > my customer's internally designed (proprietary) publishing markup > language. > My initial thought is to implement a generic node container class > with attributes for parent, next, previous, and child 'pointers'. > The node objects will be stored in a dictionary where node > 'pointers' correspond to incrementally assigned numeric keys. http://www.google.com/search?q=python+tree+data+structure HTH, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From ptmcg at austin.rr.com Thu Apr 9 14:28:35 2009 From: ptmcg at austin.rr.com (Paul McGuire) Date: Thu, 9 Apr 2009 11:28:35 -0700 (PDT) Subject: safe eval of moderately simple math expressions References: Message-ID: On Apr 9, 10:56?am, Joel Hedlund wrote: > Hi all! > > I'm writing a program that presents a lot of numbers to the user, and I > want to let the user apply moderately simple arithmentics to these > numbers. Joel - Take a look at the examples page on the pyparsing wiki (http:// pyparsing.wikispaces.com/Examples). Look at the examples fourFn.py and simpleArith.py for some expression parsers that you could extend to support whatever math builtins you wish. Since you would be doing your own parsing and eval code, you could be sure that no dangerous code was being run, just simple arithmetic. -- Paul From invalid at invalid Thu Apr 9 14:28:38 2009 From: invalid at invalid (Grant Edwards) Date: Thu, 09 Apr 2009 13:28:38 -0500 Subject: Scrap Posts References: Message-ID: On 2009-04-09, Avi wrote: > I love this group and all the awesome and python savvy people > who post here. However I also see some dumb posts like 'shoes' > or something related to sex :( And occasionally both. ;) > What can we do about crap like this? I don't know about "we", but you have a few options: 1. Use an NNTP server with more sophisticated spam filtering. 2. Filter out everything posted from google groups. 3. Read the group via the mailing list (which has much better filtering). The mailing list is gatewayed by gmane.org if you prefer web or NNTP access instead of seeing stuff in your inbox. I rely mostly on option 2. You loose some non-spam posts, but anybody posting from Google Groups ought not be surprised to be lost among the spam. > Can we clean it up? Not really, no. Google could, but they refuse. > Or atleast flag some for removal. Not many NNTP servers honor cancels these days. > Moderators? There are none: c.l.p is unmoderated. -- Grant Edwards grante Yow! Gibble, Gobble, we at ACCEPT YOU ... visi.com From jason.scheirer at gmail.com Thu Apr 9 14:40:35 2009 From: jason.scheirer at gmail.com (Jason Scheirer) Date: Thu, 9 Apr 2009 11:40:35 -0700 (PDT) Subject: Jython on Google AppEngine. References: <537ac6bf-dda0-4509-a0b6-6f67551f1015@r37g2000yqn.googlegroups.com> Message-ID: <85d1d6ee-e8b8-48f5-87e0-41fb2fc03245@z1g2000yqn.googlegroups.com> On Apr 9, 9:12?am, Alan Kennedy wrote: > Hi all, > > You may be interested to know that you can now run jython 2.2 out of > the box on Google AppEngine, thanks to their new java support. > > A patch is required for jython 2.5, but we will be folding this in > before the jython 2.5 RC release over the next few weeks. > > More details here > > http://jython.xhaus.com > > Regards, > > Alan. Finally! A way to run Python on App Engine! From hackingkk at gmail.com Thu Apr 9 14:44:08 2009 From: hackingkk at gmail.com (Krishnakant) Date: Fri, 10 Apr 2009 00:14:08 +0530 Subject: Can someone explain about the usage of unittest.TestSuite? In-Reply-To: <61448.25339.qm@web7903.mail.in.yahoo.com> References: <61448.25339.qm@web7903.mail.in.yahoo.com> Message-ID: <1239302648.9493.6.camel@kk-laptop> On Thu, 2009-04-09 at 12:35 +0530, srinivasan srinivas wrote: > Hi, > I would like to know about the unittest.TestSuite clearly like at what situations i can use this TestSuite? I am not getting the clear difference between this and unittest.TestCase. > > Thanks, > Srini > Isn't that pritty simple and streight? test suite is used when you want to combine the different test cases. you can find more insight into this at http://www.eecho.info/Echo/python/test-driven-development-in-python/ happy hacking. Krishnakant From debatem1 at gmail.com Thu Apr 9 15:16:57 2009 From: debatem1 at gmail.com (CTO) Date: Thu, 9 Apr 2009 12:16:57 -0700 (PDT) Subject: safe eval of moderately simple math expressions References: Message-ID: how about sympy? http://code.google.com/p/sympy/ From jjposner at snet.net Thu Apr 9 15:21:52 2009 From: jjposner at snet.net (John Posner) Date: Thu, 09 Apr 2009 15:21:52 -0400 Subject: Why does Python show the whole array? In-Reply-To: References: <7e7a386f-d336-4186-822d-c6af0a581751@e38g2000vbe.googlegroups.com> <87myapkeks.fsf@busola.homelinux.net> Message-ID: <49DE4AD0.1080809@snet.net> > Peter Otten wrote: >> Could you explain why you prefer 'contains(belly, beer)' >> or 'belly.contains(beer)' over 'beer in belly'? The last form may be a bit >> harder to find in the documentation, but once a newbie has learned about >> it he'll find it easy to remember. andrew cooke wrote: > i don't know why i get involved in this type of discussion, but.... ==== I sense growing exasperation, and I do *not* have strong feelings on this matter, so I promise this will be my last message on this topic. I don't really prefer 'contains(belly, beer)' over 'beer in belly'. Rather, it's a completeness/consistency argument: a language that includes these methods for string objects: belly.startswith(arg) belly.endswith(arg) ... should also include: belly.contains(arg) The lack of this method increases the likelihood that a user will mistakenly use the find() method as a predicate. From debatem1 at gmail.com Thu Apr 9 15:22:03 2009 From: debatem1 at gmail.com (CTO) Date: Thu, 9 Apr 2009 12:22:03 -0700 (PDT) Subject: Recommendations on Pythonic tree data structure design techniques References: <1239293907.1795.1309794163@webmail.messagingengine.com> Message-ID: <5cdc6302-1cf2-4329-93f9-53bb7141185b@w9g2000yqa.googlegroups.com> I'm writing a Python graph library (called Graphine) that's pretty easy to use and does what you want. It is pre-alpha right now, but if you're interested please let me know- I'm very interested in hearing outside opinions. From andrew at acooke.org Thu Apr 9 15:40:44 2009 From: andrew at acooke.org (andrew cooke) Date: Thu, 9 Apr 2009 15:40:44 -0400 (CLT) Subject: Recommendations on Pythonic tree data structure design techniques In-Reply-To: <1239293907.1795.1309794163@webmail.messagingengine.com> References: <1239293907.1795.1309794163@webmail.messagingengine.com> Message-ID: python at bdurham.com wrote: > Any recommendations on Python based tree data structures that I > can study? I'm working on an application that will model a basic > outline structure (simple tree) and am looking for ideas on > Pythonic implementation techniques. By outline I mean a > traditional hierarchical document outline (section, chapter, > sub-chapter, ...). I will be building this structure as I parse > my customer's internally designed (proprietary) publishing markup > language. > My initial thought is to implement a generic node container class > with attributes for parent, next, previous, and child 'pointers'. That sounds reasonable, but do you need parent? It creates a reference loop, which make GC less likely to happen (or at least later), and it also complicates adding and removing nodes. Many algorithms don't need it. Even if you do need to ascend the tree you may be able to get by with a "path" that is a list of nodes from root to the current node and that indexes the node. > The node objects will be stored in a dictionary where node > 'pointers' correspond to incrementally assigned numeric keys. This doesn't make sense to me. It sounds like you are re-inventing lists (arrays). Andrew From cssaua at hotmail.com Thu Apr 9 15:42:39 2009 From: cssaua at hotmail.com (Lydia) Date: Thu, 9 Apr 2009 13:42:39 -0600 Subject: calculate field in ARCGIS References: <7e7a386f-d336-4186-822d-c6af0a581751@e38g2000vbe.googlegroups.com><87myapkeks.fsf@busola.homelinux.net> <49DE4AD0.1080809@snet.net> Message-ID: Hi Python users, I ran into a problem with python coding in ARCGIS. Does anybody have the experience in dealing with this? I need to calculate NEWFIELD based on OLDFIELD under condition: if OLDFIELD == 0 then return string "B" otherwise return "". codeblock = "def codefun(code): if code == 0: return \"B\" else: return \"\" " gp.CalculateField_management("INFILE", "OLDFIELD", "codefun(!NEWFIELD!", "PYTHON", codeblock) I got error: RuntimeError: exceptions.SyntaxError: invalid syntax (line 1) Failed to execute (CalculateField). Thanks for any kind of help! Lydia -------------- next part -------------- An HTML attachment was scrubbed... URL: From amon at vnl.com Thu Apr 9 15:48:11 2009 From: amon at vnl.com (Dale Amon) Date: Thu, 9 Apr 2009 20:48:11 +0100 Subject: regexp strangeness Message-ID: <20090409194811.GR17975@vnl.com> This finds nothing: import re import string card = "abcdef" DEC029 = re.compile("[^&0-9A-Z/ $*,.\-:#@'=\"[<(+\^!);\\\]%_>?]") errs = DEC029.findall(card.strip("\n\r")) print errs This works correctly: import re import string card = "abcdef" DEC029 = re.compile("[^&0-9A-Z/ $*,.\-:#@'=\"[<(+\^!)\\;\]%_>?]") errs = DEC029.findall(card.strip("\n\r")) print errs They differ only in the positioning of the quoted backslash. Just in case it is of interest to anyone. -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 196 bytes Desc: Digital signature URL: From clp2 at rebertia.com Thu Apr 9 15:55:17 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 9 Apr 2009 12:55:17 -0700 Subject: calculate field in ARCGIS In-Reply-To: References: <7e7a386f-d336-4186-822d-c6af0a581751@e38g2000vbe.googlegroups.com> <87myapkeks.fsf@busola.homelinux.net> <49DE4AD0.1080809@snet.net> Message-ID: <50697b2c0904091255w77674fb2x61d23f6ff7097f9f@mail.gmail.com> On Thu, Apr 9, 2009 at 12:42 PM, Lydia wrote: > Hi Python users, > > I ran into a problem with python coding in ARCGIS. Does anybody have the > experience in dealing with this? > > I need to calculate NEWFIELD based on?OLDFIELD under condition: if? OLDFIELD > == 0 then return string "B" otherwise return "". > > codeblock = "def codefun(code): if code == 0: return \"B\" else: return \"\" > " > > gp.CalculateField_management("INFILE", "OLDFIELD", "codefun(!NEWFIELD!", > "PYTHON", codeblock) > I got error: > > RuntimeError: > exceptions.SyntaxError: invalid syntax (line 1) > Failed to execute (CalculateField). Might I recommend you try using the multiline equivalent (assuming ArcGIS supports C-style escape sequences): codeblock = "def codefun(code):\n\tif code == 0:\n\t\treturn \"B\"\n\telse:\n\t\treturn \"\" " Cheers, Chris -- I have a blog: http://blog.rebertia.com From __peter__ at web.de Thu Apr 9 16:05:01 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 09 Apr 2009 22:05:01 +0200 Subject: regexp strangeness References: Message-ID: Dale Amon wrote: > This finds nothing: > > import re > import string > card = "abcdef" > DEC029 = re.compile("[^&0-9A-Z/ $*,.\-:#@'=\"[<(+\^!);\\\]%_>?]") > errs = DEC029.findall(card.strip("\n\r")) > print errs > > This works correctly: > > import re > import string > card = "abcdef" > DEC029 = re.compile("[^&0-9A-Z/ $*,.\-:#@'=\"[<(+\^!)\\;\]%_>?]") > errs = DEC029.findall(card.strip("\n\r")) > print errs > > They differ only in the positioning of the quoted backslash. > > Just in case it is of interest to anyone. You have to escape twice; once for Python and once for the regular expression. Or use raw strings, denoted by an r"..." prefix: >>> re.findall("[^&0-9A-Z/ $*,.\-:#@'=\"[<(+\^!);\\\]%_>?]", "abc") [] >>> re.findall("[^&0-9A-Z/ $*,.\-:#@'=\"[<(+\^!);\\\\\\]%_>?]", "abc") ['a', 'b', 'c'] >>> re.findall(r"[^&0-9A-Z/ $*,.\-:#@'=\"[<(+\^!);\\\]%_>?]", "abc") ['a', 'b', 'c'] Peter From cssaua at hotmail.com Thu Apr 9 16:40:25 2009 From: cssaua at hotmail.com (Lydia) Date: Thu, 9 Apr 2009 14:40:25 -0600 Subject: calculate field in ARCGIS References: <7e7a386f-d336-4186-822d-c6af0a581751@e38g2000vbe.googlegroups.com> <87myapkeks.fsf@busola.homelinux.net> <49DE4AD0.1080809@snet.net> <50697b2c0904091255w77674fb2x61d23f6ff7097f9f@mail.gmail.com> Message-ID: Thanks for the suggestion. But I guess under Python this doesn't work. I tried putting the code in different ways. But still not worked. codeblock = "def codefun(code): \\ if code == 0: \\ return \"B\" \\ else: return \"\" " ----- Original Message ----- From: "Chris Rebert" To: "Lydia" Cc: Sent: Thursday, April 09, 2009 1:55 PM Subject: Re: calculate field in ARCGIS On Thu, Apr 9, 2009 at 12:42 PM, Lydia wrote: > Hi Python users, > > I ran into a problem with python coding in ARCGIS. Does anybody have the > experience in dealing with this? > > I need to calculate NEWFIELD based on OLDFIELD under condition: if > OLDFIELD > == 0 then return string "B" otherwise return "". > > codeblock = "def codefun(code): if code == 0: return \"B\" else: return > \"\" > " > > gp.CalculateField_management("INFILE", "OLDFIELD", "codefun(!NEWFIELD!", > "PYTHON", codeblock) > I got error: > > RuntimeError: > exceptions.SyntaxError: invalid syntax (line 1) > Failed to execute (CalculateField). Might I recommend you try using the multiline equivalent (assuming ArcGIS supports C-style escape sequences): codeblock = "def codefun(code):\n\tif code == 0:\n\t\treturn \"B\"\n\telse:\n\t\treturn \"\" " Cheers, Chris -- I have a blog: http://blog.rebertia.com From joshua at joshuakugler.com Thu Apr 9 16:42:51 2009 From: joshua at joshuakugler.com (Joshua Kugler) Date: Thu, 09 Apr 2009 12:42:51 -0800 Subject: Why is it that *dbm modules don't provide an iterator? (Language design question) References: <90bb445a0904090822h63ee44e1g1f57ad303593f1cd@mail.gmail.com> Message-ID: Akira Kitada wrote: > The loop has to be: > """ >>>> k = d.firstkey() >>>> while k != None: > ... print k > ... k = d.nextkey(k) > key2 > key1 > """ Why not for key in d.keys(): print key That worked for me. j From lordgabriel14 at gmail.com Thu Apr 9 16:44:19 2009 From: lordgabriel14 at gmail.com (janus99) Date: Thu, 9 Apr 2009 13:44:19 -0700 (PDT) Subject: completly unrelated to python but i had to post it Message-ID: <87b9690f-94d9-4d82-9184-1f755bd7a5bc@f19g2000yqo.googlegroups.com> i know, i know , this group is about python, but this artilce i just read, well i was pretty much just surfing the web when i found this article made in 2001, it is just 2 funny. http://www.adequacy.org/stories/2001.12.2.42056.2147.html\ if u've never read it, it'll knock ur socks off, and the comments are riveting ps. personally i think a hacker wrote it cause no person can be that ignorant From mrstevegross at gmail.com Thu Apr 9 17:25:16 2009 From: mrstevegross at gmail.com (mrstevegross) Date: Thu, 9 Apr 2009 14:25:16 -0700 (PDT) Subject: How to import a module so that the current globals are available to the module? Message-ID: <957d759b-0c2a-4f20-90a8-d44322eb5e6b@f11g2000vbf.googlegroups.com> I'm trying to import a module so that the globals() of the importer module are available to the imported module itself. Consider the following scenario: === mymod.py === def go(): some_special_function(1,2) # 'some_special_function' is a built-in function available in the scope of foo.py (see below) === foo.py === some_special_function(3,4) # 'some_special_function' is a built-in function import mymod mymod.go() === EOF === The problem is this: in foo.py, you can call 'some_special_function' all you want, because it's builtin. You can even 'print globals()' to verify that it is available. However, when mymod.py tries to invoke 'some_special_function', it fails because the function is NOT available in that scope. So, the question is: how can I make builtin functions available in the test.py scope available to the mymod.py scope? One awkward solution is to deliberately initialize mymod.py ilke so: === mymod.py === globs=None def initialize(globs_): globs = globs_ def go(): globs['some_special_function'] (1,2) === foo.py === some_special_function(3,4) # 'some_special_function' is a built-in function import mymod mymod.initialize(globals()) mymod.go() === EOF === That will work, but it's a bit ugly. Plus, I have to repeat it for every module with the same problem (of which I have many!). Is there a way to use __import__ to make this work? Any ideas? Thanks, --Steve From davidj411 at gmail.com Thu Apr 9 17:25:32 2009 From: davidj411 at gmail.com (davidj411) Date: Thu, 9 Apr 2009 14:25:32 -0700 (PDT) Subject: cgi file limit size? References: Message-ID: <5c1bb277-00bd-4bcc-93f2-3110c29cef9a@e25g2000vbe.googlegroups.com> i am using these modules: import cgi,time import cgitb; cgitb.enable() iis webmapping now works with -U (key was to remove '-u' from the grouping of "s"'s: C:\Python25\python.exe -u "%s %s" here is the form html code:
Server name:
File name:
i increased the timeout on the IIS server to 2,200 seconds and i can now upload a file that is 220 MB in size in about 350 seconds. if i try to upload a 300 MB file, i get the dreaded CGI bad headers message, "CGI Error The specified CGI application misbehaved by not returning a complete set of HTTP headers." the headers are always the same: print "Content-type: text/html\n" print HTML_TEMPLATE in the IIS log, i do see http error 400 with sc-win32-status of 64 this link explains 400 errors a little - >http://www.checkupdown.com/ status/E400.html httperr log shows "BadRequest DefaultAppPool" this link explains this particular case a little: -> http://objectmix.com/inetserver/284871-re-sc-status-400-sc-win32-status-64-what-causes.html From akitada at gmail.com Thu Apr 9 17:39:03 2009 From: akitada at gmail.com (Akira Kitada) Date: Fri, 10 Apr 2009 06:39:03 +0900 Subject: Why is it that *dbm modules don't provide an iterator? (Language design question) In-Reply-To: References: <90bb445a0904090822h63ee44e1g1f57ad303593f1cd@mail.gmail.com> Message-ID: <90bb445a0904091439i6e590f6cr46f8d811dcc94610@mail.gmail.com> keys() returns a list and my question was not about "how to" but more like "why"... I assumed there were some decisions behind this, rather than it's just not implemented yet. Best, On Friday, April 10, 2009, Joshua Kugler wrote: > Akira Kitada wrote: > >> The loop has to be: >> """ >>>>> k = d.firstkey() >>>>> while k != None: >> ... ? ?print k >> ... ? ?k = d.nextkey(k) >> key2 >> key1 >> """ > > Why not > > for key in d.keys(): > ? ?print key > > That worked for me. > > j > > -- > http://mail.python.org/mailman/listinfo/python-list > From ebonak at hotmail.com Thu Apr 9 17:47:17 2009 From: ebonak at hotmail.com (Esmail) Date: Thu, 09 Apr 2009 17:47:17 -0400 Subject: is this possible (getting info off web page) Message-ID: Hi, I am trying to keep track of two flight bookings on the kayak.com web site, and would like to automate my query using Python. If I enter the url below myself into the browser, a form gets filled out and the site searches for flights. I would love to be able to have a simple python script which I can supply with a url and have it simply display, in text if possible, the best 3 prices. I thought great opportunity to learn more about Python, and then presumably re as I parse the text.... However, when I use the code below (located via google), I end up with an alternate page, basically telling me that kayak doesn't like bots. So, it seems like Kayak has it set up to prevent automated access? Is there anyway to do what I would like to do? Thanks, Esmail -- import urllib2 url = 'http://www.kayak.com/s/search/air?l1=cmh&d1=4/23/2009&l2=yyz&d2=4/26/2009' res = urllib2.urlopen(url) page = res.read() From tino at wildenhain.de Thu Apr 9 17:51:17 2009 From: tino at wildenhain.de (Tino Wildenhain) Date: Thu, 09 Apr 2009 23:51:17 +0200 Subject: Jython on Google AppEngine. In-Reply-To: <85d1d6ee-e8b8-48f5-87e0-41fb2fc03245@z1g2000yqn.googlegroups.com> References: <537ac6bf-dda0-4509-a0b6-6f67551f1015@r37g2000yqn.googlegroups.com> <85d1d6ee-e8b8-48f5-87e0-41fb2fc03245@z1g2000yqn.googlegroups.com> Message-ID: <49DE6DD5.4070200@wildenhain.de> Jason Scheirer wrote: > On Apr 9, 9:12 am, Alan Kennedy wrote: >> Hi all, >> >> You may be interested to know that you can now run jython 2.2 out of >> the box on Google AppEngine, thanks to their new java support. ... > > Finally! A way to run Python on App Engine! ? Is that some kind of weird humor? :-) T. From skip at pobox.com Thu Apr 9 17:53:42 2009 From: skip at pobox.com (skip at pobox.com) Date: Thu, 9 Apr 2009 16:53:42 -0500 Subject: Why is it that *dbm modules don't provide an iterator? (Language design question) In-Reply-To: References: <90bb445a0904090822h63ee44e1g1f57ad303593f1cd@mail.gmail.com> Message-ID: <18910.28262.131237.33940@montanaro.dyndns.org> Joshua> Why not Joshua> for key in d.keys(): Joshua> print key Joshua> That worked for me. Time & space. One motivation for using dbm files is to write large (huge, in fact) mappings to disk. Simply reconstituting the entire set of keys may consume a lot of time (they must all be enumerated before you begin working with any of them) and space (the number of keys in the file might be very large). -- Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/ "XML sucks, dictionaries rock" - Dave Beazley From google at mrabarnett.plus.com Thu Apr 9 17:54:40 2009 From: google at mrabarnett.plus.com (MRAB) Date: Thu, 09 Apr 2009 22:54:40 +0100 Subject: regexp strangeness In-Reply-To: References: Message-ID: <49DE6EA0.8070109@mrabarnett.plus.com> Peter Otten wrote: > Dale Amon wrote: > >> This finds nothing: >> >> import re >> import string >> card = "abcdef" >> DEC029 = re.compile("[^&0-9A-Z/ $*,.\-:#@'=\"[<(+\^!);\\\]%_>?]") The regular expression you're actually providing is: >>> print "[^&0-9A-Z/ $*,.\-:#@'=\"[<(+\^!);\\\]%_>?]" [^&0-9A-Z/ $*,.\-:#@'="[<(+\^!);\\]%_>?] ^^^ The backslash is escaped (the "\\") and the set ends at the first "]". >> errs = DEC029.findall(card.strip("\n\r")) >> print errs >> >> This works correctly: >> >> import re >> import string >> card = "abcdef" >> DEC029 = re.compile("[^&0-9A-Z/ $*,.\-:#@'=\"[<(+\^!)\\;\]%_>?]") The regular expression you're actually providing is: >>> print "[^&0-9A-Z/ $*,.\-:#@'=\"[<(+\^!)\\;\]%_>?]" [^&0-9A-Z/ $*,.\-:#@'="[<(+\^!)\;\]%_>?] ^^ ^ The first "]" is escaped (the "\]") and the set ends at the second "]". >> errs = DEC029.findall(card.strip("\n\r")) >> print errs >> >> They differ only in the positioning of the quoted backslash. >> >> Just in case it is of interest to anyone. > > You have to escape twice; once for Python and once for the regular > expression. Or use raw strings, denoted by an r"..." prefix: > >>>> re.findall("[^&0-9A-Z/ $*,.\-:#@'=\"[<(+\^!);\\\]%_>?]", "abc") > [] >>>> re.findall("[^&0-9A-Z/ $*,.\-:#@'=\"[<(+\^!);\\\\\\]%_>?]", "abc") > ['a', 'b', 'c'] >>>> re.findall(r"[^&0-9A-Z/ $*,.\-:#@'=\"[<(+\^!);\\\]%_>?]", "abc") > ['a', 'b', 'c'] > From bedouglas at earthlink.net Thu Apr 9 17:57:09 2009 From: bedouglas at earthlink.net (bruce) Date: Thu, 9 Apr 2009 14:57:09 -0700 Subject: is this possible (getting info off web page) In-Reply-To: Message-ID: <794a01c9b95e$22e16130$0301a8c0@tmesa.com> Hi Esmail. I've not looked at the site. however, i can give you some general pointers that might help you in solving your issue. first off, try to accomplish your goal, using curl, or one of the other cmdline apps that fetch page data. this allows you to quickly nail down any issues that might occur, and to see if it's even possible to fetch the page. but even before you attempt curl, you might need to analyze the page/process of fetching the page. if you try a simple fetch using curl, and you don't get your page, then it might be a matter of using cookies and/or setting the user-agent, or any number of things. page/traffic analysis will allow you to see what traffic is happening between the server and your browser. if you're using firefox, you can get a plugin (livehttpheaders) which will allow you to see what's going on. this will tell you if coookies are being used/set, if you're using a straight get/post process, whether you're doing redirects, etc... once you have a better understanding of the process required, you can then setup curl to fetch the page. if you get the page with cur, you can then proceed to using python/urllib2/mech/cookies/etc.. to then fetch the page.. and once you get the page, you can then play with XPath, to manipulate/drive/parse the DOM to get the information you want... Have fun!!! -----Original Message----- From: python-list-bounces+bedouglas=earthlink.net at python.org [mailto:python-list-bounces+bedouglas=earthlink.net at python.org]On Behalf Of Esmail Sent: Thursday, April 09, 2009 2:47 PM To: python-list at python.org Subject: is this possible (getting info off web page) Hi, I am trying to keep track of two flight bookings on the kayak.com web site, and would like to automate my query using Python. If I enter the url below myself into the browser, a form gets filled out and the site searches for flights. I would love to be able to have a simple python script which I can supply with a url and have it simply display, in text if possible, the best 3 prices. I thought great opportunity to learn more about Python, and then presumably re as I parse the text.... However, when I use the code below (located via google), I end up with an alternate page, basically telling me that kayak doesn't like bots. So, it seems like Kayak has it set up to prevent automated access? Is there anyway to do what I would like to do? Thanks, Esmail -- import urllib2 url = 'http://www.kayak.com/s/search/air?l1=cmh&d1=4/23/2009&l2=yyz&d2=4/26/2009' res = urllib2.urlopen(url) page = res.read() -- http://mail.python.org/mailman/listinfo/python-list From google at mrabarnett.plus.com Thu Apr 9 17:58:05 2009 From: google at mrabarnett.plus.com (MRAB) Date: Thu, 09 Apr 2009 22:58:05 +0100 Subject: calculate field in ARCGIS In-Reply-To: References: <7e7a386f-d336-4186-822d-c6af0a581751@e38g2000vbe.googlegroups.com> <87myapkeks.fsf@busola.homelinux.net> <49DE4AD0.1080809@snet.net> <50697b2c0904091255w77674fb2x61d23f6ff7097f9f@mail.gmail.com> Message-ID: <49DE6F6D.1010807@mrabarnett.plus.com> Lydia wrote: > Thanks for the suggestion. > > But I guess under Python this doesn't work. I tried putting the code in > different ways. But still not worked. > > codeblock = "def codefun(code): \\ > if code == 0: \\ > return \"B\" \\ > else: return \"\" " > > You might also want to try a triple-quoted string, which makes it clearer: codeblock = """def codefun(code): if code == 0: return "B" else: return "" """ > ----- Original Message ----- From: "Chris Rebert" > To: "Lydia" > Cc: > Sent: Thursday, April 09, 2009 1:55 PM > Subject: Re: calculate field in ARCGIS > > > On Thu, Apr 9, 2009 at 12:42 PM, Lydia wrote: >> Hi Python users, >> >> I ran into a problem with python coding in ARCGIS. Does anybody have the >> experience in dealing with this? >> >> I need to calculate NEWFIELD based on OLDFIELD under condition: if >> OLDFIELD >> == 0 then return string "B" otherwise return "". >> >> codeblock = "def codefun(code): if code == 0: return \"B\" else: >> return \"\" >> " >> >> gp.CalculateField_management("INFILE", "OLDFIELD", "codefun(!NEWFIELD!", >> "PYTHON", codeblock) >> I got error: >> >> RuntimeError: >> exceptions.SyntaxError: invalid syntax (line 1) >> Failed to execute (CalculateField). > > Might I recommend you try using the multiline equivalent (assuming > ArcGIS supports C-style escape sequences): > > codeblock = "def codefun(code):\n\tif code == 0:\n\t\treturn > \"B\"\n\telse:\n\t\treturn \"\" " > > Cheers, > Chris > From kpdere at verizon.net Thu Apr 9 18:06:56 2009 From: kpdere at verizon.net (Ken Dere) Date: Thu, 09 Apr 2009 18:06:56 -0400 Subject: Best way to start References: <0e991af0-52f4-4849-9587-914bf38b3505@j39g2000yqn.googlegroups.com> Message-ID: <3c562$49de7180$60ffb8d4$26727@news.flashnewsgroups.com> Avi wrote: > Hi, > > What is a good way to learn Python? > > Do you recommend going by a book (suggestions welcome) or learning > with tutorials? Both? > > Thanks in advance, > Avi A good book is Learning Python by Lutz (O'Reilly) Then, you need to do some actual programming. The best way is to figure out some program that you would like to write. Ken D From ben+python at benfinney.id.au Thu Apr 9 18:17:21 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 10 Apr 2009 08:17:21 +1000 Subject: completly unrelated to python but i had to post it References: <87b9690f-94d9-4d82-9184-1f755bd7a5bc@f19g2000yqo.googlegroups.com> Message-ID: <878wm979qm.fsf@benfinney.id.au> janus99 writes: > if u've never read it, it'll knock ur socks off, and the comments > are riveting Please save it for another forum in future. -- \ ?It is wrong to think that the task of physics is to find out | `\ how nature *is*. Physics concerns what we can *say* about | _o__) nature?? ?Niels Bohr | Ben Finney From Scott.Daniels at Acm.Org Thu Apr 9 18:17:55 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Thu, 09 Apr 2009 15:17:55 -0700 Subject: How to import a module so that the current globals are available to the module? In-Reply-To: <957d759b-0c2a-4f20-90a8-d44322eb5e6b@f11g2000vbf.googlegroups.com> References: <957d759b-0c2a-4f20-90a8-d44322eb5e6b@f11g2000vbf.googlegroups.com> Message-ID: mrstevegross wrote: > I'm trying to import a module so that the globals() of the importer > module are available to the imported module itself.... > One awkward solution is to deliberately initialize mymod.py .... > That will work, but it's a bit ugly. Plus, I have to repeat it for > every module with the same problem (of which I have many!). Nope. Think about it. Only the first import is anything but a dict lookup. Even if you know how you want that to work, suppose you use two different modules in a single program, both of which define a "builtin" named "somename". Does the first one win? Does the second? Does the last that actually imported? There is a hack that can let you change the builtins module, but you don't want to use it: you make your programs harder to understand. Accept that you may want a bit of boilerplate if you have a common base, and start each use with something like: from base_functions import bibble, babble, bobble, bome --Scott David Daniels Scott.Daniels at Acm.Org From __peter__ at web.de Thu Apr 9 18:20:04 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 10 Apr 2009 00:20:04 +0200 Subject: How to import a module so that the current globals are available to the module? References: <957d759b-0c2a-4f20-90a8-d44322eb5e6b@f11g2000vbf.googlegroups.com> Message-ID: mrstevegross wrote: > I'm trying to import a module so that the globals() of the importer > module are available to the imported module itself. Consider the > following scenario: > > === mymod.py === > def go(): > some_special_function(1,2) > # 'some_special_function' is a built-in function available in the > scope of foo.py (see below) > > === foo.py === > some_special_function(3,4) # 'some_special_function' is a built-in > function > import mymod > mymod.go() > === EOF === > > The problem is this: in foo.py, you can call 'some_special_function' > all you want, because it's builtin. You can even 'print globals()' to > verify that it is available. > > However, when mymod.py tries to invoke 'some_special_function', it > fails because the function is NOT available in that scope. > > So, the question is: how can I make builtin functions available in the > test.py scope available to the mymod.py scope? > > One awkward solution is to deliberately initialize mymod.py ilke so: > > === mymod.py === > globs=None > def initialize(globs_): globs = globs_ > def go(): > globs['some_special_function'] (1,2) > > === foo.py === > some_special_function(3,4) # 'some_special_function' is a built-in > function > import mymod > mymod.initialize(globals()) > mymod.go() > === EOF === > > That will work, but it's a bit ugly. Plus, I have to repeat it for > every module with the same problem (of which I have many!). Have you considered what happens in an application that uses two of these modules? Whatever is imported last wins with its idea of "some special function". > Is there a way to use __import__ to make this work? Any ideas? Please, no. Make it explicit even if it's a bit more verbose: --- mymod.py --- def go(special_func): special_func(1, 2) --- foo.py --- import mymod def some_special_func(a, b): print "%s * %s = %s" % (a, b, a*b) mymod.go(some_special_func) You can also create a shortcut for the latter: from functools import partial foogo = partial(mymod.go, some_special_func) foogo() Finally, there is an alternative that is really unclean: --- mymod.py --- def go(): sys._getframe(1).f_globals["some_special_funct"](1, 2) Peter From jason.scheirer at gmail.com Thu Apr 9 18:37:29 2009 From: jason.scheirer at gmail.com (Jason Scheirer) Date: Thu, 9 Apr 2009 15:37:29 -0700 (PDT) Subject: calculate field in ARCGIS References: <7e7a386f-d336-4186-822d-c6af0a581751@e38g2000vbe.googlegroups.com> <87myapkeks.fsf@busola.homelinux.net> <49DE4AD0.1080809@snet.net> Message-ID: On Apr 9, 12:55?pm, Chris Rebert wrote: > On Thu, Apr 9, 2009 at 12:42 PM, Lydia wrote: > > Hi Python users, > > > I ran into a problem with python coding in ARCGIS. Does anybody have the > > experience in dealing with this? > > > I need to calculate NEWFIELD based on?OLDFIELD under condition: if? OLDFIELD > > == 0 then return string "B" otherwise return "". > > > codeblock = "def codefun(code): if code == 0: return \"B\" else: return \"\" > > " > > > gp.CalculateField_management("INFILE", "OLDFIELD", "codefun(!NEWFIELD!", > > "PYTHON", codeblock) > > I got error: > > > RuntimeError: > > exceptions.SyntaxError: invalid syntax (line 1) > > Failed to execute (CalculateField). > > Might I recommend you try using the multiline equivalent (assuming > ArcGIS supports C-style escape sequences): > > codeblock = "def codefun(code):\n\tif code == 0:\n\t\treturn > \"B\"\n\telse:\n\t\treturn \"\" " > > Cheers, > Chris > > -- > I have a blog:http://blog.rebertia.com Looks like an error in your code: gp.CalculateField_management("INFILE", "OLDFIELD", "codefun(! NEWFIELD!", "PYTHON", codeblock) Should be: gp.CalculateField_management("INFILE", "OLDFIELD", "codefun(! NEWFIELD!)", "PYTHON", codeblock) Or you could fold the function into the expression using the ternary and get rid of your code block param: gp.CalculateField_management("INFILE", "OLDFIELD", "'B' if !OLDFIELD! == 0 else ''", "PYTHON") From wescpy at gmail.com Thu Apr 9 18:45:03 2009 From: wescpy at gmail.com (wesley chun) Date: Thu, 9 Apr 2009 15:45:03 -0700 Subject: [ANN] Introduction to Python course, San Francisco, Jun 2009 Message-ID: <78b3a9580904091545w1550330an3e6f06f91f0a4509@mail.gmail.com> Need to get up-to-speed with Python as quickly as possible? Come join me, Wesley Chun, author of Prentice-Hall's bestseller "Core Python Programming," for a comprehensive intro course coming up this June in beautiful Northern California! Please pass on this note to whomever you think may be interested. I look forward to meeting you and your colleagues! (Comprehensive) Introduction to Python Mon-Wed, 2009 Jun 15-17, 9am-5pm - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (COMPREHENSIVE) INTRODUCTION TO PYTHON Although this course may appear to those new to Python, it is also perfect for those who have tinkered with it and want to "fill in the gaps" and/or want to get more in-depth formal training. It combines the best of both an introduction to the language as well as a "Python Internals" training course. We will immerse you in the world of Python in only a few days, showing you more than just its syntax (which you don't really need a book to learn, right?). Knowing more about how Python works under the covers, including the relationship between data objects and memory management, will make you a much more effective Python programmer coming out of the gate. 3 hands-on labs each day will help hammer the concepts home. Come find out why Google, Yahoo!, Disney, ILM/LucasFilm, VMware, NASA, Ubuntu, YouTube, Slide, and Red Hat all use Python. Users supporting or jumping to Plone, Zope, TurboGears, Pylons, Django, Google App Engine, Jython, IronPython, and Mailman will also benefit! FREE PREVIEW 1: you will find (and can download) a video clip of a live lesson that was delivered recently to get an idea of the lecture style and interactive classroom environment at: http://cyberwebconsulting.com (click "Python Training") FREE PREVIEW 2: Partnering with O'Reilly and Pearson, Safari Books Online has asked me to deliver a 1-hour webcast on Wednesday morning 2009 Apr 29 @ 10:30a PDT/1:30p EDT called What is Python?. This will be an online seminar based on a session that I've delivered at numerous conferences in the past. It will give you an idea of lecture style as well as an overview of the material covered in the course. Plus the first 10 registrants will receive an autographed copy of Core Python Programming! For more information: http://www.safaribooksonline.com/events/WhatIsPython.html - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - WHERE: near the San Francisco Airport (SFO/San Bruno), CA, USA WEB: http://cyberwebconsulting.com (click "Python Training") FLYER: http://starship.python.net/crew/wesc/flyerPP1jun09.pdf LOCALS: easy freeway (101/280/380) with lots of parking plus public transit (BART and CalTrain) access via the San Bruno stations, easily accessible from all parts of the Bay Area VISITORS: free shuttle to/from the airport, free high-speed internet, free breakfast and regular evening receptions; fully-equipped suites See website for costs, venue info, and registration. Discounts are available for multiple registrations as well as for teachers/students. Hope to see you there! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 http://corepython.com "Python Web Development with Django", Addison Wesley, (c)2009 http://withdjango.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com From martin at v.loewis.de Thu Apr 9 18:46:07 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Fri, 10 Apr 2009 00:46:07 +0200 Subject: Get the ipv6 address from a interface In-Reply-To: References: <86176ef7-c2e0-4c5d-b883-d91672e3eb0b@w40g2000yqd.googlegroups.com> Message-ID: <49de7ab0$0$3669$9b622d9e@news.freenet.de> > I'm not 100% sure what you're trying to do, but the above is horribly > non-portable. You probably want to be looking at socket.getpeername() and > socket.getsockname(). This only works if you are actually connected. I think he wants to find out the local address without actually connecting. > In general, concepts like "the address of an interface" are difficult. In > many OS's, a given interface may have multiple addresses. This is > especially true in IPv6 where you've have both link local and global > unicast addresses on the same interface. In Linux, you can only have one IPv4 address per interface (and you have to use alias interfaces, such as eth0:0, to assign multiple addresses to a physical link). For IPv6 and Linux, you are right. > > Can you back up a few steps and describe what it is that you're trying to > do, i.e. the use case? I guess he wants to do the equivalent of ifconfig. Regards, Martin From martin at v.loewis.de Thu Apr 9 18:47:39 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Fri, 10 Apr 2009 00:47:39 +0200 Subject: Get the ipv6 address from a interface In-Reply-To: <86176ef7-c2e0-4c5d-b883-d91672e3eb0b@w40g2000yqd.googlegroups.com> References: <86176ef7-c2e0-4c5d-b883-d91672e3eb0b@w40g2000yqd.googlegroups.com> Message-ID: <49DE7B0B.5000803@v.loewis.de> > which works great. But i am not enough into python to port that to > ipv6. It has to work under linux only. Any help is appreciated. Not sure how universal this is, but I would read /proc/net/if_inet6. At least, that's what ifconfig does, and it seems to work fine. martin at mira:~$ cat /proc/net/if_inet6 fe80000000000000000000004d804137 1c 40 20 80 sixxs fe80000000000000020d61fffe543e15 02 40 20 80 sis 00000000000000000000000000000001 01 80 10 80 lo 200106f809000a850000000000000002 1c 40 00 80 sixxs Regards, Martin From martin at v.loewis.de Thu Apr 9 18:52:11 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Fri, 10 Apr 2009 00:52:11 +0200 Subject: Why is it that *dbm modules don't provide an iterator? (Language design question) In-Reply-To: References: <90bb445a0904090822h63ee44e1g1f57ad303593f1cd@mail.gmail.com> Message-ID: <49de7c1b$0$3669$9b622d9e@news.freenet.de> > I assumed there were some decisions behind this, rather than it's just > not implemented yet. I believe this assumption is wrong - it's really that no code has been contributed to do that. For gdbm, you can also use the firstkey/nextkey methods. Regards, Martin From pavlovevidence at gmail.com Thu Apr 9 18:57:05 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 9 Apr 2009 15:57:05 -0700 (PDT) Subject: How to import a module so that the current globals are available to the module? References: <957d759b-0c2a-4f20-90a8-d44322eb5e6b@f11g2000vbf.googlegroups.com> Message-ID: <95d0235e-bf31-4b9c-8a60-6edb908e7363@l22g2000vba.googlegroups.com> On Apr 9, 2:25?pm, mrstevegross wrote: > I'm trying to import a module so that the globals() of the importer > module are available to the imported module itself. [snip] In general, the way I recommend to deal with this issue (aside from reorganizing your code) is to pass the function you want to call in as an argument. Rewrite your go function like so: def go(function_to_call): # do stuff function_to_call(1,2) # do rest of stuff Then, from the other modules, call it like this: mymod.go(some_special_func) What you are looking to do isn't THAT unreasonable, and there are some libraries that are based strongly on callbacks (which is the term for what you're trying to do). However, few if any libraries try to determine the callback automatically; you have to explicity pass the function/object to call back to. Carl Banks From sjmachin at lexicon.net Thu Apr 9 19:00:22 2009 From: sjmachin at lexicon.net (John Machin) Date: Thu, 9 Apr 2009 16:00:22 -0700 (PDT) Subject: Why does Python show the whole array? References: <7e7a386f-d336-4186-822d-c6af0a581751@e38g2000vbe.googlegroups.com> <87myapkeks.fsf@busola.homelinux.net> Message-ID: On Apr 10, 2:36?am, John Posner wrote: > Hrvoje Niksic wrote: > > ?> ?if test.contains(item) ? ? # would return a Boolean value > ?> > > ?>> That's a string method, not a function in the string module. > > Oops, of course. > > ?>>>> import operator > ?>>>> operator.contains('foo', 'o') > > That's pretty good, and IMHO a bit better than John Machin's suggestion > to use the __contains__() method. (I have this prejudice that using the > __XXX__ methods in "everyday code" is cheating.) I "suggested" no such thing. You asked whether anyone had discussed such a thing. A reply pointing out that such a thing exists already is in no way inciting people to use it. > > Given how common string maniuplations are, I guess I'm surprised that > Python hasn't yet made "contains()" into both a "string"-module function > *and* a string-object method. Perhaps because there's already a good way to do it: stringa in stringb From andrew at acooke.org Thu Apr 9 19:19:33 2009 From: andrew at acooke.org (andrew cooke) Date: Thu, 9 Apr 2009 19:19:33 -0400 (CLT) Subject: Why is it that *dbm modules don't provide an iterator? (Language design question) In-Reply-To: <49de7c1b$0$3669$9b622d9e@news.freenet.de> References: <90bb445a0904090822h63ee44e1g1f57ad303593f1cd@mail.gmail.com> <49de7c1b$0$3669$9b622d9e@news.freenet.de> Message-ID: <19054ec0f8a16993e1b265112d82b5b1.squirrel@localhost> "Martin v. L?wis" wrote: >> I assumed there were some decisions behind this, rather than it's just >> not implemented yet. > > I believe this assumption is wrong - it's really that no code has been > contributed to do that. But doesn't the issue at http://bugs.python.org/issue662923 imply that there *was* suitable code (for 2.4)? So is this a regression? And should that issue be re-opened? Andrew > For gdbm, you can also use the firstkey/nextkey methods. > > Regards, > Martin > -- > http://mail.python.org/mailman/listinfo/python-list > > From dheianevans at gmail.com Thu Apr 9 19:26:45 2009 From: dheianevans at gmail.com (dhenews) Date: Thu, 9 Apr 2009 16:26:45 -0700 (PDT) Subject: Open source web crawler with mysql integration Message-ID: <9ed1c9c7-267e-448e-ba83-98db949d97ec@o11g2000yql.googlegroups.com> I'm looking for a crawler that can spider my site and toss the results into mysql so, in turn, that database can be indexed by Sphinx Search. Since I don't want to reinvent the wheel, is anyone aware of any open source projects or code snippets that can already handle this? Thanks for any advice. From fetchinson at googlemail.com Thu Apr 9 19:37:36 2009 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Thu, 9 Apr 2009 16:37:36 -0700 Subject: Open source web crawler with mysql integration In-Reply-To: <9ed1c9c7-267e-448e-ba83-98db949d97ec@o11g2000yql.googlegroups.com> References: <9ed1c9c7-267e-448e-ba83-98db949d97ec@o11g2000yql.googlegroups.com> Message-ID: > I'm looking for a crawler that can spider my site and toss the results > into mysql so, in turn, that database can be indexed by Sphinx Search. > > Since I don't want to reinvent the wheel, is anyone aware of any open > source projects or code snippets that can already handle this? Have a look at http://nikitathespider.com/python/ HTH, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From martin at v.loewis.de Thu Apr 9 19:39:31 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Fri, 10 Apr 2009 01:39:31 +0200 Subject: Why is it that *dbm modules don't provide an iterator? (Language design question) In-Reply-To: <19054ec0f8a16993e1b265112d82b5b1.squirrel@localhost> References: <90bb445a0904090822h63ee44e1g1f57ad303593f1cd@mail.gmail.com> <49de7c1b$0$3669$9b622d9e@news.freenet.de> <19054ec0f8a16993e1b265112d82b5b1.squirrel@localhost> Message-ID: <49DE8733.8060200@v.loewis.de> >>> I assumed there were some decisions behind this, rather than it's just >>> not implemented yet. >> I believe this assumption is wrong - it's really that no code has been >> contributed to do that. > > But doesn't the issue at http://bugs.python.org/issue662923 imply that > there *was* suitable code (for 2.4)? Yes, for bsddb. > So is this a regression? No. bsddb still supports iteration in 2.6. > And should that issue be re-opened? I don't think that would matter much. Actually contributing code might. Regards, Martin From adityashukla1983 at gmail.com Thu Apr 9 20:03:25 2009 From: adityashukla1983 at gmail.com (aditya shukla) Date: Thu, 9 Apr 2009 19:03:25 -0500 Subject: Temporary file Message-ID: <73045cca0904091703j5b14638bve57bff3646549ec0@mail.gmail.com> I am trying to create a temporary file. import tempfile temp = tempfile.NamedTemporaryFile(suffix='_suffix', prefix='prefix_', dir='/tmp', ) try: print 'temp:', temp print 'temp.name:', temp.name finally: temp.close() But when i do ls /tmp or ls -a /tmp i cannot find the file or if i try to os.remove(temp.name) it shows a message that the file is not found.But print temp and print temp.name show temp: ', mode 'w+b' at 0xb7e84800> temp.name: /home/swamp2/cacsgrad/axs9347/Desktop/prefix_zOfoX__suffix Please help me to fix it. Aditya -------------- next part -------------- An HTML attachment was scrubbed... URL: From dheianevans at gmail.com Thu Apr 9 20:11:18 2009 From: dheianevans at gmail.com (dhenews) Date: Thu, 9 Apr 2009 17:11:18 -0700 (PDT) Subject: Open source web crawler with mysql integration References: <9ed1c9c7-267e-448e-ba83-98db949d97ec@o11g2000yql.googlegroups.com> Message-ID: <91fa60ec-ff14-48b0-a896-5303646ec8b0@r37g2000yqn.googlegroups.com> On Apr 9, 7:37?pm, Daniel Fetchinson wrote: > Have a look athttp://nikitathespider.com/python/ Thanks, I'll have a look. From google at mrabarnett.plus.com Thu Apr 9 20:29:33 2009 From: google at mrabarnett.plus.com (MRAB) Date: Fri, 10 Apr 2009 01:29:33 +0100 Subject: Temporary file In-Reply-To: <73045cca0904091703j5b14638bve57bff3646549ec0@mail.gmail.com> References: <73045cca0904091703j5b14638bve57bff3646549ec0@mail.gmail.com> Message-ID: <49DE92ED.5090305@mrabarnett.plus.com> aditya shukla wrote: > I am trying to create a temporary file. > > import tempfile > > temp = tempfile.NamedTemporaryFile(suffix='_suffix', > > prefix='prefix_', > dir='/tmp', > > ) > try: > print 'temp:', temp > > print 'temp.name :', temp.name > finally: > > temp.close() > > > But when i do ls /tmp or ls -a /tmp i cannot find the file or if i try to os.remove(temp.name ) it shows a message that the file is not found.But print temp and print temp.name show > > temp: ', mode 'w+b' at 0xb7e84800> > temp.name : /home/swamp2/cacsgrad/axs9347/Desktop/prefix_zOfoX__suffix > > > > Please help me to fix it. > > NamedTemporaryFile() can have an extra argument 'delete', which, if True (the default), deletes the file after it's closed. Try: temp = tempfile.NamedTemporaryFile(suffix='_suffix', prefix='prefix_', dir='/tmp', delete=False) instead. It's in the documentation: http://docs.python.org/library/tempfile.html From jdoe at usenetlove.invalid Thu Apr 9 20:50:29 2009 From: jdoe at usenetlove.invalid (John Doe) Date: Fri, 10 Apr 2009 00:50:29 GMT Subject: Cannot find text in *.py files with Windows Explorer? References: Message-ID: Tim Golden wrote: > John Doe wrote: >> Anybody have a solution for Windows (XP) Explorer search not >> finding ordinary text in *.py files? > > It came up a couple of months ago either on this > list or on python-win32. Don't have web access > at the moment, but try searching the archives for > references to search, registry settings and probably > an msdn link. I think it goes something like this... Right-click My Computer -- manage -- services and applications -- right-click indexing service -- properties -- generation -- check Index files with unknown extensions Note: turning on indexing is not necessary. Thanks to the replies. -- Interested in making Windows and games obey your verbal commands? Continuous command recognition (much easier than speech recognition) can now be enabled using Naturally Speaking and freeware Dragonfly. See (comp.lang.beta) for discussion. From ldo at geek-central.gen.new_zealand Thu Apr 9 21:31:28 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Fri, 10 Apr 2009 13:31:28 +1200 Subject: Why does Python show the whole array? References: Message-ID: In message , Peter Otten wrote: > Lawrence D'Oliveiro wrote: > >> In message , Gilles Ganault >> wrote: >> >>> test = "toto at gmail.com" >>> isp = ["gmail.com", "yahoo.com"] >>> for item in isp: >>> if test.find(item): >>> print item >>> ======= output >>> gmail.com >>> yahoo.com >>> ======= >> >> This is why conditional constructs should not accept any values other >> than True and False. > > So you think > > if test.find(item) == True: ... > > would have been better? That won't work either. Can anyone tell us what he's done wrong? From cdalten at gmail.com Thu Apr 9 21:36:55 2009 From: cdalten at gmail.com (grocery_stocker) Date: Thu, 9 Apr 2009 18:36:55 -0700 (PDT) Subject: How come I get get my background thread to output anything? Message-ID: Given the following.... #!/usr/local/bin/python import os import time import thread def domsg(string, sleeptime, *args): print "%s was here", string #os.system('tel %s test' % person); #time.sleep(sleeptime) def buildlist(): out = 1 persons = [] while(out != 0): pern = raw_input("Enter person to message:") if (len(pern)): persons.append(pern) else: out = 0 return persons if __name__ == "__main__": #buildlist() thread.start_new_thread(domsg, ("person",2)) I get.... m-net% ./massmsg.py m-net% I was expecting to see person was here From cssaua at hotmail.com Thu Apr 9 21:46:29 2009 From: cssaua at hotmail.com (Lydia) Date: Thu, 9 Apr 2009 18:46:29 -0700 Subject: [?? Probable Spam] Re: calculate field in ARCGIS References: , <7e7a386f-d336-4186-822d-c6af0a581751@e38g2000vbe.googlegroups.com>, , , <87myapkeks.fsf@busola.homelinux.net>, , , , <49DE4AD0.1080809@snet.net>, , <50697b2c0904091255w77674fb2x61d23f6ff7097f9f@mail.gmail.com>, <49DE6F6D.1010807@mrabarnett.plus.com> Message-ID: Thanks guys! You made it work. >On Apr 9, 12:55?pm, Chris Rebert wrote: > On Thu, Apr 9, 2009 at 12:42 PM, Lydia wrote: > > Hi Python users, > > > I ran into a problem with python coding in ARCGIS. Does anybody have the > > experience in dealing with this? > > > I need to calculate NEWFIELD based on?OLDFIELD under condition: if?OLDFIELD > > == 0 then return string "B" otherwise return "". > > > codeblock = "def codefun(code): if code == 0: return \"B\" else: return \"\" > > " > > > gp.CalculateField_management("INFILE", "OLDFIELD", "codefun(!NEWFIELD!", > > "PYTHON", codeblock) > > I got error: > > > RuntimeError: > > exceptions.SyntaxError: invalid syntax (line 1) > > Failed to execute (CalculateField). > > Might I recommend you try using the multiline equivalent (assuming > ArcGIS supports C-style escape sequences): > > codeblock = "def codefun(code):\n\tif code == 0:\n\t\treturn > \"B\"\n\telse:\n\t\treturn \"\" " > > Cheers, > Chris > > -- > I have a blog:http://blog.rebertia.com Looks like an error in your code: gp.CalculateField_management("INFILE", "OLDFIELD", "codefun(! NEWFIELD!", "PYTHON", codeblock) Should be: gp.CalculateField_management("INFILE", "OLDFIELD", "codefun(! NEWFIELD!)", "PYTHON", codeblock) Or you could fold the function into the expression using the ternary and get rid of your code block param: gp.CalculateField_management("INFILE", "OLDFIELD", "'B' if !OLDFIELD! == 0 else ''", "PYTHON") -- http://mail.python.org/mailman/listinfo/python-list From soumen08 at gmail.com Thu Apr 9 21:55:51 2009 From: soumen08 at gmail.com (Soumen banerjee) Date: Fri, 10 Apr 2009 07:25:51 +0530 Subject: a simple keypress sensor Message-ID: Hello, I am writing a little program where i have an array the elements of which i want to display upon keypresses by the user. The only way on linux seems like using getch() from the curses library. I wanted a simpler solution than having to use curses for such a simple thing. any ideas? Regards Soumen From cdalten at gmail.com Thu Apr 9 22:01:31 2009 From: cdalten at gmail.com (grocery_stocker) Date: Thu, 9 Apr 2009 19:01:31 -0700 (PDT) Subject: How come I can't get get my background thread to output anything? References: Message-ID: On Apr 9, 6:36 pm, grocery_stocker wrote: > Given the following.... > > #!/usr/local/bin/python > > import os > import time > import thread > > def domsg(string, sleeptime, *args): > print "%s was here", string > #os.system('tel %s test' % person); > #time.sleep(sleeptime) > > def buildlist(): > out = 1 > persons = [] > > while(out != 0): > pern = raw_input("Enter person to message:") > if (len(pern)): > persons.append(pern) > else: > out = 0 > return persons > > if __name__ == "__main__": > #buildlist() > thread.start_new_thread(domsg, ("person",2)) > > I get.... > m-net% ./massmsg.py > m-net% > > I was expecting to see > > person was here From aahz at pythoncraft.com Thu Apr 9 22:17:34 2009 From: aahz at pythoncraft.com (Aahz) Date: 9 Apr 2009 19:17:34 -0700 Subject: Python C API String Memory Consumption References: <41d95468-8f7a-4647-83e1-6df147744467@u8g2000yqn.googlegroups.com> Message-ID: In article <41d95468-8f7a-4647-83e1-6df147744467 at u8g2000yqn.googlegroups.com>, k3xji wrote: > >When I run the following function, I seem to have a mem leak, a 20 mb >of memory >is allocated and is not freed. Here is the code I run: > >>>> import esauth >>>> for i in range(1000000): > >... ss = esauth.penc('sumer') >... >>>> for i in range(1000000): > >... ss = esauth.penc('sumer') >... BTW, note that if you're using Python 2.x, range(1000000) will cause a "leak" because ints are never freed. Instead, use xrange(). -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ Why is this newsgroup different from all other newsgroups? From philip at semanchuk.com Thu Apr 9 22:45:34 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Thu, 9 Apr 2009 22:45:34 -0400 Subject: Open source web crawler with mysql integration In-Reply-To: References: <9ed1c9c7-267e-448e-ba83-98db949d97ec@o11g2000yql.googlegroups.com> Message-ID: <3BC408BC-2006-41E6-AB4E-698DA30CE56A@semanchuk.com> On Apr 9, 2009, at 7:37 PM, Daniel Fetchinson wrote: >> I'm looking for a crawler that can spider my site and toss the >> results >> into mysql so, in turn, that database can be indexed by Sphinx >> Search. >> >> Since I don't want to reinvent the wheel, is anyone aware of any open >> source projects or code snippets that can already handle this? > > Have a look at http://nikitathespider.com/python/ As the author of Nikita, I can say that (a) she used Postgres and (b) the code wasn't open sourced except for a couple of small parts. The service is now defunct. It wasn't making money. Ideally I'd like to open source the code one day, but it would take a lot of documentation work to make it installable by others, and I won't have the time to do that for the foreseeable future. At the URL provided there's a nice module for parsing robots.txt files (better than the one in the standard library IMHO) but that's about it. FYI, I wrote my spider in Python because I couldn't find a decent one written in Python. There's Nutch, but that's not Python (Java I think). Good luck Philip From ppearson at nowhere.invalid Thu Apr 9 23:31:57 2009 From: ppearson at nowhere.invalid (Peter Pearson) Date: 10 Apr 2009 03:31:57 GMT Subject: Computed attribute names References: Message-ID: <747stdF1209a6U1@mid.individual.net> On Wed, 8 Apr 2009 19:47:46 +0100, Dale Amon wrote: [snip] > self.IBM029 = re.compile([^acharset] > self.IBM026 = re.compile([^anothercharset] Whoa! Thanks for the trip down memory lane. And a cheery IEFBR14 to you, too. -- To email me, substitute nowhere->spamcop, invalid->net. From cdalten at gmail.com Thu Apr 9 23:36:16 2009 From: cdalten at gmail.com (grocery_stocker) Date: Thu, 9 Apr 2009 20:36:16 -0700 (PDT) Subject: How come I can't get get my background thread to output anything? References: Message-ID: On Apr 9, 7:01 pm, grocery_stocker wrote: > On Apr 9, 6:36 pm, grocery_stocker wrote: > > > Given the following.... > > > #!/usr/local/bin/python > > > import os > > import time > > import thread > > > def domsg(string, sleeptime, *args): > > print "%s was here", string > > #os.system('tel %s test' % person); > > #time.sleep(sleeptime) > > > def buildlist(): > > out = 1 > > persons = [] > > > while(out != 0): > > pern = raw_input("Enter person to message:") > > if (len(pern)): > > persons.append(pern) > > else: > > out = 0 > > return persons > > > if __name__ == "__main__": > > #buildlist() > > thread.start_new_thread(domsg, ("person",2)) > > > I get.... > > m-net% ./massmsg.py > > m-net% > > > I was expecting to see > > > person was here Never mind. When i add while 1:pass like in the following thread.start_new_thread(domsg, ("person",2)) while 1 : pass the code works as expected From ppearson at nowhere.invalid Thu Apr 9 23:40:55 2009 From: ppearson at nowhere.invalid (Peter Pearson) Date: 10 Apr 2009 03:40:55 GMT Subject: numpy.where References: <148739635947045949601137627602034810513-Webmail@me.com> Message-ID: <747te7F1209a6U2@mid.individual.net> On Thu, 09 Apr 2009 09:09:18 -0400, Lou Pecora wrote: > > Thanks, Neil. Always something to learn. I've used Numpy for several > years, but still have not plumbed the depths. Just tried this script > and, yep, it works. > > arr=array([-1,1.0,2.2,-10.0,1.1, 0.9,-0.9]) > cond= arr < 1.0 > print cond > brr=arr[cond] > print brr > > Output: > > [ True False False True False True True] > [ -1. -10. 0.9 -0.9] > > Really, I've gotta RTFM. :-) Hey, if you find TFM, please tell me where it is. I haven't found anything Fine. I even bought Travis Oliphant's book, which helps a little, but . . . -- To email me, substitute nowhere->spamcop, invalid->net. From bmdavll at gmail.com Thu Apr 9 23:48:20 2009 From: bmdavll at gmail.com (David Liang) Date: Thu, 9 Apr 2009 20:48:20 -0700 (PDT) Subject: Running an interactive subprocess with Popen Message-ID: <89471f45-fa0a-4dde-943e-32cf25a1794d@y13g2000yqn.googlegroups.com> Hello, Sorry for the newbie question. How do I run a program that could block, waiting for user input, using subprocess.Popen? For example, from subprocess import * def foo(): a = Popen(['python'] ...) I want to be able to get input from the user and send input to the subprocess, printing stdout and stderr as data becomes available, then return once the subprocess exits. Is it possible to send to the subprocess keyboard interrupts, EOF, and such? I tried doing stdout=PIPE, stderr=PIPE, stdin=PIPE. I tried using communicate(), but could only call it once; subsequent calls raised "ValueError: I/O operation on closed file." And both a.stdout.read() and a.stderr.read() blocked the program. Any help would be much appreciated. -David From aahz at pythoncraft.com Fri Apr 10 00:07:04 2009 From: aahz at pythoncraft.com (Aahz) Date: 9 Apr 2009 21:07:04 -0700 Subject: building release - assert-free python library References: <121dcfb3-1ffe-40bf-b6dc-d2bacc073c3f@k19g2000prh.googlegroups.com> Message-ID: In article <121dcfb3-1ffe-40bf-b6dc-d2bacc073c3f at k19g2000prh.googlegroups.com>, grbgooglefan wrote: > >How can I build a release and not the debug version of libpython.a? >I have seen that there are assert, abort statements in lot many >functions in Python code. I would like to avoid those when compiling >the libpython.a library because when this libpython gets used for >production purpose and it aborts, the application goes down without >any means of graceful handling that error condition. >How can we handle this? Are you talking about Python source code or C source code? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ Why is this newsgroup different from all other newsgroups? From davea at ieee.org Fri Apr 10 00:22:09 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 10 Apr 2009 00:22:09 -0400 Subject: How to import a module so that the current globals are available to the module? In-Reply-To: <957d759b-0c2a-4f20-90a8-d44322eb5e6b@f11g2000vbf.googlegroups.com> References: <957d759b-0c2a-4f20-90a8-d44322eb5e6b@f11g2000vbf.googlegroups.com> Message-ID: <49DEC971.704@ieee.org> mrstevegross wrote: > I'm trying to import a module so that the globals() of the importer > module are available to the imported module itself. Consider the > following scenario: > > === mymod.py === > def go(): > some_special_function(1,2) > # 'some_special_function' is a built-in function available in the > scope of foo.py (see below) > > === foo.py === > some_special_function(3,4) # 'some_special_function' is a built-in > function > import mymod > mymod.go() > === EOF === > > The problem is this: in foo.py, you can call 'some_special_function' > all you want, because it's builtin. You can even 'print globals()' to > verify that it is available. > > However, when mymod.py tries to invoke 'some_special_function', it > fails because the function is NOT available in that scope. > > So, the question is: how can I make builtin functions available in the > test.py scope available to the mymod.py scope? > > One awkward solution is to deliberately initialize mymod.py ilke so: > > === mymod.py === > globs=None > def initialize(globs_): globs = globs_ > def go(): > globs['some_special_function'] (1,2) > > === foo.py === > some_special_function(3,4) # 'some_special_function' is a built-in > function > import mymod > mymod.initialize(globals()) > mymod.go() > === EOF === > > That will work, but it's a bit ugly. Plus, I have to repeat it for > every module with the same problem (of which I have many!). > > Is there a way to use __import__ to make this work? Any ideas? > > Thanks, > --Steve > > Your sample is very hard to follow, because you're mis-using the word built-in. Built-ins are part of the Python implementation, and available everywhere, while you apparently just mean a function visible in foo.py, which may have been imported from somewhere else. One solution would be that after importing mymod, you assign to its global space, as follows: def some_special_function(): ..... import mymod mymod.some_special_function = some_special_function You're adding a global name to the mymod namespace, and giving it a reference to a function in your own space. Note that the two names don't have to be the same in the two files. You could use, for example: import os mymod.some_special_function = os.path.join From davea at ieee.org Fri Apr 10 00:43:38 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 10 Apr 2009 00:43:38 -0400 Subject: Temporary file In-Reply-To: <73045cca0904091703j5b14638bve57bff3646549ec0@mail.gmail.com> References: <73045cca0904091703j5b14638bve57bff3646549ec0@mail.gmail.com> Message-ID: <49DECE7A.1030507@ieee.org> aditya shukla wrote: > I am trying to create a temporary file. > > import tempfile > > temp = tempfile.NamedTemporaryFile(suffix='_suffix', > prefix='prefix_', > dir='/tmp', > ) > try: > print 'temp:', temp > print 'temp.name:', temp.name > finally: > temp.close() > > > But when i do ls /tmp or ls -a /tmp i cannot find the file or if i try > to os.remove(temp.name) it shows a message that the file is not > found.But print temp and print temp.name show > temp: ', mode 'w+b' at 0xb7e84800> > temp.name: /home/swamp2/cacsgrad/axs9347/Desktop/prefix_zOfoX__suffix > > > > Please help me to fix it. > > > > Aditya > > According to the docs for NamedTemporaryFile(), the file will be deleted when closed (unless you specify otherwise with delete=False) From sxn02 at yahoo.com Fri Apr 10 01:07:27 2009 From: sxn02 at yahoo.com (Sorin Schwimmer) Date: Thu, 9 Apr 2009 22:07:27 -0700 (PDT) Subject: Autocompletion Message-ID: <890793.32782.qm@web56004.mail.re3.yahoo.com> Sorry for being careless in my choice of words: when I wrote "dictionary", I meant a collection of words in any given human language, not a Python dictionary. Perhaps I should try to explain a bit more what I try to accomplish: in a notebook the user will write text, and certain words (collected in a, let's say, vocabulary) need this feature. Ideally, I'd like to start the autocompletion feature when the widget gets the focus, and stop it when focus is lost. Moreover, when starting, a numeric parameter should dictate that the autocompletion will kick in after writing the respective number of characters. I said "notebook" because that's my intended use, but any kind of text entry should have the same behaviour if autocompletion is associated with it. I am targeting an English and a French vocabulary. SxN __________________________________________________________________ Yahoo! Canada Toolbar: Search from anywhere on the web, and bookmark your favourite sites. Download it now http://ca.toolbar.yahoo.com. From fetchinson at googlemail.com Fri Apr 10 01:16:19 2009 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Thu, 9 Apr 2009 22:16:19 -0700 Subject: [Python-ideas] accurate errors for "magic" methods In-Reply-To: <200904101445.13348.steve@pearwood.info> References: <20090409190836.6c1b0802@o> <200904101445.13348.steve@pearwood.info> Message-ID: >> > Actually, I'm wrong: it's perfectly clear as long as the programmer >> > is able to follow all the necessary reflexion path; then probably >> > also able to solve the problem without any help from python. >> > >> > The issue here is that a very specific (and meaningful) case >> > (dict-like behaviour missing) is adressed using a very generic (and >> > thus helpless) message (attributeError). >> > >> > I think error cases about "magic" methods, that implement >> > conceptually meaningful behaviours, should have appropriate >> > messages. In the case above, maybe something like: "Values instance >> > is not an item container (no __getitem__ method found)." >> >> The time machine strikes again: >> >>> class A(object): pass >> >> ... >> >> >>> A()['a'] >> >> Traceback (most recent call last): >> File "", line 1, in >> TypeError: 'A' object is unsubscriptable >> >> >> (the difference being that A is new-style, while Values is >> old-style.) > > > Except that the error "object is unsubscriptable" might as well be in > Klingon to most people, particularly newbies. > > (1) It's easy to misread it as "unscriptable", which is even more > mysterious. > > (2) As far as I know, there's no tradition of describing key or index > lookup as "subscripting" in Python. I've never noticed it in doc > strings or the online docs, and after hanging around comp.lang.python > extensively for years, I feel safe to say it's not a common term among > even experienced Python developers. I suppose that there's a weak > connection between index lookup and subscripts in mathematics. > > (3) The classic error message tells the newbie exactly what the error > is: the object has no __getitem__ method. The new error message tells > the newbie nothing useful. Given that obj is unsubscriptable, what > needs to be done to make it subscriptable? Wait a minute! (1) __getitem__ can easily be confused with __gotitem__ (2) __gotitem__ is not far from __gotitom__ (3) which an unsuspecting newbie might interpret as __got it, Tom!__ (4a) in case the newbie is called Tom, he will only assume 'it' refers to his mind, i.e. will naturally conclude the error message is the evil way of the python interpreter that it managed to take control of his mind because what else can these underscores mean than a scary loud muhahahahhahahha accompanied by a 5 seconds tune by a classic orchestra? He will either switch off his computer or cut his own veins with a nearby USB plug. (4b) in case the newbie is not called Tom, well, it's better not even thinking about this option So you just be careful! Cheers, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From pavlovevidence at gmail.com Fri Apr 10 01:21:05 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 9 Apr 2009 22:21:05 -0700 (PDT) Subject: How come I can't get get my background thread to output anything? References: Message-ID: <5ce3ace6-9126-46b0-87e0-a218587c9ad9@r15g2000vbi.googlegroups.com> On Apr 9, 8:36?pm, grocery_stocker wrote: > On Apr 9, 7:01 pm, grocery_stocker wrote: > > > > > On Apr 9, 6:36 pm, grocery_stocker wrote: > > > > Given the following.... > > > > #!/usr/local/bin/python > > > > import os > > > import time > > > import thread > > > > def domsg(string, sleeptime, *args): > > > ? ? print "%s was here", string > > > ? ? #os.system('tel %s test' % person); > > > ? ? #time.sleep(sleeptime) > > > > def buildlist(): > > > ? ? out = 1 > > > ? ? persons = [] > > > > ? ? while(out != 0): > > > ? ? ? ? pern = raw_input("Enter person to message:") > > > ? ? ? ? if (len(pern)): > > > ? ? ? ? ? ? persons.append(pern) > > > ? ? ? ? else: > > > ? ? ? ? ? ? out = 0 > > > ? ? return persons > > > > if __name__ == "__main__": > > > ? ? #buildlist() > > > ? ? thread.start_new_thread(domsg, ("person",2)) > > > > I get.... > > > m-net% ./massmsg.py > > > m-net% > > > > I was expecting to see > > > > person ?was here > > ?Never mind. When i add while 1:pass like in the following > > thread.start_new_thread(domsg, ("person",2)) > while 1 : pass > > the code works as expected Whoa, there, chief, you don't want to do that. It'll cause a busy loop and run one of your CPUs to 100%. Instead, use the theading module and the join method: import threading thr = threading.Thread(target=domsg,args=("person",2)) thr.start() # do whatever in the main thread thr.join() Carl Banks From rudy.egenias at gmail.com Fri Apr 10 01:35:23 2009 From: rudy.egenias at gmail.com (mr_e) Date: Thu, 9 Apr 2009 22:35:23 -0700 (PDT) Subject: authentication Message-ID: <956e1773-1567-41cd-ac4d-1c8174197d0f@a7g2000yqk.googlegroups.com> Trying to create a parser, that will first log into one of the account in a certain page. Then its suppose to simulate a click on one of the forms in the authenticated page (line 17 onwards). Does not want to take effect. Does it have something to do with referrers or is there anything wrong with what I am doing? http://paste.pocoo.org/show/111878/ Thanks in advance. From stefan_ml at behnel.de Fri Apr 10 01:58:30 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Fri, 10 Apr 2009 07:58:30 +0200 Subject: xml.dom.minidom getElementsByTagName white space issue In-Reply-To: References: <656946.72976.qm@web110406.mail.gq1.yahoo.com> Message-ID: <49dee006$0$32682$9b4e6d93@newsspool2.arcor-online.net> R. David Murray wrote: > Leonardo lozanne wrote: >> I'm getting some XML tags with white spaces from a web service and >> when I try to get them with the getElements ByTagName I'm not able to >> do so. I'm getting an empty list. What I'm doing is: >> >> #XML_response is an xml string >> xml_msg = xml.dom.minidom.parseString(XML_response) >> >> nodes = xml_msg.getElementsByTagName("tag ten") #tag name is "tag ten" with a whitespace >> >> It all works fine with tags like tag_seven but NOT for tag names with >> a white space. I've tried some escape chars but it doesnt seems to work. >> >> Does anybody has the escape char sequence I should be using or a work >> around for this? Thanks in advanced for your replies. > > As far as I can tell what you are getting is invalid XML. > So I think the answer is "you can't do that". > > (cf: http://www.w3.org/TR/2004/REC-xml11-20040204/#NT-NameChar) Yes, that is correct. You should check why you are getting this (i.e. who sends you this) and have them fix it. There is nothing you can (or should!) do on your side to work around this. Stefan From hniksic at xemacs.org Fri Apr 10 02:23:12 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Fri, 10 Apr 2009 08:23:12 +0200 Subject: Python C API String Memory Consumption References: <41d95468-8f7a-4647-83e1-6df147744467@u8g2000yqn.googlegroups.com> Message-ID: <87iqldjacv.fsf@busola.homelinux.net> aahz at pythoncraft.com (Aahz) writes: > BTW, note that if you're using Python 2.x, range(1000000) will cause > a "leak" because ints are never freed. Instead, use xrange(). Note that using xrange() won't help with that particular problem. From spamless.aurelien.campeas at free.fr Fri Apr 10 03:25:08 2009 From: spamless.aurelien.campeas at free.fr (=?ISO-8859-1?Q?Aur=E9lien_Camp=E9as?=) Date: Fri, 10 Apr 2009 09:25:08 +0200 Subject: Jython on Google AppEngine. In-Reply-To: References: <537ac6bf-dda0-4509-a0b6-6f67551f1015@r37g2000yqn.googlegroups.com> <85d1d6ee-e8b8-48f5-87e0-41fb2fc03245@z1g2000yqn.googlegroups.com> Message-ID: <49def454$0$5625$426a34cc@news.free.fr> Le 9/04/09 23:51, Tino Wildenhain a ?crit : > Jason Scheirer wrote: >> On Apr 9, 9:12 am, Alan Kennedy wrote: >>> Hi all, >>> >>> You may be interested to know that you can now run jython 2.2 out of >>> the box on Google AppEngine, thanks to their new java support. > ... >> >> Finally! A way to run Python on App Engine! > > ? > > Is that some kind of weird humor? :-) > > T. Ni ! From robert.kern at gmail.com Fri Apr 10 03:39:46 2009 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 10 Apr 2009 02:39:46 -0500 Subject: numpy.where In-Reply-To: <747te7F1209a6U2@mid.individual.net> References: <148739635947045949601137627602034810513-Webmail@me.com> <747te7F1209a6U2@mid.individual.net> Message-ID: On 2009-04-09 22:40, Peter Pearson wrote: > On Thu, 09 Apr 2009 09:09:18 -0400, Lou Pecora wrote: >> Thanks, Neil. Always something to learn. I've used Numpy for several >> years, but still have not plumbed the depths. Just tried this script >> and, yep, it works. >> >> arr=array([-1,1.0,2.2,-10.0,1.1, 0.9,-0.9]) >> cond= arr< 1.0 >> print cond >> brr=arr[cond] >> print brr >> >> Output: >> >> [ True False False True False True True] >> [ -1. -10. 0.9 -0.9] >> >> Really, I've gotta RTFM. :-) > > Hey, if you find TFM, please tell me where it is. I haven't > found anything Fine. I even bought Travis Oliphant's book, > which helps a little, but . . . http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco From castironpi at gmail.com Fri Apr 10 03:41:12 2009 From: castironpi at gmail.com (Aaron Brady) Date: Fri, 10 Apr 2009 00:41:12 -0700 (PDT) Subject: Scrap Posts References: Message-ID: On Apr 9, 1:28?pm, Grant Edwards wrote: > On 2009-04-09, Avi wrote: > ? 2. Filter out everything posted from google groups. > > ? 3. Read the group via the mailing list (which has much better > ? ? ?filtering). The mailing list is gatewayed by gmane.org if > ? ? ?you prefer web or NNTP access instead of seeing stuff in > ? ? ?your inbox. ? > > I rely mostly on option 2. ?You loose some non-spam posts, but > anybody posting from Google Groups ought not be surprised to be > lost among the spam. > > > Can we clean it up? > > Not really, no. ?Google could, but they refuse. What, and part with such a source of revenue? From deets at nospam.web.de Fri Apr 10 03:56:11 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 10 Apr 2009 09:56:11 +0200 Subject: Running an interactive subprocess with Popen In-Reply-To: <89471f45-fa0a-4dde-943e-32cf25a1794d@y13g2000yqn.googlegroups.com> References: <89471f45-fa0a-4dde-943e-32cf25a1794d@y13g2000yqn.googlegroups.com> Message-ID: <748ccsFqk05dU1@mid.uni-berlin.de> David Liang schrieb: > Hello, > Sorry for the newbie question. How do I run a program that could > block, waiting for user input, using subprocess.Popen? For example, > > from subprocess import * > > def foo(): > a = Popen(['python'] ...) > > I want to be able to get input from the user and send input to the > subprocess, printing stdout and stderr as data becomes available, then > return once the subprocess exits. Is it possible to send to the > subprocess keyboard interrupts, EOF, and such? > > I tried doing stdout=PIPE, stderr=PIPE, stdin=PIPE. I tried using > communicate(), but could only call it once; subsequent calls raised > "ValueError: I/O operation on closed file." > And both a.stdout.read() and a.stderr.read() blocked the program. Any > help would be much appreciated. Use pexpect, which emulates a pseudo-terminal speaking with the subprocess. Diez From murali.au at gmail.com Fri Apr 10 04:45:06 2009 From: murali.au at gmail.com (Murali kumar) Date: Fri, 10 Apr 2009 14:15:06 +0530 Subject: communication between objects - help In-Reply-To: References: <49DE4521.7040605@probo.com> Message-ID: hi all.. I'm posted in a word doc becoz to add a image to explain my problem.. also I think gmail automatically scans for attachments.. anyway.. here's my problem...( see the image) http://www.2shared.com/file/5299759/45e4c614/load.html Using : Python 2.6 , wxPython 2.8.9 *Scenario: *In my application , a menu invoking a frame class. It?s having a wx.notebook control like above. Each notebook page is also a class. In loadprofile class i'm tried to load a configuration file already created by application. (when pressing load button, current selected config filename in a listbox must be loaded for application.) config file data must to be applied to my application object. mainApp object ---> Frame object ( wx.notebook ) | Load profile class | (return config file or data to mainApp object in load button Event handler function..) * I don?t know how to return config file or data to mainApp object when pressing load button. * * Is it easy to return filename only and load the file?s data into mainApp object in menu handler itself? * Please suggest me right direction.. and tell me how to do it? Usually where I can get these informations.. (suggest links and books..) Thanks for any advice.... -------------- next part -------------- An HTML attachment was scrubbed... URL: From duncan.booth at invalid.invalid Fri Apr 10 05:03:51 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 10 Apr 2009 09:03:51 GMT Subject: calculate field in ARCGIS References: Message-ID: MRAB wrote: > You might also want to try a triple-quoted string, which makes it > clearer: > > codeblock = """def codefun(code): > if code == 0: > return "B" > else: > return "" > """ > Possibly the clearest way to do something like that is to use a single backslash escape and then all the vertical alignment is exactly as you see it: codeblock = """\ def codefun(code): if code == 0: return "B" else: return "" """ From andrey.demidov at gmail.com Fri Apr 10 05:33:27 2009 From: andrey.demidov at gmail.com (Demidov Andrey) Date: Fri, 10 Apr 2009 13:33:27 +0400 Subject: XMLRPC - persistent object state on server Message-ID: Hi, all I need a XMLRPC server, which works with database and returns data to the clients. But I can not find any possibility to keep the object state on server between the clients calls. Here is my code: 1. Server: from SimpleXMLRPCServer import SimpleXMLRPCServer from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler # Restrict to a particular path. class RequestHandler(SimpleXMLRPCRequestHandler): rpc_paths = ('/RPC2',) # Create server server = SimpleXMLRPCServer(("localhost", 8000), requestHandler=RequestHandler) server.register_introspection_functions() class MyClass: def __init__(self, a): self.a = a # and some heavy works which I would like to do once def say(self): return a cl = MyClass(100) def return_my_value(): return cl.say() server.register_function(return_my_value, 'r_v') # Run the server's main loop server.serve_forever() 2. Client: import xmlrpclib s = xmlrpclib.ServerProxy('http://localhost:8000') print s.r_v() When I'm running client I get this error message: demas at myhost ~/sources/study/python $ python rpc_client.py Traceback (most recent call last): File "rpc_client.py", line 4, in print s.r_v() File "/usr/lib/python2.6/xmlrpclib.py", line 1199, in __call__ return self.__send(self.__name, args) File "/usr/lib/python2.6/xmlrpclib.py", line 1489, in __request verbose=self.__verbose File "/usr/lib/python2.6/xmlrpclib.py", line 1253, in request return self._parse_response(h.getfile(), sock) File "/usr/lib/python2.6/xmlrpclib.py", line 1392, in _parse_response return u.close() File "/usr/lib/python2.6/xmlrpclib.py", line 838, in close raise Fault(**self._stack[0]) xmlrpclib.Fault: :global name 'a' is not defined"> How can I fix it? Is there any possibility to keep the object state between the clients calls? Thanks, Demas -------------- next part -------------- An HTML attachment was scrubbed... URL: From brian at sweetapp.com Fri Apr 10 05:37:11 2009 From: brian at sweetapp.com (Brian Quinlan) Date: Fri, 10 Apr 2009 10:37:11 +0100 Subject: XMLRPC - persistent object state on server In-Reply-To: References: Message-ID: <49DF1347.4040606@sweetapp.com> Demidov Andrey wrote: > class MyClass: > def __init__(self, a): > self.a = a > # and some heavy works which I would like to do once > def say(self): > return a > Change: def say(self): return a to: def say(self): return self.a Cheers, Brian From andrey.demidov at gmail.com Fri Apr 10 05:42:25 2009 From: andrey.demidov at gmail.com (Demidov Andrey) Date: Fri, 10 Apr 2009 13:42:25 +0400 Subject: XMLRPC - persistent object state on server In-Reply-To: <49DF1347.4040606@sweetapp.com> References: <49DF1347.4040606@sweetapp.com> Message-ID: Thank you. Of course, it is my stupid mistake. Change: > def say(self): > return a > > to: > def say(self): > return self.a > > Cheers, > Brian > -------------- next part -------------- An HTML attachment was scrubbed... URL: From smartpawn at gmail.com Fri Apr 10 05:46:47 2009 From: smartpawn at gmail.com (Deepak Rokade) Date: Fri, 10 Apr 2009 15:16:47 +0530 Subject: Multiprocessing module Message-ID: <48224a820904100246n74fd303am43f52c713f2431c9@mail.gmail.com> Hi All, I have decided to use multiprocessing module in my application. In brief, my application fetches files from multiple remote directories and distributes the received files to one or more remote directories using SFTP. Since this application is going to be commercial one I want to know at this stage if there are any known serious bugs (not limitations) in the multiprocessing module? Also are there any examples where this module has been used for commercial applications? Where can I get this information about companies / products using this particular module? -- Thanx & Regards, Deepak Rokade -------------- next part -------------- An HTML attachment was scrubbed... URL: From pavlovevidence at gmail.com Fri Apr 10 05:47:32 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 10 Apr 2009 02:47:32 -0700 (PDT) Subject: Python C API String Memory Consumption References: <41d95468-8f7a-4647-83e1-6df147744467@u8g2000yqn.googlegroups.com> <87iqldjacv.fsf@busola.homelinux.net> Message-ID: <334ee872-5d92-4e7b-9906-0ca5f0b6ed38@f19g2000yqo.googlegroups.com> On Apr 9, 11:23?pm, Hrvoje Niksic wrote: > a... at pythoncraft.com (Aahz) writes: > > BTW, note that if you're using Python 2.x, range(1000000) will cause > > a "leak" because ints are never freed. ?Instead, use xrange(). > > Note that using xrange() won't help with that particular problem. I think it will because with xrange the integers will not all have to exist at one time, so Python doesn't have to increase the size of the integer pool to a million. Carl Banks From hniksic at xemacs.org Fri Apr 10 06:26:01 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Fri, 10 Apr 2009 12:26:01 +0200 Subject: Python C API String Memory Consumption References: <41d95468-8f7a-4647-83e1-6df147744467@u8g2000yqn.googlegroups.com> <87iqldjacv.fsf@busola.homelinux.net> <334ee872-5d92-4e7b-9906-0ca5f0b6ed38@f19g2000yqo.googlegroups.com> Message-ID: <878wm8kdom.fsf@busola.homelinux.net> Carl Banks writes: > On Apr 9, 11:23?pm, Hrvoje Niksic wrote: >> a... at pythoncraft.com (Aahz) writes: >> > BTW, note that if you're using Python 2.x, range(1000000) will cause >> > a "leak" because ints are never freed. ?Instead, use xrange(). >> >> Note that using xrange() won't help with that particular problem. > > I think it will because with xrange the integers will not all have > to exist at one time, so Python doesn't have to increase the size of > the integer pool to a million. Good catch! I stand corrected. From ldo at geek-central.gen.new_zealand Fri Apr 10 06:26:59 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Fri, 10 Apr 2009 22:26:59 +1200 Subject: Generating User Search Queries Message-ID: I implemented a form in a Web-based database-management application that lets the user search on a whole lot of different fields. I hate writing repetitive code. But here's part of the sequence I came up with for translating entered field values into MySQL query phrases: condition = \ ( list ( # free-text fields "%(name)s like %(value)s" % { "name" : field[0], "value" : SQLString("%" + EscapeSQLWild(Params.getvalue(field[1])) + "%" ), } for field in ( ("make", "search_make"), ("model", "search_model"), ... ) if Params.getvalue(field[1]) != "" ) + list ( # exact-match fields "%(name)s = %(value)s" % { "name" : field[0], "value" : SQLString(Params.getvalue(field[1])), } for field in ( ("class_name", "search_class"), ... ) if Params.getvalue(field[1]) != "" ) + list ( # date fields "(" + " or ".join ( "%(name)s %(op)s %(value)s" % { "name" : field[0], "op" : op[0], "value" : SQLString(Params.getvalue(field[1])), } for op in ( ("<", "lt"), ("=", "eq"), (">", "gt"), ) if GetCheckbox("%(name)s[%(op)s]" % {"name" : field[1], "op" : op[1]}) ) + ")" for field in ( ("when_purchased", "search_when_purchased"), ... ) if reduce ( operator.__or__, ( GetCheckbox("%(name)s[%(op)s]" % {"name" : field[1], "op" : op}) for op in ("lt", "eq", "gt") ) ) ) ) And then you can build the whole thing into a where-clause just with " and ".join(condition) From davea at ieee.org Fri Apr 10 06:32:54 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 10 Apr 2009 06:32:54 -0400 Subject: communication between objects - help In-Reply-To: References: <49DE4521.7040605@probo.com> Message-ID: <49DF2056.6060208@ieee.org> Murali kumar wrote: > hi all.. > I'm posted in a word doc becoz to add a image to explain my problem.. > also I think gmail automatically scans for attachments.. > > anyway.. here's my problem...( see the image) > > http://www.2shared.com/file/5299759/45e4c614/load.html > > Using : Python 2.6 , wxPython 2.8.9 > <....snip...> > > * I don?t know how to return config file or data to mainApp object when > pressing load button. * > > > * Is it easy to return filename only and load the file?s data into mainApp > object in menu handler itself? * > > > Please suggest me right direction.. and tell me how to do it? > > Usually where I can get these informations.. (suggest links and books..) > > > Thanks for any advice.... > > Most of the widget classes in your code should be derived classes, so they can hold extra instance data & event handlers and such. So that means you can have extra parameters on the constructor, besides the ones the base class requires. Use one or more of those extra parameters to store your own information about the hierarchy. Simplest example is to add the app instance to each constructor. Then each widget object would know how to call back into the app to do some work, or to load data into a common place. DaveA From b.mahdi at gmail.com Fri Apr 10 06:37:08 2009 From: b.mahdi at gmail.com (bingo) Date: Fri, 10 Apr 2009 03:37:08 -0700 (PDT) Subject: Adding a Badge to an Icon in Mac OS X Message-ID: Hi all, I have been, now for daysm trying to figure out how to add a badge to an Icon in Mac OS X. Carbon Icon Services is not documented and I new to Mac programming. Any help would be greatly appreciated! Cheers, From ebonak at hotmail.com Fri Apr 10 07:24:30 2009 From: ebonak at hotmail.com (Esmail) Date: Fri, 10 Apr 2009 07:24:30 -0400 Subject: Request For Comment In-Reply-To: References: Message-ID: <49DF2C6E.9000408@hotmail.com> Aahz wrote: > How RFC1 got created: > > http://www.nytimes.com/2009/04/07/opinion/07crocker.html That was great, thanks for posting the link. Esmail From ebonak at hotmail.com Fri Apr 10 07:24:30 2009 From: ebonak at hotmail.com (Esmail) Date: Fri, 10 Apr 2009 07:24:30 -0400 Subject: Request For Comment In-Reply-To: References: Message-ID: <49DF2C6E.9000408@hotmail.com> Aahz wrote: > How RFC1 got created: > > http://www.nytimes.com/2009/04/07/opinion/07crocker.html That was great, thanks for posting the link. Esmail From paulgeeleher at gmail.com Fri Apr 10 07:36:43 2009 From: paulgeeleher at gmail.com (sophie_newbie) Date: Fri, 10 Apr 2009 04:36:43 -0700 (PDT) Subject: Can't create list of dictionaries Message-ID: <47642810-6388-4c27-aeca-8b32830575a2@37g2000yqp.googlegroups.com> Hi there, I've got a function that returns a dictionary, I need to loop and return 1000 dictionaries and append them to a list, but the thing is that when I do the list.append(funtThatReturnsDict()) the resulting only ever has 1 dictionary attached to it, even after running the append function 1000 times! I've tried using dict.copy() on the dictionary that was returned from the function but this didn't work either. And the function is definately returning different dictionaries each time as I can see that when I print them. I know this is something to do with the dictionries being stored as references but I've no idea how to fix it seeing as the copy() function didn't work. Thanks! From paulgeeleher at gmail.com Fri Apr 10 07:49:12 2009 From: paulgeeleher at gmail.com (sophie_newbie) Date: Fri, 10 Apr 2009 04:49:12 -0700 (PDT) Subject: Can't create list of dictionaries References: <47642810-6388-4c27-aeca-8b32830575a2@37g2000yqp.googlegroups.com> Message-ID: <6971a8c6-fcfb-42a1-ba20-b63209817d36@c9g2000yqm.googlegroups.com> Scratch everything I said, copy() does work. Made a wee mistake somewhere else. Apologies if I've wasted anyones time! On Apr 10, 12:36?pm, sophie_newbie wrote: > Hi there, > > I've got a function that returns a dictionary, I need to loop and > return 1000 dictionaries and append them to a list, but the thing is > that when I do the list.append(funtThatReturnsDict()) the resulting > only ever has 1 dictionary attached to it, even after running the > append function 1000 times! > > I've tried using dict.copy() on the dictionary that was returned from > the function but this didn't work either. > > And the function is definately returning different dictionaries each > time as I can see that when I print them. > > I know this is something to do with the dictionries being stored as > references but I've no idea how to fix it seeing as the copy() > function didn't work. > > Thanks! From sylvain.thenault at logilab.fr Fri Apr 10 07:49:51 2009 From: sylvain.thenault at logilab.fr (Sylvain =?utf-8?B?VGjDqW5hdWx0?=) Date: Fri, 10 Apr 2009 13:49:51 +0200 Subject: weird try/finally behaviour Message-ID: <20090410114951.GH1430@lupus.logilab.fr> Hi there, I've encountered the following behaviour which I found surprising: >>> def test(): ... for x in ('test', 'tests'): ... try: ... if True: ... print 'return' ... return 1 ... finally: ... print 'break' ... break ... print 'end' ... >>> >>> test() return break end As you can see, the 'break' in the finally block makes the 'return 1' beeing ignored. Is this a known caveat or should it be considered as a bug? -- Sylvain Th?nault LOGILAB, Paris (France) Formations Python, Debian, M?th. Agiles: http://www.logilab.fr/formations D?veloppement logiciel sur mesure: http://www.logilab.fr/services CubicWeb, the semantic web framework: http://www.cubicweb.org From sjmachin at lexicon.net Fri Apr 10 07:53:44 2009 From: sjmachin at lexicon.net (John Machin) Date: Fri, 10 Apr 2009 04:53:44 -0700 (PDT) Subject: Can't create list of dictionaries References: <47642810-6388-4c27-aeca-8b32830575a2@37g2000yqp.googlegroups.com> Message-ID: On Apr 10, 9:36?pm, sophie_newbie wrote: > Hi there, > > I've got a function that returns a dictionary, I need to loop and > return 1000 dictionaries and append them to a list, but the thing is > that when I do the list.append(funtThatReturnsDict()) the resulting > only ever has 1 dictionary attached to it, even after running the > append function 1000 times! Do you mean that the length of the list is 1, or do you mean that the same dictionary has been appended 1000 times? > > I've tried using dict.copy() on the dictionary that was returned from > the function but this didn't work either. > > And the function is definately returning different dictionaries each > time as I can see that when I print them. > > I know this is something to do with the dictionries being stored as > references but I've no idea how to fix it seeing as the copy() > function didn't work. I think you had better show us your code, and the output from (say) 3 iterations (not 1000!!). If you are really doing "list.append (functThatReturnsDict())" all in one line, break it up into multiple lines so that some print statements can be used for debugging: adict = functThatReturnsDict() print "A", id(adict), len(adict), adict alist.append(adict) print "B", len(alist), id(alist[-1]) Clean it up a bit ... the dict.copy() is extremely unlikely to be part of a principled solution, so remove any trace of that. Cheers, John From sjmachin at lexicon.net Fri Apr 10 07:58:52 2009 From: sjmachin at lexicon.net (John Machin) Date: Fri, 10 Apr 2009 04:58:52 -0700 (PDT) Subject: Can't create list of dictionaries References: <47642810-6388-4c27-aeca-8b32830575a2@37g2000yqp.googlegroups.com> <6971a8c6-fcfb-42a1-ba20-b63209817d36@c9g2000yqm.googlegroups.com> Message-ID: <668ebfcf-4238-41cf-ad3a-67149f810d2c@u16g2000vbb.googlegroups.com> On Apr 10, 9:49?pm, sophie_newbie wrote: > Scratch everything I said, copy() does work. Made a wee mistake > somewhere else. > > Apologies if I've wasted anyones time! Ummmm ... if think you need a dict.copy() *after* the function has returned, then you are likely to be doing it in a way that perhaps you shouldn't make a habit of :-) From __peter__ at web.de Fri Apr 10 08:02:01 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 10 Apr 2009 14:02:01 +0200 Subject: Can't create list of dictionaries References: <47642810-6388-4c27-aeca-8b32830575a2@37g2000yqp.googlegroups.com> Message-ID: sophie_newbie wrote: > Hi there, > > I've got a function that returns a dictionary, I need to loop and > return 1000 dictionaries and append them to a list, but the thing is > that when I do the list.append(funtThatReturnsDict()) the resulting > only ever has 1 dictionary attached to it, even after running the > append function 1000 times! > > I've tried using dict.copy() on the dictionary that was returned from > the function but this didn't work either. > > And the function is definately returning different dictionaries each > time as I can see that when I print them. > > I know this is something to do with the dictionries being stored as > references but I've no idea how to fix it seeing as the copy() > function didn't work. > > Thanks! Let's see. First, a function that returns a new dictionary on every run: >>> def new_dict(): return {} ... >>> dicts = [new_dict() for _ in range(3)] >>> dicts [{}, {}, {}] >>> dicts[1]["x"] = 42 >>> dicts [{}, {'x': 42}, {}] Works. Now a function that always returns the same dictionary: >>> def same_dict(d={}): return d ... >>> dicts = [same_dict() for _ in range(3)] >>> dicts [{}, {}, {}] >>> dicts[1]["x"] = 42 >>> dicts [{'x': 42}, {'x': 42}, {'x': 42}] That's not what we want. Can it be fixed with dict.copy()? >>> dicts = [same_dict().copy() for _ in range(3)] >>> dicts [{'x': 42}, {'x': 42}, {'x': 42}] Hm, it wasn't a good idea to reuse same_dict() from the previous example because the default value was changed by the dicts[1]["x"] = 42 statement. Second try: >>> def same_dict(d={}): return d ... >>> dicts = [same_dict().copy() for _ in range(3)] >>> dicts [{}, {}, {}] >>> dicts[1]["x"] = 42 >>> dicts [{}, {'x': 42}, {}] So the approach using dict.copy() works, too. Note however, that you ust make a deep copy if you have mutable values >>> d = dict(a=[]) >>> e = d.copy() >>> d["a"].append(42) >>> d {'a': [42]} >>> e {'a': [42]} >>> import copy >>> d = dict(a=[]) >>> e = copy.deepcopy(d) >>> d["a"].append(42) >>> d {'a': [42]} >>> e {'a': []} Peter From zutesmog at gmail.com Fri Apr 10 08:04:09 2009 From: zutesmog at gmail.com (Tim Hoffman) Date: Fri, 10 Apr 2009 05:04:09 -0700 (PDT) Subject: weird try/finally behaviour References: Message-ID: <24082b5d-9b51-4aa2-ac87-7398abf5c657@z9g2000yqi.googlegroups.com> Hi Sylvain You should have a read of the python docs, specifically on try: finally: excerpt from docs. -- When a return, break or continue statement is executed in the try suite of a try...finally statement, the finally clause is also executed `on the way out.' A continue statement is illegal in the finally clause. (The reason is a problem with the current implementation -- this restriction may be lifted in the future). See ya Tim On Apr 10, 7:49?pm, Sylvain Th?nault wrote: > Hi there, > > I've encountered the following behaviour which I found surprising: > > >>> def test(): > > ... ? ? for x in ('test', 'tests'): > ... ? ? ? ? ?try: > ... ? ? ? ? ? ? if True: > ... ? ? ? ? ? ? ? ? ? print 'return' > ... ? ? ? ? ? ? ? ? ? return 1 > ... ? ? ? ? ?finally: > ... ? ? ? ? ? ? print 'break' > ... ? ? ? ? ? ? break > ... ? ? print 'end' > ... > > >>> test() > > return > break > end > > As you can see, the 'break' in the finally block makes the 'return 1' beeing ignored. > Is this a known caveat or should it be considered as a bug? > -- > Sylvain Th?nault ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? LOGILAB, Paris (France) > Formations Python, Debian, M?th. Agiles:http://www.logilab.fr/formations > D?veloppement logiciel sur mesure: ? ? ?http://www.logilab.fr/services > CubicWeb, the semantic web framework: ? ?http://www.cubicweb.org From ebonak at hotmail.com Fri Apr 10 08:06:55 2009 From: ebonak at hotmail.com (Esmail) Date: Fri, 10 Apr 2009 08:06:55 -0400 Subject: is this possible (getting info off web page) In-Reply-To: <794a01c9b95e$22e16130$0301a8c0@tmesa.com> References: <794a01c9b95e$22e16130$0301a8c0@tmesa.com> Message-ID: <49DF365F.2020402@hotmail.com> bruce wrote: > Hi Esmail. > > I've not looked at the site. however, i can give you some general pointers > that might help you in solving your issue. Excellent advice, thanks for pointing me in those directions. I am not familiar with curl but will take this as an opportunty to learn about it. I think looking at the user-agent parameter may also help. Cheers, Esmail From ebonak at hotmail.com Fri Apr 10 08:06:55 2009 From: ebonak at hotmail.com (Esmail) Date: Fri, 10 Apr 2009 08:06:55 -0400 Subject: is this possible (getting info off web page) In-Reply-To: <794a01c9b95e$22e16130$0301a8c0@tmesa.com> References: <794a01c9b95e$22e16130$0301a8c0@tmesa.com> Message-ID: <49DF365F.2020402@hotmail.com> bruce wrote: > Hi Esmail. > > I've not looked at the site. however, i can give you some general pointers > that might help you in solving your issue. Excellent advice, thanks for pointing me in those directions. I am not familiar with curl but will take this as an opportunty to learn about it. I think looking at the user-agent parameter may also help. Cheers, Esmail From Eric_Dexter at msn.com Fri Apr 10 08:27:08 2009 From: Eric_Dexter at msn.com (edexter) Date: Fri, 10 Apr 2009 05:27:08 -0700 (PDT) Subject: Running an interactive subprocess with Popen References: <89471f45-fa0a-4dde-943e-32cf25a1794d@y13g2000yqn.googlegroups.com> <748ccsFqk05dU1@mid.uni-berlin.de> Message-ID: On Apr 10, 2:56?am, "Diez B. Roggisch" wrote: > David Liang schrieb: > > > > > > > Hello, > > Sorry for the newbie question. How do I run a program that could > > block, waiting for user input, using subprocess.Popen? For example, > > > from subprocess import * > > > def foo(): > > ? ? a = Popen(['python'] ...) > > > I want to be able to get input from the user and send input to the > > subprocess, printing stdout and stderr as data becomes available, then > > return once the subprocess exits. Is it possible to send to the > > subprocess keyboard interrupts, EOF, and such? > > > I tried doing stdout=PIPE, stderr=PIPE, stdin=PIPE. I tried using > > communicate(), but could only call it once; subsequent calls raised > > "ValueError: I/O operation on closed file." > > And both a.stdout.read() and a.stderr.read() blocked the program. Any > > help would be much appreciated. > > Use pexpect, which emulates a pseudo-terminal speaking with the subprocess. > > Diez- Hide quoted text - > > - Show quoted text - I am not sure about that particular issue put I did manage to get it working with windows in the dex tracker project.. you would have to search the files or something to figure out where.. when you are looking at examples it does matter wich operating system you are using at least for python 2.5 From Eric_Dexter at msn.com Fri Apr 10 08:40:11 2009 From: Eric_Dexter at msn.com (Eric_Dexter at msn.com) Date: Fri, 10 Apr 2009 05:40:11 -0700 (PDT) Subject: save error with embedded python 2.5 Message-ID: I seem to be getting one extra value when I create my values with for yy in range(1, maxy): for xx in range(1, maxx): count = count + 1 #squaret.append[count] squaret.append( square(xx * 20, yy * 20)) and saving boxes with if squaresave.state == '1': outfile = open('grid.dat','w') count = 0 for s in squaret: count = count + 1 outfile.write(s.state) outfile.write(' ') if count == maxy: outfile.write('\n') count = 0 thanks for any help in advance I am using python 2.5 embeded into pycap.. the rest of the source is available at http://dexrowem.blogspot.com/2009/04/pycap-drum-machine-or-piano-roll-beta.html if needed.. From heidihannah at mac.com Fri Apr 10 08:42:15 2009 From: heidihannah at mac.com (heidi taynton) Date: Fri, 10 Apr 2009 08:42:15 -0400 Subject: loop? maybe? Message-ID: <106459432132913336683907451585093989106-Webmail@me.com> Hey guys, Sorry for being such a noob with this stuff and the language is hard for me to read through with the online manuals... i can do math speak, and science speak... not so much programming/code speak... so when you say pickle... i think food.... Anyways, I have an array and I want to turn it into a new array. where nlines = 188 new_array=n.array(nlines) for i in arange(nlines): ##### and here i want to tell it to read old_array and take n data points where new_n=1.1**old_n ... take the mean of those data points and put them in the first spot in the array.... then loop through with until the array is full? If it doesn't make sense, i can try to clarify some more... Thanks, you guys are life savers, hi From piet at cs.uu.nl Fri Apr 10 08:52:22 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 10 Apr 2009 14:52:22 +0200 Subject: Can't create list of dictionaries References: <47642810-6388-4c27-aeca-8b32830575a2@37g2000yqp.googlegroups.com> Message-ID: >>>>> sophie_newbie (sn) wrote: >sn> Hi there, >sn> I've got a function that returns a dictionary, I need to loop and >sn> return 1000 dictionaries and append them to a list, but the thing is >sn> that when I do the list.append(funtThatReturnsDict()) the resulting >sn> only ever has 1 dictionary attached to it, even after running the >sn> append function 1000 times! >sn> I've tried using dict.copy() on the dictionary that was returned from >sn> the function but this didn't work either. >sn> And the function is definately returning different dictionaries each >sn> time as I can see that when I print them. >sn> I know this is something to do with the dictionries being stored as >sn> references but I've no idea how to fix it seeing as the copy() >sn> function didn't work. You better post some real code. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From cdalten at gmail.com Fri Apr 10 09:26:47 2009 From: cdalten at gmail.com (grocery_stocker) Date: Fri, 10 Apr 2009 06:26:47 -0700 (PDT) Subject: I can't get RLock to work (warning, the following code is pretty long) Message-ID: <8148d3c3-7b97-4fa1-ba99-e0bb0c2b6cbf@k8g2000yqn.googlegroups.com> When I run the following... #!/usr/bin/python import time import thread import threading def get_first_part(string, lock, sleeptime, *args): global counter lock.acquire() try: counter = counter + 1 data = counter print "%s value is %d" % (string, counter) time.sleep(sleeptime) finally: lock.release() return data def get_second_part(string, lock, sleeptime, *args): global counter lock.acquire() try: counter = counter + 1 data = counter print "%s value is %d" % (string, counter) time.sleep(sleeptime) finally: lock.release() return data def get_both_parts(string, lock, sleeptime, *args): global first, second lock.acquire() try: first = get_first_part() second = get_second_part() print "%s values are %d and %d" % (string, first, second) time.sleep(sleeptime) finally: lock.release() return first, second if __name__ == "__main__": #lock = thread.allocate_lock() lock = threading.RLock() counter = 0 first = 0 second = 0 thread.start_new_thread(get_first_part, ("Thread1", lock, 2)) thread.start_new_thread(get_second_part, ("Thread2", lock, 2)) thread.start_new_thread(get_both_parts, ("Thread3", lock, 2)) #Yes I was told this was bad, but I'm still doing until I can make it beyond #the basics. while 1: pass The code will jsut hang.... [cdalten at localhost oakland]$ ./rlock.py Thread1 value is 1 Thread2 value is 2 Traceback (most recent call last): File "./rlock.py", line 57, in ? while 1: pass KeyboardInterrupt How come RLock isn't working in this example? From piet at cs.uu.nl Fri Apr 10 09:44:30 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 10 Apr 2009 15:44:30 +0200 Subject: writing array References: Message-ID: >>>>> heidi taynton (ht) wrote: >ht> Hey guys, >ht> I'm trying to write a new array from the one i already have.... >ht> but here is the catch: >ht> I want an array where n=# of data points used in previous point (rounded down) >ht> in every spot, I want to take 1.1**n data points and then take the mean of the data points. (i know there is a numpy function mean() ).. put that is spot 0 and so on... any suggestions? >ht> for clarification: >ht> so the first spot takes 1 data point, b/c 1.1^0 =1 and the average of that is just the data point >ht> the second spot takes 2 data points b/c 1.1^1=1.1 which rounds up to 2.... then the average of those two >ht> the third spot takes 4 data points b/c 1.1^2 = 1.21 .... and then that will repeat.... >ht> make sense? Not much. As 1.1^2=1.21, I would think this would then take 2 datapoints, not 4. If you take at each step n = 1.1^n (rounded up) n will stay at 2 forever. And first you talk about rounding down, later about rounding up. So what is it. Maybe you should give a better specification? -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From __peter__ at web.de Fri Apr 10 09:48:45 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 10 Apr 2009 15:48:45 +0200 Subject: I can't get RLock to work (warning, the following code is pretty long) References: <8148d3c3-7b97-4fa1-ba99-e0bb0c2b6cbf@k8g2000yqn.googlegroups.com> Message-ID: grocery_stocker wrote: > When I run the following... > > #!/usr/bin/python > > import time > import thread > import threading > > def get_first_part(string, lock, sleeptime, *args): > global counter > lock.acquire() > try: > counter = counter + 1 > data = counter > print "%s value is %d" % (string, counter) > time.sleep(sleeptime) > finally: > lock.release() > return data > def get_both_parts(string, lock, sleeptime, *args): > global first, second > lock.acquire() > try: > first = get_first_part() > second = get_second_part() > print "%s values are %d and %d" % (string, first, > second) > time.sleep(sleeptime) > finally: > lock.release() > return first, second > How come RLock isn't working in this example? When get_both_parts() acquires the lock it invokes get_first_part() which tries to acquire the lock. This fails because get_both_parts() does not release the lock until after get_first_part() has finished... Peter From mike at ipglobal.net Fri Apr 10 10:28:28 2009 From: mike at ipglobal.net (Support Desk) Date: Fri, 10 Apr 2009 09:28:28 -0500 Subject: Open source web crawler with mysql integration In-Reply-To: <3BC408BC-2006-41E6-AB4E-698DA30CE56A@semanchuk.com> References: <9ed1c9c7-267e-448e-ba83-98db949d97ec@o11g2000yql.googlegroups.com> <3BC408BC-2006-41E6-AB4E-698DA30CE56A@semanchuk.com> Message-ID: <41B9D72305A64C4DA96CE9E632A981F0@office.ipglobal.net> Sounds Interesting. When its done would you care to share it? Sincerely, Michael H. -----Original Message----- From: Philip Semanchuk [mailto:philip at semanchuk.com] Sent: Thursday, April 09, 2009 9:46 PM To: Python Subject: Re: Open source web crawler with mysql integration On Apr 9, 2009, at 7:37 PM, Daniel Fetchinson wrote: >> I'm looking for a crawler that can spider my site and toss the >> results >> into mysql so, in turn, that database can be indexed by Sphinx >> Search. >> >> Since I don't want to reinvent the wheel, is anyone aware of any open >> source projects or code snippets that can already handle this? > > Have a look at http://nikitathespider.com/python/ As the author of Nikita, I can say that (a) she used Postgres and (b) the code wasn't open sourced except for a couple of small parts. The service is now defunct. It wasn't making money. Ideally I'd like to open source the code one day, but it would take a lot of documentation work to make it installable by others, and I won't have the time to do that for the foreseeable future. At the URL provided there's a nice module for parsing robots.txt files (better than the one in the standard library IMHO) but that's about it. FYI, I wrote my spider in Python because I couldn't find a decent one written in Python. There's Nutch, but that's not Python (Java I think). Good luck Philip From joeygartin at gmail.com Fri Apr 10 10:36:24 2009 From: joeygartin at gmail.com (joeygartin) Date: Fri, 10 Apr 2009 07:36:24 -0700 (PDT) Subject: email in separate thread Message-ID: <66f23ffc-345e-4603-9519-cd122a1163b2@j12g2000vbl.googlegroups.com> Greetings, Disclosure: I am a total newbie! I am building an application Django and come to a dilemna when sending off emails. I have an application that sends off an email after a customer has filled out a form, actually it sends off 7 emails. The problem is the page hangs while the emails are sent. My current hosting situation does not give me direct access to an email queue. So I have two options (that I can think of): 1) Write the email information to a file and then run a cron job in the background that routinely checks the file and sends the necessary emails off OR 2) Spin-off a thread (which I have no background with at all!) for the emails and be able to redirect the user to the next page. I have posted this problem on the Django users group, but did not really get a usable response. If any of you have dealt with this or know of an actual solution, please help. Thank you! From brtzsnr at gmail.com Fri Apr 10 10:51:43 2009 From: brtzsnr at gmail.com (=?UTF-8?Q?Alexandru__Mo=C8=99oi?=) Date: Fri, 10 Apr 2009 07:51:43 -0700 (PDT) Subject: how to use logging.config.fileConfig ? Message-ID: Hello, I have a problem using logging the library. I copied and modified the example from PEP 282 [0], [1], [2], [3]. If I use logging.basicConfig() everything works fine. If I use logging.config.fileConfig() my loggers get disabled. I checked logging/ config.py and I found a big comment saying that old loggers are disabled! Why? What am I doing wrong? How do I use logging.config.fileConfig() to avoid this problem ? with logging.config.fileconfig() -------------------- 2009-04-10 17:49:19,955:MyApp:INFO - Starting my app 2009-04-10 17:49:19,955:MyApp:ERROR - There was a problem. Traceback (most recent call last): File "myapp.py", line 10, in mymodule.doIt() File "/home/voodoo/src/mymodule.py", line 7, in doIt raise TypeError, "Bogus type error for testing" TypeError: Bogus type error for testing 2009-04-10 17:49:19,956:MyApp:INFO - Ending my app ---------------------- with logging.basicConfig() ---------------------- INFO:MyApp:Starting my app DEBUG:MyModule:Doin' stuff... ERROR:MyApp:There was a problem. Traceback (most recent call last): File "myapp.py", line 10, in mymodule.doIt() File "/home/voodoo/src/mymodule.py", line 7, in doIt raise TypeError, "Bogus type error for testing" TypeError: Bogus type error for testing INFO:MyApp:Ending my app ---------------------- [0] http://rafb.net/p/nl7b7m19.html [1] http://rafb.net/p/n6KNdU44.html [2] http://rafb.net/p/OxyTga98.html [3] http://www.python.org/dev/peps/pep-0282/ From kw at codebykevin.com Fri Apr 10 10:57:39 2009 From: kw at codebykevin.com (Kevin Walzer) Date: Fri, 10 Apr 2009 10:57:39 -0400 Subject: Adding a Badge to an Icon in Mac OS X In-Reply-To: References: Message-ID: <49DF5E63.1020604@codebykevin.com> bingo wrote: > Hi all, > I have been, now for daysm trying to figure out how to add a badge to > an Icon in Mac OS X. Carbon Icon Services is not documented and I new > to Mac programming. Any help would be greatly appreciated! > > Cheers, Look at PyObjC--there should be methods in there to do it. -- Kevin Walzer Code by Kevin http://www.codebykevin.com From fpm at u.washington.edu Fri Apr 10 11:01:14 2009 From: fpm at u.washington.edu (cassiope) Date: Fri, 10 Apr 2009 08:01:14 -0700 (PDT) Subject: a simple keypress sensor References: Message-ID: <33f99969-0982-40fe-9732-deb242af392b@k19g2000prh.googlegroups.com> On Apr 9, 6:55?pm, Soumen banerjee wrote: > Hello, > I am writing a little program where i have an array the elements of > which i want to display upon keypresses by the user. The only way on > linux seems like using getch() from the curses library. I wanted a > simpler solution than having to use curses for such a simple thing. > any ideas? > Regards > Soumen A more "natural" solution - within Linux - would be to use the termios library functions. Simple, no curses necessary. HTH... -f From philip at semanchuk.com Fri Apr 10 11:09:58 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Fri, 10 Apr 2009 11:09:58 -0400 Subject: Open source web crawler with mysql integration In-Reply-To: <41B9D72305A64C4DA96CE9E632A981F0@office.ipglobal.net> References: <9ed1c9c7-267e-448e-ba83-98db949d97ec@o11g2000yql.googlegroups.com> <3BC408BC-2006-41E6-AB4E-698DA30CE56A@semanchuk.com> <41B9D72305A64C4DA96CE9E632A981F0@office.ipglobal.net> Message-ID: <7E939F69-6D57-402A-AD48-C4A88DC682B2@semanchuk.com> On Apr 10, 2009, at 10:28 AM, Support Desk wrote: > Sounds Interesting. When its done would you care to share it? Hi Michael, The coding has been done (as much as software is ever "done") for a couple of years now. It's mothballed now, sitting on my hard drive. The problem with open sourcing it isn't that the code is incomplete, the problem is that it's insufficiently documented, features a byzantine install procedure and contains a lot of code & assumptions that were relevant to my business but would not be of interest to most people looking to download a general-purpose spider. I'd love to open source it and if someone wants to pay me to make it open source-able, let's talk! But if I have to do it on my own time for free it will be a while (maybe never, although I hope not) before I can make the time. Regards Philip > -----Original Message----- > From: Philip Semanchuk [mailto:philip at semanchuk.com] > Sent: Thursday, April 09, 2009 9:46 PM > To: Python > Subject: Re: Open source web crawler with mysql integration > > > On Apr 9, 2009, at 7:37 PM, Daniel Fetchinson wrote: > >>> I'm looking for a crawler that can spider my site and toss the >>> results >>> into mysql so, in turn, that database can be indexed by Sphinx >>> Search. >>> >>> Since I don't want to reinvent the wheel, is anyone aware of any >>> open >>> source projects or code snippets that can already handle this? >> >> Have a look at http://nikitathespider.com/python/ > > > As the author of Nikita, I can say that (a) she used Postgres and (b) > the code wasn't open sourced except for a couple of small parts. The > service is now defunct. It wasn't making money. Ideally I'd like to > open source the code one day, but it would take a lot of documentation > work to make it installable by others, and I won't have the time to do > that for the foreseeable future. > > At the URL provided there's a nice module for parsing robots.txt files > (better than the one in the standard library IMHO) but that's about > it. > > FYI, I wrote my spider in Python because I couldn't find a decent one > written in Python. There's Nutch, but that's not Python (Java I > think). > > Good luck > Philip > > > From cdalten at gmail.com Fri Apr 10 11:15:29 2009 From: cdalten at gmail.com (grocery_stocker) Date: Fri, 10 Apr 2009 08:15:29 -0700 (PDT) Subject: I can't get RLock to work (warning, the following code is pretty long) References: <8148d3c3-7b97-4fa1-ba99-e0bb0c2b6cbf@k8g2000yqn.googlegroups.com> Message-ID: <2793ca16-64e7-4223-8ea4-213fc635475e@y33g2000prg.googlegroups.com> On Apr 10, 6:48 am, Peter Otten <__pete... at web.de> wrote: > grocery_stocker wrote: > > When I run the following... > > > #!/usr/bin/python > > > import time > > import thread > > import threading > > > def get_first_part(string, lock, sleeptime, *args): > > global counter > > lock.acquire() > > try: > > counter = counter + 1 > > data = counter > > print "%s value is %d" % (string, counter) > > time.sleep(sleeptime) > > finally: > > lock.release() > > return data > > def get_both_parts(string, lock, sleeptime, *args): > > global first, second > > lock.acquire() > > try: > > first = get_first_part() > > second = get_second_part() > > print "%s values are %d and %d" % (string, first, > > second) > > time.sleep(sleeptime) > > finally: > > lock.release() > > return first, second > > How come RLock isn't working in this example? > > When get_both_parts() acquires the lock it invokes get_first_part() which > tries to acquire the lock. This fails because get_both_parts() does not > release the lock until after get_first_part() has finished... > > Peter i thought a Rlock() (vs a Lock()) was suspposed to get around this kind of a problem. From kyosohma at gmail.com Fri Apr 10 11:29:58 2009 From: kyosohma at gmail.com (Mike Driscoll) Date: Fri, 10 Apr 2009 08:29:58 -0700 (PDT) Subject: loop? maybe? References: Message-ID: <1a442b42-593b-489d-83cf-99d584c01c08@r33g2000yqn.googlegroups.com> On Apr 10, 7:42?am, heidi taynton wrote: > Hey guys, > > Sorry for being such a noob with this stuff and the language is hard for me to read through with the online manuals... ?i can do math speak, and science speak... not so much programming/code speak... so when you say pickle... i think food.... > > Anyways, > > I have an array and I want to turn it into a new array. ? where > > nlines = 188 > new_array=n.array(nlines) > > for i in arange(nlines): > > ##### ? and here i want to tell it to read old_array and take ?n ?data points ?where new_n=1.1**old_n ... take the mean of those data points and put them in the first spot in the array.... ? ?then loop through with until the array is full? ? ? ?If it doesn't make sense, i can try to clarify some more... > > Thanks, you guys are life savers, > hi I'm probably missing something, but is this sort of what you are looking for? nlines = 188 new_array = [] # this creates a list for i in range(nlines): new_n = 1.1 ** i mean = (new_n + i) / 2 new_array.append(mean) If you wanted a cumulative mean of the values, then you'll have to change it slightly of course, probably by using a nested loop. Hopefully this isn't a homework question, but even if it is, it's an interesting exercise. Mike From __peter__ at web.de Fri Apr 10 11:39:53 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 10 Apr 2009 17:39:53 +0200 Subject: I can't get RLock to work (warning, the following code is pretty long) References: <8148d3c3-7b97-4fa1-ba99-e0bb0c2b6cbf@k8g2000yqn.googlegroups.com> <2793ca16-64e7-4223-8ea4-213fc635475e@y33g2000prg.googlegroups.com> Message-ID: grocery_stocker wrote: > On Apr 10, 6:48 am, Peter Otten <__pete... at web.de> wrote: >> grocery_stocker wrote: >> > When I run the following... >> >> > #!/usr/bin/python >> >> > import time >> > import thread >> > import threading >> >> > def get_first_part(string, lock, sleeptime, *args): >> > global counter >> > lock.acquire() >> > try: >> > counter = counter + 1 >> > data = counter >> > print "%s value is %d" % (string, counter) >> > time.sleep(sleeptime) >> > finally: >> > lock.release() >> > return data >> > def get_both_parts(string, lock, sleeptime, *args): >> > global first, second >> > lock.acquire() >> > try: >> > first = get_first_part() >> > second = get_second_part() >> > print "%s values are %d and %d" % (string, first, >> > second) >> > time.sleep(sleeptime) >> > finally: >> > lock.release() >> > return first, second >> > How come RLock isn't working in this example? >> >> When get_both_parts() acquires the lock it invokes get_first_part() which >> tries to acquire the lock. This fails because get_both_parts() does not >> release the lock until after get_first_part() has finished... >> >> Peter > > > i thought a Rlock() (vs a Lock()) was suspposed to get around this > kind of a problem. Oops, yes. In get_both_parts() try calling get_first_part/get_second_part() with the three necessary arguments... Peter From cdalten at gmail.com Fri Apr 10 12:03:56 2009 From: cdalten at gmail.com (grocery_stocker) Date: Fri, 10 Apr 2009 09:03:56 -0700 (PDT) Subject: I can't get RLock to work (warning, the following code is pretty long) References: <8148d3c3-7b97-4fa1-ba99-e0bb0c2b6cbf@k8g2000yqn.googlegroups.com> <2793ca16-64e7-4223-8ea4-213fc635475e@y33g2000prg.googlegroups.com> Message-ID: <6c798262-5107-4b56-8241-12ac9a1304d3@n7g2000prc.googlegroups.com> On Apr 10, 8:39 am, Peter Otten <__pete... at web.de> wrote: > grocery_stocker wrote: > > On Apr 10, 6:48 am, Peter Otten <__pete... at web.de> wrote: > >> grocery_stocker wrote: > >> > When I run the following... > > >> > #!/usr/bin/python > > >> > import time > >> > import thread > >> > import threading > > >> > def get_first_part(string, lock, sleeptime, *args): > >> > global counter > >> > lock.acquire() > >> > try: > >> > counter = counter + 1 > >> > data = counter > >> > print "%s value is %d" % (string, counter) > >> > time.sleep(sleeptime) > >> > finally: > >> > lock.release() > >> > return data > >> > def get_both_parts(string, lock, sleeptime, *args): > >> > global first, second > >> > lock.acquire() > >> > try: > >> > first = get_first_part() > >> > second = get_second_part() > >> > print "%s values are %d and %d" % (string, first, > >> > second) > >> > time.sleep(sleeptime) > >> > finally: > >> > lock.release() > >> > return first, second > >> > How come RLock isn't working in this example? > > >> When get_both_parts() acquires the lock it invokes get_first_part() which > >> tries to acquire the lock. This fails because get_both_parts() does not > >> release the lock until after get_first_part() has finished... > > >> Peter > > > i thought a Rlock() (vs a Lock()) was suspposed to get around this > > kind of a problem. > > Oops, yes. In get_both_parts() try calling get_first_part/get_second_part() > with the three necessary arguments... > Doh. I knew that. I really did. From ppearson at nowhere.invalid Fri Apr 10 12:09:29 2009 From: ppearson at nowhere.invalid (Peter Pearson) Date: 10 Apr 2009 16:09:29 GMT Subject: numpy.where References: <148739635947045949601137627602034810513-Webmail@me.com> <747te7F1209a6U2@mid.individual.net> Message-ID: <74999pF12lufhU1@mid.individual.net> On Fri, 10 Apr 2009 02:39:46 -0500, Robert Kern wrote: > On 2009-04-09 22:40, Peter Pearson wrote: >> >> Hey, if you find TFM, please tell me where it is. I haven't >> found anything Fine. I even bought Travis Oliphant's book, >> which helps a little, but . . . > > http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html Thanks. I'll make that my front-line reference for my next foray. -- To email me, substitute nowhere->spamcop, invalid->net. From bedouglas at earthlink.net Fri Apr 10 12:33:40 2009 From: bedouglas at earthlink.net (bruce) Date: Fri, 10 Apr 2009 09:33:40 -0700 Subject: Open source web crawler with mysql integration In-Reply-To: <7E939F69-6D57-402A-AD48-C4A88DC682B2@semanchuk.com> Message-ID: <7b8101c9b9fa$1be1df80$0301a8c0@tmesa.com> phillip... lots of code is opened source "as is"!!! when you get right down to it, a good deal of "open source" code from sourceforge/hotscritps/freshmeat/etc.. is pretty poor, but it is open sourced. you could simply toss your code out into the open source pool, and not be worried about supporting it, or even touching it again... peace -----Original Message----- From: python-list-bounces+bedouglas=earthlink.net at python.org [mailto:python-list-bounces+bedouglas=earthlink.net at python.org]On Behalf Of Philip Semanchuk Sent: Friday, April 10, 2009 8:10 AM To: Python (General) Subject: Re: Open source web crawler with mysql integration On Apr 10, 2009, at 10:28 AM, Support Desk wrote: > Sounds Interesting. When its done would you care to share it? Hi Michael, The coding has been done (as much as software is ever "done") for a couple of years now. It's mothballed now, sitting on my hard drive. The problem with open sourcing it isn't that the code is incomplete, the problem is that it's insufficiently documented, features a byzantine install procedure and contains a lot of code & assumptions that were relevant to my business but would not be of interest to most people looking to download a general-purpose spider. I'd love to open source it and if someone wants to pay me to make it open source-able, let's talk! But if I have to do it on my own time for free it will be a while (maybe never, although I hope not) before I can make the time. Regards Philip > -----Original Message----- > From: Philip Semanchuk [mailto:philip at semanchuk.com] > Sent: Thursday, April 09, 2009 9:46 PM > To: Python > Subject: Re: Open source web crawler with mysql integration > > > On Apr 9, 2009, at 7:37 PM, Daniel Fetchinson wrote: > >>> I'm looking for a crawler that can spider my site and toss the >>> results >>> into mysql so, in turn, that database can be indexed by Sphinx >>> Search. >>> >>> Since I don't want to reinvent the wheel, is anyone aware of any >>> open >>> source projects or code snippets that can already handle this? >> >> Have a look at http://nikitathespider.com/python/ > > > As the author of Nikita, I can say that (a) she used Postgres and (b) > the code wasn't open sourced except for a couple of small parts. The > service is now defunct. It wasn't making money. Ideally I'd like to > open source the code one day, but it would take a lot of documentation > work to make it installable by others, and I won't have the time to do > that for the foreseeable future. > > At the URL provided there's a nice module for parsing robots.txt files > (better than the one in the standard library IMHO) but that's about > it. > > FYI, I wrote my spider in Python because I couldn't find a decent one > written in Python. There's Nutch, but that's not Python (Java I > think). > > Good luck > Philip > > > -- http://mail.python.org/mailman/listinfo/python-list From google at mrabarnett.plus.com Fri Apr 10 13:03:31 2009 From: google at mrabarnett.plus.com (MRAB) Date: Fri, 10 Apr 2009 18:03:31 +0100 Subject: Bug tracker off-line? Message-ID: <49DF7BE3.9010800@mrabarnett.plus.com> Is it just me or is the bug tracker site having problems? I'm not having a problem with other sites. From j.reid at mail.cryst.bbk.ac.uk Fri Apr 10 13:05:24 2009 From: j.reid at mail.cryst.bbk.ac.uk (John Reid) Date: Fri, 10 Apr 2009 18:05:24 +0100 Subject: python crash Message-ID: Python crashes in glibc with the following stack trace. I'm using an interface to R (rpy2), ipython, matplotlib, numpy, and scipy with a wx backend. I'm not sure if the stack trace shows which is the culprit. I've probably misconfigured one of their installs but knowing which one to recompile is a bit of a problem. They all took some time to install so I don't really want to start from scratch again and there's no knowing if that would fix it anyhow. I'm on 64 bit Ubuntu: Linux john-dell 2.6.27-11-generic #1 SMP Wed Apr 1 20:53:41 UTC 2009 x86_64 GNU/Linux Python 2.5.2 Numpy: 1.2.1 Scipy: 0.7.0rc2 IPython: 0.9.1 matplotlib: 0.98.5.2 wx: 2.8.8.0 (gtk2-unicode) I suspect it is something to do with matplotlib as I think it happens when I'm plotting stuff but its not completely reproducible. Also, I'm using the Ubuntu python package but I've built numpy, scipy and matplotlib myself. I didn't think this would be a problem though. Any help appreciated. Thanks, John. *** glibc detected *** /usr/bin/python2.5: free(): invalid pointer: 0x00007f6e499967a0 *** ======= Backtrace: ========= /lib/libc.so.6[0x7f6e6dcf1a58] /lib/libc.so.6(cfree+0x76)[0x7f6e6dcf40a6] /usr/lib/python2.5/lib-dynload/readline.so[0x7f6e6aa7fcd0] /usr/bin/python2.5(PyEval_EvalFrameEx+0x57e2)[0x491052] /usr/bin/python2.5(PyEval_EvalCodeEx+0x6ad)[0x4927cd] /usr/bin/python2.5[0x4dd4c2] /usr/bin/python2.5(PyObject_Call+0x13)[0x418c33] /usr/bin/python2.5[0x41fb08] /usr/bin/python2.5(PyObject_Call+0x13)[0x418c33] /usr/bin/python2.5(PyEval_CallObjectWithKeywords+0x72)[0x48a852] /usr/bin/python2.5(PyInstance_New+0x86)[0x422956] /usr/bin/python2.5(PyObject_Call+0x13)[0x418c33] /usr/bin/python2.5(PyEval_EvalFrameEx+0x3d12)[0x48f582] /usr/bin/python2.5(PyEval_EvalFrameEx+0x6872)[0x4920e2] /usr/bin/python2.5(PyEval_EvalFrameEx+0x6872)[0x4920e2] /usr/bin/python2.5(PyEval_EvalCodeEx+0x6ad)[0x4927cd] /usr/bin/python2.5(PyEval_EvalFrameEx+0x5483)[0x490cf3] /usr/bin/python2.5(PyEval_EvalCodeEx+0x6ad)[0x4927cd] /usr/bin/python2.5[0x4dd4c2] /usr/bin/python2.5(PyObject_Call+0x13)[0x418c33] /usr/bin/python2.5[0x41fb08] /usr/bin/python2.5(PyObject_Call+0x13)[0x418c33] /usr/bin/python2.5(PyEval_CallObjectWithKeywords+0x72)[0x48a852] /usr/bin/python2.5(PyInstance_New+0x86)[0x422956] /usr/bin/python2.5(PyObject_Call+0x13)[0x418c33] /usr/bin/python2.5(PyEval_EvalFrameEx+0x3d12)[0x48f582] /usr/bin/python2.5(PyEval_EvalFrameEx+0x6872)[0x4920e2] /usr/bin/python2.5(PyEval_EvalCodeEx+0x6ad)[0x4927cd] /usr/bin/python2.5(PyEval_EvalCode+0x32)[0x4929c2] /usr/bin/python2.5(PyRun_FileExFlags+0x108)[0x4b2678] /usr/bin/python2.5[0x489948] /usr/bin/python2.5(PyEval_EvalFrameEx+0x57e2)[0x491052] /usr/bin/python2.5(PyEval_EvalCodeEx+0x6ad)[0x4927cd] /usr/bin/python2.5(PyEval_EvalFrameEx+0x4cf8)[0x490568] /usr/bin/python2.5(PyEval_EvalCodeEx+0x6ad)[0x4927cd] /usr/bin/python2.5[0x4dd4c2] /usr/bin/python2.5(PyObject_Call+0x13)[0x418c33] /usr/bin/python2.5[0x41fb08] /usr/bin/python2.5(PyObject_Call+0x13)[0x418c33] /usr/bin/python2.5(PyEval_EvalFrameEx+0x3d12)[0x48f582] /usr/bin/python2.5(PyEval_EvalFrameEx+0x6872)[0x4920e2] /usr/bin/python2.5(PyEval_EvalCodeEx+0x6ad)[0x4927cd] /usr/bin/python2.5[0x4dd4c2] /usr/bin/python2.5(PyObject_Call+0x13)[0x418c33] /usr/bin/python2.5[0x41fb08] /usr/bin/python2.5(PyObject_Call+0x13)[0x418c33] /usr/bin/python2.5(PyEval_CallObjectWithKeywords+0x72)[0x48a852] /usr/lib/python2.5/site-packages/wx-2.8-gtk2-unicode/wx/_core_.so(_ZN12wxPyCallback12EventThunkerER7wxEvent+0x107)[0x7f6e662099d7] /usr/lib/libwx_baseu-2.8.so.0(_ZN12wxEvtHandler21ProcessEventIfMatchesERK21wxEventTableEntryBasePS_R7wxEvent+0x89)[0x7f6e64556ca9] /usr/lib/libwx_baseu-2.8.so.0(_ZN12wxEvtHandler23SearchDynamicEventTableER7wxEvent+0x54)[0x7f6e64556e54] /usr/lib/libwx_baseu-2.8.so.0(_ZN12wxEvtHandler12ProcessEventER7wxEvent+0x92)[0x7f6e64557f42] /usr/lib/libwx_gtk2u_core-2.8.so.0(_ZN11wxTimerBase6NotifyEv+0x66)[0x7f6e64ee51d6] /usr/lib/libwx_gtk2u_core-2.8.so.0[0x7f6e64dec3cb] /usr/lib/libglib-2.0.so.0[0x7f6e623a551b] /usr/lib/libglib-2.0.so.0(g_main_context_dispatch+0x23b)[0x7f6e623a4d5b] /usr/lib/libglib-2.0.so.0[0x7f6e623a852d] /usr/lib/libglib-2.0.so.0(g_main_loop_run+0x1cd)[0x7f6e623a8a5d] /usr/lib/libgtk-x11-2.0.so.0(gtk_main+0xa7)[0x7f6e63fb17a7] /usr/lib/libwx_gtk2u_core-2.8.so.0(_ZN11wxEventLoop3RunEv+0x48)[0x7f6e64de3d18] /usr/lib/libwx_gtk2u_core-2.8.so.0(_ZN9wxAppBase8MainLoopEv+0x4b)[0x7f6e64e6cf4b] /usr/lib/python2.5/site-packages/wx-2.8-gtk2-unicode/wx/_core_.so(_ZN7wxPyApp8MainLoopEv+0x37)[0x7f6e66208cd7] /usr/lib/python2.5/site-packages/wx-2.8-gtk2-unicode/wx/_core_.so[0x7f6e6626707e] /usr/bin/python2.5(PyEval_EvalFrameEx+0x562b)[0x490e9b] ======= Memory map: ======== 00400000-00525000 r-xp 00000000 08:05 4825998 /usr/bin/python2.5 00724000-00725000 r--p 00124000 08:05 4825998 /usr/bin/python2.5 00725000-00757000 rw-p 00125000 08:05 4825998 /usr/bin/python2.5 00757000-0075f000 rw-p 00757000 00:00 0 018e9000-0d5d6000 rw-p 018e9000 00:00 0 [heap] 405bc000-405bd000 ---p 405bc000 00:00 0 405bd000-40dbd000 rwxp 405bd000 00:00 0 40dbd000-40dbf000 rwxp 00000000 00:0e 696 /dev/zero 40dbf000-40dc0000 ---p 40dbf000 00:00 0 40dc0000-415c0000 rwxp 40dc0000 00:00 0 41cb3000-41cb4000 ---p 41cb3000 00:00 0 41cb4000-424b4000 rwxp 41cb4000 00:00 0 7f6e487a6000-7f6e4880b000 r-xp 00000000 08:05 5318973 /usr/local/lib64/R/library/stats/libs/stats.so 7f6e4880b000-7f6e48a0a000 ---p 00065000 08:05 5318973 /usr/local/lib64/R/library/stats/libs/stats.so 7f6e48a0a000-7f6e48a0c000 r--p 00064000 08:05 5318973 /usr/local/lib64/R/library/stats/libs/stats.so 7f6e48a0c000-7f6e48a0e000 rw-p 00066000 08:05 5318973 /usr/local/lib64/R/library/stats/libs/stats.so 7f6e48a0e000-7f6e48a2c000 r-xp 00000000 08:05 5441873 /usr/local/lib64/R/library/grDevices/libs/grDevices.so 7f6e48a2c000-7f6e48c2b000 ---p 0001e000 08:05 5441873 /usr/local/lib64/R/library/grDevices/libs/grDevices.so 7f6e48c2b000-7f6e48c2c000 r--p 0001d000 08:05 5441873 /usr/local/lib64/R/library/grDevices/libs/grDevices.so 7f6e48c2c000-7f6e48c2d000 rw-p 0001e000 08:05 5441873 /usr/local/lib64/R/library/grDevices/libs/grDevices.so 7f6e48c2d000-7f6e48c33000 r-xp 00000000 08:05 5400895 /usr/local/lib64/R/library/methods/libs/methods.so 7f6e48c33000-7f6e48e33000 ---p 00006000 08:05 5400895 /usr/local/lib64/R/library/methods/libs/methods.so 7f6e48e33000-7f6e48e34000 r--p 00006000 08:05 5400895 /usr/local/lib64/R/library/methods/libs/methods.so 7f6e48e34000-7f6e48e35000 rw-p 00007000 08:05 5400895 /usr/local/lib64/R/library/methods/libs/methods.so 7f6e48e35000-7f6e48e63000 r-xp 00000000 08:05 5145219 /usr/local/lib64/R/lib/libRblas.so 7f6e48e63000-7f6e49062000 ---p 0002e000 08:05 5145219 /usr/local/lib64/R/lib/libRblas.so 7f6e49062000-7f6e49063000 r--p 0002d000 08:05 5145219 /usr/local/lib64/R/lib/libRblas.so 7f6e49063000-7f6e49064000 rw-p 0002e000 08:05 5145219 /usr/local/lib64/R/lib/libRblas.so 7f6e49064000-7f6e491df000 r-xp 00000000 08:05 5145229 /usr/local/lib64/R/lib/libRlapack.so 7f6e491df000-7f6e493df000 ---p 0017b000 08:05 5145229 /usr/local/lib64/R/lib/libRlapack.so 7f6e493df000-7f6e493e0000 r--p 0017b000 08:05 5145229 /usr/local/lib64/R/lib/libRlapack.so 7f6e493e0000-7f6e493e1000 rw-p 0017c000 08:05 5145229 /usr/local/lib64/R/lib/libRlapack.so 7f6e493e1000-7f6e4945d000 rw-p 7f6e493e1000 00:00 0 7f6e4945d000-7f6e496ea000 r-xp 00000000 08:05 5145224 /usr/local/lib64/R/lib/libR.so 7f6e496ea000-7f6e498ea000 ---p 0028d000 08:05 5145224 /usr/local/lib64/R/lib/libR.so 7f6e498ea000-7f6e498ef000 r--p 0028d000 08:05 5145224 /usr/local/lib64/R/lib/libR.so 7f6e498ef000-7f6e498ff000 rw-p 00292000 08:05 5145224 /usr/local/lib64/R/lib/libR.so 7f6e498ff000-7f6e49998000 rw-p 7f6e498ff000 00:00 0 7f6e49998000-7f6e499a4000 r-xp 00000000 08:05 4909357 /usr/lib/python2.5/site-packages/_rpy2081.so 7f6e499a4000-7f6e49ba3000 ---p 0000c000 08:05 4909357 /usr/lib/python2.5/site-packages/_rpy2081.so 7f6e49ba3000-7f6e49ba4000 r--p 0000b000 08:05 4909357 /usr/lib/python2.5/site-packages/_rpy2081.so 7f6e49ba4000-7f6e49ba5000 rw-p 0000c000 08:05 4909357 /usr/lib/python2.5/site-packages/_rpy2081.so 7f6e49ba5000-7f6e49dae000 rw-p 7f6e49ba5000 00:00 0 7f6e49dae000-7f6e49db1000 r-xp 00000000 08:05 2900033 /lib/libgpg-error.so.0.3.0 7f6e49db1000-7f6e49fb0000 ---p 00003000 08:05 2900033 /lib/libgpg-error.so.0.3.0 7f6e49fb0000-7f6e49fb1000 rw-p 00002000 08:05 2900033 /lib/libgpg-error.so.0.3.0 7f6e49fb1000-7f6e4a016000 r-xp 00000000 08:05 2900031 /lib/libgcrypt.so.11.4.4 7f6e4a016000-7f6e4a215000 ---p 00065000 08:05 2900031 /lib/libgcrypt.so.11.4.4 7f6e4a215000-7f6e4a216000 r--p 00064000 08:05 2900031 /lib/libgcrypt.so.11.4.4 7f6e4a216000-7f6e4a218000 rw-p 00065000 08:05 2900031 /lib/libgcrypt.so.11.4.4 7f6e4a218000-7f6e4a228000 r-xp 00000000 08:05 4827546 /usr/lib/libtasn1.so.3.0.15 7f6e4a228000-7f6e4a427000 ---p 00010000 08:05 4827546 /usr/lib/libtasn1.so.3.0.15 7f6e4a427000-7f6e4a429000 rw-p 0000f000 08:05 4827546 Aborted From jlagarde at dcscorp.com Fri Apr 10 13:08:13 2009 From: jlagarde at dcscorp.com (Lagarde, Jean) Date: Fri, 10 Apr 2009 13:08:13 -0400 Subject: Crash on msvcrt mismatch? Message-ID: I don't have much experience writing extension modules, and am looking for pointers to resolve python crashes on Windows. I've isolated the problem to a python file with the following two lines: ---- import PyQt4.Qwt5 import C13 ---- That causes Python to crash. If I comment out either one of the two lines, it doesn't. C13 is an extension module wrapping the API for an instrumentation card. I've written test code that uses that C13 module without problems, but that code doesn't use PyQt. My Python and PyQt come from the Python(x,y) binary distribution (version 2.1.11). C13 was compiled using mingw and used msvcr71 by default. I noticed that although Python depends on msvcr71 as well, the PyQt modules depend on msvcrt (QtCore4.dll and Qtw.pyd for example). I tried to use msvcrt for C13 as well, but got the same result. I'm looking for ideas as to what to try next. I'd like to avoid having to build PyQt from source, but if that's the recommendation I'll give it a go. Cheers, Jean From pecora at anvil.nrl.navy.mil Fri Apr 10 13:10:23 2009 From: pecora at anvil.nrl.navy.mil (Lou Pecora) Date: Fri, 10 Apr 2009 13:10:23 -0400 Subject: numpy.where References: <148739635947045949601137627602034810513-Webmail@me.com> <747te7F1209a6U2@mid.individual.net> Message-ID: In article <747te7F1209a6U2 at mid.individual.net>, Peter Pearson wrote: > On Thu, 09 Apr 2009 09:09:18 -0400, Lou Pecora wrote: > > > > Really, I've gotta RTFM. :-) > > Hey, if you find TFM, please tell me where it is. I haven't > found anything Fine. I even bought Travis Oliphant's book, > which helps a little, but . . . Travis' book is what I have and what I was thinking of when I made the statement. -- -- Lou Pecora From pecora at anvil.nrl.navy.mil Fri Apr 10 13:18:09 2009 From: pecora at anvil.nrl.navy.mil (Lou Pecora) Date: Fri, 10 Apr 2009 13:18:09 -0400 Subject: numpy.where References: <148739635947045949601137627602034810513-Webmail@me.com> <747te7F1209a6U2@mid.individual.net> Message-ID: In article , Robert Kern wrote: > http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html That helps, thanks. So I can RTFWP, too. :-) -- -- Lou Pecora From aahz at pythoncraft.com Fri Apr 10 13:30:03 2009 From: aahz at pythoncraft.com (Aahz) Date: 10 Apr 2009 10:30:03 -0700 Subject: Bug tracker off-line? References: Message-ID: In article , MRAB wrote: > >Is it just me or is the bug tracker site having problems? I'm not having >a problem with other sites. Someone else reported this on python-dev -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ Why is this newsgroup different from all other newsgroups? From norseman at hughes.net Fri Apr 10 13:33:49 2009 From: norseman at hughes.net (norseman) Date: Fri, 10 Apr 2009 10:33:49 -0700 Subject: Running an interactive subprocess with Popen In-Reply-To: <89471f45-fa0a-4dde-943e-32cf25a1794d@y13g2000yqn.googlegroups.com> References: <89471f45-fa0a-4dde-943e-32cf25a1794d@y13g2000yqn.googlegroups.com> Message-ID: <49DF82FD.3000300@hughes.net> David Liang wrote: > Hello, > Sorry for the newbie question. How do I run a program that could > block, waiting for user input, using subprocess.Popen? For example, > > from subprocess import * > > def foo(): > a = Popen(['python'] ...) > > I want to be able to get input from the user and send input to the > subprocess, printing stdout and stderr as data becomes available, then > return once the subprocess exits. Is it possible to send to the > subprocess keyboard interrupts, EOF, and such? > > I tried doing stdout=PIPE, stderr=PIPE, stdin=PIPE. I tried using > communicate(), but could only call it once; subsequent calls raised > "ValueError: I/O operation on closed file." > And both a.stdout.read() and a.stderr.read() blocked the program. Any > help would be much appreciated. > > -David > -- > http://mail.python.org/mailman/listinfo/python-list > ============================================ David; Take a look at Section 14.1.2 of the Python Library pdf reference. The popen3 should interest you. It allows 2 way communication with a 'child' process. I was going to include some samples, but it seems I took them off when that project terminated. (So they are buried somewhere in storage - sorry.) I'm running Ver. 2.5.2 for mass compatibility. Note: I never tried redirection of the mouse. Just the keyboard, which worked very well for me. In my case "Parent" controlled conversation. In essence, treating 'child' as a subroutine. 'Child' included Unix scripts, Unix programs, and some third party programs. Try to have your "parent" program NOT talk to the screen while in this mode. :) Steve From jlagarde at dcscorp.com Fri Apr 10 13:41:03 2009 From: jlagarde at dcscorp.com (Lagarde, Jean) Date: Fri, 10 Apr 2009 13:41:03 -0400 Subject: Crash on msvcrt mismatch? In-Reply-To: References: Message-ID: I'm curious to understand why that matters, but I found out that if I load the modules in the inverse order, everything works fine. -- Jean ________________________________ From: Lagarde, Jean Sent: Friday, April 10, 2009 10:08 AM To: python-list at python.org Subject: Crash on msvcrt mismatch? I don't have much experience writing extension modules, and am looking for pointers to resolve python crashes on Windows. I've isolated the problem to a python file with the following two lines: ---- import PyQt4.Qwt5 import C13 ---- That causes Python to crash. If I comment out either one of the two lines, it doesn't. C13 is an extension module wrapping the API for an instrumentation card. I've written test code that uses that C13 module without problems, but that code doesn't use PyQt. My Python and PyQt come from the Python(x,y) binary distribution (version 2.1.11). C13 was compiled using mingw and used msvcr71 by default. I noticed that although Python depends on msvcr71 as well, the PyQt modules depend on msvcrt (QtCore4.dll and Qtw.pyd for example). I tried to use msvcrt for C13 as well, but got the same result. I'm looking for ideas as to what to try next. I'd like to avoid having to build PyQt from source, but if that's the recommendation I'll give it a go. Cheers, Jean From seham_yam at yahoo.com Fri Apr 10 13:44:38 2009 From: seham_yam at yahoo.com (seham salem) Date: Fri, 10 Apr 2009 10:44:38 -0700 (PDT) Subject: knowing mobile ip Message-ID: <682556.99601.qm@web58301.mail.re3.yahoo.com> how can i know my mobile ip by bython code or something else i found a code but it does not work with my mobile. Nokia N73,E56>>> thanks From aahz at pythoncraft.com Fri Apr 10 13:47:04 2009 From: aahz at pythoncraft.com (Aahz) Date: 10 Apr 2009 10:47:04 -0700 Subject: Crash on msvcrt mismatch? References: Message-ID: In article , Lagarde, Jean wrote: > >I'm curious to understand why that matters, but I found out that if I load = >the modules in the inverse order, everything works fine. There's probably a refcount bug hiding in your code. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ Why is this newsgroup different from all other newsgroups? From norseman at hughes.net Fri Apr 10 13:50:57 2009 From: norseman at hughes.net (norseman) Date: Fri, 10 Apr 2009 10:50:57 -0700 Subject: zProblem Message-ID: <49DF8701.5010006@hughes.net> Moderator: I apologize for attaching anything. However, in order to explain my problem, I need to control the font since text graphics only work for a given font. It's as small as I can get it. Steve norseman at hughes.net -------------- next part -------------- A non-text attachment was scrubbed... Name: zProblem.pdf Type: application/pdf Size: 3532 bytes Desc: not available URL: From stuart.davenport at gmail.com Fri Apr 10 14:59:35 2009 From: stuart.davenport at gmail.com (Stuart Davenport) Date: Fri, 10 Apr 2009 11:59:35 -0700 (PDT) Subject: How to create a virtual serial port? Message-ID: Hi, I am trying to work out if its possible, to create a virtual serial port with Python? Would anyone know how to go about this in code? Any help would be greatly appreciated! :) I have a had a google and the topics returned only seem to reflect "reading" serial port data, particularly pySerial results... Cheers Stu From Scott.Daniels at Acm.Org Fri Apr 10 15:45:28 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 10 Apr 2009 12:45:28 -0700 Subject: How to create a virtual serial port? In-Reply-To: References: Message-ID: <_cCdndEvsrp8PULUnZ2dnUVZ_qydnZ2d@pdx.net> Stuart Davenport wrote: > Hi, > > I am trying to work out if its possible, to create a virtual serial > port with Python? Would anyone know how to go about this in code? Any > help would be greatly appreciated! :) > > I have a had a google and the topics returned only seem to reflect > "reading" serial port data, particularly pySerial results... > > Cheers > Stu Well, search for Smart Questions. I have no idea what your OS is, nor what level of "virtual serial port" you want/need. All anyone could do with your current request is guess what you need and try to steer you towards it. --Scott David Daniels Scott.Daniels at Acm.Org From invalid at invalid Fri Apr 10 16:10:56 2009 From: invalid at invalid (Grant Edwards) Date: Fri, 10 Apr 2009 15:10:56 -0500 Subject: How to create a virtual serial port? References: Message-ID: On 2009-04-10, Stuart Davenport wrote: > I am trying to work out if its possible, to create a virtual serial > port with Python? On Linux: no. On other OSes, I don't know. -- Grant Edwards grante Yow! I'm encased in the at lining of a pure pork visi.com sausage!! From d.a.abernathy at gmail.com Fri Apr 10 16:25:25 2009 From: d.a.abernathy at gmail.com (dj) Date: Fri, 10 Apr 2009 13:25:25 -0700 (PDT) Subject: ValueError: I/O operation on closed file Message-ID: <230a2c21-c8f1-4c58-a27e-89be8477bfe7@v19g2000yqn.googlegroups.com> I have a handler which I use with a set of log levels for the python logging module. ----------------------------------- myhandler.py -------------------------------------------------------- import logging.handlers # create my handler class class MyHandler(logging.handlers.RotatingFileHandler): def __init__(self, fn): logging.handlers.RotatingFileHandler.__init__(self, fn, maxBytes=10485760, backupCount=5) # Register handler in the "logging.handlers" namespace logging.handlers.MyHandler = MyHandler -------------------------------------------------------------------------------------------------------------------- Using it, repeatedly generates this error: Traceback (most recent call last): File "C:\python26\lib\logging\handlers.py", line 74, in emit if self.shouldRollover(record): File "C:\python26\lib\logging\handlers.py", line 146, in shouldRollover self.stream.seek(0, 2) #due to non-posix-compliant Windows feature ValueError: I/O operation on closed file I am completely stumped has to what could be the issue. Any help would be greatly appreciated. From skip at pobox.com Fri Apr 10 16:27:17 2009 From: skip at pobox.com (skip at pobox.com) Date: Fri, 10 Apr 2009 15:27:17 -0500 (CDT) Subject: Python 2.6/3.0 packages for Ubuntu? Message-ID: <20090410202717.544BEFCEB19@montanaro.dyndns.org> Does Ubuntu really not have Python 2.6 or 3.0 packages or do I just have my package list misconfigured? I'm setting up a fresh machine and am not too Ubuntu-aware. Is there a list of package repositories around somewhere? Thx, -- Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/ "XML sucks, dictionaries rock" - Dave Beazley From banibrata.dutta at gmail.com Fri Apr 10 16:48:28 2009 From: banibrata.dutta at gmail.com (Banibrata Dutta) Date: Sat, 11 Apr 2009 02:18:28 +0530 Subject: How to create a virtual serial port? In-Reply-To: References: Message-ID: <3de8e1f70904101348i6d890dc0ifc76511fe1190294@mail.gmail.com> As others have already stated, "Virtual Serial Port" isn't descriptive enough. There are various forms of "Virtual Serial Ports" and different people use the term to mean different things. For example, emulating RS232 communication over Bluetooth, or USB, is also something done using "Virtual Serial Port". I'd expect however, this to be a driver, not typically not a user-land application. On Sat, Apr 11, 2009 at 12:29 AM, Stuart Davenport < stuart.davenport at gmail.com> wrote: > Hi, > > I am trying to work out if its possible, to create a virtual serial > port with Python? Would anyone know how to go about this in code? Any > help would be greatly appreciated! :) > > I have a had a google and the topics returned only seem to reflect > "reading" serial port data, particularly pySerial results... > > Cheers > Stu > -- > http://mail.python.org/mailman/listinfo/python-list > -- regards, Banibrata http://www.linkedin.com/in/bdutta -------------- next part -------------- An HTML attachment was scrubbed... URL: From benjamin.kaplan at case.edu Fri Apr 10 17:05:45 2009 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Fri, 10 Apr 2009 17:05:45 -0400 Subject: Python 2.6/3.0 packages for Ubuntu? In-Reply-To: <20090410202717.544BEFCEB19@montanaro.dyndns.org> References: <20090410202717.544BEFCEB19@montanaro.dyndns.org> Message-ID: On Fri, Apr 10, 2009 at 4:27 PM, wrote: > Does Ubuntu really not have Python 2.6 or 3.0 packages or do I just have my > package list misconfigured? I'm setting up a fresh machine and am not too > Ubuntu-aware. Is there a list of package repositories around somewhere? > > Thx, > Which version of Ubuntu are you using? Hardy: 2.5.2 Intrepid: 2.5.2 and 3.0rc1 Jaunty (beta) : 2.6.2rc1 and 3.0.1 > -- > Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/ > "XML sucks, dictionaries rock" - Dave Beazley > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From cmh.python at gmail.com Fri Apr 10 17:07:38 2009 From: cmh.python at gmail.com (Mike H) Date: Fri, 10 Apr 2009 17:07:38 -0400 Subject: Unsupported operand types in if/else list comprehension Message-ID: Hello all, I have a question about the if/else aspect of list comprehension: I would like to go through a list and place quotes around an item if it is a string, and keep the item the same if it's anything else: e.g.['a',9,'8b'] --> ['"a"', 9, '"8b"'] I understand that if/else list comprehension should be generally: b=[(F,T)[boolean test] for val in X] so, I tried the following code: a=['test',1,'two'] b=[(inst, '"'+inst+'"')[isinstance(inst, str)] for inst in a] I end up getting the error: unsupported operand type(s) for +: 'int' and 'str' >From playing around with other examples, I get the feeling that Python is calculating both values (inst and '"'+inst+'"') before selecting which one to pass to the new list. Am I right? Is there any way I can do this using list comprehension? Thanks in advance, Michael From gherron at islandtraining.com Fri Apr 10 17:18:40 2009 From: gherron at islandtraining.com (Gary Herron) Date: Fri, 10 Apr 2009 14:18:40 -0700 Subject: Unsupported operand types in if/else list comprehension In-Reply-To: References: Message-ID: <49DFB7B0.5060507@islandtraining.com> Mike H wrote: > Hello all, I have a question about the if/else aspect of list comprehension: > > I would like to go through a list and place quotes around an item if > it is a string, and keep the item the same if it's anything else: > > e.g.['a',9,'8b'] --> ['"a"', 9, '"8b"'] > > I understand that if/else list comprehension should be generally: > > b=[(F,T)[boolean test] for val in X] > > so, I tried the following code: > > a=['test',1,'two'] > b=[(inst, '"'+inst+'"')[isinstance(inst, str)] for inst in a] > > I end up getting the error: unsupported operand type(s) for +: 'int' and 'str' > > >From playing around with other examples, I get the feeling that Python > is calculating both values (inst and '"'+inst+'"') before selecting > which one to pass to the new list. Am I right? Is there any way I can > do this using list comprehension? > > Thanks in advance, > > Michael > -- > http://mail.python.org/mailman/listinfo/python-list > While I think your goal is of questionable value, this will do it: b = [('"'+inst+'"' if isinstance(inst, str) else inst) for inst in a] The parentheses around the conditional are unnecessary, but help with readability I think. Better yet: def QuoteIfString(inst): ... b = [QuoteIfString(inst) for inst in a] would be much more readable, and you could document *why* you are doing this in the def of QuoteIfString Gary Herron From gallium.arsenide at gmail.com Fri Apr 10 17:18:53 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Fri, 10 Apr 2009 14:18:53 -0700 (PDT) Subject: Unsupported operand types in if/else list comprehension References: Message-ID: On Apr 10, 5:07?pm, Mike H wrote: > From playing around with other examples, I get the feeling > that Python is calculating both values (inst and '"'+inst+'"') > before selecting which one to pass to the new list. Am I right? I believe so. (I'm sure the experts here will tell you more definitively.) > Is there any way I can do this using list comprehension? Yes. If you are using 2.5 or later, you can do this: 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. >>> a=['test',1,'two'] >>> b=[(x if not isinstance(x, str) else '"'+x+'"') for x in a] >>> b ['"test"', 1, '"two"'] >>> If you are trying to make a CSV file, then even better may be to use the csv module. John From b.mahdi at gmail.com Fri Apr 10 17:22:35 2009 From: b.mahdi at gmail.com (bingo) Date: Fri, 10 Apr 2009 14:22:35 -0700 (PDT) Subject: Adding a Badge to an Icon in Mac OS X References: <49DF5E63.1020604@codebykevin.com> Message-ID: <0beb8fe4-c658-46a7-bfd4-6fbdac921be5@v23g2000pro.googlegroups.com> Kevin, PyObjc seems to offer the option to add badges to icons in the doc. I need to add badges to any icon... kinda like SCPlugin and dropbox do. I think that SCPlugin is doing it through carbon Icon Services. But I am still trying to figure out how it is done! Thanks, Madi On Apr 10, 4:57?am, Kevin Walzer wrote: > bingo wrote: > > Hi all, > > I have been, now for daysm trying to figure out how to add a badge to > > an Icon in Mac OS X. Carbon Icon Services is not documented and I new > > to Mac programming. Any help would be greatly appreciated! > > > Cheers, > > Look at PyObjC--there should be methods in there to do it. > > -- > Kevin Walzer > Code by Kevinhttp://www.codebykevin.com From google at mrabarnett.plus.com Fri Apr 10 17:23:25 2009 From: google at mrabarnett.plus.com (MRAB) Date: Fri, 10 Apr 2009 22:23:25 +0100 Subject: Unsupported operand types in if/else list comprehension In-Reply-To: References: Message-ID: <49DFB8CD.2020100@mrabarnett.plus.com> Mike H wrote: > Hello all, I have a question about the if/else aspect of list comprehension: > > I would like to go through a list and place quotes around an item if > it is a string, and keep the item the same if it's anything else: > > e.g.['a',9,'8b'] --> ['"a"', 9, '"8b"'] > > I understand that if/else list comprehension should be generally: > > b=[(F,T)[boolean test] for val in X] > > so, I tried the following code: > > a=['test',1,'two'] > b=[(inst, '"'+inst+'"')[isinstance(inst, str)] for inst in a] > > I end up getting the error: unsupported operand type(s) for +: 'int' and 'str' > >>From playing around with other examples, I get the feeling that Python > is calculating both values (inst and '"'+inst+'"') before selecting > which one to pass to the new list. Am I right? Is there any way I can > do this using list comprehension? > Yes, and yes: b=[(inst, '"%s"' % inst)[isinstance(inst, str)] for inst in a] From cmh.python at gmail.com Fri Apr 10 17:26:41 2009 From: cmh.python at gmail.com (Mike H) Date: Fri, 10 Apr 2009 17:26:41 -0400 Subject: Unsupported operand types in if/else list comprehension In-Reply-To: References: Message-ID: Thanks to all of you. FYI, I'm doing this because I'm working on creating some insert statements in SQL, where string values need to be quoted, and integer values need to be unquoted. I wanted to be sure that I could pass these values to the list in a normal way e.g. ['test', 1, 'two'] and have a function correct the list for me, rather than calling the function with a strangely quoted list e.g. ['"'test'"', 1, '"'two'"']. Again, thanks. On Fri, Apr 10, 2009 at 5:18 PM, John Yeung wrote: > On Apr 10, 5:07?pm, Mike H wrote: >> From playing around with other examples, I get the feeling >> that Python is calculating both values (inst and '"'+inst+'"') >> before selecting which one to pass to the new list. Am I right? > > I believe so. ?(I'm sure the experts here will tell you more > definitively.) > >> Is there any way I can do this using list comprehension? > > Yes. ?If you are using 2.5 or later, you can do this: > > 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. >>>> a=['test',1,'two'] >>>> b=[(x if not isinstance(x, str) else '"'+x+'"') for x in a] >>>> b > ['"test"', 1, '"two"'] >>>> > > If you are trying to make a CSV file, then even better may be to use > the csv module. > > John > -- > http://mail.python.org/mailman/listinfo/python-list > From gagsl-py2 at yahoo.com.ar Fri Apr 10 17:30:22 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 10 Apr 2009 18:30:22 -0300 Subject: zProblem References: <49DF8701.5010006@hughes.net> Message-ID: En Fri, 10 Apr 2009 14:50:57 -0300, norseman escribi?: > However, in order to explain my problem, I need to control the font > since text graphics only work for a given font. It's as small as I can > get it. I didn't understand what your problem is actually. -- Gabriel Genellina From benjamin.kaplan at case.edu Fri Apr 10 17:35:27 2009 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Fri, 10 Apr 2009 17:35:27 -0400 Subject: Python 2.6/3.0 packages for Ubuntu? In-Reply-To: <18911.46832.558089.553835@montanaro.dyndns.org> References: <20090410202717.544BEFCEB19@montanaro.dyndns.org> <18911.46832.558089.553835@montanaro.dyndns.org> Message-ID: On Fri, Apr 10, 2009 at 5:15 PM, wrote: > > >> Does Ubuntu really not have Python 2.6 or 3.0 packages.... > > Benjamin> Which version of Ubuntu are you using? > > Benjamin> Hardy: 2.5.2 > Benjamin> Intrepid: 2.5.2 and 3.0rc1 > Benjamin> Jaunty (beta) : 2.6.2rc1 and 3.0.1 > > I'm using Intrepid. Is there a simple way within Synaptic or by editing > config files to jump up to Jaunty? > You can either wait 13 days for the final release, or run "sudo update-manager -d" if you don't mind using the beta. You'll get a notificaiton in the update manager that a new distribution is available. The dist upgrades are pretty big (about 1GB) so make sure you have a good internet connection and/or plenty of time before you do it. > Thanks, > > -- > Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/ > "XML sucks, dictionaries rock" - Dave Beazley > -------------- next part -------------- An HTML attachment was scrubbed... URL: From stuart.davenport at gmail.com Fri Apr 10 17:47:27 2009 From: stuart.davenport at gmail.com (Stuart Davenport) Date: Fri, 10 Apr 2009 14:47:27 -0700 (PDT) Subject: How to create a virtual serial port? References: <_cCdndEvsrp8PULUnZ2dnUVZ_qydnZ2d@pdx.net> Message-ID: On 10 Apr, 20:45, Scott David Daniels wrote: > Stuart Davenport wrote: > > Hi, > > > I am trying to work out if its possible, to create a virtual serial > > port with Python? Would anyone know how to go about this in code? Any > > help would be greatly appreciated! :) > > > I have a had a google and the topics returned only seem to reflect > > "reading" serial port data, particularly pySerial results... > > > Cheers > > Stu > > Well, search for Smart Questions. ?I have no idea what your OS is, nor > what level of "virtual serial port" you want/need. ?All anyone could > do with your current request is guess what you need and try to steer you > towards it. > > --Scott David Daniels > Scott.Dani... at Acm.Org Scott, totally fair point - a little candid, but fair... I'm on a OS X, python 2.5. Basically I will have a remote application pushing data (GPS) over the network to a python application I have running on my Mac, I want this python application to again push the data on to a "virtual serial port". Then the GPS program I have running on my MAC, RouteBuddy, can read the data from that serial port as standard. The only part I am concerned about here though, is if I can create a serial port virtually and push data out of it? Hope that defines a little more clarity for yourself. From paul.hermeneutic at gmail.com Fri Apr 10 19:16:25 2009 From: paul.hermeneutic at gmail.com (Paul Watson) Date: Fri, 10 Apr 2009 18:16:25 -0500 Subject: llvm vs. parrot Message-ID: <1239405385.6357.1.camel@linux-3eb6.site> Is Parrot out of favor these days? It appears that Google is going to use llvm. http://code.google.com/p/unladen-swallow/ From philip at semanchuk.com Fri Apr 10 19:21:53 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Fri, 10 Apr 2009 19:21:53 -0400 Subject: Open source web crawler with mysql integration In-Reply-To: <7b8101c9b9fa$1be1df80$0301a8c0@tmesa.com> References: <7b8101c9b9fa$1be1df80$0301a8c0@tmesa.com> Message-ID: <6A876E4D-D095-4345-94FD-6D86338CB386@semanchuk.com> On Apr 10, 2009, at 12:33 PM, bruce wrote: > phillip... > > lots of code is opened source "as is"!!! > > when you get right down to it, a good deal of "open source" code from > sourceforge/hotscritps/freshmeat/etc.. is pretty poor, but it is open > sourced. > > you could simply toss your code out into the open source pool, and > not be > worried about supporting it, or even touching it again... You're right, and I like your enthusiasm. But I don't want to invite people to use my code if it's just going to be frustrating to 90% of them. It's bad for my reputation. And for the reputation of open source in general, but I'm more concerned about me. ;) > -----Original Message----- > From: python-list-bounces+bedouglas=earthlink.net at python.org > [mailto:python-list-bounces+bedouglas=earthlink.net at python.org]On > Behalf > Of Philip Semanchuk > Sent: Friday, April 10, 2009 8:10 AM > To: Python (General) > Subject: Re: Open source web crawler with mysql integration > > > > On Apr 10, 2009, at 10:28 AM, Support Desk wrote: > >> Sounds Interesting. When its done would you care to share it? > > Hi Michael, > The coding has been done (as much as software is ever "done") for a > couple of years now. It's mothballed now, sitting on my hard drive. > The problem with open sourcing it isn't that the code is incomplete, > the problem is that it's insufficiently documented, features a > byzantine install procedure and contains a lot of code & assumptions > that were relevant to my business but would not be of interest to most > people looking to download a general-purpose spider. I'd love to open > source it and if someone wants to pay me to make it open source-able, > let's talk! But if I have to do it on my own time for free it will be > a while (maybe never, although I hope not) before I can make the time. > > Regards > Philip > > > > >> -----Original Message----- >> From: Philip Semanchuk [mailto:philip at semanchuk.com] >> Sent: Thursday, April 09, 2009 9:46 PM >> To: Python >> Subject: Re: Open source web crawler with mysql integration >> >> >> On Apr 9, 2009, at 7:37 PM, Daniel Fetchinson wrote: >> >>>> I'm looking for a crawler that can spider my site and toss the >>>> results >>>> into mysql so, in turn, that database can be indexed by Sphinx >>>> Search. >>>> >>>> Since I don't want to reinvent the wheel, is anyone aware of any >>>> open >>>> source projects or code snippets that can already handle this? >>> >>> Have a look at http://nikitathespider.com/python/ >> >> >> As the author of Nikita, I can say that (a) she used Postgres and (b) >> the code wasn't open sourced except for a couple of small parts. The >> service is now defunct. It wasn't making money. Ideally I'd like to >> open source the code one day, but it would take a lot of >> documentation >> work to make it installable by others, and I won't have the time to >> do >> that for the foreseeable future. >> >> At the URL provided there's a nice module for parsing robots.txt >> files >> (better than the one in the standard library IMHO) but that's about >> it. >> >> FYI, I wrote my spider in Python because I couldn't find a decent one >> written in Python. There's Nutch, but that's not Python (Java I >> think). >> >> Good luck >> Philip >> >> >> > > -- > http://mail.python.org/mailman/listinfo/python-list > From norseman at hughes.net Fri Apr 10 19:38:15 2009 From: norseman at hughes.net (norseman) Date: Fri, 10 Apr 2009 16:38:15 -0700 Subject: zProblem In-Reply-To: References: <49DF8701.5010006@hughes.net> Message-ID: <49DFD867.8090409@hughes.net> Gabriel Genellina wrote: > En Fri, 10 Apr 2009 14:50:57 -0300, norseman > escribi?: > >> However, in order to explain my problem, I need to control the font >> since text graphics only work for a given font. It's as small as I can >> get it. > > I didn't understand what your problem is actually. > ================================ UUHHmmmmmmmm Did you read the pdf? From semanticist at gmail.com Fri Apr 10 19:53:16 2009 From: semanticist at gmail.com (Miles) Date: Fri, 10 Apr 2009 19:53:16 -0400 Subject: Unsupported operand types in if/else list comprehension In-Reply-To: References: Message-ID: On Fri, Apr 10, 2009 at 5:26 PM, Mike H wrote: > Thanks to all of you. > > FYI, I'm doing this because I'm working on creating some insert > statements in SQL, where string values need to be quoted, and integer > values need to be unquoted. This is what you should have posted in the first place. Your solution is entirely the wrong one, because it will break if your input strings contain the quote character (and suffers from other issues as well)--this is where SQL injection vulnerabilities come from. The safe and correct way is to allow your database driver to insert the parameters into the SQL query for you; it will look something like this (though the exact details will vary depending on what module you're using): cursor.execute('INSERT INTO my_table VALUES (?, ?, ?)', ['test',1,'two']) -Miles From semanticist at gmail.com Fri Apr 10 19:56:15 2009 From: semanticist at gmail.com (Miles) Date: Fri, 10 Apr 2009 19:56:15 -0400 Subject: Adding a Badge to an Icon in Mac OS X In-Reply-To: <0beb8fe4-c658-46a7-bfd4-6fbdac921be5@v23g2000pro.googlegroups.com> References: <49DF5E63.1020604@codebykevin.com> <0beb8fe4-c658-46a7-bfd4-6fbdac921be5@v23g2000pro.googlegroups.com> Message-ID: On Fri, Apr 10, 2009 at 5:22 PM, bingo wrote: > PyObjc seems to offer the option to add badges to icons in the doc. I > need to add badges to any icon... kinda like SCPlugin and dropbox do. > I think that SCPlugin is doing it through carbon Icon Services. But I > am still trying to figure out how it is done! I believe those programs are able to do so because they are Finder plugins--it's not something that a separate program could do. This isn't really a Python question, though; you'd probably have better luck finding answers on a OS X-related list. -Miles From vinay_sajip at yahoo.co.uk Fri Apr 10 19:57:35 2009 From: vinay_sajip at yahoo.co.uk (Vinay Sajip) Date: Fri, 10 Apr 2009 16:57:35 -0700 (PDT) Subject: how to use logging.config.fileConfig ? References: Message-ID: <344ec5e4-f7e0-48ea-bd07-25d93d7dd48f@h28g2000yqd.googlegroups.com> On Apr 10, 3:51 pm, Alexandru Mo?oi wrote: > config.py and I found a big comment saying that old loggers are > disabled! Why? What am I doing wrong? This is by design, because fileConfig is not meant for incremental configuration - it is for complete configuration of the logging system. The old loggers which are not mentioned in the config file are disabled (rather than removed from memory) as there might be threads running which reference them. So if your logging configuration is static for an application run you can use fileConfig. If it is dyanmic, you are better off using programmatic configuration. Regards, Vinay Sajip From vinay_sajip at yahoo.co.uk Fri Apr 10 20:01:15 2009 From: vinay_sajip at yahoo.co.uk (Vinay Sajip) Date: Fri, 10 Apr 2009 17:01:15 -0700 (PDT) Subject: ValueError: I/O operation on closed file References: <230a2c21-c8f1-4c58-a27e-89be8477bfe7@v19g2000yqn.googlegroups.com> Message-ID: <89008c04-f5a1-4014-8c1f-f0fa1df80330@o6g2000yql.googlegroups.com> On Apr 10, 9:25 pm, dj wrote: > I have a handler which I use with a set of log levels for the pythonloggingmodule. > > ----------------------------------- myhandler.py > -------------------------------------------------------- > importlogging.handlers > > # create my handler class > class MyHandler(logging.handlers.RotatingFileHandler): > def __init__(self, fn): > logging.handlers.RotatingFileHandler.__init__(self, fn, > > maxBytes=10485760, backupCount=5) > > # Register handler in the "logging.handlers" namespacelogging.handlers.MyHandler = MyHandler > -------------------------------------------------------------------------------------------------------------------- > > Using it, repeatedly generates this error: > > Traceback (most recent call last): > File "C:\python26\lib\logging\handlers.py", line 74, in emit > if self.shouldRollover(record): > File "C:\python26\lib\logging\handlers.py", line 146, in > shouldRollover > self.stream.seek(0, 2) #due to non-posix-compliant Windows > feature > ValueError: I/O operation on closed file > > I am completely stumped has to what could be the issue. > Any help would be greatly appreciated. Not sure - you may need to post a small script which demonstrates the error repeatably. Otherwise, it may be caused by e.g. other handles being open to the same file (say, if you open a FileHandler and a RotatingFileHandler with the same path, or have one of the log files open in an editor which locks the file. Regards, Vinay Sajip From norseman at hughes.net Fri Apr 10 20:14:01 2009 From: norseman at hughes.net (norseman) Date: Fri, 10 Apr 2009 17:14:01 -0700 Subject: zProblem In-Reply-To: <49DFD867.8090409@hughes.net> References: <49DF8701.5010006@hughes.net> <49DFD867.8090409@hughes.net> Message-ID: <49DFE0C9.4070303@hughes.net> norseman wrote: > Gabriel Genellina wrote: >> En Fri, 10 Apr 2009 14:50:57 -0300, norseman >> escribi?: >> >>> However, in order to explain my problem, I need to control the font >>> since text graphics only work for a given font. It's as small as I can >>> get it. >> >> I didn't understand what your problem is actually. >> > ================================ > > UUHHmmmmmmmm Did you read the pdf? > -- > http://mail.python.org/mailman/listinfo/python-list > ====================================== Let me recant a bit on my reply to Gabriel: To Wit: I re-read the contents of the pdf and I think I can see where I wasn't completely clear on the differences between grid and parcel. I need to place differently sized "pieces of paper" on a mockup. The docs to Tkinter mention using inches or other units of measure but Tkinter does not actually allow that. At least I cannot get it to do so. Another tidbit I had found stated that the "grid" would be the bounding dimensions of the first "piece of paper" placed on the background and could not be confined to itself only. Which seem to be true. Publishing companies have long used gridded backdrops to provide placement of non-same sized things to help minimize wasted space. (Good layout people eyeball it just fine.) I need some way to get things of different sizes onto the backdrop - where I want them, regardless. Something like, backdrop is 10 inches wide and 5 inches tall. First piece of paper is 2x2 inches and second is 6x3 (goes under first) and third is 4 x 2 and goes next to first and on top of second. And so on. Tkinter (actually Tk) isn't liking me very much. Is that better? I'm a little too close to the problem. Steve From tjreedy at udel.edu Fri Apr 10 20:19:36 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 10 Apr 2009 20:19:36 -0400 Subject: weird try/finally behaviour In-Reply-To: <20090410114951.GH1430@lupus.logilab.fr> References: <20090410114951.GH1430@lupus.logilab.fr> Message-ID: Sylvain Th?nault wrote: > Hi there, > > I've encountered the following behaviour which I found surprising: > >>>> def test(): > ... for x in ('test', 'tests'): > ... try: > ... if True: > ... print 'return' > ... return 1 > ... finally: > ... print 'break' > ... break > ... print 'end' > ... >>>> test() > return > break > end > If you say 'print test()', you shoud see None printed after 'end' (at least with 3.0) from the function falling off the end. The 'if True:' line make no difference. > As you can see, the 'break' in the finally block makes the 'return 1' beeing ignored. > Is this a known caveat or should it be considered as a bug? Neither, but it s a bit subtle. If you replace 'break' with 'return 2', what would you expect? What does happen is that 2, not 1, is returned. Similarly, the break sends control to "print 'end'" and thence to the implicit 'return None'. The try doc on finally says "If the finally clause raises another exception or executes a return or break statement, the saved exception is lost." The same is true for saved returns, for the same reason: the implicit END-FINALLY (an actual byte code) that completes the pending return/exception is never reached. When bugs.python.org works again, I will try to remember to suggest a doc improvement. Terry Jan Reedy From tjreedy at udel.edu Fri Apr 10 20:23:19 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 10 Apr 2009 20:23:19 -0400 Subject: Bug tracker off-line? In-Reply-To: References: Message-ID: Aahz wrote: > In article , > MRAB wrote: >> Is it just me or is the bug tracker site having problems? I'm not having >> a problem with other sites. > > Someone else reported this on python-dev Still down 7 hours later. From tjreedy at udel.edu Fri Apr 10 20:29:17 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 10 Apr 2009 20:29:17 -0400 Subject: Python 2.6/3.0 packages for Ubuntu? In-Reply-To: References: <20090410202717.544BEFCEB19@montanaro.dyndns.org> <18911.46832.558089.553835@montanaro.dyndns.org> Message-ID: Benjamin Kaplan wrote: > > > On Fri, Apr 10, 2009 at 5:15 PM, > wrote: > > > >> Does Ubuntu really not have Python 2.6 or 3.0 packages.... > > Benjamin> Which version of Ubuntu are you using? > > Benjamin> Hardy: 2.5.2 > Benjamin> Intrepid: 2.5.2 and 3.0rc1 > Benjamin> Jaunty (beta) : 2.6.2rc1 and 3.0.1 > > I'm using Intrepid. Is there a simple way within Synaptic or by editing > config files to jump up to Jaunty? > > > You can either wait 13 days for the final release, I am thinking of getting Ubuntu. Cannot one also download the sources and compile? (Without replacing the system version!) From tjreedy at udel.edu Fri Apr 10 20:37:37 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 10 Apr 2009 20:37:37 -0400 Subject: Unsupported operand types in if/else list comprehension In-Reply-To: References: Message-ID: Mike H wrote: > Hello all, I have a question about the if/else aspect of list comprehension: > > I would like to go through a list and place quotes around an item if > it is a string, and keep the item the same if it's anything else: > > e.g.['a',9,'8b'] --> ['"a"', 9, '"8b"'] > > I understand that if/else list comprehension should be generally: > > b=[(F,T)[boolean test] for val in X] > > so, I tried the following code: > > a=['test',1,'two'] > b=[(inst, '"'+inst+'"')[isinstance(inst, str)] for inst in a] > > I end up getting the error: unsupported operand type(s) for +: 'int' and 'str' > >>From playing around with other examples, I get the feeling that Python > is calculating both values (inst and '"'+inst+'"') before selecting > which one to pass to the new list. Am I right? Is there any way I can > do this using list comprehension? I would just use an old-fashioned for loop, especially if it were ok to quote the strings 'in-place'. for i, item in enumerate(X): if isinstance(item,str): X[i] = '"'+item+'"' tjr From steve at REMOVE-THIS-cybersource.com.au Fri Apr 10 20:54:39 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 11 Apr 2009 00:54:39 GMT Subject: safe eval of moderately simple math expressions References: Message-ID: <01efde5e$0$20620$c3e8da3@news.astraweb.com> On Thu, 09 Apr 2009 13:13:50 -0400, Terry Reedy wrote: > Joel Hedlund wrote: >> Hi all! >> >> I'm writing a program that presents a lot of numbers to the user, and I >> want to let the user apply moderately simple arithmentics to these >> numbers. One possibility that comes to mind is to use the eval >> function, but since that sends up all kinds of warning flags in my >> head, > > Where does the program execute? If on the user's own machine, no > problem. Until the user naively executes a code sample he downloaded from the Internet, and discovers to his horror that his *calculator* is able to upload his banking details to an IRC server hosted in Bulgaria. How quickly we forget... for twenty or thirty years all malware infections was via programs executed on the user's own machine. > Eval is no more dangerous than Python itself. But users know Python is a Turing-complete programming language that can do anything their computer can do. It would come to an unpleasant surprise to discover that (say) your icon editor was also a Turing- complete programming language capable of doing anything your C-compiler could do. The same holds for applications written in Python. -- Steven From benjamin.kaplan at case.edu Fri Apr 10 21:11:13 2009 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Fri, 10 Apr 2009 21:11:13 -0400 Subject: Python 2.6/3.0 packages for Ubuntu? In-Reply-To: References: <20090410202717.544BEFCEB19@montanaro.dyndns.org> <18911.46832.558089.553835@montanaro.dyndns.org> Message-ID: On Fri, Apr 10, 2009 at 8:29 PM, Terry Reedy wrote: > Benjamin Kaplan wrote: > >> >> >> On Fri, Apr 10, 2009 at 5:15 PM, > >> wrote: >> >> >> >> Does Ubuntu really not have Python 2.6 or 3.0 packages.... >> >> Benjamin> Which version of Ubuntu are you using? >> >> Benjamin> Hardy: 2.5.2 >> Benjamin> Intrepid: 2.5.2 and 3.0rc1 >> Benjamin> Jaunty (beta) : 2.6.2rc1 and 3.0.1 >> >> I'm using Intrepid. Is there a simple way within Synaptic or by >> editing >> config files to jump up to Jaunty? >> >> >> You can either wait 13 days for the final release, >> > > I am thinking of getting Ubuntu. Cannot one also download the sources and > compile? (Without replacing the system version!) > By default, user-compiled programs are installed in /usr/local, while the system programs are installed in /usr, so you won't replace anything unless you specify the prefix (though you will have to use the full path to get the system version). If you want to build a package from a later version of Ubuntu (they add a bunch of distro-specific patches to their packages), you can download that source tarball and Ubuntu's patches from packages.ubuntu.com. The binary .debs are there too but those will replace the system versions (and you'll probably need to upgrade a lot of the dependencies). FYI, the normal binary packages in the repositories don't include the header files so you'll need to install the dev versions of all the packages as well (i.e. you need both libsqlite3.0 and libsqlite3-dev to build python's sqlite module). -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at REMOVE-THIS-cybersource.com.au Fri Apr 10 21:31:59 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 11 Apr 2009 01:31:59 GMT Subject: zProblem References: Message-ID: <01efe71d$0$20620$c3e8da3@news.astraweb.com> On Fri, 10 Apr 2009 10:50:57 -0700, norseman wrote: > Moderator: I apologize for attaching anything. This newsgroup has no moderator. > However, in order to explain my problem, I need to control the font > since text graphics only work for a given font. Use the least common denominator available, so-called "ASCII graphics", and put a note that the diagrams are best viewed in a non-proportional font like Courier or equivalent. E.g.: +-------+ | Input | +-------+ | +----------+ |--------------------------| Special | | +----------+ ********* +--------+ * Box *-------| Output | ********* +--------+ Even if the lines don't quite line up, it's decipherable. This is a newsgroup for a programming language. Nearly everyone will be using a monospaced font already, or at least nearly all the *serious* contributors, so that code blocks will line up correctly. More importantly, you should describe the nature of your problem in the body of the post, as concisely as possible, and use attachments for as little content as possible. For many people, it is inconvenient and difficult to deal with attachments in their newsreader, and people won't bother unless they know the topic is of interest to them. You should also read this: http://www.catb.org/~esr/faqs/smart-questions.html -- Steven From gdamjan at gmail.com Fri Apr 10 21:50:12 2009 From: gdamjan at gmail.com (=?UTF-8?B?0JTQsNC80ZjQsNC9INCT0LXQvtGA0LPQuNC10LLRgdC60Lg=?=) Date: Sat, 11 Apr 2009 03:50:12 +0200 Subject: Get the ipv6 address from a interface References: <86176ef7-c2e0-4c5d-b883-d91672e3eb0b@w40g2000yqd.googlegroups.com> <49de7ab0$0$3669$9b622d9e@news.freenet.de> Message-ID: > In Linux, you can only have one IPv4 address per interface (and you > have to use alias interfaces, such as eth0:0, to assign multiple > addresses to a physical link). that's actually not correct, use the "ip" tool (iproute2 package) to see how easily you can have several addresses to a single interface. ip addr add 1.1.1.1/24 dev eth0 ip addr add 2.2.2.1/24 dev eth0 the need for alias interfaces has been removed, a long time ago (AFAIK even before the 2.4 kernel). -- ?????? ( http://softver.org.mk/damjan/ ) In theory, there is no difference between theory and practice. But, in practice, there is. From steve at REMOVE-THIS-cybersource.com.au Fri Apr 10 22:04:11 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 11 Apr 2009 02:04:11 GMT Subject: Unsupported operand types in if/else list comprehension References: Message-ID: <01efeea9$0$20620$c3e8da3@news.astraweb.com> On Fri, 10 Apr 2009 17:07:38 -0400, Mike H wrote: > Hello all, I have a question about the if/else aspect of list > comprehension: > > I would like to go through a list and place quotes around an item if it > is a string, and keep the item the same if it's anything else: > > e.g.['a',9,'8b'] --> ['"a"', 9, '"8b"'] > > I understand that if/else list comprehension should be generally: > > b=[(F,T)[boolean test] for val in X] That's *one* specific form of a list comprehension. A more general form is: alist = [ some-expression for val in sequence if condition ] In your case, (F, T)[boolean test] counts as one such possible expression. It's not a "magic" list comprehension syntax, you can use it anywhere: >>> t = ("not true", "correct") >>> t[45 > 15] 'correct' >>> ("incorrect", "true")[15 > 99] 'incorrect' The disadvantage of the (F, T)[bool] expression is that both F and T are evaluated *before* the boolean test. Think about it: you have to create the tuple (F, T) before you can index into it! In your case, there are three obvious solutions (untested). In no particular order: (1) Use a helper function. def quote_obj(obj): """Return strings quoted, and all other objects unchanged.""" if isinstance(obj, basestring): return '"%s"' % obj else: return obj b = [quote_obj(x) for x in alist] (2) Use the ternary if expression: b = ['"%s"' % obj if isinstance(obj, basestring) else obj for obj in alist] (3) Use a regular for loop with an accumulator: b = [] for obj in alist: if isinstance(obj, basestring): obj = '"%s"' % obj b.append(obj) The less obvious solution is to rethink your program design. Having multiple types of object in the one list is often (but not always) a sign that your design is too complicated and is trying to do to much in too little code. It is a mild code-smell: http://www.joelonsoftware.com/articles/Wrong.html -- Steven From edd at nunswithguns.net Fri Apr 10 22:04:38 2009 From: edd at nunswithguns.net (Edd) Date: Fri, 10 Apr 2009 19:04:38 -0700 (PDT) Subject: [rfc] An object that creates (nested) attributes automatically on assignment Message-ID: <8ba01b87-c3a9-4e22-8273-f8b77d5bb796@e5g2000vbe.googlegroups.com> Hi folks, I'd like to use Python itself as the configuration language for my Python application. I'd like the user to be able to write something like this in their config file(s): cfg.laser.on = True cfg.laser.colour = 'blue' cfg.discombobulated.vegetables = ['carrots', 'broccoli'] # ... To this end, I've created a class that appears to allow instance variables to be created on the fly. In other words, I can to the following to read a config file: cfg = Config() execfile(filename, {'cfg', cfg}, {}) However, I think my implementation of the Config class is a little crappy. I'd really appreciate the critical eye of a pro. Here's the sauce: class Config(object): def __init__(self, sealed=False): def seal(): for v in self._attribs.values(): if isinstance(v, self.__class__): v.seal() del self.__dict__['seal'] d = {'_attribs': {}, '_a2p': None} if not sealed: d['seal'] = seal self.__dict__.update(d) def __getattr__(self, key): if not key in self._attribs: d = Config(sealed='seal' not in self.__dict__) def add2parent(): self._attribs[key] = d if self._a2p: self._a2p() self._a2p = None # if anything is assigned to an attribute of d, # make sure that d is recorded as an attribute of this Config d._a2p = add2parent return d else: return self._attribs[key] def __setattr__(self, key, value): if key in self.__dict__: self.__dict__[key] = value else: if not 'seal' in self.__dict__: clsname = self.__class__.__name__ raise AttributeError("'%s' object attribute '%s' is read-only (object is sealed)" % (clsname, key)) self.__dict__['_attribs'][key] = value if self._a2p: self._a2p() self._a2p = None def __delattr__(self, key): if key in self.__dict__: clsname = self.__class__.__name__ raise AttributeError("can't delete '%s' object attribute '%s' as it is used for book-keeping!" % (clsname, key)) else: if key in self._attribs: del self._attribs[key] def __bool__(self): return bool(self._attribs) def __nonzero__(self): return bool(self._attribs) if __name__ == '__main__': cfg = Config() cfg.a = 1 cfg.b.c = 2 cfg.d.e.f.g.h = [1, 2, 3] print cfg.a print cfg.b.c print cfg.d.e.f.g.h del cfg.b.c print cfg.b.c try: del cfg.d.e._attribs except AttributeError, ex: print ex cfg.seal() try: cfg.k.l.z = [] except AttributeError, ex: print ex Once the config is loaded, it will be passed down to other user- written scripts and it's important that these scripts don't accidentally change the config. So the idea is that I'll call cfg.seal () to prevent any further changes before passing it on to these other scripts. Beyond the general fiddliness of the code, I think the way seal() currently works is particularly pants. I considered using a simpler approach: def mkdd(): return defaultdict(mkdd) cfg = mkdd() execfile(filename, {'cfg': cfg}, {}) But I quite like the way the '.' separators quite naturally (IMO) indicate a hierarchy of settings. Comments and suggestions welcome! Kind regards, Edd From steve at REMOVE-THIS-cybersource.com.au Fri Apr 10 22:12:02 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 11 Apr 2009 02:12:02 GMT Subject: regexp strangeness References: Message-ID: <01eff080$0$20620$c3e8da3@news.astraweb.com> On Thu, 09 Apr 2009 20:48:11 +0100, Dale Amon wrote: > This finds nothing: ... > DEC029 = re.compile("[^&0-9A-Z/ $*,.\-:#@'=\"[<(+\^!);\\\]%_>?]") > This works correctly: ... > DEC029 = re.compile("[^&0-9A-Z/ $*,.\-:#@'=\"[<(+\^!)\\;\]%_>?]") > They differ only in the positioning of the quoted backslash. So you're telling us that two different regexs do different things? Gosh. Thanks for the heads up! BTW, when creating regexes, you may find it much easier if you use raw strings to avoid needing to escape backslashes: '\n' in a string is a newline escape. To get a literal backslash followed by an n, you can write '\\n' or r'\n'. -- Steven From ldo at geek-central.gen.new_zealand Fri Apr 10 22:37:31 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sat, 11 Apr 2009 14:37:31 +1200 Subject: Open source web crawler with mysql integration References: <9ed1c9c7-267e-448e-ba83-98db949d97ec@o11g2000yql.googlegroups.com> <3BC408BC-2006-41E6-AB4E-698DA30CE56A@semanchuk.com> <41B9D72305A64C4DA96CE9E632A981F0@office.ipglobal.net> Message-ID: In message , Philip Semanchuk wrote: > I'd love to open source it and if someone wants to pay me to make it open > source-able, let's talk! Nobody's going to pay you for something of doubtful value--it's up to you to prove the value of the code first. You must go to the community, the community will not come to you. From ldo at geek-central.gen.new_zealand Fri Apr 10 22:39:43 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sat, 11 Apr 2009 14:39:43 +1200 Subject: Python 2.6/3.0 packages for Ubuntu? References: <20090410202717.544BEFCEB19@montanaro.dyndns.org> <18911.46832.558089.553835@montanaro.dyndns.org> Message-ID: In message , Terry Reedy wrote: > I am thinking of getting Ubuntu. Cannot one also download the sources > and compile? (Without replacing the system version!) Debian Unstable offers about 3 different versions of Python that you can install side-by-side. From skip at pobox.com Fri Apr 10 22:42:31 2009 From: skip at pobox.com (skip at pobox.com) Date: Fri, 10 Apr 2009 21:42:31 -0500 Subject: Python 2.6/3.0 packages for Ubuntu? In-Reply-To: References: <20090410202717.544BEFCEB19@montanaro.dyndns.org> <18911.46832.558089.553835@montanaro.dyndns.org> Message-ID: <18912.919.845050.714520@montanaro.dyndns.org> Terry> I am thinking of getting Ubuntu. Cannot one also download the Terry> sources and compile? (Without replacing the system version!) Sure. I compile it all the time on my Mac. This is just a little bupkis virtual server where I run my personal website. I'd like to just install vendor-provided packages, at least as much as I can. -- Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/ "XML sucks, dictionaries rock" - Dave Beazley From aahz at pythoncraft.com Fri Apr 10 22:47:57 2009 From: aahz at pythoncraft.com (Aahz) Date: 10 Apr 2009 19:47:57 -0700 Subject: email in separate thread References: <66f23ffc-345e-4603-9519-cd122a1163b2@j12g2000vbl.googlegroups.com> Message-ID: In article <66f23ffc-345e-4603-9519-cd122a1163b2 at j12g2000vbl.googlegroups.com>, joeygartin wrote: > >The problem is the page hangs while the emails are sent. My current >hosting situation does not give me direct access to an email queue. >So I have two options (that I can think of): > >1) Write the email information to a file and then run a cron job in >the background that routinely checks the file and sends the necessary >emails off > >OR > >2) Spin-off a thread (which I have no background with at all!) for the >emails and be able to redirect the user to the next page. 2a) Fork off a separate process How frequently do you need to send e-mail? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ Why is this newsgroup different from all other newsgroups? From banibrata.dutta at gmail.com Fri Apr 10 23:04:02 2009 From: banibrata.dutta at gmail.com (Banibrata Dutta) Date: Sat, 11 Apr 2009 08:34:02 +0530 Subject: How to create a virtual serial port? In-Reply-To: References: <_cCdndEvsrp8PULUnZ2dnUVZ_qydnZ2d@pdx.net> Message-ID: <3de8e1f70904102004t5126eedfh7ed7801f8665f498@mail.gmail.com> On Sat, Apr 11, 2009 at 3:17 AM, Stuart Davenport < stuart.davenport at gmail.com> wrote: > I'm on a OS X, python 2.5. Basically I will have a remote application > pushing data (GPS) over the network to a python application I have > running on my Mac, I want this python application to again push the > data on to a "virtual serial port". Then the GPS program I have > running on my MAC, RouteBuddy, can read the data from that serial port > as standard. Python app reads data "off" the network (s.a. TCP/IP maybe), and then pushes the data onto a serial-port ? And since you don't have the real device (using the serial-port), you want to create a virtual serial-port ? Is that the intent ? Or, is it something like this that you are trying to do -- http://www.anzio.com/products/virtport.htm ? Or something like this -- http://www.softizer.com/show_product/software_development/components_libraries/virtual_serial_ports_driver_xp/ > The only part I am concerned about here though, is if I can create a > serial port virtually and push data out of it? > I'd guess this capability to require creating a "kext" (kernel extension / driver) on OS/X, and feel that this is not possible in user-land, and thus not a Python domain problem. -- regards, Banibrata http://www.linkedin.com/in/bdutta -------------- next part -------------- An HTML attachment was scrubbed... URL: From banibrata.dutta at gmail.com Fri Apr 10 23:13:09 2009 From: banibrata.dutta at gmail.com (Banibrata Dutta) Date: Sat, 11 Apr 2009 08:43:09 +0530 Subject: How to create a virtual serial port? In-Reply-To: <3de8e1f70904102004t5126eedfh7ed7801f8665f498@mail.gmail.com> References: <_cCdndEvsrp8PULUnZ2dnUVZ_qydnZ2d@pdx.net> <3de8e1f70904102004t5126eedfh7ed7801f8665f498@mail.gmail.com> Message-ID: <3de8e1f70904102013v519176ddwf66615b04a388595@mail.gmail.com> This might help: http://en.wikipedia.org/wiki/COM_port_redirector On Sat, Apr 11, 2009 at 8:34 AM, Banibrata Dutta wrote: > On Sat, Apr 11, 2009 at 3:17 AM, Stuart Davenport < > stuart.davenport at gmail.com> wrote: > >> I'm on a OS X, python 2.5. Basically I will have a remote application >> pushing data (GPS) over the network to a python application I have >> running on my Mac, I want this python application to again push the >> data on to a "virtual serial port". Then the GPS program I have >> running on my MAC, RouteBuddy, can read the data from that serial port >> as standard. > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From philip at semanchuk.com Fri Apr 10 23:15:15 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Fri, 10 Apr 2009 23:15:15 -0400 Subject: Open source web crawler with mysql integration In-Reply-To: References: <9ed1c9c7-267e-448e-ba83-98db949d97ec@o11g2000yql.googlegroups.com> <3BC408BC-2006-41E6-AB4E-698DA30CE56A@semanchuk.com> <41B9D72305A64C4DA96CE9E632A981F0@office.ipglobal.net> Message-ID: <85F0A536-578C-4C01-9646-026318108186@semanchuk.com> On Apr 10, 2009, at 10:37 PM, Lawrence D'Oliveiro wrote: > In message , > Philip > Semanchuk wrote: > >> I'd love to open source it and if someone wants to pay me to make >> it open >> source-able, let's talk! > > Nobody's going to pay you for something of doubtful value--it's up > to you to > prove the value of the code first. You must go to the community, the > community will not come to you. Not true, people pay for things of doubtful value all the time! I just need a better sales team. =) Seriously, if I had expectations of talking someone into fronting money, do you think I'd use words like "insufficiently documented" and "byzantine"? I was just trying to emphasize what I thought was an obvious point: I can't afford the time to open source my code right now, but if someone were to make it worth my while, that'd be a different story. I may as well have said "if I win the lottery". It *could* happen, but I'm not holding my breath (or buying lottery tickets). Cheers Philip From steve at REMOVE-THIS-cybersource.com.au Fri Apr 10 23:22:43 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 11 Apr 2009 03:22:43 GMT Subject: [rfc] An object that creates (nested) attributes automatically on assignment References: <8ba01b87-c3a9-4e22-8273-f8b77d5bb796@e5g2000vbe.googlegroups.com> Message-ID: <01f00111$0$20620$c3e8da3@news.astraweb.com> On Fri, 10 Apr 2009 19:04:38 -0700, Edd wrote: > Hi folks, > > I'd like to use Python itself as the configuration language for my > Python application. I'd like the user to be able to write something like > this in their config file(s): > > cfg.laser.on = True > cfg.laser.colour = 'blue' > cfg.discombobulated.vegetables = ['carrots', 'broccoli'] # ... > > To this end, I've created a class that appears to allow instance > variables to be created on the fly. Um, don't all classes allow that? >>> class MyClass(): pass ... >>> instance = MyClass() >>> Or do you mean instance *attributes*? Again, apart from built-in types and classes that use __slots__, all classes allow that. >>> instance.parrot = 'Norwegian Blue' >>> > In other words, I can to the > following to read a config file: > > cfg = Config() > execfile(filename, {'cfg', cfg}, {}) That's okay so long as you trust the user not to put malicious, or buggy, code in your config file. Personally, I think config files should be more tolerant of errors than a programming language. > However, I think my implementation of the Config class is a little > crappy. I'd really appreciate the critical eye of a pro. Here's the > sauce: For starters, where is your documentation? No doc strings, not even any comments! No, I tell a lie... *one* obscure comment that doesn't really explain much. > class Config(object): > def __init__(self, sealed=False): > def seal(): > for v in self._attribs.values(): > if isinstance(v, self.__class__): v.seal() > del self.__dict__['seal'] > > d = {'_attribs': {}, '_a2p': None} > if not sealed: d['seal'] = seal > > self.__dict__.update(d) I'm going to try to guess what the above does. When you initialise an instance, you can tell the instance to be "sealed" or unsealed. I'm not sure what the difference is, or why you would choose one over the other. Sealed instances seem to be exactly the same as unsealed instances, except they have a seal() method (actually a closure). The seal method, when called, recursively seals any embedded Config instances inside the current instance, then deletes itself. Arghhh!!! Self-modifying code!!! Unclean, unclean!!! I'm not sure why seal() is necessary -- it seems to me that if present, all it does is delete itself. So why not just leave it out altogether? You also have a rather complicated way of adding instance attributes. Instead of d = {'_attribs': {}, '_a2p': None} self.__dict__.update(d) why not just do the more obvious: self._attribs = {} self._a2p = None ? > def __getattr__(self, key): > if not key in self._attribs: > d = Config(sealed='seal' not in self.__dict__) def > add2parent(): > self._attribs[key] = d > if self._a2p: > self._a2p() > self._a2p = None It looks like you are just re-inventing the normal attribute mechanism of Python. I'm not sure why you feel this is necessary. And it contains MORE self-modifying code! Yuck! Frankly I don't care enough to dig into your code to understand how it works in detail. > # if anything is assigned to an attribute of d, # make > sure that d is recorded as an attribute of this > Config > d._a2p = add2parent > return d > else: > return self._attribs[key] > > def __setattr__(self, key, value): > if key in self.__dict__: > self.__dict__[key] = value > else: > if not 'seal' in self.__dict__: > clsname = self.__class__.__name__ > raise AttributeError("'%s' object attribute '%s' > is read-only (object is sealed)" % (clsname, key)) > self.__dict__['_attribs'][key] = value if self._a2p: > self._a2p() > self._a2p = None Does "sealed" mean that the instance is read-only? If so, and if I'm reading this correctly, I think it is buggy. You allow modifications to attributes inside __dict__ *without* checking to see if the instance is read-only. Then you get the test backwards: surely the existence, not the absence, of a 'seal' attribute should mean it is sealed? > def __delattr__(self, key): > if key in self.__dict__: > clsname = self.__class__.__name__ > raise AttributeError("can't delete '%s' object > attribute '%s' as it is used for book-keeping!" % (clsname, key)) > else: > if key in self._attribs: > del self._attribs[key] Nothing much to say here, except that you're doing more work re-inventing the wheel, storing attributes inside _attribs instead of using the general attribute mechanism. Seems unnecessary to me, but perhaps I don't understand your use-case. > Once the config is loaded, it will be passed down to other user- written > scripts and it's important that these scripts don't accidentally change > the config. So the idea is that I'll call cfg.seal () to prevent any > further changes before passing it on to these other scripts. Or you could pass a *copy* of the config, and let them change it to their heart's content, it won't matter. Or you could say "we're all adults here", simply document that any changes will have consequences, and let user scripts change the config. And why not? > Beyond the > general fiddliness of the code, I think the way seal() currently works > is particularly pants. Is "pants" slang for "fragile, hard to understand and difficult to debug"? -- Steven From ross.jett at gmail.com Fri Apr 10 23:50:26 2009 From: ross.jett at gmail.com (Ross) Date: Fri, 10 Apr 2009 20:50:26 -0700 (PDT) Subject: sharing/swapping items between lists Message-ID: <4fd78ac3-ba83-456b-b768-3a0043548565@f19g2000vbf.googlegroups.com> I'm trying to design an iterator that produces two lists. The first list will be a list of unique pairings and the second will be a list of items that weren't used in the first list. After each round, the items that weren't used in the round before will get put back in and the second list will be populated with unique items. To clarify, here's an example of what my output would be if I had 8 items: First Iteration LIST 1 LEFTOVERS LIST [(1,2),(3,4),(5,6)] [7,8] Second Iteration [(1,3), (2,7),(4,8)] [5,6] Third Iteration [(1,5), (2,6), (7,8)] [3,4] etc Additionally, I want the items in the "leftovers" list to be used the same amount. Can you guys suggest an approach to this problem...I'm trying to teach myself python so an outline of how to approach this would probably be more helpful to me than an explicit solution. I'll cry mercy if I can't figure it out after your hints. From http Fri Apr 10 23:53:11 2009 From: http (Paul Rubin) Date: 10 Apr 2009 20:53:11 -0700 Subject: sharing/swapping items between lists References: <4fd78ac3-ba83-456b-b768-3a0043548565@f19g2000vbf.googlegroups.com> Message-ID: <7xskkfq21k.fsf@ruckus.brouhaha.com> Ross writes: > Can you guys suggest an approach to this problem...I'm trying to teach > myself python so an outline of how to approach this would probably be > more helpful to me than an explicit solution. I'll cry mercy if I > can't figure it out after your hints. Look at the "set" datatype. Think of making some sets of integers and finding their unions and intersections as appropriate. From gagsl-py2 at yahoo.com.ar Sat Apr 11 00:24:43 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sat, 11 Apr 2009 01:24:43 -0300 Subject: zProblem References: <49DF8701.5010006@hughes.net> <49DFD867.8090409@hughes.net> <49DFE0C9.4070303@hughes.net> Message-ID: En Fri, 10 Apr 2009 21:14:01 -0300, norseman escribi?: > norseman wrote: >> Gabriel Genellina wrote: >>> En Fri, 10 Apr 2009 14:50:57 -0300, norseman >>> escribi?: >>> >>>> However, in order to explain my problem, I need to control the font >>>> since text graphics only work for a given font. It's as small as I can >>>> get it. >>> I didn't understand what your problem is actually. >> UUHHmmmmmmmm Did you read the pdf? Yes (and I guess not many people would do). It wasn't clear *what* you were asking until this post. > To Wit: I re-read the contents of the pdf and I think I can see where > I wasn't completely clear on the differences between grid and parcel. (are you sure the grammatical issues are relevant at all...?) > I need some way to get things of different sizes onto the backdrop - > where I want them, regardless. Something like, backdrop is 10 inches > wide and 5 inches tall. First piece of paper is 2x2 inches and second is > 6x3 (goes under first) and third is 4 x 2 and goes next to first and on > top of second. And so on. > Tkinter (actually Tk) isn't liking me very much. Now, if the above paragraph really summarizes your problem, I think you should use the "place" geometry manager, not grid (nor pack). The Tkinter documentation [1] is rather short but the Tcl docs [2] have more info. [1] http://effbot.org/tkinterbook/place.htm [2] http://www.tcl.tk/man/tcl8.4/TkCmd/place.htm -- Gabriel Genellina From fetchinson at googlemail.com Sat Apr 11 01:45:59 2009 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Fri, 10 Apr 2009 22:45:59 -0700 Subject: Open source web crawler with mysql integration In-Reply-To: <85F0A536-578C-4C01-9646-026318108186@semanchuk.com> References: <9ed1c9c7-267e-448e-ba83-98db949d97ec@o11g2000yql.googlegroups.com> <3BC408BC-2006-41E6-AB4E-698DA30CE56A@semanchuk.com> <41B9D72305A64C4DA96CE9E632A981F0@office.ipglobal.net> <85F0A536-578C-4C01-9646-026318108186@semanchuk.com> Message-ID: On 4/10/09, Philip Semanchuk wrote: > > On Apr 10, 2009, at 10:37 PM, Lawrence D'Oliveiro wrote: > >> In message , >> Philip >> Semanchuk wrote: >> >>> I'd love to open source it and if someone wants to pay me to make >>> it open >>> source-able, let's talk! >> >> Nobody's going to pay you for something of doubtful value--it's up >> to you to >> prove the value of the code first. You must go to the community, the >> community will not come to you. > > Not true, people pay for things of doubtful value all the time! I just > need a better sales team. =) > > Seriously, if I had expectations of talking someone into fronting > money, do you think I'd use words like "insufficiently documented" and > "byzantine"? I was just trying to emphasize what I thought was an > obvious point: I can't afford the time to open source my code right > now, but if someone were to make it worth my while, that'd be a > different story. I may as well have said "if I win the lottery". It > *could* happen, but I'm not holding my breath (or buying lottery > tickets). > > Cheers > Philip > -- > http://mail.python.org/mailman/listinfo/python-list > This is what http://www.uselesspython.com/ is for! You can dump it there, interested people can be pointed to it, but you expressly say that "it's useless!" so your reputation will not be damaged. Actually, since you say the code is working and there are not many good open source web crawlers out there, I'm sure people will be quite happy with it. Your worry that 90% of the people will only be frustrated by it will not happen I think if you put it on uselesspython because the expectations will not be high anyway. Cheers, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From castironpi at gmail.com Sat Apr 11 03:34:05 2009 From: castironpi at gmail.com (Aaron Brady) Date: Sat, 11 Apr 2009 00:34:05 -0700 (PDT) Subject: weird try/finally behaviour References: <20090410114951.GH1430@lupus.logilab.fr> Message-ID: <14632a62-7875-41da-9a67-0322d3be5740@e12g2000vbe.googlegroups.com> On Apr 10, 7:19?pm, Terry Reedy wrote: > Sylvain Th?nault wrote: > > Hi there, > > > I've encountered the following behaviour which I found surprising: - > If you say 'print test()', you shoud see None printed after 'end' (at > least with 3.0) from the function falling off the end. > > The 'if True:' line make no difference. > > > As you can see, the 'break' in the finally block makes the 'return 1' beeing ignored. > > Is this a known caveat or should it be considered as a bug? > > Neither, but it s a bit subtle. ?If you replace 'break' with 'return 2' Ha! Brilliant. From castironpi at gmail.com Sat Apr 11 03:41:21 2009 From: castironpi at gmail.com (Aaron Brady) Date: Sat, 11 Apr 2009 00:41:21 -0700 (PDT) Subject: safe eval of moderately simple math expressions References: <01efde5e$0$20620$c3e8da3@news.astraweb.com> Message-ID: <734a562a-b524-46d2-b1e6-30085c5e8f8a@o18g2000vbi.googlegroups.com> On Apr 10, 7:54?pm, Steven D'Aprano wrote: > On Thu, 09 Apr 2009 13:13:50 -0400, Terry Reedy wrote: > > Joel Hedlund wrote: > >> Hi all! > > >> I'm writing a program that presents a lot of numbers to the user, and I > >> want to let the user apply moderately simple arithmentics to these > >> numbers. One possibility that comes to mind is to use the eval > >> function, but since that sends up all kinds of warning flags in my > >> head, > > > Where does the program execute? ?If on the user's own machine, no > > problem. > > Until the user naively executes a code sample he downloaded from the > Internet, and discovers to his horror that his *calculator* is able to > upload his banking details to an IRC server hosted in Bulgaria. Mine does that anyway! ..Often without telling anyone. > > How quickly we forget... for twenty or thirty years all malware > infections was via programs executed on the user's own machine. > > > Eval is no more dangerous than Python itself. > > But users know Python is a Turing-complete programming language that can > do anything their computer can do. It would come to an unpleasant > surprise to discover that (say) your icon editor was also a Turing- > complete programming language capable of doing anything your C-compiler > could do. The same holds for applications written in Python. Don't they know that his calculator is written in Python? Do many applications include a programming language? Why do I get the feeling that the authors of 'pyparsing' are out of breath? I wonder if you could do something like copy and paste a "fork" of the 'ast' module, and just remove non-arithmetic classes; then do a normal walk and transform of the foreign code... From Scott.Daniels at Acm.Org Sat Apr 11 03:52:55 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sat, 11 Apr 2009 00:52:55 -0700 Subject: How to create a virtual serial port? In-Reply-To: References: <_cCdndEvsrp8PULUnZ2dnUVZ_qydnZ2d@pdx.net> Message-ID: Stuart Davenport wrote: > ... I'm on a OS X, python 2.5. Basically I will have a remote application > pushing data (GPS) over the network to a python application I have > running on my Mac, I want this python application to again push the > data on to a "virtual serial port". Then the GPS program I have > running on my MAC, RouteBuddy, can read the data from that serial port > as standard. > > The only part I am concerned about here though, is if I can create a > serial port virtually and push data out of it? OK, this is a Max OS/X question. Does "RouteBuddy" have any other way to get data except from the serial port? Does the mac have a serial port?or are you talking "serial port over USB" (which would be an entirely different kettle of fish). With that out of the wa, I'd try to ask some Mac guys if the ponying up is possible, and if so how to do it in _any_ language (likely they'll have an Objective C way). Then pop back over here (or even better a python-mac list, where they'll be familiar with how to do lots of stuff) and you can get help getting python to either do the same thing or talk to a bit of code that does the byte-transfers. --Scott David Daniels Scott.Daniels at Acm.Org From murali.au at gmail.com Sat Apr 11 04:01:48 2009 From: murali.au at gmail.com (Murali kumar) Date: Sat, 11 Apr 2009 13:31:48 +0530 Subject: communication between objects - help In-Reply-To: References: <49DE4521.7040605@probo.com> <49DF2056.6060208@ieee.org> Message-ID: thanks a lot.. I think passing the main object only by reference.. so, this does not causes any overhead.. am i correct..? On Fri, Apr 10, 2009 at 4:02 PM, Dave Angel wrote: > > > Murali kumar wrote: > >> hi all.. >> I'm posted in a word doc becoz to add a image to explain my problem.. >> also I think gmail automatically scans for attachments.. >> >> anyway.. here's my problem...( see the image) >> >> http://www.2shared.com/file/5299759/45e4c614/load.html >> >> Using : Python 2.6 , wxPython 2.8.9 >> <....snip...> >> >> * I don?t know how to return config file or data to mainApp object when >> pressing load button. * >> >> >> * Is it easy to return filename only and load the file?s data into >> mainApp >> object in menu handler itself? * >> >> >> Please suggest me right direction.. and tell me how to do it? >> >> Usually where I can get these informations.. (suggest links and books..) >> >> >> Thanks for any advice.... >> >> >> > > Most of the widget classes in your code should be derived classes, so they > can hold extra instance data & event handlers and such. So that means you > can have extra parameters on the constructor, besides the ones the base > class requires. Use one or more of those extra parameters to store your own > information about the hierarchy. > > Simplest example is to add the app instance to each constructor. Then each > widget object would know how to call back into the app to do some work, or > to load data into a common place. > > DaveA > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From yohell at ifm.liu.se Sat Apr 11 04:18:09 2009 From: yohell at ifm.liu.se (Joel Hedlund) Date: Sat, 11 Apr 2009 10:18:09 +0200 Subject: safe eval of moderately simple math expressions In-Reply-To: <041f540c-1f2a-48fd-9329-245fe746b11d@k41g2000yqh.googlegroups.com> References: <041f540c-1f2a-48fd-9329-245fe746b11d@k41g2000yqh.googlegroups.com> Message-ID: Aaron Brady wrote: > Would you be willing to examine a syntax tree to determine if there > are any class accesses? Sure? How do I do that? I've never done that type of thing before so I can't really say if it would work or not. /Joel From yohell at ifm.liu.se Sat Apr 11 04:22:05 2009 From: yohell at ifm.liu.se (Joel Hedlund) Date: Sat, 11 Apr 2009 10:22:05 +0200 Subject: safe eval of moderately simple math expressions In-Reply-To: References: Message-ID: Matt Nordhoff wrote: >>>> '\x5f' > '_' >>>> getattr(42, '\x5f\x5fclass\x5f\x5f') # __class__ > > > Is that enough to show you the error of your ways? No, because >>> print '_' in '\x5f\x5fclass\x5f\x5f' True > :-D Cuz seriously, it's a bad idea. Yes probably, but that's not why. :-) > (BTW: What if a user tries to do some ridiculously large calculation to > DoS the app? Is that a problem?) Nope. If the user wants to hang her own app that's fine with me. /Joel From yohell at ifm.liu.se Sat Apr 11 04:30:07 2009 From: yohell at ifm.liu.se (Joel Hedlund) Date: Sat, 11 Apr 2009 10:30:07 +0200 Subject: safe eval of moderately simple math expressions In-Reply-To: References: Message-ID: Matt Nordhoff wrote: >>>> '\x5f' > '_' >>>> getattr(42, '\x5f\x5fclass\x5f\x5f') # __class__ > > > Is that enough to show you the error of your ways? No, because >>> print '_' in '\x5f\x5fclass\x5f\x5f' True > :-D Cuz seriously, it's a bad idea. Yes probably, but that's not why. :-) > (BTW: What if a user tries to do some ridiculously large calculation to > DoS the app? Is that a problem?) Nope. If the user wants to hang her own app that's fine with me. /Joel From __peter__ at web.de Sat Apr 11 04:38:32 2009 From: __peter__ at web.de (Peter Otten) Date: Sat, 11 Apr 2009 10:38:32 +0200 Subject: safe eval of moderately simple math expressions References: Message-ID: Joel Hedlund wrote: > Matt Nordhoff wrote: >>>>> '\x5f' >> '_' >>>>> getattr(42, '\x5f\x5fclass\x5f\x5f') # __class__ >> >> >> Is that enough to show you the error of your ways? > > No, because > > >>> print '_' in '\x5f\x5fclass\x5f\x5f' > True But what you're planning to do seems more like >>> def is_it_safe(source): ... return "_" not in source ... >>> source = "getattr(42, '\\x5f\\x5fclass\\x5f\\x5f')" >>> if is_it_safe(source): ... print eval(source) ... Peter From tadwelessar at gmail.com Sat Apr 11 04:44:24 2009 From: tadwelessar at gmail.com (Matteo) Date: Sat, 11 Apr 2009 01:44:24 -0700 (PDT) Subject: Reading 3 objects at a time from list Message-ID: Hi all, let's see if there is a more "pythonic" way of doing what I'm trying to do. I have a lot of strings with numbers like this one: string = "-1 1.3 100.136 1 2.6 100.726 1 3.9 101.464 -1 5.2 102.105" I need to pass the numbers to a function, but three at a time, until the string ends. The strings are of variable length, but always a multiple of three. That's what I did: num = string.split() for triple in zip(num[::3], num[1::3], num[2::3]): func(*triple) it works and I like slices, but I was wondering if there was another way of doing the same thing, maybe reading the numbers in groups of arbitrary length n... any ideas? From clp2 at rebertia.com Sat Apr 11 04:51:24 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 11 Apr 2009 01:51:24 -0700 Subject: Reading 3 objects at a time from list In-Reply-To: References: Message-ID: <50697b2c0904110151u2cf824acj96d2d1ef5c674485@mail.gmail.com> On Sat, Apr 11, 2009 at 1:44 AM, Matteo wrote: > Hi all, > let's see if there is a more "pythonic" way of doing what I'm trying > to do. > I have a lot of strings with numbers like this one: > > string = "-1 1.3 100.136 1 2.6 100.726 1 3.9 101.464 -1 5.2 102.105" > > I need to pass the numbers to a function, but three at a time, until > the string ends. The strings are of variable length, but always a > multiple of three. > > That's what I did: > num = string.split() > for triple in zip(num[::3], num[1::3], num[2::3]): > ? ?func(*triple) > > it works and I like slices, but I was wondering if there was another > way of doing the same thing, maybe reading the numbers in groups of > arbitrary length n... See the grouper() recipe in the `itertools` module -- http://docs.python.org/library/itertools.html Cheers, Chris -- I have a blog: http://blog.rebertia.com From ldo at geek-central.gen.new_zealand Sat Apr 11 04:53:15 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sat, 11 Apr 2009 20:53:15 +1200 Subject: How to create a virtual serial port? References: <_cCdndEvsrp8PULUnZ2dnUVZ_qydnZ2d@pdx.net> Message-ID: In message , Stuart Davenport wrote: > Then the GPS program I have running on my MAC, RouteBuddy, can read the > data from that serial port as standard. Macs don't have serial ports. From yohell at ifm.liu.se Sat Apr 11 05:03:16 2009 From: yohell at ifm.liu.se (Joel Hedlund) Date: Sat, 11 Apr 2009 11:03:16 +0200 Subject: safe eval of moderately simple math expressions In-Reply-To: References: Message-ID: Peter Otten wrote: > But what you're planning to do seems more like > >>>> def is_it_safe(source): > ... return "_" not in source > ... >>>> source = "getattr(42, '\\x5f\\x5fclass\\x5f\\x5f')" >>>> if is_it_safe(source): > ... print eval(source) > ... > Bah. You are completely right of course. Just as a thought experiment, would this do the trick? def is_it_safe(source): return "_" not in source and r'\' not in source I'm not asking because I'm hellbent on having eval in my app, but because it's always useful to see what hazards you don't know about. /Joel From sjmachin at lexicon.net Sat Apr 11 05:18:38 2009 From: sjmachin at lexicon.net (John Machin) Date: Sat, 11 Apr 2009 02:18:38 -0700 (PDT) Subject: An object that creates (nested) attributes automatically on assignment References: <8ba01b87-c3a9-4e22-8273-f8b77d5bb796@e5g2000vbe.googlegroups.com> <01f00111$0$20620$c3e8da3@news.astraweb.com> Message-ID: <9f4548bd-65d5-4c64-8022-f3022b99b8d3@s1g2000prd.googlegroups.com> On Apr 11, 1:22?pm, Steven D'Aprano wrote: > Is "pants" slang for "fragile, hard to understand and difficult to debug"? pommy slang for "sucks intensely, like the Deathstar's tractor beam" ... I think we agree with him. From __peter__ at web.de Sat Apr 11 05:19:41 2009 From: __peter__ at web.de (Peter Otten) Date: Sat, 11 Apr 2009 11:19:41 +0200 Subject: safe eval of moderately simple math expressions References: Message-ID: Joel Hedlund wrote: > Peter Otten wrote: >> But what you're planning to do seems more like >> >>>>> def is_it_safe(source): >> ... return "_" not in source >> ... >>>>> source = "getattr(42, '\\x5f\\x5fclass\\x5f\\x5f')" >>>>> if is_it_safe(source): >> ... print eval(source) >> ... >> > > Bah. You are completely right of course. > > Just as a thought experiment, would this do the trick? > > def is_it_safe(source): > return "_" not in source and r'\' not in source >>> "".join(map(chr, [95, 95, 110, 111, 95, 95])) '__no__' By the way, a raw string may not end with a backslash: >>> r'\' File "", line 1 r'\' ^ SyntaxError: EOL while scanning single-quoted string Peter From bockman at virgilio.it Sat Apr 11 05:23:36 2009 From: bockman at virgilio.it (Francesco Bochicchio) Date: Sat, 11 Apr 2009 11:23:36 +0200 Subject: Python 2.6/3.0 packages for Ubuntu? In-Reply-To: References: Message-ID: <49e06198$0$6834$5fc30a8@news.tiscali.it> skip at pobox.com ha scritto: > Does Ubuntu really not have Python 2.6 or 3.0 packages or do I just have my > package list misconfigured? I'm setting up a fresh machine and am not too > Ubuntu-aware. Is there a list of package repositories around somewhere? > > Thx, > In current 8.10, the default python is 2.5, but there is a set of packages python3-... which gives you the 3.0. The upcoming 09.04 will have 2.6 as standard, I think ... Ciao ----- FB From yohell at ifm.liu.se Sat Apr 11 05:27:06 2009 From: yohell at ifm.liu.se (Joel Hedlund) Date: Sat, 11 Apr 2009 11:27:06 +0200 Subject: safe eval of moderately simple math expressions In-Reply-To: References: Message-ID: Peter Otten wrote: >> def is_it_safe(source): >> return "_" not in source and r'\' not in source > >>>> "".join(map(chr, [95, 95, 110, 111, 95, 95])) > '__no__' But you don't have access to neither map or chr? /Joel From cs at zip.com.au Sat Apr 11 05:30:59 2009 From: cs at zip.com.au (Cameron Simpson) Date: Sat, 11 Apr 2009 19:30:59 +1000 Subject: Adding a Badge to an Icon in Mac OS X In-Reply-To: Message-ID: <20090411093059.GA3118@cskk.homeip.net> On 10Apr2009 19:56, Miles wrote: | On Fri, Apr 10, 2009 at 5:22 PM, bingo wrote: | > PyObjc seems to offer the option to add badges to icons in the doc. I | > need to add badges to any icon... kinda like SCPlugin and dropbox do. | > I think that SCPlugin is doing it through carbon Icon Services. But I | > am still trying to figure out how it is done! | | I believe those programs are able to do so because they are Finder | plugins--it's not something that a separate program could do. This | isn't really a Python question, though; you'd probably have better | luck finding answers on a OS X-related list. Like this one? http://mail.python.org/mailman/listinfo/pythonmac-sig Cheers, -- Cameron Simpson DoD#743 http://www.cskk.ezoshosting.com/cs/ From __peter__ at web.de Sat Apr 11 05:31:06 2009 From: __peter__ at web.de (Peter Otten) Date: Sat, 11 Apr 2009 11:31:06 +0200 Subject: safe eval of moderately simple math expressions References: Message-ID: Joel Hedlund wrote: > Peter Otten wrote: >>> def is_it_safe(source): >>> return "_" not in source and r'\' not in source >> >>>>> "".join(map(chr, [95, 95, 110, 111, 95, 95])) >> '__no__' > > But you don't have access to neither map or chr? > > /Joel >>> '5f5f7374696c6c5f6e6f745f736166655f5f'.decode("hex") '__still_not_safe__' From yohell at ifm.liu.se Sat Apr 11 05:38:50 2009 From: yohell at ifm.liu.se (Joel Hedlund) Date: Sat, 11 Apr 2009 11:38:50 +0200 Subject: safe eval of moderately simple math expressions In-Reply-To: References: Message-ID: Peter Otten wrote: > Joel Hedlund wrote: > >> Peter Otten wrote: >>>> def is_it_safe(source): >>>> return "_" not in source and r'\' not in source >>>>>> "".join(map(chr, [95, 95, 110, 111, 95, 95])) >>> '__no__' >> But you don't have access to neither map or chr? >> >> /Joel > >>>> '5f5f7374696c6c5f6e6f745f736166655f5f'.decode("hex") > '__still_not_safe__' Now *that's* a thing of beauty. A horrible, horrible kind of beauty. Thanks for blowing holes in my inflated sense of security! /Joel From bockman at virgilio.it Sat Apr 11 05:48:36 2009 From: bockman at virgilio.it (Francesco Bochicchio) Date: Sat, 11 Apr 2009 11:48:36 +0200 Subject: Reading 3 objects at a time from list In-Reply-To: References: Message-ID: <49e06774$0$700$5fc30a8@news.tiscali.it> Chris Rebert ha scritto: > On Sat, Apr 11, 2009 at 1:44 AM, Matteo wrote: >> Hi all, >> let's see if there is a more "pythonic" way of doing what I'm trying >> to do. >> I have a lot of strings with numbers like this one: >> >> string = "-1 1.3 100.136 1 2.6 100.726 1 3.9 101.464 -1 5.2 102.105" >> >> I need to pass the numbers to a function, but three at a time, until >> the string ends. The strings are of variable length, but always a >> multiple of three. >> >> That's what I did: >> num = string.split() >> for triple in zip(num[::3], num[1::3], num[2::3]): >> func(*triple) >> >> it works and I like slices, but I was wondering if there was another >> way of doing the same thing, maybe reading the numbers in groups of >> arbitrary length n... > > See the grouper() recipe in the `itertools` module -- > http://docs.python.org/library/itertools.html > > Cheers, > Chris > I would do that with a generator: >>> def groups(l,n) : ... while l: yield l[:n]; l=l[n:] ... >>> list(groups(range(14),4)) [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13]] >>> list(groups(range(18),3)) [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11], [12, 13, 14], [15, 16, 17]] Ciao ----- FB From stuart.davenport at gmail.com Sat Apr 11 05:53:09 2009 From: stuart.davenport at gmail.com (Stuart Davenport) Date: Sat, 11 Apr 2009 02:53:09 -0700 (PDT) Subject: How to create a virtual serial port? References: <_cCdndEvsrp8PULUnZ2dnUVZ_qydnZ2d@pdx.net> Message-ID: <5fd78a41-ed64-4e3e-aa80-a89c8f44cf3f@v6g2000vbb.googlegroups.com> On 11 Apr, 08:52, Scott David Daniels wrote: > Stuart Davenport wrote: > > ... I'm on a OS X, python 2.5. Basically I will have a remote application > > pushing data (GPS) over the network to a python application I have > > running on my Mac, I want this python application to again push the > > data on to a "virtual serial port". Then the GPS program I have > > running on my MAC, RouteBuddy, can read the data from that serial port > > as standard. > > > The only part I am concerned about here though, is if I can create a > > serial port virtually and push data out of it? > > OK, this is a Max OS/X question. ?Does "RouteBuddy" have any other way > to get data except from the serial port? ?Does the mac have a serial > port?or are you talking "serial port over USB" (which would be an > entirely different kettle of fish). ?With that out of the wa, I'd try > to ask some Mac guys if the ponying up is possible, and if so how to > do it in _any_ language (likely they'll have an Objective C way). > Then pop back over here (or even better a python-mac list, where > they'll be familiar with how to do lots of stuff) and you can get > help getting python to either do the same thing or talk to a bit > of code that does the byte-transfers. > > --Scott David Daniels > Scott.Dani... at Acm.Org Sadly RouteBuddy cannot retrieve data by other means... Scott, many thanks for the advice. Will try to find a more specific forum of conversation. Stu From edd at nunswithguns.net Sat Apr 11 06:01:48 2009 From: edd at nunswithguns.net (Edd) Date: Sat, 11 Apr 2009 03:01:48 -0700 (PDT) Subject: An object that creates (nested) attributes automatically on assignment References: <8ba01b87-c3a9-4e22-8273-f8b77d5bb796@e5g2000vbe.googlegroups.com> <01f00111$0$20620$c3e8da3@news.astraweb.com> Message-ID: Hi Steven, Thank you for your response! On Apr 11, 4:22?am, Steven D'Aprano wrote: > On Fri, 10 Apr 2009 19:04:38 -0700, Edd wrote: > > Hi folks, > > > I'd like to use Python itself as the configuration language for my > > Python application. I'd like the user to be able to write something like > > this in their config file(s): > > > ? ?cfg.laser.on = True > > ? ?cfg.laser.colour = 'blue' > > ? ?cfg.discombobulated.vegetables = ['carrots', 'broccoli'] # ... > > > To this end, I've created a class that appears to allow instance > > variables to be created on the fly. > > Um, don't all classes allow that? > > >>> class MyClass(): pass > ... > >>> instance = MyClass() > > Or do you mean instance *attributes*? Again, apart from built-in types > and classes that use __slots__, all classes allow that. > > >>> instance.parrot = 'Norwegian Blue' Yes I probably mean instance attributes. Forgive me, I am not particularly sure of the terminology. But your MyClass example, won't quite do what I want, as I'd like to be able to define instance attributes on top of instance attributes by assignment: >>> class MyClass(): pass ... >>> instance = MyClass() >>> instance.lasers.armed = True Traceback (most recent call last): File "", line 1, in AttributeError: MyClass instance has no attribute 'laser' >>> > > In other words, I can to the > > following to read a config file: > > > ? ? cfg = Config() > > ? ? execfile(filename, {'cfg', cfg}, {}) > > That's okay so long as you trust the user not to put malicious, or buggy, > code in your config file. Personally, I think config files should be more > tolerant of errors than a programming language. That's certainly a valid remark, but this will be a tool for programmers. I am hoping that the user will make use of the power in moderation. Often, it really will be useful to allow functions to be defined in the config files, for example. > > However, I think my implementation of the Config class is a little > > crappy. I'd really appreciate the critical eye of a pro. Here's the > > sauce: > > For starters, where is your documentation? No doc strings, not even any > comments! No, I tell a lie... *one* obscure comment that doesn't really > explain much. Yes, you're quite right. I was about to add some doc strings, but I didn't think the implementation was good enough. That's somewhat backwards, though, right?! Especially considering I'm asking for improvements. Anyway, I had hoped that the example usage at the end would show what the purpose of the class is. > > > ? ? class Config(object): > > ? ? ? ? def __init__(self, sealed=False): > > ? ? ? ? ? ? def seal(): > > ? ? ? ? ? ? ? ? for v in self._attribs.values(): > > ? ? ? ? ? ? ? ? ? ? if isinstance(v, self.__class__): v.seal() > > ? ? ? ? ? ? ? ? del self.__dict__['seal'] > > > ? ? ? ? ? ? d = ?{'_attribs': {}, '_a2p': None} > > ? ? ? ? ? ? if not sealed: d['seal'] = seal > > > ? ? ? ? ? ? self.__dict__.update(d) > > I'm going to try to guess what the above does. When you initialise an > instance, you can tell the instance to be "sealed" or unsealed. I'm not > sure what the difference is, or why you would choose one over the other. > Sealed instances seem to be exactly the same as unsealed instances, > except they have a seal() method (actually a closure). The seal method, > when called, recursively seals any embedded Config instances inside the > current instance, then deletes itself. > > Arghhh!!! Self-modifying code!!! Unclean, unclean!!! Quite! > I'm not sure why seal() is necessary -- it seems to me that if present, > all it does is delete itself. So why not just leave it out altogether? As I said in the original post, such Config objects will be made available to other kinds of user-written script and it's important that the Config not change between the execution of one script and the next. The seal() mechanism was an attempt to help the user from *accidentally* doing this and then having to try to diagnose the problem and understand how changing the config might have broken the invariants of the software. I guess a big "DON'T CHANGE THE CONFIG IN YOUR SCRIPTS" message in the manual, might be sufficient, though :) > You also have a rather complicated way of adding instance attributes. > Instead of > > d = ?{'_attribs': {}, '_a2p': None} > self.__dict__.update(d) > > why not just do the more obvious: > > self._attribs = {} > self._a2p = None Because that would go through __setattr__(), which does something else (which is the whole point of the class). At least, that was my understanding, which certainly could be at fault. This might be nicer I guess: self.__dict__['_attribs'] = {} self.__dict__['_a2p'] = None There used to be more instance attributes than just two so it was easier to put them in a dict and use update. I agree that it's rather obfuscated, though. [Edd's horrendous code snipped] > It looks like you are just re-inventing the normal attribute mechanism of > Python. I'm not sure why you feel this is necessary. And it contains MORE > self-modifying code! Yuck! Frankly I don't care enough to dig into your > code to understand how it works in detail. Ok. But is there a quick-and-easy way of creating an object, cfg, such that I can write: cfg.hovercraft.full.of = 'eels' without knowing in advance that the user will want a .hovercraft instance attribute, or a .full attribute inside that, or a .of inside that, or ... ? > > ? ? ? ? def __setattr__(self, key, value): > > ? ? ? ? ? ? if key in self.__dict__: > > ? ? ? ? ? ? ? ? self.__dict__[key] = value > > ? ? ? ? ? ? else: > > ? ? ? ? ? ? ? ? if not 'seal' in self.__dict__: > > ? ? ? ? ? ? ? ? ? ? clsname = self.__class__.__name__ > > ? ? ? ? ? ? ? ? ? ? raise AttributeError("'%s' object attribute '%s' > > is read-only (object is sealed)" % (clsname, key)) > > ? ? ? ? ? ? ? ? self.__dict__['_attribs'][key] = value if self._a2p: > > ? ? ? ? ? ? ? ? ? ? self._a2p() > > ? ? ? ? ? ? ? ? ? ? self._a2p = None > > Does "sealed" mean that the instance is read-only? If so, and if I'm > reading this correctly, I think it is buggy. You allow modifications to > attributes inside __dict__ *without* checking to see if the instance is > read-only. Then you get the test backwards: surely the existence, not the > absence, of a 'seal' attribute should mean it is sealed? The absence of the seal() 'method' means that it has already been called i.e. the object has already been sealed. I agree it's somewhat fishy, which is why I'm asking for suggestions for improvements. Perhaps I should just forget the seal() idea. > > Once the config is loaded, it will be passed down to other user- written > > scripts and it's important that these scripts don't accidentally change > > the config. So the idea is that I'll call cfg.seal () to prevent any > > further changes before passing it on to these other scripts. > > Or you could pass a *copy* of the config, and let them change it to their > heart's content, it won't matter. I think that's probably the best thing. I was worried about some parts not being deep-copyable, but I think I'm happy to put that concern aside. Besides, right now, even though you can't add/delete attributes in a Config, you can still change existing ones: cfg = Config() cfg.a.b.c = [1, 2] cfg.seal() cfg.a.b.c[1] = 3 # pffff Yes, you've helped convince me that it's just a bad idea. > Or you could say "we're all adults here", simply document that any > changes will have consequences, and let user scripts change the config. > And why not? Even adults make the occasional tiny mistake which has confusing consequences in a larger system. It was an attempt to help prevent this. I probably worry too much about that. > > Beyond the > > general fiddliness of the code, I think the way seal() currently works > > is particularly pants. > > Is "pants" slang for "fragile, hard to understand and difficult to debug"? Yes! Rest assured that I am under no illusion that what I have written is good! Steven, I greatly appreciate your taking the time to understand the aforementioned horrors. Assuming that the seal() stuff is no longer a requirement, is there a cleaner way of creating an object where (nested) instance attributes can be defined by simple assignment? Perhaps it would have been better if I had left out my awful attempt altogether. It seems like it only made my question more confusing than it needed to be :( If it would help, I'd be happy to add some doc strings and tests but I was getting ready to throw this code away when a cleaner 5-line alternative was presented! Kind regards, Edd From __peter__ at web.de Sat Apr 11 06:14:48 2009 From: __peter__ at web.de (Peter Otten) Date: Sat, 11 Apr 2009 12:14:48 +0200 Subject: Reading 3 objects at a time from list References: Message-ID: Matteo wrote: > Hi all, > let's see if there is a more "pythonic" way of doing what I'm trying > to do. > I have a lot of strings with numbers like this one: > > string = "-1 1.3 100.136 1 2.6 100.726 1 3.9 101.464 -1 5.2 102.105" > > I need to pass the numbers to a function, but three at a time, until > the string ends. The strings are of variable length, but always a > multiple of three. > > That's what I did: > num = string.split() > for triple in zip(num[::3], num[1::3], num[2::3]): > func(*triple) > > it works and I like slices, but I was wondering if there was another > way of doing the same thing, maybe reading the numbers in groups of > arbitrary length n... > > any ideas? Ideas? Many. But do they make sense? >>> from itertools import * >>> items = range(10) >>> for chunk in iter((tuple(islice(items, 3)) for items in repeat(iter(items))).next, ()): ... print chunk ... (0, 1, 2) (3, 4, 5) (6, 7, 8) (9,) >>> for chunk in takewhile(bool, (tuple(islice(items, 3)) for items in repeat(iter(items)))): ... print chunk ... (0, 1, 2) (3, 4, 5) (6, 7, 8) (9,) >>> def make_read(items): ... items = iter(items) ... def read(n): ... return tuple(islice(items, n)) ... return read ... >>> from functools import partial >>> for chunk in iter(partial(make_read(items), 3), ()): ... print chunk ... (0, 1, 2) (3, 4, 5) (6, 7, 8) (9,) >>> read = make_read(items) >>> for n in [3,4,2,1]: ... print read(n) ... (0, 1, 2) (3, 4, 5, 6) (7, 8) (9,) >>> items = iter([3, "a", "b", "c", 2, "p", "q", 4, "x", "y", "z", "t"]) >>> read = make_read(items) >>> for n in items: ... print "".join(read(n)) ... abc pq xyzt From castironpi at gmail.com Sat Apr 11 06:22:19 2009 From: castironpi at gmail.com (Aaron Brady) Date: Sat, 11 Apr 2009 03:22:19 -0700 (PDT) Subject: safe eval of moderately simple math expressions References: <041f540c-1f2a-48fd-9329-245fe746b11d@k41g2000yqh.googlegroups.com> Message-ID: On Apr 11, 3:18?am, Joel Hedlund wrote: > Aaron Brady wrote: > > Would you be willing to examine a syntax tree to determine if there > > are any class accesses? > > Sure? How do I do that? I've never done that type of thing before so I > can't really say if it would work or not. > > /Joel NO PROMISES. No warranty is made, express or implied. Of course, something this devious, a "white" list, may just make it so your enemy finds out its weakness before you do. It's ostensibly for Python 3, but IIRC there's a way to do it in 2. 'ast.literal_eval' appears to evaluate a literal, but won't do expressions, which is what you are looking for. We should refer people to it more often. +1 ast.walk, btw. If you want subtraction and division, you'll have to add them yourself. You could probably compress the 'is_it_safe' function to one line, provided that it's sound to start with: if all( x in safe_node_classes for x in ast.walk( ast.parse( exp ) ) ), or better yet, if set( ast.walk( ast.parse( exp ) ) )<= safe_node_classes. +1! /Source: import ast safe_exp= '( 2+ 4 )* 7' unsafe_exp= '( 2+ 4 ).__class__' unsafe_exp2= '__import__( "os" )' safe_node_classes= set( [ ast.Module, ast.Expr, ast.BinOp, ast.Mult, ast.Add, ast.Num ] ) def is_it_safe( exp ): print( 'trying %s'% exp ) top= ast.parse( exp ) for node in ast.walk( top ): print( node ) if node.__class__ not in safe_node_classes: return False print( 'ok!' ) return True print( safe_exp, is_it_safe( safe_exp ) ) print( ) print( unsafe_exp, is_it_safe( unsafe_exp ) ) print( ) print( unsafe_exp2, is_it_safe( unsafe_exp2 ) ) print( ) /Output: trying ( 2+ 4 )* 7 <_ast.Module object at 0x00BB5DF0> <_ast.Expr object at 0x00BB5E10> <_ast.BinOp object at 0x00BB5E30> <_ast.BinOp object at 0x00BB5E50> <_ast.Mult object at 0x00BAF590> <_ast.Num object at 0x00BB5EB0> <_ast.Num object at 0x00BB5E70> <_ast.Add object at 0x00BAF410> <_ast.Num object at 0x00BB5E90> ok! ( 2+ 4 )* 7 True trying ( 2+ 4 ).__class__ <_ast.Module object at 0x00BB5E90> <_ast.Expr object at 0x00BB5DF0> <_ast.Attribute object at 0x00BB5E10> ( 2+ 4 ).__class__ False trying __import__( "os" ) <_ast.Module object at 0x00BB5E10> <_ast.Expr object at 0x00BB5E30> <_ast.Call object at 0x00BB5E50> __import__( "os" ) False From steve at REMOVE-THIS-cybersource.com.au Sat Apr 11 06:46:45 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 11 Apr 2009 10:46:45 GMT Subject: safe eval of moderately simple math expressions References: Message-ID: <01f06921$0$20620$c3e8da3@news.astraweb.com> On Sat, 11 Apr 2009 11:03:16 +0200, Joel Hedlund wrote: > Peter Otten wrote: >> But what you're planning to do seems more like >> >>>>> def is_it_safe(source): >> ... return "_" not in source >> ... >>>>> source = "getattr(42, '\\x5f\\x5fclass\\x5f\\x5f')" if >>>>> is_it_safe(source): >> ... print eval(source) >> ... >> > > Bah. You are completely right of course. > > Just as a thought experiment, would this do the trick? > > def is_it_safe(source): > return "_" not in source and r'\' not in source > > I'm not asking because I'm hellbent on having eval in my app, but > because it's always useful to see what hazards you don't know about. Can we pass your test and still write to a file? Too easy. >>> file('spam.txt', 'r') # prove that the file doesn't exist Traceback (most recent call last): File "", line 1, in IOError: [Errno 2] No such file or directory: 'spam.txt' >>> >>> source = "4+(file('spam.txt', 'w').write('spam spam spam') or 0)+5" >>> if is_it_safe(source): ... print eval(source) ... 9 >>> file('spam.txt', 'r').read() 'spam spam spam' Can we pass your test and import a module and grab its docstring? >>> source = "getattr(eval(chr(90+5)*2+'im'+'por'+'t'+chr(None or 95)*2+'('+chr(39)+'os'+chr(39)+')'), chr(95)*2+'doc'+chr(99-4)*2)" >>> if is_it_safe(source): ... eval(source) ... "OS routines for Mac, NT, or Posix depending ... " Restricting Python is hard. No, not hard. It's *REALLY HARD*. Experts have tried and failed. A good example is Tav's recent attempt to secure Python code from *one* threat: writing a file on the local disk. Should be simple, right? If only. http://tav.espians.com/a-challenge-to-break-python-security.html The first exploit came an hour after Tav went public. You can read the discussion on the Python-Dev list starting here: http://mail.python.org/pipermail/python-dev/2009-February/086401.html More here: http://tav.espians.com/paving-the-way-to-securing-the-python-interpreter.html http://tav.espians.com/update-on-securing-the-python-interpreter.html My recommendation is that you do one of these: (1) Give up on making your code "safe". Recognise that the threat is relatively small, but real, and put a warning in your documentation about the risk to user's own system if they evaluate arbitrary code, and then just use eval and hope for the best. (2) Decide that you don't want your calculate to be a full-fledged programming language, and give up on making eval safe. Write your own mini-parser to do arithmetic expressions. It's really not that difficult: really easy with PyParsing, and not that hard without. -- Steven From GStouten at gmail.com Sat Apr 11 06:53:31 2009 From: GStouten at gmail.com (Moriaantje) Date: Sat, 11 Apr 2009 03:53:31 -0700 (PDT) Subject: I can't get RLock to work (warning, the following code is pretty long) References: <8148d3c3-7b97-4fa1-ba99-e0bb0c2b6cbf@k8g2000yqn.googlegroups.com> Message-ID: I think it would help if you would call your functions in get_both_parts with some arguments ... From greg.ewing at canterbury.ac.nz Sat Apr 11 07:10:51 2009 From: greg.ewing at canterbury.ac.nz (Greg Ewing) Date: Sat, 11 Apr 2009 23:10:51 +1200 Subject: ANN: PyGUI 2.0 Message-ID: <49E07ABB.7070803@canterbury.ac.nz> PyGUI 2.0 is available: http://www.cosc.canterbury.ac.nz/greg.ewing/python_gui/ Highlights of this release: * Native Windows implementation, based on pywin32 and ctypes. * Full set of Postscript-style path construction operators available on all platforms. * Mouse and keyboard events can be intercepted for all component classes. Plus numerous minor improvements, see CHANGES on the website for details. What is PyGUI? -------------- PyGUI is a cross-platform GUI toolkit designed to be lightweight and have a highly Pythonic API. Acknowledgements ---------------- Thanks are due to Patrick Forget and Erez-Sh for getting me started on the Windows implementation. Even though I didn't use much of their code in the end, I appreciate their efforts and may draw more from it in the future. -- Gregory Ewing greg.ewing at canterbury.ac.nz http://www.cosc.canterbury.ac.nz/greg.ewing/ From d.dalton at iinet.net.au Sat Apr 11 07:36:43 2009 From: d.dalton at iinet.net.au (Daniel Dalton) Date: Sat, 11 Apr 2009 21:36:43 +1000 Subject: storing variable value Message-ID: <20090411113643.GA18097@debian-hp.lan> Hi! I'm writing a program to provide me with battery warnings when my battery hits certain levels. It just checks the current level and does something. I plan to call it from a a cron job. But If the cron runs every minute, warnings every minute would be rather annoying. so is there a way to make the script check if it has already ran before? eg. can I write to a variable on one run of the program, and on the next read that value that was written? Thx! Daniel. -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 204 bytes Desc: Digital signature URL: From deets at nospam.web.de Sat Apr 11 07:38:01 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sat, 11 Apr 2009 13:38:01 +0200 Subject: Unsupported operand types in if/else list comprehension In-Reply-To: References: Message-ID: <74bdopF126tl6U1@mid.uni-berlin.de> Mike H schrieb: > Thanks to all of you. > > FYI, I'm doing this because I'm working on creating some insert > statements in SQL, where string values need to be quoted, and integer > values need to be unquoted. > > I wanted to be sure that I could pass these values to the list in a > normal way e.g. ['test', 1, 'two'] and have a function correct the > list for me, rather than calling the function with a strangely quoted > list e.g. ['"'test'"', 1, '"'two'"'].> Don't do that yourself. This is error-prone. Instead, use the parametrized verison of the cursor.execute-method. It will perform the necessary escaping, and depending on the database and database adapter you use better performance. Diez From steve at REMOVE-THIS-cybersource.com.au Sat Apr 11 07:54:35 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 11 Apr 2009 11:54:35 GMT Subject: An object that creates (nested) attributes automatically on assignment References: <8ba01b87-c3a9-4e22-8273-f8b77d5bb796@e5g2000vbe.googlegroups.com> <01f00111$0$20620$c3e8da3@news.astraweb.com> Message-ID: <01f07907$0$20620$c3e8da3@news.astraweb.com> On Sat, 11 Apr 2009 03:01:48 -0700, Edd wrote: > Yes I probably mean instance attributes. Forgive me, I am not > particularly sure of the terminology. But your MyClass example, won't > quite do what I want, as I'd like to be able to define instance > attributes on top of instance attributes by assignment: > >>>> class MyClass(): pass > ... >>>> instance = MyClass() >>>> instance.lasers.armed = True > Traceback (most recent call last): > File "", line 1, in > AttributeError: MyClass instance has no attribute 'laser' Ah, now it is more clear. Okay, let's try this: >>> class C(object): ... def __getattr__(self, name): ... # Only called if self.name doesn't exist. ... inst = self.__class__() ... setattr(self, name, inst) ... return inst ... >>> c = C() >>> c.x.y.z = 45 >>> c.__dict__ {'x': <__main__.C object at 0xb7c3b78c>} >>> c.x.__dict__ {'y': <__main__.C object at 0xb7c3b7ec>} >>> c.x.y.z 45 -- Steven From deets at nospam.web.de Sat Apr 11 07:55:34 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sat, 11 Apr 2009 13:55:34 +0200 Subject: storing variable value In-Reply-To: References: Message-ID: <74bepnF12k0haU1@mid.uni-berlin.de> Daniel Dalton schrieb: > Hi! > > I'm writing a program to provide me with battery warnings when my > battery hits certain levels. It just checks the current level and does > something. I plan to call it from a a cron job. But If the cron runs > every minute, warnings every minute would be rather annoying. so is > there a way to make the script check if it has already ran before? > eg. can I write to a variable on one run of the program, and on the next > read that value that was written? Use a file to store previous run's state. Use e.g. the pickle-module to store some arbitrary data-structures to the disk. Diez From steve at REMOVE-THIS-cybersource.com.au Sat Apr 11 08:00:00 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 11 Apr 2009 12:00:00 GMT Subject: storing variable value References: Message-ID: <01f07a4c$0$20620$c3e8da3@news.astraweb.com> On Sat, 11 Apr 2009 21:36:43 +1000, Daniel Dalton wrote: > Hi! > > I'm writing a program to provide me with battery warnings when my > battery hits certain levels. It just checks the current level and does > something. I plan to call it from a a cron job. But If the cron runs > every minute, warnings every minute would be rather annoying. so is > there a way to make the script check if it has already ran before? eg. > can I write to a variable on one run of the program, and on the next > read that value that was written? No, every time cron calls your script, it calls it as new. If you want persistent storage, you need to write to persistent storage like a file. Another solution is to use a daemon which is running continuously in the background. Because that's a long-running process, the daemon can then decide how often it checks the battery, and how often it notifies you. However, writing correct daemons is not something for a beginner. I recommend you just stick to writing your persistent data to a file somewhere, and sticking to cron. -- Steven From tleeuwenburg at gmail.com Sat Apr 11 08:22:13 2009 From: tleeuwenburg at gmail.com (tleeuwenburg at gmail.com) Date: Sat, 11 Apr 2009 05:22:13 -0700 (PDT) Subject: Multithreading / multiprocess Message-ID: <57065c62-2024-47b5-a07e-1d60ff85ba23@y10g2000prc.googlegroups.com> Is there anyway to begin a thread and execute a finite number of lines of code, or a finite amount of time within it? For example, say I create three child threads and I want to guarantee equal timeshare between them, can I specify a quanta (say 400 LOC although I know that is pretty small) to execute in each one in turn? -T From edd at nunswithguns.net Sat Apr 11 08:37:41 2009 From: edd at nunswithguns.net (Edd) Date: Sat, 11 Apr 2009 05:37:41 -0700 (PDT) Subject: An object that creates (nested) attributes automatically on assignment References: <8ba01b87-c3a9-4e22-8273-f8b77d5bb796@e5g2000vbe.googlegroups.com> <01f00111$0$20620$c3e8da3@news.astraweb.com> <01f07907$0$20620$c3e8da3@news.astraweb.com> Message-ID: <3dc870d5-a280-4a12-bcc9-7e8dd1320d05@q14g2000vbn.googlegroups.com> On Apr 11, 12:54?pm, Steven D'Aprano wrote: > Ah, now it is more clear. > > Okay, let's try this: > > >>> class C(object): > > ... ? ? def __getattr__(self, name): > ... ? ? ? ? ? ? # Only called if self.name doesn't exist. > ... ? ? ? ? ? ? inst = self.__class__() > ... ? ? ? ? ? ? setattr(self, name, inst) > ... ? ? ? ? ? ? return inst Ha! Perfect! I knew it should be simpler. Thanks very much! Kind regards, Edd From deets at nospam.web.de Sat Apr 11 08:39:21 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sat, 11 Apr 2009 14:39:21 +0200 Subject: Multithreading / multiprocess In-Reply-To: <57065c62-2024-47b5-a07e-1d60ff85ba23@y10g2000prc.googlegroups.com> References: <57065c62-2024-47b5-a07e-1d60ff85ba23@y10g2000prc.googlegroups.com> Message-ID: <74bhbpF12ks15U1@mid.uni-berlin.de> tleeuwenburg at gmail.com schrieb: > Is there anyway to begin a thread and execute a finite number of lines > of code, or a finite amount of time within it? > > For example, say I create three child threads and I want to guarantee > equal timeshare between them, can I specify a quanta (say 400 LOC > although I know that is pretty small) to execute in each one in turn? Not as such, no. You can play tricks with the trace-module, but these ultimately fail when the code in question runs inside C - which puts a stop to any python interpreter scheduling anyway, thus native threads are used which can't be controlled on that level. Diez From m.faustino at gmail.com Sat Apr 11 08:45:21 2009 From: m.faustino at gmail.com (=?ISO-8859-1?Q?M=E1rcio_Faustino?=) Date: Sat, 11 Apr 2009 05:45:21 -0700 (PDT) Subject: Using percent signs with SafeConfigParser Message-ID: <50bfe567-7e59-4138-ae55-c3ee2259b169@g17g2000vbi.googlegroups.com> Hi, Does the SafeConfigParser class correctly detects lone percent signs? For example, shouldn't the string "100%%" be accepted as a valid value? Executing the code below should only print one error, instead it prints 2. (I've tested this with version 2.6.1 on Windows XP.) It seems the "_badpercent_re" regular expression (an attribute of SafeConfigParser, with value "%[^%]|%$") is flawed: - The first alternative "%[^%]" fails with the string "%%_", because it matches "%_". - The second alternative "%$" fails with the string "%%", because it matches "%". The correct regular expression is "(? References: <49DE4521.7040605@probo.com> <49DF2056.6060208@ieee.org> Message-ID: <49E093BE.5060504@dejaviewphoto.com> Murali kumar wrote: > thanks a lot.. > > I think passing the main object only by reference.. so, this does not causes > any overhead.. > > am i correct..? > > > On Fri, Apr 10, 2009 at 4:02 PM, Dave Angel wrote: > > >> Murali kumar wrote: >> >> >>> hi all.. >>> I'm posted in a word doc becoz to add a image to explain my problem.. >>> also I think gmail automatically scans for attachments.. >>> >>> anyway.. here's my problem...( see the image) >>> >>> http://www.2shared.com/file/5299759/45e4c614/load.html >>> >>> Using : Python 2.6 , wxPython 2.8.9 >>> <....snip...> >>> >>> * I don?t know how to return config file or data to mainApp object when >>> pressing load button. * >>> >>> >>> * Is it easy to return filename only and load the file?s data into >>> mainApp >>> object in menu handler itself? * >>> >>> >>> Please suggest me right direction.. and tell me how to do it? >>> >>> Usually where I can get these informations.. (suggest links and books..) >>> >>> >>> Thanks for any advice.... >>> >>> >>> >>> >> Most of the widget classes in your code should be derived classes, so they >> can hold extra instance data & event handlers and such. So that means you >> can have extra parameters on the constructor, besides the ones the base >> class requires. Use one or more of those extra parameters to store your own >> information about the hierarchy. >> >> Simplest example is to add the app instance to each constructor. Then each >> widget object would know how to call back into the app to do some work, or >> to load data into a common place. >> >> DaveA >> >> -- >> http://mail.python.org/mailman/listinfo/python-list >> >> > All objects are passed by reference in python. And storing it in each widget costs very little. Note that you can choose what object or objects should be used; I just mention the app as a choice that everyone will subclass, and that will generally have direct or indirect access to all widgets. > From gvverdad at gmail.com Sat Apr 11 09:01:25 2009 From: gvverdad at gmail.com (gvv) Date: Sat, 11 Apr 2009 06:01:25 -0700 (PDT) Subject: multiprocessing and Locks Message-ID: Hi All, I am trying to understand multiprocessing, but I am getting a Runtime error on the code below. What am I missing or doing wrong? Error is: RuntimeError: Lock objects should only be shared between processes through inheritance I am using: Python 2.6 (r26:66714, Nov 28 2008, 22:17:21) [GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2 Thanks in Advance, George import multiprocessing import time class Base(object): def __init__(self, id, lock): self.Id = id lock.acquire() self.Sleep() lock.release() def Run(self): pass def Sleep(self): time.sleep(5) class Derived(Base): def __init__(self, id, lock): Base.__init__(self, id, lock) def Run(self): print self.Id def RunFunc(id, lock): obj = Derived(id, lock) obj.Run() if __name__ == "__main__": lock = multiprocessing.Lock() Pool = multiprocessing.Pool(processes=5) for i in xrange(100): Pool.apply_async(func=RunFunc, args=(i,lock)) From ptmcg at austin.rr.com Sat Apr 11 09:09:14 2009 From: ptmcg at austin.rr.com (Paul McGuire) Date: Sat, 11 Apr 2009 06:09:14 -0700 (PDT) Subject: safe eval of moderately simple math expressions References: <01efde5e$0$20620$c3e8da3@news.astraweb.com> <734a562a-b524-46d2-b1e6-30085c5e8f8a@o18g2000vbi.googlegroups.com> Message-ID: On Apr 11, 2:41?am, Aaron Brady wrote: > > Why do I get the feeling that the authors of 'pyparsing' are out of > breath? > What kind of breathlessness do you mean? I'm still breathing, last time I checked. The-rumors-of-my-demise-have-been-greatly-exaggerated'ly yours, -- Paul From jonas.esp at googlemail.com Sat Apr 11 09:11:46 2009 From: jonas.esp at googlemail.com (Kless) Date: Sat, 11 Apr 2009 06:11:46 -0700 (PDT) Subject: [ANN] Falcon - powering innovation Message-ID: <3e205f95-313a-4160-b445-5a9bfeb855da@j18g2000prm.googlegroups.com> If anybody is interesed in new technologies, you'll love this new language called Falcon [1], which has been open sourced ago little time. Falcon is a scripting engine ready to empower mission-critical multithreaded applications. It provides six integrated programming paradigms: procedural, object oriented, prototype oriented, functional, tabular and message oriented. You use what you prefer. To know more about its design, read the interview to the Falcon author that has published ComputerWorld Australia [2]. [1] http://www.falconpl.org/ [2] http://www.computerworld.com.au/article/298655/-z_programming_languages_falcon?fp=2&fpid= From davea at ieee.org Sat Apr 11 09:30:40 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 11 Apr 2009 09:30:40 -0400 Subject: storing variable value In-Reply-To: <20090411113643.GA18097@debian-hp.lan> References: <20090411113643.GA18097@debian-hp.lan> Message-ID: <49E09B80.20400@ieee.org> Daniel Dalton wrote: > Hi! > > I'm writing a program to provide me with battery warnings when my > battery hits certain levels. It just checks the current level and does > something. I plan to call it from a a cron job. But If the cron runs > every minute, warnings every minute would be rather annoying. so is > there a way to make the script check if it has already ran before? > eg. can I write to a variable on one run of the program, and on the next > read that value that was written? > > Thx! > > Daniel. > > To put it simply, you want to store a value from one run of the script, that persists till the next run of the same script. The most portable way to to that is to write a file. And since all you care about is the time of writing, the file can be zero length. So simply check for the file, determine it's creation time, and decide if it's too recent to want to run again. If the file doesn't exist, or is "old", then write the file and notify the user. It's still messy to leave this bogus file around, so think carefully about where to put it. If the source directory is writable, it's reasonble to me to simply put it there. That way, if the program gets deleted, it'll probably get deleted at the same time. Failing that, you could put it in a TEMP directory. And perhaps the sneakiest place to put it, if you're on Windows, is in the "run once on startup" directory. Make it a do-nothing batch file, and Windows will remove it next time the user restarts the system. From grante at visi.com Sat Apr 11 09:31:28 2009 From: grante at visi.com (Grant Edwards) Date: Sat, 11 Apr 2009 08:31:28 -0500 Subject: How to create a virtual serial port? References: <_cCdndEvsrp8PULUnZ2dnUVZ_qydnZ2d@pdx.net> Message-ID: On 2009-04-10, Stuart Davenport wrote: > I'm on a OS X, python 2.5. Basically I will have a remote > application pushing data (GPS) over the network to a python > application I have running on my Mac, I want this python > application to again push the data on to a "virtual serial > port". Then the GPS program I have running on my MAC, > RouteBuddy, can read the data from that serial port as > standard. You might be able to use a pty. It depends on what ioctl calls RouteBuddy uses. Unless Apple has done major surgury on the pty driver, the Unix pty driver only supports a subset of the serial-port ioctl commands: for example pty's don't support the TIOCMGET and TIOCMSET commands. If RouteBuddy won't work with a pty, then you could buy two USB-serial converters (that are supported on Mac OS X) and connect them to each other with a null-modem adapter. Or you could fix up the pty driver to support the reset of the serial-port ioctl commands. I've been meaning to do that for Linux for several years now... -- Grant From dunmer at dreams.sk Sat Apr 11 10:02:25 2009 From: dunmer at dreams.sk (Gabriel) Date: Sat, 11 Apr 2009 16:02:25 +0200 Subject: Merging byte arrays Message-ID: <49E0A2F1.7070809@dreams.sk> Hello I'm using this function to read data in byte format from file def readBytes(file, offset, size): file.seek(offset) return file.read(size) file is opened with open function: file = open(path, "rb") then i'm using array.array('B', bytes) to parse read-out data, for example in this function: def getIntValOfBytes(bytes): value = i = 0 for byte in array.array('B', bytes): value |= byte << (8 * i) i += 1 return value when i read one portion of bytes everything works ok. but now i need read from more locations in file, then somehow merge these data together and then pass to getIntValOfBytes function.. i tried list list = [] for offset in offsets list.extend(readBytes(file, offset, size)) or string string = "" for offset in offsets string += readBytes(file, offset, size) but with list i get this error for byte in array.array('B', bytes): TypeError: an integer is required in getIntValOfBytes (i passed the part of the list to the method, list[x:y]) and with string it just freeze.... I will appreciate any help.. [I'm newbie so sorry if i'm missing something obvious] From emmanuel.surleau at gmail.com Sat Apr 11 10:08:49 2009 From: emmanuel.surleau at gmail.com (Emmanuel Surleau) Date: Sat, 11 Apr 2009 16:08:49 +0200 Subject: Definition of Pythonic? Message-ID: <200904111608.49505.emmanuel.surleau@gmail.com> Hi there, I'm starting an exploratory foray into Python, being generally dissatisfied with the Ruby ecosystem (while the language is wonderful, third party libraries and documentation are not). Having written a few trivial scripts in Python, I'm curious as to how you would sum up the Pythonic philosophy of development. Judging from Python, it seems to exclude (mostly) "magical" variables like '$.'. Is this right? What else would you include in this definition? Cheers, Emm From akitada at gmail.com Sat Apr 11 10:13:11 2009 From: akitada at gmail.com (Akira Kitada) Date: Sat, 11 Apr 2009 23:13:11 +0900 Subject: Why is it that *dbm modules don't provide an iterator? (Language design question) In-Reply-To: <49DE8733.8060200@v.loewis.de> References: <90bb445a0904090822h63ee44e1g1f57ad303593f1cd@mail.gmail.com> <49de7c1b$0$3669$9b622d9e@news.freenet.de> <19054ec0f8a16993e1b265112d82b5b1.squirrel@localhost> <49DE8733.8060200@v.loewis.de> Message-ID: <90bb445a0904110713h4529bf1cxe72794c3160d8b55@mail.gmail.com> Opened a ticket for this and attached a patch. (experimental) http://bugs.python.org/issue5736 On Fri, Apr 10, 2009 at 8:39 AM, "Martin v. L?wis" wrote: >>>> I assumed there were some decisions behind this, rather than it's just >>>> not implemented yet. >>> I believe this assumption is wrong - it's really that no code has been >>> contributed to do that. >> >> But doesn't the issue at http://bugs.python.org/issue662923 imply that >> there *was* suitable code (for 2.4)? > > Yes, for bsddb. > >> So is this a regression? > > No. bsddb still supports iteration in 2.6. > >> And should that issue be re-opened? > > I don't think that would matter much. Actually contributing code might. > > Regards, > Martin > -- > http://mail.python.org/mailman/listinfo/python-list > From python.list at tim.thechases.com Sat Apr 11 10:17:20 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Sat, 11 Apr 2009 09:17:20 -0500 Subject: Definition of Pythonic? In-Reply-To: <200904111608.49505.emmanuel.surleau@gmail.com> References: <200904111608.49505.emmanuel.surleau@gmail.com> Message-ID: <49E0A670.9080906@tim.thechases.com> > Having written a few trivial scripts in Python, I'm curious as to how you > would sum up the Pythonic philosophy of development. Judging from Python, it > seems to exclude (mostly) "magical" variables like '$.'. Is this right? What > else would you include in this definition? At the python command-prompt, issue import this which will dump "The Zen of Python". I'd debate "flat vs. nested" depending on context (nested IFs? nested call-graph? nested data-structures?), but otherwise this summarizes many of the reasons I esteem Python above all the other computer languages I've used (pascal and VB came close; ruby & perl were so distant from these ideals, I cringe every time I have to touch them for anything). -tkc From skip at pobox.com Sat Apr 11 10:32:40 2009 From: skip at pobox.com (skip at pobox.com) Date: Sat, 11 Apr 2009 09:32:40 -0500 Subject: Definition of Pythonic? In-Reply-To: <200904111608.49505.emmanuel.surleau@gmail.com> References: <200904111608.49505.emmanuel.surleau@gmail.com> Message-ID: <18912.43528.441005.689965@montanaro.dyndns.org> Emm> Having written a few trivial scripts in Python, I'm curious as to Emm> how you would sum up the Pythonic philosophy of development. Try import this at your friendly, neighborhood Python prompt. -- Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/ "XML sucks, dictionaries rock" - Dave Beazley From carribeiro at gmail.com Sat Apr 11 10:35:59 2009 From: carribeiro at gmail.com (Carlos Ribeiro) Date: Sat, 11 Apr 2009 11:35:59 -0300 Subject: llvm vs. parrot In-Reply-To: <1239405385.6357.1.camel@linux-3eb6.site> References: <1239405385.6357.1.camel@linux-3eb6.site> Message-ID: <864d37090904110735x37626d5fo33df9b4441940ffc@mail.gmail.com> On Fri, Apr 10, 2009 at 20:16, Paul Watson wrote: > Is Parrot out of favor these days? It appears that Google is going to > use llvm. > As far as I can recall, Parrot was supposed to be an April Fools Day joke (literally) that was taken way too seriously. Parrot may had made a lot of sense years ago, but given today's compiler + language support + virtual machine technology, it seems that it has fallen behind. But that's just my $0.02 worth. -- Carlos Ribeiro Consultoria em Projetos blog: http://rascunhosrotos.blogspot.com blog: http://pythonnotes.blogspot.com mail: carribeiro at gmail.com mail: carribeiro at yahoo.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From google at mrabarnett.plus.com Sat Apr 11 10:36:13 2009 From: google at mrabarnett.plus.com (MRAB) Date: Sat, 11 Apr 2009 15:36:13 +0100 Subject: Merging byte arrays In-Reply-To: <49E0A2F1.7070809@dreams.sk> References: <49E0A2F1.7070809@dreams.sk> Message-ID: <49E0AADD.4040707@mrabarnett.plus.com> Gabriel wrote: > Hello > I'm using this function to read data in byte format from file > > def readBytes(file, offset, size): > file.seek(offset) > return file.read(size) > > file is opened with open function: > > file = open(path, "rb") > > then i'm using array.array('B', bytes) to parse read-out data, for > example in this function: > > def getIntValOfBytes(bytes): > value = i = 0 > for byte in array.array('B', bytes): > value |= byte << (8 * i) > i += 1 > return value > > when i read one portion of bytes everything works ok. but now i need > read from more locations in file, then somehow merge these data together > and then pass to getIntValOfBytes function.. > > i tried list > > list = [] > for offset in offsets > list.extend(readBytes(file, offset, size)) > > or string > > string = "" > for offset in offsets > string += readBytes(file, offset, size) > > but with list i get this error > > for byte in array.array('B', bytes): > TypeError: an integer is required > > in getIntValOfBytes (i passed the part of the list to the method, > list[x:y]) > That's because you're passing a list of strings. It should for a list of integers or a string. > and with string it just freeze.... > 'Freezes'? I've tried it with a string of 256 characters and it was fast. > I will appreciate any help.. > [I'm newbie so sorry if i'm missing something obvious] > From findrazor at gmail.com Sat Apr 11 10:51:56 2009 From: findrazor at gmail.com (Strato) Date: Sat, 11 Apr 2009 07:51:56 -0700 (PDT) Subject: Startup with Python Message-ID: <5be84f20-2ecb-485e-a075-4f0ee959e917@d7g2000prl.googlegroups.com> Hello, I am a beginner in Python , i am not able to set the environment variable in windows so that i can execute python script through command prompt , and also i am not able to male *.py as executable i.e. whenever i double click the file it should run it. Please help and reply me at findrazor at gmail.com . Its urgent brother , i will wait for your reply. From steve at REMOVE-THIS-cybersource.com.au Sat Apr 11 11:07:58 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 11 Apr 2009 15:07:58 GMT Subject: Pathological regular expression References: <967fe248-0e15-4247-9d36-6f3f5e0c5eb7@a7g2000yqk.googlegroups.com> Message-ID: <01f0a658$0$20620$c3e8da3@news.astraweb.com> On Thu, 09 Apr 2009 02:56:00 -0700, David Liang wrote: > Hi all, > I'm having a weird problem with a regular expression (tested in 2.6 and > 3.0): > > Basically, any of these: > _re_comments = re.compile(r'^(([^\\]+|\\.|"([^"\\]+|\\.)*")*)#.*$') > _re_comments = re.compile(r'^(([^#]+|\\.|"([^"\\]+|\\.)*")*)#.*$') > _re_comments = re.compile(r'^(([^"]+|\\.|"([^"\\]+|\\.)*")*)#.*$') > > followed by for example, > line = r'~/.[m]ozilla/firefox/*.default/chrome' > print(_re_comments.sub(r'\1', line)) > > ...hangs the interpreter. I can confirm the first one hangs the interpreter in Python 2.5 as well. I haven't tested the other two. To my mind, this is a bug in the RE engine. Is there any reason to not treat it as a bug? -- Steven From Scott.Daniels at Acm.Org Sat Apr 11 11:17:17 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sat, 11 Apr 2009 08:17:17 -0700 Subject: How to create a virtual serial port? In-Reply-To: <5fd78a41-ed64-4e3e-aa80-a89c8f44cf3f@v6g2000vbb.googlegroups.com> References: <_cCdndEvsrp8PULUnZ2dnUVZ_qydnZ2d@pdx.net> <5fd78a41-ed64-4e3e-aa80-a89c8f44cf3f@v6g2000vbb.googlegroups.com> Message-ID: Stuart Davenport wrote: > On 11 Apr, 08:52, Scott David Daniels wrote: >> Stuart Davenport wrote: >>> ... I'm on a OS X, python 2.5.... Then the GPS program I have >>> running on my MAC, RouteBuddy, can read the data from that serial port >>> as standard. I'ms confused by this statement. What physical connector does your "serial port" use to get the serial data to the Mac? I only have one 3-year old Mac laptop to look at, and I just don't see anything that I would call a serial port. > Scott, many thanks for the advice.... No problem. I was short earlier because I wanted to spend time helping someone out, not pulling clues out. If I have an hour to answer some questions, I'd rather read something that let's me hep out. I figured you might get a good answer if you asked something with full context, and there were _way_ too many variables to get you an answer from what you started with. --Scott David Daniels Scott.Daniels at Acm.Org From notvalid2 at sbcglobal.net Sat Apr 11 11:20:13 2009 From: notvalid2 at sbcglobal.net (W. eWatson) Date: Sat, 11 Apr 2009 08:20:13 -0700 Subject: Writing a "Raw" Image to a File (Win, PIL) Message-ID: I have an image of described as: Img Info: {} size: (640, 480) format: None mode: P palette: bands: ('P',) type: I'd like to write it to a file. Apparently, I need to convert it to a string first. How do I do that? Pickle? -- 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 From cmh.python at gmail.com Sat Apr 11 11:28:14 2009 From: cmh.python at gmail.com (Mike H) Date: Sat, 11 Apr 2009 11:28:14 -0400 Subject: Unsupported operand types in if/else list comprehension In-Reply-To: <74bdopF126tl6U1@mid.uni-berlin.de> References: <74bdopF126tl6U1@mid.uni-berlin.de> Message-ID: Ok, thanks again to everyone for their suggestions, even if it appears I was going down the wrong path at the start. I'm a grad student creating this database to hold some of my own research on an isolated server, so security, etc. isn't my biggest concern -- but I would like to do this right. Here's the code that I've come up with now. Although it's gotten away from the original question, those that have commented on this seem to have some SQL knowledge, so I'd like to run it by them to see if this is better in theory. (I've tried it and it works in practice!) FYI, I'm using MySQLdb to connect with the Database. Also, I realize I should probably add in some try/catch statements and other error handling... but this is what I have at the moment. def insert_cmd(myTable, myFields, myValues, myReturnKey): """Imports given fields and values into a given table, returns an SQL variable holding the Autoincrement key""" #tests to see if myParentKey is valid in mySQL. if not myReturnKey.startswith("@"): print "Error, myReturnKey must start with '@'"; sys.exit() SQLcmd="INSERT INTO " + myTable + " (%s) " % ", ".join(myFields) SQLcmd=SQLcmd + "VALUES (%s,%s,%s);" cursor.execute(SQLcmd, (myValues)) #sets and returns SQL variable. SQLcmd="select " + myReturnKey + ":=last_insert_id();" cursor.execute(SQLcmd) return myReturnKey On Sat, Apr 11, 2009 at 7:38 AM, Diez B. Roggisch wrote: > Mike H schrieb: >> >> Thanks to all of you. >> >> FYI, I'm doing this because I'm working on creating some insert >> statements in SQL, where string values need to be quoted, and integer >> values need to be unquoted. >> >> I wanted to be sure that I could pass these values to the list in a >> normal way e.g. ['test', 1, 'two'] and have a function correct the >> list for me, rather than calling the function with a strangely quoted >> list e.g. ['"'test'"', 1, '"'two'"'].> > > Don't do that yourself. This is error-prone. Instead, use the parametrized > verison of the cursor.execute-method. It will perform the necessary > escaping, and depending on the database and database adapter you use better > performance. > > Diez > -- > http://mail.python.org/mailman/listinfo/python-list > From google at mrabarnett.plus.com Sat Apr 11 11:38:58 2009 From: google at mrabarnett.plus.com (MRAB) Date: Sat, 11 Apr 2009 16:38:58 +0100 Subject: Pathological regular expression In-Reply-To: <01f0a658$0$20620$c3e8da3@news.astraweb.com> References: <967fe248-0e15-4247-9d36-6f3f5e0c5eb7@a7g2000yqk.googlegroups.com> <01f0a658$0$20620$c3e8da3@news.astraweb.com> Message-ID: <49E0B992.4080407@mrabarnett.plus.com> Steven D'Aprano wrote: > On Thu, 09 Apr 2009 02:56:00 -0700, David Liang wrote: > >> Hi all, >> I'm having a weird problem with a regular expression (tested in 2.6 and >> 3.0): >> >> Basically, any of these: >> _re_comments = re.compile(r'^(([^\\]+|\\.|"([^"\\]+|\\.)*")*)#.*$') >> _re_comments = re.compile(r'^(([^#]+|\\.|"([^"\\]+|\\.)*")*)#.*$') >> _re_comments = re.compile(r'^(([^"]+|\\.|"([^"\\]+|\\.)*")*)#.*$') >> >> followed by for example, >> line = r'~/.[m]ozilla/firefox/*.default/chrome' >> print(_re_comments.sub(r'\1', line)) >> >> ...hangs the interpreter. > > > I can confirm the first one hangs the interpreter in Python 2.5 as well. > I haven't tested the other two. > > To my mind, this is a bug in the RE engine. Is there any reason to not > treat it as a bug? > It's not a bug but one of those pathological cases where there are a LOT of possibilities to try (we're talking about exponential growth here). Hmm, maybe the re module needs to let the user set a timeout control that raises an exception if it takes longer than n seconds (it can already be interrupted by ^C)... From sjmachin at lexicon.net Sat Apr 11 11:40:03 2009 From: sjmachin at lexicon.net (John Machin) Date: Sat, 11 Apr 2009 08:40:03 -0700 (PDT) Subject: Pathological regular expression References: <967fe248-0e15-4247-9d36-6f3f5e0c5eb7@a7g2000yqk.googlegroups.com> <01f0a658$0$20620$c3e8da3@news.astraweb.com> Message-ID: <042e5adf-1aff-4baa-bcef-78d69bc36e32@d38g2000prn.googlegroups.com> On Apr 12, 1:07?am, Steven D'Aprano wrote: > On Thu, 09 Apr 2009 02:56:00 -0700, David Liang wrote: > > Hi all, > > I'm having a weird problem with a regular expression (tested in 2.6 and > > 3.0): > > > Basically, any of these: > > _re_comments = re.compile(r'^(([^\\]+|\\.|"([^"\\]+|\\.)*")*)#.*$') > > _re_comments = re.compile(r'^(([^#]+|\\.|"([^"\\]+|\\.)*")*)#.*$') > > _re_comments = re.compile(r'^(([^"]+|\\.|"([^"\\]+|\\.)*")*)#.*$') > > > followed by for example, > > line = r'~/.[m]ozilla/firefox/*.default/chrome' > > print(_re_comments.sub(r'\1', line)) > > > ...hangs the interpreter. > > I can confirm the first one hangs the interpreter in Python 2.5 as well. > I haven't tested the other two. > > To my mind, this is a bug in the RE engine. Is there any reason to not > treat it as a bug? IMHO it's not a bug -- s/hang/takes a long time to compute/ Just look at it: 2 + operators and 3 * operators ... It's one of those "come back after lunch" REs. From deets at nospam.web.de Sat Apr 11 11:42:51 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sat, 11 Apr 2009 17:42:51 +0200 Subject: Writing a "Raw" Image to a File (Win, PIL) In-Reply-To: References: Message-ID: <74bs3sF12mi0dU1@mid.uni-berlin.de> W. eWatson schrieb: > I have an image of described as: > Img Info: {} > size: (640, 480) > format: None > mode: P > palette: > bands: ('P',) > type: > > I'd like to write it to a file. Apparently, I need to convert it to a > string first. How do I do that? Pickle? Did you bother reading the PIL documentation just for about 30seconds? http://www.pythonware.com/library/pil/handbook/image.htm Hint: look for "save". Diez From aahz at pythoncraft.com Sat Apr 11 11:45:17 2009 From: aahz at pythoncraft.com (Aahz) Date: 11 Apr 2009 08:45:17 -0700 Subject: Definition of Pythonic? References: Message-ID: In article , Emmanuel Surleau wrote: > >Having written a few trivial scripts in Python, I'm curious as to how you >would sum up the Pythonic philosophy of development. Judging from Python, it >seems to exclude (mostly) "magical" variables like '$.'. Is this right? What >else would you include in this definition? python -m this -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ Why is this newsgroup different from all other newsgroups? From dotancohen at gmail.com Sat Apr 11 11:48:50 2009 From: dotancohen at gmail.com (Dotan Cohen) Date: Sat, 11 Apr 2009 18:48:50 +0300 Subject: Pathological regular expression In-Reply-To: <042e5adf-1aff-4baa-bcef-78d69bc36e32@d38g2000prn.googlegroups.com> References: <967fe248-0e15-4247-9d36-6f3f5e0c5eb7@a7g2000yqk.googlegroups.com> <01f0a658$0$20620$c3e8da3@news.astraweb.com> <042e5adf-1aff-4baa-bcef-78d69bc36e32@d38g2000prn.googlegroups.com> Message-ID: <880dece00904110848u22df102fxda20e8e23e1762f7@mail.gmail.com> > IMHO it's not a bug -- s/hang/takes a long time to compute/ > ?That is quite what a hang is, and why the timeout was invented. The real bug is that there is no timeout mechanism. > Just look at it: 2 + operators and 3 * operators ... It's one of those > "come back after lunch" REs. > Some users would not be able to tell that from the beginning. While a timeout override would be necessary in these cases, a timeout should be set. -- Dotan Cohen http://what-is-what.com http://gibberish.co.il From cmh.python at gmail.com Sat Apr 11 11:53:58 2009 From: cmh.python at gmail.com (Mike H) Date: Sat, 11 Apr 2009 11:53:58 -0400 Subject: Unsupported operand types in if/else list comprehension In-Reply-To: References: <74bdopF126tl6U1@mid.uni-berlin.de> Message-ID: Well, I'm an idiot. Obviously, the line "VALUES (%s, %s, %s);" needs to be modified to adapt for the number of arguments in the list. But otherwise.... On Sat, Apr 11, 2009 at 11:28 AM, Mike H wrote: > Ok, thanks again to everyone for their suggestions, even if it appears > I was going down the wrong path at the start. I'm a grad student > creating this database to hold some of my own research on an isolated > server, so security, etc. isn't my biggest concern -- but I would like > to do this right. Here's the code that I've come up with now. Although > it's gotten away from the original question, those that have commented > on this seem to have some SQL knowledge, so I'd like to run it by them > to see if this is better in theory. (I've tried it and it works in > practice!) > > FYI, I'm using MySQLdb to connect with the Database. > > Also, I realize I should probably add in some try/catch statements and > other error handling... but this is what I have at the moment. > > > > def insert_cmd(myTable, myFields, myValues, myReturnKey): > ? ?"""Imports given fields and values into a given table, returns an > SQL variable holding the Autoincrement key""" > > ? ?#tests to see if myParentKey is valid in mySQL. > ? ?if not myReturnKey.startswith("@"): print "Error, myReturnKey must > start with '@'"; sys.exit() > > ? ?SQLcmd="INSERT INTO " + myTable + " (%s) " % ", ".join(myFields) > ? ?SQLcmd=SQLcmd + "VALUES (%s,%s,%s);" > ? ?cursor.execute(SQLcmd, (myValues)) > > ? ?#sets and returns SQL variable. > ? ?SQLcmd="select " + myReturnKey + ":=last_insert_id();" > ? ?cursor.execute(SQLcmd) > ? ?return myReturnKey > > > On Sat, Apr 11, 2009 at 7:38 AM, Diez B. Roggisch wrote: >> Mike H schrieb: >>> >>> Thanks to all of you. >>> >>> FYI, I'm doing this because I'm working on creating some insert >>> statements in SQL, where string values need to be quoted, and integer >>> values need to be unquoted. >>> >>> I wanted to be sure that I could pass these values to the list in a >>> normal way e.g. ['test', 1, 'two'] and have a function correct the >>> list for me, rather than calling the function with a strangely quoted >>> list e.g. ['"'test'"', 1, '"'two'"'].> >> >> Don't do that yourself. This is error-prone. Instead, use the parametrized >> verison of the cursor.execute-method. It will perform the necessary >> escaping, and depending on the database and database adapter you use better >> performance. >> >> Diez >> -- >> http://mail.python.org/mailman/listinfo/python-list >> > From gallium.arsenide at gmail.com Sat Apr 11 12:00:58 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Sat, 11 Apr 2009 09:00:58 -0700 (PDT) Subject: Definition of Pythonic? References: Message-ID: <7026142e-5f9c-49bf-9fee-c3859caf3430@x6g2000vbg.googlegroups.com> On Apr 11, 10:08?am, Emmanuel Surleau wrote: > Having written a few trivial scripts in Python, I'm curious as > to how you would sum up the Pythonic philosophy of development. A couple of others have already mentioned the Zen of Python, available at the Python command prompt. I would agree with that, but also add the caveat that none of the principles expressed there are hard-and- fast rules. Hopefully that is clear from the quasi-contradictory nature of the principles, but inevitably there will be some people who complain that Python breaks this or that "rule" from the Zen. The fact is, it's impossible to satisfy every principle in every situation. To me, Python distinguishes itself for how well it balances all of them. "Compromise" is a word that comes up a lot when talking about the design of Python. To some, that has a negative connotation; to me, it's an inevitable consequence of being practical. John From ebonak at hotmail.com Sat Apr 11 12:08:58 2009 From: ebonak at hotmail.com (Esmail) Date: Sat, 11 Apr 2009 12:08:58 -0400 Subject: Startup with Python In-Reply-To: <5be84f20-2ecb-485e-a075-4f0ee959e917@d7g2000prl.googlegroups.com> References: <5be84f20-2ecb-485e-a075-4f0ee959e917@d7g2000prl.googlegroups.com> Message-ID: <49E0C09A.1000209@hotmail.com> Strato wrote: > Hello, I am a beginner in Python , i am not able to set the > environment variable in windows so that i can execute python script > through command prompt , and also i am not able to male *.py as > executable i.e. whenever i double click the file it should run it. > Please help and reply me at findrazor at gmail.com . Its urgent brother , > i will wait for your reply. > -- > http://mail.python.org/mailman/listinfo/python-list > Take a look at http://docs.python.org/using/index.html especially section 3 talks about how to setup Python on the Windows platform. Have you tried specifying the whole path in the command window? For instance I have version 2.6 installed, so I can type c:\Python26\python.exe script.py to get this to work. Good luck, Esmail ps: try renaming the .py extension to .pyw and then double click it (should have worked before too - as far as I know this only prevents a command window from opening, but it can't hurt trying). From ebonak at hotmail.com Sat Apr 11 12:08:58 2009 From: ebonak at hotmail.com (Esmail) Date: Sat, 11 Apr 2009 12:08:58 -0400 Subject: Startup with Python In-Reply-To: <5be84f20-2ecb-485e-a075-4f0ee959e917@d7g2000prl.googlegroups.com> References: <5be84f20-2ecb-485e-a075-4f0ee959e917@d7g2000prl.googlegroups.com> Message-ID: <49E0C09A.1000209@hotmail.com> Strato wrote: > Hello, I am a beginner in Python , i am not able to set the > environment variable in windows so that i can execute python script > through command prompt , and also i am not able to male *.py as > executable i.e. whenever i double click the file it should run it. > Please help and reply me at findrazor at gmail.com . Its urgent brother , > i will wait for your reply. > -- > http://mail.python.org/mailman/listinfo/python-list > Take a look at http://docs.python.org/using/index.html especially section 3 talks about how to setup Python on the Windows platform. Have you tried specifying the whole path in the command window? For instance I have version 2.6 installed, so I can type c:\Python26\python.exe script.py to get this to work. Good luck, Esmail ps: try renaming the .py extension to .pyw and then double click it (should have worked before too - as far as I know this only prevents a command window from opening, but it can't hurt trying). From emile at fenx.com Sat Apr 11 12:09:01 2009 From: emile at fenx.com (Emile van Sebille) Date: Sat, 11 Apr 2009 09:09:01 -0700 Subject: Startup with Python In-Reply-To: <5be84f20-2ecb-485e-a075-4f0ee959e917@d7g2000prl.googlegroups.com> References: <5be84f20-2ecb-485e-a075-4f0ee959e917@d7g2000prl.googlegroups.com> Message-ID: <49E0C09D.7080808@fenx.com> Strato wrote: > Hello, I am a beginner in Python , i am not able to set the > environment variable in windows so that i can execute python script > through command prompt , and also i am not able to male *.py as > executable i.e. whenever i double click the file it should run it. Download and install the activestate distribution -- here's the link http://downloads.activestate.com/ActivePython/windows/2.6/ActivePython-2.6.1.1-win32-x86.msi HTH, Emile > Please help and reply me at findrazor at gmail.com . Its urgent brother , > i will wait for your reply. > -- > http://mail.python.org/mailman/listinfo/python-list > From notvalid2 at sbcglobal.net Sat Apr 11 12:20:33 2009 From: notvalid2 at sbcglobal.net (W. eWatson) Date: Sat, 11 Apr 2009 09:20:33 -0700 Subject: Writing a "Raw" Image to a File (Win, PIL) In-Reply-To: <74bs3sF12mi0dU1@mid.uni-berlin.de> References: <74bs3sF12mi0dU1@mid.uni-berlin.de> Message-ID: Diez B. Roggisch wrote: > W. eWatson schrieb: >> I have an image of described as: >> Img Info: {} >> size: (640, 480) >> format: None >> mode: P >> palette: >> bands: ('P',) >> type: >> >> I'd like to write it to a file. Apparently, I need to convert it to a >> string first. How do I do that? Pickle? > > Did you bother reading the PIL documentation just for about 30seconds? > > http://www.pythonware.com/library/pil/handbook/image.htm > > Hint: look for "save". > > Diez So, you are telling me what? To save it as a jpg file, or maybe a bmp file? All of them have headers, right? -- 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 google at mrabarnett.plus.com Sat Apr 11 12:23:43 2009 From: google at mrabarnett.plus.com (MRAB) Date: Sat, 11 Apr 2009 17:23:43 +0100 Subject: Definition of Pythonic? In-Reply-To: <7026142e-5f9c-49bf-9fee-c3859caf3430@x6g2000vbg.googlegroups.com> References: <7026142e-5f9c-49bf-9fee-c3859caf3430@x6g2000vbg.googlegroups.com> Message-ID: <49E0C40F.8030400@mrabarnett.plus.com> John Yeung wrote: > On Apr 11, 10:08 am, Emmanuel Surleau > wrote: >> Having written a few trivial scripts in Python, I'm curious as >> to how you would sum up the Pythonic philosophy of development. > > A couple of others have already mentioned the Zen of Python, available > at the Python command prompt. I would agree with that, but also add > the caveat that none of the principles expressed there are hard-and- > fast rules. "Special cases aren't special enough to break the rules. Although practicality beats purity." This also applies to the Zen itself. > Hopefully that is clear from the quasi-contradictory > nature of the principles, but inevitably there will be some people who > complain that Python breaks this or that "rule" from the Zen. > > The fact is, it's impossible to satisfy every principle in every > situation. To me, Python distinguishes itself for how well it > balances all of them. "Compromise" is a word that comes up a lot when > talking about the design of Python. To some, that has a negative > connotation; to me, it's an inevitable consequence of being practical. > From google at mrabarnett.plus.com Sat Apr 11 12:27:17 2009 From: google at mrabarnett.plus.com (MRAB) Date: Sat, 11 Apr 2009 17:27:17 +0100 Subject: Pathological regular expression In-Reply-To: <880dece00904110848u22df102fxda20e8e23e1762f7@mail.gmail.com> References: <967fe248-0e15-4247-9d36-6f3f5e0c5eb7@a7g2000yqk.googlegroups.com> <01f0a658$0$20620$c3e8da3@news.astraweb.com> <042e5adf-1aff-4baa-bcef-78d69bc36e32@d38g2000prn.googlegroups.com> <880dece00904110848u22df102fxda20e8e23e1762f7@mail.gmail.com> Message-ID: <49E0C4E5.60904@mrabarnett.plus.com> Dotan Cohen wrote: >> IMHO it's not a bug -- s/hang/takes a long time to compute/ >> > > ?That is quite what a hang is, and why the timeout was invented. The > real bug is that there is no timeout mechanism. > I wouldn't call it a "hang" because it is actually doing work. If it was 'stuck' on a certain part of the text and not progressing then it would be a hang. >> Just look at it: 2 + operators and 3 * operators ... It's one of those >> "come back after lunch" REs. >> > > Some users would not be able to tell that from the beginning. While a > timeout override would be necessary in these cases, a timeout should > be set. > From castironpi at gmail.com Sat Apr 11 12:29:15 2009 From: castironpi at gmail.com (Aaron Brady) Date: Sat, 11 Apr 2009 09:29:15 -0700 (PDT) Subject: safe eval of moderately simple math expressions References: <01efde5e$0$20620$c3e8da3@news.astraweb.com> <734a562a-b524-46d2-b1e6-30085c5e8f8a@o18g2000vbi.googlegroups.com> Message-ID: <90b50a3a-4b07-4ed7-a3da-957eaf4870d4@t11g2000vbc.googlegroups.com> On Apr 11, 8:09?am, Paul McGuire wrote: > On Apr 11, 2:41?am, Aaron Brady wrote: > > > > > Why do I get the feeling that the authors of 'pyparsing' are out of > > breath? > > What kind of breathlessness do you mean? ?I'm still breathing, last > time I checked. > > The-rumors-of-my-demise-have-been-greatly-exaggerated'ly yours, > -- Paul Gasping, not panting. From gagsl-py2 at yahoo.com.ar Sat Apr 11 12:32:30 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sat, 11 Apr 2009 13:32:30 -0300 Subject: Can't create list of dictionaries References: <47642810-6388-4c27-aeca-8b32830575a2@37g2000yqp.googlegroups.com> <6971a8c6-fcfb-42a1-ba20-b63209817d36@c9g2000yqm.googlegroups.com> Message-ID: > On Apr 10, 12:36?pm, sophie_newbie wrote: >> >> I've got a function that returns a dictionary, I need to loop and >> return 1000 dictionaries and append them to a list, but the thing is >> that when I do the list.append(funtThatReturnsDict()) the resulting >> only ever has 1 dictionary attached to it, even after running the >> append function 1000 times! >> >> I've tried using dict.copy() on the dictionary that was returned from >> the function but this didn't work either. En Fri, 10 Apr 2009 08:49:12 -0300, sophie_newbie escribi?: > Scratch everything I said, copy() does work. Made a wee mistake > somewhere else. If the function is supposed to return a *different* dictionary each time, ensure that, don't "fix" the result value after it has returned. -- Gabriel Genellina From thudfoo at opensuse.us Sat Apr 11 12:33:05 2009 From: thudfoo at opensuse.us (member thudfoo) Date: Sat, 11 Apr 2009 09:33:05 -0700 Subject: ANN: PyGUI 2.0 In-Reply-To: <49E07ABB.7070803@canterbury.ac.nz> References: <49E07ABB.7070803@canterbury.ac.nz> Message-ID: <3d881a310904110933u17556674jba8bb0950340f512@mail.gmail.com> Line 25 of setup.py should be: packages.append("GUI.Gtk") From google at mrabarnett.plus.com Sat Apr 11 12:38:57 2009 From: google at mrabarnett.plus.com (MRAB) Date: Sat, 11 Apr 2009 17:38:57 +0100 Subject: Writing a "Raw" Image to a File (Win, PIL) In-Reply-To: References: Message-ID: <49E0C7A1.40506@mrabarnett.plus.com> W. eWatson wrote: > I have an image of described as: > Img Info: {} > size: (640, 480) > format: None > mode: P > palette: > bands: ('P',) > type: > > I'd like to write it to a file. Apparently, I need to convert it to a > string first. How do I do that? Pickle? Have you tried the .tostring() method? From exarkun at divmod.com Sat Apr 11 12:50:53 2009 From: exarkun at divmod.com (Jean-Paul Calderone) Date: Sat, 11 Apr 2009 12:50:53 -0400 Subject: Async serial communication/threads sharing data In-Reply-To: <49cb0923$0$1663$742ec2ed@news.sonic.net> Message-ID: <20090411165053.24697.1003629954.divmod.quotient.6031@henry.divmod.com> On Wed, 25 Mar 2009 22:23:25 -0700, John Nagle wrote: >Jean-Paul Calderone wrote: >>On Tue, 24 Mar 2009 22:20:49 -0700, John Nagle wrote: >>>Jean-Paul Calderone wrote: >>>>On Mon, 23 Mar 2009 05:30:04 -0500, Nick Craig-Wood >>>>wrote: >>>>>Jean-Paul Calderone wrote: >>>>>[snip] >>> After bringing in all the heavy machinery of Twisted, >>>you're still polling at 10Hz. That's disappointing. >> >>Hmm, no? There's no polling in the latest version of the code I saw. >> >>Jean-Paul > > if too_full: > reactor.callLater(0.1, self.throttle) > Hi John, You're looking at a different version of the code than what I was talking about. After discussing things with Nick, we came up with a version which includes no polling. See Nick's message of March 23, at around 1:30 PM for the code I was referring to - a version of the app which does no polling. Jean-Paul From castironpi at gmail.com Sat Apr 11 12:54:10 2009 From: castironpi at gmail.com (Aaron Brady) Date: Sat, 11 Apr 2009 09:54:10 -0700 (PDT) Subject: Pathological regular expression References: <967fe248-0e15-4247-9d36-6f3f5e0c5eb7@a7g2000yqk.googlegroups.com> <01f0a658$0$20620$c3e8da3@news.astraweb.com> Message-ID: <16ec1004-9cb8-447a-902a-f28c71421b1e@n17g2000vba.googlegroups.com> On Apr 11, 10:07?am, Steven D'Aprano wrote: > On Thu, 09 Apr 2009 02:56:00 -0700, David Liang wrote: > > Hi all, > > I'm having a weird problem with a regular expression (tested in 2.6 and > > 3.0): > > > Basically, any of these: > > _re_comments = re.compile(r'^(([^\\]+|\\.|"([^"\\]+|\\.)*")*)#.*$') > > _re_comments = re.compile(r'^(([^#]+|\\.|"([^"\\]+|\\.)*")*)#.*$') > > _re_comments = re.compile(r'^(([^"]+|\\.|"([^"\\]+|\\.)*")*)#.*$') > > > followed by for example, > > line = r'~/.[m]ozilla/firefox/*.default/chrome' > > print(_re_comments.sub(r'\1', line)) > > > ...hangs the interpreter. > > I can confirm the first one hangs the interpreter in Python 2.5 as well. > I haven't tested the other two. > > To my mind, this is a bug in the RE engine. Is there any reason to not > treat it as a bug? > > -- > Steven How long does it take on a short example? From notvalid2 at sbcglobal.net Sat Apr 11 12:57:51 2009 From: notvalid2 at sbcglobal.net (W. eWatson) Date: Sat, 11 Apr 2009 09:57:51 -0700 Subject: Writing a "Raw" Image to a File (Win, PIL) In-Reply-To: References: Message-ID: MRAB wrote: > W. eWatson wrote: >> I have an image of described as: >> Img Info: {} >> size: (640, 480) >> format: None >> mode: P >> palette: >> bands: ('P',) >> type: >> >> I'd like to write it to a file. Apparently, I need to convert it to a >> string first. How do I do that? Pickle? > > Have you tried the .tostring() method? Perfect. Works exactly like I had hoped, 640x480 bytes--nothing more. Thanks. I had noticed it, but didn't get the connection. Then I wandered around nearby in the PIL description, and noticed something about decoding and write. Unknown territory to me. However, if I had followed up there was a connection between tostring and "raw" decoding. -- 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 aahz at pythoncraft.com Sat Apr 11 12:59:57 2009 From: aahz at pythoncraft.com (Aahz) Date: 11 Apr 2009 09:59:57 -0700 Subject: Multithreading / multiprocess References: <57065c62-2024-47b5-a07e-1d60ff85ba23@y10g2000prc.googlegroups.com> Message-ID: In article <57065c62-2024-47b5-a07e-1d60ff85ba23 at y10g2000prc.googlegroups.com>, tleeuwenburg at gmail.com wrote: > >Is there anyway to begin a thread and execute a finite number of lines >of code, or a finite amount of time within it? > >For example, say I create three child threads and I want to guarantee >equal timeshare between them, can I specify a quanta (say 400 LOC >although I know that is pretty small) to execute in each one in turn? You have extremely coarse-grained control with sys.setcheckinterval(). However, there is no guarantee which thread will pick up control after each context switch, so one thread might get more than its share. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ Why is this newsgroup different from all other newsgroups? From grante at visi.com Sat Apr 11 13:05:21 2009 From: grante at visi.com (Grant Edwards) Date: Sat, 11 Apr 2009 12:05:21 -0500 Subject: How to create a virtual serial port? References: <_cCdndEvsrp8PULUnZ2dnUVZ_qydnZ2d@pdx.net> <5fd78a41-ed64-4e3e-aa80-a89c8f44cf3f@v6g2000vbb.googlegroups.com> Message-ID: On 2009-04-11, Scott David Daniels wrote: >>>> ... I'm on a OS X, python 2.5.... Then the GPS program I have >>>> running on my MAC, RouteBuddy, can read the data from that >>>> serial port as standard. > I'ms confused by this statement. What physical connector does > your "serial port" use to get the serial data to the Mac? The data comes in on the TCP/IP network. The OP wants to send that data to a program called RouteBuddy. RouteBuddy will only read data from a serial port. The phrase "that serial port" in the text which you quote refers to the virtual serial port that the elided text says the OP wants to create. The OP wants what is called a "serial port redirector" in Windows-speak. It's a program that routes data between a network connection (often a TCP/IP connection that implements RFC2217) and what appears to other programs to be a normal serial port device API. You can write a port redirector in user-space in MS-Windows, but you can't in Linux/Unix. On Unix systems you have to write a kernel module that sits below the tty layer. The tty layer is what user applications talk to with open/close/read/write/ioctl calls. -- Grant From aahz at pythoncraft.com Sat Apr 11 13:10:17 2009 From: aahz at pythoncraft.com (Aahz) Date: 11 Apr 2009 10:10:17 -0700 Subject: sharing/swapping items between lists References: <4fd78ac3-ba83-456b-b768-3a0043548565@f19g2000vbf.googlegroups.com> Message-ID: In article <4fd78ac3-ba83-456b-b768-3a0043548565 at f19g2000vbf.googlegroups.com>, Ross wrote: > >I'm trying to design an iterator that produces two lists. The first >list will be a list of unique pairings and the second will be a list >of items that weren't used in the first list. After each round, the >items that weren't used in the round before will get put back in and >the second list will be populated with unique items. How do you specify what goes into the first list? Based on your description, I would have expected that the output from the first iteration would be ( [(1,2),(3,4),(5,6)], [7,8] ) Regardless of the actual algorithm, if you are returning items one at a time and maintaining state in a computation, you probably want to use a generator. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ Why is this newsgroup different from all other newsgroups? From steve at REMOVE-THIS-cybersource.com.au Sat Apr 11 13:40:33 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 11 Apr 2009 17:40:33 GMT Subject: Pathological regular expression References: <967fe248-0e15-4247-9d36-6f3f5e0c5eb7@a7g2000yqk.googlegroups.com> <01f0a658$0$20620$c3e8da3@news.astraweb.com> <042e5adf-1aff-4baa-bcef-78d69bc36e32@d38g2000prn.googlegroups.com> Message-ID: <01f0ca1b$0$20620$c3e8da3@news.astraweb.com> On Sat, 11 Apr 2009 08:40:03 -0700, John Machin wrote: >> To my mind, this is a bug in the RE engine. Is there any reason to not >> treat it as a bug? > > IMHO it's not a bug -- s/hang/takes a long time to compute/ > > Just look at it: 2 + operators and 3 * operators ... It's one of those > "come back after lunch" REs. Well, it's been running now for about two and a half hours, that's a rather long lunch. And despite MRAB's assertion, it *cannot* be interrupted by ctrl-C. That means that to all intents and purposes, the interpreter has locked up for the duration of the calculation, which may be days or weeks for all I know. -- Steven From aahz at pythoncraft.com Sat Apr 11 13:42:00 2009 From: aahz at pythoncraft.com (Aahz) Date: 11 Apr 2009 10:42:00 -0700 Subject: Reading 3 objects at a time from list References: <49e06774$0$700$5fc30a8@news.tiscali.it> Message-ID: In article <49e06774$0$700$5fc30a8 at news.tiscali.it>, Francesco Bochicchio wrote: >> On Sat, Apr 11, 2009 at 1:44 AM, Matteo wrote: >>> >>> I need to pass the numbers to a function, but three at a time, until >>> the string ends. The strings are of variable length, but always a >>> multiple of three. > >I would do that with a generator: > > >>> def groups(l,n) : >... while l: yield l[:n]; l=l[n:] >... Unfortunately, that's O(N**2) albeit with an extremely small constant factor, because popping off the head of the list requires a full list copy. You're probably okay with this algorithm unless the string could be megabytes. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ Why is this newsgroup different from all other newsgroups? From dotancohen at gmail.com Sat Apr 11 13:55:25 2009 From: dotancohen at gmail.com (Dotan Cohen) Date: Sat, 11 Apr 2009 20:55:25 +0300 Subject: Pathological regular expression In-Reply-To: <01f0ca1b$0$20620$c3e8da3@news.astraweb.com> References: <967fe248-0e15-4247-9d36-6f3f5e0c5eb7@a7g2000yqk.googlegroups.com> <01f0a658$0$20620$c3e8da3@news.astraweb.com> <042e5adf-1aff-4baa-bcef-78d69bc36e32@d38g2000prn.googlegroups.com> <01f0ca1b$0$20620$c3e8da3@news.astraweb.com> Message-ID: <880dece00904111055j2d192bc2ua41c4983a8b7756c@mail.gmail.com> > Well, it's been running now for about two and a half hours, that's a > rather long lunch. I'd also like a pony! -- Dotan Cohen http://what-is-what.com http://gibberish.co.il From grante at visi.com Sat Apr 11 13:56:44 2009 From: grante at visi.com (Grant Edwards) Date: Sat, 11 Apr 2009 12:56:44 -0500 Subject: How to create a virtual serial port? References: <_cCdndEvsrp8PULUnZ2dnUVZ_qydnZ2d@pdx.net> <5fd78a41-ed64-4e3e-aa80-a89c8f44cf3f@v6g2000vbb.googlegroups.com> Message-ID: On 2009-04-11, Grant Edwards wrote: > You can write a port redirector in user-space in MS-Windows, > but you can't in Linux/Unix. On Unix systems you have to > write a kernel module that sits below the tty layer. Perhaps I should elucidate further. That's what the "pty" driver on Unix is: a kernel module that sits underneath the tty layer where the "normal" serial-port UART drivers sit. However, Unix pty drivers only handles a subset of the normal serial-port API. [I'm not sure why pty drivers have never been "finished" so that they fully emulate a serial port, but it's been that way for 20+ years]. If RouteBuddy doesn't try to do things like get/set modem control/status lines, then the OP might be able to use a pty. Each pty consists of two devices: a master end and a slave end. RouteBuddy would be told to use the slave end, and the OP would write a program that would transfer data between a network connection and the master end. A pty devices is what is used by programs like xterm to run programs like bash. Xterm transfers data between a network connection and the master end of a pty. Bash is connected to the slave end of that pty and "thinks" it's connected to a serial port. Bash uses little of the serial port API, so it's happy with the limited API provided by a pty slave-end. -- Grant From dsdale24 at gmail.com Sat Apr 11 14:15:03 2009 From: dsdale24 at gmail.com (Darren Dale) Date: Sat, 11 Apr 2009 11:15:03 -0700 (PDT) Subject: design question, metaclasses? Message-ID: <78c22f6f-229c-4115-a390-4f192b5b5849@t11g2000vbc.googlegroups.com> I am working on a project that provides a high level interface to hdf5 files by implementing a thin wrapper around h5py. I would like to generalize the project so the same API can be used with other formats, like netcdf or ascii files. The format specific code exists in File, Group and Dataset classes, which I could reimplement for each format. But there are other classes deriving from Group and Dataset which do not contain any format-specific code, and I would like to find a way to implement the functionality once and apply uniformly across supported formats. This is really abstract, but I was thinking of something along the lines of: format1.Group # implementation of group in format1 format2.Group # ... Base.DerivedGroup # base implementation of DerivedGroup, not directly useful format1.DerivedGroup = Base.DerivedGroup(format1.Group) # useful format2.DerivedGroup = Base.DerivedGroup(format2.Group) # useful Could anyone please offer a comment, is this an appropriate use of metaclassing, or is there maybe an easier/better alternative? From emmanuel.surleau at gmail.com Sat Apr 11 14:17:28 2009 From: emmanuel.surleau at gmail.com (Emmanuel Surleau) Date: Sat, 11 Apr 2009 20:17:28 +0200 Subject: Definition of Pythonic? In-Reply-To: <7026142e-5f9c-49bf-9fee-c3859caf3430@x6g2000vbg.googlegroups.com> References: <7026142e-5f9c-49bf-9fee-c3859caf3430@x6g2000vbg.googlegroups.com> Message-ID: <200904112017.29143.emmanuel.surleau@gmail.com> On Saturday 11 April 2009 18:00:58 John Yeung wrote: > On Apr 11, 10:08 am, Emmanuel Surleau > > wrote: > > Having written a few trivial scripts in Python, I'm curious as > > to how you would sum up the Pythonic philosophy of development. > > A couple of others have already mentioned the Zen of Python, available > at the Python command prompt. I would agree with that, but also add > the caveat that none of the principles expressed there are hard-and- > fast rules. Hopefully that is clear from the quasi-contradictory > nature of the principles, but inevitably there will be some people who > complain that Python breaks this or that "rule" from the Zen. Thank you all for the nice and prompt replies to what is a typical newbie question, which is bound to come back regularly. And no, you can't make everybody happy, per definition. > The fact is, it's impossible to satisfy every principle in every > situation. To me, Python distinguishes itself for how well it > balances all of them. "Compromise" is a word that comes up a lot when > talking about the design of Python. To some, that has a negative > connotation; to me, it's an inevitable consequence of being practical. That's fine with me: after all, you can't do software engineering without trade-offs. Cheers, Emm From castironpi at gmail.com Sat Apr 11 14:22:06 2009 From: castironpi at gmail.com (Aaron Brady) Date: Sat, 11 Apr 2009 11:22:06 -0700 (PDT) Subject: Pathological regular expression References: <967fe248-0e15-4247-9d36-6f3f5e0c5eb7@a7g2000yqk.googlegroups.com> <01f0a658$0$20620$c3e8da3@news.astraweb.com> <042e5adf-1aff-4baa-bcef-78d69bc36e32@d38g2000prn.googlegroups.com> <01f0ca1b$0$20620$c3e8da3@news.astraweb.com> Message-ID: <661eeb83-1f45-4416-aac2-ba2314d8cbcf@e24g2000vbe.googlegroups.com> On Apr 11, 12:40?pm, Steven D'Aprano wrote: > On Sat, 11 Apr 2009 08:40:03 -0700, John Machin wrote: > >> To my mind, this is a bug in the RE engine. Is there any reason to not > >> treat it as a bug? > > > IMHO it's not a bug -- s/hang/takes a long time to compute/ > > > Just look at it: 2 + operators and 3 * operators ... It's one of those > > "come back after lunch" REs. > > Well, it's been running now for about two and a half hours, that's a > rather long lunch. And despite MRAB's assertion, it *cannot* be > interrupted by ctrl-C. That means that to all intents and purposes, the > interpreter has locked up for the duration of the calculation, which may > be days or weeks for all I know. While beyond the limits of a long lunch, that would make a nice vacation? From Scott.Daniels at Acm.Org Sat Apr 11 14:28:46 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sat, 11 Apr 2009 11:28:46 -0700 Subject: How to create a virtual serial port? In-Reply-To: References: <_cCdndEvsrp8PULUnZ2dnUVZ_qydnZ2d@pdx.net> <5fd78a41-ed64-4e3e-aa80-a89c8f44cf3f@v6g2000vbb.googlegroups.com> Message-ID: <6LGdnSbyPsL1fX3UnZ2dnUVZ_uqdnZ2d@pdx.net> Grant Edwards wrote: > On 2009-04-11, Grant Edwards wrote: > >> You can write a port redirector in user-space in MS-Windows, >> but you can't in Linux/Unix. On Unix systems you have to >> write a kernel module that sits below the tty layer. > > Perhaps I should elucidate further. This part I actually understand. The OP has a program named "RouteBuddy" that talks to a device over a serial port, and he want to repalce the data stream coming from that device. My question is, "where does the device that he wants to replace plug in?" I don't see anywhere on my laptop I could plug in anything but a USB connector, an ethernet connector, a firewire connector, headphones, speakers, a display connector, or a power cord. --Scott David Daniels Scott.Daniels at Acm.Org From a.pfrengle at gmail.com Sat Apr 11 14:29:35 2009 From: a.pfrengle at gmail.com (Andreas) Date: Sat, 11 Apr 2009 11:29:35 -0700 (PDT) Subject: Regex similar to "^(?u)\w$", but without digits? Message-ID: Hello, I'd like to create a regex that captures any unicode character, but not the underscore and the digits 0-9. "^(?u)\w$" captures them also. Is there a possibility to restrict an expression like "\w" to "\w without [0-9_]"? I'm using python 2.5.4 Thanks in advance, Andreas From mdw at distorted.org.uk Sat Apr 11 14:47:17 2009 From: mdw at distorted.org.uk (Mark Wooding) Date: Sat, 11 Apr 2009 19:47:17 +0100 Subject: Definition of Pythonic? References: <7026142e-5f9c-49bf-9fee-c3859caf3430@x6g2000vbg.googlegroups.com> Message-ID: <878wm7auyy.fsf.mdw@metalzone.distorted.org.uk> John Yeung writes: > A couple of others have already mentioned the Zen of Python, available > at the Python command prompt. I would agree with that, but also add > the caveat that none of the principles expressed there are hard-and- > fast rules. Indeed, I'd suggest that the very lack of hard-and-fast rules is typical of the Python approach. > The fact is, it's impossible to satisfy every principle in every > situation. To me, Python distinguishes itself for how well it > balances all of them. "Compromise" is a word that comes up a lot when > talking about the design of Python. To some, that has a negative > connotation; to me, it's an inevitable consequence of being practical. Agreed. Then again, all language design is a compromise, between factors like runtime efficiency, simplicity of implementation, tractability of semantics (for various classes of users), supportability by tools, and expressive power. Python inhabits what seems to me to be a particularly sweet spot on this rather complex landscape. -- [mdw] From cmh.python at gmail.com Sat Apr 11 15:03:01 2009 From: cmh.python at gmail.com (Mike H) Date: Sat, 11 Apr 2009 15:03:01 -0400 Subject: Unsupported operand types in if/else list comprehension In-Reply-To: References: <74bdopF126tl6U1@mid.uni-berlin.de> Message-ID: Sigh. One more. And again, thank you for all of the help. I realized that the last version that I posted took care of an SQL injection problem for the values, but not for the fields. So, I went ahead and modified the code: def new_insert_cmd(myTable, myFields, myValues): """Imports given fields and values into a given table, returns the Autoincrement value.""" SQLcmd="INSERT INTO " + myTable + " ( " + create_input_string(myFields) + " ) VALUES ( " \ + create_input_string(myValues) +" );" allArguments=myFields+myValues cursor.execute(SQLcmd, (allArguments)) create_input_strings() is just a function that creates the necessary number of %s's for a given list (and which I'm sure there's a faster way to code): def create_input_string(myList): sOut="" for var in myList: sOut=sOut+"%s, " return sOut[:-2] However, now the cursor.execute statement won't work. I've looked at the content of SQLcmd and the values of allArguments and they seem fine. I've even tried running this at the IDLE command line: cursor.execute("INSERT INTO plan (%s, %s, %s) VALUES (%s, %s, %s);", (["name", "fileno", "size", "Test", "AAA-000", 7])) and I get this error: File "C:\Python25\lib\site-packages\MySQLdb\cursors.py", line 166, in execute self.errorhandler(self, exc, value) File "C:\Python25\lib\site-packages\MySQLdb\connections.py", line 35, in defaulterrorhandler raise errorclass, errorvalue ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''name', 'fileno', 'size') VALUES ('Test', 'AAA-000', 7)' at line 1") Can I not use the cursor.execute command to pass variables that aren't immediately next to each other? If so, is there a better way to go about solving this problem? Again, thanks for your patience and help with a newbie. Michael On Sat, Apr 11, 2009 at 11:53 AM, Mike H wrote: > Well, I'm an idiot. Obviously, the line "VALUES (%s, %s, %s);" needs > to be modified to adapt for the number of arguments in the list. But > otherwise.... > > On Sat, Apr 11, 2009 at 11:28 AM, Mike H wrote: >> Ok, thanks again to everyone for their suggestions, even if it appears >> I was going down the wrong path at the start. I'm a grad student >> creating this database to hold some of my own research on an isolated >> server, so security, etc. isn't my biggest concern -- but I would like >> to do this right. Here's the code that I've come up with now. Although >> it's gotten away from the original question, those that have commented >> on this seem to have some SQL knowledge, so I'd like to run it by them >> to see if this is better in theory. (I've tried it and it works in >> practice!) >> >> FYI, I'm using MySQLdb to connect with the Database. >> >> Also, I realize I should probably add in some try/catch statements and >> other error handling... but this is what I have at the moment. >> >> >> >> def insert_cmd(myTable, myFields, myValues, myReturnKey): >> ? ?"""Imports given fields and values into a given table, returns an >> SQL variable holding the Autoincrement key""" >> >> ? ?#tests to see if myParentKey is valid in mySQL. >> ? ?if not myReturnKey.startswith("@"): print "Error, myReturnKey must >> start with '@'"; sys.exit() >> >> ? ?SQLcmd="INSERT INTO " + myTable + " (%s) " % ", ".join(myFields) >> ? ?SQLcmd=SQLcmd + "VALUES (%s,%s,%s);" >> ? ?cursor.execute(SQLcmd, (myValues)) >> >> ? ?#sets and returns SQL variable. >> ? ?SQLcmd="select " + myReturnKey + ":=last_insert_id();" >> ? ?cursor.execute(SQLcmd) >> ? ?return myReturnKey >> >> >> On Sat, Apr 11, 2009 at 7:38 AM, Diez B. Roggisch wrote: >>> Mike H schrieb: >>>> >>>> Thanks to all of you. >>>> >>>> FYI, I'm doing this because I'm working on creating some insert >>>> statements in SQL, where string values need to be quoted, and integer >>>> values need to be unquoted. >>>> >>>> I wanted to be sure that I could pass these values to the list in a >>>> normal way e.g. ['test', 1, 'two'] and have a function correct the >>>> list for me, rather than calling the function with a strangely quoted >>>> list e.g. ['"'test'"', 1, '"'two'"'].> >>> >>> Don't do that yourself. This is error-prone. Instead, use the parametrized >>> verison of the cursor.execute-method. It will perform the necessary >>> escaping, and depending on the database and database adapter you use better >>> performance. >>> >>> Diez >>> -- >>> http://mail.python.org/mailman/listinfo/python-list >>> >> > From __peter__ at web.de Sat Apr 11 15:06:52 2009 From: __peter__ at web.de (Peter Otten) Date: Sat, 11 Apr 2009 21:06:52 +0200 Subject: Using percent signs with SafeConfigParser References: <50bfe567-7e59-4138-ae55-c3ee2259b169@g17g2000vbi.googlegroups.com> Message-ID: M?rcio Faustino wrote: > Does the SafeConfigParser class correctly detects lone percent signs? > For example, shouldn't the string "100%%" be accepted as a valid > value? Executing the code below should only print one error, instead > it prints 2. (I've tested this with version 2.6.1 on Windows XP.) > > It seems the "_badpercent_re" regular expression (an attribute of > SafeConfigParser, with value "%[^%]|%$") is flawed: > - The first alternative "%[^%]" fails with the string "%%_", because > it matches "%_". > - The second alternative "%$" fails with the string "%%", because it > matches "%". I think you are right. Please file a bug report . > The correct regular expression is "(? previous tests. IMO this doesn't fix the problem because (1) it allows "%%%" which is also incorrect (2) _interpvar_re has already butchered values like "%%(alpha)s" to "%" Peter From nad at acm.org Sat Apr 11 15:07:43 2009 From: nad at acm.org (Ned Deily) Date: Sat, 11 Apr 2009 12:07:43 -0700 Subject: How to create a virtual serial port? References: <_cCdndEvsrp8PULUnZ2dnUVZ_qydnZ2d@pdx.net> <5fd78a41-ed64-4e3e-aa80-a89c8f44cf3f@v6g2000vbb.googlegroups.com> <6LGdnSbyPsL1fX3UnZ2dnUVZ_uqdnZ2d@pdx.net> Message-ID: In article <6LGdnSbyPsL1fX3UnZ2dnUVZ_uqdnZ2d at pdx.net>, Scott David Daniels wrote: > This part I actually understand. The OP has a program named > "RouteBuddy" that talks to a device over a serial port, and he > want to repalce the data stream coming from that device. My > question is, "where does the device that he wants to replace > plug in?" I don't see anywhere on my laptop I could plug in > anything but a USB connector, an ethernet connector, a firewire > connector, headphones, speakers, a display connector, or a > power cord. A number of vendors (Keyspan, Belkin) make USB serial ports. FWIW, I use one here on this iMac and OS X with screen(1) and a null modem cable to act as a serial console for a headless Linux box. -- Ned Deily, nad at acm.org From zak.mc.kraken at libero.it Sat Apr 11 15:48:09 2009 From: zak.mc.kraken at libero.it (Vito De Tullio) Date: Sat, 11 Apr 2009 21:48:09 +0200 Subject: Reading 3 objects at a time from list References: Message-ID: Matteo wrote: > it works and I like slices, but I was wondering if there was another > way of doing the same thing, maybe reading the numbers in groups of > arbitrary length n... from http://docs.python.org/library/itertools.html#recipes def grouper(n, iterable, fillvalue=None): "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx" args = [iter(iterable)] * n return izip_longest(fillvalue=fillvalue, *args) and it's *damned* fast! -- By ZeD From grante at visi.com Sat Apr 11 16:03:46 2009 From: grante at visi.com (Grant Edwards) Date: Sat, 11 Apr 2009 15:03:46 -0500 Subject: How to create a virtual serial port? References: <_cCdndEvsrp8PULUnZ2dnUVZ_qydnZ2d@pdx.net> <5fd78a41-ed64-4e3e-aa80-a89c8f44cf3f@v6g2000vbb.googlegroups.com> <6LGdnSbyPsL1fX3UnZ2dnUVZ_uqdnZ2d@pdx.net> Message-ID: On 2009-04-11, Scott David Daniels wrote: > Grant Edwards wrote: >> On 2009-04-11, Grant Edwards wrote: >> >>> You can write a port redirector in user-space in MS-Windows, >>> but you can't in Linux/Unix. On Unix systems you have to >>> write a kernel module that sits below the tty layer. >> >> Perhaps I should elucidate further. > > This part I actually understand. The OP has a program named > "RouteBuddy" that talks to a device over a serial port, and he > want to repalce the data stream coming from that device. My > question is, "where does the device that he wants to replace > plug in?" To some other machine/device on the network that does have a serial port. You can buy dedicated devices that do serial-ethernet, or you can use any old computer that does happen to have a serial port. If he was going to plug the device into a real serial port on the machine inquestion, then he wouldn't need a virtual serial port. > I don't see anywhere on my laptop I could plug in anything but > a USB connector, an ethernet connector, a firewire connector, > headphones, speakers, a display connector, or a power cord. He's not trying to plug the device into your laptop. -- Grant From grante at visi.com Sat Apr 11 16:04:28 2009 From: grante at visi.com (Grant Edwards) Date: Sat, 11 Apr 2009 15:04:28 -0500 Subject: How to create a virtual serial port? References: <_cCdndEvsrp8PULUnZ2dnUVZ_qydnZ2d@pdx.net> <5fd78a41-ed64-4e3e-aa80-a89c8f44cf3f@v6g2000vbb.googlegroups.com> <6LGdnSbyPsL1fX3UnZ2dnUVZ_uqdnZ2d@pdx.net> Message-ID: On 2009-04-11, Ned Deily wrote: > In article <6LGdnSbyPsL1fX3UnZ2dnUVZ_uqdnZ2d at pdx.net>, > Scott David Daniels wrote: >> This part I actually understand. The OP has a program named >> "RouteBuddy" that talks to a device over a serial port, and he >> want to repalce the data stream coming from that device. My >> question is, "where does the device that he wants to replace >> plug in?" I don't see anywhere on my laptop I could plug in >> anything but a USB connector, an ethernet connector, a firewire >> connector, headphones, speakers, a display connector, or a >> power cord. > > A number of vendors (Keyspan, Belkin) make USB serial ports. FWIW, I > use one here on this iMac and OS X with screen(1) and a null modem cable > to act as a serial console for a headless Linux box. True, but that doesn't help the OP, where the data is coming into the OS-X machine via a network connection. -- Grant From aleksandr.goretoy at gmail.com Sat Apr 11 16:11:22 2009 From: aleksandr.goretoy at gmail.com (alex goretoy) Date: Sat, 11 Apr 2009 20:11:22 +0000 Subject: How to create a virtual serial port? In-Reply-To: References: <_cCdndEvsrp8PULUnZ2dnUVZ_qydnZ2d@pdx.net> <5fd78a41-ed64-4e3e-aa80-a89c8f44cf3f@v6g2000vbb.googlegroups.com> Message-ID: A number of vendors (Keyspan, Belkin) make USB serial ports. FWIW, I use one here on this iMac and OS X with screen(1) and a null modem cable to act as a serial console for a headless Linux box. +1 -Alex Goretoy http://www.goretoy.com Norman Mailer - "Writing books is the closest men ever come to childbearing." -------------- next part -------------- An HTML attachment was scrubbed... URL: From grkuntzmd at gmail.com Sat Apr 11 16:11:42 2009 From: grkuntzmd at gmail.com (grkuntzmd at gmail.com) Date: Sat, 11 Apr 2009 13:11:42 -0700 (PDT) Subject: Q:Pythonic way to create list of lists Message-ID: I am just learning Python. I am trying to create a list of empty lists: [[], [], [], ...] (10 items total). What is the most Pythonic way to do this? If I use a list comprehension (as in myList = [[] for item in xrange (0, 10)]), Netbeans warns me that 'item' is never used. If I use a for-loop (as in for item in myList = []; for item in xrange (0, 10): myList.append([])), Netbeans still warns me of the same thing. If I use '*' (as myList = [[]] * 10), all of the empty lists refer to the same object; changing one changes them all. Do I have to live with the warning, or is there a "better" way? Thanks. From ergconcepts at googlemail.com Sat Apr 11 16:14:20 2009 From: ergconcepts at googlemail.com (ergconcepts at googlemail.com) Date: Sat, 11 Apr 2009 13:14:20 -0700 (PDT) Subject: absolute newbie: divide a list into sublists (nested lists?) of fixed length Message-ID: Hi, I have a list looking like [ 0.84971586, 0.05786009, 0.9645675, 0.84971586, 0.05786009, 0.9645675, 0.84971586, 0.05786009, 0.9645675, 0.84971586, 0.05786009, 0.9645675] and I would like to break this list into subsets of fixed length (say, three elements), i.e. to convert the list into a form such as the one generated by the following example code which I have found: >>>import numpy >>>s = numpy.random.random((3,3)) >>>s array([[ 0.11916176, 0.96409475, 0.72602155], [ 0.84971586, 0.05786009, 0.96456754], [ 0.81617437, 0.845342 , 0.09109779]]) How can I create such a 2d array (i.e., something like a symmetric matrix) from my data? Thanks in advance, Bernard PS: Note that the numpy import is not important here, it is just the structure of the data that matters.. From george.sakkis at gmail.com Sat Apr 11 16:18:04 2009 From: george.sakkis at gmail.com (George Sakkis) Date: Sat, 11 Apr 2009 13:18:04 -0700 (PDT) Subject: Unsupported operand types in if/else list comprehension References: <74bdopF126tl6U1@mid.uni-berlin.de> Message-ID: <257002bd-d6bc-4771-9711-e2270004ab5f@i28g2000prd.googlegroups.com> On Apr 11, 3:03?pm, Mike H wrote: > Can I not use the cursor.execute command to pass variables that aren't > immediately next to each other? If so, is there a better way to go > about solving this problem? Yes, there is. Use one of the several production quality python SQL toolkits built for exactly this purpose instead of putting together an ad-hoc, informally-specified bug-ridden slow implementation of half of their feature set. George From python.list at tim.thechases.com Sat Apr 11 16:25:36 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Sat, 11 Apr 2009 15:25:36 -0500 Subject: Q:Pythonic way to create list of lists In-Reply-To: References: Message-ID: <49E0FCC0.8080508@tim.thechases.com> > I am trying to create a list of empty lists: [[], [], [], ...] (10 > items total). > > What is the most Pythonic way to do this? > > If I use a list comprehension (as in myList = [[] for item in xrange > (0, 10)]), Netbeans warns me that 'item' is never used. Not using Netbeans, I can't verify that the below will stop the warning. However the variable name "_" is used conventionally to mean "this doesn't matter". Additionally, the starting point for xrange defaults to 0, so you can omit it. That would translate to my_list = [[] for _ in xrange(10)] -tkc From cmh.python at gmail.com Sat Apr 11 16:26:48 2009 From: cmh.python at gmail.com (Mike H) Date: Sat, 11 Apr 2009 16:26:48 -0400 Subject: Unsupported operand types in if/else list comprehension In-Reply-To: <257002bd-d6bc-4771-9711-e2270004ab5f@i28g2000prd.googlegroups.com> References: <74bdopF126tl6U1@mid.uni-berlin.de> <257002bd-d6bc-4771-9711-e2270004ab5f@i28g2000prd.googlegroups.com> Message-ID: George, I'd love to. Can you give me an idea of where to start looking? I've gone through a couple of books, and Googled a ton of websites. Maybe I'm just not using the right terms. My background is definitely not CompSci. But if you'd give me a suggestion of where to look, I'd appreciate it. Thanks, On Sat, Apr 11, 2009 at 4:18 PM, George Sakkis wrote: > On Apr 11, 3:03?pm, Mike H wrote: > >> Can I not use the cursor.execute command to pass variables that aren't >> immediately next to each other? If so, is there a better way to go >> about solving this problem? > > Yes, there is. Use one of the several production quality python SQL > toolkits built for exactly this purpose instead of putting together an > ad-hoc, informally-specified bug-ridden slow implementation of half of > their feature set. > > George > -- > http://mail.python.org/mailman/listinfo/python-list > From nad at acm.org Sat Apr 11 16:32:57 2009 From: nad at acm.org (Ned Deily) Date: Sat, 11 Apr 2009 13:32:57 -0700 Subject: How to create a virtual serial port? References: <_cCdndEvsrp8PULUnZ2dnUVZ_qydnZ2d@pdx.net> <5fd78a41-ed64-4e3e-aa80-a89c8f44cf3f@v6g2000vbb.googlegroups.com> <6LGdnSbyPsL1fX3UnZ2dnUVZ_uqdnZ2d@pdx.net> Message-ID: In article , Grant Edwards wrote: > On 2009-04-11, Ned Deily wrote: > > In article <6LGdnSbyPsL1fX3UnZ2dnUVZ_uqdnZ2d at pdx.net>, > > Scott David Daniels wrote: > >> This part I actually understand. The OP has a program named > >> "RouteBuddy" that talks to a device over a serial port, and he > >> want to repalce the data stream coming from that device. My > >> question is, "where does the device that he wants to replace > >> plug in?" I don't see anywhere on my laptop I could plug in > >> anything but a USB connector, an ethernet connector, a firewire > >> connector, headphones, speakers, a display connector, or a > >> power cord. > > A number of vendors (Keyspan, Belkin) make USB serial ports. FWIW, I > > use one here on this iMac and OS X with screen(1) and a null modem cable > > to act as a serial console for a headless Linux box. > True, but that doesn't help the OP, where the data is coming > into the OS-X machine via a network connection. Also true but I just wanted to use "headless" in a python post. -- Ned Deily, nad at acm.org From Scott.Daniels at Acm.Org Sat Apr 11 16:35:43 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sat, 11 Apr 2009 13:35:43 -0700 Subject: How to create a virtual serial port? In-Reply-To: References: <_cCdndEvsrp8PULUnZ2dnUVZ_qydnZ2d@pdx.net> <5fd78a41-ed64-4e3e-aa80-a89c8f44cf3f@v6g2000vbb.googlegroups.com> <6LGdnSbyPsL1fX3UnZ2dnUVZ_uqdnZ2d@pdx.net> Message-ID: Grant Edwards wrote: > ...If he was going to plug the device into a real serial port on > the machine inquestion, then he wouldn't need a virtual serial > port. > >> I don't see anywhere on my laptop I could plug in anything but >> a USB connector, an ethernet connector, a firewire connector, >> headphones, speakers, a display connector, or a power cord. > > He's not trying to plug the device into your laptop. Yup. It seems to me you could write a slug to talk USB driver to OS/X, but if it were really sending I/O instructions to an on-board chip, I would say there was no hope. That is why I was trying to figure that out. --Scott David Daniels Scott.Daniels at Acm.Org From a.pfrengle at gmail.com Sat Apr 11 16:37:25 2009 From: a.pfrengle at gmail.com (Andreas Pfrengle) Date: Sat, 11 Apr 2009 13:37:25 -0700 (PDT) Subject: absolute newbie: divide a list into sublists (nested lists?) of fixed length References: Message-ID: <155a0588-78e4-44f4-a134-b467daf96996@r36g2000vbr.googlegroups.com> On 11 Apr., 22:14, ergconce... at googlemail.com wrote: > Hi, > I have a list looking like > > [ 0.84971586, ?0.05786009, ?0.9645675, ?0.84971586, ?0.05786009, > 0.9645675, 0.84971586, ?0.05786009, ?0.9645675, ?0.84971586, > 0.05786009, ?0.9645675] > > and I would like to break this list into subsets of fixed length (say, > three elements), i.e. to convert the list into a form such as the one > generated by the following example code which I have found: > > >>>import numpy > >>>s = numpy.random.random((3,3)) > >>>s > > array([[ 0.11916176, ?0.96409475, ?0.72602155], > ? ? ? ?[ 0.84971586, ?0.05786009, ?0.96456754], > ? ? ? ?[ 0.81617437, ?0.845342 ?, ?0.09109779]]) > > How can I create such a 2d array (i.e., something like a symmetric > matrix) from my data? > > Thanks in advance, > > Bernard > > PS: Note that the numpy import is not important here, it is just the > structure of the data that matters.. Not sure if that's the best solution, but you could try: my_list = [] for x in range(3): my_list.append([]) for y in range(3): my_list[x].append(some_value) From george.sakkis at gmail.com Sat Apr 11 16:52:56 2009 From: george.sakkis at gmail.com (George Sakkis) Date: Sat, 11 Apr 2009 13:52:56 -0700 (PDT) Subject: Unsupported operand types in if/else list comprehension References: <74bdopF126tl6U1@mid.uni-berlin.de> <257002bd-d6bc-4771-9711-e2270004ab5f@i28g2000prd.googlegroups.com> Message-ID: <361e7de6-be55-4bc4-9713-eeaf87836578@w31g2000prd.googlegroups.com> On Apr 11, 4:26?pm, Mike H wrote: > George, > > I'd love to. Can you give me an idea of where to start looking? I've > gone through a couple of books, and Googled a ton of websites. Maybe > I'm just not using the right terms. My background is definitely not > CompSci. But if you'd give me a suggestion of where to look, I'd > appreciate it. Sure; off the top of my head, check out SQLAlchemy [1], SQLObject [2] or Storm [3]. SQLAlchemy is probably the most powerful but it has arguably the steepest learning curve, so you might find it less appealing initially. In any case, something as simple as an insert should be trivial in all frameworks, e.g. in SQLAlchemy (if you don't use the object-relational mapper) you can create an insert statement as: # create the statement (my_table: an sqlalchemy.Table) >>> ins = my_table.insert().values(field1=value1, field2=value2, ..., fieldN=valueN) # you can print to see the actual generated SQL >>> str(ins) 'INSERT INTO my_table (field1, field2, ... fieldN) VALUES (:field1, :field2, ... :fieldN)' # execute it (conn: an active sqlachemy.engine.Connection) >>> result = conn.execute(ins) HTH, George [1] http://www.sqlalchemy.org/ [2] http://www.sqlobject.org/ [3] https://storm.canonical.com/ > On Sat, Apr 11, 2009 at 4:18 PM, George Sakkis wrote: > > On Apr 11, 3:03?pm, Mike H wrote: > > >> Can I not use the cursor.execute command to pass variables that aren't > >> immediately next to each other? If so, is there a better way to go > >> about solving this problem? > > > Yes, there is. Use one of the several production quality python SQL > > toolkits built for exactly this purpose instead of putting together an > > ad-hoc, informally-specified bug-ridden slow implementation of half of > > their feature set. > > > George > > -- > >http://mail.python.org/mailman/listinfo/python-list From ergconcepts at googlemail.com Sat Apr 11 17:00:11 2009 From: ergconcepts at googlemail.com (ergconcepts at googlemail.com) Date: Sat, 11 Apr 2009 14:00:11 -0700 (PDT) Subject: absolute newbie: divide a list into sublists (nested lists?) of fixed length References: <155a0588-78e4-44f4-a134-b467daf96996@r36g2000vbr.googlegroups.com> Message-ID: On Apr 11, 10:37?pm, Andreas Pfrengle wrote: > my_list = [] > for x in range(3): > ?my_list.append([]) > ?for y in range(3): > ? my_list[x].append(some_value) Thanks for your help - but I'm sorry I do not understand: > my_list = [] I guess here you are implying to write something like my_list = [ 0.84971586, 0.05786009, 0.9645675, 0.84971586, 0.05786009, 0.9645675, 0.84971586, 0.05786009, 0.9645675, 0.84971586, 0.05786009, 0.9645675] ? then what would this do: > my_list.append([]) ? and finally, what do you mean by "some_value"? > my_list[x].append(some_value) of course, the value to append would always be the next one from my list, and not one I would want to give explicitly each time.. I'm sorry if I did not make myself very clear before but I am completely new to the python syntax and also to the technical terms.. Best regards, Bernhard From cdalten at gmail.com Sat Apr 11 17:06:12 2009 From: cdalten at gmail.com (grocery_stocker) Date: Sat, 11 Apr 2009 14:06:12 -0700 (PDT) Subject: Overriding __init__() questions Message-ID: <60be95d3-b971-47ad-bb83-f2fcafb1ea00@q33g2000pra.googlegroups.com> What's the difference between doing something calling A.__init__(self) like in the following... [cdalten at localhost ~]$ python Python 2.4.3 (#1, Oct 1 2006, 18:00:19) [GCC 4.1.1 20060928 (Red Hat 4.1.1-28)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> class A: ... def __init__(self): ... pass ... >>> class B(A): ... def __init__(self, x): ... A.__init__(self) ... self.x = x ... print x ... >>> x=B(5) 5 >>> y=A(5) Traceback (most recent call last): File "", line 1, in ? TypeError: __init__() takes exactly 1 argument (2 given) >>> y = A() >>> versus something like the following.... [cdalten at localhost ~]$ python Python 2.4.3 (#1, Oct 1 2006, 18:00:19) [GCC 4.1.1 20060928 (Red Hat 4.1.1-28)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> class A: ... def __init__(self): ... pass ... >>> class B(A): ... def __init__(self, x): ... self.x = x ... print x ... >>> x = B(5) 5 >>> y = A(5) Traceback (most recent call last): File "", line 1, in ? TypeError: __init__() takes exactly 1 argument (2 given) >>> y = A() >>> Just curious because the former seems to be common when using the python Thread module. From george.sakkis at gmail.com Sat Apr 11 17:18:03 2009 From: george.sakkis at gmail.com (George Sakkis) Date: Sat, 11 Apr 2009 14:18:03 -0700 (PDT) Subject: absolute newbie: divide a list into sublists (nested lists?) of fixed length References: Message-ID: <52a4ce19-6a1e-4788-aae5-a100ee1ab910@i28g2000prd.googlegroups.com> On Apr 11, 4:14?pm, ergconce... at googlemail.com wrote: > Hi, > I have a list looking like > > [ 0.84971586, ?0.05786009, ?0.9645675, ?0.84971586, ?0.05786009, > 0.9645675, 0.84971586, ?0.05786009, ?0.9645675, ?0.84971586, > 0.05786009, ?0.9645675] > > and I would like to break this list into subsets of fixed length (say, > three elements), i.e. to convert the list into a form such as the one > generated by the following example code which I have found: > > >>>import numpy > >>>s = numpy.random.random((3,3)) > >>>s > > array([[ 0.11916176, ?0.96409475, ?0.72602155], > ? ? ? ?[ 0.84971586, ?0.05786009, ?0.96456754], > ? ? ? ?[ 0.81617437, ?0.845342 ?, ?0.09109779]]) > > How can I create such a 2d array (i.e., something like a symmetric > matrix) from my data? > > Thanks in advance, > > Bernard > > PS: Note that the numpy import is not important here, it is just the > structure of the data that matters.. The numpy import *is* important if you want to use numpy-specific features; there are many "tricks" you can do easily with numpy arrays that you have to write manually for, say, regular python lists. For example what you want to do is trivial with numpy: >>>import numpy as N >>> s = N.array([ 0.84971586, 0.05786009, 0.9645675, 0.84971586, 0.05786009, 0.9645675, 0.84971586, 0.05786009, 0.9645675, 0.84971586, 0.05786009, 0.9645675]) >>> # convert to a 4by3 array in place >>> s.shape = (4,3) >>> s array([[ 0.84971586, 0.05786009, 0.9645675 ], [ 0.84971586, 0.05786009, 0.9645675 ], [ 0.84971586, 0.05786009, 0.9645675 ], [ 0.84971586, 0.05786009, 0.9645675 ]]) HTH, George From tjreedy at udel.edu Sat Apr 11 17:26:04 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 11 Apr 2009 17:26:04 -0400 Subject: ANN: PyGUI 2.0 In-Reply-To: <49E07ABB.7070803__30474.2209300914$1239448329$gmane$org@canterbury.ac.nz> References: <49E07ABB.7070803__30474.2209300914$1239448329$gmane$org@canterbury.ac.nz> Message-ID: Greg Ewing wrote: > PyGUI 2.0 is available: > > http://www.cosc.canterbury.ac.nz/greg.ewing/python_gui/ Does it work with 3.0? (or just 2.3-2.6?) From arnodel at googlemail.com Sat Apr 11 17:26:05 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Sat, 11 Apr 2009 22:26:05 +0100 Subject: Generators/iterators, Pythonicity, and primes References: Message-ID: John Posner writes: > Inspired by recent threads (and recalling my first message to Python > edu-sig), I did some Internet searching on producing prime numbers using > Python generators. Most algorithms I found don't go for the infinite, > contenting themselves with "list all the primes below a given number". > > Here's a very Pythonic (IMHO) implementation that keeps going and >going and going ...: > > from itertools import count > from math import sqrt > > def prime_gen(): > """ > Generate all prime numbers > """ > primes = [] > for n in count(2): > if all(n%p for p in primes if p < sqrt(n)): > primes.append(n) > yield n You could do something like this with the help of itertools.ifilter: prime_gen = ifilter( lambda n, P=[]: all(n%p for p in P) and not P.append(n), count(2) ) I omitted the 'if p <= sqrt(n)' as it doesn't help the generator go faster. You could use itertools.takewhile for that: prime_gen = ifilter( lambda n, P=[]: all(n%p for p in takewhile(lambda p, s=n**0.5: p<=s, P)) and not P.append(n), count(2) ) Of course there is no excuse for writing Python like that :) -- Arnaud From google at mrabarnett.plus.com Sat Apr 11 17:32:02 2009 From: google at mrabarnett.plus.com (MRAB) Date: Sat, 11 Apr 2009 22:32:02 +0100 Subject: Pathological regular expression In-Reply-To: <01f0ca1b$0$20620$c3e8da3@news.astraweb.com> References: <967fe248-0e15-4247-9d36-6f3f5e0c5eb7@a7g2000yqk.googlegroups.com> <01f0a658$0$20620$c3e8da3@news.astraweb.com> <042e5adf-1aff-4baa-bcef-78d69bc36e32@d38g2000prn.googlegroups.com> <01f0ca1b$0$20620$c3e8da3@news.astraweb.com> Message-ID: <49E10C52.2070903@mrabarnett.plus.com> Steven D'Aprano wrote: > On Sat, 11 Apr 2009 08:40:03 -0700, John Machin wrote: > >>> To my mind, this is a bug in the RE engine. Is there any reason to not >>> treat it as a bug? >> IMHO it's not a bug -- s/hang/takes a long time to compute/ >> >> Just look at it: 2 + operators and 3 * operators ... It's one of those >> "come back after lunch" REs. > > Well, it's been running now for about two and a half hours, that's a > rather long lunch. And despite MRAB's assertion, it *cannot* be > interrupted by ctrl-C. That means that to all intents and purposes, the > interpreter has locked up for the duration of the calculation, which may > be days or weeks for all I know. > I've just tried all 3 regexes in Python 2.5.2. All could be interrupted by ctrl-C. From tjreedy at udel.edu Sat Apr 11 17:37:46 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 11 Apr 2009 17:37:46 -0400 Subject: Q:Pythonic way to create list of lists In-Reply-To: References: Message-ID: grkuntzmd at gmail.com wrote: > I am just learning Python. > > I am trying to create a list of empty lists: [[], [], [], ...] (10 > items total). > > What is the most Pythonic way to do this? > > If I use a list comprehension (as in myList = [[] for item in xrange > (0, 10)]), Netbeans warns me that 'item' is never used. Never heard of netbeans, but a decent code checker should have a mechanism to annotate (comment) code to say "I know what I am doing here, shut up". > If I use a for-loop (as in for item in myList = []; for item in xrange > (0, 10): myList.append([])), Netbeans still warns me of the same > thing. Adding something stupic like 'item = item' in the loop might 'work' ;-) > If I use '*' (as myList = [[]] * 10), all of the empty lists refer to > the same object; changing one changes them all. Right. Typical newbie mistake. Good for you for discovering that yourself. > Do I have to live with the warning, or is there a "better" way? Different checker? PyChecker? PhLint? tjr From tjreedy at udel.edu Sat Apr 11 17:42:05 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 11 Apr 2009 17:42:05 -0400 Subject: Overriding __init__() questions In-Reply-To: <60be95d3-b971-47ad-bb83-f2fcafb1ea00@q33g2000pra.googlegroups.com> References: <60be95d3-b971-47ad-bb83-f2fcafb1ea00@q33g2000pra.googlegroups.com> Message-ID: grocery_stocker wrote: > What's the difference between doing something calling A.__init__(self) > like in the following... > > [cdalten at localhost ~]$ python > Python 2.4.3 (#1, Oct 1 2006, 18:00:19) > [GCC 4.1.1 20060928 (Red Hat 4.1.1-28)] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> class A: > ... def __init__(self): > ... pass > ... >>>> class B(A): > ... def __init__(self, x): > ... A.__init__(self) > ... self.x = x > ... print x > ... >>>> x=B(5) > 5 >>>> y=A(5) > Traceback (most recent call last): > File "", line 1, in ? > TypeError: __init__() takes exactly 1 argument (2 given) >>>> y = A() >>>> > > versus something like the following.... > > [cdalten at localhost ~]$ python > Python 2.4.3 (#1, Oct 1 2006, 18:00:19) > [GCC 4.1.1 20060928 (Red Hat 4.1.1-28)] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> class A: > ... def __init__(self): > ... pass > ... >>>> class B(A): > ... def __init__(self, x): > ... self.x = x > ... print x In one you call A.__init__ and the other you don't. If A.__init__ does something useful, as it usually would, then not calling it would not get that useful work done. Or you repeat the useful code of A.__init__ in B.__init__, which is not a good idea, especially when you have to change that code in two places. > ... >>>> x = B(5) > 5 >>>> y = A(5) > Traceback (most recent call last): > File "", line 1, in ? > TypeError: __init__() takes exactly 1 argument (2 given) >>>> y = A() >>>> > > Just curious because the former seems to be common when using the > python Thread module. > -- > http://mail.python.org/mailman/listinfo/python-list > From mensanator at aol.com Sat Apr 11 18:01:13 2009 From: mensanator at aol.com (Mensanator) Date: Sat, 11 Apr 2009 15:01:13 -0700 (PDT) Subject: absolute newbie: divide a list into sublists (nested lists?) of fixed length References: <52a4ce19-6a1e-4788-aae5-a100ee1ab910@i28g2000prd.googlegroups.com> Message-ID: On Apr 11, 4:18?pm, George Sakkis wrote: > On Apr 11, 4:14?pm, ergconce... at googlemail.com wrote: > > > > > > > Hi, > > I have a list looking like > > > [ 0.84971586, ?0.05786009, ?0.9645675, ?0.84971586, ?0.05786009, > > 0.9645675, 0.84971586, ?0.05786009, ?0.9645675, ?0.84971586, > > 0.05786009, ?0.9645675] > > > and I would like to break this list into subsets of fixed length (say, > > three elements), i.e. to convert the list into a form such as the one > > generated by the following example code which I have found: > > > >>>import numpy > > >>>s = numpy.random.random((3,3)) > > >>>s > > > array([[ 0.11916176, ?0.96409475, ?0.72602155], > > ? ? ? ?[ 0.84971586, ?0.05786009, ?0.96456754], > > ? ? ? ?[ 0.81617437, ?0.845342 ?, ?0.09109779]]) > > > How can I create such a 2d array (i.e., something like a symmetric > > matrix) from my data? > > > Thanks in advance, > > > Bernard > > > PS: Note that the numpy import is not important here, it is just the > > structure of the data that matters.. > > The numpy import *is* important if you want to use numpy-specific > features; there are many "tricks" you can do easily with numpy arrays > that you have to write manually for, say, regular python lists. For > example what you want to do is trivial with numpy: > > >>>import numpy as N > >>> s = N.array([ 0.84971586, ?0.05786009, ?0.9645675, ?0.84971586, ?0.05786009, > > 0.9645675, 0.84971586, ?0.05786009, ?0.9645675, ?0.84971586, > 0.05786009, ?0.9645675])>>> # convert to a 4by3 array in place > >>> s.shape = (4,3) > >>> s What does numpy do if the original list has 13 elements? > > array([[ 0.84971586, ?0.05786009, ?0.9645675 ], > ? ? ? ?[ 0.84971586, ?0.05786009, ?0.9645675 ], > ? ? ? ?[ 0.84971586, ?0.05786009, ?0.9645675 ], > ? ? ? ?[ 0.84971586, ?0.05786009, ?0.9645675 ]]) > > HTH, > George From ergconcepts at googlemail.com Sat Apr 11 18:05:25 2009 From: ergconcepts at googlemail.com (ergconcepts at googlemail.com) Date: Sat, 11 Apr 2009 15:05:25 -0700 (PDT) Subject: absolute newbie: divide a list into sublists (nested lists?) of fixed length References: <52a4ce19-6a1e-4788-aae5-a100ee1ab910@i28g2000prd.googlegroups.com> Message-ID: On Apr 11, 11:18?pm, George Sakkis wrote: > The numpy import *is* important if you want to use numpy-specific > features; there are many "tricks" you can do easily with numpy arrays > that you have to write manually for, say, regular python lists. For > example what you want to do is trivial with numpy: > > >>>import numpy as N > >>> s = N.array([ 0.84971586, ?0.05786009, ?0.9645675, ?0.84971586, ?0.05786009, > > 0.9645675, 0.84971586, ?0.05786009, ?0.9645675, ?0.84971586, > 0.05786009, ?0.9645675])>>> # convert to a 4by3 array in place > >>> s.shape = (4,3) > >>> s > > array([[ 0.84971586, ?0.05786009, ?0.9645675 ], > ? ? ? ?[ 0.84971586, ?0.05786009, ?0.9645675 ], > ? ? ? ?[ 0.84971586, ?0.05786009, ?0.9645675 ], > ? ? ? ?[ 0.84971586, ?0.05786009, ?0.9645675 ]]) Thanks very much - works fine! Now for a follow-up question:) Actually my original list called "mylist" contains 81217 elements - I shape those into >>> len(mylist) 81217 >>> s = N.array(mylist) >>> s.shape = (241,337) which works because the "total size of new array must be unchanged" (241x337=81217) Now this "matrix" actually is a 2D colorcoded map - I want to plot this using the imshow splot routine in mayavi2.. However, there might be a problem: I believe that sometimes my original array will not exactly contain 81217 elements, but maybe only 81216 or so. Nevertheless, I know that time x times y - structure will always be the same (that is, 241 columns). What is of interest to me is the first 241 x 241 part of the matrix anyway. Is there an easy way to reshape my original array into a symmetric 241x241 matrix? Thanks a lot again, Bernard From andreengels at gmail.com Sat Apr 11 18:15:45 2009 From: andreengels at gmail.com (Andre Engels) Date: Sun, 12 Apr 2009 00:15:45 +0200 Subject: absolute newbie: divide a list into sublists (nested lists?) of fixed length In-Reply-To: References: <52a4ce19-6a1e-4788-aae5-a100ee1ab910@i28g2000prd.googlegroups.com> Message-ID: <6faf39c90904111515y50dc6f97g9cd92c71aadff78b@mail.gmail.com> On Sun, Apr 12, 2009 at 12:05 AM, wrote: > Thanks very much - works fine! Now for a follow-up question:) > > Actually my original list called "mylist" contains 81217 elements - I > shape those into > >>>> len(mylist) > 81217 >>>> s = N.array(mylist) >>>> s.shape = (241,337) > > which works because the "total size of new array must be > unchanged" (241x337=81217) > > Now this "matrix" actually is a 2D colorcoded map - I want to plot > this using the imshow splot routine in mayavi2.. > > However, there might be a problem: I believe that sometimes my > original array will not exactly contain 81217 elements, but maybe only > 81216 or so. Nevertheless, I know that time x times y - structure will > always be the same (that is, 241 columns). What is of interest to me > is the first 241 x 241 part of the matrix anyway. Is there an easy way > to reshape my original array into a symmetric 241x241 matrix? Provided that the array always has at least 58081 elements: [mylist[i:i+241] for i in range(0,58081,241)] -- Andr? Engels, andreengels at gmail.com From george.sakkis at gmail.com Sat Apr 11 18:36:47 2009 From: george.sakkis at gmail.com (George Sakkis) Date: Sat, 11 Apr 2009 15:36:47 -0700 (PDT) Subject: absolute newbie: divide a list into sublists (nested lists?) of fixed length References: <52a4ce19-6a1e-4788-aae5-a100ee1ab910@i28g2000prd.googlegroups.com> Message-ID: <4ae6f506-423c-45c0-96cc-42055b7aa208@w31g2000prd.googlegroups.com> On Apr 11, 6:05?pm, ergconce... at googlemail.com wrote: > On Apr 11, 11:18?pm, George Sakkis wrote: > > > > > The numpy import *is* important if you want to use numpy-specific > > features; there are many "tricks" you can do easily with numpy arrays > > that you have to write manually for, say, regular python lists. For > > example what you want to do is trivial with numpy: > > > >>>import numpy as N > > >>> s = N.array([ 0.84971586, ?0.05786009, ?0.9645675, ?0.84971586, ?0.05786009, > > > 0.9645675, 0.84971586, ?0.05786009, ?0.9645675, ?0.84971586, > > 0.05786009, ?0.9645675])>>> # convert to a 4by3 array in place > > >>> s.shape = (4,3) > > >>> s > > > array([[ 0.84971586, ?0.05786009, ?0.9645675 ], > > ? ? ? ?[ 0.84971586, ?0.05786009, ?0.9645675 ], > > ? ? ? ?[ 0.84971586, ?0.05786009, ?0.9645675 ], > > ? ? ? ?[ 0.84971586, ?0.05786009, ?0.9645675 ]]) > > Thanks very much - works fine! Now for a follow-up question:) > > Actually my original list called "mylist" contains 81217 elements - I > shape those into > > > > >>> len(mylist) > 81217 > >>> s = N.array(mylist) > >>> s.shape = (241,337) > > which works because the "total size of new array must be > unchanged" (241x337=81217) > > Now this "matrix" actually is a 2D colorcoded map - I want to plot > this using the imshow splot routine in mayavi2.. > > However, there might be a problem: I believe that sometimes my > original array will not exactly contain 81217 elements, but maybe only > 81216 or so. Nevertheless, I know that time x times y - structure will > always be the same (that is, 241 columns). What is of interest to me > is the first 241 x 241 part of the matrix anyway. Is there an easy way > to reshape my original array into a symmetric 241x241 matrix? Yes, use numpy.resize: >>> s = N.resize(my_list, (241,241)) You should probably be able to find the answer to most of your upcoming numpy questions at http://numpy.scipy.org/numpydoc/numpy.html (especially the array functions, http://numpy.scipy.org/numpydoc/numpy-9.html) George From m.faustino at gmail.com Sat Apr 11 18:47:42 2009 From: m.faustino at gmail.com (=?ISO-8859-1?Q?M=E1rcio_Faustino?=) Date: Sat, 11 Apr 2009 15:47:42 -0700 (PDT) Subject: Using percent signs with SafeConfigParser References: <50bfe567-7e59-4138-ae55-c3ee2259b169@g17g2000vbi.googlegroups.com> Message-ID: On 11 Abr, 20:06, Peter Otten <__pete... at web.de> wrote: > I think you are right. Please file a bug report . I will. > IMO this doesn't fix the problem because > > (1) it allows "%%%" which is also incorrect You're right, how about this one "(? (2) _interpvar_re has already butchered values like "%%(alpha)s" to "%" Isn't that expected? I mean, one wouldn't write "%%(alpha)s", but instead "%%%(alpha)s" or "%(alpha)s" right? From google at mrabarnett.plus.com Sat Apr 11 19:14:49 2009 From: google at mrabarnett.plus.com (MRAB) Date: Sun, 12 Apr 2009 00:14:49 +0100 Subject: Using percent signs with SafeConfigParser In-Reply-To: References: <50bfe567-7e59-4138-ae55-c3ee2259b169@g17g2000vbi.googlegroups.com> Message-ID: <49E12469.4050108@mrabarnett.plus.com> M?rcio Faustino wrote: > On 11 Abr, 20:06, Peter Otten <__pete... at web.de> wrote: >> I think you are right. Please file a bug report . > > I will. > >> IMO this doesn't fix the problem because >> >> (1) it allows "%%%" which is also incorrect > > You're right, how about this one "(? Instead of checking for a 'bad' value, why not check for a 'good' one? Change: _badpercent_re = re.compile(r"%[^%]|%$") to: _goodpercent_re = re.compile(r"(?:%%|[^%])*$") and then: m = self._badpercent_re.search(tmp_value) if m: to: m = self._goodpercent_re.match(tmp_value) if not m: >> (2) _interpvar_re has already butchered values like "%%(alpha)s" to "%" > > Isn't that expected? I mean, one wouldn't write "%%(alpha)s", but > instead "%%%(alpha)s" or "%(alpha)s" right? > From gagsl-py2 at yahoo.com.ar Sat Apr 11 19:27:08 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sat, 11 Apr 2009 20:27:08 -0300 Subject: Multiprocessing module References: <48224a820904100246n74fd303am43f52c713f2431c9@mail.gmail.com> Message-ID: En Fri, 10 Apr 2009 06:46:47 -0300, Deepak Rokade escribi?: > Since this application is going to be commercial one I want to know at > this > stage if there are any known serious bugs (not limitations) in the > multiprocessing module? Go to http://bugs.python.org/ click on Search on the left, and evaluate how serious may be the known problems in your own context. -- Gabriel Genellina From deets at nospam.web.de Sat Apr 11 19:36:39 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sun, 12 Apr 2009 01:36:39 +0200 Subject: Writing a "Raw" Image to a File (Win, PIL) In-Reply-To: References: <74bs3sF12mi0dU1@mid.uni-berlin.de> Message-ID: <74cns7F12qllkU1@mid.uni-berlin.de> W. eWatson schrieb: > Diez B. Roggisch wrote: >> W. eWatson schrieb: >>> I have an image of described as: >>> Img Info: {} >>> size: (640, 480) >>> format: None >>> mode: P >>> palette: >>> bands: ('P',) >>> type: >>> >>> I'd like to write it to a file. Apparently, I need to convert it to a >>> string first. How do I do that? Pickle? >> >> Did you bother reading the PIL documentation just for about 30seconds? >> >> http://www.pythonware.com/library/pil/handbook/image.htm >> >> Hint: look for "save". >> >> Diez > So, you are telling me what? To save it as a jpg file, or maybe a bmp > file? All of them have headers, right? Yes. Where exactly do you say "I don't want headers, I want raw data dumped to the disk"? Diez From sjmachin at lexicon.net Sat Apr 11 19:46:20 2009 From: sjmachin at lexicon.net (John Machin) Date: Sat, 11 Apr 2009 16:46:20 -0700 (PDT) Subject: Pathological regular expression References: <967fe248-0e15-4247-9d36-6f3f5e0c5eb7@a7g2000yqk.googlegroups.com> <01f0a658$0$20620$c3e8da3@news.astraweb.com> <042e5adf-1aff-4baa-bcef-78d69bc36e32@d38g2000prn.googlegroups.com> <01f0ca1b$0$20620$c3e8da3@news.astraweb.com> Message-ID: <94cef701-79a1-4d45-93b6-d0c96e016464@d19g2000prh.googlegroups.com> On Apr 12, 3:40?am, Steven D'Aprano wrote: > On Sat, 11 Apr 2009 08:40:03 -0700, John Machin wrote: > >> To my mind, this is a bug in the RE engine. Is there any reason to not > >> treat it as a bug? > > > IMHO it's not a bug -- s/hang/takes a long time to compute/ > > > Just look at it: 2 + operators and 3 * operators ... It's one of those > > "come back after lunch" REs. > > Well, it's been running now for about two and a half hours, that's a > rather long lunch. And despite MRAB's assertion, it *cannot* be > interrupted by ctrl-C. That means that to all intents and purposes, the > interpreter has locked up for the duration of the calculation, which may > be days or weeks for all I know. If you don't know, experiment! import re, time _re_comments = re.compile(r'^(([^\\]+|\\.|"([^"\\]+|\\.)*")*)#.*$') for end in ("# yadda", ""): for start in ("", '"#"'): for size in xrange(25): line = start + "x" * size + end print line t0 = time.clock() result = _re_comments_nc.sub(r"\1", line) t1 = time.clock() print "%s (%.6f secs)" % (result, t1 - t0) Observations: 1. Runs at light speed in first two cases (genuine comemnt to be removed) 2. Takes approx O(2**N) in the 3rd case (no # in sight) 3. Takes even longer to produce a *WRONG* result in the 4th case (# in a string literal) 4. The RE assumes that only " can quote a string literal; what about ' ''' and """? Cluesticks: 1. if "#" not in line: dont_need_no_stinking_regexes() 2. try timing on short pieces of input 3. Test the RE, get it correct From google at mrabarnett.plus.com Sat Apr 11 19:48:02 2009 From: google at mrabarnett.plus.com (MRAB) Date: Sun, 12 Apr 2009 00:48:02 +0100 Subject: Writing a "Raw" Image to a File (Win, PIL) In-Reply-To: <74cns7F12qllkU1@mid.uni-berlin.de> References: <74bs3sF12mi0dU1@mid.uni-berlin.de> <74cns7F12qllkU1@mid.uni-berlin.de> Message-ID: <49E12C32.8060105@mrabarnett.plus.com> Diez B. Roggisch wrote: > W. eWatson schrieb: >> Diez B. Roggisch wrote: >>> W. eWatson schrieb: >>>> I have an image of described as: >>>> Img Info: {} >>>> size: (640, 480) >>>> format: None >>>> mode: P >>>> palette: >>>> bands: ('P',) >>>> type: >>>> >>>> I'd like to write it to a file. Apparently, I need to convert it to >>>> a string first. How do I do that? Pickle? >>> >>> Did you bother reading the PIL documentation just for about 30seconds? >>> >>> http://www.pythonware.com/library/pil/handbook/image.htm >>> >>> Hint: look for "save". >>> >>> Diez >> So, you are telling me what? To save it as a jpg file, or maybe a bmp >> file? All of them have headers, right? > > Yes. Where exactly do you say "I don't want headers, I want raw data > dumped to the disk"? > Erm, in the Subject line? :-) From sjmachin at lexicon.net Sat Apr 11 19:53:52 2009 From: sjmachin at lexicon.net (John Machin) Date: Sat, 11 Apr 2009 16:53:52 -0700 (PDT) Subject: Pathological regular expression References: <967fe248-0e15-4247-9d36-6f3f5e0c5eb7@a7g2000yqk.googlegroups.com> <01f0a658$0$20620$c3e8da3@news.astraweb.com> <042e5adf-1aff-4baa-bcef-78d69bc36e32@d38g2000prn.googlegroups.com> <01f0ca1b$0$20620$c3e8da3@news.astraweb.com> <94cef701-79a1-4d45-93b6-d0c96e016464@d19g2000prh.googlegroups.com> Message-ID: On Apr 12, 9:46?am, John Machin wrote: > ? ? ? ? ? ? result = _re_comments_nc.sub(r"\1", line) s/_nc// ... that's an artifact of timing it with Non-Capture groups (?:blahblah) on the two internal groups that don't need to be capturing (results identical, and no perceptible effect on running time) From deets at nospam.web.de Sat Apr 11 20:07:38 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sun, 12 Apr 2009 02:07:38 +0200 Subject: Writing a "Raw" Image to a File (Win, PIL) In-Reply-To: References: <74bs3sF12mi0dU1@mid.uni-berlin.de> <74cns7F12qllkU1@mid.uni-berlin.de> Message-ID: <74cpmaF1361dfU1@mid.uni-berlin.de> MRAB schrieb: > Diez B. Roggisch wrote: >> W. eWatson schrieb: >>> Diez B. Roggisch wrote: >>>> W. eWatson schrieb: >>>>> I have an image of described as: >>>>> Img Info: {} >>>>> size: (640, 480) >>>>> format: None >>>>> mode: P >>>>> palette: >>>>> bands: ('P',) >>>>> type: >>>>> >>>>> I'd like to write it to a file. Apparently, I need to convert it to >>>>> a string first. How do I do that? Pickle? >>>> >>>> Did you bother reading the PIL documentation just for about 30seconds? >>>> >>>> http://www.pythonware.com/library/pil/handbook/image.htm >>>> >>>> Hint: look for "save". >>>> >>>> Diez >>> So, you are telling me what? To save it as a jpg file, or maybe a bmp >>> file? All of them have headers, right? >> >> Yes. Where exactly do you say "I don't want headers, I want raw data >> dumped to the disk"? >> > Erm, in the Subject line? :-) Darn. Should consider reading them. Diez From sjmachin at lexicon.net Sat Apr 11 20:08:08 2009 From: sjmachin at lexicon.net (John Machin) Date: Sat, 11 Apr 2009 17:08:08 -0700 (PDT) Subject: Regex similar to "^(?u)\w$", but without digits? References: Message-ID: On Apr 12, 4:29?am, Andreas wrote: > Hello, > > I'd like to create a regex that captures any unicode character, but > not the underscore and the digits 0-9. [requirement 1] > "^(?u)\w$" captures them also. > Is there a possibility to restrict an expression like "\w" to "\w > without [0-9_]"? [requirement 2] The two requirements are not the same. R1: [^0-9_] matches any character except the underscore and the digits 0-9 R2: To match "like \w except for underscore and digits 0-9", find "negative lookbehind assertion" in the re docs. I've omitted the ^, $ and (?u) because the above advice is general. HTH, John From steve at REMOVE-THIS-cybersource.com.au Sat Apr 11 20:31:44 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 12 Apr 2009 00:31:44 GMT Subject: Pathological regular expression References: <967fe248-0e15-4247-9d36-6f3f5e0c5eb7@a7g2000yqk.googlegroups.com> <01f0a658$0$20620$c3e8da3@news.astraweb.com> <042e5adf-1aff-4baa-bcef-78d69bc36e32@d38g2000prn.googlegroups.com> <01f0ca1b$0$20620$c3e8da3@news.astraweb.com> <94cef701-79a1-4d45-93b6-d0c96e016464@d19g2000prh.googlegroups.com> Message-ID: <01f12a78$0$20620$c3e8da3@news.astraweb.com> On Sat, 11 Apr 2009 16:46:20 -0700, John Machin wrote: > On Apr 12, 3:40?am, Steven D'Aprano cybersource.com.au> wrote: >> On Sat, 11 Apr 2009 08:40:03 -0700, John Machin wrote: >> >> To my mind, this is a bug in the RE engine. Is there any reason to >> >> not treat it as a bug? >> >> > IMHO it's not a bug -- s/hang/takes a long time to compute/ >> >> > Just look at it: 2 + operators and 3 * operators ... It's one of >> > those "come back after lunch" REs. >> >> Well, it's been running now for about two and a half hours, that's a >> rather long lunch. And despite MRAB's assertion, it *cannot* be >> interrupted by ctrl-C. That means that to all intents and purposes, the >> interpreter has locked up for the duration of the calculation, which >> may be days or weeks for all I know. > > If you don't know, experiment! My original test has now been running for close to ten hours now, and still can't be interrupted with ctrl-C. However that's in Python 2.5, having tried it in Python 2.6.2 they can be interrupted, so I'm satisfied that this bug of "regex hangs the interpreter" is not worth reporting, as it effects only older versions. [...] > 3. Test the RE, get it correct They're not my regexes. I don't care whether they are correct or not, I'm more concerned about them locking up the interpreter. -- Steven From metolone+gmane at gmail.com Sat Apr 11 20:31:51 2009 From: metolone+gmane at gmail.com (Mark Tolonen) Date: Sat, 11 Apr 2009 17:31:51 -0700 Subject: Regex similar to "^(?u)\w$", but without digits? References: Message-ID: "Andreas" wrote in message news:f953c845-3660-4bb5-8ba7-00b93989cd20 at b1g2000vbc.googlegroups.com... > Hello, > > I'd like to create a regex that captures any unicode character, but > not the underscore and the digits 0-9. "^(?u)\w$" captures them also. > Is there a possibility to restrict an expression like "\w" to "\w > without [0-9_]"? '(?u)[^\W0-9_]' removes 0-9_ from \w. -Mark From catphive at catphive.net Sat Apr 11 20:36:10 2009 From: catphive at catphive.net (Brendan Miller) Date: Sat, 11 Apr 2009 17:36:10 -0700 Subject: PyHeapTypeObject Message-ID: What's the point of PyHeapTypeObject in Include/object.h? Why does the layout of object types need to be different on the heap vs statically allocated? Thanks, Brendan From castironpi at gmail.com Sat Apr 11 20:41:58 2009 From: castironpi at gmail.com (Aaron Brady) Date: Sat, 11 Apr 2009 17:41:58 -0700 (PDT) Subject: PyHeapTypeObject References: Message-ID: <77239b0c-2253-490e-8fc4-2aaed77a04cb@r3g2000vbp.googlegroups.com> On Apr 11, 7:36?pm, Brendan Miller wrote: > What's the point of PyHeapTypeObject in Include/object.h? Why does the > layout of object types need to be different on the heap vs statically > allocated? > > Thanks, > Brendan Does it need to be garbage collected, finalized, and deallocated? E.g. the string in tp_name. I understand that the 'tp_dict' member is always dynamic though anyway. From castironpi at gmail.com Sat Apr 11 20:45:12 2009 From: castironpi at gmail.com (Aaron Brady) Date: Sat, 11 Apr 2009 17:45:12 -0700 (PDT) Subject: Pathological regular expression References: <967fe248-0e15-4247-9d36-6f3f5e0c5eb7@a7g2000yqk.googlegroups.com> <01f0a658$0$20620$c3e8da3@news.astraweb.com> <042e5adf-1aff-4baa-bcef-78d69bc36e32@d38g2000prn.googlegroups.com> <01f0ca1b$0$20620$c3e8da3@news.astraweb.com> <94cef701-79a1-4d45-93b6-d0c96e016464@d19g2000prh.googlegroups.com> <01f12a78$0$20620$c3e8da3@news.astraweb.com> Message-ID: On Apr 11, 7:31?pm, Steven D'Aprano wrote: _ > My original test has now been running for close to ten hours now, and > still can't be interrupted with ctrl-C. However that's in Python 2.5, > having tried it in Python 2.6.2 they can be interrupted, so I'm satisfied > that this bug of "regex hangs the interpreter" is not worth reporting, as > it effects only older versions. > > [...] > > > 3. Test the RE, get it correct > > They're not my regexes. I don't care whether they are correct or not, I'm > more concerned about them locking up the interpreter. > > -- > Steven I don't believe in second consoles or kill(1)... or telnet. I wonder if the hanging is symptomatic of a wider-spread bug, that shorter operations would never reveal. ... From sjmachin at lexicon.net Sat Apr 11 21:08:20 2009 From: sjmachin at lexicon.net (John Machin) Date: Sat, 11 Apr 2009 18:08:20 -0700 (PDT) Subject: Pathological regular expression References: <967fe248-0e15-4247-9d36-6f3f5e0c5eb7@a7g2000yqk.googlegroups.com> <01f0a658$0$20620$c3e8da3@news.astraweb.com> <042e5adf-1aff-4baa-bcef-78d69bc36e32@d38g2000prn.googlegroups.com> <01f0ca1b$0$20620$c3e8da3@news.astraweb.com> <94cef701-79a1-4d45-93b6-d0c96e016464@d19g2000prh.googlegroups.com> <01f12a78$0$20620$c3e8da3@news.astraweb.com> Message-ID: <74705051-0e44-4709-8efb-0da73ec7c643@v35g2000pro.googlegroups.com> On Apr 12, 10:31?am, Steven D'Aprano wrote: > On Sat, 11 Apr 2009 16:46:20 -0700, John Machin wrote: > > On Apr 12, 3:40?am, Steven D'Aprano > cybersource.com.au> wrote: > >> On Sat, 11 Apr 2009 08:40:03 -0700, John Machin wrote: > >> >> To my mind, this is a bug in the RE engine. Is there any reason to > >> >> not treat it as a bug? > > >> > IMHO it's not a bug -- s/hang/takes a long time to compute/ > > >> > Just look at it: 2 + operators and 3 * operators ... It's one of > >> > those "come back after lunch" REs. > > >> Well, it's been running now for about two and a half hours, that's a > >> rather long lunch. And despite MRAB's assertion, it *cannot* be > >> interrupted by ctrl-C. That means that to all intents and purposes, the > >> interpreter has locked up for the duration of the calculation, which > >> may be days or weeks for all I know. > > > If you don't know, experiment! > > My original test has now been running for close to ten hours now, and > still can't be interrupted with ctrl-C. However that's in Python 2.5, What platform are you running on? It works OK for me back to 2.1 on Windows XP SP3: Traceback (most recent call last): File "weirdre.py", line 34, in ? result = _re_comments.sub(r"\1", line) File "C:\python21\lib\sre.py", line 164, in _sub return _subn(pattern, template, string, count)[0] File "C:\python21\lib\sre.py", line 179, in _subn m = c.search() KeyboardInterrupt > having tried it in Python 2.6.2 they can be interrupted, so I'm satisfied > that this bug of "regex hangs the interpreter" is not worth reporting, as > it effects only older versions. > > [...] > > > 3. Test the RE, get it correct > > They're not my regexes. I don't care whether they are correct or not, I'm > more concerned about them locking up the interpreter. That's understood -- it was a shotgun reply to the OP and most responders; each can take their own pellets ;-) From jjposner at snet.net Sat Apr 11 21:21:18 2009 From: jjposner at snet.net (John Posner) Date: Sat, 11 Apr 2009 21:21:18 -0400 Subject: Generators/iterators, Pythonicity, and primes In-Reply-To: References: Message-ID: <49E1420E.3030005@snet.net> Arnaud Delobelle wrote: > You could do something like this with the help of itertools.ifilter: > > prime_gen = ifilter( > lambda n, P=[]: all(n%p for p in P) and not P.append(n), > count(2) > ) > Formidable! (both the English and French meanings) This is the most elegant, Sieve of Eratosthenes-like solution. > I omitted the 'if p <= sqrt(n)' as it doesn't help the generator go > faster. You could use itertools.takewhile for that: > > prime_gen = ifilter( > lambda n, P=[]: > all(n%p for p in takewhile(lambda p, s=n**0.5: p<=s, P)) > and not P.append(n), > count(2) > ) > Do know what in the itertools implementation causes adding a 'if p <= sqrt(n)' clause to *decrease* performance, while adding a 'takewhile()' clause *increases* performance? > Of course there is no excuse for writing Python like that :) +1 (but it's fun every once in a while, eh?) Thanks a lot! -John From grante at visi.com Sat Apr 11 21:36:06 2009 From: grante at visi.com (Grant Edwards) Date: Sat, 11 Apr 2009 20:36:06 -0500 Subject: How to create a virtual serial port? References: <_cCdndEvsrp8PULUnZ2dnUVZ_qydnZ2d@pdx.net> <5fd78a41-ed64-4e3e-aa80-a89c8f44cf3f@v6g2000vbb.googlegroups.com> <6LGdnSbyPsL1fX3UnZ2dnUVZ_uqdnZ2d@pdx.net> Message-ID: <5_udnQjsYLMb2HzUnZ2dnUVZ_sOdnZ2d@posted.usinternet> On 2009-04-11, Scott David Daniels wrote: > Grant Edwards wrote: >> ...If he was going to plug the device into a real serial port on >> the machine inquestion, then he wouldn't need a virtual serial >> port. >> >>> I don't see anywhere on my laptop I could plug in anything but >>> a USB connector, an ethernet connector, a firewire connector, >>> headphones, speakers, a display connector, or a power cord. >> >> He's not trying to plug the device into your laptop. > > Yup. It seems to me you could write a slug to talk USB driver > to OS/X, USB is an icky, nasty, complicated protocol. It has Microsoft's fingerprints all over it. Adding support for more ioctl() calls to the pty driver would be a lot easier. -- Grant From grante at visi.com Sat Apr 11 21:45:09 2009 From: grante at visi.com (Grant Edwards) Date: Sat, 11 Apr 2009 20:45:09 -0500 Subject: How to create a virtual serial port? References: <_cCdndEvsrp8PULUnZ2dnUVZ_qydnZ2d@pdx.net> <5fd78a41-ed64-4e3e-aa80-a89c8f44cf3f@v6g2000vbb.googlegroups.com> Message-ID: On 2009-04-11, Dennis Lee Bieber wrote: >> You can write a port redirector in user-space in MS-Windows, >> but you can't in Linux/Unix. On Unix systems you have to write >> a kernel module that sits below the tty layer. The tty layer >> is what user applications talk to with open/close/read/write/ioctl >> calls. > > Extending to the point of absurdity... Does the machine have a > physical serial port from which the application could read? I believe not. > Does the machine have a USB port? Definitely. All Mac's have had USB ports for a long, long time > What would setting a pair of serial ports (possibly one or > both using USB<>Serial adapters) with a null modem between > them give? That should work as long as the application isn't trying to do timing-critical tricks with the modem control and status lines. Such tricks don't work well with USB attached serial ports. > Could the application read from one, while Python is writing > to the other? Yup. > Yes, it IS an absurd klutz, but... Sadly, if a pty won't work, that's probably the easiest solution. You can get USB-serial adapters from newegg for under $10. For a slightly less ugly kludge, you can get a dual-port one for $25. And when you need to test the effects of a high-latency, low-bandwith WAN connection on something, you can plug them together with a null-modem cable, set them up as PPP interfaces running at 1200 baud, and route network traffic through them. -- Grant From usenet at janc.invalid Sat Apr 11 23:02:06 2009 From: usenet at janc.invalid (JanC) Date: Sun, 12 Apr 2009 03:02:06 +0000 (UTC) Subject: Python 2.6/3.0 packages for Ubuntu? References: <49e06198$0$6834$5fc30a8@news.tiscali.it> Message-ID: Francesco Bochicchio wrote: > skip at pobox.com ha scritto: >> Does Ubuntu really not have Python 2.6 or 3.0 packages or do I just have my >> package list misconfigured? I'm setting up a fresh machine and am not too >> Ubuntu-aware. Is there a list of package repositories around somewhere? > In current 8.10, the default python is 2.5, but there is a set of > packages python3-... which gives you the 3.0. It has a 3.0 beta I think (the final version was released too late to include it). > The upcoming 09.04 will have 2.6 as standard, I think ... 2.6.1 actually, and it has 3.0.1 too, as well as versions of 2.4 (for zope2) and 2.5. Plus jython & ironpython... ;) http://packages.ubuntu.com/python http://packages.ubuntu.com/python2.6 http://packages.ubuntu.com/python3 (For Debian use "packages.debian.org" instead.) -- JanC From ldo at geek-central.gen.new_zealand Sat Apr 11 23:16:45 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sun, 12 Apr 2009 15:16:45 +1200 Subject: llvm vs. parrot References: <1239405385.6357.1.camel@linux-3eb6.site> Message-ID: In message <1239405385.6357.1.camel at linux-3eb6.site>, Paul Watson wrote: > Is Parrot out of favor these days? Nonsense. It's just resting. From usenet at janc.invalid Sat Apr 11 23:20:30 2009 From: usenet at janc.invalid (JanC) Date: Sun, 12 Apr 2009 03:20:30 +0000 (UTC) Subject: How to create a virtual serial port? References: <_cCdndEvsrp8PULUnZ2dnUVZ_qydnZ2d@pdx.net> <5fd78a41-ed64-4e3e-aa80-a89c8f44cf3f@v6g2000vbb.googlegroups.com> Message-ID: Scott David Daniels wrote: > I'ms confused by this statement. What physical connector does your > "serial port" use to get the serial data to the Mac? I only have one > 3-year old Mac laptop to look at, and I just don't see anything that I > would call a serial port. USB *is* a serial port... that's what the "S" stands for. ;) -- JanC From usenet at janc.invalid Sat Apr 11 23:25:25 2009 From: usenet at janc.invalid (JanC) Date: Sun, 12 Apr 2009 03:25:25 +0000 (UTC) Subject: How to create a virtual serial port? References: Message-ID: Grant Edwards wrote: > On 2009-04-10, Stuart Davenport wrote: > >> I am trying to work out if its possible, to create a virtual serial >> port with Python? > > On Linux: no. I wonder if there is no way to emulate ptys from userspace? (Like you can use FUSE to implement filesystems in python.) -- JanC From jcn-france1972 at pobox.com Sat Apr 11 23:42:27 2009 From: jcn-france1972 at pobox.com (Jean-Claude Neveu) Date: Sat, 11 Apr 2009 20:42:27 -0700 Subject: Regular Expression Help Message-ID: Hello, I was wondering if someone could tell me where I'm going wrong with my regular expression. I'm trying to write a regexp that identifies whether a string contains a correctly-formatted currency amount. I want to support dollars, UK pounds and Euros, but the example below deliberately omits Euros in case the Euro symbol get mangled anywhere in email or listserver processing. I also want people to be able to omit the currency symbol if they wish. My regexp that I'm matching against is: "^\$\??\d{0,10}(\.\d{2})?$" Here's how I think it should work (but clearly I'm wrong, because it does not actually work): ^\$\?? Require zero or one instance of $ or ? at the start of the string. d{0,10} Next, require between zero and ten alpha characters. (\.\d{2})? Optionally, two characters can follow. They must be preceded by a decimal point. Examples of acceptable input should be: $12.42 $12 ?12.42 $12,482.96 (now I think about it, I have not catered for this in my regexp) And unacceptable input would be: $12b.42 blah $blah etc Here is my Python script: # import re def is_currency(str): rex = "^\$\??\d{0,10}(\.\d{2})?$" if re.match(rex, str): return 1 else: return 0 def test_match(str): if is_currency (str): print str + " is a match" else: print str + " is not a match" # All should match except the last two test_match("$12.47") test_match("12.47") test_match("?12.47") test_match("?12") test_match("$12") test_match("$12588.47") test_match("$12,588.47") test_match("?12588.47") test_match("12588.47") test_match("?12588") test_match("$12588") test_match("blah") test_match("$12b.56") AND HERE IS THE OUTPUT FROM THE ABOVE SCRIPT: $12.47 is a match 12.47 is not a match ?12.47 is not a match ?12 is not a match $12 is a match $12588.47 is a match $12,588.47 is not a match ?12588.47 is not a match 12588.47 is not a match ?12588 is not a match $12588 is a match blah is not a match $12b.56 is not a match Many thanks in advance. Regular expressions are not my strong suit :) J-C From mail at johnohagan.com Sun Apr 12 00:00:40 2009 From: mail at johnohagan.com (John O'Hagan) Date: Sun, 12 Apr 2009 04:00:40 +0000 Subject: any(), all() and empty iterable Message-ID: <200904120400.41678.mail@johnohagan.com> Hi, I was getting some surprising false positives as a result of not expecting this: all(element in item for item in iterable) to return True when 'iterable' is empty. I guess it goes into hairy Boolean territory trying to decide if an element is in an item that doesn't exist (if that's what's happening), but I would have thought not. It seems inconsistent with the behaviour of any(element in item for item in iterable) which returns False when 'iterable' is empty. Sorry if this has come up before, but 'any' and 'all' make for fruitless googling! Any light to be shed? Regards, John From ooklah at gmail.com Sun Apr 12 00:10:20 2009 From: ooklah at gmail.com (ookrin) Date: Sat, 11 Apr 2009 21:10:20 -0700 (PDT) Subject: Python and XML Help Message-ID: <08d683de-df71-43c0-8ede-90b15faeec13@z19g2000vbz.googlegroups.com> I'm in the process of learning python and PyQt4. I had decided to make myself a simple app and soon discovered that I needed to crash into xml to use some of the data I was going to be getting off of the server. I picked up enough xml to use the sax parser to get the data out of the xml. I can get as far as printing it to the screen, but there is where I get stuck.... I can't seem to send the data to anywhere else, into another variable, to another function. The parser stops after the first line it finds. class offlineLoad(): def loadXmlFile(self): print "Loading from File" xf = open('CharacterID.xml','r') xml = xmlHandler() saxparser = make_parser() print "parser created" saxparser.setContentHandler(xml) print "parser runn" try: saxparser.parse(xf) except: print "Error Reading xml" class xmlHandler(ContentHandler): def startElement(self, name, attrs): self.charList = [] charName = [] charID = [] if name == "row": charName = attrs.get("name", "") charID = attrs.get("characterID", "") print charName, '\t', charID self.buildlist(self,charName,charID) #self.test() def test(self): print "TeST" def buildlist(self,charName,charID): print charName, '\t',charID so here... test will print, but buildlist makes the parser stop and print "Error reading XML" after the first row it finds. Appending charName and charID to another variable makes it stop as well. I'm confused at this point From rurpy at yahoo.com Sun Apr 12 00:19:06 2009 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Sat, 11 Apr 2009 21:19:06 -0700 (PDT) Subject: Regular Expression Help References: Message-ID: On Apr 11, 9:42 pm, Jean-Claude Neveu wrote: > My regexp that I'm matching against is: "^\$\??\d{0,10}(\.\d{2})?$" > > Here's how I think it should work (but clearly > I'm wrong, because it does not actually work): > > ^\$\?? Require zero or one instance of $ or ? at the start of the string. The "or" in "$ or ?" above is a vertical bar. You want ^(\$|?)? here. > d{0,10} Next, require between zero and ten alpha characters. > (\.\d{2})? Optionally, two characters can > follow. They must be preceded by a decimal point. From phynkel at gmail.com Sun Apr 12 00:30:06 2009 From: phynkel at gmail.com (Petyr David) Date: Sat, 11 Apr 2009 21:30:06 -0700 (PDT) Subject: Python student seeks help regex/strings Message-ID: I'm familiar with simple string substitutions in shell and perl: string= "I have 30 days to find it" echo $string | sed "s/\(.*\)\(30\)\(.*\)/\2/ which will (if I did it right) leave 30 and perl likewise. I'm having trouble figuring out how to do this in Python and it's not for lack of trying to find an example. It can't be this hard is it? Hopefully I will limit my newbie questions. Thanks Peter From clp2 at rebertia.com Sun Apr 12 00:32:32 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 11 Apr 2009 21:32:32 -0700 Subject: any(), all() and empty iterable In-Reply-To: <200904120400.41678.mail@johnohagan.com> References: <200904120400.41678.mail@johnohagan.com> Message-ID: <50697b2c0904112132i62c3cd50rec3a80f1866a8d63@mail.gmail.com> On Sat, Apr 11, 2009 at 9:00 PM, John O'Hagan wrote: > Hi, > > I was getting some surprising false positives as a result of not expecting > this: > > all(element in item for item in iterable) > > to return True when 'iterable' is empty. > > I guess it goes into hairy Boolean territory trying to decide if an element is > in an item that doesn't exist (if that's what's happening), but I would have > thought not. It seems inconsistent with the behaviour of > > any(element in item for item in iterable) > > which returns False when 'iterable' is empty. > > Sorry if this has come up before, but 'any' and 'all' make for fruitless > googling! > > Any light to be shed? This is justified in formal logic by the concept of vacuous truth. See http://en.wikipedia.org/wiki/Vacuous_truth all(P(x) for x in y) #Original Python statement ?x?y. P(x) #equivalent in formal logic ??[?x?y. P(x)] #apply Double Negation ?[?x?y. ?P(x)] #negate Universal Quantifier English interpretation: There does not exist a counterexample `x` in the set `y` which fails to satisfy predicate `P`. Comment: Indeed, in the case the set `y` is empty, there is no counterexample to be had, and thus both the final statement and the initial equivalent one are vacuously true. Yes, this may seem unintuitive at first. Logic is like that sometimes. Python equivalent to final statement: not any(not P(x) for x in y) Comment: Likewise, in the case of a empty `y`, any() returns False, so the code simplifies to `not False`, which obviously simplifies further to just True. So, if you agree with the behavior of any(), you must logically agree with the behavior of all(). ;-) Discrete mathematics FTW! Cheers, Chris -- I have a blog: http://blog.rebertia.com From marduk at letterboxes.org Sun Apr 12 00:39:11 2009 From: marduk at letterboxes.org (Albert Hopkins) Date: Sun, 12 Apr 2009 00:39:11 -0400 Subject: any(), all() and empty iterable In-Reply-To: <200904120400.41678.mail@johnohagan.com> References: <200904120400.41678.mail@johnohagan.com> Message-ID: <1239511151.28008.3.camel@blackwidow.nbk> On Sun, 2009-04-12 at 04:00 +0000, John O'Hagan wrote: > Hi, > > I was getting some surprising false positives as a result of not expecting > this: > > all(element in item for item in iterable) > > to return True when 'iterable' is empty. > > I guess it goes into hairy Boolean territory trying to decide if an element is > in an item that doesn't exist (if that's what's happening), but I would have > thought not. It seems inconsistent with the behaviour of > > any(element in item for item in iterable) > > which returns False when 'iterable' is empty. > >From the docs: all(iterable) Return True if all elements of the iterable are true. Equivalent to: def all(iterable): for element in iterable: if not element: return False return True any(iterable) Return True if any element of the iterable is true. Equivalent to: def any(iterable): for element in iterable: if element: return True return False > Sorry if this has come up before, but 'any' and 'all' make for fruitless > googling! Try Googling for: "any site:docs.python.org" From kyrie at uh.cu Sun Apr 12 00:46:32 2009 From: kyrie at uh.cu (Luis Alberto Zarrabeitia Gomez) Date: Sun, 12 Apr 2009 00:46:32 -0400 Subject: any(), all() and empty iterable In-Reply-To: <200904120400.41678.mail@johnohagan.com> References: <200904120400.41678.mail@johnohagan.com> Message-ID: <1239511592.49e1722825d03@mail.uh.cu> Quoting John O'Hagan : > Hi, > > I was getting some surprising false positives as a result of not expecting > this: > > all(element in item for item in iterable) > > to return True when 'iterable' is empty. > [...] > any(element in item for item in iterable) > > which returns False when 'iterable' is empty. The "any" and "all" functions for python2.5 mirror the existential[1] and uiniversal[2] quantifiers from predicate logic. In layman terms, "any" returns true if there exists at least one element in the collection for which the predicate ("element in item" in your case) is true. If the collection is empty, there are no elements, and in particular, there can be no elements that satisfy your predicate (returns false). This function/quantifier is well defined on an empty set. "all" returns true if for every element in the collection, the predicate holds. If you have an empty set, there is no way that the predicate could evaluate to false, thus, it return true. While this may seem arbitrary at first, keep in mind that one expects the phrase "all this values in this set are true" to mean the same as "there are no false values in this set", i.e, all(x for x in list) == not any(not x for x in list). So, the universal quantifier/python's "all" function may be easier to understand as: "returns true if there is no element in the set that for which the predicate is false". These would be "any" and "all" implementations in pure python: def any(elements): for element in elements: if element: return True return False def all(elements): for element in elements: if not element: return False return True You may also want to look at [3] K. P.S: Forgive my english. I'm falling asleep... I hope I made sense. [1] http://en.wikipedia.org/wiki/Existential_quantification [2] http://en.wikipedia.org/wiki/Universal_quantification [3] http://en.wikipedia.org/wiki/Vacuous_truth -- 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 sjmachin at lexicon.net Sun Apr 12 01:40:00 2009 From: sjmachin at lexicon.net (John Machin) Date: Sat, 11 Apr 2009 22:40:00 -0700 (PDT) Subject: Regular Expression Help References: Message-ID: On Apr 12, 2:19?pm, ru... at yahoo.com wrote: > On Apr 11, 9:42 pm, Jean-Claude Neveu > wrote: > > > My regexp that I'm matching against is: "^\$\??\d{0,10}(\.\d{2})?$" > > > Here's how I think it should work (but clearly > > I'm wrong, because it does not actually work): > > > ^\$\?? ? ? ?Require zero or one instance of $ or ? at the start of the string. > > The "or" in "$ or ?" above is a vertical bar. ?You > want ^(\$|?)? here. Best not to use a capturing group (blah) when you don't need to capture ... use (?:blah) instead. When the alternatives are all single characters, for greater typing efficiency and computing efficiency use a character class: ^[\$?]? From murali.au at gmail.com Sun Apr 12 01:53:07 2009 From: murali.au at gmail.com (Murali kumar) Date: Sun, 12 Apr 2009 11:23:07 +0530 Subject: communication between objects - help In-Reply-To: <49E093BE.5060504@dejaviewphoto.com> References: <49DE4521.7040605@probo.com> <49DF2056.6060208@ieee.org> <49E093BE.5060504@dejaviewphoto.com> Message-ID: Thanks a lot for help.. On Sat, Apr 11, 2009 at 6:27 PM, Dave Angel wrote: > > > Murali kumar wrote: > >> thanks a lot.. >> >> I think passing the main object only by reference.. so, this does not >> causes >> any overhead.. >> >> am i correct..? >> >> >> On Fri, Apr 10, 2009 at 4:02 PM, Dave Angel wrote: >> >> >> >>> Murali kumar wrote: >>> >>> >>> >>>> hi all.. >>>> I'm posted in a word doc becoz to add a image to explain my problem.. >>>> also I think gmail automatically scans for attachments.. >>>> >>>> anyway.. here's my problem...( see the image) >>>> >>>> http://www.2shared.com/file/5299759/45e4c614/load.html >>>> >>>> Using : Python 2.6 , wxPython 2.8.9 >>>> <....snip...> >>>> >>>> * I don?t know how to return config file or data to mainApp object when >>>> pressing load button. * >>>> >>>> >>>> * Is it easy to return filename only and load the file?s data into >>>> mainApp >>>> object in menu handler itself? * >>>> >>>> >>>> Please suggest me right direction.. and tell me how to do it? >>>> >>>> Usually where I can get these informations.. (suggest links and books..) >>>> >>>> >>>> Thanks for any advice.... >>>> >>>> >>>> >>>> >>>> >>> Most of the widget classes in your code should be derived classes, so >>> they >>> can hold extra instance data & event handlers and such. So that means >>> you >>> can have extra parameters on the constructor, besides the ones the base >>> class requires. Use one or more of those extra parameters to store your >>> own >>> information about the hierarchy. >>> >>> Simplest example is to add the app instance to each constructor. Then >>> each >>> widget object would know how to call back into the app to do some work, >>> or >>> to load data into a common place. >>> >>> DaveA >>> >>> -- >>> http://mail.python.org/mailman/listinfo/python-list >>> >>> >>> >> All objects are passed by reference in python. And storing it in each >> widget costs very little. Note that you can choose what object or objects >> should be used; I just mention the app as a choice that everyone will >> subclass, and that will generally have direct or indirect access to all >> widgets. >> >> > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From n00m at narod.ru Sun Apr 12 01:59:08 2009 From: n00m at narod.ru (n00m) Date: Sat, 11 Apr 2009 22:59:08 -0700 (PDT) Subject: Python student seeks help regex/strings References: Message-ID: <2c6d8c4f-e21b-4cad-af85-77b97cc6a013@b1g2000vbc.googlegroups.com> >>> s = 'I have 30 days to find it' >>> ss = filter(lambda si: si.isdigit(), s) >>> ss '30' >>> From sjmachin at lexicon.net Sun Apr 12 02:10:33 2009 From: sjmachin at lexicon.net (John Machin) Date: Sat, 11 Apr 2009 23:10:33 -0700 (PDT) Subject: Python student seeks help regex/strings References: Message-ID: <8bc9a99c-05d9-4eff-9785-5d66703374f6@s38g2000prg.googlegroups.com> On Apr 12, 2:30?pm, Petyr David wrote: > I'm familiar with simple string substitutions in shell and perl: > > string= "I have 30 days to find it" > > echo $string | sed "s/\(.*\)\(30\)\(.*\)/\2/ > > which will (if I did it right) leave 30 > > and perl likewise. > > I'm having trouble figuring out how to do this in Python and it's not > for lack of trying to find an example. It can't be this hard is it? No, it isn't; there are examples in the docs (http://docs.python.org/ library/re.html#re.sub) and the HOWTO (http://docs.python.org/dev/ howto/regex.html#search-and-replace) ... how did you manage to avoid finding them? >>> import re >>> strg = "yadda 30 blah" >>> re.sub(r"(.*)(30)(.*)", r"\2", strg) '30' From __peter__ at web.de Sun Apr 12 03:12:05 2009 From: __peter__ at web.de (Peter Otten) Date: Sun, 12 Apr 2009 09:12:05 +0200 Subject: Using percent signs with SafeConfigParser References: <50bfe567-7e59-4138-ae55-c3ee2259b169@g17g2000vbi.googlegroups.com> Message-ID: M?rcio Faustino wrote: > On 11 Abr, 20:06, Peter Otten <__pete... at web.de> wrote: >> (2) _interpvar_re has already butchered values like "%%(alpha)s" to "%" > > Isn't that expected? I mean, one wouldn't write "%%(alpha)s", but > instead "%%%(alpha)s" or "%(alpha)s" right? "%%(alpha)s" would mean that you want the value to be "%(alpha)s" (unlikely, but possible). Peter From ldo at geek-central.gen.new_zealand Sun Apr 12 03:29:48 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sun, 12 Apr 2009 19:29:48 +1200 Subject: How to create a virtual serial port? References: <_cCdndEvsrp8PULUnZ2dnUVZ_qydnZ2d@pdx.net> <5fd78a41-ed64-4e3e-aa80-a89c8f44cf3f@v6g2000vbb.googlegroups.com> Message-ID: In message , JanC wrote: > USB *is* a serial port... that's what the "S" stands for. ;) And the "B" stands for "bus". It's a serial bus, not a serial port. From deets at nospam.web.de Sun Apr 12 03:51:53 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sun, 12 Apr 2009 09:51:53 +0200 Subject: Python and XML Help In-Reply-To: <08d683de-df71-43c0-8ede-90b15faeec13@z19g2000vbz.googlegroups.com> References: <08d683de-df71-43c0-8ede-90b15faeec13@z19g2000vbz.googlegroups.com> Message-ID: <74dksqF12ss8dU1@mid.uni-berlin.de> ookrin schrieb: > I'm in the process of learning python and PyQt4. I had decided to make > myself a simple app and soon discovered that I needed to crash into > xml to use some of the data I was going to be getting off of the > server. > > I picked up enough xml to use the sax parser to get the data out of > the xml. I can get as far as printing it to the screen, but there is > where I get stuck.... I can't seem to send the data to anywhere else, > into another variable, to another function. The parser stops after the > first line it finds. > > class offlineLoad(): > def loadXmlFile(self): > print "Loading from File" > xf = open('CharacterID.xml','r') > xml = xmlHandler() > saxparser = make_parser() > print "parser created" > saxparser.setContentHandler(xml) > print "parser runn" > try: > saxparser.parse(xf) > except: > print "Error Reading xml" This is a very bad thing to do - there are only very few justified cases of catch-all for exceptions. This certainly isn't one, as it suppresses the real cause for what is happening. Which makes it harder for you & for us to diagnose the problem, because you don't show (and currently can't) the stacktrace to us. > class xmlHandler(ContentHandler): > def startElement(self, name, attrs): > self.charList = [] If this shall gather some state over the coures of time, this is the wrong place to initialize it, as it will get overwritten with each element. > charName = [] > charID = [] There is no need to "prefill" variables. And it's confusing you do it with a different type than what you assign to them below. > if name == "row": > charName = attrs.get("name", "") > charID = attrs.get("characterID", "") > print charName, '\t', charID > self.buildlist(self,charName,charID) > #self.test() > > def test(self): > print "TeST" > def buildlist(self,charName,charID): > print charName, '\t',charID > > so here... test will print, but buildlist makes the parser stop and > print "Error reading XML" after the first row it finds. Appending > charName and charID to another variable makes it stop as well. I'm > confused at this point Please show us the stacktrace you suppress. Then help might be possible. Diez From __peter__ at web.de Sun Apr 12 03:57:48 2009 From: __peter__ at web.de (Peter Otten) Date: Sun, 12 Apr 2009 09:57:48 +0200 Subject: Using percent signs with SafeConfigParser References: <50bfe567-7e59-4138-ae55-c3ee2259b169@g17g2000vbi.googlegroups.com> Message-ID: MRAB wrote: > M?rcio Faustino wrote: > > On 11 Abr, 20:06, Peter Otten <__pete... at web.de> wrote: > >> I think you are right. Please file a bug report . > > > > I will. > > > >> IMO this doesn't fix the problem because > >> > >> (1) it allows "%%%" which is also incorrect > > > > You're right, how about this one "(? > > Instead of checking for a 'bad' value, why not check for a 'good' one? > > Change: > > _badpercent_re = re.compile(r"%[^%]|%$") > > to: > > _goodpercent_re = re.compile(r"(?:%%|[^%])*$") > > and then: > > m = self._badpercent_re.search(tmp_value) > if m: > > to: > > m = self._goodpercent_re.match(tmp_value) > if not m: Configparser also knows a construct "%(whatever)s" which works like Python's string formatting and takes "whatever" from the "DEFAULT" section of the config file. I like your approach, but I think the regex should be _goodpercent_re = re.compile(r"^(?:%%|%\([^)]+\)s|[^%])*$" or similar -- what is currently allowed within the parens seems to be an implementation accident... Peter From deets at nospam.web.de Sun Apr 12 04:00:04 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sun, 12 Apr 2009 10:00:04 +0200 Subject: Unsupported operand types in if/else list comprehension In-Reply-To: References: <74bdopF126tl6U1@mid.uni-berlin.de> Message-ID: <74dlc5F131906U1@mid.uni-berlin.de> Mike H schrieb: > Sigh. One more. And again, thank you for all of the help. > > I realized that the last version that I posted took care of an SQL > injection problem for the values, but not for the fields. So, I went > ahead and modified the code: > > def new_insert_cmd(myTable, myFields, myValues): > """Imports given fields and values into a given table, returns the > Autoincrement value.""" > SQLcmd="INSERT INTO " + myTable + " ( " + > create_input_string(myFields) + " ) VALUES ( " \ > + create_input_string(myValues) +" );" > allArguments=myFields+myValues > cursor.execute(SQLcmd, (allArguments)) > > create_input_strings() is just a function that creates the necessary > number of %s's for a given list (and which I'm sure there's a faster > way to code): > > def create_input_string(myList): > sOut="" > for var in myList: > sOut=sOut+"%s, " > return sOut[:-2] > > However, now the cursor.execute statement won't work. I've looked at > the content of SQLcmd and the values of allArguments and they seem > fine. > > I've even tried running this at the IDLE command line: > > cursor.execute("INSERT INTO plan (%s, %s, %s) VALUES (%s, %s, %s);", > (["name", "fileno", "size", "Test", "AAA-000", 7])) and I get this > error: > > File "C:\Python25\lib\site-packages\MySQLdb\cursors.py", line 166, in execute > self.errorhandler(self, exc, value) > File "C:\Python25\lib\site-packages\MySQLdb\connections.py", line > 35, in defaulterrorhandler > raise errorclass, errorvalue > ProgrammingError: (1064, "You have an error in your SQL syntax; check > the manual that corresponds to your MySQL server version for the right > syntax to use near ''name', 'fileno', 'size') VALUES ('Test', > 'AAA-000', 7)' at line 1") > > Can I not use the cursor.execute command to pass variables that aren't > immediately next to each other? If so, is there a better way to go > about solving this problem? The escaping mechanism is only working (and supposedly so) for *values*, not for creating SQL. To prevent SQL-injection don't allow dynamic content in there (there shouldn't be any need anyway, because your datamodel isn't changing due to user-input) Diez From stef.mientki at gmail.com Sun Apr 12 05:23:38 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Sun, 12 Apr 2009 11:23:38 +0200 Subject: video capture in Python ? Message-ID: <49E1B31A.5020806@gmail.com> hello, has anyone got video capturing (from a webcam) successful running in a wxPython application under winXP ? I got some links, but it seems quit complicated to get everything installed, and all the packages you need don't describe which versions of the other parts you need, so a lot trial and error I expect. thanks, Stef Mientki From arnodel at googlemail.com Sun Apr 12 07:01:44 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Sun, 12 Apr 2009 12:01:44 +0100 Subject: Q:Pythonic way to create list of lists References: Message-ID: grkuntzmd at gmail.com writes: > I am just learning Python. > > I am trying to create a list of empty lists: [[], [], [], ...] (10 > items total). > > What is the most Pythonic way to do this? > > If I use a list comprehension (as in myList = [[] for item in xrange > (0, 10)]), Netbeans warns me that 'item' is never used. IMHO this is the best way. > If I use a for-loop (as in for item in myList = []; for item in xrange > (0, 10): myList.append([])), Netbeans still warns me of the same > thing. > > If I use '*' (as myList = [[]] * 10), all of the empty lists refer to > the same object; changing one changes them all. > > Do I have to live with the warning, or is there a "better" way? You could do this: >>> map(list, [()]*10) [[], [], [], [], [], [], [], [], [], []] But it is wasteful as it creates two lists. There are some contrived ways to go around this using using itertools, e.g. >>> from itertools import * >>> map(list, repeat((), 10)) [[], [], [], [], [], [], [], [], [], []] Note that if you use Python 3 you would get an iterator so it would need to be wrapped in a list() call. Or you could take advantage of iter(function, sentinel) which is little used: >>> list(islice(iter(list, None), 10)) [[], [], [], [], [], [], [], [], [], []] But none of these are very compelling. -- Arnaud From ldo at geek-central.gen.new_zealand Sun Apr 12 07:16:19 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sun, 12 Apr 2009 23:16:19 +1200 Subject: Definition of Pythonic? References: Message-ID: Pythonic--an acid, capable of reacting with bases to form pythanates? Would it be an organic or inorganic acid? Deprive it of a bit of oxygen, and it becomes pythonous, reacting to form pythonites. What do you mean, the fume cupboard's broken down? Honestly I feel fine... From tleeuwenburg at gmail.com Sun Apr 12 07:26:28 2009 From: tleeuwenburg at gmail.com (TennesseeLeeuwenburg) Date: Sun, 12 Apr 2009 04:26:28 -0700 (PDT) Subject: Multithreading / multiprocess References: <57065c62-2024-47b5-a07e-1d60ff85ba23@y10g2000prc.googlegroups.com> <74bhbpF12ks15U1@mid.uni-berlin.de> Message-ID: <13ad3747-80b0-4630-b613-b5f500f8aa86@f41g2000pra.googlegroups.com> On Apr 11, 10:39?pm, "Diez B. Roggisch" wrote: > tleeuwenb... at gmail.com schrieb: > > > Is there anyway to begin a thread and execute a finite number of lines > > of code, or a finite amount of time within it? > > > For example, say I create three child threads and I want to guarantee > > equal timeshare between them, can I specify a quanta (say 400 LOC > > although I know that is pretty small) to execute in each one in turn? > > Not as such, no. You can play tricks with the trace-module, but these > ultimately fail when the code in question runs inside C - which puts a > stop to any python interpreter scheduling anyway, thus native threads > are used which can't be controlled on that level. > > Diez Mmmm good point. 1 line of Python != 1 line of C. That's probably not very important for what I have in mind, but I hadn't really been considering that angle. Cheers, -T From tleeuwenburg at gmail.com Sun Apr 12 07:27:38 2009 From: tleeuwenburg at gmail.com (TennesseeLeeuwenburg) Date: Sun, 12 Apr 2009 04:27:38 -0700 (PDT) Subject: Multithreading / multiprocess References: <57065c62-2024-47b5-a07e-1d60ff85ba23@y10g2000prc.googlegroups.com> Message-ID: On Apr 12, 2:59?am, a... at pythoncraft.com (Aahz) wrote: > In article <57065c62-2024-47b5-a07e-1d60ff85b... at y10g2000prc.googlegroups.com>, > > tleeuwenb... at gmail.com wrote: > > >Is there anyway to begin a thread and execute a finite number of lines > >of code, or a finite amount of time within it? > > >For example, say I create three child threads and I want to guarantee > >equal timeshare between them, can I specify a quanta (say 400 LOC > >although I know that is pretty small) to execute in each one in turn? > > You have extremely coarse-grained control with sys.setcheckinterval(). > However, there is no guarantee which thread will pick up control after > each context switch, so one thread might get more than its share. Thanks for your reply! Will add that to my reading list. Cheers, -T From tino at wildenhain.de Sun Apr 12 07:39:42 2009 From: tino at wildenhain.de (Tino Wildenhain) Date: Sun, 12 Apr 2009 13:39:42 +0200 Subject: Q:Pythonic way to create list of lists In-Reply-To: References: Message-ID: <49E1D2FE.9050007@wildenhain.de> grkuntzmd at gmail.com wrote: > I am just learning Python. > > I am trying to create a list of empty lists: [[], [], [], ...] (10 > items total). Apart from the solutions shown, why would you do that in the first place? Creating such a structure is probably not so pythonic so the way to create it does not matter. Sparse datastructures might be better represented with dicts. Regards Tino From python.list at tim.thechases.com Sun Apr 12 07:53:24 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Sun, 12 Apr 2009 06:53:24 -0500 Subject: any(), all() and empty iterable In-Reply-To: <1239511151.28008.3.camel@blackwidow.nbk> References: <200904120400.41678.mail@johnohagan.com> <1239511151.28008.3.camel@blackwidow.nbk> Message-ID: <49E1D634.8000100@tim.thechases.com> >>From the docs: > > all(iterable) > > Return True if all elements of the iterable are true. Equivalent > to: > > def all(iterable): > for element in iterable: > if not element: > return False > return True Then I'd say the comment is misleading. An empty list has no item that is true (or false), yet it returns true. The comment in the docs should read "Return False if any element of the iterable is not true" or "Return True if all elements of the iterable are true or if the iterable is empty." To get the behavior the original comment describes, would seem to require an implementation something like this: def all(iterable): iterable = iter(iterable) try: element = iterable.next() except StopIteration: raise UnderdefinedBecauseNoElementsToCompareToTrue while element: try: element = iterable.next() except StopIteration: return True return False Tweaking the documentation seems like an easier and more backwards compatible solution to me :) -tkc From http Sun Apr 12 08:09:54 2009 From: http (Paul Rubin) Date: 12 Apr 2009 05:09:54 -0700 Subject: any(), all() and empty iterable References: <200904120400.41678.mail@johnohagan.com> <1239511151.28008.3.camel@blackwidow.nbk> Message-ID: <7xy6u613al.fsf@ruckus.brouhaha.com> Tim Chase writes: > > Return True if all elements of the iterable are > > true. ... > Then I'd say the comment is misleading. An empty list has no item > that is true (or false), yet it returns true. The comment is correct. "All the items of the iterable are true" means EXACTLY the same thing as "there are no items of the iterable that are false". The empty list has no false items. Therefore all(empty_list) = True is the correct behavior. Another possible implementation: import operator,itertools def all(xs): return reduce(operator.and_, itertools.imap(bool, xs), True) From duncan.booth at invalid.invalid Sun Apr 12 08:27:25 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 12 Apr 2009 12:27:25 GMT Subject: Generators/iterators, Pythonicity, and primes References: Message-ID: John Posner wrote: > Do know what in the itertools implementation causes adding a 'if p <= > sqrt(n)' clause to *decrease* performance, while adding a > 'takewhile()' clause *increases* performance? I haven't timed it, but I would guess that the takewhile was faster only because the sqrt(n) had been factored out of the loop. Try the original loop again precalculating the sqrt(n) and see how that compares. From grante at visi.com Sun Apr 12 09:00:48 2009 From: grante at visi.com (Grant Edwards) Date: Sun, 12 Apr 2009 08:00:48 -0500 Subject: How to create a virtual serial port? References: Message-ID: On 2009-04-12, JanC wrote: > Grant Edwards wrote: > >> On 2009-04-10, Stuart Davenport wrote: >> >>> I am trying to work out if its possible, to create a virtual serial >>> port with Python? >> >> On Linux: no. > > I wonder if there is no way to emulate ptys from userspace? Didn't I just answer that question? On Linux: no. There have been a couple projects that attempted to allow drivers to be written in user-space. http://www.circlemud.org/~jelson/software/fusd/ http://www.gelato.unsw.edu.au/IA64wiki/UserLevelDrivers However, AFAICT, both of these projects are defunct and neither provided an interface to the TTY layer. Without a tty-layer interface, you would have to duplicate the entire tty layer in your user-space driver driver as well. > (Like you can use FUSE to implement filesystems in python.) -- From dunmer at dreams.sk Sun Apr 12 09:07:11 2009 From: dunmer at dreams.sk (Gabriel) Date: Sun, 12 Apr 2009 15:07:11 +0200 Subject: GUI Programming Message-ID: <49E1E77F.4030308@dreams.sk> Hello, I'm python newbie and i need to write gui for my school work in python. I need to write it really quick, because i haven't much time .) So question is, which of gui toolkits should i pick and learn? I heard PyGTK and Glade are best for quick gui programming? Is it good for beginner? Or is there something better? I have some experience with java swing, btw.. From fuzzyman at gmail.com Sun Apr 12 09:18:10 2009 From: fuzzyman at gmail.com (Fuzzyman) Date: Sun, 12 Apr 2009 06:18:10 -0700 (PDT) Subject: llvm vs. parrot References: <1239405385.6357.1.camel@linux-3eb6.site> Message-ID: <2de4b1bc-e235-429a-a645-6c28deb78f1e@g36g2000vbi.googlegroups.com> On Apr 11, 12:16?am, Paul Watson wrote: > Is Parrot out of favor these days? ?It appears that Google is going to > use llvm. > > http://code.google.com/p/unladen-swallow/ Has Parrot ever been in favour? Actually they're quite different things. Michael -- http://www.ironpythoninaction.com/ From arnodel at googlemail.com Sun Apr 12 09:27:43 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Sun, 12 Apr 2009 14:27:43 +0100 Subject: any(), all() and empty iterable References: <200904120400.41678.mail@johnohagan.com> <1239511151.28008.3.camel@blackwidow.nbk> <7xy6u613al.fsf@ruckus.brouhaha.com> Message-ID: Paul Rubin writes: > Tim Chase writes: >> > Return True if all elements of the iterable are >> > true. ... >> Then I'd say the comment is misleading. An empty list has no item >> that is true (or false), yet it returns true. > > The comment is correct. "All the items of the iterable are true" > means EXACTLY the same thing as "there are no items of the iterable > that are false". The empty list has no false items. Therefore > all(empty_list) = True is the correct behavior. > > > Another possible implementation: > > import operator,itertools > def all(xs): > return reduce(operator.and_, itertools.imap(bool, xs), True) A contest! My entry: def all(iterable): return not sum(not x for x in iterable) -- Arnaud From arnodel at googlemail.com Sun Apr 12 09:29:31 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Sun, 12 Apr 2009 14:29:31 +0100 Subject: Generators/iterators, Pythonicity, and primes References: Message-ID: Duncan Booth writes: > John Posner wrote: > >> Do know what in the itertools implementation causes adding a 'if p <= >> sqrt(n)' clause to *decrease* performance, while adding a >> 'takewhile()' clause *increases* performance? > > I haven't timed it, but I would guess that the takewhile was faster > only because the sqrt(n) had been factored out of the loop. Try the > original loop again precalculating the sqrt(n) and see how that compares. Most likely -- Arnaud From python.list at tim.thechases.com Sun Apr 12 09:49:29 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Sun, 12 Apr 2009 08:49:29 -0500 Subject: any(), all() and empty iterable In-Reply-To: References: <200904120400.41678.mail@johnohagan.com> <1239511151.28008.3.camel@blackwidow.nbk> <7xy6u613al.fsf@ruckus.brouhaha.com> Message-ID: <49E1F169.6040707@tim.thechases.com> Arnaud Delobelle wrote: > Paul Rubin writes: > >> Tim Chase writes: >>>> Return True if all elements of the iterable are >>>> true. ... >>> Then I'd say the comment is misleading. An empty list has no item >>> that is true (or false), yet it returns true. >> The comment is correct. "All the items of the iterable are true" >> means EXACTLY the same thing as "there are no items of the iterable >> that are false". The empty list has no false items. Therefore >> all(empty_list) = True is the correct behavior. >> >> >> Another possible implementation: >> >> import operator,itertools >> def all(xs): >> return reduce(operator.and_, itertools.imap(bool, xs), True) > > A contest! My entry: > > def all(iterable): > return not sum(not x for x in iterable) Problem with both entries: short-circuit evaluation. def test_me(how_many=99999999999999999): yield False for _ in xrange(how_many): yield True print all(test_me()) The stdlib version wisely bails on the first False. A particularly useful aspect when test_me() does something time-consuming: def test_me(times=100) for _ in xrange(times): yield some_long_running_process_that_usually_returns_false() where that process may do something like slurp a web-page across the planet, or calculate some expensive expression. -tkc From christophedeze at gmail.com Sun Apr 12 10:01:57 2009 From: christophedeze at gmail.com (Toff) Date: Sun, 12 Apr 2009 07:01:57 -0700 (PDT) Subject: win32 wins settings Message-ID: hello I don't understand why this doesn't woks. def setwins(self): from win32com.client import GetObject objWMIService = GetObject("winmgmts: {impersonationLevel=impersonate}!\\\\.\\root\\cimv2") colNicConfigs = objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True") for nic in colNicConfigs: print nic.Description nic.SetWINSServer('','') it looks good http://msdn.microsoft.com/en-us/library/aa393624(VS.85).aspx someone got helps ? thanks From clickkt at gmail.com Sun Apr 12 10:23:49 2009 From: clickkt at gmail.com (clickkt.com) Date: Sun, 12 Apr 2009 07:23:49 -0700 (PDT) Subject: Subscribe My Feed RSS And Commenting Then I Will To Do Too! Message-ID: Subscribe My Feed RSS And Commenting Then I Will To Do Too! My Feed: http://feeds2.feedburner.com/clickkt My blog http://clickkt.com And if you and my link at your blog, I will to do too! From zhangle2002 at gmail.com Sun Apr 12 10:44:26 2009 From: zhangle2002 at gmail.com (zhangle2002 at gmail.com) Date: Sun, 12 Apr 2009 07:44:26 -0700 (PDT) Subject: a newbie question Message-ID: hi, I am just learning Python and get a problem when trying this example: from urllib import urlopen doc=urlopen("http://www.python.org").read() print(doc) when i run this, i was tould that 'cannot import name "urlopen" What's wrong with this code? Do i need to place my code in some specific directory? Thanks a lot. ' From eugene.perederey at gmail.com Sun Apr 12 10:46:34 2009 From: eugene.perederey at gmail.com (Eugene Perederey) Date: Sun, 12 Apr 2009 18:46:34 +0400 Subject: a newbie question In-Reply-To: References: Message-ID: <35253ca50904120746o420ff297tb0a931302b989571@mail.gmail.com> use urllib2 2009/4/12 : > hi, > > I am just learning Python and get a problem when trying this example: > > from urllib import urlopen > doc=urlopen("http://www.python.org").read() > print(doc) > > when i run this, i was tould that 'cannot import name "urlopen" > > > What's wrong with this code? Do i need to place my code in some > specific directory? Thanks a lot. ' > -- > http://mail.python.org/mailman/listinfo/python-list > -- Sincerely yours, Eugene Perederey From __peter__ at web.de Sun Apr 12 11:01:14 2009 From: __peter__ at web.de (Peter Otten) Date: Sun, 12 Apr 2009 17:01:14 +0200 Subject: a newbie question References: Message-ID: zhangle2002 at gmail.com wrote: > I am just learning Python and get a problem when trying this example: > > from urllib import urlopen > doc=urlopen("http://www.python.org").read() > print(doc) > > when i run this, i was tould that 'cannot import name "urlopen" > > > What's wrong with this code? Do i need to place my code in some > specific directory? Thanks a lot. ' This function has been moved in Python 3.0. Change your import to from urllib.request import urlopen If you are learning with a book using Python 2.x consider switching to Python 2.5 or 2.6 to try out the examples. Peter From rt8396 at gmail.com Sun Apr 12 11:40:23 2009 From: rt8396 at gmail.com (r) Date: Sun, 12 Apr 2009 08:40:23 -0700 (PDT) Subject: GUI Programming References: Message-ID: On Apr 12, 8:07?am, Gabriel wrote: > Hello, > > I'm python newbie and i need to write gui for my school work in python. > I need to write it really quick, [snip] Tkinter is built-in, why not start there? from Tkinter import * root = Tk() root.mainloop() From m.faustino at gmail.com Sun Apr 12 11:44:45 2009 From: m.faustino at gmail.com (=?ISO-8859-1?Q?M=E1rcio_Faustino?=) Date: Sun, 12 Apr 2009 08:44:45 -0700 (PDT) Subject: Extracting zip files containing directories with ZipFile Message-ID: <57cd0a77-3eb5-4b5a-a5e4-c6727b0a8578@e5g2000vbe.googlegroups.com> Hi, Does the ZipFile class correctly handles directories contained within zip files? For example, calling "extractall" on an archive with only "folder/ file.txt" in it results in an IOError saying "No such file or directory" for "./folder/file.txt", because it created a file named "folder" instead of a directory. (I've tested this with version 2.6.1 on Windows XP.) However, changing that method to the following, seems to solve this particular problem: #---------- def extractall(self, path = os.path.curdir, members = None, password = None): if members is None: members = self.namelist() # Place directories first to create them before extracting any file. members.sort() for name in members: if name.endswith('/'): os.makedirs(os.path.join(path, name)) else: self.extract(name, path, password) #---------- Thanks, From google at mrabarnett.plus.com Sun Apr 12 12:01:46 2009 From: google at mrabarnett.plus.com (MRAB) Date: Sun, 12 Apr 2009 17:01:46 +0100 Subject: Extracting zip files containing directories with ZipFile In-Reply-To: <57cd0a77-3eb5-4b5a-a5e4-c6727b0a8578@e5g2000vbe.googlegroups.com> References: <57cd0a77-3eb5-4b5a-a5e4-c6727b0a8578@e5g2000vbe.googlegroups.com> Message-ID: <49E2106A.60203@mrabarnett.plus.com> M?rcio Faustino wrote: > Hi, > > Does the ZipFile class correctly handles directories contained within > zip files? > > For example, calling "extractall" on an archive with only "folder/ > file.txt" in it results in an IOError saying "No such file or > directory" for "./folder/file.txt", because it created a file named > "folder" instead of a directory. (I've tested this with version 2.6.1 > on Windows XP.) However, changing that method to the following, seems > to solve this particular problem: > > #---------- > def extractall(self, path = os.path.curdir, members = None, password = > None): > if members is None: > members = self.namelist() > > # Place directories first to create them before extracting any > file. > members.sort() > > for name in members: > if name.endswith('/'): > os.makedirs(os.path.join(path, name)) > else: > self.extract(name, path, password) > #---------- > Strictly speaking, zip files don't contain nested folders, but only a flat list of files. However, by convention a folder hierarchy is shown by the use of slashes in the names eg. "foo/bar.txt" is a folder "foo" containing a file "bar.txt". You can also represent an empty folder with a (zero-length) file ending with a slash, eg "foo/" is an empty folder "foo". Just create any intermediate folders on demand if they don't already exist. From dunmer at dreams.sk Sun Apr 12 12:04:49 2009 From: dunmer at dreams.sk (Gabriel) Date: Sun, 12 Apr 2009 18:04:49 +0200 Subject: GUI Programming In-Reply-To: References: Message-ID: <49E21121.50105@dreams.sk> r wrote: > On Apr 12, 8:07 am, Gabriel wrote: > >> Hello, >> >> I'm python newbie and i need to write gui for my school work in python. >> I need to write it really quick, >> > [snip] > > Tkinter is built-in, why not start there? > > from Tkinter import * > root = Tk() > root.mainloop() > > > > > > -- > http://mail.python.org/mailman/listinfo/python-list > > It seems ugly to me.. From research at johnohagan.com Sun Apr 12 12:20:03 2009 From: research at johnohagan.com (John O'Hagan) Date: Sun, 12 Apr 2009 16:20:03 +0000 Subject: any(), all() and empty iterable In-Reply-To: <7xy6u613al.fsf@ruckus.brouhaha.com> References: <200904120400.41678.mail@johnohagan.com> <7xy6u613al.fsf@ruckus.brouhaha.com> Message-ID: <200904121620.05016.research@johnohagan.com> On Sun, 12 Apr 2009, Paul Rubin wrote: > Tim Chase writes: > > > Return True if all elements of the iterable are > > > true. ... > > > > Then I'd say the comment is misleading. An empty list has no item > > that is true (or false), yet it returns true. > > The comment is correct. "All the items of the iterable are true" > means EXACTLY the same thing as "there are no items of the iterable > that are false". The empty list has no false items. Therefore > all(empty_list) = True is the correct behavior. > [...] By the same argument, all the items of the iterable are also False, or blue, or anything else you care to say about them, because there aren't any. From the Wikipedia article on vacuous truth (see Chris Rebert's reply in this thread): "All pink rhinoceros are carnivores." "All pink rhinoceros are herbivores." But you're right, by this logic the comment is correct, and although the choice of True seems arbitrary, I've learnt something about logic today. Regards, John From arnodel at googlemail.com Sun Apr 12 12:24:38 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Sun, 12 Apr 2009 17:24:38 +0100 Subject: any(), all() and empty iterable References: <200904120400.41678.mail@johnohagan.com> <1239511151.28008.3.camel@blackwidow.nbk> <7xy6u613al.fsf@ruckus.brouhaha.com> Message-ID: Tim Chase writes: > Arnaud Delobelle wrote: >> Paul Rubin writes: >> >>> Tim Chase writes: >>>>> Return True if all elements of the iterable are >>>>> true. ... >>>> Then I'd say the comment is misleading. An empty list has no item >>>> that is true (or false), yet it returns true. >>> The comment is correct. "All the items of the iterable are true" >>> means EXACTLY the same thing as "there are no items of the iterable >>> that are false". The empty list has no false items. Therefore >>> all(empty_list) = True is the correct behavior. >>> >>> >>> Another possible implementation: >>> >>> import operator,itertools >>> def all(xs): >>> return reduce(operator.and_, itertools.imap(bool, xs), True) >> >> A contest! My entry: >> >> def all(iterable): >> return not sum(not x for x in iterable) > > Problem with both entries: short-circuit evaluation. > > def test_me(how_many=99999999999999999): > yield False > for _ in xrange(how_many): yield True > print all(test_me()) > > The stdlib version wisely bails on the first False. A particularly > useful aspect when test_me() does something time-consuming: > > def test_me(times=100) > for _ in xrange(times): > yield some_long_running_process_that_usually_returns_false() > > where that process may do something like slurp a web-page across the > planet, or calculate some expensive expression. > > -tkc I was aware of this but I mimicked the behaviour of Paul's implementation. It's even worse if the iterable is something like itertools.repeat(False) -- Arnaud From rt8396 at gmail.com Sun Apr 12 12:38:07 2009 From: rt8396 at gmail.com (r) Date: Sun, 12 Apr 2009 09:38:07 -0700 (PDT) Subject: GUI Programming References: Message-ID: <8ad37d36-8d98-45df-9007-73f1706e542c@l1g2000yqk.googlegroups.com> On Apr 12, 11:04?am, Gabriel wrote: [snip] > > It seems ugly to me.. OK, i shall inform the BDFL of this "inadequacy". Good Luck! From stuart.davenport at gmail.com Sun Apr 12 12:38:40 2009 From: stuart.davenport at gmail.com (Stuart Davenport) Date: Sun, 12 Apr 2009 09:38:40 -0700 (PDT) Subject: How to create a virtual serial port? References: <_cCdndEvsrp8PULUnZ2dnUVZ_qydnZ2d@pdx.net> <5fd78a41-ed64-4e3e-aa80-a89c8f44cf3f@v6g2000vbb.googlegroups.com> Message-ID: <3b74bcd8-eb3d-4308-843b-7896e8128ca7@c9g2000yqm.googlegroups.com> On Apr 11, 6:56?pm, Grant Edwards wrote: > On 2009-04-11, Grant Edwards wrote: > > > You can write a port redirector in user-space in MS-Windows, > > but you can't in Linux/Unix. ?On Unix systems you have to > > write a kernel module that sits below the tty layer. > > Perhaps I should elucidate further. > > That's what the "pty" driver on Unix is: a kernel module that > sits underneath the tty layer where the "normal" serial-port > UART drivers sit. > > However, Unix pty drivers only handles a subset of the normal > serial-port API. ?[I'm not sure why pty drivers have never been > "finished" so that they fully emulate a serial port, but it's > been that way for 20+ years]. > > If RouteBuddy doesn't try to do things like get/set modem > control/status lines, then the OP might be able to use a pty. > > Each pty consists of two devices: a master end and a slave end. > RouteBuddy would be told to use the slave end, and the OP would > write a program that would transfer data between a network > connection and the master end. > > A pty devices is what is used by programs like xterm to run > programs like bash. ?Xterm transfers data between a network > connection and the master end of a pty. ?Bash is connected to > the slave end of that pty and "thinks" it's connected to a > serial port. ?Bash uses little of the serial port API, so it's > happy with the limited API provided by a pty slave-end. > > -- > Grant Grant - HERO! THIS EXACTLY WHAT I WANT TO DO... I didn't realise people were still posting on this thread and have spent the last 3 hours trying to get some example code to create a pseudo TTY (pty) in Python, I get that I have to use the pty module, but finding an example is like searching for... lets go festing ...an easter egg in a forest ;) You wouldn't have any pointers to initiating a PTY and how to write to it? This is all I want to achieve. My point in all this is actually that I ordered a USB GPS Receiver and it wont arrive for another two weeks, being my impatient self, I am writing an app for my iPhone to broadcast its GPS location over the network to my laptop. I then want to get this data into the NMEA format and push this data onto a PTY - this will in-effect replace the USB GPS Receiver and the GPS software can read it :) Cheers Stu From lkcl at lkcl.net Sun Apr 12 12:50:52 2009 From: lkcl at lkcl.net (Luke Kenneth Casson Leighton) Date: Sun, 12 Apr 2009 16:50:52 +0000 Subject: [ANN] Pyjamas 0.5p1 Web Widget Set and python-to-javascript Compiler released Message-ID: Pyjamas 0.5p1 - http://pyjs.org - is a bug-fix release. Pyjamas is a Web Widget Framework, written in python, that is compiled to javascript using its stand-alone python-to-javascript compiler. It began as a port of GWT, to python. Many of the issues faced by web developers - javascript and CSS incompatibilities and difficulties - simply go away, with Pyjamas, thanks to the declarative style used for Pyjamas application development, that's near-identical to that of python-qt4, python-gtk2 and python-wxWidgets. The use of Pyjamas for application development turns web browsers into a desktop widget application development platform, with the added advantage over traditional desktop widget sets of having full access to the complete set of HTML features of web browsers. The stand-alone python-to-javascript compiler is also beginning to develop into a python accelerator. The combination of pyjs and pyv8, or pyjs and python-spidermonkey, offers some interesting possibilities for JIT compilation of python applications into x86 or ARM assembler (using pyv8), by way of intermediate compilation to javascript. Anyone who is interested in the possibilities should contact the pyjamas developers at http://groups.google.com/group/pyjamas-dev Downloads: http://code.google.com/p/pyjamas http://sf.net/projects/pyjamas http://pypi.python.org/pypi/Pyjamas/0.5p1 Web Site: http://pyjs,org From mail at johnohagan.com Sun Apr 12 12:53:51 2009 From: mail at johnohagan.com (John O'Hagan) Date: Sun, 12 Apr 2009 16:53:51 +0000 Subject: any(), all() and empty iterable In-Reply-To: <50697b2c0904112132i62c3cd50rec3a80f1866a8d63@mail.gmail.com> References: <200904120400.41678.mail@johnohagan.com> <50697b2c0904112132i62c3cd50rec3a80f1866a8d63@mail.gmail.com> Message-ID: <200904121653.51126.mail@johnohagan.com> On Sun, 12 Apr 2009, Chris Rebert wrote: > On Sat, Apr 11, 2009 at 9:00 PM, John O'Hagan wrote: > > Hi, > > > > I was getting some surprising false positives as a result of not > > expecting this: > > > > all(element in item for item in iterable) > > > > to return True when 'iterable' is empty. [...] > > This is justified in formal logic by the concept of vacuous truth. See > http://en.wikipedia.org/wiki/Vacuous_truth > > all(P(x) for x in y) #Original Python statement > ?x?y. P(x) #equivalent in formal logic > ??[?x?y. P(x)] #apply Double Negation > ?[?x?y. ?P(x)] #negate Universal Quantifier > > English interpretation: There does not exist a counterexample `x` in > the set `y` which fails to satisfy predicate `P`. > Comment: Indeed, in the case the set `y` is empty, there is no > counterexample to be had, and thus both the final statement and the > initial equivalent one are vacuously true. Yes, this may seem > unintuitive at first. Logic is like that sometimes. > > Python equivalent to final statement: not any(not P(x) for x in y) > Comment: Likewise, in the case of a empty `y`, any() returns False, so > the code simplifies to `not False`, which obviously simplifies further > to just True. > > So, if you agree with the behavior of any(), you must logically agree > with the behavior of all(). ;-) I do like this reasoning, but when you're testing for the ubiquity of something you don't expect to find it in an empty space - I was checking lists of sets to see which possible set members were in all the sets of a list, and found they all were: if the list contained no sets! I think that is surprising. Or to put it another way, if I ask someone "Amongst your books, is one of them 'Robinson Crusoe'?", and they don't have any books, they could answer 'yes' (or 'no' equally truthfully), but I'd rather they told me that no, they don't have 'Robinson Crusoe'. Still, point taken, and quibble easily solved with a little extra test for emptiness. Thanks, John From Eric_Dexter at msn.com Sun Apr 12 13:01:14 2009 From: Eric_Dexter at msn.com (edexter) Date: Sun, 12 Apr 2009 10:01:14 -0700 (PDT) Subject: GUI Programming References: Message-ID: <4a97a430-d7aa-496d-8a44-d1d1b5fc77ee@z14g2000yqa.googlegroups.com> On Apr 12, 8:07?am, Gabriel wrote: > Hello, > > I'm python newbie and i need to write gui for my school work in python. > I need to write it really quick, because i haven't much time .) > So question is, which of gui toolkits should i pick and learn? I heard > PyGTK and Glade are best for quick gui programming? Is it good for > beginner? Or is there something better? > I have some experience with java swing, btw.. pygame may work for you (sdl doesn't work on my compaq though so test the examples befour reading the manual)... most of the other ones are going to be "ugly".. If what you want is realy simple and pygame doesn't work for you may want pycap (a gaming system with python embeded) .. What do you need it to do?? just simple buttons??? From rkmr.em at gmail.com Sun Apr 12 13:05:41 2009 From: rkmr.em at gmail.com (rkmr.em at gmail.com) Date: Sun, 12 Apr 2009 10:05:41 -0700 Subject: moving to processing from threading, global variables In-Reply-To: References: Message-ID: no one? how can i share variables in processing? please help out! On Thu, Apr 9, 2009 at 8:05 AM, rkmr.em at gmail.com wrote: > hi > i am trying to move from threading to processing package. this is the > controller i used to spawn new threads, and it used the global > variable done to check if it needs to spawn more threads or not. it worked > great for me. it checks if there is new data to be processed every > 30 seconds, and spawns of new threads to do the work, till all work is > done. > > > but now in processing, anychange made to global variable done in sendalert > method, is not reflected in the controller method. > > can please point out what is wrong? > thanks a lot! > > > code > > > done = False > def sendalert(): > global done > users = q.get('xx') > if not users: > done = True > return > > done = False > for u in users: > do stuff > > > def control(number_threads_min=3, number_threads_max = 100): > global done > while True: > number_threads = len(processing.activeChildren()) > if not done and number_threads processing.Process(target=sendalert).start() > if done and number_threads processing.Process(target=sendalert).start() > time.sleep(30) > > if __name__ == '__main__': > processing.Process(target=control).start() > -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Sun Apr 12 13:11:25 2009 From: __peter__ at web.de (Peter Otten) Date: Sun, 12 Apr 2009 19:11:25 +0200 Subject: any(), all() and empty iterable References: <200904120400.41678.mail@johnohagan.com> <50697b2c0904112132i62c3cd50rec3a80f1866a8d63@mail.gmail.com> Message-ID: John O'Hagan wrote: > Or to put it another way, if I ask someone "Amongst your books, is one of > them 'Robinson Crusoe'?", and they don't have any books, they could > answer 'yes' (or 'no' equally truthfully), but I'd rather they told me > that no, they don't have ?'Robinson Crusoe'. That's why you ask "Do you have any books called 'Robinson Crusoe'?" rather than "Are all your books called 'Robinson Crusoe'?". The difference between logic and natural language is more striking with "or". When you ask "Do you take the bus at nine or eleven?" you certainly don't expect "Yes" as the answer. Peter From luke.leighton at googlemail.com Sun Apr 12 13:15:47 2009 From: luke.leighton at googlemail.com (lkcl) Date: Sun, 12 Apr 2009 10:15:47 -0700 (PDT) Subject: Best Compatible JS Lib for Django References: Message-ID: <934e7670-460c-4ccb-8e56-933f83fd419a@x3g2000yqa.googlegroups.com> On Apr 4, 7:20 pm, Daniel Fetchinson wrote: > > Does anyone have experience with using JS Libraries with Django? yes - pyjamas - http://pyjs.org - although, strictly speaking, it's a python widget-set and python-to-javascript compiler. the possibility now of being able to program in python both client- side (to generate the front-end) and server-side is just _such_ a relief. i now almost exclusively use django and pyjamas, for all web development. > > Do some work better than others and are easier to code with? i just did a quick review of one of the alternatives to pyjamas - qooxdoo.org - which actually looks pretty good, except then you think "hang on a minute - this is near-identical functionality to pyjamas, i'm a python programmer, i'd have to get involved with javascript, what the _hell_ am i _thinking_!" http://groups.google.com/group/django-users/browse_thread/thread/9dad05e276c99a0c/d0c6dee57d89d376?lnk=gst&q=pyjamas#d0c6dee57d89d376 basically, pyjamas turns the web browser into a desktop widget set platform. apps are written in the same declarative style as python- qt4 and python-gtk2 and python-wxwidgets. all the browser incompatibilities are taken care of, with seamless per- platform overrides for Netscape, Mozilla, Safari, Opera and IE. so it couldn't _get_ any easier. you don't even need to know any javascript - just a bit of CSS and some HTML. l. From steve at REMOVE-THIS-cybersource.com.au Sun Apr 12 13:28:44 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 12 Apr 2009 17:28:44 GMT Subject: GUI Programming References: Message-ID: <01f218cf$0$20620$c3e8da3@news.astraweb.com> On Sun, 12 Apr 2009 15:07:11 +0200, Gabriel wrote: > Hello, > > I'm python newbie and i need to write gui for my school work in python. > I need to write it really quick, because i haven't much time .) So > question is, which of gui toolkits should i pick and learn? I heard > PyGTK and Glade are best for quick gui programming? Is it good for > beginner? Or is there something better? I have some experience with java > swing, btw.. I've never used it, but I've heard good things about PythonCard. http://pythoncard.sourceforge.net/ -- Steven From piet at cs.uu.nl Sun Apr 12 13:30:56 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sun, 12 Apr 2009 19:30:56 +0200 Subject: multiprocessing and Locks References: Message-ID: >>>>> gvv (G) wrote: >G> Hi All, >G> I am trying to understand multiprocessing, but I am getting a Runtime >G> error on the >G> code below. What am I missing or doing wrong? >G> Error is: >G> RuntimeError: Lock objects should only be shared between processes >G> through inheritance [code deleted] I guess you can't share locks (and probably other objects) between processes from a Pool. Maybe because there is no direct parent-child relation or so (there is a separate thread involved). There is nothing in the doc that explicitely forbids it AFAICT but it says that you have to be careful with sharing. But it could be a bug. You can do it with a manager, however, but this involves an additional process under the hood. if __name__ == "__main__": manager = multiprocessing.Manager() lock = manager.Lock() pool = multiprocessing.Pool(processes=5) for i in xrange(100): pool.apply_async(func=RunFunc, args=(i,lock)) pool.close() pool.join() -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From ryniek90 at gmail.com Sun Apr 12 13:44:12 2009 From: ryniek90 at gmail.com (Ryniek90) Date: Sun, 12 Apr 2009 19:44:12 +0200 Subject: GUI Programming In-Reply-To: References: Message-ID: <49E2286C.5080507@gmail.com> I think you should get involved with Eagle Python GUI or Kiwi GUI Framework . Good luck. From python.list at tim.thechases.com Sun Apr 12 13:45:52 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Sun, 12 Apr 2009 12:45:52 -0500 Subject: any(), all() and empty iterable In-Reply-To: References: <200904120400.41678.mail@johnohagan.com> <50697b2c0904112132i62c3cd50rec3a80f1866a8d63@mail.gmail.com> Message-ID: <49E228D0.7070606@tim.thechases.com> > That's why you ask "Do you have any books called 'Robinson Crusoe'?" rather > than "Are all your books called 'Robinson Crusoe'?". Mu. If I don't have any books..."Have you stopped beating all your wives?" The question makes the presumption that I have books (or that you've been beating your wife). -tkc From eduardo.padoan at gmail.com Sun Apr 12 13:59:10 2009 From: eduardo.padoan at gmail.com (Eduardo O. Padoan) Date: Sun, 12 Apr 2009 14:59:10 -0300 Subject: any(), all() and empty iterable In-Reply-To: <49E1D634.8000100@tim.thechases.com> References: <200904120400.41678.mail@johnohagan.com> <1239511151.28008.3.camel@blackwidow.nbk> <49E1D634.8000100@tim.thechases.com> Message-ID: On Sun, Apr 12, 2009 at 8:53 AM, Tim Chase wrote: >>> From the docs: >> >> all(iterable) >> ? ? ? ? ? ? ? ?Return True if all elements of the iterable are true. >> Equivalent >> ? ? ? ?to: >> ? ? ? ? ? ? ? ?def all(iterable): >> ? ? ? ? ? ?for element in iterable: >> ? ? ? ? ? ? ? ?if not element: >> ? ? ? ? ? ? ? ? ? ?return False >> ? ? ? ? ? ?return True > > Then I'd say the comment is misleading. ?An empty list has no item that is > true (or false), yet it returns true. ?The comment in the docs should read > "Return False if any element of the iterable is not true" or "Return True if > all elements of the iterable are true or if the iterable is empty." I didn't knew about "Vacuous True" (my fault, it seems Discrete Math 101) until reading about on this thread (thanks everyone!), and reading on wikipedia it answered this exact question. > To get the behavior the original comment describes, would seem to require an > implementation something like this: > > ?def all(iterable): > ? ?iterable = iter(iterable) > ? ?try: > ? ? ?element = iterable.next() > ? ?except StopIteration: > ? ? ?raise UnderdefinedBecauseNoElementsToCompareToTrue > ? ?while element: > ? ? ?try: > ? ? ? ?element = iterable.next() > ? ? ?except StopIteration: > ? ? ? ?return True > ? ?return False > > > Tweaking the documentation seems like an easier and more backwards > compatible solution to me :) > > -tkc > > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Eduardo de Oliveira Padoan http://importskynet.blogspot.com http://djangopeople.net/edcrypt/ "Distrust those in whom the desire to punish is strong." -- Goethe, Nietzsche, Dostoevsky From jjposner at snet.net Sun Apr 12 14:03:27 2009 From: jjposner at snet.net (John Posner) Date: Sun, 12 Apr 2009 14:03:27 -0400 Subject: Generators/iterators, Pythonicity, and primes In-Reply-To: References: Message-ID: <49E22CEF.2050200@snet.net> Duncan Booth wrote: > John Posner wrote: > >> Do know what in the itertools implementation causes adding a 'if p <= >> sqrt(n)' clause to *decrease* performance, while adding a >> 'takewhile()' clause *increases* performance? >> > > I haven't timed it, but I would guess that the takewhile was faster > only because the sqrt(n) had been factored out of the loop. Try the > original loop again precalculating the sqrt(n) and see how that compares. Here's my "timeit" scorecard, with repeat=5 (only the minimum value reported) and number=5000: **** all prev. primes 3.97347791658 **** prev. primes that don't exceed SQRT 6.23250413579 **** [CACHED] prev. primes that don't exceed SQRT 3.45557325672 **** [TAKEWHILE] prev. primes that don't exceed SQRT 0.371197373201 **** [TAKEWHILE] squares, using *, of prev. primes that don't exceed N 0.358001074011 **** [TAKEWHILE] squares, using **, of prev. primes that don't exceed N 0.383540147515 **** [TAKEWHILE, CACHED] squares, using *, of prev. primes that don't exceed N 0.410309506343 **** [TAKEWHILE, CACHED] squares, using **, of prev. primes that don't exceed N 0.401269222462 So ... adding the SQRT optimization degrades the performance significantly, but adding CACHING to the SQRT optimization swings the performance pendulum back to the "benefit" side. TAKEWHILE is a big win, but adding CACHING to TAKEWHILE degrades performance. The code (110 lines) is at http://cl1p.net/python_prime_generators/ -John From dsdale24 at gmail.com Sun Apr 12 14:30:51 2009 From: dsdale24 at gmail.com (Darren Dale) Date: Sun, 12 Apr 2009 11:30:51 -0700 (PDT) Subject: design question, metaclasses? References: <78c22f6f-229c-4115-a390-4f192b5b5849@t11g2000vbc.googlegroups.com> Message-ID: <09346220-b380-433a-91a5-09c476cee5a7@k8g2000yqn.googlegroups.com> On Apr 11, 2:15?pm, Darren Dale wrote: > I am working on a project that provides a high level interface to hdf5 > files by implementing a thin wrapper around h5py. I would like to > generalize the project so the same API can be used with other formats, > like netcdf or ascii files. The format specific code exists in File, > Group and Dataset classes, which I could reimplement for each format. > But there are other classes deriving from Group and Dataset which do > not contain any format-specific code, and I would like to find a way > to implement the functionality once and apply uniformly across > supported formats. This is really abstract, but I was thinking of > something along the lines of: > > format1.Group # implementation of group in format1 > format2.Group # ... > Base.DerivedGroup # base implementation of DerivedGroup, not directly > useful > format1.DerivedGroup = Base.DerivedGroup(format1.Group) # useful > format2.DerivedGroup = Base.DerivedGroup(format2.Group) # useful > > Could anyone please offer a comment, is this an appropriate use of > metaclassing, or is there maybe an easier/better alternative? I don't fully understand metaclasses, but I think I have convinced myself that they are not what I was looking for. I think this will do what I want it to: class Group1(object): def origin(self): return "Group1" class Group2(object): def origin(self): return "Group2" def _SubGroup(superclass): class SubGroup(superclass): pass return SubGroup SubGroup = _SubGroup(Group2) sub_group = SubGroup() print sub_group.origin() From dunmer at dreams.sk Sun Apr 12 14:45:30 2009 From: dunmer at dreams.sk (Gabriel) Date: Sun, 12 Apr 2009 20:45:30 +0200 Subject: GUI Programming In-Reply-To: <4a97a430-d7aa-496d-8a44-d1d1b5fc77ee@z14g2000yqa.googlegroups.com> References: <4a97a430-d7aa-496d-8a44-d1d1b5fc77ee@z14g2000yqa.googlegroups.com> Message-ID: <49E236CA.6070001@dreams.sk> edexter wrote: > On Apr 12, 8:07 am, Gabriel wrote: > >> Hello, >> >> I'm python newbie and i need to write gui for my school work in python. >> I need to write it really quick, because i haven't much time .) >> So question is, which of gui toolkits should i pick and learn? I heard >> PyGTK and Glade are best for quick gui programming? Is it good for >> beginner? Or is there something better? >> I have some experience with java swing, btw.. >> > > pygame may work for you (sdl doesn't work on my compaq though so test > the examples befour reading the manual)... most of the other ones are > going to be "ugly".. If what you want is realy simple and pygame > doesn't work for you may want pycap (a gaming system with python > embeded) .. What do you need it to do?? just simple buttons??? > -- > http://mail.python.org/mailman/listinfo/python-list > > I definitely need some kind of treeview, text area to represents text (hexa strings .)), buttons, maybe some kind of menu.. From google at mrabarnett.plus.com Sun Apr 12 14:46:13 2009 From: google at mrabarnett.plus.com (MRAB) Date: Sun, 12 Apr 2009 19:46:13 +0100 Subject: moving to processing from threading, global variables In-Reply-To: References: Message-ID: <49E236F5.5060502@mrabarnett.plus.com> rkmr.em at gmail.com wrote: > no one? It's Easter! :-) > how can i share variables in processing? > please help out! > Threads run in the same address space, so they can share variables; processes run in different address spaces, so they can't (normally). You'll need to read the documentation carefully to see what the recommended techniques are (and I doubt that sharing a global Boolean variable is one of them...). > On Thu, Apr 9, 2009 at 8:05 AM, rkmr.em at gmail.com > > wrote: > > hi > i am trying to move from threading to processing package. this is > the controller i used to spawn new threads, and it used the global > variable done to check if it needs to spawn more threads or not. it > worked great for me. it checks if there is new data to be processed > every > 30 seconds, and spawns of new threads to do the work, till all work > is done. > > > but now in processing, anychange made to global variable done in > sendalert method, is not reflected in the controller method. > > can please point out what is wrong? > thanks a lot! > > > code > > > done = False > def sendalert(): > global done > users = q.get('xx') > if not users: > done = True > return > > done = False > for u in users: > do stuff > > > def control(number_threads_min=3, number_threads_max = 100): > global done > while True: > number_threads = len(processing.activeChildren()) > if not done and number_threads processing.Process(target=sendalert).start() > if done and number_threads processing.Process(target=sendalert).start() > time.sleep(30) > > if __name__ == '__main__': > processing.Process(target=control).start() > > > > ------------------------------------------------------------------------ > > -- > http://mail.python.org/mailman/listinfo/python-list From emmanuel.surleau at gmail.com Sun Apr 12 14:48:52 2009 From: emmanuel.surleau at gmail.com (Emmanuel Surleau) Date: Sun, 12 Apr 2009 20:48:52 +0200 Subject: GUI Programming In-Reply-To: <49E1E77F.4030308@dreams.sk> References: <49E1E77F.4030308@dreams.sk> Message-ID: <200904122048.52889.emmanuel.surleau@gmail.com> Howdy, > I'm python newbie and i need to write gui for my school work in python. > I need to write it really quick, because i haven't much time .) > So question is, which of gui toolkits should i pick and learn? I heard > PyGTK and Glade are best for quick gui programming? Is it good for > beginner? Or is there something better? > I have some experience with java swing, btw.. You might want to have a look at PyQt4 (http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/pyqt4ref.html) if you don't want something "ugly" (native look on all platforms). Also, it has excellent documentation. You can use Qt Designer with it, additionally. Cheers, Emm From martin at v.loewis.de Sun Apr 12 15:01:44 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Sun, 12 Apr 2009 21:01:44 +0200 Subject: Extracting zip files containing directories with ZipFile In-Reply-To: <57cd0a77-3eb5-4b5a-a5e4-c6727b0a8578@e5g2000vbe.googlegroups.com> References: <57cd0a77-3eb5-4b5a-a5e4-c6727b0a8578@e5g2000vbe.googlegroups.com> Message-ID: <49e23a98$0$3997$9b622d9e@news.freenet.de> > Does the ZipFile class correctly handles directories contained within > zip files? That depends on the version of Python you are using. See http://bugs.python.org/4710 Regards, Martin From online.service.com at gmail.com Sun Apr 12 15:11:04 2009 From: online.service.com at gmail.com (online.service.com at gmail.com) Date: Sun, 12 Apr 2009 12:11:04 -0700 (PDT) Subject: How to check if a list contains an item Message-ID: <974c8e5d-2ffb-4199-bd24-8e8f0f6d3c16@y33g2000prg.googlegroups.com> python doesn't have a list.contains() method? thanks From castironpi at gmail.com Sun Apr 12 15:23:21 2009 From: castironpi at gmail.com (Aaron Brady) Date: Sun, 12 Apr 2009 12:23:21 -0700 (PDT) Subject: design question, metaclasses? References: <78c22f6f-229c-4115-a390-4f192b5b5849@t11g2000vbc.googlegroups.com> <09346220-b380-433a-91a5-09c476cee5a7@k8g2000yqn.googlegroups.com> Message-ID: <59bd6414-d9ea-49cb-b15a-ec371427e23d@r33g2000yqn.googlegroups.com> On Apr 12, 1:30?pm, Darren Dale wrote: > On Apr 11, 2:15?pm, Darren Dale wrote: > > _ > > > format1.Group # implementation of group in format1 > > format2.Group # ... > > Base.DerivedGroup # base implementation of DerivedGroup, not directly > > useful > > format1.DerivedGroup = Base.DerivedGroup(format1.Group) # useful > > format2.DerivedGroup = Base.DerivedGroup(format2.Group) # useful > _ > > class Group1(object): > > ? ? def origin(self): > ? ? ? ? return "Group1" > > class Group2(object): > > ? ? def origin(self): > ? ? ? ? return "Group2" > > def _SubGroup(superclass): > > ? ? class SubGroup(superclass): > ? ? ? ? pass > > ? ? return SubGroup > > SubGroup = _SubGroup(Group2) > sub_group = SubGroup() > > print sub_group.origin() You can create new types in one statement: SubGroup= type( "SubGroup", ( BaseGroup, ), { } ) From ebonak at hotmail.com Sun Apr 12 15:26:45 2009 From: ebonak at hotmail.com (Esmail) Date: Sun, 12 Apr 2009 15:26:45 -0400 Subject: How to check if a list contains an item In-Reply-To: <974c8e5d-2ffb-4199-bd24-8e8f0f6d3c16@y33g2000prg.googlegroups.com> References: <974c8e5d-2ffb-4199-bd24-8e8f0f6d3c16@y33g2000prg.googlegroups.com> Message-ID: <49E24075.7060102@hotmail.com> online.service.com at gmail.com wrote: > python doesn't have a list.contains() method? > How about the 'in' operator? In [1]: l1=['aa', 'bb', 'cc'] In [2]: 'aa' in l1 Out[2]: True In [3]: 'ab' in l1 Out[3]: False In [4]: 'cc' in l1 Out[4]: True Will this help? Esmail From ebonak at hotmail.com Sun Apr 12 15:26:45 2009 From: ebonak at hotmail.com (Esmail) Date: Sun, 12 Apr 2009 15:26:45 -0400 Subject: How to check if a list contains an item In-Reply-To: <974c8e5d-2ffb-4199-bd24-8e8f0f6d3c16@y33g2000prg.googlegroups.com> References: <974c8e5d-2ffb-4199-bd24-8e8f0f6d3c16@y33g2000prg.googlegroups.com> Message-ID: <49E24075.7060102@hotmail.com> online.service.com at gmail.com wrote: > python doesn't have a list.contains() method? > How about the 'in' operator? In [1]: l1=['aa', 'bb', 'cc'] In [2]: 'aa' in l1 Out[2]: True In [3]: 'ab' in l1 Out[3]: False In [4]: 'cc' in l1 Out[4]: True Will this help? Esmail From __peter__ at web.de Sun Apr 12 15:26:59 2009 From: __peter__ at web.de (Peter Otten) Date: Sun, 12 Apr 2009 21:26:59 +0200 Subject: How to check if a list contains an item References: <974c8e5d-2ffb-4199-bd24-8e8f0f6d3c16@y33g2000prg.googlegroups.com> Message-ID: online.service.com at gmail.com wrote: > python doesn't have a list.contains() method? It has. It's called list.__contains__() and used indirectly through the 'in' operator: >>> "red" in ["red", "green", "blue"] True >>> "yellow" in ["red", "green", "blue"] False From google at mrabarnett.plus.com Sun Apr 12 15:29:21 2009 From: google at mrabarnett.plus.com (MRAB) Date: Sun, 12 Apr 2009 20:29:21 +0100 Subject: How to check if a list contains an item In-Reply-To: <974c8e5d-2ffb-4199-bd24-8e8f0f6d3c16@y33g2000prg.googlegroups.com> References: <974c8e5d-2ffb-4199-bd24-8e8f0f6d3c16@y33g2000prg.googlegroups.com> Message-ID: <49E24111.7010700@mrabarnett.plus.com> online.service.com at gmail.com wrote: > python doesn't have a list.contains() method? > It does have a __contains__ method, but that's not called directly. Use "item in my_list" instead (and the opposite is "item not in my_list"). From python.list at tim.thechases.com Sun Apr 12 15:38:02 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Sun, 12 Apr 2009 14:38:02 -0500 Subject: How to check if a list contains an item In-Reply-To: <974c8e5d-2ffb-4199-bd24-8e8f0f6d3c16@y33g2000prg.googlegroups.com> References: <974c8e5d-2ffb-4199-bd24-8e8f0f6d3c16@y33g2000prg.googlegroups.com> Message-ID: <49E2431A.8070108@tim.thechases.com> online.service.com at gmail.com wrote: > python doesn't have a list.contains() method? Because it doesn't need one? >>> x = [1,2,3,4] >>> 2 in x True >>> 42 in x False pedantic side-note, objects can have a __contains__ method which is used under the covers for "in" testing, so your custom objects can also test for containment. -tkc From dsdale24 at gmail.com Sun Apr 12 15:52:44 2009 From: dsdale24 at gmail.com (Darren Dale) Date: Sun, 12 Apr 2009 12:52:44 -0700 (PDT) Subject: design question, metaclasses? References: <78c22f6f-229c-4115-a390-4f192b5b5849@t11g2000vbc.googlegroups.com> <09346220-b380-433a-91a5-09c476cee5a7@k8g2000yqn.googlegroups.com> <59bd6414-d9ea-49cb-b15a-ec371427e23d@r33g2000yqn.googlegroups.com> Message-ID: <2f03801a-c80d-42c5-afaa-fd1d9716a81f@g37g2000yqn.googlegroups.com> On Apr 12, 3:23?pm, Aaron Brady wrote: > On Apr 12, 1:30?pm, Darren Dale wrote: > > > > > On Apr 11, 2:15?pm, Darren Dale wrote: > > _ > > > > format1.Group # implementation of group in format1 > > > format2.Group # ... > > > Base.DerivedGroup # base implementation of DerivedGroup, not directly > > > useful > > > format1.DerivedGroup = Base.DerivedGroup(format1.Group) # useful > > > format2.DerivedGroup = Base.DerivedGroup(format2.Group) # useful > > _ > > > class Group1(object): > > > ? ? def origin(self): > > ? ? ? ? return "Group1" > > > class Group2(object): > > > ? ? def origin(self): > > ? ? ? ? return "Group2" > > > def _SubGroup(superclass): > > > ? ? class SubGroup(superclass): > > ? ? ? ? pass > > > ? ? return SubGroup > > > SubGroup = _SubGroup(Group2) > > sub_group = SubGroup() > > > print sub_group.origin() > > You can create new types in one statement: > > SubGroup= type( "SubGroup", ( BaseGroup, ), { } ) But how can I implement the *instance* behavior of SubGroup with this example? In my original example: format1.Group # implementation of group in format1 format2.Group # implementation of group in format2 Base.DerivedGroup # base implementation of DerivedGroup, must subclass a group format1.DerivedGroup = Base.DerivedGroup(format1.Group) # useful format2.DerivedGroup = Base.DerivedGroup(format2.Group) # useful I'm trying to achieve uniform behavior of my derived groups across supported formats. My derived groups are abstracted such that they do not need to be reimplemented for each format, I only need to implement Group for each format. This is a real mind bender for me, even my factory function gets hairy because I have additional classes that derive from DerivedGroup. Maybe what I need is the ability to provide context at import time, is that possible? From davea at ieee.org Sun Apr 12 16:13:21 2009 From: davea at ieee.org (Dave Angel) Date: Sun, 12 Apr 2009 16:13:21 -0400 Subject: moving to processing from threading, global variables In-Reply-To: References: Message-ID: <49E24B61.7020508@ieee.org> rkmr.em at gmail.com wrote: > no one? how can i share variables in processing? > please help out! > > On Thu, Apr 9, 2009 at 8:05 AM, rkmr.em at gmail.com wrote: > > >> hi >> i am trying to move from threading to processing package. this is the >> controller i used to spawn new threads, and it used the global >> variable done to check if it needs to spawn more threads or not. it worked >> great for me. it checks if there is new data to be processed every >> 30 seconds, and spawns of new threads to do the work, till all work is >> done. >> >> >> but now in processing, anychange made to global variable done in sendalert >> method, is not reflected in the controller method. >> >> can please point out what is wrong? >> thanks a lot! >> >> >> code >> >> >> done = False >> def sendalert(): >> global done >> users = q.get('xx') >> if not users: >> done = True >> return >> >> done = False >> for u in users: >> do stuff >> >> >> def control(number_threads_min=3, number_threads_max = 100): >> global done >> while True: >> number_threads = len(processing.activeChildren()) >> if not done and number_threads> processing.Process(target=sendalert).start() >> if done and number_threads> processing.Process(target=sendalert).start() >> time.sleep(30) >> >> if __name__ == '__main__': >> processing.Process(target=control).start() >> >> Some of the discussion below varies between operating systems, especially historically. For example., 16 bit Windows (say version 3.1 for example) didn't have separate address spaces or threads. And certainly some of the capabilities vary by programming language. There are several different models of two routines running "simultaneously" : 1) co-routines - closest thing that Python has is generators, where the generator runs for a while, then yields up some time to its caller. Co-routines share everything, but they can be a pain to use in the general case. 2) threads - these run in the same process (and address space), and share variables, file handles, etc. The only real distinction between the threads is scheduling. 3) processes - these run in separate address spaces. By default nothing is shared between them. But there are OS functions that allow them to share pretty intimately. 4) computers - these run on separate computers. They communicate only by I/O operations, such as pipes, sockets, etc. There are reasons in many environments to switch from threads to processes. Most common is that something you need done is already encapsulated in an executable, so you spawn that as a separte process. Another reason is that some library the two tasks use is not threadsafe, so you don't dare have both operations in the same process. Simplest example of that is GUI programming, where most of the GUI code must run in a single thread, with a single event loop. But now to your question. When you have two separate processes, you can share by various means the OS provides. Simplest is to use the command line to pass parameters to the child process, and the return code to pass a (int) value back. Next easiest is to simply write data to a file that you can both see. There are ways to lock the file so you're not both updating it at the same time. Next after that is a pipe (or socket, which is somewhat more general). And finally shared memory. Shared memory is the easiest to conceptualize. Basically you both map a hunk of shared memory and take turns reading and writing it. But as with all the others, you have to be careful about concurrent or overlapping updates. For example, if process A is changing string (not Python string class, but a similar abstraction) from "Howard" to "Gloria". You don't want process B to fetch the value "Hooria" by mistake. Generally, you want to do the simplest sharing that can solve the need. In your case, if 'done' was the only global, I'd recommend using a presence or absence of a file (s) as your communications method. Process A writes it or deletes it, and Process B checks for its existence, and maybe its timestamp. As for the filename, you can pass that on the command line when you start the second process. From gagsl-py2 at yahoo.com.ar Sun Apr 12 16:42:26 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sun, 12 Apr 2009 17:42:26 -0300 Subject: Reading only headers References: Message-ID: En Thu, 09 Apr 2009 07:13:08 -0300, S.Selvam escribi?: > I want to read headers from web page and check whether its Content-Type > is > xml or not.I used the following code > ... > request = urllib2.Request(url, None, USER_AGENT) > opener = urllib2.build_opener() > datastream = opener.open(request) > if datastream.headers.get('Content-Type','').find('xml') == -1: > raise "Its not xml!" > else: > """ > Read the content and process > """ > ... > > Is this the good way to read headers ? ,as i do not want the content > ,unless > it is xml. > Please suggest me,if there are some other good methods to read only the > headers. The best way is to issue an HTTP HEAD request -- so only the headers are sent. Search this newsgroup for some ways of doing that. Ok, see this posts: http://groups.google.com/group/comp.lang.python/t/b1060b62b2f12a04/ http://groups.google.com/group/comp.lang.python/t/bbac82df3d64d48e/ -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Sun Apr 12 16:49:55 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sun, 12 Apr 2009 17:49:55 -0300 Subject: Recommendations on Pythonic tree data structure design techniques References: <1239293907.1795.1309794163@webmail.messagingengine.com> Message-ID: En Thu, 09 Apr 2009 13:18:27 -0300, escribi?: > Any recommendations on Python based tree data structures that I > can study? I'm working on an application that will model a basic > outline structure (simple tree) and am looking for ideas on > Pythonic implementation techniques. I'd use ElementTree. An Element is a generic container for hierarchical data - doesn't have to be XML. See http://effbot.org/zone/element-index.htm and http://docs.python.org/library/xml.etree.elementtree.html -- Gabriel Genellina From kay.schluehr at gmx.net Sun Apr 12 16:50:33 2009 From: kay.schluehr at gmx.net (Kay Schluehr) Date: Sun, 12 Apr 2009 13:50:33 -0700 (PDT) Subject: design question, metaclasses? References: <78c22f6f-229c-4115-a390-4f192b5b5849@t11g2000vbc.googlegroups.com> Message-ID: On 11 Apr., 20:15, Darren Dale wrote: > I am working on a project that provides a high level interface to hdf5 > files by implementing a thin wrapper around h5py. > I would like to > generalize the project so the same API can be used with other formats, > like netcdf or ascii files. The format specific code exists in File, > Group and Dataset classes, which I could reimplement for each format. > But there are other classes deriving from Group and Dataset which do > not contain any format-specific code, and I would like to find a way > to implement the functionality once and apply uniformly across > supported formats. Seems like you are doing it wrong. The classical OO approach is to add more details / refining classes in subclasses instead of doing it the other way round and derive the less specific classes from the more specific ones. From duncan.booth at invalid.invalid Sun Apr 12 17:04:21 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 12 Apr 2009 21:04:21 GMT Subject: Generators/iterators, Pythonicity, and primes References: Message-ID: Duncan Booth wrote: > John Posner wrote: > >> Do know what in the itertools implementation causes adding a 'if p <= >> sqrt(n)' clause to *decrease* performance, while adding a >> 'takewhile()' clause *increases* performance? > > I haven't timed it, but I would guess that the takewhile was faster > only because the sqrt(n) had been factored out of the loop. Try the > original loop again precalculating the sqrt(n) and see how that compares. > Which of course is rubbish, extracting the sdqrt will have an effect but the main factor is that takewhile exits the loop as soon as the condition is false whereas a conditional in a generator comprehension doesn't stop the loop continuing to the end. From gagsl-py2 at yahoo.com.ar Sun Apr 12 17:29:50 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sun, 12 Apr 2009 18:29:50 -0300 Subject: save error with embedded python 2.5 References: Message-ID: En Fri, 10 Apr 2009 09:40:11 -0300, Eric_Dexter at msn.com escribi?: > I seem to be getting one extra value when I create my values with > > for yy in range(1, maxy): > for xx in range(1, maxx): > count = count + 1 > #squaret.append[count] > squaret.append( square(xx * 20, yy * 20)) yy goes from 1 to maxy-1, xx goes from 1 to maxx-1, there are (maxy-1)*(maxx-1) squares generated. Is that what you want? > and saving boxes with > > if squaresave.state == '1': > outfile = open('grid.dat','w') > count = 0 > for s in squaret: > count = count + 1 > outfile.write(s.state) > outfile.write(' ') > if count == maxy: > outfile.write('\n') > count = 0 Here, you're counting from 1 to maxy inclusive. Why don't you use the same two-loop design both times? Or better, use a bidimensional data structure (like a list of lists, or the numpy library if you're mostly concerned with numbers). -- Gabriel Genellina From dsdale24 at gmail.com Sun Apr 12 17:53:40 2009 From: dsdale24 at gmail.com (Darren Dale) Date: Sun, 12 Apr 2009 14:53:40 -0700 (PDT) Subject: design question, metaclasses? References: <78c22f6f-229c-4115-a390-4f192b5b5849@t11g2000vbc.googlegroups.com> Message-ID: On Apr 12, 4:50?pm, Kay Schluehr wrote: > On 11 Apr., 20:15, Darren Dale wrote: > > > I am working on a project that provides a high level interface to hdf5 > > files by implementing a thin wrapper around h5py. > > I would like to > > generalize the project so the same API can be used with other formats, > > like netcdf or ascii files. The format specific code exists in File, > > Group and Dataset classes, which I could reimplement for each format. > > But there are other classes deriving from Group and Dataset which do > > not contain any format-specific code, and I would like to find a way > > to implement the functionality once and apply uniformly across > > supported formats. > > Seems like you are doing it wrong. The classical OO approach is to add > more details / refining classes in subclasses instead of doing it the > other way round and derive the less specific classes from the more > specific ones. I think I am following the classical OO approach, refining details in subclasses. I just want a given subclass implementation describing a complex dataset to be able to work on top of multiple hierarchical file formats (like NetCDF or HDF5) by deriving from either NetCDF or HDF5 base classes that have an identical API. Those base classes encapsulate all the format-specific details, the subclasses allow a uniform image to be handled differently than a nonuniform image with a mask (for example). Maybe I should be delegating rather than subclassing. From cs at zip.com.au Sun Apr 12 17:55:28 2009 From: cs at zip.com.au (Cameron Simpson) Date: Mon, 13 Apr 2009 07:55:28 +1000 Subject: named pipe and Linux In-Reply-To: Message-ID: <20090412215528.GA1569@cskk.homeip.net> On 08Apr2009 16:13, Thomas Bellman wrote: | Cameron Simpson wrote: | > On 07Apr2009 10:08, akineko wrote: | >| I'm trying to use named pipes to fuse a Python program and a C | >| program. | >| One side creates pipes using os.mkfifo() and both sides use the same | >| named pipes (one side reads, another side writes). The read side uses | >| select.select() to wait for incoming messages and read the message | >| when select.select() says it is ready. | >| The length of the message is unknown to the read side. | | > That's a serious flaw in the message protocol. [...] | > No! | > You should use os.read() with the maximum size of a message. | > It _should_ return with the number of bytes in the message, provided the | > C program writes messages with a single OS-level write() call. | | No! | | That's still broken. You can't know if the writer has managed to | write one or several messages onto the pipe. I did think of that. It's an insurmountable problem, which is why he needs a message length in the protocol, or other way to recognise message boundaries. akineko has said elsewhere that's he's chasing that side of things. The os.read() thing simply avoids the tedium of polling; you can block on the read without the annoyance of file.read() blocking waiting for "too much" data. Cheers, -- Cameron Simpson DoD#743 http://www.cskk.ezoshosting.com/cs/ The Government that robs Peter to pay Paul, can always count on Paul for support. - George Bernard Shaw From m.faustino at gmail.com Sun Apr 12 17:59:51 2009 From: m.faustino at gmail.com (=?ISO-8859-1?Q?M=E1rcio_Faustino?=) Date: Sun, 12 Apr 2009 14:59:51 -0700 (PDT) Subject: Extracting zip files containing directories with ZipFile References: <57cd0a77-3eb5-4b5a-a5e4-c6727b0a8578@e5g2000vbe.googlegroups.com> <49e23a98$0$3997$9b622d9e@news.freenet.de> Message-ID: <3ca131ae-fffe-4eb3-b61a-2287b8914b79@j8g2000yql.googlegroups.com> On Apr 12, 5:01?pm, MRAB wrote: > Strictly speaking, zip files don't contain nested folders, but only a > flat list of files. Didn't know that, thanks. On Apr 12, 8:01?pm, "Martin v. L?wis" wrote: > That depends on the version of Python you are using. Seehttp://bugs.python.org/4710 Thanks. :) From ryniek90 at gmail.com Sun Apr 12 18:21:34 2009 From: ryniek90 at gmail.com (Ryniek90) Date: Mon, 13 Apr 2009 00:21:34 +0200 Subject: OverflowError while sending large file via socket Message-ID: <49E2696E.5020900@gmail.com> When i wanted to send an .iso file of 4GB length, i had traceback: "OverflowError: requested number of bytes is more than a Python string can hold" Sockets are being used in every network app, i.e: p2p progs (like BitTorrent), and exchanged data is often bigger than 4GB. So why i've had that Traceback? How many number of bytes Python string can hold? From python at rgbaz.eu Sun Apr 12 18:28:18 2009 From: python at rgbaz.eu (Python) Date: Mon, 13 Apr 2009 00:28:18 +0200 Subject: GUI Programming In-Reply-To: <49E1E77F.4030308@dreams.sk> References: <49E1E77F.4030308@dreams.sk> Message-ID: On 12 apr 2009, at 15:07, Gabriel wrote: > Hello, > > I'm python newbie and i need to write gui for my school work in > python. > I need to write it really quick, because i haven't much time .) > So question is, which of gui toolkits should i pick and learn? I > heard PyGTK and Glade are best for quick gui programming? Is it good > for beginner? Or is there something better? > I have some experience with java swing, btw.. > -- > http://mail.python.org/mailman/listinfo/python-list > > I chose wxpython because of it's excellent crossplatform compatibility it has a native look on all platforms too and no commercial license whatsoever... there are a huge lot of examples oonline f almost anything you want ideal for schoolwork ;) gr Arno From http Sun Apr 12 18:28:43 2009 From: http (Paul Rubin) Date: 12 Apr 2009 15:28:43 -0700 Subject: OverflowError while sending large file via socket References: Message-ID: <7x63h9ecbo.fsf@ruckus.brouhaha.com> Ryniek90 writes: > When i wanted to send an .iso file of 4GB length, i had traceback: > "OverflowError: requested number of bytes is more than a Python string > can hold" You're not supposed to put the 4GB all in one string. Open the socket and send smaller packets through it. From fabiofz at gmail.com Sun Apr 12 18:34:42 2009 From: fabiofz at gmail.com (Fabio Zadrozny) Date: Sun, 12 Apr 2009 19:34:42 -0300 Subject: Pydev 1.4.5 Released Message-ID: Hi All, Pydev and Pydev Extensions 1.4.5 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: ----------------------------------------------------------------- * Derived resources are no longer analyzed * No longer giving spurios warnings when file being analyzed is removed Release Highlights in Pydev: ---------------------------------------------- * Better error handling in the grammar * Code Formatter o Can be applied from context menu (recursively applied for folders) o Can trim whitespaces from the end of the lines o Can add new a line to the end of the file o Can automatically apply code-formatting on save o Fixed issues with unary operators and exponential o Fixed issues where parenthesis was lost if no closing parenthesis was available * Python 3.0 o Parser supporting unicode identifiers o Star expr recognized * Python 3.1 version acknowledged (and proper grammar used) * Pydev package explorer o Can show working sets as top-level elements o Folders without __init__.py are no longer shown as packages * Interactive console o When waiting for user input, the prompt is not shown o Console initial commands compatible with Python 3.0 o Timeout for starting console communication while the shell is not fully initilized o More info is available if connection fails * Alt+R working (mnemonics set for pydev contributed menus) * With Ctrl+2, matches will no longer take into acount the case * Code completion: Can get args from docstring when '*' is present * Better heuristics for automatic insertion of "self" and "import" * Fixed problem configuring external jars and zip files * Launch getting interpreter from project on default config * After a parenthesis, 'n' indentation levels may be applied (patch by Radim Kubacki) * .pyc files are now marked as derived (note that this will only happen when they're changed) * Fixed debugger issue with Jython 2.5b3 * Jython: completions working for static members access * Hover works on Eclipse 3.2 What is PyDev? --------------------------- PyDev is a plugin that enables users to use Eclipse for Python and Jython 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 Enviroment for Eclipse http://pydev.sf.net http://pydev.blogspot.com From ryniek90 at gmail.com Sun Apr 12 18:38:17 2009 From: ryniek90 at gmail.com (Ryniek90) Date: Mon, 13 Apr 2009 00:38:17 +0200 Subject: Python-list Digest, Vol 67, Issue 192 In-Reply-To: References: Message-ID: <49E26D59.6020600@gmail.com> > ------------------------------------------------------------------------ > > Temat: > Re: OverflowError while sending large file via socket > Od: > Paul Rubin > Data: > 12 Apr 2009 15:28:43 -0700 > Do: > python-list at python.org > > Do: > python-list at python.org > > > Ryniek90 writes: > >> When i wanted to send an .iso file of 4GB length, i had traceback: >> "OverflowError: requested number of bytes is more than a Python string >> can hold" >> > > You're not supposed to put the 4GB all in one string. Open the > socket and send smaller packets through it. > > > ------------------------------------------------------------------------ Ok, so i will split that data for smaller files. But i've still haven't got answer for question: "What's the max. length of string bytes which Python can hold? From clp2 at rebertia.com Sun Apr 12 18:45:07 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sun, 12 Apr 2009 15:45:07 -0700 Subject: Python-list Digest, Vol 67, Issue 192 In-Reply-To: <49E26D59.6020600@gmail.com> References: <49E26D59.6020600@gmail.com> Message-ID: <50697b2c0904121545j5dc49711i7ef95094d88e4ce2@mail.gmail.com> On Sun, Apr 12, 2009 at 3:38 PM, Ryniek90 wrote: >> Paul Rubin >> Ryniek90 writes: >> >>> >>> When i wanted to send an .iso file of 4GB length, i had traceback: >>> "OverflowError: requested number of bytes is more than a Python string >>> can hold" >>> >> >> You're not supposed to put the 4GB all in one string. ?Open the >> socket and send smaller packets through it. >> >> ?------------------------------------------------------------------------ > > Ok, so i will split that data for smaller files. But i've still haven't got > answer for question: "What's the max. length of string bytes which Python > can hold? sys.maxsize The largest positive integer supported by the platform?s Py_ssize_t type, and thus the maximum size lists, strings, dicts, and many other containers can have. Cheers, Chris -- I have a blog: http://blog.rebertia.com From benjamin at python.org Sun Apr 12 19:05:23 2009 From: benjamin at python.org (Benjamin Peterson) Date: Sun, 12 Apr 2009 23:05:23 +0000 (UTC) Subject: PyHeapTypeObject References: Message-ID: Brendan Miller catphive.net> writes: > > What's the point of PyHeapTypeObject in Include/object.h? Why does the > layout of object types need to be different on the heap vs statically > allocated? Heap types have to do some extra book-keeping like garbage collection. From a.pfrengle at gmail.com Sun Apr 12 19:11:07 2009 From: a.pfrengle at gmail.com (Andreas Pfrengle) Date: Sun, 12 Apr 2009 16:11:07 -0700 (PDT) Subject: Regex similar to "^(?u)\w$", but without digits? References: Message-ID: <348f6755-778a-48eb-abde-c0fa519aba02@y9g2000yqg.googlegroups.com> On 12 Apr., 02:31, "Mark Tolonen" wrote: > "Andreas" wrote in message > > news:f953c845-3660-4bb5-8ba7-00b93989cd20 at b1g2000vbc.googlegroups.com... > > > Hello, > > > I'd like to create a regex that captures any unicode character, but > > not the underscore and the digits 0-9. "^(?u)\w$" captures them also. > > Is there a possibility to restrict an expression like "\w" to "\w > > without [0-9_]"? > > '(?u)[^\W0-9_]' removes 0-9_ from \w. > > -Mark Hello Mark, haven't tried it yet, but it looks good! @John: Sorry for being imprecise, I meant *letters*, not *characters*, so requirement 2 fits my needs. Regards, Andreas From a.pfrengle at gmail.com Sun Apr 12 19:11:18 2009 From: a.pfrengle at gmail.com (Andreas Pfrengle) Date: Sun, 12 Apr 2009 16:11:18 -0700 (PDT) Subject: Regex similar to "^(?u)\w$", but without digits? References: Message-ID: <26d3bec3-8329-4432-a680-05c17f930a6a@3g2000yqk.googlegroups.com> On 12 Apr., 02:31, "Mark Tolonen" wrote: > "Andreas" wrote in message > > news:f953c845-3660-4bb5-8ba7-00b93989cd20 at b1g2000vbc.googlegroups.com... > > > Hello, > > > I'd like to create a regex that captures any unicode character, but > > not the underscore and the digits 0-9. "^(?u)\w$" captures them also. > > Is there a possibility to restrict an expression like "\w" to "\w > > without [0-9_]"? > > '(?u)[^\W0-9_]' removes 0-9_ from \w. > > -Mark Hello Mark, haven't tried it yet, but it looks good! @John: Sorry for being imprecise, I meant *letters*, not *characters*, so requirement 2 fits my needs. Regards, Andreas From ryniek90 at gmail.com Sun Apr 12 19:29:01 2009 From: ryniek90 at gmail.com (Ryniek90) Date: Mon, 13 Apr 2009 01:29:01 +0200 Subject: Python-list Digest, Vol 67, Issue 192 In-Reply-To: <50697b2c0904121545j5dc49711i7ef95094d88e4ce2@mail.gmail.com> References: <49E26D59.6020600@gmail.com> <50697b2c0904121545j5dc49711i7ef95094d88e4ce2@mail.gmail.com> Message-ID: <49E2793D.5040004@gmail.com> Chris Rebert pisze: > On Sun, Apr 12, 2009 at 3:38 PM, Ryniek90 wrote: > >>> Paul Rubin >>> Ryniek90 writes: >>> >>> >>>> When i wanted to send an .iso file of 4GB length, i had traceback: >>>> "OverflowError: requested number of bytes is more than a Python string >>>> can hold" >>>> >>>> >>> You're not supposed to put the 4GB all in one string. Open the >>> socket and send smaller packets through it. >>> >>> ------------------------------------------------------------------------ >>> >> Ok, so i will split that data for smaller files. But i've still haven't got >> answer for question: "What's the max. length of string bytes which Python >> can hold? >> > > sys.maxsize > The largest positive integer supported by the platform?s > Py_ssize_t type, and thus the maximum size lists, strings, dicts, and > many other containers can have. > > Cheers, > Chris > > Thanks. I've wanted to check very carefully what's up, and i found this: "strings (currently restricted to 2GiB)". It's here, in PEP #353 (PEP 0353 ). Besides of this, i've found in sys module's docstring this: maxint = 2147483647 maxunicode = 1114111 Which when added gives us 2148597758.0 bytes, which are equal to 2049.0624980926514 MiB's. So files larger than 2049.06 MiB's should be splitted into smaller ones and sent partially. Correct me if i'm wrong. From ldo at geek-central.gen.new_zealand Sun Apr 12 20:15:44 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 13 Apr 2009 12:15:44 +1200 Subject: How to create a virtual serial port? References: Message-ID: In message , Grant Edwards wrote: > On Linux: no. What about this . From aahz at pythoncraft.com Sun Apr 12 20:52:38 2009 From: aahz at pythoncraft.com (Aahz) Date: 12 Apr 2009 17:52:38 -0700 Subject: win32 wins settings References: Message-ID: In article , Toff wrote: > >I don't understand why this doesn't woks. In what way is it not working? BTW, you should look up "raw strings". See also http://www.catb.org/~esr/faqs/smart-questions.html -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ Why is this newsgroup different from all other newsgroups? From soumen08 at gmail.com Sun Apr 12 21:05:21 2009 From: soumen08 at gmail.com (Soumen banerjee) Date: Mon, 13 Apr 2009 06:35:21 +0530 Subject: choosing background color in tkfiledialog.askopenfile() Message-ID: Hello, I want to know how i can choose a background color in tkfiledialog.askopenfilename()? Due to a certain theme i am using, i cant see any text and changing the theme settings dont seem to affect the file dialog From greg.ewing at canterbury.ac.nz Sun Apr 12 21:26:20 2009 From: greg.ewing at canterbury.ac.nz (Greg Ewing) Date: Mon, 13 Apr 2009 13:26:20 +1200 Subject: ANN: PyGUI 2.0.1 Message-ID: <49E294BC.8070807@canterbury.ac.nz> PyGUI 2.0.1 is available: http://www.cosc.canterbury.ac.nz/greg.ewing/python_gui/ Fixes some problems in setup.py affecting installation on Linux and Windows. What is PyGUI? -------------- PyGUI is a cross-platform GUI toolkit designed to be lightweight and have a highly Pythonic API. -- Gregory Ewing greg.ewing at canterbury.ac.nz http://www.cosc.canterbury.ac.nz/greg.ewing/ From steve at REMOVE-THIS-cybersource.com.au Sun Apr 12 21:45:56 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 13 Apr 2009 01:45:56 GMT Subject: OverflowError while sending large file via socket References: Message-ID: <01f28d52$0$20620$c3e8da3@news.astraweb.com> On Mon, 13 Apr 2009 00:21:34 +0200, Ryniek90 wrote: > When i wanted to send an .iso file of 4GB length, i had traceback: > "OverflowError: requested number of bytes is more than a Python string > can hold" > > Sockets are being used in every network app, i.e: p2p progs (like > BitTorrent), and exchanged data is often bigger than 4GB. But they don't transfer the entire file as ONE packet. Split your data into smaller packets. I don't know what a good size for each packet would be, but if I were doing this, I'd probably start with 4096 or 8192 *bytes*. http://www.amk.ca/python/howto/sockets/ > So why i've > had that Traceback? How many number of bytes Python string can hold? The documentation doesn't seem to specify a maximum string length: http://docs.python.org/library/stdtypes.html which suggests to me that it will be implementation dependent. However, I'd guess that the current CPython implementation will have a hard limit of 2**32 bytes (4GB), and a soft limit on the amount of memory that you have. You're trying to create a single, continuous block of memory 4GB in size! Unless you've got *at least* 4GB of RAM, this is impossible even in principle, and in practice you need more than that to allow for the overhead of the operating system, Python, and any other applications you have running. -- Steven From exarkun at divmod.com Sun Apr 12 21:55:26 2009 From: exarkun at divmod.com (Jean-Paul Calderone) Date: Sun, 12 Apr 2009 21:55:26 -0400 Subject: OverflowError while sending large file via socket In-Reply-To: <01f28d52$0$20620$c3e8da3@news.astraweb.com> Message-ID: <20090413015526.24697.487132759.divmod.quotient.6894@henry.divmod.com> On 13 Apr 2009 01:45:56 GMT, Steven D'Aprano wrote: >On Mon, 13 Apr 2009 00:21:34 +0200, Ryniek90 wrote: > >> When i wanted to send an .iso file of 4GB length, i had traceback: >> "OverflowError: requested number of bytes is more than a Python string >> can hold" >> >> Sockets are being used in every network app, i.e: p2p progs (like >> BitTorrent), and exchanged data is often bigger than 4GB. > >But they don't transfer the entire file as ONE packet. Split your data >into smaller packets. I don't know what a good size for each packet would >be, but if I were doing this, I'd probably start with 4096 or 8192 >*bytes*. Everything in that paragraph is true, but it's rather misleading. The size of the packets you send is *not* determined by the length of the string you pass to socket.send. Packet size in a TCP connection is determined by various things beyond the control of an application using the BSD socket API. Jean-Paul From david at abbottdavid.com Sun Apr 12 21:56:34 2009 From: david at abbottdavid.com (David) Date: Sun, 12 Apr 2009 21:56:34 -0400 Subject: Login to Drupal 6 site using pycurl Message-ID: <49E29BD2.6030801@abbottdavid.com> I can not get this to work; #!/usr/bin/python import urllib import pycurl user_agent = 'Mozilla/4.0 (compatible: MSIE 6.0)' crl = pycurl.Curl() crl.setopt(pycurl.URL, 'http://beta.gentooligans.com') crl.setopt(pycurl.HEADER, 1) crl.setopt(pycurl.USERAGENT, user_agent) crl.setopt(pycurl.FOLLOWLOCATION, 1) crl.setopt(pycurl.COOKIEFILE, '/tmp/cookie.txt') crl.setopt(pycurl.COOKIEJAR, '/tmp/cookie.txt') login = [('name', 'username'), ('pass', 'password'), ('form-id', 'user_login'), ('op', 'Log in')] login_data = urllib.urlencode(login) crl.setopt(pycurl.POSTFIELDS, login_data) crl.setopt(pycurl.URL, 'http://beta.gentooligans.com/user/login') crl.perform() crl.close() I tried it like this also; ('edit[name]', user), ('edit[pass]', password), output [snip]