From nachik8kamat at gmail.com Thu Nov 1 01:14:52 2012 From: nachik8kamat at gmail.com (nachiket) Date: Wed, 31 Oct 2012 22:14:52 -0700 (PDT) Subject: how to perform word sense disambiguation? Message-ID: <86113b4c-6099-42e5-8fd6-379e442be7ef@googlegroups.com> an initial part of my project involves assigning sense to each word in sentence. I came across this tool called wordnet. do share your views From wxjmfauth at gmail.com Thu Nov 1 04:52:58 2012 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Thu, 1 Nov 2012 01:52:58 -0700 (PDT) Subject: sort order for strings of digits In-Reply-To: References: Message-ID: <9296aee8-23b4-4376-a77f-7a032d1e5d23@googlegroups.com> Le mercredi 31 octobre 2012 16:17:19 UTC+1, djc a ?crit?: > I learn lots of useful things from the list, some not always welcome. No > > sooner had I found a solution to a minor inconvenience in my code, than > > a recent thread here drew my attention to the fact that it will not work > > for python 3. So suggestions please: > > > > TODO 2012-10-22: sort order numbers first then alphanumeric > > >>> n > > ('1', '10', '101', '3', '40', '31', '13', '2', '2000') > > >>> s > > ('a', 'ab', 'acd', 'bcd', '1a', 'a1', '222 bb', 'b a 4') > > > > >>> sorted(n) > > ['1', '10', '101', '13', '2', '2000', '3', '31', '40'] > > >>> sorted(s) > > ['1a', '222 bb', 'a', 'a1', 'ab', 'acd', 'b a 4', 'bcd'] > > >>> sorted(n+s) > > ['1', '10', '101', '13', '1a', '2', '2000', '222 bb', '3', '31', '40', > > 'a', 'a1', 'ab', 'acd', 'b a 4', 'bcd'] > > > > > > > > Possibly there is a better way but for Python 2.7 this gives the > > required result > > > > Python 2.7.3 (default, Sep 26 2012, 21:51:14) > > > > >>> sorted(int(x) if x.isdigit() else x for x in n+s) > > [1, 2, 3, 10, 13, 31, 40, 101, 2000, '1a', '222 bb', 'a', 'a1', 'ab', > > 'acd', 'b a 4', 'bcd'] > > > > > > [str(x) for x in sorted(int(x) if x.isdigit() else x for x in n+s)] > > ['1', '2', '3', '10', '13', '31', '40', '101', '2000', '1a', '222 bb', > > 'a', 'a1', 'ab', 'acd', 'b a 4', 'bcd'] > > > > > > But not for Python 3 > > Python 3.2.3 (default, Oct 19 2012, 19:53:16) > > > > >>> sorted(n+s) > > ['1', '10', '101', '13', '1a', '2', '2000', '222 bb', '3', '31', '40', > > 'a', 'a1', 'ab', 'acd', 'b a 4', 'bcd'] > > > > >>> sorted(int(x) if x.isdigit() else x for x in n+s) > > Traceback (most recent call last): > > File "", line 1, in > > TypeError: unorderable types: str() < int() > > >>> > > > > The best I can think of is to split the input sequence into two lists, > > sort each and then join them. > > > > > > -- > > djc >>> # Py 3.2.3 >>> z = ['1', '10', '101', '13', '1a', '2', '2000', ... '222 bb', '3', '31', '40', 'a', 'a1', 'ab', ... 'acd', 'b a 4', 'bcd' ... ] >>> n, s = [], [] >>> for e in z: ... if e.isdigit(): ... n.append(int(e)) ... else: ... s.append(e) ... >>> n.sort() >>> s.sort() >>> ns = [str(e) for e in n] >>> ns.extend(s) >>> ns ['1', '2', '3', '10', '13', '31', '40', '101', '2000', '1a', '222 bb', 'a', 'a1', 'ab', 'acd', 'b a 4', 'bcd'] jmf From insideshoes at gmail.com Thu Nov 1 04:55:34 2012 From: insideshoes at gmail.com (inshu chauhan) Date: Thu, 1 Nov 2012 09:55:34 +0100 Subject: python and Open cv Message-ID: How to load a yml file in python and work with it ?? I used : import cv data = cv.Load("Z:/data/xyz_00000_ 300.yml") But when I print data.. it just gives the detail of the image like number of rows and columns etc.... I want read what is there in the pixel of the image.. I tried to use the following code .. but it gives me only the pixel values not the information contained in pixel ?? def AccessPixels(img): for y in range(0, img.height): for x in range(0, img.width): cv.Get2D(img, y, x) # Slow get pixel value. cv.Set2D(img, y, x, (0, 0, 0, 0)) # Slow set pixel value. can somebody help.. thanx in advance !!! -------------- next part -------------- An HTML attachment was scrubbed... URL: From jamie at kode5.net Thu Nov 1 05:44:27 2012 From: jamie at kode5.net (Jamie Paul Griffin) Date: Thu, 1 Nov 2012 09:44:27 +0000 Subject: Obnoxious postings from Google Groups In-Reply-To: <5091a72b$0$29967$c3e8da3$5496439d@news.astraweb.com> References: <8370412f-5176-45ff-87eb-72cbca5149d5@googlegroups.com> <5055a1a6$0$29981$c3e8da3$5496439d@news.astraweb.com> <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <5091a72b$0$29967$c3e8da3$5496439d@news.astraweb.com> Message-ID: <20121101094427.GE19327@kontrol.kode5.net> / Steven D'Aprano wrote on Wed 31.Oct'12 at 22:33:16 +0000 / > On Wed, 31 Oct 2012 12:32:57 -0700, rurpy wrote: > I don't killfile merely for posting from Gmail or Google Groups, but > regarding your second point, it has seemed to me for some years now that > Gmail is the new Hotmail, which was the new AOL. Whenever there is an > inane, lazy, mind-numbingly stupid question or post, chances are > extremely high that the sender has a Gmail address. That's a bit harsh but then, sadly there's some truth in it. I subscribe to a number of technical mailing lists, like that of my OS OpenBSD and the problem doesn't exist there whether they use Gmail or Hotmail, etc, or not. This and the tutor python list are the two I have the most problems with formatting. Some people just don't seem to give a shit about sending horrid html and other irritating formatted mail in spite of being asked not to do so. From jamie at kode5.net Thu Nov 1 05:55:20 2012 From: jamie at kode5.net (Jamie Paul Griffin) Date: Thu, 1 Nov 2012 09:55:20 +0000 Subject: Obnoxious postings from Google Groups In-Reply-To: References: <8370412f-5176-45ff-87eb-72cbca5149d5@googlegroups.com> <5055a1a6$0$29981$c3e8da3$5496439d@news.astraweb.com> <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> Message-ID: <20121101095520.GF19327@kontrol.kode5.net> / Robert Miles wrote on Wed 31.Oct'12 at 0:39:02 -0500 / > For those of you running Linux: You may want to look into whether > NoCeM is compatible with your newsreader and your version of Linux. > It checks newsgroups news.lists.filters and alt.nocem.misc for lists > of spam posts, and will automatically hide them for you. Not available > for other operating systems, though, except possibly Unix. Anybody serious about programming should be using a form of UNIX/Linux if you ask me. It's inconceivable that these systems should be avoided if you're serious about Software Engineering and Computer Science, etc. For UNIX there are loads of decent news reading software and mail user agents to learn and use. slrn is a good one and point it at gmane.org as someone else pointed out. I can't even imagine using a browser or Google Groups, etc. now. From stefan at epy.co.at Thu Nov 1 05:57:18 2012 From: stefan at epy.co.at (Stefan H. Holek) Date: Thu, 1 Nov 2012 10:57:18 +0100 Subject: python and Open cv In-Reply-To: References: Message-ID: <8B68D38C-F9D7-4BCF-BB72-21B7ACCA11D5@epy.co.at> On 01.11.2012, at 09:55, inshu chauhan wrote: > How to load a yml file in python and work with it ?? > Try one of these: http://pypi.python.org/pypi?%3Aaction=search&term=yaml&submit=search -- Stefan H. Holek stefan at epy.co.at From robert.kern at gmail.com Thu Nov 1 07:13:48 2012 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 01 Nov 2012 11:13:48 +0000 Subject: Negative array indicies and slice() In-Reply-To: <50918716.3080305@r3dsolutions.com> References: <509053F2.6020900@r3dsolutions.com> <50912ADC.2020401@r3dsolutions.com> <50918716.3080305@r3dsolutions.com> Message-ID: On 10/31/12 8:16 PM, Andrew Robinson wrote: > On 10/31/2012 02:20 PM, Ian Kelly wrote: >> On Wed, Oct 31, 2012 at 7:42 AM, Andrew Robinson wrote: >> >> Then; I'd note: The non-goofy purpose of slice is to hold three data >> values; They are either numbers or None. These *normally* encountered >> values can't create a memory loop. >> So, FOR AS LONG, as the object representing slice does not contain an >> explicit GC pair; I move that we mandate (yes, in the current python >> implementation, even as a *fix*) that its named members may not be >> assigned any objects other than None or numbers.... >> >> eg: Lists would be forbidden.... >> >> Since functions, and subclasses, can be test evaluated by int( >> the_thing_to_try ) and *[] can too, >> generality need not be lost for generating nothing or numbers. >> >> >> PEP 357 requires that anything implementing the __index__ special method be >> allowed for slicing sequences (and also that __index__ be used for the >> conversion). For the most part, that includes ints and numpy integer types, >> but other code could be doing esoteric things with it. > > I missed something... (but then that's why we're still talking about it...) > > Reading the PEP, it notes that *only* integers (or longs) are permitted in slice > syntax. > (Overlooking None, of course... which is strange...) > > The PEP gives the only exceptions as objects with method "__index__". > > Automatically, then, an empty list is forbidden (in slice syntax). > However, What you did, was circumvent the PEP by passing an empty list directly > to slice(), and avoiding running it through slice syntax processing. Why do you think it is forbidden by the syntax? [~] |1> class A(object): ..> def __getitem__(self, key): ..> return key ..> [~] |2> a = A() [~] |3> a[[]:] slice([], None, None) The PEP is a little unclear and refers to a state of the Python interpreter that no longer exists. At the time, I think __getslice__() was still not deprecated, and it did require ints (or after the PEP, __index__able objects). __getslice__() is now deprecated in favor of __getitem__() where you can interpret slice objects with arbitrary objects to your heart's content. Arbitrary objects *are* definitely allowed by the slice syntax (how could the syntax know what is an int and what is another kind of object?). Most objects that interpret slices, especially the builtin sequence types, do require __index__able objects (or None). > So, what's the psychology behind allowing slice() to hold objects which are not > converted to ints/longs in the first place? In numpy, we (ab)use this freedom for some convenient notation in special objects. We have a couple of grid-making convenience objects: [~] |5> numpy.mgrid[1.5:2.5:0.1] array([ 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1, 2.2, 2.3, 2.4]) This syntax uses the start:stop:step notation to make a float range. If we use an imaginary integer in the "step" slot, mgrid will interpret it as the number of items requested instead of the step. [~] |6> numpy.mgrid[1.5:2.5:11j] array([ 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1, 2.2, 2.3, 2.4, 2.5]) -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco From breamoreboy at yahoo.co.uk Thu Nov 1 07:15:23 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 01 Nov 2012 11:15:23 +0000 Subject: python and Open cv In-Reply-To: References: Message-ID: On 01/11/2012 08:55, inshu chauhan wrote: > How to load a yml file in python and work with it ?? > > I used : import cv > data = cv.Load("Z:/data/xyz_00000_ > 300.yml") > > But when I print data.. it just gives the detail of the image like number > of rows and columns etc.... > I want read what is there in the pixel of the image.. > > I tried to use the following code .. but it gives me only the pixel values > not the information contained in pixel ?? > > def AccessPixels(img): > for y in range(0, img.height): > for x in range(0, img.width): > cv.Get2D(img, y, x) # Slow get pixel value. > cv.Set2D(img, y, x, (0, 0, 0, 0)) # Slow set pixel value. > > > can somebody help.. thanx in advance !!! > I think the subject should be changed to "Obnoxious postings from Google Groups", given this is the same question from the same person within 24 hours but a different subject line, and from a gmail address. -- Cheers. Mark Lawrence. From robert.kern at gmail.com Thu Nov 1 07:17:07 2012 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 01 Nov 2012 11:17:07 +0000 Subject: how to perform word sense disambiguation? In-Reply-To: <86113b4c-6099-42e5-8fd6-379e442be7ef@googlegroups.com> References: <86113b4c-6099-42e5-8fd6-379e442be7ef@googlegroups.com> Message-ID: On 11/1/12 5:14 AM, nachiket wrote: > an initial part of my project involves assigning sense to each word in sentence. I came across this tool called wordnet. do share your views You can get access to Wordnet and a wide variety of useful tools in NLTK: http://nltk.org/ http://nltk.org/book/ch06.html http://nltk.org/api/nltk.classify.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 andrew3 at r3dsolutions.com Thu Nov 1 07:32:54 2012 From: andrew3 at r3dsolutions.com (Andrew Robinson) Date: Thu, 01 Nov 2012 04:32:54 -0700 Subject: Negative array indicies and slice() In-Reply-To: <5092833F.4070609@stoneleaf.us> References: <509053F2.6020900@r3dsolutions.com> <50912ADC.2020401@r3dsolutions.com> <50918716.3080305@r3dsolutions.com> <5092833F.4070609@stoneleaf.us> Message-ID: <50925DE6.7020100@r3dsolutions.com> On 11/01/2012 07:12 AM, Ethan Furman wrote: > Andrew Robinson wrote: >> On 10/31/2012 02:20 PM, Ian Kelly wrote: >>> On Wed, Oct 31, 2012 at 7:42 AM, Andrew Robinson wrote: >>>> Then; I'd note: The non-goofy purpose of slice is to hold three >>>> data values; They are either numbers or None. These *normally* >>>> encountered values can't create a memory loop. >>>> So, FOR AS LONG, as the object representing slice does not contain >>>> an explicit GC pair; A little review... The premise of my statement here, is that Tim Peter's closed the Bug report; http://bugs.python.org/issue1501180 With the *reason* being that using GC was *goofy* on account of what slice() was intended to hold, None and a number. So, My first attempt at bug fix was simply to take Tim Peter's at his word... since we all assume he *isn't* a "Bloody Idiot". Hey isn't that a swear-word somewhere in the world? Its not where I live, but I seem to recall... oh, well... whatever. >> I missed something... (but then that's why we're still talking about >> it...) >> >> Reading the PEP, it notes that *only* integers (or longs) are >> permitted in slice syntax. > > Keep in mind that PEPs represent Python /at that time/ -- as Python > moves forward, PEPs are not updated (this has gotten me a couple times). And, since I am reading them in the order written (but in 3.0) trying to get the whole of Python into my mind on the journey to prep for porting it into a tiny chip -- I'm frustrated by not being finished yet... > Furman, actually. :) :-! > > And my values do *not* convert to indices (at least, not automatically). Ahhh.... (Rhetorical & sarcastic) I was wondering how you added index() method to strings, not access it, and still be following the special PEP we are talking about,when you gave that example using unwrapped strings. -------------------------- Hmmmm.... was that PEP the active state of Python, when Tim rejected the bug report? eg: have we "moved on" into a place where the bug report ought to be re-issued since that PEP is now *effectively* passe, and Tim could thus be vindicated from being a "b... Idiot?" (Or has he been given the 1st place, Python Twit award -- and his *man* the bug list been stripped?) > In other words, the slice contains the strings, and my code calculates > the offsets -- Python doesn't do it for me. > > ~Ethan~ I see, so the problem is that PEP wants you to implement the index(), but that is going to cause you to subclass string, and add a wrapper interface every time you need to index something. eg: doing something llke --- mydbclass[ MyString( 'fromColumn' ) : MyString( 'toColum' ) ] and the code becomes a candy machine interface issue (Chapter 5, Writing Solid Code). My favorite line there uses no swearing .... "If they had just taken an extra *30* seconds thinking about their design, they could have saved me, and I'm sure countless others, from getting something they didn't want." I laugh, if they didn't get it already -- an extra *30* seconds is WAAAAY to optimistic. Try minutes at least, will a policeman glaring over their shoulder. But anyhow --- The problem lies in *when* the conversion to an integer is to take place, not so much if it is going to happen. Your indexes, no matter how disguised, eventually will become numbers; and you have a way that minimizes coding cruft (The very reason I started the thread, actually... subclassing trivially to fix candy machine interfaces leads to perpetual code increases -- In cPython source-code, "realloc" wrappers and "malloc" wrappers are found .... I've seen these wrappers *re*-invented in nearly every C program I've every looked at! Talk about MAN-hours, wasted space, and cruft.) So; is this a reasonable summary of salient features (status quo) ? * Enforcing strict numerical indexes (in the slice [::] operator) causes much psychological angst when attempting to write clear code without lots of wrapper cruft. * Pep 357 merely added cruft with index(), but really solved nothing. Everything index() does could be implemented in __getitem__ and usually is. * slice().xxxs are merely a container for *whatever* was passed to [::] * slice() is * slice is also a full blown object, which implements a trivial method to dump the contents of itself to a tuple. * presently slice() allows memory leaks through GC loops. * Slice(), even though an object with a constructor, does no error checking to deny construction of memory leaks. If people would take 30 seconds to think about this.... the more details added -- the more comprehensive can be my understanding -- and perhaps a consensus reached about the problem. These are a list of relevant options, without respect to feasability. * Don't bother to fix the bug; allow Python to crash with a subtle bug that often take weeks to track down by the very small minority doing strange things (Equivalent to the "monkey patch" syndrome of D'Aprano; BTW: The longer the bug is left unfixed, the more people will invent "uses" for it ) * Convert the specially coded Slice() object into a normal Python object (essentially adds the GC); This can be a "named" tuple, or an immutable object with slots..., or just adding GC to existing object. * Work through the difficult problem of guessing all the different ways people will want to use slice() to represent indexes; and then raise exceptions IN THE SLICE CONSTRUCTOR when a usage is tried outside these ways. (caveat, if a reasonable idea for loops is found -- we *must* implement GC.) * Improve the GC so that memory loops aren't a problem in the first place. And here's a list of qualms: * Adding bytes of memory for GC isn't worth it... * Removing bytes of GC from other objects when loops would be rare isn't worth it.... * Doing things which violate the present *theoretical* API will cause *subtle* bugs... * Fixing something in a way breaking a general paradigm (no GC) enlarges code, and makes generalizations about the language full of complex exceptions hard to code for. Did I miss anything important??? So finally, here's a list of missing information, and I'd *REALLY* appreciate someone helping me figure out how to profile memory usage in Python with an example, or link, for solving any of the following. * As a fraction of the used memory (at fixed points in time) of a typical Python program: o How much is used by GC? o How much is used by slice objects? o How much will be used by additional GC if added to slice objects (can be calculated). o How much is used by generic tuples? * As a fraction of tuples used for generic purposes in Python o How many contain only data elements that are basic Python types without ability to create loops; Strings, floats, self contained objects. (I'm thinking a flag might be useful to decide when GC may be omitted in these cases). -------------- next part -------------- An HTML attachment was scrubbed... URL: From andriy.kornatskyy at live.com Thu Nov 1 07:36:26 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Thu, 1 Nov 2012 14:36:26 +0300 Subject: Fastest web framework In-Reply-To: References: Message-ID: Per community request turbogears and pysi were added.?The following posts have been updated: http://mindref.blogspot.com/2012/09/python-fastest-web-framework.html http://mindref.blogspot.com/2012/10/python-web-pep8-consistency.html Comments or suggestions are welcome. Thanks. Andriy ---------------------------------------- > From: andriy.kornatskyy at live.com > To: python-list at python.org > Subject: Fastest web framework > Date: Sun, 23 Sep 2012 12:19:16 +0300 > > > I have run recently a benchmark of a trivial 'hello world' application for various python web frameworks (bottle, django, flask, pyramid, web.py, wheezy.web) hosted in uWSGI/cpython2.7 and gunicorn/pypy1.9... you might find it interesting: > > http://mindref.blogspot.com/2012/09/python-fastest-web-framework.html > > Comments or suggestions are welcome. > > Thanks. > > Andriy Kornatskyy > > -- > http://mail.python.org/mailman/listinfo/python-list From invalid at invalid.invalid Thu Nov 1 08:09:42 2012 From: invalid at invalid.invalid (Grant Edwards) Date: Thu, 1 Nov 2012 12:09:42 +0000 (UTC) Subject: datetime issue References: <52847c35-388c-4758-b72d-1d1859b788a3@googlegroups.com> <8370412f-5176-45ff-87eb-72cbca5149d5@googlegroups.com> <5055a1a6$0$29981$c3e8da3$5496439d@news.astraweb.com> <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> Message-ID: On 2012-10-31, rurpy at yahoo.com wrote: > On 10/31/2012 09:11 AM, Grant Edwards wrote:> On 2012-09-16, ???????? ?????? wrote: >> >>> Iam positng via google groups using chrome, thats all i know. >> >> Learn something else. Google Groups is seriously and permanently >> broken, and all posts from Google Groups are filtered out and ignored >> by many people (including myself -- I only saw this because somebody >> else replied to it). > > Feel free to filter whatever you want but be aware than in > doing so you risk missing information that could help you > avoid disseminating erroneous info. Of course, carrying out > some kind of private war against Google Groups may be more > important to you than that... Based on past experience, the chances of useful information being posted via Google Groups is very small. The benefit of avoiding all the spam and garbage the comes from that source is more than worth the small risk of missing something worthwhile. Note that I'm _not_ talking about people posting to the mailing list using Gmail -- only people posting via the Google Groups web UI. -- Grant Edwards grant.b.edwards Yow! Four thousand at different MAGNATES, MOGULS gmail.com & NABOBS are romping in my gothic solarium!! From insideshoes at gmail.com Thu Nov 1 08:48:18 2012 From: insideshoes at gmail.com (inshu chauhan) Date: Thu, 1 Nov 2012 13:48:18 +0100 Subject: Fwd: python and Open cv In-Reply-To: References: Message-ID: ---------- Forwarded message ---------- From: inshu chauhan Date: Thu, Nov 1, 2012 at 1:26 PM Subject: Re: python and Open cv To: Mark Lawrence I am sorry.. but I need to know what are the rules and what about gmail ?? Many people are using gmail to mail to the list. On Thu, Nov 1, 2012 at 12:15 PM, Mark Lawrence wrote: > On 01/11/2012 08:55, inshu chauhan wrote: > >> How to load a yml file in python and work with it ?? >> >> I used : import cv >> data = cv.Load("Z:/data/xyz_00000_ >> 300.yml") >> >> But when I print data.. it just gives the detail of the image like number >> of rows and columns etc.... >> I want read what is there in the pixel of the image.. >> >> I tried to use the following code .. but it gives me only the pixel values >> not the information contained in pixel ?? >> >> def AccessPixels(img): >> for y in range(0, img.height): >> for x in range(0, img.width): >> cv.Get2D(img, y, x) # Slow get pixel value. >> cv.Set2D(img, y, x, (0, 0, 0, 0)) # Slow set pixel value. >> >> >> can somebody help.. thanx in advance !!! >> >> > I think the subject should be changed to "Obnoxious postings from Google > Groups", given this is the same question from the same person within 24 > hours but a different subject line, and from a gmail address. > > > -- > Cheers. > > Mark Lawrence. > > -- > http://mail.python.org/**mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ethan at stoneleaf.us Thu Nov 1 10:12:15 2012 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 01 Nov 2012 07:12:15 -0700 Subject: Negative array indicies and slice() In-Reply-To: <50918716.3080305@r3dsolutions.com> References: <509053F2.6020900@r3dsolutions.com> <50912ADC.2020401@r3dsolutions.com> <50918716.3080305@r3dsolutions.com> Message-ID: <5092833F.4070609@stoneleaf.us> Andrew Robinson wrote: > On 10/31/2012 02:20 PM, Ian Kelly wrote: >> On Wed, Oct 31, 2012 at 7:42 AM, Andrew Robinson wrote: >>> Then; I'd note: The non-goofy purpose of slice is to hold three >>> data values; They are either numbers or None. These *normally* >>> encountered values can't create a memory loop. >>> So, FOR AS LONG, as the object representing slice does not contain >>> an explicit GC pair; I move that we mandate (yes, in the current >>> python implementation, even as a *fix*) that its named members may >>> not be assigned any objects other than None or numbers.... >>> >>> eg: Lists would be forbidden.... >>> >>> Since functions, and subclasses, can be test evaluated by int( >>> the_thing_to_try ) and *[] can too, >>> generality need not be lost for generating nothing or numbers. >> >> >> PEP 357 requires that anything implementing the __index__ special >> method be allowed for slicing sequences (and also that __index__ be >> used for the conversion). For the most part, that includes ints and >> numpy integer types, but other code could be doing esoteric things >> with it. > > I missed something... (but then that's why we're still talking about it...) > > Reading the PEP, it notes that *only* integers (or longs) are permitted > in slice syntax. Keep in mind that PEPs represent Python /at that time/ -- as Python moves forward, PEPs are not updated (this has gotten me a couple times). >> The change would be backward-incompatible in any case, since there is >> certainly code out there that uses non-numeric slices -- one example >> has already been given in this thread. > > Hmmm..... > > Now, I'm thinking -- The purpose of index(), specifically, is to notify > when something which is not an integer may be used as an index; You've > helpfully noted that index() also *converts* those objects into numbers. > > Ethan Fullman mentioned that he used the names of fields, "instead of > having to remember the _offsets_"; Which means that his values _do > convert_ to offset numbers Furman, actually. :) And my values do *not* convert to indices (at least, not automatically). My __getitem__ code looks like: elif isinstance(item, slice): sequence = [] if isinstance(item.start, (str, unicode)) \ or isinstance(item.stop, (str, unicode)): field_names = dbf.field_names(self) start, stop, step = item.start, item.stop, item.step if start not in field_names or stop not in field_names: raise MissingFieldError( "Either %r or %r (or both) are not valid field names" % (start, stop)) if step is not None and not isinstance(step, (int, long)): raise DbfError( "step value must be an int or long, not %r" % type(step)) start = field_names.index(start) stop = field_names.index(stop) + 1 item = slice(start, stop, step) for index in self._meta.fields[item]: sequence.append(self[index]) return sequence In other words, the slice contains the strings, and my code calculates the offsets -- Python doesn't do it for me. > His example was actually given in slice syntax notation [::]. > Hence, his objects must have an index() method, correct?. Nope. ~Ethan~ From rosuav at gmail.com Thu Nov 1 10:40:22 2012 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 2 Nov 2012 01:40:22 +1100 Subject: Negative array indicies and slice() In-Reply-To: <5092833F.4070609@stoneleaf.us> References: <509053F2.6020900@r3dsolutions.com> <50912ADC.2020401@r3dsolutions.com> <50918716.3080305@r3dsolutions.com> <5092833F.4070609@stoneleaf.us> Message-ID: On Fri, Nov 2, 2012 at 1:12 AM, Ethan Furman wrote: > In other words, the slice contains the strings, and my code calculates > the offsets -- Python doesn't do it for me. That's correct, but you're still translating those strings into numeric indices. You can slice a database record based on column names (though personally I would recommend against it - creates too much dependence on column order, which I prefer to treat as non-significant), but you can't, for instance, slice a dictionary by keys: foo = {"asdf":123,"qwer":234,"zxcv":345,"1234":456} foo["qwer":"1234"] # What should this return? I suppose conceptually you could slice any iterable by discarding till you match the start, then yielding till you match the stop, then returning (it'd function like itertools.islice but using non-numeric indices - somehow). But it still depends on there being a dependable order. (Incidentally, isinstance(X, (str, unicode)) can become isinstance(X, basestring) - they both inherit from that.) ChrisA From shambhu.rajak at calsoftinc.com Thu Nov 1 10:50:50 2012 From: shambhu.rajak at calsoftinc.com (Shambhu Rajak) Date: Thu, 1 Nov 2012 20:20:50 +0530 Subject: How to remotely Automate GUI using pywinauto Message-ID: <001f01cdb840$4980d4d0$dc827e70$@calsoftinc.com> Hi, Here I have a situation: I am trying to automate a GUI application and get some data from it. I tried using 'pywinauto' to launch the application and select the data and save it in -say notepad and later read it. This works fine when the script is locally run on the windows machine. My scenario is to do it remotely. What I have done is, keep the script in a location on windows machine and execute from a linux machine over SSH. The scripts gets Initiated but raises exception as below: File "C:\Python27\lib\site-packages\pywinauto\application.py", line 229, in __getattr__ ctrls = _resolve_control(self.criteria) File "C:\Python27\lib\site-packages\pywinauto\application.py", line 788, in _resolve_control raise e.original_exception Can anyone please tell me how to remotely Automate Windwos GUI using pywinauto , or there another better method. Thanks, Shambhu -------------- next part -------------- An HTML attachment was scrubbed... URL: From insideshoes at gmail.com Thu Nov 1 11:32:25 2012 From: insideshoes at gmail.com (inshu chauhan) Date: Thu, 1 Nov 2012 16:32:25 +0100 Subject: pythonic way Message-ID: what is the most pythonic way to do this : if 0 < ix < 10 and 0 < iy < 10 ??? -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at mrabarnett.plus.com Thu Nov 1 11:52:11 2012 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 01 Nov 2012 15:52:11 +0000 Subject: pythonic way In-Reply-To: References: Message-ID: <50929AAB.4060507@mrabarnett.plus.com> On 2012-11-01 15:32, inshu chauhan wrote: > what is the most pythonic way to do this : > > if 0 < ix < 10 and 0 < iy < 10 ??? > > That looks Pythonic to me, except for the missing colon. From python.list at tim.thechases.com Thu Nov 1 11:57:12 2012 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 01 Nov 2012 10:57:12 -0500 Subject: pythonic way In-Reply-To: References: Message-ID: <50929BD8.7090706@tim.thechases.com> On 11/01/12 10:32, inshu chauhan wrote: > what is the most pythonic way to do this : > > if 0 < ix < 10 and 0 < iy < 10 ??? What's wrong with the syntax you provide? It's perfectly pythonic: ix = 42 yx = 3.14159 if 0 < ix < 10 and 0 < iy < 10: do_stuff(ix, iy) else: do_other_stuff(ix, iy) -tkc From tjreedy at udel.edu Thu Nov 1 12:04:12 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 01 Nov 2012 12:04:12 -0400 Subject: pythonic way In-Reply-To: References: Message-ID: On 11/1/2012 11:32 AM, inshu chauhan wrote: > what is the most pythonic way to do this : > > if 0 < ix < 10 and 0 < iy < 10 ??? end with : instead of ??? >>> ix, iy = 3,4 >>> if 0 < ix < 10 and 0 < iy < 10: print('This was too easy') This was too easy -- Terry Jan Reedy From ethan at stoneleaf.us Thu Nov 1 12:15:14 2012 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 01 Nov 2012 09:15:14 -0700 Subject: Negative array indicies and slice() In-Reply-To: References: <509053F2.6020900@r3dsolutions.com> <50912ADC.2020401@r3dsolutions.com> <50918716.3080305@r3dsolutions.com> <5092833F.4070609@stoneleaf.us> Message-ID: <5092A012.6030206@stoneleaf.us> Chris Angelico wrote: > On Fri, Nov 2, 2012 at 1:12 AM, Ethan Furman wrote: >> In other words, the slice contains the strings, and my code calculates >> the offsets -- Python doesn't do it for me. > > That's correct, but you're still translating those strings into > numeric indices. True, but the point is that the /slice/ contains a data type that is neither a number, nor directly translatable into a number (that is, no __index__ method), and my code would cease to function should that change to slices be made. ~Ethan~ From ian.g.kelly at gmail.com Thu Nov 1 12:40:38 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 1 Nov 2012 10:40:38 -0600 Subject: pythonic way In-Reply-To: References: Message-ID: On Thu, Nov 1, 2012 at 9:32 AM, inshu chauhan wrote: > what is the most pythonic way to do this : > > if 0 < ix < 10 and 0 < iy < 10 ??? > I suppose you could do if all(0 < i < 10 for i in (ix, iy)): but I think that the original is more readable unless you have several variables to test. -------------- next part -------------- An HTML attachment was scrubbed... URL: From schesis at gmail.com Thu Nov 1 12:54:43 2012 From: schesis at gmail.com (Zero Piraeus) Date: Thu, 1 Nov 2012 12:54:43 -0400 Subject: pythonic way In-Reply-To: References: Message-ID: : On 1 November 2012 11:32, inshu chauhan wrote: > what is the most pythonic way to do this : > > if 0 < ix < 10 and 0 < iy < 10 ??? As everyone else has said, it's perfectly pythonic once you stick the colon on the end. You might find it more instantly readable with some extra parentheses: if (0 < ix < 10) and (0 < iy < 10): # do something ... but that's really just down to taste. -[]z. From schesis at gmail.com Thu Nov 1 13:04:42 2012 From: schesis at gmail.com (Zero Piraeus) Date: Thu, 1 Nov 2012 13:04:42 -0400 Subject: python and Open cv In-Reply-To: References: Message-ID: : On 1 November 2012 08:48, inshu chauhan wrote: > I am sorry.. but I need to know what are the rules and what about gmail ?? > Many people are using gmail to mail to the list. There aren't any rules about gmail (except the unwritten rule that to be a "real" geek you need to use a mail client that takes a whole weekend to configure, and another three years to properly understand). It's helpful to other people on the list, though, if you post follow-up questions in the same thread - finding the last relevant message and hitting "reply to all" will do this in gmail. -[]z. From paul.nospam at rudin.co.uk Thu Nov 1 14:02:01 2012 From: paul.nospam at rudin.co.uk (Paul Rudin) Date: Thu, 01 Nov 2012 18:02:01 +0000 Subject: python and Open cv References: Message-ID: <87bofhtd5y.fsf@no-fixed-abode.cable.virginmedia.net> Zero Piraeus writes: > There aren't any rules about gmail (except the unwritten rule that to > be a "real" geek you need to use a mail client that takes a whole > weekend to configure, and another three years to properly understand). Ha! 3 years? I've been using gnus for nearly 20 years and I still don't understand it! From rosuav at gmail.com Thu Nov 1 14:53:54 2012 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 2 Nov 2012 05:53:54 +1100 Subject: Negative array indicies and slice() In-Reply-To: <50925DE6.7020100@r3dsolutions.com> References: <509053F2.6020900@r3dsolutions.com> <50912ADC.2020401@r3dsolutions.com> <50918716.3080305@r3dsolutions.com> <5092833F.4070609@stoneleaf.us> <50925DE6.7020100@r3dsolutions.com> Message-ID: On Thu, Nov 1, 2012 at 10:32 PM, Andrew Robinson wrote: > presently slice() allows memory leaks through GC loops. Forgive me if I've missed something here, but isn't it only possible to make a refloop by decidedly abnormal behaviour? Stuff like: a=[]; a.append(slice(a)) Seriously, who does this? First you have to have a reference to a container in place of an index, and then you have to retain the slice object inside that same container as well. Neither operation is normal use of a slice. Where is the problem? ChrisA From ian.g.kelly at gmail.com Thu Nov 1 15:07:32 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 1 Nov 2012 13:07:32 -0600 Subject: Negative array indicies and slice() In-Reply-To: <50925DE6.7020100@r3dsolutions.com> References: <509053F2.6020900@r3dsolutions.com> <50912ADC.2020401@r3dsolutions.com> <50918716.3080305@r3dsolutions.com> <5092833F.4070609@stoneleaf.us> <50925DE6.7020100@r3dsolutions.com> Message-ID: On Thu, Nov 1, 2012 at 5:32 AM, Andrew Robinson wrote: > Hmmmm.... was that PEP the active state of Python, when Tim rejected the bug report? Yes. The PEP was accepted and committed in March 2006 for release in Python 2.5. The bug report is from June 2006 has a version classification of Python 2.5, although 2.5 was not actually released until September 2006. > Pep 357 merely added cruft with index(), but really solved nothing. Everything index() does could be implemented in __getitem__ and usually is. No. There is a significant difference between implementing this on the container versus implementing it on the indexes. Ethan implemented his string-based slicing on the container, because the behavior he wanted was specific to the container type, not the index type. Custom index types like numpy integers on the other hand implement __index__ on the index type, because they apply to all sequences, not specific containers. This must be separate from standard int conversion, because standard int conversion is too general for indexing. > slice is also a full blown object, which implements a trivial method to dump the contents of itself to a tuple. slice.indices() does not trivially dump its contents as given. It takes a sequence length and adjusts its indices to that length. The C implementation of this is around 60 lines of code. > Don't bother to fix the bug; allow Python to crash with a subtle bug that often take weeks to track down by the very small minority doing strange things (Equivalent to the "monkey patch" syndrome of D'Aprano; BTW: The longer the bug is left unfixed, the more people will invent "uses" for it ) It's been 6 years already. AFAIK nobody has invented any uses that are actually at risk of invoking the GC bug. From duret.tanguy at gmail.com Thu Nov 1 16:49:49 2012 From: duret.tanguy at gmail.com (Tengy Td) Date: Thu, 1 Nov 2012 21:49:49 +0100 Subject: Coordination between developers in the Python project Message-ID: Hello, I am a French student and I am currently realizing my final thesis in the field of Free/libre open source software. It would be a great help for me if you could answer a short online survey (it should take approximately 5 minutes). This survey is designed to reach a better understanding of the cooperation and coordination between developers in Free/libre open source projects. There is no right or wrong answers, therefore, feel free to answer spontaneously and to skip the questions you feel you do not want to answer. The link to the survey is: http://bit.ly/SzVrJe I would like to remind you that the participation is absolutely anonymous and voluntary, and you can quit it at any time. Your answers will be strictly confidential and will be used only for research purpose (no commercial use of any information you provided). If you find this survey interesting, you are welcome to share it with other developers! If you want to add something, or if you need further information about this survey, feel free to contact me at: d uret.tanguy at gmail.com Thank you in advance for your cooperation and your enthusiasm! :) Tanguy Duret -------------- next part -------------- An HTML attachment was scrubbed... URL: From ethan at stoneleaf.us Thu Nov 1 16:55:46 2012 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 01 Nov 2012 13:55:46 -0700 Subject: Negative array indicies and slice() In-Reply-To: References: <509053F2.6020900@r3dsolutions.com> <50912ADC.2020401@r3dsolutions.com> <50918716.3080305@r3dsolutions.com> <5092833F.4070609@stoneleaf.us> <50925DE6.7020100@r3dsolutions.com> Message-ID: <5092E1D2.4080509@stoneleaf.us> Ian Kelly wrote: > On Thu, Nov 1, 2012 at 5:32 AM, Andrew Robinson wrote: >> Don't bother to fix the bug; allow Python to crash with a subtle bug that often take weeks to track down by the very small minority doing strange things (Equivalent to the "monkey patch" syndrome of D'Aprano; BTW: The longer the bug is left unfixed, the more people will invent "uses" for it ) > > It's been 6 years already. AFAIK nobody has invented any uses that > are actually at risk of invoking the GC bug. The bug is not that slice allows non-numbers, but that slice objects aren't tracked by gc; I'm not seeing an issue with not fixing the bug. ~Ethan~ From andrea.crotti.0 at gmail.com Thu Nov 1 17:38:53 2012 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Thu, 01 Nov 2012 21:38:53 +0000 Subject: lazy properties? Message-ID: <5092EBED.2090002@gmail.com> Seeing the wonderful "lazy val" in Scala I thought that I should try to get the following also in Python. The problem is that I often have this pattern in my code: class Sample: def __init__(self): self._var = None @property def var(self): if self._var is None: self._var = long_computation() else: return self._var which is quite useful when you have some expensive attribute to compute that is not going to change. I was trying to generalize it in a @lazy_property but my attempts so far failed, any help on how I could do that? What I would like to write is @lazy_property def var_lazy(self): return long_computation() and this should imply that the long_computation is called only once.. From insideshoes at gmail.com Thu Nov 1 17:50:17 2012 From: insideshoes at gmail.com (inshu chauhan) Date: Thu, 1 Nov 2012 22:50:17 +0100 Subject: python and Open cv In-Reply-To: <87bofhtd5y.fsf@no-fixed-abode.cable.virginmedia.net> References: <87bofhtd5y.fsf@no-fixed-abode.cable.virginmedia.net> Message-ID: On Thu, Nov 1, 2012 at 7:02 PM, Paul Rudin wrote: > Zero Piraeus writes: > > > There aren't any rules about gmail (except the unwritten rule that to > > be a "real" geek you need to use a mail client that takes a whole > > weekend to configure, and another three years to properly understand). > :D :D :D > > Ha! 3 years? I've been using gnus for nearly 20 years and I still don't > understand it! > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From insideshoes at gmail.com Thu Nov 1 17:51:48 2012 From: insideshoes at gmail.com (inshu chauhan) Date: Thu, 1 Nov 2012 22:51:48 +0100 Subject: pythonic way In-Reply-To: References: Message-ID: OK ..I got it.. On Thu, Nov 1, 2012 at 5:54 PM, Zero Piraeus wrote: > : > > On 1 November 2012 11:32, inshu chauhan wrote: > > what is the most pythonic way to do this : > > > > if 0 < ix < 10 and 0 < iy < 10 ??? > > As everyone else has said, it's perfectly pythonic once you stick the > colon on the end. You might find it more instantly readable with some > extra parentheses: > > if (0 < ix < 10) and (0 < iy < 10): > # do something > > ... but that's really just down to taste. > > -[]z. > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ian.g.kelly at gmail.com Thu Nov 1 17:52:17 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 1 Nov 2012 15:52:17 -0600 Subject: lazy properties? In-Reply-To: <5092EBED.2090002@gmail.com> References: <5092EBED.2090002@gmail.com> Message-ID: On Thu, Nov 1, 2012 at 3:38 PM, Andrea Crotti wrote: > What I would like to write is > @lazy_property > def var_lazy(self): > return long_computation() > > and this should imply that the long_computation is called only once.. If you're using Python 3.2+, then functools.lru_cache probably suffices for your needs. @property @functools.lru_cache() def var_lazy(self): return long_computation() If you really need to shorten that to a single declaration: def lazy_property(func): return property(functools.lru_cache()(func)) From rurpy at yahoo.com Thu Nov 1 18:00:48 2012 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Thu, 1 Nov 2012 15:00:48 -0700 (PDT) Subject: datetime issue In-Reply-To: References: <52847c35-388c-4758-b72d-1d1859b788a3@googlegroups.com> <8370412f-5176-45ff-87eb-72cbca5149d5@googlegroups.com> <5055a1a6$0$29981$c3e8da3$5496439d@news.astraweb.com> <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> Message-ID: <8085a710-46c0-4d1a-b46e-f94eea0e8a0a@googlegroups.com> On Wednesday, October 31, 2012 3:38:57 PM UTC-6, Mark Lawrence wrote: > On 31/10/2012 19:35, ru... at yahoo.com wrote: >> "Broken"? Yes. But so is every piece of software in one way >> or another. Thunderbird is one of the most perpetually buggy >> pierces of software I have ever used on a continuing basis > > Please provide evidence that Thunderbird is buggy. I use it quite > happily, don't have problems, and have never seen anybody complaining > about it. I hesitate to respond to this because thunderbird bugs are really OT, but since you insist... A few years ago started keeping a list TB problems I encountered. I stopped maintaining it a year or so ago. It is a mixture of bugs, misfeatures, and things I couldn't figure out, etc. Many have been fixed in later versions. Nevertheless, it illustrates *my* experience with TB. Note that despite the problems, I still use TB in some contexts. Different apps have different bugs/(mis)features and every person has their own weighting factors on those bugs. End result is different people prefer different apps for different purposes (and some of us find GG the least worst choice for posting here.) ==== * In Search dialog clicked on the Search in folder dropdown after an earlier search and TB crashed (disappeared along with the new message I was editing.) [3.0.1] * Search for japanese text in body no longer works (never finds text even when it exists). [3.0.1] Used to work. * Each mail message displays useless header info that uses 25% of the available vertical space. * When editing a reply message, typing a return at eol results in two blank lines inserted. * When editing message, siometimes edit cursor disappears tho editing still seems to work normally. * Requires voodoo to create hmtl new message when default is plain text or visa versa. Have to hold shift key while clicking "write" button. Shift while choosing "new", "reply", etc in menu doesn't work. * Frequently, clinking on a usenet newsgroup results in TB freeze. Only solution is to remove group and re-add later. (Problem since 2.x days.) * Frequently after an apparently normal shutdown, attempt to restart results in "TB is already running" error. * Some folders in the folder tree view are greyed out and not selectable even though they have no visible different properties than selectable ones. Don't know how they got that way. * Clicking on a folder in the folder tree pane results in a pop- up dialog "this folder is not selectable" that has to be dismissed, even though one is trying to get it's properties, or is selecting it to create a subfolder. * Messages change from "read" to "unread" in folders at random when folder is accessed, or even when it is not accessed. [gone in 3.0.3?] * Interminttently, when I click a usenet message to view it, the contents pane remains blank. Contents of that message remain blank foreever. [new with 3.0.1]. * When TB main window closed while editing an email, can no longer save the email, even if the TB main window is reopened. * Counts of new messages in usenet groups are often wildly high. * When opening up a usenet server, status bar reports "no new messages on server" even though TB then updates the groups with the new messages that *are* on the server. [new in 3.0.1] * After upgrade to TB-3.0, opening a usenet server results not only in the group being scanned for new messages in each group (as before) but also the headers for all those new messages being downloaded (slow on a dialup connection and a waste of time if I don't intend to read anything in that group). No obvious way to revert to original behaviour. * Even tho the number of unread messages listed beside a usenet group in the folder pane is less than the download limit, I sometimes get pop-up asking how many messages to download, when I access the group. (side effect of above problem I think.) * Sometimes TB downloads two copies of each header. * When I compress folders, get a series of several dozen pop- up messages (each requiring a click on "OK") telling me that foplder "xx" is not selectable. * Copy-paste from TB to other app fails if TB is closed before the paste -- the paste buffer appears empty [TB-3.0b4] * Copy paste fails by removing text (forgot if it is just the copied text or other text) in the TB message when pasted somewhere else. [TB-2?] * After upgrade to TB-3.0x, i no longer see a way to create a new imap subfolder. Had to create it using MSOE. * After upgrade to TB-3.0x double clicking on attached .jpg image no longer opens it -- only option is to save and open outside of TB. phfft. * HTF do you change the font size for plain text message composition? Prefs has a setting for html but... * search of body for multiple "anded" text never ends if one of the search boxes is empty. * Search "stop" button doesn't stop search. * One group frequently, without any action on my part, read thousands of old articles, showing them as unread, and when I choose one, responds with "article expired" message. (tb-3.0.4) * Sometimes clicking a group in tree pane causes TB freeze. (This is different than similar problem listed above in that above problem happens when clicking on article in group, and this problem only requires a tb resart to fix whereas above problem required unsubscribing/resubscribing group.) * Sometimes newgroups are colored blue in the tree pane, no idea why. * Sometimes when a message is dragged to a different folder, the message pane continue to display the message contents although the message highlighted in the message list pane is a different one. (tb-3.0.4) * Every time I access a particular news group, TB seems create two connections to it, the second one times out after 5 minutes or so but keeps the progress bar active and the status bar displaying "no new messages". Deleting and recreating acount does not fix. (tb-3.0.4, 3.1.10) * Save as html page gives Gnome "open" dialog, click on directory button to save in parent directory, click save, file is saved in original directory. * Lately when TB is started, there is one account that is always displayed expanded even though it was collapsed when TB was last shutdown. (tb-13.0). From cs at zip.com.au Thu Nov 1 18:08:09 2012 From: cs at zip.com.au (Cameron Simpson) Date: Fri, 2 Nov 2012 09:08:09 +1100 Subject: lazy properties? In-Reply-To: <5092EBED.2090002@gmail.com> References: <5092EBED.2090002@gmail.com> Message-ID: <20121101220809.GA22876@cskk.homeip.net> On 01Nov2012 21:38, Andrea Crotti wrote: | Seeing the wonderful "lazy val" in Scala I thought that I should try to | get the following also in Python. | The problem is that I often have this pattern in my code: | | class Sample: | def __init__(self): | self._var = None | | @property | def var(self): | if self._var is None: | self._var = long_computation() | else: | return self._var | | | which is quite useful when you have some expensive attribute to compute | that is not going to change. | I was trying to generalize it in a @lazy_property but my attempts so far | failed, any help on how I could do that? | | What I would like to write is | @lazy_property | def var_lazy(self): | return long_computation() | | and this should imply that the long_computation is called only once.. I've got one of these which I use exactly as you wish above: def lazy_property(func): ''' A property whose access is controlled by a lock if unset. ''' lock_name = '_lock' prop_name = '_' + func.__name__ unset_object = None def getprop(self): ''' Attempt lockless fetch of property first. Use lock if property is unset. ''' p = getattr(self, prop_name) if p is unset_object: with getattr(self, lock_name): p = getattr(self, prop_name) if p is unset_object: ##debug("compute %s...", prop_name) p = func(self) ##warning("compute %s[%s].%s: %s", self, id(self), prop_name, type(p)) setattr(self, prop_name, p) return p return property(getprop) It computes the cached property name from the function name, but uses a global lock name "_lock" on the basis that the long_computation() will use shared state with the rest of the object. The microoptimisation of the lockless fetch may be either nonportable or pointless. I need to abstract this with a deeper level of nesting to support chaning lock_name, prop_name and unset_object if the caller desires, but for what you want it will work out of the box. I've got a similar thing that watches files for modification and reloads at need. Cheers, -- Cameron Simpson Cordless hoses have been around for quite some time. They're called buckets. - Dan Prener From rurpy at yahoo.com Thu Nov 1 18:08:26 2012 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Thu, 1 Nov 2012 15:08:26 -0700 (PDT) Subject: Obnoxious postings from Google Groups In-Reply-To: References: <8370412f-5176-45ff-87eb-72cbca5149d5@googlegroups.com> <5055a1a6$0$29981$c3e8da3$5496439d@news.astraweb.com> <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> Message-ID: On 11/01/2012 03:55 AM, Jamie Paul Griffin wrote: > Anybody serious about programming should be using a form of > UNIX/Linux if you ask me. It's inconceivable that these systems > should be avoided if you're serious about Software Engineering and > Computer Science, etc. For UNIX there are loads of decent news > reading software and mail user agents to learn and use. slrn is a > good one and point it at gmane.org as someone else pointed out. I > can't even imagine using a browser or Google Groups, etc. now. Are you saying that this group is only for "serious" programmers? "serious" is also a matter of opinion. I have some serious programmer friends who maintain, in complete sincerity, that serious programmers should not waste time on slow, script-kiddie languages like Python, but should be developing their skills with serious professional languages like Java, C#, etc. From andrew3 at r3dsolutions.com Thu Nov 1 18:25:51 2012 From: andrew3 at r3dsolutions.com (Andrew Robinson) Date: Thu, 01 Nov 2012 15:25:51 -0700 Subject: Negative array indicies and slice() In-Reply-To: References: <509053F2.6020900@r3dsolutions.com> <50912ADC.2020401@r3dsolutions.com> <50918716.3080305@r3dsolutions.com> <5092833F.4070609@stoneleaf.us> <50925DE6.7020100@r3dsolutions.com> Message-ID: <5092F6EF.1070301@r3dsolutions.com> On 11/01/2012 12:07 PM, Ian Kelly wrote: > On Thu, Nov 1, 2012 at 5:32 AM, Andrew Robinson > wrote: >> Hmmmm.... was that PEP the active state of Python, when Tim rejected the bug report? > Yes. The PEP was accepted and committed in March 2006 for release in > Python 2.5. The bug report is from June 2006 has a version > classification of Python 2.5, although 2.5 was not actually released > until September 2006. That explain's Peter's remark. Thank you. He looks *much* smarter now. > >> Pep 357 merely added cruft with index(), but really solved nothing. Everything index() does could be implemented in __getitem__ and usually is. > No. There is a significant difference between implementing this on > the container versus implementing it on the indexes. Ethan > implemented his string-based slicing on the container, because the > behavior he wanted was specific to the container type, not the index > type. Custom index types like numpy integers on the other hand > implement __index__ on the index type, because they apply to all > sequences, not specific containers. Hmmm... D'Aprano didn't like the monkey patch;and sub-classing was his fix-all. Part of my summary is based on that conversation with him,and you touched on one of the unfinished points; I responded to him that I thought __getitem__ was under-developed. The object slice() has no knowledge of the size of the sequence; nor can it get that size on it's own, but must passively wait for it to be given to it. The bottom line is: __getitem__ must always *PASS* len( seq ) to slice() each *time* the slice() object is-used. Since this is the case, it would have been better to have list, itself, have a default member which takes the raw slice indicies and does the conversion itself. The size would not need to be duplicated or passed -- memory savings, & speed savings... I'm just clay pidgeoning an idea out here.... Let's apply D'Aprano 's logic to numpy; Numpy could just have subclassed *list*; so let's ignore pure python as a reason to do anything on the behalf on Numpy: Then, lets' consider all thrid party classes; These are where subclassing becomes a pain -- BUT: I think those could all have been injected. >>> class ThirdParty( list ): # Pretend this is someone else's... ... def __init__(self): return ... def __getitem__(self,aSlice): return aSlice ... We know it will default work like this: >>> a=ThirdParty() >>> a[1:2] slice(1, 2, None) # So, here's an injection... >>> ThirdParty.superOnlyOfNumpy__getitem__ = MyClass.__getitem__ >>> ThirdParty.__getitem__ = lambda self,aSlice: ( 1, 3, self.superOnlyOfNumpy__getitem__(aSlice ).step ) >>> a[5:6] (1, 3, None) Numpy could have exported a (workable) function that would modify other list functions to affect ONLY numpy data types (eg: a filter). This allows user's creating their own classes to inject them with Numpy's filter only when they desire; Recall Tim Peter's "explicit is better than implicit" Zen? Most importantly normal programs not using Numpy wouldn't have had to carry around an extra API check for index() *every* single time the heavily used [::] happened. Memory & speed both. It's also a monkey patch, in that index() allows *conflicting* assumptions in violation of the unexpected monkey patch interaction worry. eg: Numpy *CAN* release an index() function on their floats -- at which point a basic no touch class (list itself) will now accept float as an index in direct contradiction of PEP 357's comment on floats... see? My point isn't that this particular implementation I have shown is the best (or even really safe, I'd have to think about that for a while). Go ahead and shoot it down... My point is that, the methods found in slice(), and index() now have moved all the code regarding a sequence *out* of the object which has information on that sequence. It smacks of legacy. The Python parser takes values from many other syntactical constructions and passes them directly to their respective objects -- but in the case of list(), we have a complicated relationship; and not for any reason that can't be handled in a simpler way. Don't consider the present API legacy for a moment, I'm asking hypothetical design questions: How many users actually keep slice() around from every instance of [::] they use? If it is rare, why create the slice() object in the first place and constantly be allocating and de-allocating memory, twice over? (once for the original, and once for the repetitive method which computes dynamic values?) Would a single mutable have less overhead, since it is destroyed anyway? From rurpy at yahoo.com Thu Nov 1 18:28:10 2012 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Thu, 1 Nov 2012 15:28:10 -0700 (PDT) Subject: datetime issue In-Reply-To: References: <52847c35-388c-4758-b72d-1d1859b788a3@googlegroups.com> <8370412f-5176-45ff-87eb-72cbca5149d5@googlegroups.com> <5055a1a6$0$29981$c3e8da3$5496439d@news.astraweb.com> <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> Message-ID: On 11/01/2012 06:09 AM, Grant Edwards wrote:> On 2012-10-31, rurpy at yahoo.com wrote: >> On 10/31/2012 09:11 AM, Grant Edwards wrote:> On 2012-09-16, ???????? ?????? wrote: >>> >>>> Iam positng via google groups using chrome, thats all i know. >>> >>> Learn something else. Google Groups is seriously and permanently >>> broken, and all posts from Google Groups are filtered out and ignored >>> by many people (including myself -- I only saw this because somebody >>> else replied to it). >> >> Feel free to filter whatever you want but be aware than in >> doing so you risk missing information that could help you >> avoid disseminating erroneous info. Of course, carrying out >> some kind of private war against Google Groups may be more >> important to you than that... > > Based on past experience, the chances of useful information being > posted via Google Groups is very small. The benefit of avoiding all > the spam and garbage the comes from that source is more than worth the > small risk of missing something worthwhile. > > Note that I'm _not_ talking about people posting to the mailing list > using Gmail -- only people posting via the Google Groups web UI. Your experience is that most GG posts are junk but not most Gmail posts. And yet someone else posted that Gmail posts are the junk ones. So I wonder how accurate these evaluations are. In either case, while there has been some spam and garbage posts in c.l.p in the past, I haven't seen very many lately. And even in the past, they seemed pretty recognizable to me and easily skipped over. As for stupid questions, undesired formatting etc, I seldom look at the message headers so I never noticed whether they are mostly from GG or Gmail. But then again I never found it hard to glance at a post and move on if I didn't want to read it. YMMV. From ian.g.kelly at gmail.com Thu Nov 1 18:45:22 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 1 Nov 2012 16:45:22 -0600 Subject: Private methods In-Reply-To: <5074b86b$0$6574$c3e8da3$5496439d@news.astraweb.com> References: <1krpbz9.1wge3j11123k2vN%real-not-anti-spam-address@apple-juice.co.uk> <1krpdak.u0qy9e1a4knspN%real-not-anti-spam-address@apple-juice.co.uk> <50742FD4.8030007@gmail.com> <5074b86b$0$6574$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, Oct 9, 2012 at 5:51 PM, Steven D'Aprano wrote: > On Tue, 09 Oct 2012 11:08:13 -0600, Ian Kelly wrote: > >> I tend to view name mangling as being more for avoiding internal >> attribute collisions in complex inheritance structures than for >> designating names as private. > > Really? I tend to view name mangling as a waste of time, and complex > inheritance structures as something to avoid. Name mangling is also useful for object tagging. Suppose you have object A that is passed object B and needs to track some data concerning object B, but does not need a strong reference to B. One solution is to use a weak-key dictionary, but this relies upon B being hashable, and I find it neater to just store the data on B itself. The problem is that whatever name you use might conflict with an existing attribute belonging to B. class Tagger(object): def tag(self, taggee): taggee.__tag = some_tag_data One of the curious ramifications of Python's name-mangling system is that even though the attribute above is being set on some arbitrary object, the mangling that is applied is in any case that of the class Tagger. Thus the mangled attribute name ends up being "_Tagger__tag", which is unlikely to cause a conflict. There are some disadvantages to tagging. One is that you can't tag objects of built-in types. Another is that you can't tag instances of classes with __slots__. I tend to view the latter as another reason to avoid using __slots__. From miki.tebeka at gmail.com Thu Nov 1 19:30:42 2012 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Thu, 1 Nov 2012 16:30:42 -0700 (PDT) Subject: lazy properties? In-Reply-To: References: <5092EBED.2090002@gmail.com> Message-ID: <6efda4fe-4c69-439b-a2ae-8bed20ce08df@googlegroups.com> > If you're using Python 3.2+, then functools.lru_cache probably > ... And if you're on 2.X, you can grab lru_cache from http://code.activestate.com/recipes/498245-lru-and-lfu-cache-decorators/ From miki.tebeka at gmail.com Thu Nov 1 19:30:42 2012 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Thu, 1 Nov 2012 16:30:42 -0700 (PDT) Subject: lazy properties? In-Reply-To: References: <5092EBED.2090002@gmail.com> Message-ID: <6efda4fe-4c69-439b-a2ae-8bed20ce08df@googlegroups.com> > If you're using Python 3.2+, then functools.lru_cache probably > ... And if you're on 2.X, you can grab lru_cache from http://code.activestate.com/recipes/498245-lru-and-lfu-cache-decorators/ From rosuav at gmail.com Thu Nov 1 19:32:08 2012 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 2 Nov 2012 10:32:08 +1100 Subject: Obnoxious postings from Google Groups In-Reply-To: References: <8370412f-5176-45ff-87eb-72cbca5149d5@googlegroups.com> <5055a1a6$0$29981$c3e8da3$5496439d@news.astraweb.com> <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> Message-ID: On Fri, Nov 2, 2012 at 9:08 AM, wrote: > On 11/01/2012 03:55 AM, Jamie Paul Griffin wrote: >> Anybody serious about programming should be using a form of >> UNIX/Linux if you ask me. It's inconceivable that these systems >> should be avoided if you're serious about Software Engineering and >> Computer Science, etc. For UNIX there are loads of decent news >> reading software and mail user agents to learn and use. slrn is a >> good one and point it at gmane.org as someone else pointed out. I >> can't even imagine using a browser or Google Groups, etc. now. > > Are you saying that this group is only for "serious" programmers? It's not; also, so long as Python maintains an official Windows build, this list/group will be fielding questions about Windows. But there's still good reason to grab a Linux. http://catb.org/~esr/faqs/smart-questions.html#idp29970256 > "serious" is also a matter of opinion. I have some serious > programmer friends who maintain, in complete sincerity, that > serious programmers should not waste time on slow, script-kiddie > languages like Python, but should be developing their skills > with serious professional languages like Java, C#, etc. And there are probably still a few around who maintain that Java, C#, and even C are too modern, and that serious programmers use FORTRAN or COBOL. Or assembly language. Let 'em. Meanwhile I'll have demonstrable code done while they're still fiddling around with details, because my languages (including, though by no means limited to, Python) do most of the work for me. ChrisA From steve+comp.lang.python at pearwood.info Thu Nov 1 20:27:15 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Nov 2012 00:27:15 GMT Subject: sort order for strings of digits References: <5091b7cd$0$29967$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50931362$0$29967$c3e8da3$5496439d@news.astraweb.com> On Thu, 01 Nov 2012 11:53:06 +1100, Chris Angelico wrote: > On Thu, Nov 1, 2012 at 10:44 AM, Steven D'Aprano > wrote: >> On the contrary. If you are using cmp with sort, your sorts are slow, >> and you should upgrade to using a key function as soon as possible. >> >> > But cmp_to_key doesn't actually improve anything. So I'm not sure how > Py3 has achieved anything; Py2 supported key-based sorting already. Yes, but there is a lot of old code pre-dating key-based sorts. There's also some examples of code where it isn't obvious how to write it as a key-based sort, but a comparison function is simple. And people coming from other languages that only support comparison-based sorts (C?) will probably continue with what they know. Even though key-based sorting is better, there's a lot of comparison sorting that falls under "if it ain't broke, don't fix it". So even though key-based sorts are better, there are still comparison- based sorts in the wild. Python 2 has to support them. Python 3, which is allowed to break backwards compatibility, does not. So when porting to 3, you have to change the sorts. Most of the time it is simple to convert a comparison-based sort to a key- based sort. For the cases where you either can't come up with a good key function yourself, or were you want to do so mechanically, Python provides cmp_to_key. -- Steven From ian.g.kelly at gmail.com Thu Nov 1 20:32:36 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 1 Nov 2012 18:32:36 -0600 Subject: Negative array indicies and slice() In-Reply-To: <5092F6EF.1070301@r3dsolutions.com> References: <509053F2.6020900@r3dsolutions.com> <50912ADC.2020401@r3dsolutions.com> <50918716.3080305@r3dsolutions.com> <5092833F.4070609@stoneleaf.us> <50925DE6.7020100@r3dsolutions.com> <5092F6EF.1070301@r3dsolutions.com> Message-ID: On Thu, Nov 1, 2012 at 4:25 PM, Andrew Robinson wrote: > The bottom line is: __getitem__ must always *PASS* len( seq ) to slice() > each *time* the slice() object is-used. Since this is the case, it would > have been better to have list, itself, have a default member which takes the > raw slice indicies and does the conversion itself. The size would not need > to be duplicated or passed -- memory savings, & speed savings... And then tuple would need to duplicate the same code. As would deque. And str. And numpy.array, and anything else that can be sliced, including custom sequence classes. > Let's apply D'Aprano 's logic to numpy; Numpy could just have subclassed > *list*; Numpy arrays are very different internally from lists. They are basically fancy wrappers of C arrays, whereas lists are a higher-level abstraction. They allow for multiple dimensions, which lists do not. Slices of numpy arrays produce views, whereas slices of lists produce brand new lists. And they certainly do not obey the Liskov Substitution Principle with respect to lists. >>>> class ThirdParty( list ): # Pretend this is someone else's... > ... def __init__(self): return > ... def __getitem__(self,aSlice): return aSlice > ... > > We know it will default work like this: >>>> a=ThirdParty() >>>> a[1:2] > slice(1, 2, None) > > # So, here's an injection... >>>> ThirdParty.superOnlyOfNumpy__getitem__ = MyClass.__getitem__ >>>> ThirdParty.__getitem__ = lambda self,aSlice: ( 1, 3, >>>> self.superOnlyOfNumpy__getitem__(aSlice ).step ) >>>> a[5:6] > (1, 3, None) I'm not understanding what this is meant to demonstrate. Is "MyClass" a find-replace error of "ThirdParty"? Why do you have __getitem__ returning slice objects instead of items or subsequences? What does this example have to do with numpy? > Numpy could have exported a (workable) function that would modify other list > functions to affect ONLY numpy data types (eg: a filter). This allows > user's creating their own classes to inject them with Numpy's filter only > when they desire; > > Recall Tim Peter's "explicit is better than implicit" Zen? We could also require the user to explicitly declare when they're performing arithmetic on variables that might not be floats. Then we can turn off run-time type checking unless the user explicitly requests it, all in the name of micro-optimization and explicitness. Seriously, whether x is usable as a sequence index is a property of x, not a property of the sequence. Users shouldn't need to pick and choose *which* particular sequence index types their custom sequences are willing to accept. They should even be able to accept sequence index types that haven't been written yet. > Most importantly normal programs not using Numpy wouldn't have had to carry > around an extra API check for index() *every* single time the heavily used > [::] happened. Memory & speed both. The O(1) __index__ check is probably rather inconsequential compared to the O(n) cost of actually performing the slicing. > It's also a monkey patch, in that index() allows *conflicting* assumptions > in violation of the unexpected monkey patch interaction worry. > > eg: Numpy *CAN* release an index() function on their floats -- at which > point a basic no touch class (list itself) will now accept float as an index > in direct contradiction of PEP 357's comment on floats... see? Such a change would only affect numpy floats, not all floats, so it would not be a monkey-patch. In any case, that would be incorrect usage of __index__. We're all consenting adults here; we don't need supervision to protect us from buggy third-party code. From dihedral88888 at googlemail.com Thu Nov 1 21:08:33 2012 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Thu, 1 Nov 2012 18:08:33 -0700 (PDT) Subject: Negative array indicies and slice() In-Reply-To: <6998a955-7b34-4f4f-b8d6-62d1028f7561@googlegroups.com> References: <6998a955-7b34-4f4f-b8d6-62d1028f7561@googlegroups.com> Message-ID: andrew... at gmail.com? 2012?10?29????UTC+8??11?12?11???? > The slice operator does not give any way (I can find!) to take slices from negative to positive indexes, although the range is not empty, nor the expected indexes out of range that I am supplying. > > > > Many programs that I write would require introducing variables and logical statements to correct the problem which is very lengthy and error prone unless there is a simple work around. > > > > I *hate* replicating code every time I need to do this! > > > > I also don't understand why slice() is not equivalent to an iterator, but can replace an integer in __getitem__() whereas xrange() can't. > > > > > > Here's an example for Linux shell, otherwise remove /bin/env... > > {{{#!/bin/env python > > a=[1,2,3,4,5,6,7,8,9,10] > > print a[-4:3] # I am interested in getting [7,8,9,10,1,2] but I get []. > > }}} I'll suggest to use the reverse method to get what you want. Of course, the reverse method is not efficient for a list of a huge number of objects in python. From steve+comp.lang.python at pearwood.info Thu Nov 1 21:25:37 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Nov 2012 01:25:37 GMT Subject: Obnoxious postings from Google Groups References: <8370412f-5176-45ff-87eb-72cbca5149d5@googlegroups.com> <5055a1a6$0$29981$c3e8da3$5496439d@news.astraweb.com> <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> Message-ID: <50932111$0$29967$c3e8da3$5496439d@news.astraweb.com> On Fri, 02 Nov 2012 10:32:08 +1100, Chris Angelico wrote: > And there are probably still a few around who maintain that Java, C#, > and even C are too modern, and that serious programmers use FORTRAN or > COBOL. Huh. If you're messing about with ancient[1] languages like Java, C# and especially C, you're not a real programmer. Real programmers use modern, advanced languages like D, Erlang, Go or Haskell. [1] "Ancient" is a frame of mind, not a chronological state. -- Steven From roy at panix.com Thu Nov 1 21:42:22 2012 From: roy at panix.com (Roy Smith) Date: Thu, 01 Nov 2012 21:42:22 -0400 Subject: Obnoxious postings from Google Groups References: <8370412f-5176-45ff-87eb-72cbca5149d5@googlegroups.com> <5055a1a6$0$29981$c3e8da3$5496439d@news.astraweb.com> <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> <50932111$0$29967$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <50932111$0$29967$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > Huh. If you're messing about with ancient[1] languages like Java, C# and > especially C, you're not a real programmer. Real programmers use modern, > advanced languages like D, Erlang, Go or Haskell. Does anybody actually use D for anything? I looked at the language a while ago. There seemed to be a lot in it that made sense. Does it get any real use, or is it just an interesting research project? From rhodri at wildebst.demon.co.uk Thu Nov 1 22:07:19 2012 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Fri, 02 Nov 2012 02:07:19 -0000 Subject: Obnoxious postings from Google Groups References: <8370412f-5176-45ff-87eb-72cbca5149d5@googlegroups.com> <5055a1a6$0$29981$c3e8da3$5496439d@news.astraweb.com> <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> <50932111$0$29967$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, 02 Nov 2012 01:25:37 -0000, Steven D'Aprano wrote: > Huh. If you're messing about with ancient[1] languages like Java, C# and > especially C, you're not a real programmer. Real programmers use modern, > advanced languages like D, Erlang, Go or Haskell. Advanced? Huh. I have here a language that does exactly what I want without all that messy syntax nonsense. I call it "Research Assistant." -- Rhodri James *-* Wildebeest Herder to the Masses From steve+comp.lang.python at pearwood.info Thu Nov 1 22:14:55 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Nov 2012 02:14:55 GMT Subject: Negative array indicies and slice() References: <509053F2.6020900@r3dsolutions.com> <50912ADC.2020401@r3dsolutions.com> <50918716.3080305@r3dsolutions.com> <5092833F.4070609@stoneleaf.us> <50925DE6.7020100@r3dsolutions.com> Message-ID: <50932c9f$0$29967$c3e8da3$5496439d@news.astraweb.com> On Thu, 01 Nov 2012 15:25:51 -0700, Andrew Robinson wrote: > On 11/01/2012 12:07 PM, Ian Kelly wrote: >>> Pep 357 merely added cruft with index(), but really solved nothing. >>> Everything index() does could be implemented in __getitem__ and >>> usually is. >> >> No. There is a significant difference between implementing this on the >> container versus implementing it on the indexes. Ethan implemented his >> string-based slicing on the container, because the behavior he wanted >> was specific to the container type, not the index type. Custom index >> types like numpy integers on the other hand implement __index__ on the >> index type, because they apply to all sequences, not specific >> containers. > > Hmmm... > D'Aprano didn't like the monkey patch;and sub-classing was his fix-all. I pointed out that monkey-patching is a bad idea, even if it worked. But it doesn't work -- you simply cannot monkey-patch built-ins in Python. Regardless of whether "I like" the m-p or not, *you can't use it* because you patch built-in list methods. The best you could do is subclass list, then shadow the built-in name "list" with your subclass. But that gives all sorts of problems too, in some ways even worse than monkey-patching. You started this thread with a question about slicing. You believe that one particular use-case for slicing, which involves interpreting lists as circular rather than linear, is the use-case that built-in list slicing should have supported. Fine, you're entitled to your option. But that boat has sailed about 20 years ago. Python didn't make that choice, and it won't change now. If you write up a PEP, you could aim to have the built-in behaviour changed for Python 4 in perhaps another 10-15 years or so. But for the time being, that's not what lists, tuples, strings, etc. do. If you want that behaviour, if you want a circular list, then you have to implement it yourself, and the easiest way to do so is with a subclass. That's not a "fix-all". I certainly don't believe that subclassing is the *only* way to fix this, nor that it will fix "all" things. But it might fix *some* things, such as you wanting a data type that is like a circular list rather than a linear list. If you prefer to create a circular-list class from scratch, re- implementing all the list-like behaviour, instead of inheriting from an existing class, then by all means go right ahead. If you have a good reason to spend days or weeks writing, testing, debugging and fine-tuning your new class, instead of about 15 minutes with a subclass, then I'm certainly not going to tell you not to. > Part of my summary is based on that conversation with him,and you > touched on one of the unfinished points; I responded to him that I > thought __getitem__ was under-developed. The object slice() has no > knowledge of the size of the sequence; nor can it get that size on it's > own, but must passively wait for it to be given to it. That's because the slice object is independent of the sequence. As I demonstrated, you can pass a slice object to multiple sequences. This is a feature, not a bug. > The bottom line is: __getitem__ must always *PASS* len( seq ) to > slice() each *time* the slice() object is-used. The bottom line is: even if you are right, so what? The slice object doesn't know what the length of the sequence is. What makes you think that __getitem__ passes the length to slice()? Why would it need to recreate a slice object that already exists? It is the *sequence*, not the slice object, that is responsible for extracting the appropriate items when __getitem__ is called. __getitem__ gets a slice object as argument, it doesn't create one. It no more creates the slice object than mylist[5] creates the int 5. > Since this is the case, But it isn't. > it would have been better to have list, itself, have a default member > which takes the raw slice indicies and does the conversion itself. The > size would not need to be duplicated or passed -- memory savings, & > speed savings... We have already demonstrated that slice objects are smaller than (x)range objects and three-item tuples. In Python 3.3: py> sys.getsizeof(range(1, 10, 2)) # xrange remained in Python 3 24 py> sys.getsizeof((1, 10, 2)) 36 py> sys.getsizeof(slice(1, 10, 2)) 20 It might help you to be taken seriously if you base your reasoning on Python as it actually is, rather than counter-factual assumptions. > I'm just clay pidgeoning an idea out here.... Let's apply D'Aprano 's > logic to numpy; Numpy could just have subclassed *list*; Sure they could have, if numpy arrays were intended to be a small variation on Python lists. But they weren't, so they didn't. > so let's ignore > pure python as a reason to do anything on the behalf on Numpy: > > Then, lets' consider all thrid party classes; These are where > subclassing becomes a pain -- BUT: I think those could all have been > injected. > > >>> class ThirdParty( list ): # Pretend this is someone else's... > ... def __init__(self): return > ... def __getitem__(self,aSlice): return aSlice > ... Strange and bizarre semantics for slicing, but okay. > We know it will default work like this: > >>> a=ThirdParty() > >>> a[1:2] > slice(1, 2, None) > > # So, here's an injection... > >>> ThirdParty.superOnlyOfNumpy__getitem__ = MyClass.__getitem__ > >>> ThirdParty.__getitem__ = lambda self,aSlice: ( 1, 3, > self.superOnlyOfNumpy__getitem__(aSlice ).step ) > >>> a[5:6] > (1, 3, None) > > Numpy could have exported a (workable) function that would modify other > list functions to affect ONLY numpy data types (eg: a filter). This > allows user's creating their own classes to inject them with Numpy's > filter only when they desire; Sure, the numpy people could have done this, if they were smoking crack. Have you actually programmed before? Judging from the techniques you seem to prefer for everyday use (monkey-patching other classes) and techniques you seem to hate (subclassing), I'm getting the impression you've read about bleeding edge programming hacks but never actually written code. Sort of like somebody who has never driven a car, but fantasises about doing the sort of extreme stunt driving that kills people in real life and occasionally even stunt drivers. And now you are *insisting* that everyone should drive like that, *all the time*, because stopping at traffic lights is so inefficient. Of course, I could be wrong. Maybe you've been programming for years and know exactly what you are doing. But if so, you are coming across as exactly the kind of cowboy coder that I pray to all the gods I never have deal with in real life. [...] > Don't consider the present API legacy for a moment, I'm asking > hypothetical design questions: > > How many users actually keep slice() around from every instance of [::] > they use? Does it matter? It is supported behaviour, so even *one* user is enough. > If it is rare, why create the slice() object in the first place and > constantly be allocating and de-allocating memory, twice over? (once for > the original, and once for the repetitive method which computes dynamic > values?) Huh? As opposed to what? Creating an xrange() object, and constantly allocating and de-allocating memory? Or a tuple? Same again. Some sort of object has to be created. And I have no idea what you are talking about "twice over". What "repetitive method which computers dynamic values"? In any case, I return to my comment earlier in this thread: if you have profiled your application and have hard evidence that creating slice objects is a bottleneck, then we can talk about optimizing the slice objects. Until then, you are wasting your time and ours by prematurely optimizing the wrong parts of your code. > Would a single mutable have less overhead, since it is > destroyed anyway? What? This question makes no sense. Why do you think that mutable objects have "less overhead" than immutable ones? -- Steven From richardbp at gmail.com Thu Nov 1 22:16:17 2012 From: richardbp at gmail.com (Richard) Date: Thu, 1 Nov 2012 19:16:17 -0700 (PDT) Subject: Avoiding defunct processes Message-ID: Hello, I create child processes with subprocess.Popen(). Then I either wait for them to finish or kill them. Either way these processes end up as defunct until the parent process completes: $ ps e 6851 pts/5 Z+ 1:29 [python] This confuses another library as to whether the processes are complete. For now I detect which processes are defunct by parsing the output of "ps". What would you recommend? I am hoping there is a cleaner way. Richard From rosuav at gmail.com Thu Nov 1 22:36:40 2012 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 2 Nov 2012 13:36:40 +1100 Subject: Avoiding defunct processes In-Reply-To: References: Message-ID: On Fri, Nov 2, 2012 at 1:16 PM, Richard wrote: > Hello, > > I create child processes with subprocess.Popen(). > Then I either wait for them to finish or kill them. > Either way these processes end up as defunct until the parent process > completes: > $ ps e > 6851 pts/5 Z+ 1:29 [python] > > This confuses another library as to whether the processes are > complete. > For now I detect which processes are defunct by parsing the output of > "ps". > What would you recommend? I am hoping there is a cleaner way. That's a zombie process, it's finished but the parent hasn't wait()ed for it yet. http://docs.python.org/3.3/library/subprocess.html#subprocess.Popen.wait Once the process has ended, call that to get its return value and clean everything up. ChrisA From martinhewitson at mac.com Fri Nov 2 02:16:09 2012 From: martinhewitson at mac.com (Martin Hewitson) Date: Fri, 02 Nov 2012 07:16:09 +0100 Subject: Organisation of python classes and their methods Message-ID: Dear list, I'm relatively new to Python and have googled and googled but haven't found a reasonable answer to this question, so I thought I'd ask it here. I'm beginning a large Python project which contains many packages, modules and classes. The organisation of those is clear to me. Now, the classes can contain many methods (100s of data analysis methods) which operate on instances of the class they belong to. These methods can be long and complex. So if I put these methods all in the module file inside the class, the file will get insanely long. Reading on google, the answer is usually "refactor", but that really doesn't make sense here. It's just that the methods are many, and each method can be a long piece of code. So, is there a way to put these methods in their own files and have them 'included' in the class somehow? I read a little about mixins but all the solutions looked very hacky. Is there an official python way to do this? I don't like having source files with 100's of lines of code in, let alone 1000's. Many thanks, Martin From naruto0.1 at live.cn Fri Nov 2 03:14:32 2012 From: naruto0.1 at live.cn (jack) Date: Fri, 2 Nov 2012 15:14:32 +0800 Subject: pythonic way In-Reply-To: References: Message-ID: Sometimes, I need to alter the element as traverse a list like this (it's a sample): c = range(10) i = 0 for ele in c: # do something # branch: c[i] = # value i += 1 How to be pythonic? 2012/11/2 0:54, Zero Piraeus : > : > > On 1 November 2012 11:32, inshu chauhan wrote: >> what is the most pythonic way to do this : >> >> if 0 < ix < 10 and 0 < iy < 10 ??? > As everyone else has said, it's perfectly pythonic once you stick the > colon on the end. You might find it more instantly readable with some > extra parentheses: > > if (0 < ix < 10) and (0 < iy < 10): > # do something > > ... but that's really just down to taste. > > -[]z. From no.email at nospam.invalid Fri Nov 2 03:38:31 2012 From: no.email at nospam.invalid (Paul Rubin) Date: Fri, 02 Nov 2012 00:38:31 -0700 Subject: Organisation of python classes and their methods References: Message-ID: <7xa9v0wj2g.fsf@ruckus.brouhaha.com> Martin Hewitson writes: > So, is there a way to put these methods in their own files and have > them 'included' in the class somehow? ... Is there an official python > way to do this? I don't like having source files with 100's of lines > of code in, let alone 1000's. That code sounds kind of smelly... why are there so many methods per class? Python lets you inject new methods into existing classes (this is sometimes called duck punching) but I don't recommend doing this. A few hundred lines of code in a file is completely reasonable. Even a few thousand is ok. IME it starts getting unwieldy at 3000 or so. From rosuav at gmail.com Fri Nov 2 03:56:33 2012 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 2 Nov 2012 18:56:33 +1100 Subject: pythonic way In-Reply-To: References: Message-ID: On Fri, Nov 2, 2012 at 6:14 PM, jack wrote: > Sometimes, I need to alter the element as traverse a list like this (it's a > sample): > c = range(10) > i = 0 > for ele in c: > # do something > # branch: > c[i] = # value > i += 1 > > How to be pythonic? Check out the enumerate() function. ChrisA From __peter__ at web.de Fri Nov 2 04:00:39 2012 From: __peter__ at web.de (Peter Otten) Date: Fri, 02 Nov 2012 09:00:39 +0100 Subject: Organisation of python classes and their methods References: Message-ID: Martin Hewitson wrote: > Dear list, > > I'm relatively new to Python and have googled and googled but haven't > found a reasonable answer to this question, so I thought I'd ask it here. > > I'm beginning a large Python project which contains many packages, modules > and classes. The organisation of those is clear to me. > > Now, the classes can contain many methods (100s of data analysis methods) > which operate on instances of the class they belong to. These methods can > be long and complex. So if I put these methods all in the module file > inside the class, the file will get insanely long. Reading on google, the > answer is usually "refactor", but that really doesn't make sense here. > It's just that the methods are many, and each method can be a long piece > of code. So, is there a way to put these methods in their own files and > have them 'included' in the class somehow? I read a little about mixins > but all the solutions looked very hacky. Is there an official python way > to do this? I don't like having source files with 100's of lines of code > in, let alone 1000's. You googled, found the right answer ("refactor"), didn't like it and are now looking to cure the symptoms of the original problem? Seriously, a good editor can deal with a long source file, but a class with hundreds of methods will bring trouble to any old brain. From martinhewitson at mac.com Fri Nov 2 04:08:07 2012 From: martinhewitson at mac.com (Martin Hewitson) Date: Fri, 02 Nov 2012 09:08:07 +0100 Subject: Organisation of python classes and their methods In-Reply-To: <7xa9v0wj2g.fsf@ruckus.brouhaha.com> References: <7xa9v0wj2g.fsf@ruckus.brouhaha.com> Message-ID: On 2, Nov, 2012, at 08:38 AM, Paul Rubin wrote: > Martin Hewitson writes: >> So, is there a way to put these methods in their own files and have >> them 'included' in the class somehow? ... Is there an official python >> way to do this? I don't like having source files with 100's of lines >> of code in, let alone 1000's. > > That code sounds kind of smelly... why are there so many methods per > class? Simply because there are many different ways to process the data. The class encapsulates the data, and the user can process the data in many ways. Of course, one could have classes which encapsulate the algorithms, as well as the data, but it also seems natural to me to have algorithms as methods which are part of the data class, so the user operates on the data using methods of that class. > > Python lets you inject new methods into existing classes (this is > sometimes called duck punching) but I don't recommend doing this. Is there not a way just to declare the method in the class and put the actual implementation in another file on the python path so that it's picked up a run time? > > A few hundred lines of code in a file is completely reasonable. Even a > few thousand is ok. IME it starts getting unwieldy at 3000 or so. In all other projects (in other languages) I've always tried to keep individual files short to ease maintainability and search-ability (if there is such a word). Even if one takes reasonable numbers: 20 methods, each method has 20 lines of documentation, then we immediately have 400 lines in the file before writing a line of code. It would seem much more natural to me to have these methods in their own file, grouped nicely in sub-directories. But it seems this is not the python way. Sigh. Thanks for your thoughts, Martin From sacharook at gmail.com Fri Nov 2 04:19:27 2012 From: sacharook at gmail.com (Sacha Rook) Date: Fri, 2 Nov 2012 01:19:27 -0700 (PDT) Subject: python-forum Message-ID: Hi does anyone know where the python-form.org site has gone? From martinhewitson at mac.com Fri Nov 2 04:20:52 2012 From: martinhewitson at mac.com (Martin Hewitson) Date: Fri, 02 Nov 2012 09:20:52 +0100 Subject: Organisation of python classes and their methods In-Reply-To: References: Message-ID: On 2, Nov, 2012, at 09:00 AM, Peter Otten <__peter__ at web.de> wrote: > Martin Hewitson wrote: > >> Dear list, >> >> I'm relatively new to Python and have googled and googled but haven't >> found a reasonable answer to this question, so I thought I'd ask it here. >> >> I'm beginning a large Python project which contains many packages, modules >> and classes. The organisation of those is clear to me. >> >> Now, the classes can contain many methods (100s of data analysis methods) >> which operate on instances of the class they belong to. These methods can >> be long and complex. So if I put these methods all in the module file >> inside the class, the file will get insanely long. Reading on google, the >> answer is usually "refactor", but that really doesn't make sense here. >> It's just that the methods are many, and each method can be a long piece >> of code. So, is there a way to put these methods in their own files and >> have them 'included' in the class somehow? I read a little about mixins >> but all the solutions looked very hacky. Is there an official python way >> to do this? I don't like having source files with 100's of lines of code >> in, let alone 1000's. > > You googled, found the right answer ("refactor"), didn't like it and are now > looking to cure the symptoms of the original problem? > Seriously, a good editor can deal with a long source file, but a class with > hundreds of methods will bring trouble to any old brain. Well, here we disagree. Suppose I have a class which encapsulates time-series data. Below is a list of the absolute minimum methods one would have to process that data. That's close to 100 already before even having any specialised methods for dealing with the data. Each of these methods will have maybe 20 lines of documentation. That's 2000 lines already. And what if someone wants to extend that class to add their own processing methods? It would a maintenance nightmare for them to edit the actual class file, which they would then have to repeat each time a new version of the 'official' class file is released. So maybe some rethinking of this design is needed to handle this 'limitation' of python. Perhaps grouping the processing algorithms into methods of processing classes, then pass the data objects to these methods. But somehow I don't like that. I have the feeling these methods would end up peppered with things like: if this data type, do this else if this data type, do this else .... normally this would be solved by overloading methods in different data subclasses. More thinking needed, clearly. Martin 'abs' 'acos' 'asin' 'atan' 'atan2' 'average' 'cohere' 'conv' 'corr' 'cos' 'cov' 'cpsd' 'detrend' 'dft' 'diff' 'downsample' 'exp' 'export' 'fft' 'fftfilt' 'filter' 'filtfilt' 'find' 'heterodyne' 'hist' 'imag' 'integrate' 'interp' 'join' 'le' 'lincom' 'ln' 'load' 'log' 'log10' 'lscov' 'max' 'mean' 'median' 'min' 'minus' 'mode' 'mpower' 'mrdivide' 'mtimes' 'ne' 'norm' 'or' 'plot' 'plus' 'polyfit' 'power' 'psd' 'rdivide' 'real' 'resample' 'rms' 'round' 'save' 'scale' 'search' 'select' 'sin' 'smoother' 'sort' 'spectrogram' 'split' 'sqrt' 'std' 'sum' 'sumjoin' 'svd' 'tan' 'tfe' 'timeaverage' 'times' 'timeshift' 'transpose' 'uminus' 'upsample' 'zeropad' From clp2 at rebertia.com Fri Nov 2 04:26:40 2012 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 2 Nov 2012 01:26:40 -0700 Subject: python-forum In-Reply-To: References: Message-ID: On Fri, Nov 2, 2012 at 1:19 AM, Sacha Rook wrote: > Hi does anyone know where the python-form.org site has gone? Some googling suggests that it's under new management: http://mcompute.co.uk/showthread.php?tid=2161 But comp.lang.python/python-list is better anyway [ ;-) ], and you're already here, so why not stay a while? Cheers, Chris From stefan at epy.co.at Fri Nov 2 04:36:11 2012 From: stefan at epy.co.at (Stefan H. Holek) Date: Fri, 2 Nov 2012 09:36:11 +0100 Subject: lazy properties? In-Reply-To: <5092EBED.2090002@gmail.com> References: <5092EBED.2090002@gmail.com> Message-ID: <84EDB57A-3B7A-410E-9A57-206280D3781B@epy.co.at> On 01.11.2012, at 22:38, Andrea Crotti wrote: > Seeing the wonderful "lazy val" in Scala I thought that I should try to get the following also in Python. > The problem is that I often have this pattern in my code: > > class Sample: > def __init__(self): > self._var = None > > @property > def var(self): > if self._var is None: > self._var = long_computation() > else: > return self._var > > > which is quite useful when you have some expensive attribute to compute that is not going to change. > I was trying to generalize it in a @lazy_property but my attempts so far failed, any help on how I could do that? There is a ready made and well tested lazy decorator at http://pypi.python.org/pypi/lazy Stefan -- Stefan H. Holek stefan at epy.co.at From breamoreboy at yahoo.co.uk Fri Nov 2 04:40:06 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 02 Nov 2012 08:40:06 +0000 Subject: Organisation of python classes and their methods In-Reply-To: References: <7xa9v0wj2g.fsf@ruckus.brouhaha.com> Message-ID: On 02/11/2012 08:08, Martin Hewitson wrote: > > Even if one takes reasonable numbers: 20 methods, each method has 20 lines of documentation, then we immediately have 400 lines in the file before writing a line of code. It would seem much more natural to me to have these methods in their own file, grouped nicely in sub-directories. But it seems this is not the python way. Sigh. > > Thanks for your thoughts, > > Martin > 20 lines of documentation per method? As far as I'm concerned that's not a smell, that's a stink. -- Cheers. Mark Lawrence. From martinhewitson at mac.com Fri Nov 2 04:45:43 2012 From: martinhewitson at mac.com (Martin Hewitson) Date: Fri, 02 Nov 2012 09:45:43 +0100 Subject: Organisation of python classes and their methods In-Reply-To: References: <7xa9v0wj2g.fsf@ruckus.brouhaha.com> Message-ID: On 2, Nov, 2012, at 09:40 AM, Mark Lawrence wrote: > On 02/11/2012 08:08, Martin Hewitson wrote: >> >> Even if one takes reasonable numbers: 20 methods, each method has 20 lines of documentation, then we immediately have 400 lines in the file before writing a line of code. It would seem much more natural to me to have these methods in their own file, grouped nicely in sub-directories. But it seems this is not the python way. Sigh. >> >> Thanks for your thoughts, >> >> Martin >> > > 20 lines of documentation per method? As far as I'm concerned that's not a smell, that's a stink. Wow, I don't think I've ever been criticised before for writing too much documentation :) I guess we have different end users. This is not a set of classes for other developers to use: it's a set of classes which creates a data analysis environment for scientists to use. They are not programmers, and expect the algorithms to be documented in detail. Martin > > -- > Cheers. > > Mark Lawrence. > > -- > http://mail.python.org/mailman/listinfo/python-list From andrew3 at r3dsolutions.com Fri Nov 2 04:57:11 2012 From: andrew3 at r3dsolutions.com (Andrew Robinson) Date: Fri, 02 Nov 2012 01:57:11 -0700 Subject: Negative array indicies and slice() In-Reply-To: <50932c9f$0$29967$c3e8da3$5496439d@news.astraweb.com> References: <509053F2.6020900@r3dsolutions.com> <50912ADC.2020401@r3dsolutions.com> <50918716.3080305@r3dsolutions.com> <5092833F.4070609@stoneleaf.us> <50925DE6.7020100@r3dsolutions.com> <50932c9f$0$29967$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50938AE7.4060705@r3dsolutions.com> Hi Ian, I apologize for trying your patience with the badly written code example. All objects were meant to be ThirdParty(), the demo was only to show how a slice() filter could have been applied for the reasons PEP357 made index() to exist. eg: because numpy items passed to __getitems__ via slice syntax [::] were illegal values. PEP 357 is the one who specifically mentioned Numpy types -- which is the only reason I used the name in the example; I could have just as well used a string. I am fully aware of what numpy does -- I have used it; modified the fortran interfaces underneath, etc. The index() method, however, affects *all* list objects in Python, not just Numpy's -- correct? I'll write a working piece of code tomorrow to demonstrate the filter very clearly rather than a skeleton, and test it before posting. From naruto0.1 at live.cn Fri Nov 2 04:58:17 2012 From: naruto0.1 at live.cn (jack) Date: Fri, 2 Nov 2012 16:58:17 +0800 Subject: pythonic way In-Reply-To: References: Message-ID: thanks,but I don't think enumerate() is my want Have some ways to operate the reference of element,not a copy when I tried to traverse a list? I'm so sorry about my poor English, hope you don't mind it. On 2012/11/2 15:56, Chris Angelico wrote: > On Fri, Nov 2, 2012 at 6:14 PM, jack wrote: >> Sometimes, I need to alter the element as traverse a list like this (it's a >> sample): >> c = range(10) >> i = 0 >> for ele in c: >> # do something >> # branch: >> c[i] = # value >> i += 1 >> >> How to be pythonic? > Check out the enumerate() function. > > ChrisA From robert.kern at gmail.com Fri Nov 2 05:05:33 2012 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 02 Nov 2012 09:05:33 +0000 Subject: Organisation of python classes and their methods In-Reply-To: References: Message-ID: On 11/2/12 8:20 AM, Martin Hewitson wrote: > > On 2, Nov, 2012, at 09:00 AM, Peter Otten <__peter__ at web.de> wrote: > >> Martin Hewitson wrote: >> >>> Dear list, >>> >>> I'm relatively new to Python and have googled and googled but haven't >>> found a reasonable answer to this question, so I thought I'd ask it here. >>> >>> I'm beginning a large Python project which contains many packages, modules >>> and classes. The organisation of those is clear to me. >>> >>> Now, the classes can contain many methods (100s of data analysis methods) >>> which operate on instances of the class they belong to. These methods can >>> be long and complex. So if I put these methods all in the module file >>> inside the class, the file will get insanely long. Reading on google, the >>> answer is usually "refactor", but that really doesn't make sense here. >>> It's just that the methods are many, and each method can be a long piece >>> of code. So, is there a way to put these methods in their own files and >>> have them 'included' in the class somehow? I read a little about mixins >>> but all the solutions looked very hacky. Is there an official python way >>> to do this? I don't like having source files with 100's of lines of code >>> in, let alone 1000's. >> >> You googled, found the right answer ("refactor"), didn't like it and are now >> looking to cure the symptoms of the original problem? >> Seriously, a good editor can deal with a long source file, but a class with >> hundreds of methods will bring trouble to any old brain. > > Well, here we disagree. Suppose I have a class which encapsulates time-series data. Below is a list of the absolute minimum methods one would have to process that data. That's close to 100 already before even having any specialised methods for dealing with the data. Each of these methods will have maybe 20 lines of documentation. That's 2000 lines already. And what if someone wants to extend that class to add their own processing methods? It would a maintenance nightmare for them to edit the actual class file, which they would then have to repeat each time a new version of the 'official' class file is released. Do not make them methods of the time-series class. Make functions that take a time-series object. Then you can organize the functions in separate modules to your heart's content and import them all into a single convenience namespace. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco From rosuav at gmail.com Fri Nov 2 05:11:49 2012 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 2 Nov 2012 20:11:49 +1100 Subject: pythonic way In-Reply-To: References: Message-ID: On Fri, Nov 2, 2012 at 7:58 PM, jack wrote: > thanks,but I don't think enumerate() is my want > Have some ways to operate the reference of element,not a copy when I tried > to traverse a list? > > I'm so sorry about my poor English, hope you don't mind it. No probs, I'll be a little less vague and pointer-y and give you some example code. :) lst = ['foo', 'bar', 'quux', 'asdf', 'qwer', 'zxcv'] for idx, val in enumerate(lst): if val[1]=='w': lst[idx]='Replaced' print(lst) ['foo', 'bar', 'quux', 'asdf', 'Replaced', 'zxcv'] Does that explain it a bit better? You get the index and can then mutate the list using that index, thus replacing the original entry. ChrisA From rosuav at gmail.com Fri Nov 2 05:15:11 2012 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 2 Nov 2012 20:15:11 +1100 Subject: Organisation of python classes and their methods In-Reply-To: References: <7xa9v0wj2g.fsf@ruckus.brouhaha.com> Message-ID: On Fri, Nov 2, 2012 at 7:08 PM, Martin Hewitson wrote: > > On 2, Nov, 2012, at 08:38 AM, Paul Rubin wrote: > >> Martin Hewitson writes: >>> So, is there a way to put these methods in their own files and have >>> them 'included' in the class somehow? ... Is there an official python >>> way to do this? I don't like having source files with 100's of lines >>> of code in, let alone 1000's. >> >> That code sounds kind of smelly... why are there so many methods per >> class? > > Simply because there are many different ways to process the data. The class encapsulates the data, and the user can process the data in many ways. Of course, one could have classes which encapsulate the algorithms, as well as the data, but it also seems natural to me to have algorithms as methods which are part of the data class, so the user operates on the data using methods of that class. Are these really needing to be methods, or ought they to be module-level functions? Remember, Python code doesn't have to be organized into classes the way Java code is. ChrisA From __peter__ at web.de Fri Nov 2 05:17:16 2012 From: __peter__ at web.de (Peter Otten) Date: Fri, 02 Nov 2012 10:17:16 +0100 Subject: Organisation of python classes and their methods References: Message-ID: Martin Hewitson wrote: > > On 2, Nov, 2012, at 09:00 AM, Peter Otten <__peter__ at web.de> wrote: > >> Martin Hewitson wrote: >> >>> Dear list, >>> >>> I'm relatively new to Python and have googled and googled but haven't >>> found a reasonable answer to this question, so I thought I'd ask it >>> here. >>> >>> I'm beginning a large Python project which contains many packages, >>> modules and classes. The organisation of those is clear to me. >>> >>> Now, the classes can contain many methods (100s of data analysis >>> methods) which operate on instances of the class they belong to. These >>> methods can be long and complex. So if I put these methods all in the >>> module file inside the class, the file will get insanely long. Reading >>> on google, the answer is usually "refactor", but that really doesn't >>> make sense here. It's just that the methods are many, and each method >>> can be a long piece of code. So, is there a way to put these methods in >>> their own files and have them 'included' in the class somehow? I read a >>> little about mixins but all the solutions looked very hacky. Is there an >>> official python way to do this? I don't like having source files with >>> 100's of lines of code in, let alone 1000's. >> >> You googled, found the right answer ("refactor"), didn't like it and are >> now looking to cure the symptoms of the original problem? >> Seriously, a good editor can deal with a long source file, but a class >> with hundreds of methods will bring trouble to any old brain. > > Well, here we disagree. Suppose I have a class which encapsulates > time-series data. Below is a list of the absolute minimum methods one > would have to process that data. That's close to 100 already before even > having any specialised methods for dealing with the data. Each of these > methods will have maybe 20 lines of documentation. That's 2000 lines > already. And what if someone wants to extend that class to add their own > processing methods? from timeseries import TimeSeries class MyTimeSeries(TimeSeries): def average(): # specialised implementation > It would a maintenance nightmare for them to edit the > actual class file, which they would then have to repeat each time a new > version of the 'official' class file is released. Patient: Doctor, it hurts when I ... Doctor: Then don't do that. > So maybe some rethinking of this design is needed to handle this > 'limitation' of python. Perhaps grouping the processing algorithms into > methods of processing classes, then pass the data objects to these > methods. But somehow I don't like that. I have the feeling these methods > would end up peppered with things like: > > if this data type, do this > else if this data type, do this > else .... > > normally this would be solved by overloading methods in different data > subclasses. You could ask your TimeSeries for the appropriate Statistics subclass stats = ts.get_stats() print stats.mean() where get_stats() is a classmethod that returns an object that provides min(), max(), average() etc. Another approach are mix-in classes: class Stats: def min(): ... def average(): ... class SpecialStats(Stats): def min(): return 42 class TimeSeries(BaseTimeSeries, Stats): pass class SpecialTimeSeries(BaseTimeSeries, SpecialStats): pass > 'abs' [...] > 'zeropad' You are not perchance reimplementing numpy? > More thinking needed, clearly. That will never hurt. Well, almost: http://www.theonion.com/articles/beaver-overthinking-dam,1942/ :) From jamie at kode5.net Fri Nov 2 05:36:54 2012 From: jamie at kode5.net (Jamie Paul Griffin) Date: Fri, 2 Nov 2012 09:36:54 +0000 Subject: Obnoxious postings from Google Groups In-Reply-To: References: <5055a1a6$0$29981$c3e8da3$5496439d@news.astraweb.com> <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> Message-ID: <20121102093654.GH15963@kontrol.kode5.net> / rurpy at yahoo.com wrote on Thu 1.Nov'12 at 15:08:26 -0700 / > On 11/01/2012 03:55 AM, Jamie Paul Griffin wrote: > > Anybody serious about programming should be using a form of > > UNIX/Linux if you ask me. It's inconceivable that these systems > > should be avoided if you're serious about Software Engineering and > > Computer Science, etc. For UNIX there are loads of decent news > > reading software and mail user agents to learn and use. slrn is a > > good one and point it at gmane.org as someone else pointed out. I > > can't even imagine using a browser or Google Groups, etc. now. > Are you saying that this group is only for "serious" programmers? I don't see where my comments suggested that this group is only for serious programmers. I simply believe that the UNIX platform, in whatever form, is better placed and designed for all sorts of computing and engineering projects. The history of UNIX speaks for itself. Many Universities that offer respected and credible science based degree programmes, namely engineering and computing programmes, strongly encourage students to become competent with UNIX systems. Windows in my opinion is really for those who use the internet on a casual basis or in a commercial environment where its staff are not necessarily computer literate and therefore need a platform that they can use which doesn't require them to learn more complex techniques and protocols. But, having said that, I'm not against Windows at all. I use it frequently and enjoy using it most of the time. > "serious" is also a matter of opinion. I have some serious > programmer friends who maintain, in complete sincerity, that > serious programmers should not waste time on slow, script-kiddie > languages like Python, but should be developing their skills > with serious professional languages like Java, C#, etc. That is a narrow minded approach. different languages serve different purposes and it's down to the developer to use which language she needs to achieve what it is they've set out to do. Sometimes, basic shell scripts can be extremely powerful for certain tasks; other needs will require something different. I certainly wouldn't describe Python as a "script-kiddie" language. It's extremely powerful and modern. So there ;-P lol From jamie at kode5.net Fri Nov 2 05:57:47 2012 From: jamie at kode5.net (Jamie Paul Griffin) Date: Fri, 2 Nov 2012 09:57:47 +0000 Subject: datetime issue In-Reply-To: <8085a710-46c0-4d1a-b46e-f94eea0e8a0a@googlegroups.com> References: <8370412f-5176-45ff-87eb-72cbca5149d5@googlegroups.com> <5055a1a6$0$29981$c3e8da3$5496439d@news.astraweb.com> <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <8085a710-46c0-4d1a-b46e-f94eea0e8a0a@googlegroups.com> Message-ID: <20121102095747.GI15963@kontrol.kode5.net> / rurpy at yahoo.com wrote on Thu 1.Nov'12 at 15:00:48 -0700 / > ==== > * In Search dialog clicked on the Search in folder dropdown after > an earlier search and TB crashed (disappeared along with the > new message I was editing.) [3.0.1] > > * Search for japanese text in body no longer works (never finds > text even when it exists). [3.0.1] Used to work. > > * Each mail message displays useless header info that uses 25% > of the available vertical space. > > * When editing a reply message, typing a return at eol results in > two blank lines inserted. > > * When editing message, siometimes edit cursor disappears > tho editing still seems to work normally. > > * Requires voodoo to create hmtl new message when default is > plain text or visa versa. Have to hold shift key while clicking > "write" button. Shift while choosing "new", "reply", etc in > menu doesn't work. > > * Frequently, clinking on a usenet newsgroup results in TB freeze. > Only solution is to remove group and re-add later. (Problem > since 2.x days.) > > * Frequently after an apparently normal shutdown, attempt to restart > results in "TB is already running" error. > > * Some folders in the folder tree view are greyed out and not > selectable even though they have no visible different properties > than selectable ones. Don't know how they got that way. > > * Clicking on a folder in the folder tree pane results in a pop- > up dialog "this folder is not selectable" that has to be dismissed, > even though one is trying to get it's properties, or is selecting > it to create a subfolder. > > * Messages change from "read" to "unread" in folders at random when > folder is accessed, or even when it is not accessed. [gone in 3.0.3?] > > * Interminttently, when I click a usenet message to view it, > the contents pane remains blank. Contents of that message > remain blank foreever. [new with 3.0.1]. > > * When TB main window closed while editing an email, can no longer > save the email, even if the TB main window is reopened. > > * Counts of new messages in usenet groups are often wildly high. > > * When opening up a usenet server, status bar reports "no new > messages on server" even though TB then updates the groups > with the new messages that *are* on the server. [new in 3.0.1] > > * After upgrade to TB-3.0, opening a usenet server results not > only in the group being scanned for new messages in each group > (as before) but also the headers for all those new messages > being downloaded (slow on a dialup connection and a waste of > time if I don't intend to read anything in that group). > No obvious way to revert to original behaviour. > > * Even tho the number of unread messages listed beside a usenet > group in the folder pane is less than the download limit, I > sometimes get pop-up asking how many messages to download, > when I access the group. (side effect of above problem I think.) > > * Sometimes TB downloads two copies of each header. > > * When I compress folders, get a series of several dozen pop- > up messages (each requiring a click on "OK") telling me > that foplder "xx" is not selectable. > > * Copy-paste from TB to other app fails if TB is closed > before the paste -- the paste buffer appears empty [TB-3.0b4] > > * Copy paste fails by removing text (forgot if it is just the > copied text or other text) in the TB message when pasted > somewhere else. [TB-2?] > > * After upgrade to TB-3.0x, i no longer see a way to create a > new imap subfolder. Had to create it using MSOE. > > * After upgrade to TB-3.0x double clicking on attached .jpg image > no longer opens it -- only option is to save and open outside > of TB. phfft. > > * HTF do you change the font size for plain text message composition? > Prefs has a setting for html but... > > * search of body for multiple "anded" text never ends if one of > the search boxes is empty. > > * Search "stop" button doesn't stop search. > > * One group frequently, without any action on my part, read thousands > of old articles, showing them as unread, and when I choose one, responds > with "article expired" message. (tb-3.0.4) [ ... ] With a list of problems like that maybe the time spent on learning how to use a Usenet client or mua that is properly written would be worthwhile. Personally I haven't used the Google Groups interface, and most likely never will so I can't really comment on how it performs or how nice it is to use but if you're happy with it that's the main thing. But you can't deny that it does cause irritating problems for other people trying to read information sent from it. From frank at chagford.com Fri Nov 2 05:58:04 2012 From: frank at chagford.com (Frank Millman) Date: Fri, 02 Nov 2012 11:58:04 +0200 Subject: Organisation of python classes and their methods In-Reply-To: References: Message-ID: On 02/11/2012 08:16, Martin Hewitson wrote: > Dear list, > > I'm relatively new to Python and have googled and googled but haven't found a reasonable answer to this question, so I thought I'd ask it here. > > I'm beginning a large Python project which contains many packages, modules and classes. The organisation of those is clear to me. > > Now, the classes can contain many methods (100s of data analysis methods) which operate on instances of the class they belong to. These methods can be long and complex. So if I put these methods all in the module file inside the class, the file will get insanely long. Reading on google, the answer is usually "refactor", but that really doesn't make sense here. It's just that the methods are many, and each method can be a long piece of code. So, is there a way to put these methods in their own files and have them 'included' in the class somehow? I read a little about mixins but all the solutions looked very hacky. Is there an official python way to do this? I don't like having source files with 100's of lines of code in, let alone 1000's. > > Many thanks, > > Martin > I have read the other responses, so I may get some flak for encouraging bad habits. Nevertheless, I did have a similar requirement, and I found a solution that worked for me. My situation was not as extreme as yours. I had a class with a number of methods. Some of them were of an 'operational' nature - they represented the main functionality of the class, and could be called often. Some of them were of a 'setup' nature - they were optionally called when the object was instantiated, but would only be called once. I found that when I wanted to focus on one set of methods, the other set 'got in my way', and vice-versa. My solution was to take the 'setup' methods and put them in another file. This is how I did it. BEFORE ====== main.py - class MyClass: def setup1(self, ...): [...] def setup2(self, ...): [...] def func1(self, ...): [...] def func2(self, ...): [...] AFTER ===== setup.py - def setup1(self, ...): [...] def setup2(self, ...): [...] main.py - import setup class MyClass: setup1 = setup.setup1 setup2 = setup.setup2 def func1(self, ...): [...] def func2(self, ...): [...] Hope this gives you some ideas. Frank Millman From ulrich.eckhardt at dominolaser.com Fri Nov 2 06:18:08 2012 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Fri, 02 Nov 2012 11:18:08 +0100 Subject: Organisation of python classes and their methods In-Reply-To: References: <7xa9v0wj2g.fsf@ruckus.brouhaha.com> Message-ID: <08jcm9-to5.ln1@satorlaser.homedns.org> Am 02.11.2012 09:08, schrieb Martin Hewitson: > On 2, Nov, 2012, at 08:38 AM, Paul Rubin > wrote: >> Martin Hewitson writes: >>> So, is there a way to put these methods in their own files and >>> have them 'included' in the class somehow? ... Is there an >>> official python way to do this? I don't like having source files >>> with 100's of lines of code in, let alone 1000's. >> >> That code sounds kind of smelly... why are there so many methods >> per class? > > Simply because there are many different ways to process the data. The > class encapsulates the data, and the user can process the data in > many ways. Of course, one could have classes which encapsulate the > algorithms, as well as the data, but it also seems natural to me to > have algorithms as methods which are part of the data class, so the > user operates on the data using methods of that class. This is largely a matter of taste and a question of circumstances, but I'd like to point out here that your "natural" is not universal. If you take a different approach, namely that a class should encapsulate in order to maintain its internal consistency but otherwise be as small as possible, then algorithms operating on some data are definitely not part of that data. The advantage is that the data class gets smaller, and in the algorithms you don't risk ruining the internal integrity of the used data. Further, encapsulating algorithms into classes is also not natural. Algorithms are often expressed much better as functions. Shoe-horning things into a class in the name of OOP is IMHO misguided. Concerning mixins, you can put them into separate modules[1]. If it is clearly documented that class FooColourMixin handles the colour-related stuff for class Foo, and reversely that class Foo inherits FooShapeMixin and FooColourMixin that provide handling of shape and colour, then that is fine. It allows you to not only encapsulate things inside class Foo but to partition things inside Foo. Note that mixins are easier to write than in C++. If the mixin needs access to the derived class' function bar(), it just calls self.bar(). There is no type-casting or other magic involved. The same applies to data attributes (non-function attributes), basically all attributes are "virtual". The compile-time, static type checking of e.g. C++ doesn't exist. >> Python lets you inject new methods into existing classes (this is >> sometimes called duck punching) but I don't recommend doing this. > > Is there not a way just to declare the method in the class and put > the actual implementation in another file on the python path so that > it's picked up a run time? To answer your question, no, not directly. Neither is there a separation like in C++ between interface and implementation, nor is there something like in C# with partial classes. C++ interface/implementation separation is roughly provided by abstract base classes. C# partial classes are most closely emulated with mixins. That said, modifying classes is neither magic nor is it uncommon: class foo: pass import algo_x foo.algo = algo_x.function Classes are not immutable, you can add and remove things just like you can do with objects. BTW: If you told us which language(s) you have a background in, it could be easier to help you with identifying the idioms in that language that turn into misconceptions when applied to Python. Greetings! Uli [1] Actually, modules themselves provide the kind of separation that I think you are after. Don't always think "class" if it comes to encapsulation and modularization! From steve+comp.lang.python at pearwood.info Fri Nov 2 06:20:36 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Nov 2012 10:20:36 GMT Subject: Organisation of python classes and their methods References: <7xa9v0wj2g.fsf@ruckus.brouhaha.com> Message-ID: <50939e73$0$29967$c3e8da3$5496439d@news.astraweb.com> On Fri, 02 Nov 2012 08:40:06 +0000, Mark Lawrence wrote: > On 02/11/2012 08:08, Martin Hewitson wrote: >> >> Even if one takes reasonable numbers: 20 methods, each method has 20 >> lines of documentation, then we immediately have 400 lines in the file >> before writing a line of code. It would seem much more natural to me to >> have these methods in their own file, grouped nicely in >> sub-directories. But it seems this is not the python way. Sigh. >> >> Thanks for your thoughts, >> >> Martin >> >> > 20 lines of documentation per method? As far as I'm concerned that's > not a smell, that's a stink. Depends on the method. For some, 20 lines is 18 lines too many. For others, that's 80 lines too few. -- Steven From __peter__ at web.de Fri Nov 2 06:21:58 2012 From: __peter__ at web.de (Peter Otten) Date: Fri, 02 Nov 2012 11:21:58 +0100 Subject: Organisation of python classes and their methods References: <7xa9v0wj2g.fsf@ruckus.brouhaha.com> Message-ID: Martin Hewitson wrote: > On 2, Nov, 2012, at 09:40 AM, Mark Lawrence > wrote: >> 20 lines of documentation per method? As far as I'm concerned that's not >> a smell, that's a stink. > > Wow, I don't think I've ever been criticised before for writing too much > documentation :) > > I guess we have different end users. This is not a set of classes for > other developers to use: it's a set of classes which creates a data > analysis environment for scientists to use. They are not programmers, and > expect the algorithms to be documented in detail. While I would never discourage thorough documentation you may be better off with smaller docstrings and the details in an external document. Python projects typically use rst-files processed by sphinx. http://pypi.python.org/pypi/Sphinx/ From robert.kern at gmail.com Fri Nov 2 06:48:50 2012 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 02 Nov 2012 10:48:50 +0000 Subject: Negative array indicies and slice() In-Reply-To: <50938AE7.4060705@r3dsolutions.com> References: <509053F2.6020900@r3dsolutions.com> <50912ADC.2020401@r3dsolutions.com> <50918716.3080305@r3dsolutions.com> <5092833F.4070609@stoneleaf.us> <50925DE6.7020100@r3dsolutions.com> <50932c9f$0$29967$c3e8da3$5496439d@news.astraweb.com> <50938AE7.4060705@r3dsolutions.com> Message-ID: On 11/2/12 8:57 AM, Andrew Robinson wrote: > Hi Ian, > > I apologize for trying your patience with the badly written code example. All > objects were meant to be ThirdParty(), the demo was only to show how a slice() > filter could have been applied for the reasons PEP357 made index() to exist. > eg: because numpy items passed to __getitems__ via slice syntax [::] were > illegal values. > PEP 357 is the one who specifically mentioned Numpy types -- which is the only > reason I used the name in the example; I could have just as well used a string. > > I am fully aware of what numpy does -- I have used it; modified the fortran > interfaces underneath, etc. > > The index() method, however, affects *all* list objects in Python, not just > Numpy's -- correct? Please forget that PEP 357 mentions slices at all. The motivation for the __index__() method (not index()) goes far beyond slices. I'm not really sure why they are given such a prominent place in the PEP. Let me try to lay out the motivation more clearly. numpy has objects that represent integers but cannot be subclasses of the Python int or long objects because their internal representations are different. These are the width-specific types: uint8, int16, int64, etc. Before __index__() was introduced, all indexing operations in the builtin Python sequence types strictly checked for int or long objects and rejected other objects. We wanted to provide a generic method that third party types could implement to say, "Yes, I really am an integer, here is my value in a canonical representation you can understand." We could not use __int__() for this purpose because it has additional semantics, namely conversion from not-integers to integers. This is why floats are mentioned; they do not generally represent integers but they do define an __int__() method for their conversion to ints via the floor() function. Generally, they should be rejected as indices. With the __index__() method, we have a solution: int16 and the rest get __index__() methods and float doesn't. This is used where an integer index or offset is needed, not just in slices. List indices, file.seek(), mmap.mmap(), etc. The change to use PyIndex_Check() instead of PyInt_Check() was not very difficult or extensive. Even if you were to change the slicing API for your other reasons, __index__() would still be needed. -- 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 breamoreboy at yahoo.co.uk Fri Nov 2 06:48:54 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 02 Nov 2012 10:48:54 +0000 Subject: Organisation of python classes and their methods In-Reply-To: References: <7xa9v0wj2g.fsf@ruckus.brouhaha.com> Message-ID: On 02/11/2012 08:45, Martin Hewitson wrote: > > On 2, Nov, 2012, at 09:40 AM, Mark Lawrence wrote: > >> On 02/11/2012 08:08, Martin Hewitson wrote: >>> >>> Even if one takes reasonable numbers: 20 methods, each method has 20 lines of documentation, then we immediately have 400 lines in the file before writing a line of code. It would seem much more natural to me to have these methods in their own file, grouped nicely in sub-directories. But it seems this is not the python way. Sigh. >>> >>> Thanks for your thoughts, >>> >>> Martin >>> >> >> 20 lines of documentation per method? As far as I'm concerned that's not a smell, that's a stink. > > Wow, I don't think I've ever been criticised before for writing too much documentation :) > > I guess we have different end users. This is not a set of classes for other developers to use: it's a set of classes which creates a data analysis environment for scientists to use. They are not programmers, and expect the algorithms to be documented in detail. > > Martin > You've completely missed the point. 99% of the time if you can't write down what a method does in at most half a dozen lines, the method is screaming out to be refactored. Rightly or wrongly you've already rejected that option, although I suspect that rightly is nearer the mark in this case on the grounds that practicality beats purity. -- Cheers. Mark Lawrence. From ulrich.eckhardt at dominolaser.com Fri Nov 2 06:49:10 2012 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Fri, 02 Nov 2012 11:49:10 +0100 Subject: Organisation of python classes and their methods In-Reply-To: References: Message-ID: <62lcm9-4t5.ln1@satorlaser.homedns.org> Am 02.11.2012 09:20, schrieb Martin Hewitson: > Well, here we disagree. Suppose I have a class which encapsulates > time-series data. Below is a list of the absolute minimum methods one > would have to process that data. [...] > 'abs' 'acos' 'asin' 'atan' 'atan2' 'average' 'cohere' 'conv' 'corr' > 'cos' 'cov' 'cpsd' 'detrend' 'dft' 'diff' 'downsample' 'exp' > 'export' 'fft' 'fftfilt' 'filter' 'filtfilt' 'find' 'heterodyne' > 'hist' 'imag' 'integrate' 'interp' 'join' 'le' 'lincom' 'ln' 'load' > 'log' 'log10' 'lscov' 'max' 'mean' 'median' 'min' 'minus' 'mode' > 'mpower' 'mrdivide' 'mtimes' 'ne' 'norm' 'or' 'plot' 'plus' > 'polyfit' 'power' 'psd' 'rdivide' 'real' 'resample' 'rms' 'round' > 'save' 'scale' 'search' 'select' 'sin' 'smoother' 'sort' > 'spectrogram' 'split' 'sqrt' 'std' 'sum' 'sumjoin' 'svd' 'tan' 'tfe' > 'timeaverage' 'times' 'timeshift' 'transpose' 'uminus' 'upsample' > 'zeropad' Just as a suggestion, you can separate these into categories: 1. Things that modify the data, yielding a different (although derived) data set, e.g. import/load, split, join, plus, minus, zeropad. 2. Things that operate on the data without modifying it, e.g. export/save, average, find, plot, integrate. The latter can easily be removed from the class. Since they don't touch the content, they can't invalidate internals and can't break encapsulation. For the former, providing general means to construct or modify the data (like e.g. adding records or joining sequences) is also all that needs to remain inside the class to ensure internal consistency, everything else can be built on top of these using external functions. Uli From nachik8kamat at gmail.com Fri Nov 2 06:49:51 2012 From: nachik8kamat at gmail.com (nachiket) Date: Fri, 2 Nov 2012 03:49:51 -0700 (PDT) Subject: word sense disambiguation Message-ID: hello, do you know how to perform word sense disambiguation. Input:- sentence Output:- Sense tagged words. From robert.kern at gmail.com Fri Nov 2 06:55:06 2012 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 02 Nov 2012 10:55:06 +0000 Subject: Organisation of python classes and their methods In-Reply-To: References: <7xa9v0wj2g.fsf@ruckus.brouhaha.com> Message-ID: On 11/2/12 10:21 AM, Peter Otten wrote: > Martin Hewitson wrote: > >> On 2, Nov, 2012, at 09:40 AM, Mark Lawrence >> wrote: > >>> 20 lines of documentation per method? As far as I'm concerned that's not >>> a smell, that's a stink. >> >> Wow, I don't think I've ever been criticised before for writing too much >> documentation :) >> >> I guess we have different end users. This is not a set of classes for >> other developers to use: it's a set of classes which creates a data >> analysis environment for scientists to use. They are not programmers, and >> expect the algorithms to be documented in detail. > > While I would never discourage thorough documentation you may be better off > with smaller docstrings and the details in an external document. Python > projects typically use rst-files processed by sphinx. > > http://pypi.python.org/pypi/Sphinx/ In the science/math community, we tend to build the Sphinx API reference from the thorough, authoritative docstrings. We like having complete docstrings because we are frequently at the interactive prompt. We tend to have broad APIs, so having a single source of documentation and not repeating ourselves is important. http://docs.scipy.org/doc/numpy/reference/index.html http://docs.scipy.org/doc/scipy/reference/index.html http://www.sagemath.org/doc/reference/ http://docs.sympy.org/0.7.2/modules/index.html http://scikit-learn.org/stable/modules/classes.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 robert.kern at gmail.com Fri Nov 2 07:07:54 2012 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 02 Nov 2012 11:07:54 +0000 Subject: Organisation of python classes and their methods In-Reply-To: References: <7xa9v0wj2g.fsf@ruckus.brouhaha.com> Message-ID: On 11/2/12 10:48 AM, Mark Lawrence wrote: > On 02/11/2012 08:45, Martin Hewitson wrote: >> >> On 2, Nov, 2012, at 09:40 AM, Mark Lawrence wrote: >> >>> On 02/11/2012 08:08, Martin Hewitson wrote: >>>> >>>> Even if one takes reasonable numbers: 20 methods, each method has 20 lines >>>> of documentation, then we immediately have 400 lines in the file before >>>> writing a line of code. It would seem much more natural to me to have these >>>> methods in their own file, grouped nicely in sub-directories. But it seems >>>> this is not the python way. Sigh. >>>> >>>> Thanks for your thoughts, >>>> >>>> Martin >>>> >>> >>> 20 lines of documentation per method? As far as I'm concerned that's not a >>> smell, that's a stink. >> >> Wow, I don't think I've ever been criticised before for writing too much >> documentation :) >> >> I guess we have different end users. This is not a set of classes for other >> developers to use: it's a set of classes which creates a data analysis >> environment for scientists to use. They are not programmers, and expect the >> algorithms to be documented in detail. >> >> Martin > > You've completely missed the point. 99% of the time if you can't write down > what a method does in at most half a dozen lines, the method is screaming out to > be refactored. Rightly or wrongly you've already rejected that option, although > I suspect that rightly is nearer the mark in this case on the grounds that > practicality beats purity. You've completely missed the context. These are not really complicated methods doing lots of things all at once such that can be refactored to simpler methods. The docstrings are not just glorified comments for other developers reading the source code. They are the online documentation for non-programmer end-users who are using the interactive prompt as an interactive data analysis environment. Frequently, they not only have to describe what it's doing, but also introduce the whole concept of what it's doing, why you would want to do such a thing, and provide examples of its use. That's why they are so long. For example: http://docs.scipy.org/doc/numpy/reference/generated/numpy.fft.fft.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 robert.kern at gmail.com Fri Nov 2 07:08:47 2012 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 02 Nov 2012 11:08:47 +0000 Subject: word sense disambiguation In-Reply-To: References: Message-ID: On 11/2/12 10:49 AM, nachiket wrote: > hello, > > do you know how to perform word sense disambiguation. > > Input:- sentence > Output:- Sense tagged words. You've asked this already, and I have pointed you to NLTK. http://nltk.org/ http://nltk.org/book/ch06.html http://nltk.org/api/nltk.classify.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 hexusnexus at gmail.com Fri Nov 2 07:20:20 2012 From: hexusnexus at gmail.com (Jason Benjamin) Date: Fri, 02 Nov 2012 04:20:20 -0700 Subject: Proper place for everything Message-ID: Anybody know of the appropriate place to troll and flame about various Python related issues? I'm kind of mad about some Python stuff and I need a place to vent where people may or may not listen, but at at least respond. Thought this would be a strange question, but I might as well start somewhere. From awilliam at whitemice.org Fri Nov 2 07:22:50 2012 From: awilliam at whitemice.org (Adam Tauno Williams) Date: Fri, 02 Nov 2012 07:22:50 -0400 Subject: date and time comparison how to In-Reply-To: <50e576ea-2ebe-4340-954e-6c23b5885680@googlegroups.com> References: <50e576ea-2ebe-4340-954e-6c23b5885680@googlegroups.com> Message-ID: <1351855370.2490.39.camel@workstation.wmmi.net> On Mon, 2012-10-29 at 16:13 -0700, noydb wrote: > All, > I need help with a date and time comparison. > Say a user enters a date-n-time and a file on disk. I want to compare > the date and time of the file to the entered date-n-time; if the file > is newer than the entered date-n-time, add the file to a list to > process. > How best to do? I have looked at the datetime module, tried a few > things, no luck. > Is os.stat a part of it? Tried, not sure of the output, the > st_mtime/st_ctime doesnt jive with the file's correct date and > time. ?? > Any help would be appreciated! Date and time is much more complicated then people guess at first. What do you mean by "st_mtime/st_ctime doesnt jive with the file's correct date"? Is it off my some offset, or does it completely not match? From robert.kern at gmail.com Fri Nov 2 07:38:34 2012 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 02 Nov 2012 11:38:34 +0000 Subject: Proper place for everything In-Reply-To: References: Message-ID: On 11/2/12 11:20 AM, Jason Benjamin wrote: > Anybody know of the appropriate place to troll and flame about various Python > related issues? I'm kind of mad about some Python stuff and I need a place to > vent where people may or may not listen, but at at least respond. Thought this > would be a strange question, but I might as well start somewhere. There are plenty of good, free blog hosting options. -- 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 mail at timgolden.me.uk Fri Nov 2 07:42:59 2012 From: mail at timgolden.me.uk (Tim Golden) Date: Fri, 02 Nov 2012 11:42:59 +0000 Subject: Proper place for everything In-Reply-To: References: Message-ID: <5093B1C3.3000108@timgolden.me.uk> On 02/11/2012 11:20, Jason Benjamin wrote: > Anybody know of the appropriate place to troll and flame about various > Python related issues? I'm kind of mad about some Python stuff and I > need a place to vent where people may or may not listen, but at at least > respond. Thought this would be a strange question, but I might as well > start somewhere. A Blog seems the obvious choice. If you don't already have one there are certainly freely-hosted ones available: blogspot, wordpress, etc. TJG From alister.ware at ntlworld.com Fri Nov 2 07:47:15 2012 From: alister.ware at ntlworld.com (Alister) Date: Fri, 02 Nov 2012 11:47:15 GMT Subject: Proper place for everything References: Message-ID: <7pOks.130794$g62.29128@fx06.am4> On Fri, 02 Nov 2012 04:20:20 -0700, Jason Benjamin wrote: > Anybody know of the appropriate place to troll and flame about various > Python related issues? I'm kind of mad about some Python stuff and I > need a place to vent where people may or may not listen, but at at least > respond. Thought this would be a strange question, but I might as well > start somewhere. Trolls & flames are probably best sent to /dev/null -- The meta-Turing test counts a thing as intelligent if it seeks to devise and apply Turing tests to objects of its own creation. -- Lew Mammel, Jr. From ulrich.eckhardt at dominolaser.com Fri Nov 2 08:26:33 2012 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Fri, 02 Nov 2012 13:26:33 +0100 Subject: Proper place for everything In-Reply-To: References: Message-ID: Am 02.11.2012 12:20, schrieb Jason Benjamin: > Anybody know of the appropriate place to troll and flame about various > Python related issues? I'm kind of mad about some Python stuff and I > need a place to vent where people may or may not listen, but at at least > respond. Thought this would be a strange question, but I might as well > start somewhere. Depending on the kind of responses you want I would try alt.comp.zoo.reptiles or maybe a PHP mailinglist. Alternatively, if you are willing to invest some real money, I would suggest a good Islay single malt or a gym membership. If health, money and time are of no importance to you, I heard of these things called girlfriends or boyfriends, these could be completely overhyped rumours though. Uli From hexusnexus at gmail.com Fri Nov 2 09:49:18 2012 From: hexusnexus at gmail.com (Jason Benjamin) Date: Fri, 02 Nov 2012 06:49:18 -0700 Subject: Proper place for everything In-Reply-To: References: Message-ID: Yeah, alright. I've just found that if you mention anything about a library that has well established competitors, the post will tend to get ignored here. On 11/02/2012 04:38 AM, Robert Kern wrote: > On 11/2/12 11:20 AM, Jason Benjamin wrote: >> Anybody know of the appropriate place to troll and flame about various >> Python >> related issues? I'm kind of mad about some Python stuff and I need a >> place to >> vent where people may or may not listen, but at at least respond. >> Thought this >> would be a strange question, but I might as well start somewhere. > > There are plenty of good, free blog hosting options. > From schesis at gmail.com Fri Nov 2 10:09:48 2012 From: schesis at gmail.com (Zero Piraeus) Date: Fri, 2 Nov 2012 10:09:48 -0400 Subject: Proper place for everything In-Reply-To: References: Message-ID: : On 2 November 2012 07:20, Jason Benjamin wrote: > Anybody know of the appropriate place to troll and flame about various > Python related issues? I don't know about appropriate, but there'd be a certain amount of poetic justice in using one of the alt.conspiracy.* or alt.religion.* newsgroups ... -[]z. From invalid at invalid.invalid Fri Nov 2 10:11:12 2012 From: invalid at invalid.invalid (Grant Edwards) Date: Fri, 2 Nov 2012 14:11:12 +0000 (UTC) Subject: Obnoxious postings from Google Groups References: <8370412f-5176-45ff-87eb-72cbca5149d5@googlegroups.com> <5055a1a6$0$29981$c3e8da3$5496439d@news.astraweb.com> <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> <50932111$0$29967$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2012-11-02, Steven D'Aprano wrote: > On Fri, 02 Nov 2012 10:32:08 +1100, Chris Angelico wrote: > >> And there are probably still a few around who maintain that Java, C#, >> and even C are too modern, and that serious programmers use FORTRAN or >> COBOL. > > Huh. If you're messing about with ancient[1] languages like Java, C# and > especially C, you're not a real programmer. Real programmers use modern, > advanced languages like D, Erlang, Go or Haskell. Don't forget Smalltalk! Old, but always modern and advanced... -- Grant Edwards grant.b.edwards Yow! ! Everybody out of at the GENETIC POOL! gmail.com From mail at timgolden.me.uk Fri Nov 2 10:29:49 2012 From: mail at timgolden.me.uk (Tim Golden) Date: Fri, 02 Nov 2012 14:29:49 +0000 Subject: Proper place for everything In-Reply-To: References: Message-ID: <5093D8DD.6050302@timgolden.me.uk> On 02/11/2012 13:49, Jason Benjamin wrote: > Yeah, alright. I've just found that if you mention anything about a > library that has well established competitors, the post will tend to get > ignored here. I'm not sure exactly what you're referring to. (Perhaps you can link to an existing post or discussion). Without hearing a tone of voice, I'm not sure whether with "... will tend to get ignored here" you're suggesting a conspiracy of silence or merely a general lack of interest. The people who read and post here are the people who read and post here. And they comment according to whatever interests them or whatever opinions they hold. That people choose to respond to a post about Library A and not to one about Library B should not be construed as representing any kind of behind-the-scenes action encouraging or suppressing interest. It simply means that the people who felt like it commented on Library A and not on Library B. Or have I misunderstood the tenor of your comment? TJG From martinhewitson at mac.com Fri Nov 2 10:47:26 2012 From: martinhewitson at mac.com (Martin Hewitson) Date: Fri, 02 Nov 2012 15:47:26 +0100 Subject: Organisation of python classes and their methods In-Reply-To: <62lcm9-4t5.ln1@satorlaser.homedns.org> References: <62lcm9-4t5.ln1@satorlaser.homedns.org> Message-ID: <33317826-00CF-4965-A6AC-E62DE8D7FA2D@mac.com> On 2, Nov, 2012, at 11:49 AM, Ulrich Eckhardt wrote: > Am 02.11.2012 09:20, schrieb Martin Hewitson: >> Well, here we disagree. Suppose I have a class which encapsulates >> time-series data. Below is a list of the absolute minimum methods one >> would have to process that data. > [...] > > 'abs' 'acos' 'asin' 'atan' 'atan2' 'average' 'cohere' 'conv' 'corr' > > 'cos' 'cov' 'cpsd' 'detrend' 'dft' 'diff' 'downsample' 'exp' > > 'export' 'fft' 'fftfilt' 'filter' 'filtfilt' 'find' 'heterodyne' > > 'hist' 'imag' 'integrate' 'interp' 'join' 'le' 'lincom' 'ln' 'load' > > 'log' 'log10' 'lscov' 'max' 'mean' 'median' 'min' 'minus' 'mode' > > 'mpower' 'mrdivide' 'mtimes' 'ne' 'norm' 'or' 'plot' 'plus' > > 'polyfit' 'power' 'psd' 'rdivide' 'real' 'resample' 'rms' 'round' > > 'save' 'scale' 'search' 'select' 'sin' 'smoother' 'sort' > > 'spectrogram' 'split' 'sqrt' 'std' 'sum' 'sumjoin' 'svd' 'tan' 'tfe' > > 'timeaverage' 'times' 'timeshift' 'transpose' 'uminus' 'upsample' > > 'zeropad' > > > Just as a suggestion, you can separate these into categories: > > 1. Things that modify the data, yielding a different (although derived) data set, e.g. import/load, split, join, plus, minus, zeropad. > 2. Things that operate on the data without modifying it, e.g. export/save, average, find, plot, integrate. > > The latter can easily be removed from the class. Since they don't touch the content, they can't invalidate internals and can't break encapsulation. > > For the former, providing general means to construct or modify the data (like e.g. adding records or joining sequences) is also all that needs to remain inside the class to ensure internal consistency, everything else can be built on top of these using external functions. > Thank you all so much for your thoughts and suggestions. I need to absorb all of this and decide on the best approach in this case. Thanks again, Martin > > Uli > > > > -- > http://mail.python.org/mailman/listinfo/python-list From martinhewitson at me.com Fri Nov 2 10:49:16 2012 From: martinhewitson at me.com (Martin Hewitson) Date: Fri, 02 Nov 2012 15:49:16 +0100 Subject: Organisation of python classes and their methods In-Reply-To: <08jcm9-to5.ln1@satorlaser.homedns.org> References: <7xa9v0wj2g.fsf@ruckus.brouhaha.com> <08jcm9-to5.ln1@satorlaser.homedns.org> Message-ID: <746E1174-9BF2-4526-ADE4-6DE5C467696A@me.com> > > > BTW: If you told us which language(s) you have a background in, it could be easier to help you with identifying the idioms in that language that turn into misconceptions when applied to Python. I'm considering porting some MATLAB code to python to move away from commercial software. Python seemed like the right choice simply because of the wonderful numpy, scipy and matplotlib. So my project will build on these packages to provide some additional state and functionality. Cheers, Martin > > Greetings! > > Uli > > [1] Actually, modules themselves provide the kind of separation that I think you are after. Don't always think "class" if it comes to encapsulation and modularization! > -- > http://mail.python.org/mailman/listinfo/python-list From nobody at nowhere.com Fri Nov 2 11:46:04 2012 From: nobody at nowhere.com (Nobody) Date: Fri, 02 Nov 2012 15:46:04 +0000 Subject: Avoiding defunct processes References: Message-ID: On Thu, 01 Nov 2012 19:16:17 -0700, Richard wrote: > I create child processes with subprocess.Popen(). > Then I either wait for them to finish or kill them. > Either way these processes end up as defunct until the parent process > completes: > $ ps e > 6851 pts/5 Z+ 1:29 [python] You need to either call the .wait() method, or keep calling the .poll() method until the .returncode attribute is not None. From xs.nepaul at gmail.com Fri Nov 2 11:50:30 2012 From: xs.nepaul at gmail.com (nepaul) Date: Fri, 2 Nov 2012 08:50:30 -0700 (PDT) Subject: What the diferences : web.py Tornado Twisted ?! Message-ID: What the diferences : web.py Tornado Twisted ?! From michi.schwarz at gmail.com Fri Nov 2 12:11:28 2012 From: michi.schwarz at gmail.com (Michael Schwarz) Date: Fri, 2 Nov 2012 17:11:28 +0100 Subject: How to improve the usability of nested packages Message-ID: I need some guidance on how to structure a large library with lots of packages and sub-packages. Using the library should be as effortless as possible. It's your average beast of a catch-all for sharing code across applications at our company. Let's call it "the_library". In my attempt to structure the library, I was following two principles: - The complete library is contained in a single package. This is to avoid polluting the top-level namespace. - Only modules and sub-packages directly under the top-level package should be imported directly. This means that any class or function in the library is accessed using the same qualified name everywhere inside the library or the application. This makes moving code around easier. Following this, using a module from the library is pretty straight-forward. A typical file in the application code could start with: from the_library import sip, rtp, sdp This works from any module or script in the library or application. Then I decided to split the "sip" module into smaller modules, e.g. "message", "transaction", "dialog", all contained in a package named "sip". Ideally, an application would still import the sip package using the import above and then, for example, access the "DialogID" class using "sip.dialog.DialogID". Currently this is only possible when also explicitly importing the "dialog" module: from the_library import sip import the_library.sip.dialog This is ugly and seems unnecessary to me as, for example, having all the modules in the "sip" package available using a single import would not pollute the local namespace. So I tried to enable this by importing all the modules in the "sip" package from the package's "__init__.py": from . import message, transaction, dialog ? which doesn't work. Some of the modules reference other modules in the same package. I'm not talking about cyclic references, but, for example, the "dialog" module uses the "transaction" module. The problem is that the "dialog" module uses the same mechanism shown above to import the other modules from it's package. This means that modules and packages are imported in this order: - Application code executes "from the_library import sip" - the_library/__init__.py is executed. No imports here. - the_library/sip/__init__.py executes "from . import [...], dialog" - the_library/sip/dialog.py executes "from the_library import sip" During the last import a problem arises: The module object for the package "the_library" does not yet have a "sip" member (as it is still executing the import) and so the import fails. It is still possible to import the "transaction" module directly from the "dialog" module using: from . import transaction But this would make the "transaction" module available under a different qualified name as anywhere else (where it's accessed using "sip.transaction"). What path would you take to circumvent this problem? Would you break the rule that any module should be accessed using the same way, no matter from where it is accessed, or would you maybe structure the library entirely different? Thanks for any suggestions! Michael From sacharook at gmail.com Fri Nov 2 12:45:40 2012 From: sacharook at gmail.com (Sacha Rook) Date: Fri, 2 Nov 2012 16:45:40 +0000 Subject: python-forum In-Reply-To: References: Message-ID: Thanks for the update and the invite don't mind if I do. cheers sachlar On 2 November 2012 08:26, Chris Rebert wrote: > On Fri, Nov 2, 2012 at 1:19 AM, Sacha Rook wrote: > > Hi does anyone know where the python-form.org site has gone? > > Some googling suggests that it's under new management: > http://mcompute.co.uk/showthread.php?tid=2161 > > But comp.lang.python/python-list is better anyway [ ;-) ], and you're > already here, so why not stay a while? > > Cheers, > Chris > -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Fri Nov 2 13:02:05 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 02 Nov 2012 17:02:05 +0000 Subject: Organisation of python classes and their methods In-Reply-To: <746E1174-9BF2-4526-ADE4-6DE5C467696A@me.com> References: <7xa9v0wj2g.fsf@ruckus.brouhaha.com> <08jcm9-to5.ln1@satorlaser.homedns.org> <746E1174-9BF2-4526-ADE4-6DE5C467696A@me.com> Message-ID: On 02/11/2012 14:49, Martin Hewitson wrote: [Top posting fixed] > >> >> >> BTW: If you told us which language(s) you have a background in, it could be easier to help you with identifying the idioms in that language that turn into misconceptions when applied to Python. > >> >> Greetings! >> >> Uli >> >> [1] Actually, modules themselves provide the kind of separation that I think you are after. Don't always think "class" if it comes to encapsulation and modularization! >> -- >> http://mail.python.org/mailman/listinfo/python-list > > I'm considering porting some MATLAB code to python to move away from commercial software. Python seemed like the right choice simply because of the wonderful numpy, scipy and matplotlib. > > So my project will build on these packages to provide some additional state and functionality. > > Cheers, > > Martin > Just in case you're not aware there are separate mailing lists for numpy and matplotlib, presumably scipy as well, should you have specific questions. Further matplotlib is now at version 1.2rc3 with Python 3 support, yippee. Combine that with the recently released Python 3.3 and it should make one hell of a tool kit :) -- Cheers. Mark Lawrence. From no.email at nospam.invalid Fri Nov 2 13:24:56 2012 From: no.email at nospam.invalid (Paul Rubin) Date: Fri, 02 Nov 2012 10:24:56 -0700 Subject: Organisation of python classes and their methods References: Message-ID: <7xhap7dijb.fsf@ruckus.brouhaha.com> Martin Hewitson writes: > Well, here we disagree. Suppose I have a class which encapsulates > time-series data. Below is a list of the absolute minimum methods one > would have to process that data. ... > 'abs' > 'acos' > 'asin' > ... Ok, THERE is your problem. Why do you have separate implementations of all those functions? Does the abs of a time series simply mean the abs of each element of the series? In that case you want just ONE method, something like "map", which applies an arbitrary function to all elements of the series. Then for time series ts, instead of saying ts.abs(), you'd say ts.map(abs) where abs is the existing, built-in absolute value function. You could similarly say ts.map(acos) etc. That gets rid of almost all of those methods. From sacharook at gmail.com Fri Nov 2 13:25:09 2012 From: sacharook at gmail.com (Sacha Rook) Date: Fri, 2 Nov 2012 10:25:09 -0700 (PDT) Subject: csv read clean up and write out to csv Message-ID: <10133786-7f4c-4b19-8678-a50781ff453d@googlegroups.com> Hi I have a problem with a csv file from a supplier, so they export data to csv however the last column in the record is a description which is marked up with html. trying to automate the processing of this csv to upload elsewhere in a useable format. If i open the csv with csved it looks like all the records aren't escaped correctly as after a while i find html tags and text on the next line/record. If I 'openwith' excel the description stays on the correct line/record? I want to use python to read these records in and output a valid csv with the descriptions intact preferably without the html tags so a string of text formatted with newline/CR where appropriate. So far I have this but don't know where to go from here can someone help me? import csv infile = open('c:\data\input.csv', 'rb') outfile = open('c:\data\output.csv', 'wb') reader = csv.reader(infile) writer = csv.writer(outfile) for line in reader: print line writer.writerow(line) The input.csv is set out as follows; HEADER ROW 1st "FileDate","ProductID","Name","StandardPrice","DropshipPrice","SRP","Brand","Xline","InStock","Stock","Barcode","Weight","CategoryID","Category","SmallImage","LargeImage","Description" A COMPLETE RECORD LOOKS LIKE THIS WITH THE DESCRIPTION FIELD POPULATED SOME RECORDS DON'T HAVE THE DESCRIPTION FIELD POPULATED "2012-11-01T18:28:45.25+00:00","10198","(Venom) PS2 DVD Remote Control (Black)","3.7800","4.3500","12.9800","Venom","true","In Stock","1","5031300025009","200","1339","PC/Games_Console / Playstation / PS2 / Remote Controls","http://www.atssitecentre.co.uk/images/products/10000/10198.gif","http://www.atssitecentre.co.uk/images/products/10000/10198f.jpg","Never have to unplug your joypad / DVD user friendly / Works up to 30 feet from PS/2 / IR wireless technology." THIS IS AN EXAMPLE OF THE BAD RECORD CAUSING PROBLEMS, THE DESCRIPTION FIELD STARTS ""features:

AS YOU CAN SEE CONTAINS HTML BUT BECAUSE OF THIS FORMAT SUBSEQUENT HTML TAGS ARE ADDED AS NEW RECORDS ON SUBSEQUENT LINES. "2012-11-01T18:28:45.25+00:00","11116","3.5 inch Disk Drive Lock","2.9500","2.9500","9.9500","None","true","In Stock","3","077511994166","131","1332","PC/Games_Console / PC Security / General","http://www.atssitecentre.co.uk/images/products/11000/11116.gif","http://www.atssitecentre.co.uk/images/products/11000/11116f.jpg","features:

  • 3 1/2" FDD Lock.
  • Die casting housing and cylinder chrome plated.
  • Lock Cover : PBT + GF 15%. (PLASTIC)
  • 2 Keys supplied per lock.
" I know I am far from complete but don't know how to proceed :-) As I said I want to reconstruct a clean record either strip out the html tags or at least escape the records appropriately.. Thanks all From martinhewitson at mac.com Fri Nov 2 13:40:34 2012 From: martinhewitson at mac.com (Martin Hewitson) Date: Fri, 02 Nov 2012 18:40:34 +0100 Subject: Organisation of python classes and their methods In-Reply-To: <7xhap7dijb.fsf@ruckus.brouhaha.com> References: <7xhap7dijb.fsf@ruckus.brouhaha.com> Message-ID: <9585E961-8418-495F-AA7B-52BB76778BB3@mac.com> On 2, Nov, 2012, at 06:24 PM, Paul Rubin wrote: > Martin Hewitson writes: >> Well, here we disagree. Suppose I have a class which encapsulates >> time-series data. Below is a list of the absolute minimum methods one >> would have to process that data. ... >> 'abs' >> 'acos' >> 'asin' >> ... > > Ok, THERE is your problem. Why do you have separate implementations of > all those functions? Does the abs of a time series simply mean the abs > of each element of the series? In that case you want just ONE method, > something like "map", which applies an arbitrary function to all > elements of the series. Then for time series ts, instead of saying > ts.abs(), you'd say ts.map(abs) where abs is the existing, built-in > absolute value function. You could similarly say ts.map(acos) etc. > That gets rid of almost all of those methods. Well, because one of the features that the framework will have is to capture history steps (in a tree structure) so that each processing step the user does is tracked. So while methods such as abs(), cos(), etc will eventually just call a built-in method, there will be some house-keeping around them. All that said, as I've been trying to implement this structure, it turns out that in Python, this is more naturally achieved (I think) if each algorithm is implemented as a class, so that each algorithm can return its set of supported parameters for validation against the user inputs and, ultimately, for inclusion in a step in the history tree. Since most of that infrastructure will turn out to be boiler-plate code, it would make sense to have an algorithm base class, which all other algorithms (abs, cos, etc) will inherit from. Then I just need to get my head around the interplay between these algorithm classes and the data classes. Some more prototyping needed. Thanks for the info about map(); this will likely turn out to be very useful, if not at the user level, at least within the framework. Again, a main requirement is that the users should be able to work without knowing much about Python or programming in general; they just get this toolkit and, after minimal training should be able to do most of what they want in an intuitive way. Cheers, Martin > -- > http://mail.python.org/mailman/listinfo/python-list From ian.g.kelly at gmail.com Fri Nov 2 13:42:14 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 2 Nov 2012 11:42:14 -0600 Subject: Obnoxious postings from Google Groups In-Reply-To: <20121102093654.GH15963@kontrol.kode5.net> References: <5055a1a6$0$29981$c3e8da3$5496439d@news.astraweb.com> <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> <20121102093654.GH15963@kontrol.kode5.net> Message-ID: On Fri, Nov 2, 2012 at 3:36 AM, Jamie Paul Griffin wrote: > / rurpy at yahoo.com wrote on Thu 1.Nov'12 at 15:08:26 -0700 / > >> On 11/01/2012 03:55 AM, Jamie Paul Griffin wrote: >> > Anybody serious about programming should be using a form of >> > UNIX/Linux if you ask me. It's inconceivable that these systems >> > should be avoided if you're serious about Software Engineering and >> > Computer Science, etc. For UNIX there are loads of decent news >> > reading software and mail user agents to learn and use. slrn is a >> > good one and point it at gmane.org as someone else pointed out. I >> > can't even imagine using a browser or Google Groups, etc. now. > >> Are you saying that this group is only for "serious" programmers? > > I don't see where my comments suggested that this group is only for serious programmers. I simply believe that the UNIX platform, in whatever form, is better placed and designed for all sorts of computing and engineering projects. The history of UNIX speaks for itself. Many Universities that offer respected and credible science based degree programmes, namely engineering and computing programmes, strongly encourage students to become competent with UNIX systems. Windows in my opinion is really for those who use the internet on a casual basis or in a commercial environment where its staff are not necessarily computer literate and therefore need a platform that they can use which doesn't require them to learn more complex techniques and protocols. But, having said that, I'm not against Windows at all. I use it frequently and enjoy using it most of the time. I am comfortable with both Windows and Unix systems, and I do not find that either environment is particularly more effective for software engineering or helps me to be more productive than the other. My job has me developing Windows software, so I use Windows at work since at the very least I require it for testing and debugging. I could use virtualization to run Unix as well, and I have known some who do, but my philosophy is: why waste time dealing with two distinct environments where only one is required? Cheers, Ian From tjreedy at udel.edu Fri Nov 2 13:56:06 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 02 Nov 2012 13:56:06 -0400 Subject: How to improve the usability of nested packages In-Reply-To: References: Message-ID: nested package == subpackage > or would you maybe structure the library entirely different? Based on my limited experience with subpackages* plus reports on this list about problems, such as yours, I have concluded that subpackages are an attractive nuisance that are generally more trouble than they are worth. I suggest you consider sticking with your original flat (no subpackage) design. (But maybe someone knows better than me how to make subpackages work ;-). * In my current project, I started with all modules in subpackages but have since moved them into the package and deleted the subpackages. -- Terry Jan Reedy From hexusnexus at gmail.com Fri Nov 2 14:11:14 2012 From: hexusnexus at gmail.com (Jason Benjamin) Date: Fri, 02 Nov 2012 11:11:14 -0700 Subject: Proper place for everything In-Reply-To: References: Message-ID: It's a *really* old post, but it was back when I was first started learning Python when Python 3 wasn't out yet. It was a dumb question so I can see why people wouldn't have had interest in answering it, but I had been developing a game and wanted to find a recommendation on a GUI toolkit. Now that that's out, yes, I realize I could have searched the Internet for some ideas, but I tend to have tunnel vision when it comes to the Internet, much like modern teenagers seem to only realize they can communicate with Facebook. The incident may have been isolated, but I stereotype. On 11/02/2012 10:31 AM, Dennis Lee Bieber wrote: > On Fri, 02 Nov 2012 06:49:18 -0700, Jason Benjamin > declaimed the following in > gmane.comp.python.general: > >> Yeah, alright. I've just found that if you mention anything about a >> library that has well established competitors, the post will tend to get >> ignored here. >> > Interesting... The oldest post still in my news reader's cache is > October 17. > > Since that time, there are only two posts under your name in that > cache, both in this thread. You should maybe provide some link to such a > post so late-comers or others can find them... a message-ID could be > useful... > > I'll concede some posts may have been removed from the cache due to > filter actions (though for this group as found on gmane I only have > three active filters, and your name is not in any of them). > From stefan at epy.co.at Fri Nov 2 14:23:00 2012 From: stefan at epy.co.at (Stefan H. Holek) Date: Fri, 2 Nov 2012 19:23:00 +0100 Subject: How to improve the usability of nested packages In-Reply-To: References: Message-ID: <5F9CBDF7-2142-4B4E-9EA8-4E1A10777FE5@epy.co.at> Hi Michael, What we have learned from creating the Zope Toolkit (formerly Zope 3), is that __init__.py files in namespace packages should be empty, and imports should be absolute. [1] That said, there are ways to avoid import cycles. One is to very carefully craft your modules so they do not have to import from each other. Another is to not have imports at the module level, but move them into the functions where they are required. Third, large libraries like the Zope Toolkit usually have mechanisms to defer imports to some point after initial loading. You may want explore this direction as well. [2] (Not trying to plug the ZTK here, it just happens to be a large, namespace-using library I know.) Hope this helps, Stefan [1] http://docs.zope.org/zopetoolkit/ [2] http://pypi.python.org/pypi/zope.deferredimport -- Stefan H. Holek stefan at epy.co.at From no.email at nospam.invalid Fri Nov 2 14:29:01 2012 From: no.email at nospam.invalid (Paul Rubin) Date: Fri, 02 Nov 2012 11:29:01 -0700 Subject: Organisation of python classes and their methods References: <7xhap7dijb.fsf@ruckus.brouhaha.com> Message-ID: <7xsj8r97v6.fsf@ruckus.brouhaha.com> Martin Hewitson writes: >> you want just ONE method, something like "map"... > Well, because one of the features that the framework will have is to > capture history steps (in a tree structure) so that each processing > step the user does is tracked. So while methods such as abs(), cos(), > etc will eventually just call a built-in method, there will be some > house-keeping around them. Make the "map" wrapper do the house-keeping. > turns out that in Python, this is more naturally achieved (I think) if > each algorithm is implemented as a class, so that each algorithm can > return its set of supported parameters for validation against the user > inputs and, ultimately, for inclusion in a step in the history > tree. Since most of that infrastructure will turn out to be > boiler-plate code, it would make sense to have an algorithm base > class, which all other algorithms (abs, cos, etc) will inherit > from. That sounds like over-use of classes and inheritance. It's probably easiest to just use a dictionary with functions in it (untested): from collections import namedtuple Ts_func = namedtuple('Ts_func', ['name', func, 'doc', 'validate']) all_funcs = {} def add_func(name, func, doc, validate): all_funcs[name] = Ts_func(name, func, doc, validate) add_func('abs', abs, 'absolute value', lambda x: True) add_func('acos', math.acos, 'arc cosine', lambda x: abs(x) <= 1) ... You can then look up any of these entries and pass it to your map method. From rurpy at yahoo.com Fri Nov 2 14:39:10 2012 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Fri, 2 Nov 2012 11:39:10 -0700 (PDT) Subject: Obnoxious postings from Google Groups In-Reply-To: References: <5055a1a6$0$29981$c3e8da3$5496439d@news.astraweb.com> <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> Message-ID: On 11/02/2012 03:36 AM, Jamie Paul Griffin wrote: > / rurpy at yahoo.com wrote on Thu 1.Nov'12 at 15:08:26 -0700 / > >> On 11/01/2012 03:55 AM, Jamie Paul Griffin wrote: >>> Anybody serious about programming should be using a form of >>> UNIX/Linux if you ask me. It's inconceivable that these systems >>> should be avoided if you're serious about Software Engineering >>> and Computer Science, etc. For UNIX there are loads of decent >>> news reading software and mail user agents to learn and use. slrn >>> is a good one and point it at gmane.org as someone else pointed >>> out. I can't even imagine using a browser or Google Groups, etc. >>> now. > >> Are you saying that this group is only for "serious" programmers? > > I don't see where my comments suggested that this group is only for > serious programmers. I simply believe that the UNIX platform, in > whatever form, is better placed and designed for all sorts of > computing and engineering projects. The history of UNIX speaks for > itself. Many Universities that offer respected and credible science > based degree programmes, namely engineering and computing programmes, > strongly encourage students to become competent with UNIX systems. > Windows in my opinion is really for those who use the internet on a > casual basis or in a commercial environment where its staff are not > necessarily computer literate and therefore need a platform that they > can use which doesn't require them to learn more complex techniques > and protocols. But, having said that, I'm not against Windo ws at > all. I use it frequently and enjoy using it most of the time. Wow, that makes me feel like I am back in the 1990s! Thanks for the trip down memory lane. :-) >> "serious" is also a matter of opinion. I have some serious >> programmer friends who maintain, in complete sincerity, that >> serious programmers should not waste time on slow, script-kiddie >> languages like Python, but should be developing their skills with >> serious professional languages like Java, C#, etc. > > That is a narrow minded approach. different languages serve different > purposes and it's down to the developer to use which language she > needs to achieve what it is they've set out to do. Sometimes, basic > shell scripts can be extremely powerful for certain tasks; other > needs will require something different. I certainly wouldn't describe > Python as a "script-kiddie" language. It's extremely powerful and > modern. So there ;-P lol Right. I happen to agree with you and was just repeating an elitist attitude I've often heard where what *I* use is for *serious* business and what *they* use is just for playing around, for those without as much technical competence as me, etc. Without a quantitative definition of "serious" and some objective evidence supporting it, your opinion that unix is more "serious" than windows is as narrow-minded as my friends' opinion (which was the point I was trying to make and which you seem to have missed.) I don't particularly like Windows and am able to mostly avoid it these days, but think you should realize that describing it as not for *serious* use is going irritate some people and make you look like you are not able to make objective judgements. (I also hope I haven't just been suckered by a troll attempt, windows/unix is better then unix/windows being an age-old means of trolling.) From rurpy at yahoo.com Fri Nov 2 14:43:16 2012 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Fri, 2 Nov 2012 11:43:16 -0700 (PDT) Subject: datetime issue In-Reply-To: References: <8370412f-5176-45ff-87eb-72cbca5149d5@googlegroups.com> <5055a1a6$0$29981$c3e8da3$5496439d@news.astraweb.com> <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <8085a710-46c0-4d1a-b46e-f94eea0e8a0a@googlegroups.com> Message-ID: On 11/02/2012 03:57 AM, Jamie Paul Griffin wrote: > / rurpy at yahoo.com wrote on Thu 1.Nov'12 at 15:00:48 -0700 / >> [...list of Thunderbird problems...] > > With a list of problems like that maybe the time spent on learning > how to use a Usenet client or mua that is properly written would be > worthwhile. And that would be which one exactly? I have tried a few other mail and usenet clients, and as I said, indeed the point of my post was, they *all* have their own set of bugs, misfeatures and missing features. > Personally I haven't used the Google Groups interface, > and most likely never will so I can't really comment on how it > performs or how nice it is to use but if you're happy with it that's > the main thing. But you can't deny that it does cause irritating > problems for other people trying to read information sent from it. I can and do deny that. I certainly acknowledge that GG contributes to such posts, but naive users of GG and readers who are too easily irritated are two other major contributing factors. From hexusnexus at gmail.com Fri Nov 2 14:51:29 2012 From: hexusnexus at gmail.com (Jason Benjamin) Date: Fri, 02 Nov 2012 11:51:29 -0700 Subject: Proper place for everything In-Reply-To: References: Message-ID: On another note, it appears that Google (the only archive I can find for this group) only has a little under 400 messages archived for this group, while the slackware newsgroup has upwards 40,000 and contains posts I can't even remember that were made with one of the first emails I've ever used. On 11/02/2012 10:31 AM, Dennis Lee Bieber wrote: > On Fri, 02 Nov 2012 06:49:18 -0700, Jason Benjamin > declaimed the following in > gmane.comp.python.general: > >> Yeah, alright. I've just found that if you mention anything about a >> library that has well established competitors, the post will tend to get >> ignored here. >> > Interesting... The oldest post still in my news reader's cache is > October 17. > > Since that time, there are only two posts under your name in that > cache, both in this thread. You should maybe provide some link to such a > post so late-comers or others can find them... a message-ID could be > useful... > > I'll concede some posts may have been removed from the cache due to > filter actions (though for this group as found on gmane I only have > three active filters, and your name is not in any of them). > From foster63 at gmail.com Fri Nov 2 15:19:50 2012 From: foster63 at gmail.com (foster63 at gmail.com) Date: Fri, 2 Nov 2012 12:19:50 -0700 (PDT) Subject: Haskell -> Python Message-ID: Hi All, As part of a Nim solver I'm playing around with I'm trying to code this Haskell snippet: options [x] = zero : [ [y] | y <- [1..x - 1] ] options (x:xs) = map (++ xs) (options [x]) ++ map (x:) (options xs) in Python. So far I have this, which works OK, but somehow doesn't feel right: def options( heaps ): if heaps == []: return [] head, tail = heaps[:1], heaps[1:] # Calculate all possible moves which is the sum of # prepending all possible head "moves" to the tail # and appending all possible tail "moves" to the head return [ [h] + tail for h in range( head[0] ) ] \ + [ head + t for t in options( tail ) ] Is there anything anyone could recommend to make it more "Pythonic" or more functional. It looks clumsy next to the Haskell. Regards etc. From neilc at norwich.edu Fri Nov 2 15:33:41 2012 From: neilc at norwich.edu (Neil Cerutti) Date: 2 Nov 2012 19:33:41 GMT Subject: csv read clean up and write out to csv References: <10133786-7f4c-4b19-8678-a50781ff453d@googlegroups.com> Message-ID: On 2012-11-02, Sacha Rook wrote: > Hi > > I have a problem with a csv file from a supplier, so they > export data to csv however the last column in the record is a > description which is marked up with html. > > trying to automate the processing of this csv to upload > elsewhere in a useable format. If i open the csv with csved it > looks like all the records aren't escaped correctly as after a > while i find html tags and text on the next line/record. Maybe compose a simple parter to disambiguate the lines from the file. Something like (you'll have to write is_html, and my Python 2 is mighty rusty, you'll have to fix up. Note that infile doesn't have to be in binary mode with this scheme, but it would fail on bizarre newlines in the file): def parse_records(iter): for line in iter: if is_html(line): yield ('html', line) else: yield ('csv', csv.reader([line.strip()]).next()) infile = open('c:\data\input.csv') outfile = open('c:\data\output.csv', 'wb') writer = csv.writer(outfile) for tag, rec in parse_record(infile): if tag == 'html': print rec elif tag == 'csv': writer.writerow(rec) else: raise ValueError("Unknown record type %s" % tag) -- Neil Cerutti From hansmu at xs4all.nl Fri Nov 2 15:51:55 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Fri, 02 Nov 2012 20:51:55 +0100 Subject: csv read clean up and write out to csv In-Reply-To: <10133786-7f4c-4b19-8678-a50781ff453d@googlegroups.com> References: <10133786-7f4c-4b19-8678-a50781ff453d@googlegroups.com> Message-ID: <5094245b$0$6847$e4fe514c@news2.news.xs4all.nl> On 2/11/12 18:25:09, Sacha Rook wrote: > I have a problem with a csv file from a supplier, so they export data to csv > however the last column in the record is a description which is marked up > with html. > > trying to automate the processing of this csv to upload elsewhere in a > useable format. If i open the csv with csved it looks like all the records > aren't escaped correctly as after a while i find html tags and text on the > next line/record. The example line you gave was correctly escaped: the description starts with a double quote, and ends several lines later with another double quote. Double quotes in the HTML are represented by '"'. Maybe csved doesn't recognize this escape convention? > If I 'openwith' excel the description stays on the correct line/record? Excel implements this convention > I want to use python to read these records in and output a valid csv with > the descriptions intact preferably without the html tags so a string of > text formatted with newline/CR where appropriate. How about this: import csv infile = file("input.csv", "rb") outfile = file("output.csv", "wb") reader = csv.reader(infile) writer = csv.writer(outfile) for line in reader: line[-1] = line[-1].replace("\n", " ") print line writer.writerow(line) infile.close() outfile.close() That will replace the newlines inside the HTML, which your csved doesn't seem to recognize, by spaces. When viewed as HTML code, spaces have the same effect as newlines, so this replacement shouldn't alter the meaning of the HTML text. Hope this helps, -- HansM From d at davea.name Fri Nov 2 15:56:40 2012 From: d at davea.name (Dave Angel) Date: Fri, 02 Nov 2012 15:56:40 -0400 Subject: Haskell -> Python In-Reply-To: References: Message-ID: <50942578.2090202@davea.name> On 11/02/2012 03:19 PM, foster63 at gmail.com wrote: > Hi All, > > As part of a Nim solver I'm playing around with I'm trying to code this Haskell snippet: > > options [x] = zero : [ [y] | y <- [1..x - 1] ] > options (x:xs) = map (++ xs) (options [x]) ++ map (x:) (options xs) > > in Python. So far I have this, which works OK, but somehow doesn't feel right: > > def options( heaps ): > > if heaps == []: return [] > > head, tail = heaps[:1], heaps[1:] > > # Calculate all possible moves which is the sum of > # prepending all possible head "moves" to the tail > # and appending all possible tail "moves" to the head > > return [ [h] + tail for h in range( head[0] ) ] \ > + [ head + t for t in options( tail ) ] > > Is there anything anyone could recommend to make it more "Pythonic" or more functional. It looks clumsy next to the Haskell. > > Regards > > etc. You'd save people a lot of time if you'd specify that the parameter heaps is a list of ints, perhaps initially [1,3,5,7] or [3, 4, 5] depending on which variation of Nim you're trying to. There are many. One variant is that some versions of Nim say the winner is the player who does NOT take the last piece. I'll assume that the goal is to end up with [0,0,0,0] My main problem with studying your code is that brute force is totally unnecessary; there's a fairly simple strategy for winning at Nim. Certainly it's simple enough to have perfect strategy without any computer. A "good" move is any one where the xor of all the items in the list ends up as zero. There is always at least one move for an "ungood" position that results in a "good" one. Thus the strategy is to go from good to good, with the opponent always stuck on an ungood one. -- DaveA From andrew3 at r3dsolutions.com Fri Nov 2 15:57:47 2012 From: andrew3 at r3dsolutions.com (Andrew Robinson) Date: Fri, 02 Nov 2012 12:57:47 -0700 Subject: Memory profiling: Python 3.2 Message-ID: <509425BB.30104@r3dsolutions.com> When Python3.2 is running, is there an easy way within Python to capture the *total* amount of heap space the program is actually using (eg:real memory)? And how much of that heap space is allocated to variables ( including re-capturable data not yet GC'd ) ? From mail at timgolden.me.uk Fri Nov 2 16:05:30 2012 From: mail at timgolden.me.uk (Tim Golden) Date: Fri, 02 Nov 2012 20:05:30 +0000 Subject: Proper place for everything In-Reply-To: References: Message-ID: <5094278A.6010406@timgolden.me.uk> On 02/11/2012 18:51, Jason Benjamin wrote: > On another note, it appears that Google (the only archive I can find for > this group) only has a little under 400 messages archived for this > group http://mail.python.org/pipermail/python-list/ http://markmail.org/search/?q=python#query:python%20list%3Aorg.python.python-list+page:1+state:facets and doubtless others. TJG From simon.foster at inbox.com Fri Nov 2 16:09:29 2012 From: simon.foster at inbox.com (Simon Foster) Date: Fri, 02 Nov 2012 20:09:29 +0000 Subject: Haskell -> Python In-Reply-To: References: Message-ID: <1IWdnd9wkMbntQnNnZ2dnUVZ8ridnZ2d@brightview.co.uk> On 02/11/12 19:56, Dave Angel wrote: > On 11/02/2012 03:19 PM, foster63 at gmail.com wrote: >> Hi All, >> >> As part of a Nim solver I'm playing around with I'm trying to code this Haskell snippet: >> >> options [x] = zero : [ [y] | y <- [1..x - 1] ] >> options (x:xs) = map (++ xs) (options [x]) ++ map (x:) (options xs) >> >> in Python. So far I have this, which works OK, but somehow doesn't feel right: >> >> def options( heaps ): >> >> if heaps == []: return [] >> >> head, tail = heaps[:1], heaps[1:] >> >> # Calculate all possible moves which is the sum of >> # prepending all possible head "moves" to the tail >> # and appending all possible tail "moves" to the head >> >> return [ [h] + tail for h in range( head[0] ) ] \ >> + [ head + t for t in options( tail ) ] >> >> Is there anything anyone could recommend to make it more "Pythonic" or more functional. It looks clumsy next to the Haskell. >> >> Regards >> >> etc. > > You'd save people a lot of time if you'd specify that the parameter > heaps is a list of ints, perhaps initially [1,3,5,7] or [3, 4, 5] > depending on which variation of Nim you're trying to. There are many. > One variant is that some versions of Nim say the winner is the player > who does NOT take the last piece. I'll assume that the goal is to end > up with [0,0,0,0] > > My main problem with studying your code is that brute force is totally > unnecessary; there's a fairly simple strategy for winning at Nim. > Certainly it's simple enough to have perfect strategy without any computer. > > A "good" move is any one where the xor of all the items in the list ends > up as zero. There is always at least one move for an "ungood" position > that results in a "good" one. Thus the strategy is to go from good to > good, with the opponent always stuck on an ungood one. > > Hi Dave, Thanks for the comments. Yes, I should have specified that the input is a list of ints giving the size of each heap, and the return value should be a list of all game positions reachable from the input position. At the moment I'm not concentrating on any particular Nim flavour, just trying to enumerate all possible moves from a given position. I know that there's an easier way to determine winning-losing positions, but my question was more about programming style than Nim strategy. My code to calculate the "nim-value" looks like this: def nim_val( heaps ): return functools.reduce( operator.xor, heaps, 0 ) Assuming that we're playing "non-misere" Nim then a zero nim-value is a lose for the player *about* to play. Regards Simon From sanluca78 at gmail.com Fri Nov 2 16:31:15 2012 From: sanluca78 at gmail.com (Luca Sanna) Date: Fri, 2 Nov 2012 13:31:15 -0700 (PDT) Subject: ping in bluetooth Message-ID: <9c0a3110-495b-4cca-8c20-8d123826d623@googlegroups.com> hi, how do I send a ping in bluetooth? because android phones are not always visible. I can not find the ping command thanks From hexusnexus at gmail.com Fri Nov 2 16:48:27 2012 From: hexusnexus at gmail.com (Jason Benjamin) Date: Fri, 02 Nov 2012 13:48:27 -0700 Subject: Proper place for everything In-Reply-To: References: Message-ID: Yeah, now that I take a look at the said old post on this group, I can see why the post was ignored: http://markmail.org/thread/mnxpzt4jzx3zjeio On 11/02/2012 01:05 PM, Tim Golden wrote: > On 02/11/2012 18:51, Jason Benjamin wrote: >> On another note, it appears that Google (the only archive I can find for >> this group) only has a little under 400 messages archived for this >> group > > http://mail.python.org/pipermail/python-list/ > > > http://markmail.org/search/?q=python#query:python%20list%3Aorg.python.python-list+page:1+state:facets > > > and doubtless others. > > TJG > From andriy.kornatskyy at live.com Fri Nov 2 17:13:19 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Sat, 3 Nov 2012 00:13:19 +0300 Subject: How to generate account number? Message-ID: Requirements for `account number` generator: 1. Issue pseudo random consistent number (must be unique for dozen millions of records) 2. Easy check validity (without a need to make a database call) Interested? Read more here: http://mindref.blogspot.com/2012/11/generate-account-number.html Comments or suggestions are welcome. Thanks. Andriy Kornatskyy From josen.figueroa at unixmexico.org Fri Nov 2 17:29:13 2012 From: josen.figueroa at unixmexico.org (Jose Figueroa) Date: Fri, 2 Nov 2012 15:29:13 -0600 Subject: How to generate account number? In-Reply-To: References: Message-ID: Hello Andriy Thanks for your work! I will try it! Jose On Fri, Nov 2, 2012 at 3:13 PM, Andriy Kornatskyy < andriy.kornatskyy at live.com> wrote: > > Requirements for `account number` generator: > > 1. Issue pseudo random consistent number (must be unique for dozen > millions of records) > 2. Easy check validity (without a need to make a database call) > > Interested? Read more here: > > http://mindref.blogspot.com/2012/11/generate-account-number.html > > Comments or suggestions are welcome. > > Thanks. > > Andriy Kornatskyy > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ian.g.kelly at gmail.com Fri Nov 2 17:40:53 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 2 Nov 2012 15:40:53 -0600 Subject: Haskell -> Python In-Reply-To: References: Message-ID: On Fri, Nov 2, 2012 at 1:19 PM, wrote: > Is there anything anyone could recommend to make it more "Pythonic" or more functional. It looks clumsy next to the Haskell. def options(heaps): for i, heap in enumerate(heaps): head = heaps[:i] tail = heaps[i+1:] yield from (head + [x] + tail for x in range(heap)) "yield from" is Python 3.3 syntax. If you're not using Python 3.3, then that line could be replaced by: for x in range(heap): yield head + [x] + tail Cheers, Ian From ian.g.kelly at gmail.com Fri Nov 2 17:46:45 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 2 Nov 2012 15:46:45 -0600 Subject: Haskell -> Python In-Reply-To: References: Message-ID: On Fri, Nov 2, 2012 at 3:40 PM, Ian Kelly wrote: > On Fri, Nov 2, 2012 at 1:19 PM, wrote: >> Is there anything anyone could recommend to make it more "Pythonic" or more functional. It looks clumsy next to the Haskell. > > def options(heaps): > for i, heap in enumerate(heaps): > head = heaps[:i] > tail = heaps[i+1:] > yield from (head + [x] + tail for x in range(heap)) > > "yield from" is Python 3.3 syntax. If you're not using Python 3.3, > then that line could be replaced by: > > for x in range(heap): > yield head + [x] + tail In fact, the more that I look at it, the more that I think the latter might be preferable in any case. From solipsis at pitrou.net Fri Nov 2 17:47:43 2012 From: solipsis at pitrou.net (Antoine Pitrou) Date: Fri, 2 Nov 2012 21:47:43 +0000 (UTC) Subject: Memory profiling: Python 3.2 References: <509425BB.30104@r3dsolutions.com> Message-ID: Andrew Robinson r3dsolutions.com> writes: > > When Python3.2 is running, is there an easy way within Python to capture > the *total* amount of heap space the program is actually using (eg:real > memory)? I'm not sure what you mean with "real memory" or how precise you want that measurement to be, but you could try http://www.selenic.com/smem/ (and, within Python, re-use smem's concepts, which shouldn't be too difficult) > And how much of that heap space is allocated to variables ( > including re-capturable data not yet GC'd ) ? Again, not sure what you mean with "allocated to variables" (global variables? local variables? everything?). As for "re-capturable data not yet GC'd", the best way to figure out is to run gc.collect() :-) Regards Antoine. From steve+comp.lang.python at pearwood.info Fri Nov 2 17:57:31 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Nov 2012 21:57:31 GMT Subject: Proper place for everything References: Message-ID: <509441cb$0$29967$c3e8da3$5496439d@news.astraweb.com> On Fri, 02 Nov 2012 04:20:20 -0700, Jason Benjamin wrote: > Anybody know of the appropriate place to troll and flame about various > Python related issues? I'm kind of mad about some Python stuff and I > need a place to vent where people may or may not listen, but at at least > respond. Thought this would be a strange question, but I might as well > start somewhere. Thank you for your honesty, but trolling is not welcome. However if you have actual issues about Python, either pro or con, and hope to have a serious, respectful dialog where both parties listen to each other, feel free to raise them here. Keep in mind three things: - We've probably heard all your arguments a thousand times before. It's unlikely that you are either the first or the last person to notice that (e.g.) Python has significant indentation. So expect a certain amount of brusqueness. - If your argument boils down to "Python isn't " we will not be sympathetic, and will probably sneer or laugh at you privately. And possibly publicly too. - If you hope to convince the Python community to change , we are constrained by backwards-compatibility issues, policies, and design decisions. Frequently there are (mis-)features that we simply have to live with, for good or ill. -- Steven From GangGreene at example.com Fri Nov 2 18:02:09 2012 From: GangGreene at example.com (GangGreene) Date: Fri, 2 Nov 2012 18:02:09 -0400 Subject: How to generate account number? References: Message-ID: <1gsdm9-r2c.ln1@crazy-horse.bildanet.com> On Sat, 03 Nov 2012 00:13:19 +0300, Andriy Kornatskyy wrote: > Requirements for `account number` generator: > > 1. Issue pseudo random consistent number (must be unique for dozen > millions of records) > 2. Easy check validity (without a need to make a database call) > > Interested? Read more here: > > http://mindref.blogspot.com/2012/11/generate-account-number.html > > Comments or suggestions are welcome. > > Thanks. > > Andriy Kornatskyy generate sha1sum on the ((key database record(s))+date+timeofday) Should be unique for billions/trillions of records. From steve+comp.lang.python at pearwood.info Fri Nov 2 18:12:21 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Nov 2012 22:12:21 GMT Subject: Proper place for everything References: Message-ID: <50944545$0$29970$c3e8da3$5496439d@news.astraweb.com> On Fri, 02 Nov 2012 11:51:29 -0700, Jason Benjamin wrote: > On another note, it appears that Google (the only archive I can find for > this group) only has a little under 400 messages archived for this > group, Google Groups is poison. If you post with it, you will be ignored by a large percentage of the regulars here. Good options are: - Use the official email mailing list instead of news: http://mail.python.org/mailman/listinfo/python-list Do yourself (and us) a HUGE favour and use regular mail, not daily digests. - If you prefer Usenet, and your ISP gives you access to comp.lang.python, use that. - If not, there are many cheap Usenet providers you can try. - Try http://news.gmane.org/gmane.comp.python.general Bad options are: - Absolutely anything to do with Google Groups. It's a crap user- interface, posts don't follow established standards for email and news, and Google requires you to sign in just to read the archives, which in my opinion is morally indefensible. -- Steven From steve+comp.lang.python at pearwood.info Fri Nov 2 18:14:14 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Nov 2012 22:14:14 GMT Subject: Proper place for everything References: Message-ID: <509445b6$0$29967$c3e8da3$5496439d@news.astraweb.com> On Fri, 02 Nov 2012 13:48:27 -0700, Jason Benjamin wrote: > Yeah, now that I take a look at the said old post on this group, I can > see why the post was ignored: > > http://markmail.org/thread/mnxpzt4jzx3zjeio Good lord man, that was FOUR AND A HALF YEARS AGO. Have you been holding a grudge for all that time? Maybe nobody knew the answer. I certainly don't. The question was: [quote] I have some PNGs with transparent backgrounds. How do I draw them using PyGTK? [end quote] It is perfectly appropriate to wait three or four days, then reply with a comment requesting attention. You probably would have got some answers, even if they were only "take it to a specialist PyGTK mailing list". -- Steven From pkleiweg at xs4all.nl Fri Nov 2 18:22:53 2012 From: pkleiweg at xs4all.nl (Peter Kleiweg) Date: Fri, 2 Nov 2012 23:22:53 +0100 Subject: enabling universal newline Message-ID: In Python 3.1 and 3.2 At start-up, the value of sys.stdin.newlines is None, which means, universal newline should be enabled. But it isn't. So I do this: sys.stdin = io.TextIOWrapper(sys.stdin.detach(), newline=None) Now, sys.stdin.newlines is still None, but universal newline is enabled. Why is this? -- Peter Kleiweg http://pkleiweg.home.xs4all.nl/ From d at davea.name Fri Nov 2 18:24:57 2012 From: d at davea.name (Dave Angel) Date: Fri, 02 Nov 2012 18:24:57 -0400 Subject: Haskell -> Python In-Reply-To: References: Message-ID: <50944839.1070109@davea.name> On 11/02/2012 05:40 PM, Ian Kelly wrote: > On Fri, Nov 2, 2012 at 1:19 PM, wrote: >> Is there anything anyone could recommend to make it more "Pythonic" or more functional. It looks clumsy next to the Haskell. > def options(heaps): > for i, heap in enumerate(heaps): > head = heaps[:i] > tail = heaps[i+1:] > yield from (head + [x] + tail for x in range(heap)) > > "yield from" is Python 3.3 syntax. If you're not using Python 3.3, > then that line could be replaced by: > > for x in range(heap): > yield head + [x] + tail > > Cheers, > Ian Perhaps range(heap) should be replaced by range(len(heap)) -- DaveA From ian.g.kelly at gmail.com Fri Nov 2 18:27:59 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 2 Nov 2012 16:27:59 -0600 Subject: Haskell -> Python In-Reply-To: <50944839.1070109@davea.name> References: <50944839.1070109@davea.name> Message-ID: On Fri, Nov 2, 2012 at 4:24 PM, Dave Angel wrote: > Perhaps range(heap) should be replaced by range(len(heap)) "heaps" is a list of ints per the OP, so "heap" is an int. From steve+comp.lang.python at pearwood.info Fri Nov 2 18:39:31 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Nov 2012 22:39:31 GMT Subject: How to generate account number? References: Message-ID: <50944ba2$0$29967$c3e8da3$5496439d@news.astraweb.com> On Sat, 03 Nov 2012 00:13:19 +0300, Andriy Kornatskyy wrote: > Requirements for `account number` generator: > > 1. Issue pseudo random consistent number (must be unique for dozen > millions of records) How much randomness do you need? From the perspective of any one user, a simple incrementing counter returns arbitrary values, which may be "close enough" to random. last_num = 103872 # Pick an arbitrary starting value. def get_account_number(): """Return the next account number.""" global last_num last_num += 1 return last_num Stick that value in a database instead of a global, and you're done. What are the consequences of people guessing account numbers? If the consequences are serious, then you need to make account numbers cryptographically strong. If the account number alone is not important, then you don't. > 2. Easy check validity (without a need to make a database call) Add a check digit to the number you generate. There are all sorts of ways to do that. Here are two examples: http://code.activestate.com/recipes/577692 http://code.activestate.com/recipes/577691 > Interested? Read more here: If you ask a question here, please keep the discussion here, don't split it to your personal blog. Tell us your requirements in more detail, and we will try to help you. -- Steven From steve+comp.lang.python at pearwood.info Fri Nov 2 19:09:34 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Nov 2012 23:09:34 GMT Subject: enabling universal newline References: Message-ID: <509452ae$0$29967$c3e8da3$5496439d@news.astraweb.com> On Fri, 02 Nov 2012 23:22:53 +0100, Peter Kleiweg wrote: > In Python 3.1 and 3.2 > > At start-up, the value of sys.stdin.newlines is None, which means, > universal newline should be enabled. But it isn't. What makes you think it is not enabled? sys.stdin.newlines shows you the newlines actually seen. Until you put text including newlines through stdin, it will remain None. http://docs.python.org/2/library/stdtypes.html#file.newlines http://docs.python.org/3/library/io.html#io.TextIOBase.newlines For example, I have Python built with universal newlines, but stdin.newlines remains None: py> f = open('test.txt') py> f.newlines py> f.readlines() ['a\n', 'b\n', 'c\n', 'd\n'] py> f.newlines ('\r', '\n', '\r\n') py> sys.stdin.newlines is None True -- Steven From breamoreboy at yahoo.co.uk Fri Nov 2 20:06:12 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 03 Nov 2012 00:06:12 +0000 Subject: Obnoxious postings from Google Groups In-Reply-To: References: <5055a1a6$0$29981$c3e8da3$5496439d@news.astraweb.com> <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> Message-ID: On 02/11/2012 18:39, rurpy at yahoo.com wrote: > On 11/02/2012 03:36 AM, Jamie Paul Griffin wrote: >> / rurpy at yahoo.com wrote on Thu 1.Nov'12 at 15:08:26 -0700 / >> >>> On 11/01/2012 03:55 AM, Jamie Paul Griffin wrote: >>>> Anybody serious about programming should be using a form of >>>> UNIX/Linux if you ask me. It's inconceivable that these systems >>>> should be avoided if you're serious about Software Engineering >>>> and Computer Science, etc. For UNIX there are loads of decent >>>> news reading software and mail user agents to learn and use. slrn >>>> is a good one and point it at gmane.org as someone else pointed >>>> out. I can't even imagine using a browser or Google Groups, etc. >>>> now. >> >>> Are you saying that this group is only for "serious" programmers? >> >> I don't see where my comments suggested that this group is only for >> serious programmers. I simply believe that the UNIX platform, in >> whatever form, is better placed and designed for all sorts of >> computing and engineering projects. The history of UNIX speaks for >> itself. Many Universities that offer respected and credible science >> based degree programmes, namely engineering and computing programmes, >> strongly encourage students to become competent with UNIX systems. >> Windows in my opinion is really for those who use the internet on a >> casual basis or in a commercial environment where its staff are not >> necessarily computer literate and therefore need a platform that they >> can use which doesn't require them to learn more complex techniques >> and protocols. But, having said that, I'm not against Windo ws at >> all. I use it frequently and enjoy using it most of the time. > > Wow, that makes me feel like I am back in the 1990s! > Thanks for the trip down memory lane. :-) > >>> "serious" is also a matter of opinion. I have some serious >>> programmer friends who maintain, in complete sincerity, that >>> serious programmers should not waste time on slow, script-kiddie >>> languages like Python, but should be developing their skills with >>> serious professional languages like Java, C#, etc. >> >> That is a narrow minded approach. different languages serve different >> purposes and it's down to the developer to use which language she >> needs to achieve what it is they've set out to do. Sometimes, basic >> shell scripts can be extremely powerful for certain tasks; other >> needs will require something different. I certainly wouldn't describe >> Python as a "script-kiddie" language. It's extremely powerful and >> modern. So there ;-P lol > > Right. I happen to agree with you and was just repeating > an elitist attitude I've often heard where what *I* use > is for *serious* business and what *they* use is just > for playing around, for those without as much technical > competence as me, etc. > > Without a quantitative definition of "serious" and some > objective evidence supporting it, your opinion that unix > is more "serious" than windows is as narrow-minded as my > friends' opinion (which was the point I was trying to > make and which you seem to have missed.) > > I don't particularly like Windows and am able to mostly > avoid it these days, but think you should realize that > describing it as not for *serious* use is going irritate > some people and make you look like you are not able to > make objective judgements. > > (I also hope I haven't just been suckered by a troll > attempt, windows/unix is better then unix/windows being > an age-old means of trolling.) > Does Unix now have clustering, or is it still behind VMS aka Very Much Safer? -- Cheers. Mark Lawrence. From vasudevram at gmail.com Fri Nov 2 21:03:17 2012 From: vasudevram at gmail.com (vasudevram) Date: Fri, 2 Nov 2012 18:03:17 -0700 (PDT) Subject: PDFBuilder can create composite PDFs Message-ID: <71c0743f-34d9-490f-af90-bbf21c81e07f@googlegroups.com> PDFBuilder is a tool to create composite PDFs, i.e. PDFs comprising of data from multiple different input data formats (any number of files, in any order). It is a new component of my xtopdf toolkit for PDF generation. A blog post about PDFBuilder gives more information, an example, and a download link: http://jugad2.blogspot.in/2012/11/pdfbuilderpy-can-create-composite-pdfs.html - Vasudev Ram http://www.dancingbison.com http://jugad2.blogspot.com http://twitter.com/vasudevram From breamoreboy at yahoo.co.uk Fri Nov 2 21:04:35 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 03 Nov 2012 01:04:35 +0000 Subject: What the diferences : web.py Tornado Twisted ?! In-Reply-To: References: Message-ID: On 02/11/2012 15:50, nepaul wrote: > What the diferences : web.py Tornado Twisted ?! > Web.py is spelt w e b . p y. Tornado is spelt T o r n a d o. Twisted is spelt T w i s t e d. -- Cheers. Mark Lawrence. From steve+comp.lang.python at pearwood.info Fri Nov 2 21:06:45 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 03 Nov 2012 01:06:45 GMT Subject: Organisation of python classes and their methods References: Message-ID: <50946e25$0$29967$c3e8da3$5496439d@news.astraweb.com> On Fri, 02 Nov 2012 07:16:09 +0100, Martin Hewitson wrote: > I'm beginning a large Python project which contains many packages, > modules and classes. The organisation of those is clear to me. [...] > I don't like having source files with > 100's of lines of code in, let alone 1000's. Why? Do you do your software development on an iPhone? Hundreds of lines is nothing, especially if you are including docstrings and comments in that count. My Python startup file is over 100 lines, and it is trivial. 100 lines is approximately and a half pages using a 10pt font size (depending on the font and the platform, of course). In any case, it's not a lot. If you average two lines of code every comment, blank line or docstring, 100 lines is only 66 lines of actual code. To give you an example of what I consider pushing the limit of how much code should go into a single file, look at the source code to decimal.py. In Python 3.2, that file is 6249 lines. There are: - 2880 lines of actual code - 2070 lines in docstrings, including blanks - 606 commented lines - 693 blank lines outside of docstrings The module includes 20 top-level functions, 18 classes, and 213 methods. Most of those methods are in just two classes, Context and Decimal, with 76 and 117 methods respectively. So there is an average of 12 lines of code per function or method. I wouldn't like to have to deal with a single file twice that size, but decimal.py is relatively easy to deal with. It's not a trivial module by any means, but nor is it especially over-complex. The complexity is all in the computations, not the code layout. Please understand me -- I'm not saying that you should stuff all your code into a single module. Code should only be placed into a single module when the code is related. But in my opinion, you should not split a logical single unit of code, a module of related code, into separate files just because it is "too big" until it is at least as large as decimal.py. -- Steven From steve+comp.lang.python at pearwood.info Fri Nov 2 21:08:39 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 03 Nov 2012 01:08:39 GMT Subject: Organisation of python classes and their methods References: <50946e25$0$29967$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50946e97$0$29967$c3e8da3$5496439d@news.astraweb.com> On Sat, 03 Nov 2012 01:06:45 +0000, Steven D'Aprano wrote: > 100 lines is approximately and a half pages using a 10pt font size > (depending on the font and the platform, of course). Crap. I meant approx *one* and a half pages. -- Steven From steve+comp.lang.python at pearwood.info Fri Nov 2 21:38:55 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 03 Nov 2012 01:38:55 GMT Subject: Organisation of python classes and their methods References: <7xa9v0wj2g.fsf@ruckus.brouhaha.com> Message-ID: <509475af$0$29967$c3e8da3$5496439d@news.astraweb.com> On Fri, 02 Nov 2012 09:08:07 +0100, Martin Hewitson wrote: > Even if one takes reasonable numbers: 20 methods, each method has 20 > lines of documentation, then we immediately have 400 lines in the file > before writing a line of code. It would seem much more natural to me to > have these methods in their own file, grouped nicely in sub-directories. Ewww. Ewww ewww ewww ewww. Just reading about it makes me feel dirty. Seriously. That means any time you want to jump back and forth from one method to another method OF THE SAME CLASS, you have to change files. Yuck. -- Steven From d at davea.name Fri Nov 2 22:03:17 2012 From: d at davea.name (Dave Angel) Date: Fri, 02 Nov 2012 22:03:17 -0400 Subject: Haskell -> Python In-Reply-To: References: <50944839.1070109@davea.name> Message-ID: <50947B65.9020902@davea.name> On 11/02/2012 06:27 PM, Ian Kelly wrote: > On Fri, Nov 2, 2012 at 4:24 PM, Dave Angel wrote: >> Perhaps range(heap) should be replaced by range(len(heap)) > "heaps" is a list of ints per the OP, so "heap" is an int. You're right of course . I was distracted by the fact that a heap is normally a collection of thing, and didn't notice that here it is a count of those things. -- DaveA From rosuav at gmail.com Fri Nov 2 22:57:31 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 3 Nov 2012 13:57:31 +1100 Subject: What the diferences : web.py Tornado Twisted ?! In-Reply-To: References: Message-ID: On Sat, Nov 3, 2012 at 12:04 PM, Mark Lawrence wrote: > On 02/11/2012 15:50, nepaul wrote: >> >> What the diferences : web.py Tornado Twisted ?! >> > > Web.py is spelt w e b . p y. Tornado is spelt T o r n a d o. Twisted is > spelt T w i s t e d. Wow! That's profound! Oh, the insights made available, free of charge, by the generous members of this list! ChrisA From bob.martin at excite.com Sat Nov 3 03:44:01 2012 From: bob.martin at excite.com (Bob Martin) Date: Sat, 03 Nov 2012 07:44:01 GMT Subject: Obnoxious postings from Google Groups References: Message-ID: in 684220 20121102 093654 Jamie Paul Griffin wrote: >/ rurpy at yahoo.com wrote on Thu 1.Nov'12 at 15:08:26 -0700 / > >> On 11/01/2012 03:55 AM, Jamie Paul Griffin wrote: >> > Anybody serious about programming should be using a form of >> > UNIX/Linux if you ask me. It's inconceivable that these systems >> > should be avoided if you're serious about Software Engineering and >> > Computer Science, etc. For UNIX there are loads of decent news >> > reading software and mail user agents to learn and use. slrn is a >> > good one and point it at gmane.org as someone else pointed out. I >> > can't even imagine using a browser or Google Groups, etc. now. > >> Are you saying that this group is only for "serious" programmers? > >I don't see where my comments suggested that this group is only for serious programmers. I simply believe that the UNIX platform, in whatever form, is better placed and designed for all sorts of computing and engineering projects. The history of UNIX speaks for itself. Many Universities that offer respected and credible science based degree programmes, namely engineering and computing programmes, strongly encourage students to become competent with UNIX systems. Windows in my opinion is really for those who use the internet on a casual basis or in a commercial environment where its staff are not necessarily computer literate and therefore need a platform that they can use which doesn't require them to learn more complex techniques and protocols. But, having said that, I'm not against Windows at all. I use it frequently and enjoy using it most of the time. > >> "serious" is also a matter of opinion. I have some serious >> programmer friends who maintain, in complete sincerity, that >> serious programmers should not waste time on slow, script-kiddie >> languages like Python, but should be developing their skills >> with serious professional languages like Java, C#, etc. > >That is a narrow minded approach. different languages serve different purposes and it's down to the developer to use which language she needs to achieve what it is they've set out to do. Sometimes, basic shell scripts can be extremely powerful for certain tasks; other needs will require something different. I certainly wouldn't describe Python as a "script-kiddie" language. It's extremely powerful and modern. So there ;-P lol Real programmers (can) write in assembler. From maniandram01 at gmail.com Sat Nov 3 04:26:09 2012 From: maniandram01 at gmail.com (Ramchandra Apte) Date: Sat, 3 Nov 2012 01:26:09 -0700 (PDT) Subject: better way for ' '.join(args) + '\n'? In-Reply-To: References: <20121026212602.GE25893@ccapuser-ubuntu.WICOURTS.GOV> <508B0272.2020003@davea.name> Message-ID: <4b5a2a54-3b80-42f3-8eb2-d4830fedd3c5@googlegroups.com> On Saturday, 27 October 2012 03:12:31 UTC+5:30, Tycho Andersen wrote: > On Fri, Oct 26, 2012 at 05:36:50PM -0400, Dave Angel wrote: > > > On 10/26/2012 05:26 PM, Tycho Andersen wrote: > > > > Assuming it's the length of the list that's the problem, not the > > > > length of the strings in the list... > > > > > > > > args = ['foo', 'bar', 'baz'] > > > > args[-1] = args[-1] + '\n' > > > > line = ' '.join(args) > > > > > > > > \t > > > > > > Main problem with that is the trailing space before the newline. If > > > that's not a problem, then fine. > > > > What trailing space before the newline? The other solutions have it, > > the above does not. However, the above does mutate args, which isn't > > all that great. Alas, if you want the performance of mutable > > structures, you're probably going to have to mutate something. (In any > > case, it's easy enough to change it back, though ugly.) > > > > > Not sure why we try so hard to optimize something that's going to take > > > negligible time. > > > > The same reason some people enjoy sporting events: it's fun :-) Me too > > > > \t From maniandram01 at gmail.com Sat Nov 3 04:26:09 2012 From: maniandram01 at gmail.com (Ramchandra Apte) Date: Sat, 3 Nov 2012 01:26:09 -0700 (PDT) Subject: better way for ' '.join(args) + '\n'? In-Reply-To: References: <20121026212602.GE25893@ccapuser-ubuntu.WICOURTS.GOV> <508B0272.2020003@davea.name> Message-ID: <4b5a2a54-3b80-42f3-8eb2-d4830fedd3c5@googlegroups.com> On Saturday, 27 October 2012 03:12:31 UTC+5:30, Tycho Andersen wrote: > On Fri, Oct 26, 2012 at 05:36:50PM -0400, Dave Angel wrote: > > > On 10/26/2012 05:26 PM, Tycho Andersen wrote: > > > > Assuming it's the length of the list that's the problem, not the > > > > length of the strings in the list... > > > > > > > > args = ['foo', 'bar', 'baz'] > > > > args[-1] = args[-1] + '\n' > > > > line = ' '.join(args) > > > > > > > > \t > > > > > > Main problem with that is the trailing space before the newline. If > > > that's not a problem, then fine. > > > > What trailing space before the newline? The other solutions have it, > > the above does not. However, the above does mutate args, which isn't > > all that great. Alas, if you want the performance of mutable > > structures, you're probably going to have to mutate something. (In any > > case, it's easy enough to change it back, though ugly.) > > > > > Not sure why we try so hard to optimize something that's going to take > > > negligible time. > > > > The same reason some people enjoy sporting events: it's fun :-) Me too > > > > \t From __peter__ at web.de Sat Nov 3 04:26:39 2012 From: __peter__ at web.de (Peter Otten) Date: Sat, 03 Nov 2012 09:26:39 +0100 Subject: enabling universal newline References: <509452ae$0$29967$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Fri, 02 Nov 2012 23:22:53 +0100, Peter Kleiweg wrote: > >> In Python 3.1 and 3.2 >> >> At start-up, the value of sys.stdin.newlines is None, which means, >> universal newline should be enabled. But it isn't. > > What makes you think it is not enabled? $ python3 -c 'open("tmp.txt", "wb").write(b"a\nb\r\nc\rd")' This is the output with universal newlines: $ python3 -c 'print(open("tmp.txt").readlines())' ['a\n', 'b\n', 'c\n', 'd'] But this is what you get from stdin: $ cat tmp.txt | python3 -c 'import sys; print(sys.stdin.readlines())' ['a\n', 'b\r\n', 'c\rd'] With Peter Kleiweg's fix: $ cat tmp.txt | python3 -c 'import sys, io; print(io.TextIOWrapper(sys.stdin.detach(), newline=None).readlines())' ['a\n', 'b\n', 'c\n', 'd'] I think it's reasonable to make the latter the default. From pkleiweg at xs4all.nl Sat Nov 3 04:30:46 2012 From: pkleiweg at xs4all.nl (Peter Kleiweg) Date: Sat, 3 Nov 2012 09:30:46 +0100 Subject: enabling universal newline In-Reply-To: <509452ae$0$29967$c3e8da3$5496439d@news.astraweb.com> References: <509452ae$0$29967$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steven D'Aprano schreef op de 2e dag van de slachtmaand van het jaar 2012: > On Fri, 02 Nov 2012 23:22:53 +0100, Peter Kleiweg wrote: > > > In Python 3.1 and 3.2 > > > > At start-up, the value of sys.stdin.newlines is None, which means, > > universal newline should be enabled. But it isn't. > > What makes you think it is not enabled? Script 1: #!/usr/bin/env python3.1 import sys print(sys.stdin.readlines()) Output: ~ test.py < text ['a\rbc\rdef\r'] Script 2: #!/usr/bin/env python3.1 import io, sys sys.stdin = io.TextIOWrapper(sys.stdin.detach(), newline=None) print(sys.stdin.readlines()) Output: ~ test.py < text ['a\n', 'bc\n', 'def\n'] -- Peter Kleiweg http://pkleiweg.home.xs4all.nl/ From andriy.kornatskyy at live.com Sat Nov 3 04:33:06 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Sat, 3 Nov 2012 11:33:06 +0300 Subject: How to generate account number? In-Reply-To: References: , Message-ID: Jose, absolutely, let me know should you have any issues. Andriy ________________________________ > Date: Fri, 2 Nov 2012 15:29:13 -0600 > Subject: Re: How to generate account number? > From: josen.figueroa at unixmexico.org > To: andriy.kornatskyy at live.com > CC: python-list at python.org > > Hello Andriy > > Thanks for your work! > > I will try it! > Jose > > > On Fri, Nov 2, 2012 at 3:13 PM, Andriy Kornatskyy > > wrote: > > Requirements for `account number` generator: > > 1. Issue pseudo random consistent number (must be unique for dozen > millions of records) > 2. Easy check validity (without a need to make a database call) > > Interested? Read more here: > > http://mindref.blogspot.com/2012/11/generate-account-number.html > > Comments or suggestions are welcome. > > Thanks. > > Andriy Kornatskyy > > -- > http://mail.python.org/mailman/listinfo/python-list > From andriy.kornatskyy at live.com Sat Nov 3 04:38:38 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Sat, 3 Nov 2012 11:38:38 +0300 Subject: How to generate account number? In-Reply-To: <1gsdm9-r2c.ln1@crazy-horse.bildanet.com> References: , <1gsdm9-r2c.ln1@crazy-horse.bildanet.com> Message-ID: >>> from hashlib import sha1 >>> sha1('GangGreene-20120203-1012').hexdigest() 'ef764a2fe44532008dc9a99c391c70cd85ec9d82' It is too long and not verifiable. >>> from uuid import uuid4 >>> uuid4() UUID('2c14484b-5a0c-4f4b-b7bc-8187548b4888') Pretty much the same what you suggest but simpler and shorter. Not quite elegant for humans. Here are examples per this post: http://mindref.blogspot.com/2012/11/generate-account-number.html >>> account_number(1) 'Z05738521581' >>> account_number(2) 'Z17888279480' >>> account_number(3) 'Z07395350007' Short, human readable and satisfy original requirements. Andriy ---------------------------------------- > From: GangGreene at example.com > Subject: Re: How to generate account number? > Date: Fri, 2 Nov 2012 18:02:09 -0400 > To: python-list at python.org > > On Sat, 03 Nov 2012 00:13:19 +0300, Andriy Kornatskyy wrote: > >> Requirements for `account number` generator: >> >> 1. Issue pseudo random consistent number (must be unique for dozen >> millions of records) >> 2. Easy check validity (without a need to make a database call) >> >> Interested? Read more here: >> >> http://mindref.blogspot.com/2012/11/generate-account-number.html >> >> Comments or suggestions are welcome. >> >> Thanks. >> >> Andriy Kornatskyy > > generate sha1sum on the ((key database record(s))+date+timeofday) > Should be unique for billions/trillions of records. > -- > http://mail.python.org/mailman/listinfo/python-list From andriy.kornatskyy at live.com Sat Nov 3 04:42:38 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Sat, 3 Nov 2012 11:42:38 +0300 Subject: How to generate account number? In-Reply-To: <50944ba2$0$29967$c3e8da3$5496439d@news.astraweb.com> References: , <50944ba2$0$29967$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steven, see below, please. ---------------------------------------- > From: steve+comp.lang.python at pearwood.info > Subject: Re: How to generate account number? > Date: Fri, 2 Nov 2012 22:39:31 +0000 > To: python-list at python.org > > On Sat, 03 Nov 2012 00:13:19 +0300, Andriy Kornatskyy wrote: > >> Requirements for `account number` generator: >> >> 1. Issue pseudo random consistent number (must be unique for dozen >> millions of records) > > How much randomness do you need? From the perspective of any one user, a > simple incrementing counter returns arbitrary values, which may be "close > enough" to random. > > last_num = 103872 # Pick an arbitrary starting value. > def get_account_number(): > """Return the next account number.""" > global last_num > last_num += 1 > return last_num > > Stick that value in a database instead of a global, and you're done. > > What are the consequences of people guessing account numbers? If the > consequences are serious, then you need to make account numbers > cryptographically strong. If the account number alone is not important, > then you don't. Yes. There are consequences to not use sequential numbers, yet humans deal with it (enter as input somewhere, etc). The approach suggested here: http://mindref.blogspot.com/2012/11/generate-account-number.html is using Feistel cipher to generate pseudo random thus makes guessing account numbers hard (impossible?). >> 2. Easy check validity (without a need to make a database call) > > Add a check digit to the number you generate. There are all sorts of ways > to do that. Here are two examples: > > http://code.activestate.com/recipes/577692 > http://code.activestate.com/recipes/577691 These tell me how to verify some code, but doesn't how to generate it. The approach suggested here: http://mindref.blogspot.com/2012/11/generate-account-number.html gives you ability to customize `sample_f` function to make it unique to your business case. >> Interested? Read more here: > > If you ask a question here, please keep the discussion here, don't split > it to your personal blog. The question was rhetorical with my answer in the blog and discussion here to reach something. > Tell us your requirements in more detail, and we will try to help you. I have presented solution to `account number` challenge. So it was share with community and seek for thoughts if any. From michi.schwarz at gmail.com Sat Nov 3 04:54:43 2012 From: michi.schwarz at gmail.com (Michael Schwarz) Date: Sat, 3 Nov 2012 09:54:43 +0100 Subject: How to improve the usability of nested packages In-Reply-To: References: Message-ID: <13D3B568-C4FE-49B1-8569-F9595595D6D3@gmail.com> Hi Terry On 2012-W44-5, at 18:56, Terry Reedy wrote: >> or would you maybe structure the library entirely different? > > Based on my limited experience with subpackages* plus reports on this list about problems, such as yours, I have concluded that subpackages are an attractive nuisance that are generally more trouble than they are worth. I suggest you consider sticking with your original flat (no subpackage) design. (But maybe someone knows better than me how to make subpackages work ;-). One thing that I would lose is the way I can choose very short names for the packages and modules that are imported into the local namespace (like sip or rtp) and also add new stuff without fearing a namespace conflict in one of the applications using the library. I really hope there is a better way :-(. Michael -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2227 bytes Desc: not available URL: From 1248283536 at qq.com Sat Nov 3 04:58:47 2012 From: 1248283536 at qq.com (=?gb18030?B?y66+ssH3ye4=?=) Date: Sat, 3 Nov 2012 16:58:47 +0800 Subject: install pyOpenSSL in python2.7 Message-ID: i have install pyOpenSSL-0.11 in python2.7 this way: download pyOpenSSL-0.11.tar.gz #tar -zvxf pyOpenSSL-0.11.tar.gz #cd pyOpenSSL-0.11 #python setup.py install >>> import OpenSSL Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python2.7/dist-packages/OpenSSL/__init__.py", line 45, in from OpenSSL import rand, SSL ImportError: /usr/local/lib/python2.7/dist-packages/OpenSSL/SSL.so: undefined symbol: SSLv2_method how can i fix the problem? ImportError: /usr/local/lib/python2.7/dist-packages/OpenSSL/SSL.so: undefined symbol: SSLv2_method -------------- next part -------------- An HTML attachment was scrubbed... URL: From michi.schwarz at gmail.com Sat Nov 3 05:26:32 2012 From: michi.schwarz at gmail.com (Michael Schwarz) Date: Sat, 3 Nov 2012 10:26:32 +0100 Subject: How to improve the usability of nested packages In-Reply-To: <5F9CBDF7-2142-4B4E-9EA8-4E1A10777FE5@epy.co.at> References: <5F9CBDF7-2142-4B4E-9EA8-4E1A10777FE5@epy.co.at> Message-ID: <062021B6-963F-4420-B2D1-505713B36034@gmail.com> Hi Stefan On 2012-W44-5, at 19:23, Stefan H. Holek wrote: > That said, there are ways to avoid import cycles. One is to very carefully craft your modules so they do not have to import from each other. Another is to not have imports at the module level, but move them into the functions where they are required. I've also thought about that. I do not like the fact that I then need to educate the other developers about minute details of the import machinery just so they can add code to the library. I'm currently the only developer doing actual work in Python and would like to make the transition as painless as possible. > Third, large libraries like the Zope Toolkit usually have mechanisms to defer imports to some point after initial loading. You may want explore this direction as well. [2] Hmm, I like the idea but sadly it doesn't look very IDE-friendly. Thanks for your tips! Michael [1]: http://docs.zope.org/zopetoolkit/codingstyle/python-style.html -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2227 bytes Desc: not available URL: From alt.mcarter at gmail.com Sat Nov 3 06:20:48 2012 From: alt.mcarter at gmail.com (Mark Carter) Date: Sat, 3 Nov 2012 03:20:48 -0700 (PDT) Subject: Google spreadsheets - getting started Message-ID: <7d42e442-15cb-47e9-b1d1-48ee334648c8@googlegroups.com> I want to mess around with my online Google spreadsheets from my Linux box programmatically. I am TOTALLY confused. I've got gdata installed, and it appears that the best way to access the spreadsheets is to authenticate with Oauth2. Here's the main thing: how do I get an Oauth2 key to use with Google spreadsheets? I obtain a "p12" key, but I don't know if that's for something completely different. From alt.mcarter at gmail.com Sat Nov 3 07:03:30 2012 From: alt.mcarter at gmail.com (Mark Carter) Date: Sat, 3 Nov 2012 04:03:30 -0700 (PDT) Subject: Google spreadsheets - getting started In-Reply-To: <7d42e442-15cb-47e9-b1d1-48ee334648c8@googlegroups.com> References: <7d42e442-15cb-47e9-b1d1-48ee334648c8@googlegroups.com> Message-ID: <4f36c90c-1d85-44f7-a642-9bc7d3b9a276@googlegroups.com> OK, maybe the p12 file is useful after all (?) I've got the following code: import gdata tokenfile = "my-privatekey.p12" f = open(tokenfile, 'r') blob = f.read() f.close() token = gdata.gauth.token_from_blob(blob) When I run that I get: Traceback (most recent call last): File "/home/mcarter/wapp.py", line 8, in token = gdata.gauth.token_from_blob(blob) AttributeError: 'module' object has no attribute 'gauth' I guess I'm using a newer version of gdata (2.0.14). None of this makes any sense. From alt.mcarter at gmail.com Sat Nov 3 08:40:51 2012 From: alt.mcarter at gmail.com (Mark Carter) Date: Sat, 3 Nov 2012 05:40:51 -0700 (PDT) Subject: Google spreadsheets - getting started In-Reply-To: <4f36c90c-1d85-44f7-a642-9bc7d3b9a276@googlegroups.com> References: <7d42e442-15cb-47e9-b1d1-48ee334648c8@googlegroups.com> <4f36c90c-1d85-44f7-a642-9bc7d3b9a276@googlegroups.com> Message-ID: OK, the story so far: import gdata import gdata.auth import gdata.gauth import gdata.docs.service import OpenSSL.crypto tokenfile = "privatekey.p12" #f = open(tokenfile, 'r') #blob = f.read() #f.close() #if blob: p12 = OpenSSL.crypto.load_pkcs12(file(tokenfile, 'rb').read(), 'notasecret') print p12.get_certificate() #token = gdata.gauth.token_from_blob(p12) #print "token: ", token gd_client = gdata.docs.service.DocsService() #gd_client.SetOAuthToken(token) gd_client.SetOAuthToken(p12) feed = gd_client.GetDocumentListFeed() # line 22 for entry in feed.entry: print entry.title.text.encode('UTF-8') print "Finished" It baulks as follows: /usr/bin/python -u "/home/mcarter/wapp.py" Traceback (most recent call last): File "/home/mcarter/wapp.py", line 22, in feed = gd_client.GetDocumentListFeed() File "/usr/lib/pymodules/python2.7/gdata/docs/service.py", line 259, in GetDocumentListFeed return self.QueryDocumentListFeed(uri) File "/usr/lib/pymodules/python2.7/gdata/docs/service.py", line 238, in QueryDocumentListFeed return self.Get(uri, converter=gdata.docs.DocumentListFeedFromString) File "/usr/lib/pymodules/python2.7/gdata/service.py", line 1068, in Get headers=extra_headers) File "/usr/lib/pymodules/python2.7/atom/__init__.py", line 92, in optional_warn_function return f(*args, **kwargs) File "/usr/lib/pymodules/python2.7/atom/service.py", line 184, in request return auth_token.perform_request(self.http_client, operation, url, AttributeError: 'PKCS12' object has no attribute 'perform_request' What?? From roy at panix.com Sat Nov 3 09:22:55 2012 From: roy at panix.com (Roy Smith) Date: Sat, 03 Nov 2012 09:22:55 -0400 Subject: How to generate account number? References: <1gsdm9-r2c.ln1@crazy-horse.bildanet.com> Message-ID: In article , Andriy Kornatskyy wrote: > 'Z05738521581' > 'Z17888279480' > 'Z07395350007' > > Short, human readable and satisfy original requirements. > > Andriy If you really want human readable, it's better to chunk the data up into 3 or 4 digit groups. So, instead of Z05738521581, maybe Z05-738-521-581. Or perhaps even better, Z05-7385-21-581 (just a hunch, but I suspect varying the length of the groups makes it easier to read). Even better might be base-32 encoding the value. Strings of digits have an information density of about 3.2 bits/char. Base-32 is just about as readable, but gives you 5 bits/char, so you end up with a few less characters (which you still want to chunk into 3 or 4 character groups). From bv8bv8bv8 at gmail.com Sat Nov 3 09:56:19 2012 From: bv8bv8bv8 at gmail.com (BV BV) Date: Sat, 3 Nov 2012 06:56:19 -0700 (PDT) Subject: What is Islam? Message-ID: What is Islam? In this episode Shaikh Yusuf Estes Explains the meaning of Islam in detail. http://youtube.googleapis.com/v/Gl-wuhzOkpo?rel=0 thank you From d at davea.name Sat Nov 3 10:24:15 2012 From: d at davea.name (Dave Angel) Date: Sat, 03 Nov 2012 10:24:15 -0400 Subject: Obnoxious postings from Google Groups In-Reply-To: References: Message-ID: <5095290F.9080206@davea.name> On 11/03/2012 03:44 AM, Bob Martin wrote: > >> > Real programmers (can) write in assembler. Real programmers can (and have) write in hex/octal or binary. For my first project at a permanent job, I had to write code for a machine with no assembler. Near the end of the project, I wrote a text editor and (cross) assembler for it, because maintaining the source with pen/ink was getting tedious. For the DOS world, real programmers have written a "complete" *.com program using only echo. -- DaveA From torriem at gmail.com Sat Nov 3 10:50:58 2012 From: torriem at gmail.com (Michael Torrie) Date: Sat, 03 Nov 2012 08:50:58 -0600 Subject: How to generate account number? In-Reply-To: References: Message-ID: <50952F52.5070707@gmail.com> On 11/02/2012 03:13 PM, Andriy Kornatskyy wrote: > > Requirements for `account number` generator: > > 1. Issue pseudo random consistent number (must be unique for dozen millions of records) > 2. Easy check validity (without a need to make a database call) > > Interested? Read more here: > > http://mindref.blogspot.com/2012/11/generate-account-number.html > > Comments or suggestions are welcome. Thank you for sharing. Your post came along at just the right time. I was just pondering on how to create a number that is unique each time (or most of the time), and unlikely to be guessed ahead of time. Your technique should work very well for me. From python.list at tim.thechases.com Sat Nov 3 11:34:26 2012 From: python.list at tim.thechases.com (Tim Chase) Date: Sat, 03 Nov 2012 10:34:26 -0500 Subject: How to generate account number? In-Reply-To: References: <1gsdm9-r2c.ln1@crazy-horse.bildanet.com> Message-ID: <50953982.7090305@tim.thechases.com> On 11/03/12 08:22, Roy Smith wrote: > Even better might be base-32 encoding the value. Strings of > digits have an information density of about 3.2 bits/char. > Base-32 is just about as readable, but gives you 5 bits/char, so > you end up with a few less characters (which you still want to > chunk into 3 or 4 character groups). For things that will be read off a screen/paper, I recommend omitting several letters that are easy to mistake visually: i/I/l/1 and O/0 in particular. The VIN (vehicle identification number) on all US cars avoids these characters[*], making it easier to read them back without concern for "is that a zero or an oh; and is that an ell, a one, a lowercase eye, or a capital eye?" As an encoding advantage, >>> print len(''.join(c for c in (string.ascii_uppercase + string.digits) if c not in "O0iIl1")) 32 the number 32 is pretty handy when dealing with binary :-) -tkc [*] The VIN avoids "Q" too and does use the digits 0/1, but the idea holds. Make it easy to ready back. From rosuav at gmail.com Sat Nov 3 12:05:42 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 4 Nov 2012 03:05:42 +1100 Subject: Obnoxious postings from Google Groups In-Reply-To: <5095290F.9080206@davea.name> References: <5095290F.9080206@davea.name> Message-ID: On Sun, Nov 4, 2012 at 1:24 AM, Dave Angel wrote: > For the DOS world, real programmers have written a "complete" *.com > program using only echo. Only as an exercise. It was satisfying to prove to myself that I could do it, but pretty useless. Normally I used DEBUG.EXE to build my code - it has a mini-assembler in it. Incidentally, I used debug as a full assembler, with a bit of a REXX harness around it - used that to write OS/2 code in pure assembly, without the bother of, yaknow, getting an actual assembler. Suddenly things got WAY easier once I got hold of nasm... ChrisA From andriy.kornatskyy at live.com Sat Nov 3 12:18:09 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Sat, 3 Nov 2012 19:18:09 +0300 Subject: How to generate account number? In-Reply-To: References: , <1gsdm9-r2c.ln1@crazy-horse.bildanet.com>, , Message-ID: Roy, Per your advise: >>> from base64 import b32encode >>> human_format = lambda n: 'Z%s-%s' % (b32encode(chr((n >> 24) & 255) + chr((n >> 16) & 255))[:4], b32encode(chr((n >> 8) & 255) + chr(n & 255))[:4]) >>> human_format(5738521581) 'ZKYFA-4PWQ' >>> human_format(17888279480) 'ZFI4Q-PO4A' >>> human_format(7395350007) 'ZXDGA-CX3Q' Side by side: Z05738521581 = ZKYFA-4PWQ Z17888279480 = ZFI4Q-PO4A Z07395350007 = ZXDGA-CX3Q Thanks. Andriy ---------------------------------------- > From: roy at panix.com > Subject: Re: How to generate account number? > Date: Sat, 3 Nov 2012 09:22:55 -0400 > To: python-list at python.org > > In article , > Andriy Kornatskyy wrote: > > > 'Z05738521581' > > 'Z17888279480' > > 'Z07395350007' > > > > Short, human readable and satisfy original requirements. > > > > Andriy > > If you really want human readable, it's better to chunk the data up into > 3 or 4 digit groups. So, instead of Z05738521581, maybe > Z05-738-521-581. Or perhaps even better, Z05-7385-21-581 (just a hunch, > but I suspect varying the length of the groups makes it easier to read). > > Even better might be base-32 encoding the value. Strings of digits have > an information density of about 3.2 bits/char. Base-32 is just about as > readable, but gives you 5 bits/char, so you end up with a few less > characters (which you still want to chunk into 3 or 4 character groups). > -- > http://mail.python.org/mailman/listinfo/python-list From steve+comp.lang.python at pearwood.info Sat Nov 3 12:18:56 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 03 Nov 2012 16:18:56 GMT Subject: Obnoxious postings from Google Groups References: Message-ID: <509543f0$0$29967$c3e8da3$5496439d@news.astraweb.com> On Sat, 03 Nov 2012 10:24:15 -0400, Dave Angel wrote: > For the DOS world, real programmers have written a "complete" *.com > program using only echo. Echo? Wimps. Real programmers write their code directly on the surface of the hard drive using only a magnetised needle. -- Steven From andriy.kornatskyy at live.com Sat Nov 3 12:23:36 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Sat, 3 Nov 2012 19:23:36 +0300 Subject: How to generate account number? In-Reply-To: <50953982.7090305@tim.thechases.com> References: <1gsdm9-r2c.ln1@crazy-horse.bildanet.com> , , <50953982.7090305@tim.thechases.com> Message-ID: Tim, Good point. b32decode seems to be capable to understand such common mistakes (see map01 argument to b32decode), I haven't tried: http://docs.python.org/2/library/base64.html Thanks. Andriy ---------------------------------------- > Date: Sat, 3 Nov 2012 10:34:26 -0500 > From: python.list at tim.thechases.com > To: roy at panix.com > Subject: Re: How to generate account number? > CC: python-list at python.org > > On 11/03/12 08:22, Roy Smith wrote: > > Even better might be base-32 encoding the value. Strings of > > digits have an information density of about 3.2 bits/char. > > Base-32 is just about as readable, but gives you 5 bits/char, so > > you end up with a few less characters (which you still want to > > chunk into 3 or 4 character groups). > > For things that will be read off a screen/paper, I recommend > omitting several letters that are easy to mistake visually: i/I/l/1 > and O/0 in particular. The VIN (vehicle identification number) on > all US cars avoids these characters[*], making it easier to read > them back without concern for "is that a zero or an oh; and is that > an ell, a one, a lowercase eye, or a capital eye?" As an encoding > advantage, > > >>> print len(''.join(c for c in (string.ascii_uppercase + > string.digits) if c not in "O0iIl1")) > 32 > > the number 32 is pretty handy when dealing with binary :-) > > -tkc > > > [*] > The VIN avoids "Q" too and does use the digits 0/1, but the idea > holds. Make it easy to ready back. > -- > http://mail.python.org/mailman/listinfo/python-list From duncan.booth at invalid.invalid Sat Nov 3 12:29:44 2012 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 3 Nov 2012 16:29:44 GMT Subject: Haskell -> Python References: Message-ID: Ian Kelly wrote: > On Fri, Nov 2, 2012 at 1:19 PM, wrote: >> Is there anything anyone could recommend to make it more "Pythonic" >> or more functional. It looks clumsy next to the Haskell. > > def options(heaps): > for i, heap in enumerate(heaps): > head = heaps[:i] > tail = heaps[i+1:] > yield from (head + [x] + tail for x in range(heap)) > > "yield from" is Python 3.3 syntax. If you're not using Python 3.3, > then that line could be replaced by: > > for x in range(heap): > yield head + [x] + tail > > Cheers, > Ian An alternative that is closer to foster63's original but still more "Pythonic" for some definitions of those words. def options(heaps): if not heaps: return [] head, *tail = heaps for h in range(head): yield [h]+tail for t in options(tail): yield [head]+t For a more 'functional' version there is also the Python 3.3 variant: def options(heaps): if not heaps: return [] head, *tail = heaps yield from ([h]+tail for h in range(head)) yield from ([head]+t for t in options(tail)) -- Duncan Booth http://kupuguy.blogspot.com From aahz at pythoncraft.com Sat Nov 3 15:41:28 2012 From: aahz at pythoncraft.com (Aahz) Date: 3 Nov 2012 12:41:28 -0700 Subject: is implemented with id ? References: <50475822$0$6867$e4fe514c@news2.news.xs4all.nl> Message-ID: [got some free time, catching up to threads two months old] In article <50475822$0$6867$e4fe514c at news2.news.xs4all.nl>, Hans Mulder wrote: >On 5/09/12 15:19:47, Franck Ditter wrote: >> >> - I should have said that I work with Python 3. Does that matter ? >> - May I reformulate the queston : "a is b" and "id(a) == id(b)" >> both mean : "a et b share the same physical address". Is that True ? > >Yes. > >Keep in mind, though, that in some implementation (e.g. Jython), the >physical address may change during the life time of an object. > >It's usually phrased as "a and b are the same object". If the object >is mutable, then changing a will also change b. If a and b aren't >mutable, then it doesn't really matter whether they share a physical >address. That last sentence is not quite true. intern() is used to ensure that strings share a physical address to save memory. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "....Normal is what cuts off your sixth finger and your tail..." --Siobhan From hansmu at xs4all.nl Sat Nov 3 17:49:07 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Sat, 03 Nov 2012 22:49:07 +0100 Subject: is implemented with id ? In-Reply-To: References: <50475822$0$6867$e4fe514c@news2.news.xs4all.nl> Message-ID: <50959154$0$6880$e4fe514c@news2.news.xs4all.nl> On 3/11/12 20:41:28, Aahz wrote: > [got some free time, catching up to threads two months old] > > In article <50475822$0$6867$e4fe514c at news2.news.xs4all.nl>, > Hans Mulder wrote: >> On 5/09/12 15:19:47, Franck Ditter wrote: >>> >>> - I should have said that I work with Python 3. Does that matter ? >>> - May I reformulate the queston : "a is b" and "id(a) == id(b)" >>> both mean : "a et b share the same physical address". Is that True ? >> >> Yes. >> >> Keep in mind, though, that in some implementation (e.g. Jython), the >> physical address may change during the life time of an object. >> >> It's usually phrased as "a and b are the same object". If the object >> is mutable, then changing a will also change b. If a and b aren't >> mutable, then it doesn't really matter whether they share a physical >> address. > > That last sentence is not quite true. intern() is used to ensure that > strings share a physical address to save memory. That's a matter of perspective: in my book, the primary advantage of working with interned strings is that I can use 'is' rather than '==' to test for equality if I know my strings are interned. The space savings are minor; the time savings may be significant. -- HansM From steve+comp.lang.python at pearwood.info Sat Nov 3 18:18:15 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 03 Nov 2012 22:18:15 GMT Subject: is implemented with id ? References: <50475822$0$6867$e4fe514c@news2.news.xs4all.nl> <50959154$0$6880$e4fe514c@news2.news.xs4all.nl> Message-ID: <50959827$0$29967$c3e8da3$5496439d@news.astraweb.com> On Sat, 03 Nov 2012 22:49:07 +0100, Hans Mulder wrote: > On 3/11/12 20:41:28, Aahz wrote: >> [got some free time, catching up to threads two months old] >> >> In article <50475822$0$6867$e4fe514c at news2.news.xs4all.nl>, Hans Mulder >> wrote: >>> On 5/09/12 15:19:47, Franck Ditter wrote: >>>> >>>> - I should have said that I work with Python 3. Does that matter ? - >>>> May I reformulate the queston : "a is b" and "id(a) == id(b)" >>>> both mean : "a et b share the same physical address". Is that True >>>> ? >>> >>> Yes. >>> >>> Keep in mind, though, that in some implementation (e.g. Jython), the >>> physical address may change during the life time of an object. >>> >>> It's usually phrased as "a and b are the same object". If the object >>> is mutable, then changing a will also change b. If a and b aren't >>> mutable, then it doesn't really matter whether they share a physical >>> address. >> >> That last sentence is not quite true. intern() is used to ensure that >> strings share a physical address to save memory. > > That's a matter of perspective: in my book, the primary advantage of > working with interned strings is that I can use 'is' rather than '==' to > test for equality if I know my strings are interned. The space savings > are minor; the time savings may be significant. Actually, for many applications, the space "savings" may actually be *costs*, since interning forces Python to hold onto strings even after they would normally be garbage collected. CPython interns strings that look like identifiers. It really wouldn't be a good idea for it to automatically intern every string. You can make your own intern system with a simple dict: interned_strings = {} Then, for every string you care about, do: s = interned_strings.set_default(s, s) to ensure you are always working with a single string object for each unique value. In some applications that will save time at the expense of space. And there is no need to write "is" instead of "==", because string equality already optimizes the "strings are identical" case. By using ==, you don't get into bad habits, you defend against the odd un-interned string sneaking in, and you still have high speed equality tests. -- Steven From andrew3 at r3dsolutions.com Sat Nov 3 18:34:04 2012 From: andrew3 at r3dsolutions.com (Andrew Robinson) Date: Sat, 03 Nov 2012 15:34:04 -0700 Subject: Fwd: Re: Negative array indicies and slice() In-Reply-To: <50959B64.6060902@r3dsolutions.com> References: <50959B64.6060902@r3dsolutions.com> Message-ID: <50959BDC.1010609@r3dsolutions.com> Forwarded to python list: -------- Original Message -------- Subject: Re: Negative array indicies and slice() Date: Sat, 03 Nov 2012 15:32:04 -0700 From: Andrew Robinson Reply-To: andrew3 at r3dsolutions.com To: Ian Kelly <> On 11/01/2012 05:32 PM, Ian Kelly wrote: > On Thu, Nov 1, 2012 at 4:25 PM, Andrew Robinson >> The bottom line is: __getitem__ must always *PASS* len( seq ) to slice() >> each *time* the slice() object is-used. Since this is the case, it would >> have been better to have list, itself, have a default member which takes the >> raw slice indicies and does the conversion itself. The size would not need >> to be duplicated or passed -- memory savings,& speed savings... > And then tuple would need to duplicate the same code. As would deque. > And str. And numpy.array, and anything else that can be sliced, > including custom sequence classes. I don't think that's true. A generic function can be shared among different objects without being embedded in an external index data structure to boot! If *self* were passed to an index conversion function (as would naturally happen anyway if it were a method), then the method could take len( self ) without knowing what the object is; Should the object be sliceable -- the len() will definitely return the required piece of information. > Numpy arrays are very different internally from lists. Of course! (Although, lists do allow nested lists.) > I'm not understanding what this is meant to demonstrate. Is "MyClass" > a find-replace error of "ThirdParty"? Why do you have __getitem__ > returning slice objects instead of items or subsequences? What does > this example have to do with numpy? Here's a very cleaned up example file, cut and pastable: #!/bin/env python # File: sliceIt.py --- a pre PEP357 hypothesis test skeleton class Float16(): """ Numpy creates a float type, with very limited precision -- float16 Rather than force you to install np for this test, I'm just making a faux object. normally we'd just "import np" """ def __init__(self,value): self.value = value def AltPEP357Solution(self): """ This is doing exactly what __index__ would be doing. """ return None if self.value is None else int( self.value ) class ThirdParty( list ): """ A simple class to implement a list wrapper, having all the properties of a normal list -- but explicitly showing portions of the interface. """ def __init__(self, aList): self.aList = aList def __getitem__(self, aSlice): print( "__getitems__", aSlice ) temp=[] edges = aSlice.indices( len( self.aList ) ) # *unavoidable* call for i in range( *edges ): temp.append( self.aList[ i ] ) return temp def Inject_FloatSliceFilter( theClass ): """ This is a courtesy function to allow injecting (duck punching) a float index filter into a user object. """ def Filter_FloatSlice( self, aSlice ): # Single index retrieval filter try: start=aSlice.AltPEP357Solution() except AttributeError: pass else: return self.aList[ start ] # slice retrieval filter try: start=aSlice.start.AltPEP357Solution() except AttributeError: start=aSlice.start try: stop=aSlice.stop.AltPEP357Solution() except AttributeError: stop=aSlice.stop try: step=aSlice.step.AltPEP357Solution() except AttributeError: step=aSlice.step print( "Filter To",start,stop,step ) return self.super_FloatSlice__getitem__( slice(start,stop,step) ) theClass.super_FloatSlice__getitem__ = theClass.__getitem__ theClass.__getitem__ = Filter_FloatSlice # EOF: sliceIt.py -------------------------------------------------------- Example run: >>> from sliceIt import * >>> test = ThirdParty( [1,2,3,4,5,6,7,8,9] ) >>> test[0:6:3] ('__getitems__', slice(0, 6, 3)) [1, 4] >>> f16=Float16(8.3) >>> test[0:f16:2] ('__getitems__', slice(0,, 2)) Traceback (most recent call last): File "", line 1, in File "sliceIt.py", line 26, in __getitem__ edges = aSlice.indices( len( self.aList ) ) # This is an *unavoidable* call TypeError: object cannot be interpreted as an index >>> Inject_FloatSliceFilter( ThirdParty ) >>> test[0:f16:2] ('Filter To', 0, 8, 2) ('__getitems__', slice(0, 8, 2)) [1, 3, 5, 7] >>> test[f16] 9 > We could also require the user to explicitly declare when they're > performing arithmetic on variables that might not be floats. Then we > can turn off run-time type checking unless the user explicitly > requests it, all in the name of micro-optimization and explicitness. :) None of those would help micro-optimization that I can see. > Seriously, whether x is usable as a sequence index is a property of x, > not a property of the sequence. Yes, but the *LENGTH* of the sequence is a function of the *sequence*. > Users shouldn't need to pick and choose *which* particular sequence > index types their custom sequences are willing to accept. They should > even be able to accept sequence index types that haven't been written > yet. I disagree, and "float" is a good example. Besides -- Personally -- I don't have a problem with subclassing for a custom sequence; in spite of what D'Aprano thinks. It's the generic sequences that irritate me. OK, then, in your opinion what's the unspoken reason that PEP 357 happened, when in fact people already could have just said: myList[ int(firstItem) : int(secondItem), int(thirdItem) ] ? >> Most importantly normal programs not using Numpy wouldn't have had to carry >> around an extra API check for index() *every* single time the heavily used >> [::] happened. Memory& speed both. > The O(1) __index__ check is probably rather inconsequential compared > to the O(n) cost of actually performing the slicing. I'm sure that's true; at least -- I'm sure that O(1) index check done at the *C* level is probably inconsequential compared to slicing at the *C* level. When the index checking has to happen at the python interpreter level, I'm not so sure... I'm trying to learn how to profile that. > > Such a change would only affect numpy floats, not all floats, so it > would not be a monkey-patch. User's of python generally don't bother checking the types. The object "typeing" ability, I think, is a rather new development. When a function accepts float, it often returns a "float"; so there is no reason that one might not mix a python float and third party "float" as function parameters -- and then use a return from that function which could be *either* kind of float. Since this is typical behavior, variables which have traditionally been python floats can become another type without explicit warning; and then may (all the sudden) index any list, anywhere, any time. Besides, PEP357 doesn't DISTINGUISH between system floats and python floats as indices. The writers clearly believed that NETHER of them were acceptable as indices. That alone makes being able to turn *some* floats "ON" as indices unexpected behavior. ( I think the PEP writers did the best they could with the limited tools they had coming into their minds. ) As an aside: I am treating this as a postmortem; Gathering information and looking for what was *good* as well as what was bad about an implementation. I have, for example, noticed that non-mutables can't be made to have loops later; Hence any object made strictly out of non mutables at every step -- do not need garbage collection; and that can be used to get rid of GC overhead on any object obeying that property. -------------- next part -------------- An HTML attachment was scrubbed... URL: From roy at panix.com Sat Nov 3 18:41:50 2012 From: roy at panix.com (Roy Smith) Date: Sat, 03 Nov 2012 18:41:50 -0400 Subject: is implemented with id ? References: <50475822$0$6867$e4fe514c@news2.news.xs4all.nl> <50959154$0$6880$e4fe514c@news2.news.xs4all.nl> Message-ID: In article <50959154$0$6880$e4fe514c at news2.news.xs4all.nl>, Hans Mulder wrote: > That's a matter of perspective: in my book, the primary advantage of > working with interned strings is that I can use 'is' rather than '==' > to test for equality if I know my strings are interned. The space > savings are minor; the time savings may be significant. Depending on your problem domain, the space savings may be considerable. From rosuav at gmail.com Sat Nov 3 18:50:28 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 4 Nov 2012 09:50:28 +1100 Subject: is implemented with id ? In-Reply-To: <50959827$0$29967$c3e8da3$5496439d@news.astraweb.com> References: <50475822$0$6867$e4fe514c@news2.news.xs4all.nl> <50959154$0$6880$e4fe514c@news2.news.xs4all.nl> <50959827$0$29967$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, Nov 4, 2012 at 9:18 AM, Steven D'Aprano wrote: > On Sat, 03 Nov 2012 22:49:07 +0100, Hans Mulder wrote: > Actually, for many applications, the space "savings" may actually be > *costs*, since interning forces Python to hold onto strings even after > they would normally be garbage collected. CPython interns strings that > look like identifiers. It really wouldn't be a good idea for it to > automatically intern every string. I don't know about that. /* This dictionary holds all interned unicode strings. Note that references to strings in this dictionary are *not* counted in the string's ob_refcnt. When the interned string reaches a refcnt of 0 the string deallocation function will delete the reference from this dictionary. Another way to look at this is that to say that the actual reference count of a string is: s->ob_refcnt + (s->state ? 2 : 0) */ static PyObject *interned; Empirical testing (on a Linux 3.3a0 that I had lying around) showed the process's memory usage drop, but I closed the terminal before copying and pasting (oops). Attempting to recreate in IDLE on 3.2 on Windows. >>> a="$"*1024*1024*256 # Make $$$....$$$ fast! >>> import sys >>> sys.getsizeof(a) # Clearly this is a narrow build 536870942 >>> a="$"*1024*1024*256 --> MemoryError. Blah. This is what I get for only having a gig and a half in this laptop. And I was working with 1024*1024*1024 on the other box. Start over... >>> import sys >>> a="$"*1024*1024*128 >>> b="$"*1024*1024*128 >>> a is b False >>> a=sys.intern(a) >>> b=sys.intern(b) >>> c="$"*1024*1024*128 >>> c=sys.intern(c) Memory usage (according to Task Mangler) goes up to ~512MB when I create a new string (like c), then back down to ~256MB when I intern it. So far so good. >>> del a,b,c Memory usage has dropped to 12MB. Unnecessarily-interned strings don't cost anything. (The source does refer to immortal interned strings, but AFAIK you can't create them in user-level code. At least, I didn't find it in help(sys.intern) which is the obvious place to look.) > You can make your own intern system with a simple dict: > > interned_strings = {} > > Then, for every string you care about, do: > > s = interned_strings.set_default(s, s) > > to ensure you are always working with a single string object for each > unique value. In some applications that will save time at the expense of > space. Doing it manually like this _will_ leak like that, though, unless you periodically check sys.getrefcount and dispose of unreferenced entries. > And there is no need to write "is" instead of "==", because string > equality already optimizes the "strings are identical" case. By using ==, > you don't get into bad habits, you defend against the odd un-interned > string sneaking in, and you still have high speed equality tests. This one I haven't checked the source for, but ISTR discussions on this list about comparison of two unequal interned strings not being optimized, so they'll end up being compared char-for-char. Using 'is' guarantees that the check stops with identity. This may or may not be significant, and as you say, defending against an uninterned string slipping through is potentially critical. ChrisA From oscar.j.benjamin at gmail.com Sat Nov 3 21:14:29 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Sun, 4 Nov 2012 01:14:29 +0000 Subject: is implemented with id ? In-Reply-To: References: <50475822$0$6867$e4fe514c@news2.news.xs4all.nl> <50959154$0$6880$e4fe514c@news2.news.xs4all.nl> <50959827$0$29967$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 3 November 2012 22:50, Chris Angelico wrote: > This one I haven't checked the source for, but ISTR discussions on > this list about comparison of two unequal interned strings not being > optimized, so they'll end up being compared char-for-char. Using 'is' > guarantees that the check stops with identity. This may or may not be > significant, and as you say, defending against an uninterned string > slipping through is potentially critical. The source is here (and it shows what you suggest): http://hg.python.org/cpython/file/6c639a1ff53d/Objects/unicodeobject.c#l6128 Comparing strings char for char is really not that big a deal though. This has been discussed before: you don't need to compare very many characters to conclude that strings are unequal (if I remember correctly you were part of that discussion). I can imagine cases where I might consider using intern on lots of strings to speed up comparisons but I would have to be involved in some seriously heavy and obscure string processing problem before I considered using 'is' to compare those interned strings. That is confusing to anyone who reads the code, prone to bugs and unlikely to achieve the desired outcome of speeding things up (noticeably). Oscar From rosuav at gmail.com Sat Nov 3 21:22:17 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 4 Nov 2012 12:22:17 +1100 Subject: is implemented with id ? In-Reply-To: References: <50475822$0$6867$e4fe514c@news2.news.xs4all.nl> <50959154$0$6880$e4fe514c@news2.news.xs4all.nl> <50959827$0$29967$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, Nov 4, 2012 at 12:14 PM, Oscar Benjamin wrote: > On 3 November 2012 22:50, Chris Angelico wrote: >> This one I haven't checked the source for, but ISTR discussions on >> this list about comparison of two unequal interned strings not being >> optimized, so they'll end up being compared char-for-char. Using 'is' >> guarantees that the check stops with identity. This may or may not be >> significant, and as you say, defending against an uninterned string >> slipping through is potentially critical. > > The source is here (and it shows what you suggest): > http://hg.python.org/cpython/file/6c639a1ff53d/Objects/unicodeobject.c#l6128 > > Comparing strings char for char is really not that big a deal though. > This has been discussed before: you don't need to compare very many > characters to conclude that strings are unequal (if I remember > correctly you were part of that discussion). Yes, and a quite wide-ranging discussion it was too! What color did we end up whitewashing that bikeshed? *whistles innocently* > I can imagine cases where I might consider using intern on lots of > strings to speed up comparisons but I would have to be involved in > some seriously heavy and obscure string processing problem before I > considered using 'is' to compare those interned strings. That is > confusing to anyone who reads the code, prone to bugs and unlikely to > achieve the desired outcome of speeding things up (noticeably). Good point. It's still true that 'is' will be faster, it's just not worth it. ChrisA From kapetanovic.zerina at gmail.com Sat Nov 3 22:27:08 2012 From: kapetanovic.zerina at gmail.com (kapetanovic.zerina at gmail.com) Date: Sat, 3 Nov 2012 19:27:08 -0700 (PDT) Subject: PYTHON - Using Mosaic Qt Developer Suite with Qt Designer Message-ID: <99ccda0d-923e-4922-8d53-3442a207c174@googlegroups.com> Hi, I'm using Qt Designer to create a GUI that reads data from a file and plots in on a graph. I downloaded the Qt Developer and followed the directions in the readme.txt to add the plugins. My issue is that when I open Qt Designer the graphing widgets are not there to use. I was hoping to get some tips or advice on why this is not working. Thanks in Advance, Zerina From steve+comp.lang.python at pearwood.info Sat Nov 3 23:10:24 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 04 Nov 2012 03:10:24 GMT Subject: is implemented with id ? References: <50475822$0$6867$e4fe514c@news2.news.xs4all.nl> <50959154$0$6880$e4fe514c@news2.news.xs4all.nl> <50959827$0$29967$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5095dca0$0$29967$c3e8da3$5496439d@news.astraweb.com> On Sun, 04 Nov 2012 01:14:29 +0000, Oscar Benjamin wrote: > On 3 November 2012 22:50, Chris Angelico wrote: >> This one I haven't checked the source for, but ISTR discussions on this >> list about comparison of two unequal interned strings not being >> optimized, so they'll end up being compared char-for-char. Using 'is' >> guarantees that the check stops with identity. This may or may not be >> significant, and as you say, defending against an uninterned string >> slipping through is potentially critical. > > The source is here (and it shows what you suggest): > http://hg.python.org/cpython/file/6c639a1ff53d/Objects/ unicodeobject.c#l6128 I don't think it does, although I could be wrong, I find reading C to be quite difficult. The unicode_compare function compares character by character, true, but it doesn't get called directly. The public interface is PyUnicode_Compare, which includes this test before calling unicode_compare: /* Shortcut for empty or interned objects */ if (v == u) { Py_DECREF(u); Py_DECREF(v); return 0; } result = unicode_compare(u, v); where v and u are pointers to the unicode object. So it appears that the test for strings being equal length have been dropped, but the identity test is still present. > Comparing strings char for char is really not that big a deal though. Depends on how big the string and where the first difference is. > This has been discussed before: you don't need to compare very many > characters to conclude that strings are unequal (if I remember correctly > you were part of that discussion). On average. Worst case, you have to look at every character. -- Steven From steve+comp.lang.python at pearwood.info Sat Nov 3 23:11:22 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 04 Nov 2012 03:11:22 GMT Subject: PYTHON - Using Mosaic Qt Developer Suite with Qt Designer References: <99ccda0d-923e-4922-8d53-3442a207c174@googlegroups.com> Message-ID: <5095dcd9$0$29967$c3e8da3$5496439d@news.astraweb.com> On Sat, 03 Nov 2012 19:27:08 -0700, kapetanovic.zerina wrote: > Hi, > > I'm using Qt Designer to create a GUI that reads data from a file and > plots in on a graph. I downloaded the Qt Developer and followed the > directions in the readme.txt to add the plugins. My issue is that when I > open Qt Designer the graphing widgets are not there to use. > > I was hoping to get some tips or advice on why this is not working. Try asking on a Qt Designer forum. -- Steven From rosuav at gmail.com Sat Nov 3 23:19:55 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 4 Nov 2012 14:19:55 +1100 Subject: is implemented with id ? In-Reply-To: <5095dca0$0$29967$c3e8da3$5496439d@news.astraweb.com> References: <50475822$0$6867$e4fe514c@news2.news.xs4all.nl> <50959154$0$6880$e4fe514c@news2.news.xs4all.nl> <50959827$0$29967$c3e8da3$5496439d@news.astraweb.com> <5095dca0$0$29967$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, Nov 4, 2012 at 2:10 PM, Steven D'Aprano wrote: > /* Shortcut for empty or interned objects */ > if (v == u) { > Py_DECREF(u); > Py_DECREF(v); > return 0; > } > result = unicode_compare(u, v); > > where v and u are pointers to the unicode object. There's a shortcut if they're the same. There's no shortcut if they're both interned and have different pointers, which is a guarantee that they're distinct strings. They'll still be compared char-for-char until there's a difference. But it probably isn't enough of a performance penalty to be concerned with. It's enough to technically prove the point that 'is' is faster than '==' and is still safe if both strings are interned; it's not enough to make 'is' better than '==', except in very specific situations. ChrisA From vincent.vandevyvre at swing.be Sun Nov 4 00:26:51 2012 From: vincent.vandevyvre at swing.be (Vincent Vande Vyvre) Date: Sun, 04 Nov 2012 05:26:51 +0100 Subject: PYTHON - Using Mosaic Qt Developer Suite with Qt Designer In-Reply-To: <99ccda0d-923e-4922-8d53-3442a207c174@googlegroups.com> References: <99ccda0d-923e-4922-8d53-3442a207c174@googlegroups.com> Message-ID: <5095EE8B.7090706@swing.be> Le 04/11/12 03:27, kapetanovic.zerina at gmail.com a ?crit : > Hi, > > I'm using Qt Designer to create a GUI that reads data from a file and plots in on a graph. I downloaded the Qt Developer and followed the directions in the readme.txt to add the plugins. My issue is that when I open Qt Designer the graphing widgets are not there to use. > > I was hoping to get some tips or advice on why this is not working. > > > Thanks in Advance, > > Zerina What plugin?, his name, eventually the link where you've downloaded it. And what graphing widget? QGraphicsView? -- Vincent V.V. Oqapy . Qarte . PaQager From aahz at pythoncraft.com Sun Nov 4 01:08:08 2012 From: aahz at pythoncraft.com (Aahz) Date: 3 Nov 2012 22:08:08 -0700 Subject: is implemented with id ? References: <50959154$0$6880$e4fe514c@news2.news.xs4all.nl> <50959827$0$29967$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <50959827$0$29967$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > >Actually, for many applications, the space "savings" may actually be >*costs*, since interning forces Python to hold onto strings even after >they would normally be garbage collected. That's old news, fixed in 2.5 or 2.6 IIRC -- interned strings now get collected by refcounting like everything else. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "....Normal is what cuts off your sixth finger and your tail..." --Siobhan From aahz at pythoncraft.com Sun Nov 4 01:09:24 2012 From: aahz at pythoncraft.com (Aahz) Date: 3 Nov 2012 22:09:24 -0700 Subject: is implemented with id ? References: <5095dca0$0$29967$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article , Chris Angelico wrote: >On Sun, Nov 4, 2012 at 2:10 PM, Steven D'Aprano > wrote: >> >> /* Shortcut for empty or interned objects */ >> if (v == u) { >> Py_DECREF(u); >> Py_DECREF(v); >> return 0; >> } >> result = unicode_compare(u, v); >> >> where v and u are pointers to the unicode object. > >There's a shortcut if they're the same. There's no shortcut if they're >both interned and have different pointers, which is a guarantee that >they're distinct strings. They'll still be compared char-for-char >until there's a difference. Without looking at the code, I'm pretty sure there's a hash check first. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "....Normal is what cuts off your sixth finger and your tail..." --Siobhan From aahz at pythoncraft.com Sun Nov 4 01:12:10 2012 From: aahz at pythoncraft.com (Aahz) Date: 3 Nov 2012 22:12:10 -0700 Subject: is implemented with id ? References: <50475822$0$6867$e4fe514c@news2.news.xs4all.nl> <50959154$0$6880$e4fe514c@news2.news.xs4all.nl> Message-ID: In article <50959154$0$6880$e4fe514c at news2.news.xs4all.nl>, Hans Mulder wrote: >On 3/11/12 20:41:28, Aahz wrote: >> In article <50475822$0$6867$e4fe514c at news2.news.xs4all.nl>, >> Hans Mulder wrote: >>> On 5/09/12 15:19:47, Franck Ditter wrote: >>>> >>>> - I should have said that I work with Python 3. Does that matter ? >>>> - May I reformulate the queston : "a is b" and "id(a) == id(b)" >>>> both mean : "a et b share the same physical address". Is that True ? >>> >>> Yes. >>> >>> Keep in mind, though, that in some implementation (e.g. Jython), the >>> physical address may change during the life time of an object. >>> >>> It's usually phrased as "a and b are the same object". If the object >>> is mutable, then changing a will also change b. If a and b aren't >>> mutable, then it doesn't really matter whether they share a physical >>> address. >> >> That last sentence is not quite true. intern() is used to ensure that >> strings share a physical address to save memory. > >That's a matter of perspective: in my book, the primary advantage of >working with interned strings is that I can use 'is' rather than '==' >to test for equality if I know my strings are interned. The space >savings are minor; the time savings may be significant. As others have pointed out, using ``is`` with strings is a Bad Habit likely leading to nasty, hard-to-find bugs. intern() costs time, but saves considerable space in any application with lots of duplicate computed strings (hundreds of megabytes in some cases). -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "....Normal is what cuts off your sixth finger and your tail..." --Siobhan From aahz at pythoncraft.com Sun Nov 4 01:16:30 2012 From: aahz at pythoncraft.com (Aahz) Date: 3 Nov 2012 22:16:30 -0700 Subject: Haskell -> Python References: Message-ID: In article , wrote: > >def options( heaps ): > > if heaps == []: return [] > > head, tail = heaps[:1], heaps[1:] > > # Calculate all possible moves which is the sum of > # prepending all possible head "moves" to the tail > # and appending all possible tail "moves" to the head > > return [ [h] + tail for h in range( head[0] ) ] \ > + [ head + t for t in options( tail ) ] > >Is there anything anyone could recommend to make it more "Pythonic" or >more functional. It looks clumsy next to the Haskell. If you want more Pythonic, follow PEP8 in your formatting. ;-) -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "....Normal is what cuts off your sixth finger and your tail..." --Siobhan From aahz at pythoncraft.com Sun Nov 4 01:19:19 2012 From: aahz at pythoncraft.com (Aahz) Date: 3 Nov 2012 22:19:19 -0700 Subject: Proper place for everything References: <509441cb$0$29967$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <509441cb$0$29967$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: >On Fri, 02 Nov 2012 04:20:20 -0700, Jason Benjamin wrote: >> >> Anybody know of the appropriate place to troll and flame about various >> Python related issues? I'm kind of mad about some Python stuff and I >> need a place to vent where people may or may not listen, but at at least >> respond. Thought this would be a strange question, but I might as well >> start somewhere. > >Thank you for your honesty, but trolling is not welcome. > >However if you have actual issues about Python, either pro or con, and >hope to have a serious, respectful dialog where both parties listen to >each other, feel free to raise them here. Keep in mind three things: > >- We've probably heard all your arguments a thousand times before. It's > unlikely that you are either the first or the last person to notice > that (e.g.) Python has significant indentation. So expect a certain > amount of brusqueness. > >- If your argument boils down to "Python isn't " > we will not be sympathetic, and will probably sneer or laugh at you > privately. And possibly publicly too. > >- If you hope to convince the Python community to change , > we are constrained by backwards-compatibility issues, policies, and > design decisions. Frequently there are (mis-)features that we simply > have to live with, for good or ill. You forgot the fourth point. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "....Normal is what cuts off your sixth finger and your tail..." --Siobhan From levinie001 at gmail.com Sun Nov 4 01:29:00 2012 From: levinie001 at gmail.com (Levi Nie) Date: Sun, 4 Nov 2012 13:29:00 +0800 Subject: who can give me some practical tutorials on django 1.4 or 1.5? Message-ID: Who can give me some practical tutorials on django 1.4 or 1.5? Thank you. -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Sun Nov 4 02:58:36 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 04 Nov 2012 07:58:36 GMT Subject: Proper place for everything References: <509441cb$0$29967$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5096202c$0$29967$c3e8da3$5496439d@news.astraweb.com> On Sat, 03 Nov 2012 22:19:19 -0700, Aahz wrote: > In article <509441cb$0$29967$c3e8da3$5496439d at news.astraweb.com>, Steven > D'Aprano wrote: >>On Fri, 02 Nov 2012 04:20:20 -0700, Jason Benjamin wrote: >>> >>> Anybody know of the appropriate place to troll and flame about various >>> Python related issues? I'm kind of mad about some Python stuff and I >>> need a place to vent where people may or may not listen, but at at >>> least respond. Thought this would be a strange question, but I might >>> as well start somewhere. >> >>Thank you for your honesty, but trolling is not welcome. >> >>However if you have actual issues about Python, either pro or con, and >>hope to have a serious, respectful dialog where both parties listen to >>each other, feel free to raise them here. Keep in mind three things: [snip three things] > You forgot the fourth point. Apparently so did you :) -- Steven From dihedral88888 at googlemail.com Sun Nov 4 03:33:16 2012 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sun, 4 Nov 2012 01:33:16 -0700 (PDT) Subject: is implemented with id ? In-Reply-To: <5047648f$0$29981$c3e8da3$5496439d@news.astraweb.com> References: <5047648f$0$29981$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Wednesday, September 5, 2012 10:41:19 PM UTC+8, Steven D'Aprano wrote: > On Wed, 05 Sep 2012 10:00:09 -0400, Dave Angel wrote: > > > > > On 09/05/2012 09:19 AM, Franck Ditter wrote: > > >> Thanks to all, but : > > >> - I should have said that I work with Python 3. Does that matter ? - > > >> May I reformulate the queston : "a is b" and "id(a) == id(b)" > > >> both mean : "a et b share the same physical address". Is that True ? > > >> Thanks, > > > > > > No, id() has nothing to do with physical address. The Python language > > > does not specify anything about physical addresses. Some > > > implementations may happen to use physical addresses, others arbitrary > > > integers. And they may reuse such integers, or not. Up to the > > > implementation. > > > > True. In principle, some day there might be a version of Python that runs > > on some exotic quantum computer where the very concept of "physical > > address" is meaningless. Or some sort of peptide or DNA computer, where > > the calculations are performed via molecular interactions rather than by > > flipping bits in fixed memory locations. > > > > But less exotically, Frank isn't entirely wrong. With current day > > computers, it is reasonable to say that any object has exactly one > > physical location at any time. In Jython, objects can move around; in > > CPython, they can't. But at any moment, any object has a specific > > location, and no other object can have that same location. Two objects > > cannot both be at the same memory address at the same time. > > > > So, for current day computers at least, it is reasonable to say that > > "a is b" implies that a and b are the same object at a single location. > > > > The second half of the question is more complex: > > > > "id(a) == id(b)" *only* implies that a and b are the same object at the > > same location if they exist at the same time. If they don't exist at the > > same time, then you can't conclude anything. > > > > > > > > -- > > Steven The function id(x) might not be implemented as an address in the user space. Do we need to distinguish archived objets and objects in the memory? From hansmu at xs4all.nl Sun Nov 4 05:13:07 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Sun, 04 Nov 2012 11:13:07 +0100 Subject: is implemented with id ? In-Reply-To: References: <5095dca0$0$29967$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50963fb4$0$6947$e4fe514c@news2.news.xs4all.nl> On 4/11/12 06:09:24, Aahz wrote: > In article , > Chris Angelico wrote: >> On Sun, Nov 4, 2012 at 2:10 PM, Steven D'Aprano >> wrote: >>> >>> /* Shortcut for empty or interned objects */ >>> if (v == u) { >>> Py_DECREF(u); >>> Py_DECREF(v); >>> return 0; >>> } >>> result = unicode_compare(u, v); >>> >>> where v and u are pointers to the unicode object. >> >> There's a shortcut if they're the same. There's no shortcut if they're >> both interned and have different pointers, which is a guarantee that >> they're distinct strings. They'll still be compared char-for-char >> until there's a difference. > > Without looking at the code, I'm pretty sure there's a hash check first. In 3.3, there is no such check. It was recently proposed on python-dev to add such a check, but AFAIK, no action was taken. -- HansM From jamie at kode5.net Sun Nov 4 06:13:29 2012 From: jamie at kode5.net (Jamie Paul Griffin) Date: Sun, 4 Nov 2012 11:13:29 +0000 Subject: Obnoxious postings from Google Groups In-Reply-To: References: <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> Message-ID: <20121104111329.GA2074@kontrol.kode5.net> / rurpy at yahoo.com wrote on Fri 2.Nov'12 at 11:39:10 -0700 / > (I also hope I haven't just been suckered by a troll > attempt, windows/unix is better then unix/windows being > an age-old means of trolling.) No, i'm not a "troll". I was just adding my opinion to the thread, I assumed that was allowed. I didn't say UNIX is better than Windows, did I; I just feel that Windows is not -- for me anyway -- the most suitable plaform for learning about the science of computing and coding, etc... being a computer science student that's the view i have and share with those I learn with and from. Why must people be accused of trolling everytime they make a statement that conveys a preference over one platform or language, for example, than the other. Provoking someone by labeling them a troll or implying they might be is a bit childish really. From roy at panix.com Sun Nov 4 06:17:42 2012 From: roy at panix.com (Roy Smith) Date: Sun, 04 Nov 2012 06:17:42 -0500 Subject: Proper place for everything References: <509441cb$0$29967$c3e8da3$5496439d@news.astraweb.com> <5096202c$0$29967$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <5096202c$0$29967$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > On Sat, 03 Nov 2012 22:19:19 -0700, Aahz wrote: > > > In article <509441cb$0$29967$c3e8da3$5496439d at news.astraweb.com>, Steven > > D'Aprano wrote: > >>On Fri, 02 Nov 2012 04:20:20 -0700, Jason Benjamin wrote: > >>> > >>> Anybody know of the appropriate place to troll and flame about various > >>> Python related issues? I'm kind of mad about some Python stuff and I > >>> need a place to vent where people may or may not listen, but at at > >>> least respond. Thought this would be a strange question, but I might > >>> as well start somewhere. > >> > >>Thank you for your honesty, but trolling is not welcome. > >> > >>However if you have actual issues about Python, either pro or con, and > >>hope to have a serious, respectful dialog where both parties listen to > >>each other, feel free to raise them here. Keep in mind three things: > > [snip three things] > > > You forgot the fourth point. > > Apparently so did you :) "Amongst the points are such diverse elements as..." From doomster at knuut.de Sun Nov 4 06:57:56 2012 From: doomster at knuut.de (Ulrich Eckhardt) Date: Sun, 04 Nov 2012 12:57:56 +0100 Subject: surprising += for lists Message-ID: Hi everybody! I was just smacked by some very surprising Python 2.7 behaviour. I was assembling some 2D points into a list: points = [] points += (3, 5) points += (4, 6) What I would have expected is to have [(3, 5), (4, 6)], instead I got [3, 5, 4, 6]. My interpretations thereof is that the tuple (x, y) is iterable, so the elements are appended one after the other. Actually, I should have used points.append(), but that's a different issue. Now, what really struck me was the fact that [] + (3, 5) will give me a type error. Here I wonder why the augmented assignment behaves so much different. Can anyone help me understand this? Thanks! Uli From alec.taylor6 at gmail.com Sun Nov 4 07:06:03 2012 From: alec.taylor6 at gmail.com (Alec Taylor) Date: Sun, 4 Nov 2012 23:06:03 +1100 Subject: surprising += for lists In-Reply-To: References: Message-ID: Quick aside, you can insert tuples without much effort: `points += ((3,5),)` And also that I can't do the reverse, i.e.: >>> foo = tuple() >>> foo += [5,6] Traceback (most recent call last): File "", line 1, in TypeError: can only concatenate tuple (not "list") to tuple On Sun, Nov 4, 2012 at 10:57 PM, Ulrich Eckhardt wrote: > Hi everybody! > > I was just smacked by some very surprising Python 2.7 behaviour. I was > assembling some 2D points into a list: > > points = [] > points += (3, 5) > points += (4, 6) > > What I would have expected is to have [(3, 5), (4, 6)], instead I got [3, > 5, 4, 6]. My interpretations thereof is that the tuple (x, y) is iterable, > so the elements are appended one after the other. Actually, I should have > used points.append(), but that's a different issue. > > Now, what really struck me was the fact that [] + (3, 5) will give me a > type error. Here I wonder why the augmented assignment behaves so much > different. > > Can anyone help me understand this? > > Thanks! > > Uli > > > -- > http://mail.python.org/mailman/listinfo/python-list From marduk at letterboxes.org Sun Nov 4 07:35:17 2012 From: marduk at letterboxes.org (Albert Hopkins) Date: Sun, 04 Nov 2012 07:35:17 -0500 Subject: who can give me some practical tutorials on django 1.4 or 1.5? In-Reply-To: References: Message-ID: <1352032517.5003.0.camel@localhost> On Sun, 2012-11-04 at 13:29 +0800, Levi Nie wrote: > Who can give me some practical tutorials on django 1.4 or 1.5? > Thank you. Is the official[1] tutorial not practical enough? [1] https://docs.djangoproject.com/en/1.4/intro/tutorial01/ From d at davea.name Sun Nov 4 07:45:45 2012 From: d at davea.name (Dave Angel) Date: Sun, 04 Nov 2012 07:45:45 -0500 Subject: surprising += for lists In-Reply-To: References: Message-ID: <50966379.3070801@davea.name> On 11/04/2012 06:57 AM, Ulrich Eckhardt wrote: > Hi everybody! > > I was just smacked by some very surprising Python 2.7 behaviour. I was > assembling some 2D points into a list: > > points = [] > points += (3, 5) > points += (4, 6) > > What I would have expected is to have [(3, 5), (4, 6)], instead I got [3, > 5, 4, 6]. mylist += is equivalent to mylist.extend. And as you say, what you wanted was append. > My interpretations thereof is that the tuple (x, y) is iterable, You're confusing cause and effect. If it weren't iterable, it'd be an error. It would NOT just somehow change to be equivalent to append. >>> points.extend(4) Traceback (most recent call last): File "", line 1, in TypeError: 'int' object is not iterable > so the elements are appended one after the other. Actually, I should have > used points.append(), but that's a different issue. > > Now, what really struck me was the fact that [] + (3, 5) will give me a > type error. Here I wonder why the augmented assignment behaves so much > different. What I wonder about is why list's __add__ is so fussy. > Can anyone help me understand this? > > Thanks! > > Uli > > I'd also point out that when using the extend() function call, we'd have to spell it: points.extend((3,5)) The extra parentheses are to make it clear to the compiler that this is a single argument, a tuple, and not two arguments. And similarly, points.append((3,5)) to get your original desired behavior. -- DaveA From roy at panix.com Sun Nov 4 08:32:18 2012 From: roy at panix.com (Roy Smith) Date: Sun, 04 Nov 2012 08:32:18 -0500 Subject: __unicode__() works, unicode() blows up. Message-ID: Environment: Python-2.7.3 Ubuntu Precise mongoengine 0.6.20 I have a class which includes a __unicode__() method: class User(mongoengine.Document): def __unicode__(self): return self.username If I create an instance of this class by calling the constructor directly, self.username is None. When I pass that to unicode(), it blows up. However, calling __unicode__() directly, works as expected: >>> u = User() >>> print u.username None >>> print u.__unicode__() None >>> print unicode(u) Traceback (most recent call last): File "", line 1, in TypeError: coercing to Unicode: need string or buffer, NoneType found What's going on here? I thought (http://docs.python.org/2/library/functions.html#unicode) the latter two calls should be identical, but obviously they're not. From roy at panix.com Sun Nov 4 08:41:08 2012 From: roy at panix.com (Roy Smith) Date: Sun, 04 Nov 2012 08:41:08 -0500 Subject: __unicode__() works, unicode() blows up. (Never mind!) References: Message-ID: In article , Roy Smith wrote: > >>> print u.__unicode__() > None > > >>> print unicode(u) > Traceback (most recent call last): > File "", line 1, in > TypeError: coercing to Unicode: need string or buffer, NoneType found > > What's going on here? I thought > (http://docs.python.org/2/library/functions.html#unicode) the latter two > calls should be identical, but obviously they're not. Why is it, that no matter how long you stare at a problem, the answer comes to you moments after you hit the Post button? :-) The problem is that __unicode__() is supposed to return a Unicode object, and unicode() enforces that. The fix is to change: def __unicode__(self): return self.username to be: def __unicode__(self): return unicode(self.username) This never got noticed before because normally, self.username already is a unicode string, so it just works. From giacomo.alzetta at gmail.com Sun Nov 4 09:42:38 2012 From: giacomo.alzetta at gmail.com (Giacomo Alzetta) Date: Sun, 4 Nov 2012 06:42:38 -0800 (PST) Subject: Missing modules compiling python3.3 Message-ID: I'm trying to compile python3.3 on my (K)ubuntu 12.04, but some modules are missing. In particular when doing make test I get: Python build finished, but the necessary bits to build these modules were not found: _bz2 _curses _curses_panel _dbm _gdbm _lzma _sqlite3 _tkinter readline To find the necessary bits, look in setup.py in detect_modules() for the module's name. And also the "test_urlwithfrag" test fails, but when trying to do(as suggested in the README) "./python -m test -v test_urlwithfrag" I get an error[ImportError: No module named 'test.test_urlwithfrag' ] and when doing "./python -m test -v test_urllib2net" it skips the test saying it's normal on linux(then why make test runs it and it fails???) What am I missing? Should I install those modules manually? Is this expected? From none at mailinator.com Sun Nov 4 09:56:20 2012 From: none at mailinator.com (mm0fmf) Date: Sun, 04 Nov 2012 14:56:20 +0000 Subject: Missing modules compiling python3.3 In-Reply-To: References: Message-ID: <6mvls.146238$Tf3.46577@fx12.am4> Giacomo Alzetta wrote: > I'm trying to compile python3.3 on my (K)ubuntu 12.04, but some modules are missing. > > In particular when doing make test I get: > > Python build finished, but the necessary bits to build these modules were not found: > _bz2 _curses _curses_panel You haven't installed the development headers for those modules giving errors. So for curses you'll need to install libncurses5-dev, lzma-dev etc. Sorry, I can't remember the package names as it's a while since I did this. Andy From christian at python.org Sun Nov 4 09:57:13 2012 From: christian at python.org (Christian Heimes) Date: Sun, 04 Nov 2012 15:57:13 +0100 Subject: Missing modules compiling python3.3 In-Reply-To: References: Message-ID: Am 04.11.2012 15:42, schrieb Giacomo Alzetta: > I'm trying to compile python3.3 on my (K)ubuntu 12.04, but some modules are missing. > > In particular when doing make test I get: > > Python build finished, but the necessary bits to build these modules were not found: > _bz2 _curses _curses_panel > _dbm _gdbm _lzma > _sqlite3 _tkinter readline > To find the necessary bits, look in setup.py in detect_modules() for the module's name. Hello Giacomo, your installation is lacking a couple of dependencies and header files. It's very easy to install the dependencies on Ubuntu: sudo apt-get build-dep python3.2 sudo apt-get install liblzma-dev HTH Christian From joshua.landau.ws at gmail.com Sun Nov 4 10:04:15 2012 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Sun, 4 Nov 2012 15:04:15 +0000 Subject: __unicode__() works, unicode() blows up. In-Reply-To: References: Message-ID: On 4 November 2012 13:32, Roy Smith wrote: > Environment: > Python-2.7.3 > Ubuntu Precise > mongoengine 0.6.20 > > I have a class which includes a __unicode__() method: > > class User(mongoengine.Document): > def __unicode__(self): > return self.username > > If I create an instance of this class by calling the constructor > directly, self.username is None. When I pass that to unicode(), it > blows up. However, calling __unicode__() directly, works as expected: > > >>> u = User() > >>> print u.username > None > > >>> print u.__unicode__() > None > > >>> print unicode(u) > Traceback (most recent call last): > File "", line 1, in > TypeError: coercing to Unicode: need string or buffer, NoneType found > > What's going on here? I thought > (http://docs.python.org/2/library/functions.html#unicode) the latter two > calls should be identical, but obviously they're not. >>> class Foo: ... def __unicode__(self): return "Bar" # NOT Unicode ... >>> Foo().__unicode__() 'Bar' >>> unicode(Foo()) u'Bar' unicode(x) calls x.__unicode__() *and then* coerces the result to Unicode. None cannot be coerced to Unicode. -------------- next part -------------- An HTML attachment was scrubbed... URL: From vs at it.uu.se Sun Nov 4 11:52:13 2012 From: vs at it.uu.se (Virgil Stokes) Date: Sun, 04 Nov 2012 17:52:13 +0100 Subject: Obnoxious postings from Google Groups In-Reply-To: <20121104111329.GA2074@kontrol.kode5.net> References: <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> <20121104111329.GA2074@kontrol.kode5.net> Message-ID: <50969D3D.8060702@it.uu.se> On 04-Nov-2012 12:13, Jamie Paul Griffin wrote: > / rurpy at yahoo.com wrote on Fri 2.Nov'12 at 11:39:10 -0700 / > >> (I also hope I haven't just been suckered by a troll >> attempt, windows/unix is better then unix/windows being >> an age-old means of trolling.) > No, i'm not a "troll". I was just adding my opinion to the thread, I assumed that was allowed. I didn't say UNIX is better than Windows, did I; I just feel that Windows is not -- for me anyway -- the most suitable plaform for learning about the science of computing and coding, etc... being a computer science student that's the view i have and share with those I learn with and from. Why must people be accused of trolling everytime they make a statement that conveys a preference over one platform or language, for example, than the other. Provoking someone by labeling them a troll or implying they might be is a bit childish really. Well stated Jamie --- I agree. I don't believe that all members of this list label you as a troll. --V From aahz at pythoncraft.com Sun Nov 4 12:13:47 2012 From: aahz at pythoncraft.com (Aahz) Date: 4 Nov 2012 09:13:47 -0800 Subject: __unicode__() works, unicode() blows up. (Never mind!) References: Message-ID: In article , Roy Smith wrote: >In article , > Roy Smith wrote: >> >> >>> print u.__unicode__() >> None >> >> >>> print unicode(u) >> Traceback (most recent call last): >> File "", line 1, in >> TypeError: coercing to Unicode: need string or buffer, NoneType found >> >> What's going on here? I thought >> (http://docs.python.org/2/library/functions.html#unicode) the latter two >> calls should be identical, but obviously they're not. > >Why is it, that no matter how long you stare at a problem, the answer >comes to you moments after you hit the Post button? :-) > >The problem is that __unicode__() is supposed to return a Unicode >object, and unicode() enforces that. The fix is to change: > > def __unicode__(self): > return self.username > >to be: > > def __unicode__(self): > return unicode(self.username) > >This never got noticed before because normally, self.username already is >a unicode string, so it just works. You apparently need more coffee when programming after waking up! (Or even worse, staying up all night.) -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "....Normal is what cuts off your sixth finger and your tail..." --Siobhan From tjreedy at udel.edu Sun Nov 4 12:57:11 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 04 Nov 2012 12:57:11 -0500 Subject: surprising += for lists In-Reply-To: <50966379.3070801@davea.name> References: <50966379.3070801@davea.name> Message-ID: On 11/4/2012 7:45 AM, Dave Angel wrote: > What I wonder about is why list's __add__ is so fussy. Guido's reason is that it is not clear what the types of [1,2] + (3,4), (1,2) + [3,4], [] + range(4), range(2) + [3,4], etcetera should be. Such mixtures may be bugs. Seq.__add__ exists to implement '+'. [].extend() will clearly be a list, and accepts any iterable. I believe the same logic is used for set operations. -- Terry Jan Reedy From tjreedy at udel.edu Sun Nov 4 13:10:55 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 04 Nov 2012 13:10:55 -0500 Subject: __unicode__() works, unicode() blows up. (Never mind!) In-Reply-To: References: Message-ID: On 11/4/2012 8:41 AM, Roy Smith wrote: > In article , > Roy Smith wrote: > >>>>> print u.__unicode__() >> None >> >>>>> print unicode(u) >> Traceback (most recent call last): >> File "", line 1, in >> TypeError: coercing to Unicode: need string or buffer, NoneType found >> >> What's going on here? I thought >> (http://docs.python.org/2/library/functions.html#unicode) the latter two >> calls should be identical, but obviously they're not. > > Why is it, that no matter how long you stare at a problem, the answer > comes to you moments after you hit the Post button? :-) > > The problem is that __unicode__() is supposed to return a Unicode > object, and unicode() enforces that. The fix is to change: > > def __unicode__(self): > return self.username > > to be: > > def __unicode__(self): > return unicode(self.username) > > This never got noticed before because normally, self.username already is > a unicode string, so it just works. The same principle applies to some of the other special methods that sit behind builtin functions. >>> class C: def __len__(self): return '42' # whoops >>> len(C()) Traceback (most recent call last): File "", line 1, in len(C()) TypeError: 'str' object cannot be interpreted as an integer >>> class C: def __len__(self): return -42 # whoops again >>> len(C()) Traceback (most recent call last): File "", line 1, in len(C()) ValueError: __len__() should return >= 0 -- Terry Jan Reedy From tjreedy at udel.edu Sun Nov 4 13:17:09 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 04 Nov 2012 13:17:09 -0500 Subject: who can give me some practical tutorials on django 1.4 or 1.5? In-Reply-To: <1352032517.5003.0.camel@localhost> References: <1352032517.5003.0.camel@localhost> Message-ID: On 11/4/2012 7:35 AM, Albert Hopkins wrote: > On Sun, 2012-11-04 at 13:29 +0800, Levi Nie wrote: >> Who can give me some practical tutorials on django 1.4 or 1.5? >> Thank you. > > Is the official[1] tutorial not practical enough? > > [1] https://docs.djangoproject.com/en/1.4/intro/tutorial01/ There is also django-users list https://www.djangoproject.com/community/ -- Terry Jan Reedy From jeff.jeffries.iii at gmail.com Sun Nov 4 13:29:53 2012 From: jeff.jeffries.iii at gmail.com (Jeff Jeffries) Date: Sun, 4 Nov 2012 13:29:53 -0500 Subject: Proper place for everything In-Reply-To: References: <509441cb$0$29967$c3e8da3$5496439d@news.astraweb.com> <5096202c$0$29967$c3e8da3$5496439d@news.astraweb.com> Message-ID: everyone on this list is troll On Sun, Nov 4, 2012 at 6:17 AM, Roy Smith wrote: > In article <5096202c$0$29967$c3e8da3$5496439d at news.astraweb.com>, > Steven D'Aprano wrote: > > > On Sat, 03 Nov 2012 22:19:19 -0700, Aahz wrote: > > > > > In article <509441cb$0$29967$c3e8da3$5496439d at news.astraweb.com>, > Steven > > > D'Aprano wrote: > > >>On Fri, 02 Nov 2012 04:20:20 -0700, Jason Benjamin wrote: > > >>> > > >>> Anybody know of the appropriate place to troll and flame about > various > > >>> Python related issues? I'm kind of mad about some Python stuff and I > > >>> need a place to vent where people may or may not listen, but at at > > >>> least respond. Thought this would be a strange question, but I might > > >>> as well start somewhere. > > >> > > >>Thank you for your honesty, but trolling is not welcome. > > >> > > >>However if you have actual issues about Python, either pro or con, and > > >>hope to have a serious, respectful dialog where both parties listen to > > >>each other, feel free to raise them here. Keep in mind three things: > > > > [snip three things] > > > > > You forgot the fourth point. > > > > Apparently so did you :) > > "Amongst the points are such diverse elements as..." > -- > http://mail.python.org/mailman/listinfo/python-list > -- Cheers, Jeff Jeffries III CEO: www.willyoubemyfriend.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Sun Nov 4 13:39:06 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 04 Nov 2012 18:39:06 +0000 Subject: Obnoxious postings from Google Groups In-Reply-To: <20121101095520.GF19327@kontrol.kode5.net> References: <8370412f-5176-45ff-87eb-72cbca5149d5@googlegroups.com> <5055a1a6$0$29981$c3e8da3$5496439d@news.astraweb.com> <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> <20121101095520.GF19327@kontrol.kode5.net> Message-ID: On 01/11/2012 09:55, Jamie Paul Griffin wrote: > / Robert Miles wrote on Wed 31.Oct'12 at 0:39:02 -0500 / > >> For those of you running Linux: You may want to look into whether >> NoCeM is compatible with your newsreader and your version of Linux. >> It checks newsgroups news.lists.filters and alt.nocem.misc for lists >> of spam posts, and will automatically hide them for you. Not available >> for other operating systems, though, except possibly Unix. > > Anybody serious about programming should be using a form of UNIX/Linux if you ask me. It's inconceivable that these systems should be avoided if you're serious about Software Engineering and Computer Science, etc. For UNIX there are loads of decent news reading software and mail user agents to learn and use. slrn is a good one and point it at gmane.org as someone else pointed out. I can't even imagine using a browser or Google Groups, etc. now. > Anybody serious about programming should know that an OS is a combination of the hardware and software. Can the *Nix variants now do proper clustering or are they still decades behind VMS? Never used the other main/mini frame systems myself but perhaps they are still vastly superior to this highly overrated *Nix crap. -- Cheers. Mark Lawrence. From jdgleeson at mac.com Sun Nov 4 14:10:14 2012 From: jdgleeson at mac.com (John Gleeson) Date: Sun, 04 Nov 2012 12:10:14 -0700 Subject: install pyOpenSSL in python2.7 In-Reply-To: References: Message-ID: <57A6E632-8F97-4ED8-8843-431EB81D7446@mac.com> On 2012-11-03, at 2:58 AM, ???? wrote: > i have install pyOpenSSL-0.11 in python2.7 this way: > download pyOpenSSL-0.11.tar.gz > #tar -zvxf pyOpenSSL-0.11.tar.gz > #cd pyOpenSSL-0.11 > #python setup.py install > > >>> import OpenSSL > Traceback (most recent call last): > File "", line 1, in > File "/usr/local/lib/python2.7/dist-packages/OpenSSL/__init__.py", > line 45, in > from OpenSSL import rand, SSL > ImportError: /usr/local/lib/python2.7/dist-packages/OpenSSL/SSL.so: > undefined symbol: SSLv2_method > > > how can i fix the problem? > ImportError: /usr/local/lib/python2.7/dist-packages/OpenSSL/SSL.so: > undefined symbol: SSLv2_method > > -- > http://mail.python.org/mailman/listinfo/python-list The pyOpenSSL home page at https://launchpad.net/pyopenssl says that the latest version is 0.11, but that is not true. The latest version is 0.13. You can get it on PyPI here http://pypi.python.org/pypi/pyOpenSSL SSLv2 is no longer supported in OpenSSL, and version 0.13 no longer expects it. - John From ian.g.kelly at gmail.com Sun Nov 4 14:15:58 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sun, 4 Nov 2012 12:15:58 -0700 Subject: Obnoxious postings from Google Groups In-Reply-To: References: <8370412f-5176-45ff-87eb-72cbca5149d5@googlegroups.com> <5055a1a6$0$29981$c3e8da3$5496439d@news.astraweb.com> <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> <20121101095520.GF19327@kontrol.kode5.net> Message-ID: On Sun, Nov 4, 2012 at 11:39 AM, Mark Lawrence wrote: > Anybody serious about programming should know that an OS is a combination > of the hardware and software. Can the *Nix variants now do proper > clustering or are they still decades behind VMS? Never used the other > main/mini frame systems myself but perhaps they are still vastly superior > to this highly overrated *Nix crap. > What relevance does clustering have for a desktop workstation OS? -------------- next part -------------- An HTML attachment was scrubbed... URL: From giacomo.alzetta at gmail.com Sun Nov 4 15:31:36 2012 From: giacomo.alzetta at gmail.com (Giacomo Alzetta) Date: Sun, 4 Nov 2012 12:31:36 -0800 (PST) Subject: Missing modules compiling python3.3 In-Reply-To: <6mvls.146238$Tf3.46577@fx12.am4> References: <6mvls.146238$Tf3.46577@fx12.am4> Message-ID: Il giorno domenica 4 novembre 2012 15:56:03 UTC+1, mm0fmf ha scritto: > Giacomo Alzetta wrote: > > > I'm trying to compile python3.3 on my (K)ubuntu 12.04, but some modules are missing. > > > > > > In particular when doing make test I get: > > > > > > Python build finished, but the necessary bits to build these modules were not found: > > > _bz2 _curses _curses_panel > > > > You haven't installed the development headers for those modules giving > > errors. > > > > So for curses you'll need to install libncurses5-dev, lzma-dev etc. > > Sorry, I can't remember the package names as it's a while since I did this. > > > > Andy That's right! Sorry, but I thought I installed those some months ago for an other python installation, but probably I've also removed them :s The "test_urlwithfrag" is still failing though. From vasudevram at gmail.com Sun Nov 4 19:24:59 2012 From: vasudevram at gmail.com (vasudevram) Date: Sun, 4 Nov 2012 16:24:59 -0800 (PST) Subject: PDFBuilder can now take multiple input files from command line Message-ID: <2d3c895d-eae3-4a5d-bca7-af3aabca7895@googlegroups.com> Here is the blog post about it: http://jugad2.blogspot.in/2012/11/pdfbuilder-can-now-take-multiple-input.html In short: removed the temporary hard-coding, refactored the code some. PDFBuilder can now use multiple input files (of type .csv / .tdv), specified on the command-line, to create a composite PDF from those inputs. .tdv = Tab Delimited Values, such as commonly used in UNIX tools like sed / grep / awk and friends. - Vasudev Ram www.dancingbison.com From bkuberek at gmail.com Sun Nov 4 19:45:53 2012 From: bkuberek at gmail.com (bkuberek at gmail.com) Date: Sun, 4 Nov 2012 16:45:53 -0800 (PST) Subject: No more Python support in NetBeans 7.0 In-Reply-To: <4a98.4d8b560e.5ebee@altium.nl> References: <4a98.4d8b560e.5ebee@altium.nl> Message-ID: On Thursday, March 24, 2011 10:32:44 AM UTC-4, Kees Bakker wrote: > Hi, > > Sad news (for me, at least), in the upcoming version 7.0 of NetBeans > there will be no Python plugin anymore. > > I have been using NetBeans for Python development for a while now > and I was very happy with it. > > See this archive for details: > http://netbeans.org/projects/www/lists/nbpython-dev/archive/2010-11/message/0 > http://netbeans.org/projects/www/lists/nbpython-dev/archive/2011-01/message/0 > -- > Kees It is sad indeed. I would have preferred to use netbeans for all my coding and now I have to use both netbeans and a python IDE. I have been using PyCharm for a year now and I am very happy with it. It is the best I have used for python development with django, html, javascript and css support as well. I recommend trying it out. However I am not happy about having to use different IDEs as I find myself coding in both python and php from project to project. Jetbrains IDEA Ultimate edition has both php and python support but is way out of my budget. From davidwong.xc at gmail.com Mon Nov 5 00:53:56 2012 From: davidwong.xc at gmail.com (David Wong) Date: Sun, 4 Nov 2012 21:53:56 -0800 (PST) Subject: Web application for drawing directed graphs from the output of Python's cProfile Message-ID: I recently built startgraphi.com. It's a web application that draws directed graphs of running times and function calls from the output of Python's cProfile. It also creates a sortable table of running times and function calls. I hope someone finds it useful. From rustompmody at gmail.com Mon Nov 5 01:10:16 2012 From: rustompmody at gmail.com (rusi) Date: Sun, 4 Nov 2012 22:10:16 -0800 (PST) Subject: Obnoxious postings from Google Groups References: <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> Message-ID: On Nov 4, 4:14?pm, Jamie Paul Griffin wrote: > / ru... at yahoo.com wrote on Fri ?2.Nov'12 at 11:39:10 -0700 / > > > (I also hope I haven't just been suckered by a troll > > attempt, windows/unix is better then unix/windows being > > an age-old means of trolling.) > > No, i'm not a "troll". I was just adding my opinion to the thread, I assumed that was allowed. I didn't say UNIX is better than Windows, did I; I just feel that Windows is not -- for me anyway -- the most suitable plaform for learning about the science of computing and coding, etc... being a computer science student that's the view i have and share with those I learn with and from. Why must people be accused of trolling everytime they make a statement that conveys a preference over one platform or language, for example, than the other. Provoking someone by labeling them a troll or implying they might be is a bit childish really. Hi Jamie Among people who know me, I am a linux nerd: My sister scolded me yesterday because I put files on her computer without spaces: DoesAnyoneWriteLikeThis?!?! Your post reminds me: As someone who has taught CS for 25 years, Ive not only been party to his Unix-fanboy viewpoint but have even actively fostered it. Over time Ive come to have some pangs of conscience about this. Evidently this kind of attitude has helped no one: not my students, not the corporations they join, not the society at large. So now, on my blog I maintain a record of the foibles of CS academics. http://blog.languager.org/2011/02/cs-education-is-fat-and-weak-1.html is a history of CS as it is normally given. http://blog.languager.org/2011/02/cs-education-is-fat-and-weak-2.html is the above deconstructed with stupidities of academic CS factored in. From demianbrecht at gmail.com Mon Nov 5 01:27:52 2012 From: demianbrecht at gmail.com (Demian Brecht) Date: Sun, 4 Nov 2012 22:27:52 -0800 Subject: Multi-dimensional list initialization Message-ID: So, here I was thinking "oh, this is a nice, easy way to initialize a 4D matrix" (running 2.7.3, non-core libs not allowed): m = [[None] * 4] * 4 The way to get what I was after was: m = [[None] * 4, [None] * 4, [None] * 4, [None * 4]] (Obviously, I could have just hardcoded the initialization, but I'm too lazy to type all that out ;)) The behaviour I encountered seems a little contradictory to me. [None] * 4 creates four distinct elements in a single array while [[None] * 4] * 4 creates one distinct array of four distinct elements, with three references to it: >>> a = [None] * 4 >>> a[0] = 'a' >>> a ['a', None, None, None] >>> m = [[None] * 4] * 4 >>> m[0][0] = 'm' >>> m [['m', None, None, None], ['m', None, None, None], ['m', None, None, None], ['m', None, None, None]] Is this expected behaviour and if so, why? In my mind either result makes sense, but the inconsistency is what throws me off. Demian Brecht @demianbrecht http://demianbrecht.github.com From demianbrecht at gmail.com Mon Nov 5 01:29:49 2012 From: demianbrecht at gmail.com (Demian Brecht) Date: Sun, 4 Nov 2012 22:29:49 -0800 Subject: No more Python support in NetBeans 7.0 In-Reply-To: References: <4a98.4d8b560e.5ebee@altium.nl> Message-ID: <90EA1038-276F-4CFC-9DBD-22EA221B3365@gmail.com> On 2012-11-04, at 4:45 PM, bkuberek at gmail.com wrote: > However I am not happy about having to use different IDEs as I find myself coding in both python and php from project to project. One of the many reasons Vim is my editor of choice. Demian Brecht @demianbrecht http://demianbrecht.github.com From rosuav at gmail.com Mon Nov 5 01:39:35 2012 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 5 Nov 2012 17:39:35 +1100 Subject: Obnoxious postings from Google Groups In-Reply-To: References: <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> Message-ID: On Mon, Nov 5, 2012 at 5:10 PM, rusi wrote: > Among people who know me, I am a linux nerd: My sister scolded me > yesterday because I put files on her computer without spaces: > DoesAnyoneWriteLikeThis?!?! My filenames seldom have spaces in them, but that has nothing to do with how I write English. Names are names. They're not essays, they are not written as full sentences. When a name contains spaces, it must be delimited (or the space must be escaped, if your environment permits) any time it occurs inside some other context - most commonly, as a command-line argument. Back when I was using MS-DOS 5, it was possible to have file names with spaces. It wasn't easy to manipulate them from the command line, but you could reference them using globs (eg replace the space(s) with ? and hope that there are no false hits). OS/2, when working on a FAT filesystem, would create files called "EA DATA. SF" or "WP ROOT. SF" or "WP SHARE. SF" (two spaces in each), and most DOS/Windows programs wouldn't (couldn't) touch them - they were safe repositories for system metadata (on smarter filesystems, that sort of thing would be stored as file attributes, not as separate files). It's nothing to do with operating system. File names are names, and spaces in them are seldom worth the hassle unless you manipulate those files solely using a GUI. ChrisA From andrew3 at r3dsolutions.com Mon Nov 5 01:44:39 2012 From: andrew3 at r3dsolutions.com (Andrew Robinson) Date: Sun, 04 Nov 2012 22:44:39 -0800 Subject: Multi-dimensional list initialization In-Reply-To: References: Message-ID: <50976057.6080007@r3dsolutions.com> On 11/04/2012 10:27 PM, Demian Brecht wrote: > So, here I was thinking "oh, this is a nice, easy way to initialize a 4D matrix" (running 2.7.3, non-core libs not allowed): > > m = [[None] * 4] * 4 > > The way to get what I was after was: > > m = [[None] * 4, [None] * 4, [None] * 4, [None * 4]] FYI: The behavior is the same in python 3.2 m=[[None]*4]*4 produces a nested list with all references being to the first instance of the inner list construction. I agree, the result is very counter-intuitive; hmmm... but I think you meant: m = [[None] * 4, [None] * 4, [None] * 4, [None] *4 ] rather than: m = [[None] * 4, [None] * 4, [None] * 4, [None * 4]] ? :) ? I asked a why question on another thread, and watched several dodges to the main question; I'll be watching to see if you get anything other than "That's the way it's defined in the API". IMHO -- that's not a real answer. My guess is that the original implementation never considered anything beyond a 1d list. :) A more precise related question might be: is there a way to force the replication operator to use copying rather than referencing? :/ From ferencikioan at gmail.com Mon Nov 5 01:51:00 2012 From: ferencikioan at gmail.com (Ferencik Ioan) Date: Sun, 4 Nov 2012 22:51:00 -0800 (PST) Subject: python destructor Message-ID: Hello there folks, I have a bit of a special issue. I'll start by disclosing myself for what i am doing. I am a postgraduate student and I really have good reasons to do what I am doing. At least i think so. And not the issue. I am building a python web service. This web service has some generic objects and I use a metaclass to customize the classes. Second I use a non-conventional object oriented database, dybase (http://www.garret.ru/dybase/doc/dybase.html#introduction) Now these is a OODBMS claiming to support ACID transactions. The instances of my objects are recursively organizing themselves into a hierarchical tree-like structure. When I make an instance of this object persistent dybase actually can recursively save all tree structure. Everything works well here. I altered the main class situated at the root of my class hierarchy to actually store inside the__dict__ not the instances of its children but their unique ID's. Then when I set a child attribute I create it and instead of being stored in the instance the child goes to a database index object. Thus it becomes Universally addressable. The a parent retrieves the child it actually fetches it from the database. In this way I ended up with very small objects.However these objects can regenerate the treelike structure as if they were storing there children in the __dict__. The issue is how to give the instances access to the database and properly handle the opening and closing of the database. It seems futile to me to actually open/close the connection through a context. Because the database is a file it will issue an IO operation on every attribute access and we all know __getattribute__ is used extremely often. For this reason I thought the best way would be to wrap the dybase Storage (main class) into a local storage version which would have __del__ method. The local Storage is a new style class..it opens the DB file but the __del__ is never called. This is because the Storage class has at least 2 cyclic references. So my Storage class never closes the database. I would like this class to close the database when it is garbage collected. The class is a Singleton FYI as well but this might not be relevant or even necessary. So my question is: what s the best way to force __del__ on a singleton that has cyclic references. Should i use weakref and alter the original source? Is there a way i can force a singleton to garbage collect itself?. I am by no means a software engineer so i would appreciate any advice from some experts on the matter. Thank you in advance. From clp2 at rebertia.com Mon Nov 5 02:07:07 2012 From: clp2 at rebertia.com (Chris Rebert) Date: Sun, 4 Nov 2012 23:07:07 -0800 Subject: Multi-dimensional list initialization In-Reply-To: References: Message-ID: On Sun, Nov 4, 2012 at 10:27 PM, Demian Brecht wrote: > So, here I was thinking "oh, this is a nice, easy way to initialize a 4D matrix" (running 2.7.3, non-core libs not allowed): > > m = [[None] * 4] * 4 > > The way to get what I was after was: > > m = [[None] * 4, [None] * 4, [None] * 4, [None * 4]] > > (Obviously, I could have just hardcoded the initialization, but I'm too lazy to type all that out ;)) > > The behaviour I encountered seems a little contradictory to me. > [None] * 4 creates four distinct elements in a single array > while [[None] * 4] * 4 creates one distinct array of four distinct elements, with three references to it: Incorrect. In /both/ cases, the result is a list of length 4, whose elements are 4 (references to) the exact same object as the original list's element. Put simply, the list multiplication operator never copies objects; it just makes additional references to them. However, unlike a list object (as in your latter example), the object `None` is completely immutable (and what's more, a singleton value), so you just-so-happen *not to be able to* run into the same problem of mutating an object (assignment to an index of a list constitutes mutation of that list) that is referenced in multiple places, for you cannot mutate None in the first place!: >>> x = None >>> x.a = 42 Traceback (most recent call last): File "", line 1, in AttributeError: 'NoneType' object has no attribute 'a' >>> # it doesn't overload any mutating operators: >>> type(None).__dict__.keys() ['__hash__', '__repr__', '__doc__'] >>> # and it obviously has no instance variables, >>> # so, we can't modify it in any way whatsoever! (Lists, on the other hand, define item assignment, .pop(), .remove(), and a few other mutator methods.) >>>> a = [None] * 4 >>>> a[0] = 'a' >>>> a > ['a', None, None, None] > >>>> m = [[None] * 4] * 4 >>>> m[0][0] = 'm' >>>> m > [['m', None, None, None], ['m', None, None, None], ['m', None, None, None], ['m', None, None, None]] > > Is this expected behavior Yes. It's also a FAQ: http://docs.python.org/2/faq/programming.html#how-do-i-create-a-multidimensional-list > and if so, why? It's a general (albeit AFAIK unstated) principle that Python never copies objects unless you explicitly ask it to. You have encountered one example of this rule in action. > In my mind either result makes sense, but the inconsistency is what throws me off. It is perfectly consistent, once you understand what list multiplication actually does. Cheers, Chris -- http://rebertia.com From rosuav at gmail.com Mon Nov 5 02:27:40 2012 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 5 Nov 2012 18:27:40 +1100 Subject: Multi-dimensional list initialization In-Reply-To: References: Message-ID: On Mon, Nov 5, 2012 at 6:07 PM, Chris Rebert wrote: >>>> x = None >>>> x.a = 42 > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'NoneType' object has no attribute 'a' Python needs a YouGottaBeKiddingMeError for times when you do something utterly insane like this. Attributes of None??!? :) ChrisA From rustompmody at gmail.com Mon Nov 5 02:34:54 2012 From: rustompmody at gmail.com (rusi) Date: Sun, 4 Nov 2012 23:34:54 -0800 (PST) Subject: Obnoxious postings from Google Groups References: <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> Message-ID: <3a3c216b-eb85-4c0c-b775-dd16c66e0b2b@x9g2000pbe.googlegroups.com> On Nov 5, 11:40?am, Chris Angelico wrote: > On Mon, Nov 5, 2012 at 5:10 PM, rusi wrote: > > Among people who know me, I am a linux nerd: My sister scolded me > > yesterday because I put files on her computer without spaces: > > DoesAnyoneWriteLikeThis?!?! > > My filenames seldom have spaces in them, but that has nothing to do > with how I write English. Names are names. They're not essays, they > are not written as full sentences. When a name contains spaces, it > must be delimited (or the space must be escaped, if your environment > permits) any time it occurs inside some other context - most commonly, > as a command-line argument. > > Back when I was using MS-DOS 5, it was possible to have file names > with spaces. It wasn't easy to manipulate them from the command line, > but you could reference them using globs (eg replace the space(s) with > ? and hope that there are no false hits). OS/2, when working on a FAT > filesystem, would create files called "EA DATA. SF" or "WP ROOT. SF" > or "WP SHARE. SF" (two spaces in each), and most DOS/Windows programs > wouldn't (couldn't) touch them - they were safe repositories for > system metadata (on smarter filesystems, that sort of thing would be > stored as file attributes, not as separate files). > > It's nothing to do with operating system. File names are names, and > spaces in them are seldom worth the hassle unless you manipulate those > files solely using a GUI. > > ChrisA So you and I (and probably many on this list) agree! From rosuav at gmail.com Mon Nov 5 02:36:59 2012 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 5 Nov 2012 18:36:59 +1100 Subject: [Python-ideas] sys.py3k In-Reply-To: <5096ED46.20502@pearwood.info> References: <5096ED46.20502@pearwood.info> Message-ID: On Mon, Nov 5, 2012 at 9:33 AM, Steven D'Aprano wrote: > On 05/11/12 08:49, anatoly techtonik wrote: >> >> if sys.py3k: >> # some py2k specific code >> pass > > # Bring back reload in Python 3. > try: > reload > except NameError: > from imp import reload > > try: > any > except NameError: > # Python 2.4 compatibility. > def any(items): > for item in items: > if item: > return True > return False Take the best of both worlds: try: # py3k reload except NameError: from imp import reload Now you can grep your code for py3k without changing the language! Never underestimate the value of comment tokens. Universal ones like TODO or private ones like NULLSAFE, all it takes is grep or your editor's Find function to make them all obvious. ChrisA From rosuav at gmail.com Mon Nov 5 02:40:22 2012 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 5 Nov 2012 18:40:22 +1100 Subject: No more Python support in NetBeans 7.0 In-Reply-To: <90EA1038-276F-4CFC-9DBD-22EA221B3365@gmail.com> References: <4a98.4d8b560e.5ebee@altium.nl> <90EA1038-276F-4CFC-9DBD-22EA221B3365@gmail.com> Message-ID: On Mon, Nov 5, 2012 at 5:29 PM, Demian Brecht wrote: > > On 2012-11-04, at 4:45 PM, bkuberek at gmail.com wrote: >> However I am not happy about having to use different IDEs as I find myself coding in both python and php from project to project. > > One of the many reasons Vim is my editor of choice. Same here. My IDE needs are very simple. Give me an editor that lets me manipulate multiple files at once, can one-key invoke make, and preferably has syntax highlighting, and I'm happy. So for me, SciTE is my editor and IDE. ChrisA From dieter at handshake.de Mon Nov 5 02:48:43 2012 From: dieter at handshake.de (Dieter Maurer) Date: Mon, 05 Nov 2012 08:48:43 +0100 Subject: Applying a paid third party ssl certificate References: Message-ID: <87ehk8bick.fsf@handshake.de> ehsmenggroups at gmail.com writes: > I haven't quite figured out how to apply a paid ssl cert, say RapidSSL free SSL test from Python's recent sponsor sslmatrix.com and what to do with that to make Python happy. > > This good fellow suggests using the PEM format. I tried and failed. > http://www.minnmyatsoe.com/category/python-2/ > > The self signed cert recepies found all work swell, but some browsers (webkit) gets very upset indeed. I want to use ajax requests from clients (e.g autocompletion, stats collection etc) and put that in a python program without hogging down the main apache stack, but without a proper ssl cert this doesn't work. > > Does anyone have any ideas what to do? >From your description, I derive that you want your client (python program) to autenticate itself via an SSL certificate. If my assumption is correct, I would start with a look at the Python documentation for HTTPS connections. When I remember right, they have 2 optional parameters to specify a client certificate and to specify trusted certificates (when server presented certificates should be verified). Once, you have determined how to present the client certificate for the base HTTPS connection, you may need to look at the documentation or source code of higher level apis (such as "urllib2") to learn how to pass on your certificate down to the real connection. You may also have a look at "PyPI". You may find there packages facilitating Python's "SSL" support. From andrew3 at r3dsolutions.com Mon Nov 5 02:54:49 2012 From: andrew3 at r3dsolutions.com (Andrew Robinson) Date: Sun, 04 Nov 2012 23:54:49 -0800 Subject: Multi-dimensional list initialization In-Reply-To: References: Message-ID: <509770C9.8000003@r3dsolutions.com> On 11/04/2012 11:27 PM, Chris Angelico wrote: > On Mon, Nov 5, 2012 at 6:07 PM, Chris Rebert wrote: >>>>> x = None >>>>> x.a = 42 >> Traceback (most recent call last): >> File "", line 1, in >> AttributeError: 'NoneType' object has no attribute 'a' > Python needs a YouGottaBeKiddingMeError for times when you do > something utterly insane like this. Attributes of None??!? :) > > ChrisA Hmmm? Everything in Python is an object. Therefore! SURE. None *does* have attributes! ( even if not useful ones... ) eg: " None.__getattribute__( "__doc__" ) " doesn't produce an error. In C, in Linux, at the end of the file "errno.h", where all error codes are listed eg:( EIO, EAGAIN, EBUSY, E....) They had a final error like the one you dreamed up, it was called "EIEIO"; and the comment read something like, "All the way around Elmer's barn". :) The poster just hit that strange wall -- *all* built in types are injection proof; and that property is both good and bad... From rosuav at gmail.com Mon Nov 5 03:05:48 2012 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 5 Nov 2012 19:05:48 +1100 Subject: Multi-dimensional list initialization In-Reply-To: <509770C9.8000003@r3dsolutions.com> References: <509770C9.8000003@r3dsolutions.com> Message-ID: On Mon, Nov 5, 2012 at 6:54 PM, Andrew Robinson wrote: > On 11/04/2012 11:27 PM, Chris Angelico wrote: >> >> On Mon, Nov 5, 2012 at 6:07 PM, Chris Rebert wrote: >>>>>> >>>>>> x = None >>>>>> x.a = 42 >>> >>> Traceback (most recent call last): >>> File "", line 1, in >>> AttributeError: 'NoneType' object has no attribute 'a' >> >> Python needs a YouGottaBeKiddingMeError for times when you do >> something utterly insane like this. Attributes of None??!? :) >> >> ChrisA > > Hmmm? Everything in Python is an object. > Therefore! SURE. None *does* have attributes! ( even if not useful ones... ) > > eg: " None.__getattribute__( "__doc__" ) " doesn't produce an error. Eh, I meant mutating None's attributes, which is just as insane as I said. > In C, in Linux, at the end of the file "errno.h", where all error codes are > listed eg:( EIO, EAGAIN, EBUSY, E....) They had a final error like the one > you dreamed up, it was called "EIEIO"; and the comment read something like, > "All the way around Elmer's barn". There's been a collection of those around the place. A few memorable ones: EMILYPOST: Bad fork() ETOBACCO: Read on empty pipe EHORSE: Mount failed I may be misremembering, but I'm sure the originals can be found at the other end of a web search. ChrisA From ferencikioan at gmail.com Mon Nov 5 04:07:40 2012 From: ferencikioan at gmail.com (Ferencik Ioan) Date: Mon, 5 Nov 2012 01:07:40 -0800 (PST) Subject: python destructor In-Reply-To: References: Message-ID: On Monday, November 5, 2012 8:51:00 AM UTC+2, Ferencik Ioan wrote: > Hello there folks, > > > > I have a bit of a special issue. > > I'll start by disclosing myself for what i am doing. I am a postgraduate student and I really have good reasons to do what I am doing. At least i think so. > > > > And not the issue. > > I am building a python web service. This web service has some generic objects and I use a metaclass to customize the classes. > > Second I use a non-conventional object oriented database, dybase > > > > (http://www.garret.ru/dybase/doc/dybase.html#introduction) > > > > Now these is a OODBMS claiming to support ACID transactions. > > > > > > The instances of my objects are recursively organizing themselves into a hierarchical tree-like structure. When I make an instance of this object persistent dybase actually can recursively save all tree structure. > > Everything works well here. > > > > I altered the main class situated at the root of my class hierarchy to actually store inside the__dict__ not the instances of its children but their unique ID's. Then when I set a child attribute I create it and instead of being stored in the instance the child goes to a database index object. Thus it becomes Universally addressable. The a parent retrieves the child it actually fetches it from the database. > > In this way I ended up with very small objects.However these objects can regenerate the treelike structure as if they were storing there children in the __dict__. > > > > The issue is how to give the instances access to the database and properly handle the opening and closing of the database. > > It seems futile to me to actually open/close the connection through a context. Because the database is a file it will issue an IO operation on every attribute access and we all know __getattribute__ is used extremely often. > > For this reason I thought the best way would be to wrap the dybase Storage (main class) into a local storage version which would have __del__ method. > > The local Storage is a new style class..it opens the DB file but the __del__ is never called. > > This is because the Storage class has at least 2 cyclic references. > > So my Storage class never closes the database. I would like this class to close the database when it is garbage collected. > > The class is a Singleton FYI as well but this might not be relevant or even necessary. > > So my question is: > > what s the best way to force __del__ on a singleton that has cyclic references. Should i use weakref and alter the original source? Is there a way i can force a singleton to garbage collect itself?. > > > > I am by no means a software engineer so i would appreciate any advice from some experts on the matter. > > Thank you in advance. Just in case somebody is interested: Because my Storage is a singleton I registered the close() method with atexit from the Storage open(). This actually closes the connection. Not sure if this is feasible but it WORKS! I am using mod_wsgi in daemon mode so I have multithreading issues. If I configure the mod_wsgi with one process dybase works correctly. I have to override the Persistent.store() and make it thread safe using multiprocessing. This is for ANYONE who uses or plans to use dybase in a web environment. From hansmu at xs4all.nl Mon Nov 5 04:13:06 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Mon, 05 Nov 2012 10:13:06 +0100 Subject: Multi-dimensional list initialization In-Reply-To: References: Message-ID: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> On 5/11/12 07:27:52, Demian Brecht wrote: > So, here I was thinking "oh, this is a nice, easy way to initialize a 4D matrix" > (running 2.7.3, non-core libs not allowed): > > m = [[None] * 4] * 4 > > The way to get what I was after was: > > m = [[None] * 4, [None] * 4, [None] * 4, [None * 4]] Or alternateively: m = [[None] * 4 for _ in range(4)] > (Obviously, I could have just hardcoded the initialization, but I'm too > lazy to type all that out ;)) > > The behaviour I encountered seems a little contradictory to me. > [None] * 4 creates four distinct elements in a single array Actually, it creates a list with four references to the same object. But then, this object is immutable, so you won't notice that it's the same object. > while [[None] * 4] * 4 creates one distinct array of four distinct > elements, with three references to it: We usually phrase that as "a list with four references to the same list". The first reference is not special in any way. >>>> a = [None] * 4 >>>> a[0] = 'a' >>>> a > ['a', None, None, None] > >>>> m = [[None] * 4] * 4 >>>> m[0][0] = 'm' >>>> m > [['m', None, None, None], ['m', None, None, None], ['m', None, None, None], ['m', None, None, None]] > > Is this expected behaviour Yes. > and if so, why? In my mind either result makes sense, but the > inconsistency is what throws me off. There's no inconsistency: in both cases you get a list with four references to the same object. The only difference is that in the fist case, the references are to an immutable object, so the fact that it's the same object won't hurt you. Hope this helps, -- HansM From wxjmfauth at gmail.com Mon Nov 5 04:55:45 2012 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Mon, 5 Nov 2012 01:55:45 -0800 (PST) Subject: Multi-dimensional list initialization In-Reply-To: References: Message-ID: Le lundi 5 novembre 2012 07:28:00 UTC+1, Demian Brecht a ?crit?: > So, here I was thinking "oh, this is a nice, easy way to initialize a 4D matrix" (running 2.7.3, non-core libs not allowed): > > > > m = [[None] * 4] * 4 > > > > The way to get what I was after was: > > > > m = [[None] * 4, [None] * 4, [None] * 4, [None * 4]] > > > > (Obviously, I could have just hardcoded the initialization, but I'm too lazy to type all that out ;)) > > > > The behaviour I encountered seems a little contradictory to me. [None] * 4 creates four distinct elements in a single array while [[None] * 4] * 4 creates one distinct array of four distinct elements, with three references to it: > > > > >>> a = [None] * 4 > > >>> a[0] = 'a' > > >>> a > > ['a', None, None, None] > > > > >>> m = [[None] * 4] * 4 > > >>> m[0][0] = 'm' > > >>> m > > [['m', None, None, None], ['m', None, None, None], ['m', None, None, None], ['m', None, None, None]] > > > > Is this expected behaviour and if so, why? In my mind either result makes sense, but the inconsistency is what throws me off. > > > > Demian Brecht > > @demianbrecht > > http://demianbrecht.github.com ---------- You probably mean a two-dimensional matrix not a 4D matrix. >>> def DefMatrix(nrow, ncol, val): ... return [[val] * ncol for i in range(nrow)] ... >>> aa = DefMatrix(2, 3, 1.0) >>> aa >>> aa = DefMatrix(2, 3, 1.0) >>> aa [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]] >>> aa[0][0] = 3.14 >>> aa[1][2] = 2.718 >>> aa [[3.14, 1.0, 1.0], [1.0, 1.0, 2.718]] >>> >>> bb = DefMatrix(2, 3, None) >>> bb [[None, None, None], [None, None, None]] >>> bb[0][0] = 3.14 >>> bb[1][2] = 2.718 >>> bb [[3.14, None, None], [None, None, 2.718]] jmf From andrea.crotti.0 at gmail.com Mon Nov 5 05:54:08 2012 From: andrea.crotti.0 at gmail.com (andrea crotti) Date: Mon, 5 Nov 2012 10:54:08 +0000 Subject: accepting file path or file object? Message-ID: Quite often I find convenient to get a filename or a file object as argument of a function, and do something as below: def grep_file(regexp, filepath_obj): """Check if the given text is found in any of the file lines, take a path to a file or an opened file object """ if isinstance(filepath_obj, basestring): fobj = open(filepath_obj) else: fobj = filepath_obj for line in fobj: if re.search(regexp, line): return True return False This makes it also more convenient to unit-test, since I can just pass a StringIO. But then there are other problems, for example if I pass a file object is the caller that has to make sure to close the file handle.. So I'm thinking if it's not just worth to skip the support for file objects and only use the filenames, which seems a more robust and consistent choice.. Any comment/suggestions about this? From __peter__ at web.de Mon Nov 5 06:49:01 2012 From: __peter__ at web.de (Peter Otten) Date: Mon, 05 Nov 2012 12:49:01 +0100 Subject: accepting file path or file object? References: Message-ID: andrea crotti wrote: > Quite often I find convenient to get a filename or a file object as > argument of a function, and do something as below: > > def grep_file(regexp, filepath_obj): > """Check if the given text is found in any of the file lines, take > a path to a file or an opened file object > """ > if isinstance(filepath_obj, basestring): > fobj = open(filepath_obj) > else: > fobj = filepath_obj > > for line in fobj: > if re.search(regexp, line): > return True > > return False > > > This makes it also more convenient to unit-test, since I can just pass > a StringIO. But then there are other problems, for example if I pass > a file object is the caller that has to make sure to close the file > handle.. > > So I'm thinking if it's not just worth to skip the support for file > objects and only use the filenames, which seems a more robust and > consistent choice.. > > Any comment/suggestions about this? I sometimes do something like this: $ cat xopen.py import re import sys from contextlib import contextmanager @contextmanager def xopen(file=None, mode="r"): if hasattr(file, "read"): yield file elif file == "-": if "w" in mode: yield sys.stdout else: yield sys.stdin else: with open(file, mode) as f: yield f def grep(stream, regex): search = re.compile(regex).search return any(search(line) for line in stream) if len(sys.argv) == 1: print grep(["alpha", "beta", "gamma"], "gamma") else: with xopen(sys.argv[1]) as f: print grep(f, sys.argv[2]) $ python xopen.py True $ echo 'alpha beta gamma' | python xopen.py - gamma True $ echo 'alpha beta gamma' | python xopen.py - delta False $ python xopen.py xopen.py context True $ python xopen.py xopen.py gamma True $ python xopen.py xopen.py delta False $ From ulrich.eckhardt at dominolaser.com Mon Nov 5 07:35:56 2012 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Mon, 05 Nov 2012 13:35:56 +0100 Subject: accepting file path or file object? In-Reply-To: References: Message-ID: Am 05.11.2012 11:54, schrieb andrea crotti: > Quite often I find convenient to get a filename or a file object as > argument of a function, and do something as below: > > def grep_file(regexp, filepath_obj): > """Check if the given text is found in any of the file lines, take > a path to a file or an opened file object > """ > if isinstance(filepath_obj, basestring): > fobj = open(filepath_obj) > else: > fobj = filepath_obj > > for line in fobj: > if re.search(regexp, line): > return True > > return False > > This makes it also more convenient to unit-test, since I can just pass > a StringIO. I do the same for the same reason, but I either pass in a file object or the actual data contained in the file, but not a path. > But then there are other problems, for example if I pass a file > object is the caller that has to make sure to close the file > handle.. I don't consider that a problem. If you open a file, you should do that in a with expression: with open(..) as f: found = grep_file(regex, f) That is also the biggest criticism I have with your code, because you don't close the file after use. Another things is the readability of your code: grep_file("foo", "bar") The biggest problem there is that I don't know which of the two arguments is which. I personally would expect the file to come first, although the POSIX grep has it opposite on the commandline. Consider as alternative: grep("foo", path="bar") with open(..) as f: grep("foo", file=f) with open(..) as f: grep("foo", data=f.read()) Using **kwargs, you could switch inside the function depending on the mode that was used, extract lines accordingly and match these against the regex. Greetings! Uli From roy at panix.com Mon Nov 5 07:56:01 2012 From: roy at panix.com (Roy Smith) Date: Mon, 05 Nov 2012 07:56:01 -0500 Subject: Obnoxious postings from Google Groups References: <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> Message-ID: In article , Chris Angelico wrote: > It's nothing to do with operating system. File names are names, and > spaces in them are seldom worth the hassle unless you manipulate those > files solely using a GUI. That's a very ascii-esqe attitude. In a fully unicode world, I could easily see using U+00A0 (NO-BREAK SPACE) in file names, and still have space-delimited CLI work just fine. But, yeah, in the world we live in today, I try to avoid spaces in filenames. But, instead of turning "My File Name" into MyFileName, I'll usually do it as My-File-Name or My_File_Name. From andrea.crotti.0 at gmail.com Mon Nov 5 08:16:47 2012 From: andrea.crotti.0 at gmail.com (andrea crotti) Date: Mon, 5 Nov 2012 13:16:47 +0000 Subject: accepting file path or file object? In-Reply-To: References: Message-ID: 2012/11/5 Peter Otten <__peter__ at web.de>: > I sometimes do something like this: > > $ cat xopen.py > import re > import sys > from contextlib import contextmanager > > @contextmanager > def xopen(file=None, mode="r"): > if hasattr(file, "read"): > yield file > elif file == "-": > if "w" in mode: > yield sys.stdout > else: > yield sys.stdin > else: > with open(file, mode) as f: > yield f > > def grep(stream, regex): > search = re.compile(regex).search > return any(search(line) for line in stream) > > if len(sys.argv) == 1: > print grep(["alpha", "beta", "gamma"], "gamma") > else: > with xopen(sys.argv[1]) as f: > print grep(f, sys.argv[2]) > $ python xopen.py > True > $ echo 'alpha beta gamma' | python xopen.py - gamma > True > $ echo 'alpha beta gamma' | python xopen.py - delta > False > $ python xopen.py xopen.py context > True > $ python xopen.py xopen.py gamma > True > $ python xopen.py xopen.py delta > False > $ > > > -- > http://mail.python.org/mailman/listinfo/python-list That's nice thanks, there is still the problem of closing the file handle but that's maybe not so important if it gets closed at termination anyway.. From __peter__ at web.de Mon Nov 5 08:47:03 2012 From: __peter__ at web.de (Peter Otten) Date: Mon, 05 Nov 2012 14:47:03 +0100 Subject: accepting file path or file object? References: Message-ID: andrea crotti wrote: > 2012/11/5 Peter Otten <__peter__ at web.de>: >> I sometimes do something like this: >> @contextmanager >> def xopen(file=None, mode="r"): >> if hasattr(file, "read"): >> yield file elif file == "-" or file is None: # add file=None handling >> if "w" in mode: >> yield sys.stdout >> else: >> yield sys.stdin >> else: >> with open(file, mode) as f: >> yield f > That's nice thanks, there is still the problem of closing the file > handle but that's maybe not so important if it gets closed at > termination anyway.. I think you misunderstood the behaviour of my context manager. The with- statement (the last two lines) in xopen() implicitly closes files that are opened by xopen(). If you pass a file name the file will be closed: >>> with xopen("xopen.py") as f: pass ... >>> f.closed True However, if you pass an existing file it will not be closed: >>> import sys >>> from xopen import xopen >>> with xopen(sys.stdout) as f: pass ... >>> f.closed False That behaviour allows arbitrary nesting of with-statements fn = ... with xopen(fn) as f1: with xopen(f1) as f2: with xopen(f2) as f3: pass and the file may only be closed on the outermost level. From rosuav at gmail.com Mon Nov 5 09:20:27 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Nov 2012 01:20:27 +1100 Subject: Obnoxious postings from Google Groups In-Reply-To: References: <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> Message-ID: On Mon, Nov 5, 2012 at 11:56 PM, Roy Smith wrote: > That's a very ascii-esqe attitude. In a fully unicode world, I could > easily see using U+00A0 (NO-BREAK SPACE) in file names, and still have > space-delimited CLI work just fine. > Oh, do you have a "U+00A0-bar" on your keyboard? ChrisA From insideshoes at gmail.com Mon Nov 5 09:23:27 2012 From: insideshoes at gmail.com (inshu chauhan) Date: Mon, 5 Nov 2012 15:23:27 +0100 Subject: Difference between range and xrange ?? Message-ID: what is the difference between range and xrange.. both seem to work the same. ? And which should be used where and in what situations.. ?? -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Mon Nov 5 09:36:41 2012 From: d at davea.name (Dave Angel) Date: Mon, 05 Nov 2012 09:36:41 -0500 Subject: Difference between range and xrange ?? In-Reply-To: References: Message-ID: <5097CEF9.60508@davea.name> On 11/05/2012 09:23 AM, inshu chauhan wrote: > what is the difference between range and xrange.. both seem to work the > same. ? And which should be used where and in what situations.. ?? > > One difference is that from versions of Python 3.0 and later, xrange doesn't exist, and range takes over the behavior of what was formerly xrange. So presumably you're asking about Python 2.x In Python 2.x, range() generates a list, possibly a very large one. Sometimes that's exactly what you need. But other times, you're just using the list as an iterable, perhaps as a counter, or simply as a way to make a loop go a fixed number of times. xrange(), usually more efficient for speed, and certainly for space, generates an iterable. So it's interchangeable in a for loop, for example. In general, if you're going to discard the list immediately after using it, you should be using the iterable form, not the list form. In Python 3.x, if you really need a list, you can trivially convert an iterable into a list with the list "function." mylist = list(range(4)) -- DaveA From joel.goldstick at gmail.com Mon Nov 5 09:42:48 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 5 Nov 2012 09:42:48 -0500 Subject: Difference between range and xrange ?? In-Reply-To: References: Message-ID: in python 2.x xrange is a generator and range returns a list. In python 3.x xrange is renamed to range replacing the list function with the generator On Mon, Nov 5, 2012 at 9:23 AM, inshu chauhan wrote: > what is the difference between range and xrange.. both seem to work the > same. ? And which should be used where and in what situations.. ?? > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- Joel Goldstick -------------- next part -------------- An HTML attachment was scrubbed... URL: From demianbrecht at gmail.com Mon Nov 5 09:56:33 2012 From: demianbrecht at gmail.com (Demian Brecht) Date: Mon, 5 Nov 2012 06:56:33 -0800 Subject: Multi-dimensional list initialization In-Reply-To: <50976057.6080007@r3dsolutions.com> References: <50976057.6080007@r3dsolutions.com> Message-ID: <9D0B77FA-6402-47E8-98D6-B930231F5B93@gmail.com> On 2012-11-04, at 10:44 PM, Andrew Robinson wrote: > but I think you meant: > > m = [[None] * 4, [None] * 4, [None] * 4, [None] *4 ] > rather than: > m = [[None] * 4, [None] * 4, [None] * 4, [None * 4]] Yes, I meant the former, thanks for catching the typo. Demian Brecht @demianbrecht http://demianbrecht.github.com From invalid at invalid.invalid Mon Nov 5 09:59:13 2012 From: invalid at invalid.invalid (Grant Edwards) Date: Mon, 5 Nov 2012 14:59:13 +0000 (UTC) Subject: Obnoxious postings from Google Groups References: <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> Message-ID: On 2012-11-05, Roy Smith wrote: > In article , > Chris Angelico wrote: > >> It's nothing to do with operating system. File names are names, and >> spaces in them are seldom worth the hassle unless you manipulate those >> files solely using a GUI. > > That's a very ascii-esqe attitude. In a fully unicode world, I could > easily see using U+00A0 (NO-BREAK SPACE) in file names, and still have > space-delimited CLI work just fine. No, it wouldn't work just fine. You'd never know when looking at names whether it was a "regular" space or a "no-break space", and names would be visually ambiguous. Visually ambiguous names are horrible. > But, yeah, in the world we live in today, I try to avoid spaces in > filenames. But, instead of turning "My File Name" into MyFileName, I'll > usually do it as My-File-Name or My_File_Name. -- Grant Edwards grant.b.edwards Yow! Boys, you have ALL at been selected to LEAVE th' gmail.com PLANET in 15 minutes!! From demianbrecht at gmail.com Mon Nov 5 10:01:55 2012 From: demianbrecht at gmail.com (Demian Brecht) Date: Mon, 5 Nov 2012 07:01:55 -0800 Subject: Multi-dimensional list initialization In-Reply-To: References: Message-ID: On 2012-11-04, at 11:07 PM, Chris Rebert wrote: > However, unlike a list object (as in your latter example), the object > `None` is completely immutable (and what's more, a singleton value), > so you just-so-happen *not to be able to* run into the same problem of > mutating an object (assignment to an index of a list constitutes > mutation of that list) that is referenced in multiple places, for you > cannot mutate None in the first place! Thanks for clearing that up Chris (and the link to the FAQ). I had thought about that after going to bed ("D'oh.. None is immutable.. *That's* gotta be why"). Demian Brecht @demianbrecht http://demianbrecht.github.com From invalid at invalid.invalid Mon Nov 5 10:05:23 2012 From: invalid at invalid.invalid (Grant Edwards) Date: Mon, 5 Nov 2012 15:05:23 +0000 (UTC) Subject: accepting file path or file object? References: Message-ID: On 2012-11-05, andrea crotti wrote: > Quite often I find convenient to get a filename or a file object as > argument of a function, and do something as below: > > def grep_file(regexp, filepath_obj): [...] > if isinstance(filepath_obj, basestring): > fobj = open(filepath_obj) > else: > fobj = filepath_obj [...] > This makes it also more convenient to unit-test, since I can just pass > a StringIO. But then there are other problems, for example if I pass > a file object is the caller that has to make sure to close the file > handle.. > > So I'm thinking if it's not just worth to skip the support for file > objects and only use the filenames, which seems a more robust and > consistent choice.. > > Any comment/suggestions about this? I have found that accepting either a "file-like-object" or a filename is sometimes worth the effort for a module that's going to be re-used in a variety of contexts. However, when I do it, I don't usually check the type of the object -- I check for whatever "feature" I want to use. If I'm going to want to be able to call a read() method, I check for presence of a read() method. If that fails, then I assume it's a filename and pass it to open(). If that fails, then it fails. -- Grant Edwards grant.b.edwards Yow! Oh my GOD -- the at SUN just fell into YANKEE gmail.com STADIUM!! From invalid at invalid.invalid Mon Nov 5 13:20:38 2012 From: invalid at invalid.invalid (Grant Edwards) Date: Mon, 5 Nov 2012 18:20:38 +0000 (UTC) Subject: Obnoxious postings from Google Groups References: Message-ID: On 2012-11-05, Dennis Lee Bieber wrote: > On Mon, 5 Nov 2012 17:39:35 +1100, Chris Angelico > declaimed the following in gmane.comp.python.general: > >> >> It's nothing to do with operating system. File names are names, and >> spaces in them are seldom worth the hassle unless you manipulate those >> files solely using a GUI. >> > At least spaces are visible. > > CP/V would accept non-printable characters in file names... Things > like BEL were valid -- but figure out where in the name the BEL > character was when listing the directory contents Don't most OSes allow non-printing characters in filenames? VMS and Unix always have. AFAIK, there are only two characters that can't appear in a Unix filename: '\x00' and '/'. -- Grant Edwards grant.b.edwards Yow! I feel better about at world problems now! gmail.com From tjreedy at udel.edu Mon Nov 5 13:30:17 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 05 Nov 2012 13:30:17 -0500 Subject: accepting file path or file object? In-Reply-To: References: Message-ID: On 11/5/2012 5:54 AM, andrea crotti wrote: > Quite often I find convenient to get a filename or a file object as > argument of a function, and do something as below: > > def grep_file(regexp, filepath_obj): > """Check if the given text is found in any of the file lines, take > a path to a file or an opened file object > """ > if isinstance(filepath_obj, basestring): > fobj = open(filepath_obj) > else: > fobj = filepath_obj > > for line in fobj: > if re.search(regexp, line): > return True > > return False > > > This makes it also more convenient to unit-test, since I can just pass > a StringIO. But then there are other problems, for example if I pass > a file object [it] is the caller that has to make sure to close the file > handle.. Which is as it should be. The caller should (normally) call your function within a with statement or might want to do other operations on the file before or after passing it to your grep_file. -- Terry Jan Reedy From tjreedy at udel.edu Mon Nov 5 13:38:06 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 05 Nov 2012 13:38:06 -0500 Subject: Difference between range and xrange ?? In-Reply-To: References: Message-ID: On 11/5/2012 9:23 AM, inshu chauhan wrote: > what is the difference between range and xrange.. both seem to work the > same. ? >>> range(3) [0, 1, 2] >>> xrange(3) xrange(3) You should read the appropriate manual entries before asking trivial questions. They say pretty clearly that range returns a list and xrange an xrange object. http://docs.python.org/2/library/functions.html#range http://docs.python.org/2/library/functions.html#xrange If you do not understand the entries, quote the part that confuses you and say what you are unclear about. > And which should be used where and in what situations.. ?? The entry for xrange (added after range) tries to explain this. It is for situations in which one does not want a list, but only the sequence of numbers, especially in situations where the length of the list would be large enough to make creating the unneeded list a nuisance. -- Terry Jan Reedy From tjreedy at udel.edu Mon Nov 5 14:09:08 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 05 Nov 2012 14:09:08 -0500 Subject: Coordination between developers in the Python project In-Reply-To: References: Message-ID: <50980ED4.7030705@udel.edu> On 11/1/2012 4:49 PM, Tengy Td wrote: > Hello, > > > I am a French student and I am currently realizing my final thesis in > the field of Free/libre open source software. If you really are what you claim, you should give more details to make that plausible: university, college/dept, course of study, and post from a university account, not an address at one of the current favorite sites for con artists. > It would be a great help for me if you could answer a short online > survey (it should take approximately 5 minutes). If you want the survey emailed to you, you should include it. If it is hosted on a legitimate survey site, you should give a direct link. > The link to the survey is:http://bit.ly/SzVrJe Careful people do not open anonymous links from stangers. That file could literally be anything, including a malware injection page. > I would like to remind you that the participation is absolutely > anonymous and voluntary, and you can quit it at any time. Your answers > will be strictly confidential and will be used only for research purpose > (no commercial use of any information you provided). Even if you mean that for yourself, you cannot guarantee any of that. For all you know, bit.ly records the ip address of each click. -- Terry Jan Reedy From pascalwinkelmann1 at gmail.com Mon Nov 5 14:34:44 2012 From: pascalwinkelmann1 at gmail.com (Monkey) Date: Mon, 5 Nov 2012 11:34:44 -0800 (PST) Subject: py2exe with python 3.x Message-ID: <0d6ffba7-df53-43d7-9943-8aab2fa5149f@googlegroups.com> Hi, i wonderd if there is a way to convert a py-file to a exe-file with version 3.x of python. I know that it was possible till version 2.7.x. But is there a way to do it in version 3.x? Yours sincerely, Monkey From kwpolska at gmail.com Mon Nov 5 14:39:50 2012 From: kwpolska at gmail.com (Kwpolska) Date: Mon, 5 Nov 2012 20:39:50 +0100 Subject: py2exe with python 3.x In-Reply-To: <0d6ffba7-df53-43d7-9943-8aab2fa5149f@googlegroups.com> References: <0d6ffba7-df53-43d7-9943-8aab2fa5149f@googlegroups.com> Message-ID: On Mon, Nov 5, 2012 at 8:34 PM, Monkey wrote: > Hi, > i wonderd if there is a way to convert a py-file to a exe-file with version 3.x of python. > > I know that it was possible till version 2.7.x. But is there a way to do it in version 3.x? > > Yours sincerely, > Monkey > -- > http://mail.python.org/mailman/listinfo/python-list Use cx_Freeze: http://cx-freeze.sourceforge.net/ -- Kwpolska stop html mail | always bottom-post www.asciiribbon.org | www.netmeister.org/news/learn2quote.html GPG KEY: 5EAAEA16 From rurpy at yahoo.com Mon Nov 5 14:41:24 2012 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Mon, 5 Nov 2012 11:41:24 -0800 (PST) Subject: Obnoxious postings from Google Groups In-Reply-To: References: <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> Message-ID: <11dd9f93-3166-4980-8ca7-f77abbf094fc@googlegroups.com> On 11/04/2012 04:13 AM, Jamie Paul Griffin wrote: > / rurpy at yahoo.com wrote on Fri 2.Nov'12 at 11:39:10 -0700 / > >> (I also hope I haven't just been suckered by a troll attempt, >> windows/unix is better then unix/windows being an age-old means of >> trolling.) > > No, i'm not a "troll". I was just adding my opinion to the thread, I > assumed that was allowed. I didn't say UNIX is better than Windows, > did I; I just feel that Windows is not -- for me anyway -- the most > suitable plaform for learning about the science of computing and > coding, etc... being a computer science student that's the view i > have and share with those I learn with and from. Why must people be > accused of trolling everytime they make a statement that conveys a > preference over one platform or language, for example, than the > other. Provoking someone by labeling them a troll or implying they > might be is a bit childish really. I deliberately worded my response to avoid calling you a troll but rather just suggested the possibility so a denial without all the indignation would be more appropriate. (And FWIW I agree that "troll" is often used here synonymously with "I disagree with you" but that is not how I used it.) You should realize that a good way to start an endless argument and one that has a long history of being used by trolls is to assert that OS/language/whatever is "better" than some other one in an environment where there are also lots of users of the other one. Substituting "serious" or any other value-laden, unsubstantiatable word for "better" is just a insignificant variation. From rurpy at yahoo.com Mon Nov 5 14:43:31 2012 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Mon, 5 Nov 2012 11:43:31 -0800 (PST) Subject: Proper place for everything In-Reply-To: <50944545$0$29970$c3e8da3$5496439d@news.astraweb.com> References: <50944545$0$29970$c3e8da3$5496439d@news.astraweb.com> Message-ID: <9abc3f9e-72dc-4806-804d-e4c144ea9ca0@googlegroups.com> On 11/02/2012 04:12 PM, Steven D'Aprano wrote: > On Fri, 02 Nov 2012 11:51:29 -0700, Jason Benjamin wrote: > >> On another note, it appears that Google (the only archive I can find for >> this group) only has a little under 400 messages archived for this >> group, > > Google Groups is poison. If you post with it, you will be ignored by a > large percentage of the regulars here. >[...] > Bad options are: > > - Absolutely anything to do with Google Groups. It's a crap user- > interface, posts don't follow established standards for email and news, > and Google requires you to sign in just to read the archives, which in > my opinion is morally indefensible. Jason, You should also be aware that there are a number of people here such as myself that use Google Groups for various reasons and there is concurrently some discussion of this in other threads including the correction of just plain erroneous info about GG that has been propagated here. AFAIK, there is no real definition of "regular" nor any count of how many of them (whoever "them" is) that ignore GG. Pretty obviously "crap user interface" is in the eye of the beholder and the login requirement is a personal issue. From rosuav at gmail.com Mon Nov 5 16:02:16 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Nov 2012 08:02:16 +1100 Subject: Coordination between developers in the Python project In-Reply-To: <50980ED4.7030705@udel.edu> References: <50980ED4.7030705@udel.edu> Message-ID: On Tue, Nov 6, 2012 at 6:09 AM, Terry Reedy wrote: >> I would like to remind you that the participation is absolutely >> anonymous and voluntary, and you can quit it at any time. Your answers >> will be strictly confidential and will be used only for research purpose >> (no commercial use of any information you provided). > > > Even if you mean that for yourself, you cannot guarantee any of that. For > all you know, bit.ly records the ip address of each click. Maybe so, but bit.ly won't get your survey answers, so it's still anonymous in that sense. But I agree with your other concerns, which is why I did not click the link (and didn't even bother to respond till now). ChrisA From ian.g.kelly at gmail.com Mon Nov 5 16:49:36 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 5 Nov 2012 14:49:36 -0700 Subject: Coordination between developers in the Python project In-Reply-To: <50980ED4.7030705@udel.edu> References: <50980ED4.7030705@udel.edu> Message-ID: On Mon, Nov 5, 2012 at 12:09 PM, Terry Reedy wrote: > >> The link to the survey is:http://bit.ly/SzVrJe > > > Careful people do not open anonymous links from stangers. That file could > literally be anything, including a malware injection page. In this case, the link leads to a Google Docs form, which is easy to verify with a URL unshortening service. From joshua.landau.ws at gmail.com Mon Nov 5 16:51:05 2012 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Mon, 5 Nov 2012 21:51:05 +0000 Subject: Multi-dimensional list initialization In-Reply-To: References: Message-ID: On 5 November 2012 06:27, Demian Brecht wrote: > >>> a = [None] * 4 > >>> a[0] = 'a' > >>> a > ['a', None, None, None] > > >>> m = [[None] * 4] * 4 > >>> m[0][0] = 'm' > >>> m > [['m', None, None, None], ['m', None, None, None], ['m', None, None, > None], ['m', None, None, None]] > > Is this expected behaviour and if so, why? In my mind either result makes > sense, but the inconsistency is what throws me off. z = [[None] * 4] goes to z = [x, x, x, x] where x = [y] where y = None AND THEN z[0] = 2 means z = [p, x, x, x] where p = 2 AND z[1][0] = 3 means x = [q] where q = 3 hence z = [2, [3], [3], [3]] -------------- next part -------------- An HTML attachment was scrubbed... URL: From cs at zip.com.au Mon Nov 5 16:58:49 2012 From: cs at zip.com.au (Cameron Simpson) Date: Tue, 6 Nov 2012 08:58:49 +1100 Subject: accepting file path or file object? In-Reply-To: References: Message-ID: <20121105215849.GA12666@cskk.homeip.net> On 05Nov2012 10:54, andrea crotti wrote: | Quite often I find convenient to get a filename or a file object as | argument of a function, and do something as below: I tend to do this: def f(fp): if isinstance(fp, str): with open(fp) as subfp: return f(subfp) ... main code using fp as file object ... That solves the file close issue neatly and lets you put this stuff up the top where it is obvious. | So I'm thinking if it's not just worth to skip the support for file | objects and only use the filenames, which seems a more robust and | consistent choice.. You can't always use filenames; plenty of calling code will already have a file-like object to hand (eg a HTTP response or a StringIO or an already opened file or any of a million other things). So requiring filenames all the time is unreasonable. It is almost always etter to write for file objects, since that is what you would be converting any passed filename into, and put a self call at the top to convert a filename into a file object if that is a reasonable use case in your app. Cheers, -- Cameron Simpson ...valve spreeengs? VALVE _*SPREEEEEEEEENGS*_!?! We don' neeeed no steeeenking valve spreeeeeengs!... - Dr. Desmo From rosuav at gmail.com Mon Nov 5 17:01:43 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Nov 2012 09:01:43 +1100 Subject: Coordination between developers in the Python project In-Reply-To: References: <50980ED4.7030705@udel.edu> Message-ID: On Tue, Nov 6, 2012 at 8:49 AM, Ian Kelly wrote: > On Mon, Nov 5, 2012 at 12:09 PM, Terry Reedy wrote: >> >>> The link to the survey is:http://bit.ly/SzVrJe >> >> >> Careful people do not open anonymous links from stangers. That file could >> literally be anything, including a malware injection page. > > In this case, the link leads to a Google Docs form, which is easy to > verify with a URL unshortening service. By "URL unshortening service" you mean a simple HTTP request to bit.ly:80, right? :) Though I admit there aren't many easy and convenient ways to do that without immediately following the 301 redirect. ChrisA From rosuav at gmail.com Mon Nov 5 17:13:48 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Nov 2012 09:13:48 +1100 Subject: Coordination between developers in the Python project In-Reply-To: References: Message-ID: On Fri, Nov 2, 2012 at 7:49 AM, Tengy Td wrote: > It would be a great help for me if you could answer a short online survey > (it should take approximately 5 minutes). > > This survey is designed to reach a better understanding of the cooperation > and coordination between developers in Free/libre open source projects. > > There is no right or wrong answers, therefore, feel free to answer > spontaneously and to skip the questions you feel you do not want to answer. Okay, now that I have a chance to actually look at your survey, I have some more useful comments to make :) Most surveys don't care about people's exact ages. Unless yours is an unusual exception, it'd be more manageable to ask for ranges, eg: How old are you? * Under 18 * 18-25 * 26-35 * 36-45 * 46-55 * 56-65 * Over 65 Also, there's a bit of a gap in your Education drop-down. I had to leave it blank, as I never finished high school. Or does that not matter to your survey? ChrisA From oscar.j.benjamin at gmail.com Mon Nov 5 20:32:09 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 6 Nov 2012 01:32:09 +0000 Subject: Multi-dimensional list initialization In-Reply-To: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> Message-ID: On 5 November 2012 09:13, Hans Mulder wrote: > On 5/11/12 07:27:52, Demian Brecht wrote: >> So, here I was thinking "oh, this is a nice, easy way to initialize a 4D matrix" >> (running 2.7.3, non-core libs not allowed): >> >> m = [[None] * 4] * 4 >> >> The way to get what I was after was: >> >> m = [[None] * 4, [None] * 4, [None] * 4, [None * 4]] > > Or alternateively: > > m = [[None] * 4 for _ in range(4)] That's the way to do it. I've seen this question many times between here and the python-tutor list. It does seem to be a common gotcha. I was just thinking to myself that it would be a hard thing to change because the list would need to know how to instantiate copies of all the different types of the elements in the list. Then I realised it doesn't. It is simply a case of how the list multiplication operator is implemented and whether it chooses to use a reference to the same list or make a copy of that list. Since all of this is implemented within the same list type it is a relatively easy change to make (ignoring backward compatibility concerns). I don't see this non-copying list multiplication behaviour as contradictory but has anyone ever actually found a use for it? Oscar From rosuav at gmail.com Mon Nov 5 21:01:46 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Nov 2012 13:01:46 +1100 Subject: Multi-dimensional list initialization In-Reply-To: References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> Message-ID: On Tue, Nov 6, 2012 at 12:32 PM, Oscar Benjamin wrote: > I was just thinking to myself that it would be a hard thing to change > because the list would need to know how to instantiate copies of all > the different types of the elements in the list. Then I realised it > doesn't. It is simply a case of how the list multiplication operator > is implemented and whether it chooses to use a reference to the same > list or make a copy of that list. Since all of this is implemented > within the same list type it is a relatively easy change to make > (ignoring backward compatibility concerns). > > I don't see this non-copying list multiplication behaviour as > contradictory but has anyone ever actually found a use for it? Stupid example of why it can't copy: bad = [open("test_file")] * 4 How do you clone something that isn't Plain Old Data? Ultimately, that's where the problem comes from. It's easy enough to clone something that's all scalars (strings, integers, None, etc) and non-recursive lists/dicts of scalars, but anything more complicated than that is rather harder. If you want a deep copy and are prepared to handle any issues that might result, you can do this: >>> import copy >>> a=[[2,3,4]] >>> a.extend(copy.deepcopy(a)) >>> a[0][1]=10 >>> a [[2, 10, 4], [2, 3, 4]] And some things just won't work: >>> bad.extend(copy.deepcopy(bad)) Traceback (most recent call last): File "", line 1, in bad.extend(copy.deepcopy(bad)) File "C:\Python32\lib\copy.py", line 147, in deepcopy y = copier(x, memo) File "C:\Python32\lib\copy.py", line 209, in _deepcopy_list y.append(deepcopy(a, memo)) File "C:\Python32\lib\copy.py", line 166, in deepcopy rv = reductor(2) TypeError: cannot serialize '_io.TextIOWrapper' object The default behaviour is safe and reliable. When you want something other than the default, there are ways of doing it. ChrisA From oscar.j.benjamin at gmail.com Mon Nov 5 21:30:00 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 6 Nov 2012 02:30:00 +0000 Subject: Multi-dimensional list initialization In-Reply-To: References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> Message-ID: On 6 November 2012 02:01, Chris Angelico wrote: > On Tue, Nov 6, 2012 at 12:32 PM, Oscar Benjamin > wrote: >> I was just thinking to myself that it would be a hard thing to change >> because the list would need to know how to instantiate copies of all >> the different types of the elements in the list. Then I realised it >> doesn't. It is simply a case of how the list multiplication operator >> is implemented and whether it chooses to use a reference to the same >> list or make a copy of that list. Since all of this is implemented >> within the same list type it is a relatively easy change to make >> (ignoring backward compatibility concerns). >> >> I don't see this non-copying list multiplication behaviour as >> contradictory but has anyone ever actually found a use for it? > > Stupid example of why it can't copy: > > bad = [open("test_file")] * 4 > > How do you clone something that isn't Plain Old Data? Ultimately, > that's where the problem comes from. It's easy enough to clone > something that's all scalars (strings, integers, None, etc) and > non-recursive lists/dicts of scalars, but anything more complicated > than that is rather harder. That's not what I meant. But now you've made me realise that I was wrong about what I did mean. In the case of stuff = [[obj] * n] * m I thought that the multiplication of the inner list ([obj] * n) by m could create a new list of lists using copies. On closer inspection I see that the list being multiplied is in fact [[obj] * n] and that this list can only know that it is a list of lists by inspecting its element(s) which makes things more complicated. I retract my claim that this change would be easy to implement. Oscar From ronggui.huang at gmail.com Mon Nov 5 22:32:41 2012 From: ronggui.huang at gmail.com (Wincent) Date: Mon, 5 Nov 2012 19:32:41 -0800 (PST) Subject: problem with eval and time Message-ID: <932c348a-2670-44f0-a38b-4fddae577a0e@googlegroups.com> Dear all, I would like to convert tstr to representation of time, but encounter the following error. Is there a simple way to get what I want? Thanks. >>> import time >>> tstr = str(time.localtime()) >>> eval(tstr) Traceback (most recent call last): File "", line 1, in File "", line 1, in TypeError: structseq() takes at most 2 arguments (9 given) >>> sys.version '2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]' Wincent From rouslank at msn.com Mon Nov 5 23:02:42 2012 From: rouslank at msn.com (Rouslan Korneychuk) Date: Mon, 05 Nov 2012 23:02:42 -0500 Subject: How to improve the usability of nested packages In-Reply-To: References: Message-ID: On 11/02/2012 12:11 PM, Michael Schwarz wrote: > ? which doesn't work. Some of the modules reference other modules in > the same package. I'm not talking about cyclic references, but, for > example, the "dialog" module uses the "transaction" module. The > problem is that the "dialog" module uses the same mechanism shown > above to import the other modules from it's package. This means that > modules and packages are imported in this order: > > - Application code executes "from the_library import sip" > - the_library/__init__.py is executed. No imports here. > - the_library/sip/__init__.py executes "from . import [...], dialog" > - the_library/sip/dialog.py executes "from the_library import sip" In a way, you do have a cyclic reference. If you think of "import sip" as "from sip import __init__ as sip", it should become apparent. Anyway, I see two ways around this. One is to rename the package and create a module under the_library with the old package name that imports what you want. To keep using the same names, every module in the package can import siblings like so (where sippkg is the new package name): import the_library.sippkg.transaction sip = the_library.sippkg The second way is to not use import at all to load sibling modules (except in __init__.py), and instead use: sip = sys.modules['the_library.sip'] This will work as long as you are careful to load the modules from __init__.py in the correct order, where each module's dependencies must appear before the module, in the import list. From wuwei23 at gmail.com Mon Nov 5 23:22:44 2012 From: wuwei23 at gmail.com (alex23) Date: Mon, 5 Nov 2012 20:22:44 -0800 (PST) Subject: problem with eval and time References: <932c348a-2670-44f0-a38b-4fddae577a0e@googlegroups.com> Message-ID: <43fc26d7-50f4-470a-9d02-3b3e27271b63@nl3g2000pbc.googlegroups.com> On Nov 6, 1:32?pm, Wincent wrote: > Dear all, I would like to convert tstr to representation > of time, but encounter the following error. Is there a > simple way to get what I want? Thanks. > > >>> import time > >>> tstr = str(time.localtime()) > >>> eval(tstr) > > Traceback (most recent call last): > ? File "", line 1, in > ? File "", line 1, in > TypeError: structseq() takes at most 2 arguments (9 given)>>> sys.version The problem is that the repr of `time.struct_time` isn't its constructor, so you won't be able to do this without parsing the string, I believe. What are you trying to achieve here? You already have a time.struct_time object, why turn it into a string if what you want is the object? If you're wanting to pass time values around as strings, maybe `time.strptime` will be more useful. From ronggui.huang at gmail.com Mon Nov 5 23:29:47 2012 From: ronggui.huang at gmail.com (Wincent) Date: Mon, 5 Nov 2012 20:29:47 -0800 (PST) Subject: problem with eval and time In-Reply-To: <43fc26d7-50f4-470a-9d02-3b3e27271b63@nl3g2000pbc.googlegroups.com> References: <932c348a-2670-44f0-a38b-4fddae577a0e@googlegroups.com> <43fc26d7-50f4-470a-9d02-3b3e27271b63@nl3g2000pbc.googlegroups.com> Message-ID: <0963c058-5ba2-474b-8a18-6d3decb5889c@googlegroups.com> Thanks. I fetch data from social networking sites and want to mark the time of access. I store all the information in a redis database, which converts everything into strings and I need to convert those strings back to original python objects when analyzing the data. Best Regards On Tuesday, November 6, 2012 12:22:44 PM UTC+8, alex23 wrote: > On Nov 6, 1:32?pm, Wincent wrote: > > > Dear all, I would like to convert tstr to representation > > > of time, but encounter the following error. Is there a > > > simple way to get what I want? Thanks. > > > > > > >>> import time > > > >>> tstr = str(time.localtime()) > > > >>> eval(tstr) > > > > > > Traceback (most recent call last): > > > ? File "", line 1, in > > > ? File "", line 1, in > > > TypeError: structseq() takes at most 2 arguments (9 given)>>> sys.version > > > > The problem is that the repr of `time.struct_time` isn't its > > constructor, so you won't be able to do this without parsing the > > string, I believe. > > > > What are you trying to achieve here? You already have a > > time.struct_time object, why turn it into a string if what you want is > > the object? > > > > If you're wanting to pass time values around as strings, maybe > > `time.strptime` will be more useful. From steve+comp.lang.python at pearwood.info Mon Nov 5 23:31:26 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 06 Nov 2012 04:31:26 GMT Subject: Coordination between developers in the Python project References: Message-ID: <5098929e$0$29980$c3e8da3$5496439d@news.astraweb.com> On Mon, 05 Nov 2012 14:09:08 -0500, Terry Reedy wrote: > On 11/1/2012 4:49 PM, Tengy Td wrote: >> Hello, >> >> >> I am a French student and I am currently realizing my final thesis in >> the field of Free/libre open source software. > > If you really are what you claim, you should give more details to make > that plausible: university, college/dept, course of study, and post from > a university account, not an address at one of the current favorite > sites for con artists. > >> It would be a great help for me if you could answer a short online >> survey (it should take approximately 5 minutes). > > If you want the survey emailed to you, you should include it. If it is > hosted on a legitimate survey site, you should give a direct link. I'm pretty sure that "legitimate" survey sites don't vet the moral character of their users. Just because a link comes from "www.legitimatesurveys.com" doesn't mean it is safe or desirable. https://docs.google.com/spreadsheet/viewform? formkey=dDE4YzYxZ1lDN0dpdFFheGJZM3prdVE6MA Is that legitimate enough? >> The link to the survey is:http://bit.ly/SzVrJe > > Careful people do not open anonymous links from stangers. That file > could literally be anything, including a malware injection page. Yes dad. While it is good and proper to exhibit a certain level of caution on the internet, it is important to keep the dangers in perspective and not go overboard. You just implicitly accusing somebody of likely being a criminal who lures others to a malware site based on little more evidence than the fact that he has a gmail email address and linked to bit.ly. I think that has edging right up to the line between showing proper caution, and rudeness and paranoia. "Legitimate" sites are not safe either. With the proliferation of Javascript, Flash, third-party advertisers, etc., just about any http page on the Internet could in principle contain malware. And not just by accident: Google has been caught illegally installing tracking cookies then lying about it, Sony installed a root-kit on users' computers (although not over the Internet) and Facebook is engaged in a neverending privacy and security war against its users. Nevertheless most pages are safe. Hundreds of millions of people browse the web without being infected, protected by a combination of: - firewall - OS security features (such as not running everything as the Administrator or root user) - anti-virus and anti-spyware software etc. Let me be frank here: if this link was malware, there would be *far* more cost-effective ways to spread it than by appealing to FOSS developers to fill in a survey. A fake link promoted with "Check out this blog post by Linus Torvalds where he smacks down Richard Stallman" ought to do it. >> I would like to remind you that the participation is absolutely >> anonymous and voluntary, and you can quit it at any time. Your answers >> will be strictly confidential and will be used only for research >> purpose (no commercial use of any information you provided). > > Even if you mean that for yourself, you cannot guarantee any of that. > For all you know, bit.ly records the ip address of each click. You use gmane to post here. How do you know that they aren't recording your IP address? If they are, what are they going to do with it? What nefarious use do you think people are going to do with your IP address? -- Steven From rosuav at gmail.com Mon Nov 5 23:38:47 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Nov 2012 15:38:47 +1100 Subject: problem with eval and time In-Reply-To: <0963c058-5ba2-474b-8a18-6d3decb5889c@googlegroups.com> References: <932c348a-2670-44f0-a38b-4fddae577a0e@googlegroups.com> <43fc26d7-50f4-470a-9d02-3b3e27271b63@nl3g2000pbc.googlegroups.com> <0963c058-5ba2-474b-8a18-6d3decb5889c@googlegroups.com> Message-ID: On Tue, Nov 6, 2012 at 3:29 PM, Wincent wrote: > Thanks. > > I fetch data from social networking sites and want to mark the time of access. I store all the information in a redis database, which converts everything into strings and I need to convert those strings back to original python objects when analyzing the data. The easiest way, imho, is to store Unix times - simply the number of seconds since 1970, as an integer or float. That can easily and safely be turned into a string and back (floats might lose a little accuracy, depending on how you do it, but the difference will be a small fraction of a second). >>> time.time() 1352176547.787 >>> time.gmtime(1352176547.787) time.struct_time(tm_year=2012, tm_mon=11, tm_mday=6, tm_hour=4, tm_min=35, tm_sec=47, tm_wday=1, tm_yday=311, tm_isdst=0) Easy and unambiguous. Also compact, which may or may not be a selling point. ChrisA From d at davea.name Mon Nov 5 23:42:42 2012 From: d at davea.name (Dave Angel) Date: Mon, 05 Nov 2012 23:42:42 -0500 Subject: problem with eval and time In-Reply-To: <0963c058-5ba2-474b-8a18-6d3decb5889c@googlegroups.com> References: <932c348a-2670-44f0-a38b-4fddae577a0e@googlegroups.com> <43fc26d7-50f4-470a-9d02-3b3e27271b63@nl3g2000pbc.googlegroups.com> <0963c058-5ba2-474b-8a18-6d3decb5889c@googlegroups.com> Message-ID: <50989542.1000203@davea.name> On 11/05/2012 11:29 PM, Wincent wrote: (Please don't top-post. it messes everything up. And your use of Google-groups is making everything double-spaced) > Thanks. > > I fetch data from social networking sites and want to mark the time of access. I store all the information in a redis database, which converts everything into strings and I need to convert those strings back to original python objects when analyzing the data. > Then restate your problem in terms of describing these strings. Apparently you don't get them from str(time.localtime()), but from some redis methodology. To convert most reasonable strings back to a date/time, you can probably use strftime. But you certainly cannot reasonably use eval. You're lucky it didn't work, so you didn't leave it that way. Eval on data stored in some database? You gotta be kidding. -- DaveA From steve+comp.lang.python at pearwood.info Mon Nov 5 23:46:11 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 06 Nov 2012 04:46:11 GMT Subject: Obnoxious postings from Google Groups References: Message-ID: <50989613$0$29980$c3e8da3$5496439d@news.astraweb.com> On Mon, 05 Nov 2012 14:47:47 -0500, Dennis Lee Bieber wrote: >> Don't most OSes allow non-printing characters in filenames? VMS and >> Unix always have. AFAIK, there are only two characters that can't >> appear in a Unix filename: '\x00' and '/'. > > But can you /enter/ them with common keystrokes on a plain text > terminal (it's been 35 years, so I don't recall the exact key used for > the BEL on CP/V -- my mind thinks was used)... No cut&paste > from "character map", no -3digitsequence... For most people, that's a pointless restriction. You might as well insist that the file name can be typed without using the shift key, or using only the left hand of the keyboard. Copy-paste, char map, alt-digits are as much a part of the input environment on modern systems as the keyboard. Nevertheless, I do tend to prefer underscores to spaces, simply because I often use naive tools that treat spaces as separators. That is, command line shells. For what it's worth, you can enter any control character in Unix/Linux systems with readline in bash using the C-q key combination. Newline needs a bit of special treatment: you need to wrap the name in single quotes to stop the newline from being interpreted as the end of the command. [steve at ando temp]$ touch 'foo bar' To enter the newline, I typed Ctrl-Q to tell bash to treat the next character as a literal, and then typed Ctrl-J to get a newline. [steve at ando temp]$ ls foo?bar [steve at ando temp]$ python -c "import os > for nm in os.listdir('.'): print repr(nm)" 'foo\nbar' -- Steven From steve+comp.lang.python at pearwood.info Tue Nov 6 00:03:19 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 06 Nov 2012 05:03:19 GMT Subject: Obnoxious postings from Google Groups References: <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> Message-ID: <50989a16$0$29980$c3e8da3$5496439d@news.astraweb.com> On Mon, 05 Nov 2012 17:39:35 +1100, Chris Angelico wrote: > On Mon, Nov 5, 2012 at 5:10 PM, rusi wrote: >> Among people who know me, I am a linux nerd: My sister scolded me >> yesterday because I put files on her computer without spaces: >> DoesAnyoneWriteLikeThis?!?! > > My filenames seldom have spaces in them, but that has nothing to do with > how I write English. Names are names. They're not essays, they are not > written as full sentences. But names are often multiple words and sentence fragments, and for those you *need* spaces. Or at least an ersatz space, like underscore, which is ugly and harder to use, but at least makes using command shells easier to use. But don't be fooled: the fault belongs to the shell, not the space. The problem is that shells are untyped and treat *everything* as a stream of text, and therefore cannot distinguish between arguments, variables, numbers, commands, etc. except by a few simplistic conventions such as: "The first word on the line is the command." "If it starts with a dash, it must be a command option." "Arguments are separated by spaces." etc. When your data (e.g. filenames) violate those naive assumptions, as they frequently do, the shell cannot cope. One solution would be to fix the tools. Another would be to mangle the data. "Real" programming languages don't have this problem. You can trivially refer to any file name, regardless of the characters in its name, in Python because you have a much more powerful set of tools: commands take real arguments, and the presence of a space in one argument cannot cause it to "bleed over" into the next argument. The downside is that if spaces are not argument separators, then you need something else to be an argument separator. Or you need argument delimiters. Or strings need to be quoted. Programming languages do these things because they are designed to be correct. Shell do not because they are designed for lazy users and merely aim to be "good enough". -- Steven From redstone-cold at 163.com Tue Nov 6 00:19:58 2012 From: redstone-cold at 163.com (iMath) Date: Mon, 5 Nov 2012 21:19:58 -0800 (PST) Subject: How to only get a list of the names of the non-directory files in current directory ('.')? Message-ID: How to only get a list of the names of the non-directory files in current directory ('.')? (Note excluding its subdirectories ). I need the code : ) From rosuav at gmail.com Tue Nov 6 00:24:38 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Nov 2012 16:24:38 +1100 Subject: How to only get a list of the names of the non-directory files in current directory ('.')? In-Reply-To: References: Message-ID: On Tue, Nov 6, 2012 at 4:19 PM, iMath wrote: > How to only get a list of the names of the non-directory files in current directory ('.')? > (Note excluding its subdirectories ). > > I need the code : ) Start by getting a list of names of everything in the current directory. Then filter that list by testing each one to see if it's a directory. Tip: The second step can be done with os.path.isdir Put some code together and try it. If you have trouble, post your code, any exception traceback you get, and what you're having difficulty with, and we'll go on from there. Have fun! ChrisA From andrew3 at r3dsolutions.com Tue Nov 6 00:51:24 2012 From: andrew3 at r3dsolutions.com (Andrew Robinson) Date: Mon, 05 Nov 2012 21:51:24 -0800 Subject: Multi-dimensional list initialization In-Reply-To: References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> Message-ID: <5098A55C.3090201@r3dsolutions.com> On 11/05/2012 06:30 PM, Oscar Benjamin wrote: > On 6 November 2012 02:01, Chris Angelico wrote: >> On Tue, Nov 6, 2012 at 12:32 PM, Oscar Benjamin >> wrote: >>> I was just thinking to myself that it would be a hard thing to change >>> because the list would need to know how to instantiate copies of all >>> the different types of the elements in the list. Then I realised it >>> doesn't. It is simply a case of how the list multiplication operator >>> is implemented and whether it chooses to use a reference to the same >>> list or make a copy of that list. Since all of this is implemented >>> within the same list type it is a relatively easy change to make >>> (ignoring backward compatibility concerns). >>> >>> I don't see this non-copying list multiplication behaviour as >>> contradictory but has anyone ever actually found a use for it? >> Stupid example of why it can't copy: >> >> bad = [open("test_file")] * 4 >> >> How do you clone something that isn't Plain Old Data? Ultimately, >> that's where the problem comes from. It's easy enough to clone >> something that's all scalars (strings, integers, None, etc) and >> non-recursive lists/dicts of scalars, but anything more complicated >> than that is rather harder. > That's not what I meant. But now you've made me realise that I was > wrong about what I did mean. In the case of > > stuff = [[obj] * n] * m > > I thought that the multiplication of the inner list ([obj] * n) by m > could create a new list of lists using copies. On closer inspection I > see that the list being multiplied is in fact [[obj] * n] and that > this list can only know that it is a list of lists by inspecting its > element(s) which makes things more complicated. > > I retract my claim that this change would be easy to implement. > > > Oscar Hi Oscar, In general, people don't use element multiplication (that I have *ever* seen) to make lists where all elements of the outer most list point to the same sub-*list* by reference. The most common use of the multiplication is to fill an array with a constant, or short list of constants; Hence, almost everyone has to work around the issue as the initial poster did by using a much longer construction. The most compact notation in programming really ought to reflect the most *commonly* desired operation. Otherwise, we're really just making people do extra typing for no reason. Further, list comprehensions take quite a bit longer to run than low level copies; by a factor of roughly 10. SO, it really would be worth implementing the underlying logic -- even if it wasn't super easy. I really don't think doing a shallow copy of lists would break anyone's program. The non-list elements, whatever they are, can be left as reference copies -- but any element which is a list ought to be shallow copied. The behavior observed in the opening post where modifying one element of a sub-list, modifies all elements of all sub-lists is never desired as far as I have ever witnessed. The underlying implementation of Python can check an object type trivially, and the only routine needed is a shallow list copy. So, no it really isn't a complicated operation to do shallow copies of lists. :) From rosuav at gmail.com Tue Nov 6 01:07:06 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Nov 2012 17:07:06 +1100 Subject: Multi-dimensional list initialization In-Reply-To: <5098A55C.3090201@r3dsolutions.com> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098A55C.3090201@r3dsolutions.com> Message-ID: On Tue, Nov 6, 2012 at 4:51 PM, Andrew Robinson wrote: > I really don't think doing a shallow copy of lists would break anyone's > program. Well, it's a change, a semantic change. It's almost certainly going to break _something_. But for the sake of argument, we can suppose that the change could be made. Would it be the right thing to do? Shallow copying by default would result in extremely weird behaviour. All the same confusion would result, only instead of comparing [None]*4 with [[None]]*4, there'd be confusion over the difference between [[None]]*4 and [[[None]]]*4. I don't think it would help anything, and it'd result in a lot more work for no benefit. ChrisA From amit.amitagrawal.agrawal at gmail.com Tue Nov 6 01:17:42 2012 From: amit.amitagrawal.agrawal at gmail.com (Amit Agrawal) Date: Tue, 6 Nov 2012 11:47:42 +0530 Subject: delete value in list in indexing Message-ID: i want to delete list in upper and lower some specific value -------------- next part -------------- An HTML attachment was scrubbed... URL: From andrew3 at r3dsolutions.com Tue Nov 6 03:21:07 2012 From: andrew3 at r3dsolutions.com (Andrew Robinson) Date: Tue, 06 Nov 2012 00:21:07 -0800 Subject: Multi-dimensional list initialization In-Reply-To: References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098A55C.3090201@r3dsolutions.com> Message-ID: <5098C873.2000200@r3dsolutions.com> On 11/05/2012 10:07 PM, Chris Angelico wrote: > On Tue, Nov 6, 2012 at 4:51 PM, Andrew Robinson > wrote: >> I really don't think doing a shallow copy of lists would break anyone's >> program. > Well, it's a change, a semantic change. It's almost certainly going to > break _something_. But for the sake of argument, we can suppose that > the change could be made. Would it be the right thing to do? > > Shallow copying by default would result in extremely weird behaviour. > All the same confusion would result, only instead of comparing > [None]*4 with [[None]]*4, there'd be confusion over the difference > between [[None]]*4 and [[[None]]]*4. > > I don't think it would help anything, and it'd result in a lot more > work for no benefit. > > ChrisA I don't follow. a=[ None ]*4 would give a=[ None, None, None, None ] as usual. All four None's would be the same object, but there are automatically 4 different pointers to it. Hence, a[0]=1 would give a=[ 1, None, None, None ] as usual. a=[ [None] ]*4 would give a=[ [None], [None], [None], [None] ] as usual BUT: a[0][0] = 1 would no longer give a=[ [1],[1],[1],[1] ] *Rather* it would give a=[ [1].[None].[None],[None] ] The None objects are all still the same one, BUT the lists themselves are different. Again, a=[ ["alpha","beta"] * 4 ] would give: a=[ ["alpha","beta"], ["alpha","beta"], ["alpha","beta"], ["alpha","beta"] ] All four strings, "alpha", are the same object -- but there are 5 different lists; The pointers inside the initial list are copied four times -- not the string objects; But the *lists* themselves are created new for each replication. If you nest it another time; [[[None]]]*4, the same would happen; all lists would be independent -- but the objects which aren't lists would be refrenced-- not copied. a=[[["alpha","beta"]]]*4 would yield: a=[[['alpha', 'beta']], [['alpha', 'beta']], [['alpha', 'beta']], [['alpha', 'beta']]] and a[0][0]=1 would give [[1],[['alpha', 'beta']], [['alpha', 'beta']], [['alpha', 'beta']]]] rather than a=[[1], [1], [1], [1]] Or at another level down: a[0][0][0]=1 would give: a=[[[1, 'beta']], [['alpha', 'beta']], [['alpha', 'beta']], [['alpha', 'beta']] ] rather than a=[[[1, 'beta']], [[1, 'beta']], [[1, 'beta']], [[1, 'beta']]] The point is, there would be no difference at all noticed in what data is found where in the array; the *only* thing that would change is that replacing an item by assignment would only affect the *location* assigned to -- all other locations would not be affected. That really is what people *generally* want. If the entire list is meant to be read only -- the change would affect *nothing* at all. See if you can find *any* python program where people desired the multiplication to have the die effect that changing an object in one of the sub lists -- changes all the objects in the other sub lists. I'm sure you're not going to find it -- and even if you do, it's going to be 1 program in 1000's. From ian.g.kelly at gmail.com Tue Nov 6 03:49:19 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 6 Nov 2012 01:49:19 -0700 Subject: Coordination between developers in the Python project In-Reply-To: References: <50980ED4.7030705@udel.edu> Message-ID: On Mon, Nov 5, 2012 at 3:01 PM, Chris Angelico wrote: > On Tue, Nov 6, 2012 at 8:49 AM, Ian Kelly wrote: >> On Mon, Nov 5, 2012 at 12:09 PM, Terry Reedy wrote: >>> >>>> The link to the survey is:http://bit.ly/SzVrJe >>> >>> >>> Careful people do not open anonymous links from stangers. That file could >>> literally be anything, including a malware injection page. >> >> In this case, the link leads to a Google Docs form, which is easy to >> verify with a URL unshortening service. > > By "URL unshortening service" you mean a simple HTTP request to > bit.ly:80, right? :) Though I admit there aren't many easy and > convenient ways to do that without immediately following the 301 > redirect. That's one way to do it. Although I find it a bit easier to just use something like unshorten.com, which is the first Google hit for "url unshortener". Assuming that you trust that site to not be hosting malware itself. :-) From rosuav at gmail.com Tue Nov 6 03:59:12 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Nov 2012 19:59:12 +1100 Subject: Coordination between developers in the Python project In-Reply-To: References: <50980ED4.7030705@udel.edu> Message-ID: On Tue, Nov 6, 2012 at 7:49 PM, Ian Kelly wrote: > On Mon, Nov 5, 2012 at 3:01 PM, Chris Angelico wrote: >> By "URL unshortening service" you mean a simple HTTP request to >> bit.ly:80, right? :) Though I admit there aren't many easy and >> convenient ways to do that without immediately following the 301 >> redirect. > > That's one way to do it. Although I find it a bit easier to just use > something like unshorten.com, which is the first Google hit for "url > unshortener". Assuming that you trust that site to not be hosting > malware itself. :-) A few years ago, I found myself bugged by URLs getting broken across multiple lines when shared across a MUD communication channel (80-character lines, with a header on each line, so there'd be between 65 and 75 usable characters per line), so I wrote a feature into my MUD client that would auto-shorten long URLs by passing them to tinyurl.com, and also "render" them on the other side - if it started http://tinyurl.com/ then the client would, on request, query tinyurl and snag the returned URL back into the display. For some reason it never occurred to me to do it any way other than direct socket operations in C. ChrisA From tm at tobix.eu Tue Nov 6 04:00:47 2012 From: tm at tobix.eu (Tobias Marquardt) Date: Tue, 06 Nov 2012 10:00:47 +0100 Subject: py2exe with python 3.x In-Reply-To: References: <0d6ffba7-df53-43d7-9943-8aab2fa5149f@googlegroups.com> Message-ID: <5098D1BF.9080201@tobix.eu> Am 05.11.2012 20:39, schrieb Kwpolska: > On Mon, Nov 5, 2012 at 8:34 PM, Monkey wrote: >> Hi, >> i wonderd if there is a way to convert a py-file to a exe-file with version 3.x of python. >> >> I know that it was possible till version 2.7.x. But is there a way to do it in version 3.x? >> >> Yours sincerely, >> Monkey >> -- >> http://mail.python.org/mailman/listinfo/python-list > Use cx_Freeze: http://cx-freeze.sourceforge.net/ > cx_Freeze is great as far as you don't need to use the latest Python version 3.3.0, because there is still a bug: http://sourceforge.net/p/cx-freeze/bugs/33/ I hope this will be fixed soon... From steve+comp.lang.python at pearwood.info Tue Nov 6 04:04:44 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 06 Nov 2012 09:04:44 GMT Subject: Multi-dimensional list initialization References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> Message-ID: <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> On Mon, 05 Nov 2012 21:51:24 -0800, Andrew Robinson wrote: > The most compact notation in programming really ought to reflect the > most *commonly* desired operation. Otherwise, we're really just making > people do extra typing for no reason. There are many reasons not to put minimizing of typing ahead of all other values: * Typically, code is written once and read many times. Minimizing typing might save you a second or two once, and then cost you many seconds every time you read the code. That's why we tell people to choose meaningful variable names, instead of naming everything "a" and "b". * Consistency of semantics is better than a plethora of special cases. Python has a very simple and useful rule: objects should not be copied unless explicitly requested to be copied. This is much better than having to remember whether this operation or that operation makes a copy. The answer is consistent: (pardon me for belabouring the point here) Q: Does [0]*10 make ten copies of the integer object? A: No, list multiplication doesn't make copies of elements. Q: How about [0.0]*10? A: No, the elements are never copied. Q: What if I use a singleton? Does [None]*10 try to copy? A: No, the elements are never copied. Q: How about things like file objects that can't be copied? A: No, the elements are never copied. Q: What about [[]]*10? A: No, the elements are never copied. Q: How about if the elements are subclasses of list? A: No, the elements are never copied. Q: What about other mutable objects like sets or dicts? A: No, the elements are never copied. Q: What about instances of custom classes? A: No, the elements are never copied. Q: What if they are old-style Classic classes? A: No, the elements are never copied. Q: What if I do some funny tricks with the metaclass? A: No, the elements are never copied. Q: How about on Tuesdays? I bet they're copied on Tuesdays. A: No, the elements are never copied. Your proposal throws away consistency for a trivial benefit on a rare use- case, and replaces it with a bunch of special cases: Q: What about [[]]*10? A: Oh yeah, I forgot about lists, they're copied. Q: How about if the elements are subclasses of list? A: Hmmm, that's a good one, I'm not actually sure. Q: How about if I use delegation to proxy a list? A: Oh no, they definitely won't be copied. Q: What about other mutable objects like sets or dicts? A: No, definitely not. Unless people complain enough. Q: What about instances of custom classes? A: That's a definite maybe. Q: How about on Tuesdays? I bet they're copied on Tuesdays. A: Only if you're in Belgium. Losing consistency in favour of saving a few characters for something as uncommon as list multiplication is a poor tradeoff. That's why this proposal has been rejected again and again and again every time it has been suggested. List multiplication [x]*n is conceptually equivalent to: newlist = [] for i in range(n): newlist.append(x) or if you prefer a list comp: [x for i in range(n)] This is nice and simple and efficient. Some objects cannot be copied at all. Copying other objects is slow and inefficient. Keeping list multiplication consistent, and fast, is MUCH more important than making it work as expected for the rare case of 2D arrays: [[0]*n]*m where there are other alternatives. > Further, list comprehensions take quite a bit longer to run than low > level copies; by a factor of roughly 10. SO, it really would be worth > implementing the underlying logic -- even if it wasn't super easy. Copying those elements does not come for free. It is true that list multiplication can be much faster than a list comp. But that's because the list multiply doesn't have to inspect the elements, copy them, or engage the iteration machinery. Avoiding copying gives you a big saving: [steve at ando ~]$ python3.3 -m timeit -s "x = range(1000)" "[x for _ in range(100)]" # not copied 100000 loops, best of 3: 11.9 usec per loop [steve at ando utilities]$ python3.3 -m timeit -s "x = range(1000)" "[x[:] for _ in range(100)]" # copied 10000 loops, best of 3: 103 usec per loop So there's a factor of ten difference right there. If list multiplication had to make copies, it would lose much of its speed advantage. For large enough lists, or complicated enough objects, it would become slower than a list comprehension. It would be even slower if list multiplication had to inspect each element first and decide whether or not to copy. > I really don't think doing a shallow copy of lists would break anyone's > program. Anyone who is currently using list multiplication with mutable objects is expecting that they will be the same object, and relying on that fact. Otherwise they wouldn't be using list multiplication. You're suggesting a semantic change. Therefore they will be expecting something different from what actually happens. Result: broken code. It's not just mutable objects. It's also objects that can't be copied. Result: mylist*3 used to work, now it raises an exception. And performance issues: what used to be fast is now slow. Even if this change was allowed, it would have to go through a multi-year process. Python 3.3 is too late -- the absolute earliest would be Python 3.4, which is scheduled for about 18 months from now. So in Python 3.4 you could write: from __future__ import list_multiplication_copying to get the behaviour you want, and then in Python 3.5 it would become the default. That's three years until it becomes the standard. Meanwhile, there will still be millions of people using Python 2.7 or 3.2, and their code will behave differently from your code. Conservatively, if you write code to support three previous releases, that means you can't use this feature until Python 3.7. So that's about six years before it can be used widely. If the problem being solved was big enough, this would be worth doing. But it's not. > The non-list elements, whatever they are, can be left as reference > copies -- but any element which is a list ought to be shallow copied. That's even worse than "list multiplication always copies". At least that is simple and consistent, even if it isn't consistent with the rest of the language, at least it is self-consistent. You are proposing something much worse: special cases to remember. "Objects aren't copied, except for lists, which are copied." And then people will wonder why sets aren't copied, and dicts. People will make a 2D array like so: [[0]*5]*10 and it will work. Then they'll write this: [{}]*5 and wonder why it doesn't work the way they expect. Consistency is *much* more valuable than ad hoc DWIM semantics. Languages that try to Do What I Mean somehow end up Doing What Somebody Else Meant, But Not Me. -- Steven From ian.g.kelly at gmail.com Tue Nov 6 04:19:12 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 6 Nov 2012 02:19:12 -0700 Subject: Multi-dimensional list initialization In-Reply-To: <5098C873.2000200@r3dsolutions.com> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098A55C.3090201@r3dsolutions.com> <5098C873.2000200@r3dsolutions.com> Message-ID: On Tue, Nov 6, 2012 at 1:21 AM, Andrew Robinson wrote: > If you nest it another time; > [[[None]]]*4, the same would happen; all lists would be independent -- but > the objects which aren't lists would be refrenced-- not copied. > > a=[[["alpha","beta"]]]*4 would yield: > a=[[['alpha', 'beta']], [['alpha', 'beta']], [['alpha', 'beta']], [['alpha', > 'beta']]] > and a[0][0]=1 would give [[1],[['alpha', 'beta']], [['alpha', 'beta']], > [['alpha', 'beta']]]] > rather than a=[[1], [1], [1], [1]] > > Or at another level down: a[0][0][0]=1 would give: a=[[[1, 'beta']], > [['alpha', 'beta']], [['alpha', 'beta']], [['alpha', 'beta']] ] > rather than a=[[[1, 'beta']], [[1, 'beta']], [[1, 'beta']], [[1, 'beta']]] You wrote "shallow copy". When the outer-level list is multiplied, the mid-level lists would be copied. Because the copies are shallow, although the mid-level lists are copied, their contents are not. Thus the inner-level lists would still be all referencing the same list. To demonstrate: >>> from copy import copy >>> class ShallowCopyList(list): ... def __mul__(self, number): ... new_list = ShallowCopyList() ... for _ in range(number): ... new_list.extend(map(copy, self)) ... return new_list ... >>> a = ShallowCopyList([[["alpha", "beta"]]]) >>> a [[['alpha', 'beta']]] >>> b = a * 4 >>> b [[['alpha', 'beta']], [['alpha', 'beta']], [['alpha', 'beta']], [['alpha', 'beta']]] >>> b[0][0][0] = 1 >>> b [[[1, 'beta']], [[1, 'beta']], [[1, 'beta']], [[1, 'beta']]] >>> b[0][0] = 1 >>> b [[1], [[1, 'beta']], [[1, 'beta']], [[1, 'beta']]] This shows that assignments at the middle level are independent with a shallow copy on multiplication, but assignments at the inner level are not. In order to achieve the behavior you describe, a deep copy would be needed. > That really is what people *generally* want. > If the entire list is meant to be read only -- the change would affect > *nothing* at all. The time and memory cost of the multiplication operation would become quadratic instead of linear. > See if you can find *any* python program where people desired the > multiplication to have the die effect that changing an object in one of the > sub lists -- changes all the objects in the other sub lists. > > I'm sure you're not going to find it -- and even if you do, it's going to be > 1 program in 1000's. Per the last thread where we discussed extremely rare scenarios, shouldn't you be rounding "1 in 1000s" up to 20%? ;-) From anuradha.raghupathy2010 at gmail.com Tue Nov 6 06:28:05 2012 From: anuradha.raghupathy2010 at gmail.com (anuradha.raghupathy2010 at gmail.com) Date: Tue, 6 Nov 2012 03:28:05 -0800 (PST) Subject: Logging output to be redirected to a particular folder Message-ID: Hi, Below is the python code that I have. I want to redirect the output to my C drive..myapp.log. I am editing and creating scripts in IDLE and running it as a python shell or module. Can you help? import logging def main(): logging.basicConfig(Filename='c://myapp.log', level=logging.ERROR) logging.debug('started debug') logging.info('info printing') logging.warning('test warning') logging.debug('debug again') logging.error('Error') if __name__ == '__main__': main() From __peter__ at web.de Tue Nov 6 07:26:11 2012 From: __peter__ at web.de (Peter Otten) Date: Tue, 06 Nov 2012 13:26:11 +0100 Subject: Logging output to be redirected to a particular folder References: Message-ID: anuradha.raghupathy2010 at gmail.com wrote: > Hi, > > Below is the python code that I have. I want to redirect the output to my > C drive..myapp.log. > > I am editing and creating scripts in IDLE and running it as a python shell > or module. > > Can you help? > > import logging > > def main(): > logging.basicConfig(Filename='c://myapp.log', level=logging.ERROR) Python is case-sensitive. Try: logging.basicConfig(filename='c://myapp.log', level=logging.ERROR) > logging.debug('started debug') > logging.info('info printing') > logging.warning('test warning') > logging.debug('debug again') > logging.error('Error') > > if __name__ == '__main__': > main() From insideshoes at gmail.com Tue Nov 6 07:31:34 2012 From: insideshoes at gmail.com (inshu chauhan) Date: Tue, 6 Nov 2012 13:31:34 +0100 Subject: error Message-ID: I am getting this error while running my programme.. : error: index is out of range I cannot find a valid reason for it in my prog can somebody suggest what may be possible reasons for this error.. The part of the code is : def addpoints (data, points, ix, iy): # makes a list of relevant points if 0 < ix < data.width and 0 < iy < data.height: point = data[ix, iy] if point != (0.0, 0.0, 0.0): points.append(point) return points for dx in xrange(-mask_size2, mask_size2 + 1): for dy in xrange(-mask_size2, mask_size2 + 1): ix, iy = x + dx, y + dy addpoints(data, points, ix , iy ) -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Tue Nov 6 08:05:50 2012 From: d at davea.name (Dave Angel) Date: Tue, 06 Nov 2012 08:05:50 -0500 Subject: error In-Reply-To: References: Message-ID: <50990B2E.9010708@davea.name> On 11/06/2012 07:31 AM, inshu chauhan wrote: > I am getting this error while running my programme.. : > > error: index is out of range Please paste the whole error, including the traceback. It'll show you the line that had the error, as well as the calling sequence that got you there. > > I cannot find a valid reason for it in my prog can somebody suggest what > may be possible reasons for this error.. > > The part of the code is : > > def addpoints (data, points, ix, iy): # makes a list of relevant points > if 0 < ix < data.width and 0 < iy < data.height: > point = data[ix, iy] Tell us the type of the parameters. Perhaps data is of type Zanzibar, and the first subscript is the height, and the second is the width? Show us your class definition, since data is obviously not a built-in collection. > if point != (0.0, 0.0, 0.0): > points.append(point) > return points > > for dx in xrange(-mask_size2, mask_size2 + 1): > for dy in xrange(-mask_size2, mask_size2 + 1): What is the meaning and value of mask_size2 ? Is it a negative int? > ix, iy = x + dx, y + dy > addpoints(data, points, ix , iy ) > > BTW, this function's initial comment is incorrect. It doesn't make a list, it appends to one passed in. And there's no point in returning that list, since it has already been modified. By good convention, a simple function either returns a result or modifies its parameter, it shouldn't do both. Obviously there are exceptions for complex functions, but even then, for a single collection, it should either be modified in place, or created and returned, not both. -- DaveA From shambhu.rajak at calsoftinc.com Tue Nov 6 08:27:27 2012 From: shambhu.rajak at calsoftinc.com (Shambhu Rajak) Date: Tue, 6 Nov 2012 18:57:27 +0530 Subject: Multi-dimensional list initialization In-Reply-To: <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: <01cc01cdbc22$76f39fc0$64dadf40$@calsoftinc.com> Well said Steve, I agree with you... -Shambhu -----Original Message----- From: Steven D'Aprano [mailto:steve+comp.lang.python at pearwood.info] Sent: Tuesday, November 06, 2012 2:35 PM To: python-list at python.org Subject: Re: Multi-dimensional list initialization On Mon, 05 Nov 2012 21:51:24 -0800, Andrew Robinson wrote: > The most compact notation in programming really ought to reflect the > most *commonly* desired operation. Otherwise, we're really just > making people do extra typing for no reason. There are many reasons not to put minimizing of typing ahead of all other values: * Typically, code is written once and read many times. Minimizing typing might save you a second or two once, and then cost you many seconds every time you read the code. That's why we tell people to choose meaningful variable names, instead of naming everything "a" and "b". * Consistency of semantics is better than a plethora of special cases. Python has a very simple and useful rule: objects should not be copied unless explicitly requested to be copied. This is much better than having to remember whether this operation or that operation makes a copy. The answer is consistent: (pardon me for belabouring the point here) Q: Does [0]*10 make ten copies of the integer object? A: No, list multiplication doesn't make copies of elements. Q: How about [0.0]*10? A: No, the elements are never copied. Q: What if I use a singleton? Does [None]*10 try to copy? A: No, the elements are never copied. Q: How about things like file objects that can't be copied? A: No, the elements are never copied. Q: What about [[]]*10? A: No, the elements are never copied. Q: How about if the elements are subclasses of list? A: No, the elements are never copied. Q: What about other mutable objects like sets or dicts? A: No, the elements are never copied. Q: What about instances of custom classes? A: No, the elements are never copied. Q: What if they are old-style Classic classes? A: No, the elements are never copied. Q: What if I do some funny tricks with the metaclass? A: No, the elements are never copied. Q: How about on Tuesdays? I bet they're copied on Tuesdays. A: No, the elements are never copied. Your proposal throws away consistency for a trivial benefit on a rare use- case, and replaces it with a bunch of special cases: Q: What about [[]]*10? A: Oh yeah, I forgot about lists, they're copied. Q: How about if the elements are subclasses of list? A: Hmmm, that's a good one, I'm not actually sure. Q: How about if I use delegation to proxy a list? A: Oh no, they definitely won't be copied. Q: What about other mutable objects like sets or dicts? A: No, definitely not. Unless people complain enough. Q: What about instances of custom classes? A: That's a definite maybe. Q: How about on Tuesdays? I bet they're copied on Tuesdays. A: Only if you're in Belgium. Losing consistency in favour of saving a few characters for something as uncommon as list multiplication is a poor tradeoff. That's why this proposal has been rejected again and again and again every time it has been suggested. List multiplication [x]*n is conceptually equivalent to: newlist = [] for i in range(n): newlist.append(x) or if you prefer a list comp: [x for i in range(n)] This is nice and simple and efficient. Some objects cannot be copied at all. Copying other objects is slow and inefficient. Keeping list multiplication consistent, and fast, is MUCH more important than making it work as expected for the rare case of 2D arrays: [[0]*n]*m where there are other alternatives. > Further, list comprehensions take quite a bit longer to run than low > level copies; by a factor of roughly 10. SO, it really would be worth > implementing the underlying logic -- even if it wasn't super easy. Copying those elements does not come for free. It is true that list multiplication can be much faster than a list comp. But that's because the list multiply doesn't have to inspect the elements, copy them, or engage the iteration machinery. Avoiding copying gives you a big saving: [steve at ando ~]$ python3.3 -m timeit -s "x = range(1000)" "[x for _ in range(100)]" # not copied 100000 loops, best of 3: 11.9 usec per loop [steve at ando utilities]$ python3.3 -m timeit -s "x = range(1000)" "[x[:] for _ in range(100)]" # copied 10000 loops, best of 3: 103 usec per loop So there's a factor of ten difference right there. If list multiplication had to make copies, it would lose much of its speed advantage. For large enough lists, or complicated enough objects, it would become slower than a list comprehension. It would be even slower if list multiplication had to inspect each element first and decide whether or not to copy. > I really don't think doing a shallow copy of lists would break anyone's > program. Anyone who is currently using list multiplication with mutable objects is expecting that they will be the same object, and relying on that fact. Otherwise they wouldn't be using list multiplication. You're suggesting a semantic change. Therefore they will be expecting something different from what actually happens. Result: broken code. It's not just mutable objects. It's also objects that can't be copied. Result: mylist*3 used to work, now it raises an exception. And performance issues: what used to be fast is now slow. Even if this change was allowed, it would have to go through a multi-year process. Python 3.3 is too late -- the absolute earliest would be Python 3.4, which is scheduled for about 18 months from now. So in Python 3.4 you could write: from __future__ import list_multiplication_copying to get the behaviour you want, and then in Python 3.5 it would become the default. That's three years until it becomes the standard. Meanwhile, there will still be millions of people using Python 2.7 or 3.2, and their code will behave differently from your code. Conservatively, if you write code to support three previous releases, that means you can't use this feature until Python 3.7. So that's about six years before it can be used widely. If the problem being solved was big enough, this would be worth doing. But it's not. > The non-list elements, whatever they are, can be left as reference > copies -- but any element which is a list ought to be shallow copied. That's even worse than "list multiplication always copies". At least that is simple and consistent, even if it isn't consistent with the rest of the language, at least it is self-consistent. You are proposing something much worse: special cases to remember. "Objects aren't copied, except for lists, which are copied." And then people will wonder why sets aren't copied, and dicts. People will make a 2D array like so: [[0]*5]*10 and it will work. Then they'll write this: [{}]*5 and wonder why it doesn't work the way they expect. Consistency is *much* more valuable than ad hoc DWIM semantics. Languages that try to Do What I Mean somehow end up Doing What Somebody Else Meant, But Not Me. -- Steven From cyberirakli at gmail.com Tue Nov 6 08:47:03 2012 From: cyberirakli at gmail.com (cyberirakli at gmail.com) Date: Tue, 6 Nov 2012 05:47:03 -0800 (PST) Subject: Base class and Derived class question Message-ID: <31d86df5-8cfe-46b8-ae54-84fbfd82e98e@googlegroups.com> Hey guys, I'm trying to understand how is working base class and derived class. So, I have to files baseClass.py and derivedClass.py. baseClass.py : [CODE]class baseClass(): def bFunction(self): print "We are in a base class"[/CODE] derivedClass.py: [CODE]import baseClass as baseClassMod reload(baseClassMod) class derivedClass(baseClassMod): def dFunction(self): print "We are in a derived Class" [/CODE] buwhen I'm trying to run derivedClass.py I get this error : [CODE][COLOR=Red]TypeError: Error when calling the metaclass bases module.__init__() takes at most 2 arguments (3 given)[/COLOR][/CODE] Interesting thing is that if I run baseClass.py and then run : [CODE]class derivedClass(baseClass): def dFunction(self): print "We are in a derived Class"[/CODE] It works fine From cyberirakli at gmail.com Tue Nov 6 08:50:25 2012 From: cyberirakli at gmail.com (cyberirakli at gmail.com) Date: Tue, 6 Nov 2012 05:50:25 -0800 (PST) Subject: Base class and Derived class question Message-ID: Hey guys, I'm trying to understand how is working base class and derived class. So, I have to files baseClass.py and derivedClass.py. baseClass.py : >>> class baseClass(): def bFunction(self): print "We are in a base class" derivedClass.py: >>>import baseClass as baseClassMod reload(baseClassMod) class derivedClass(baseClassMod): def dFunction(self): print "We are in a derived Class" buwhen I'm trying to run derivedClass.py I get this error : TypeError: Error when calling the metaclass bases module.__init__() takes at most 2 arguments (3 given) Interesting thing is that if I run baseClass.py and then run : >>>class derivedClass(baseClass): def dFunction(self): print "We are in a derived Class" It works fin From roy at panix.com Tue Nov 6 08:52:36 2012 From: roy at panix.com (Roy Smith) Date: Tue, 06 Nov 2012 08:52:36 -0500 Subject: Obnoxious postings from Google Groups References: <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> <50989a16$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <50989a16$0$29980$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > Shell do not [quote strings, etc] because they > are designed for lazy users and merely aim to be "good enough". Well, sort of. Or, perhaps more correctly, "Yes, but that's a good thing". Shells are designed to be interactive tools, where most commands get run once and thrown away. As such, you want everything to be easy to type, which largely means your fingers never leave the main part of the keyboard and you never have to reach for the shift key. The basic unix shell syntax was laid down in the days of the ASR-33. It was slow, hard to type on, and only had a single case of the alphabet (and missing a few pieces of punctuation). Saving keystrokes was an important consideration. Programming languages are designed to write programs. Not only will the code be {used, read, maintained} for a much longer period of time, it will be used by people other than the original author, and on inputs other than originally intended. It needs to be more robust. The problem is that shells got pressed into service as programming languages. At that, they suck. Sure, putting a few commands into a file for reuse was great. Adding a few bells and whistles like variables and conditional execution added greatly to the power of the tool. But, by the time we got to 100 (or 1000!) line shell scripts with functions, loops, arithmetic, etc, things had clearly gone off into the weeds. It's just the wrong tool for that. From d at davea.name Tue Nov 6 09:01:33 2012 From: d at davea.name (Dave Angel) Date: Tue, 06 Nov 2012 09:01:33 -0500 Subject: delete value in list in indexing In-Reply-To: References: Message-ID: <5099183D.9060904@davea.name> On 11/06/2012 01:17 AM, Amit Agrawal wrote: > i want to delete list in upper and lower some specific value > > That question will have to be rephrased, in clearer English. What Python version are you using? What data do you start with, and what data do you want to end up with? Be specific with regards to types. For example, you might be asking: "I am using Python 2.81, on Windows 9. I have a program that's got two strings, the second of which is a single character. I'd like to produce a 3rd string which consists of all characters of the first string that do not match the single character supplied. Further, if the character is an ASCII alpha character, I'd like to do it without regards to ASCII uppercase/lowercase." or "I am using .... I have a program that has a list of ints, and a single value. I'd like to do an in-place removal of all numbers that are within 5 of the specified value." -- DaveA From d at davea.name Tue Nov 6 09:13:18 2012 From: d at davea.name (Dave Angel) Date: Tue, 06 Nov 2012 09:13:18 -0500 Subject: Base class and Derived class question In-Reply-To: References: Message-ID: <50991AFE.3080108@davea.name> On 11/06/2012 08:50 AM, cyberirakli at gmail.com wrote: > Hey guys, > I'm trying to understand how is working base class and derived class. in what Python version ? > So, I have to files baseClass.py and derivedClass.py. > baseClass.py : >>>> class baseClass(): How did all those angle brackets get into the file? Are you confusing an interactive interpreter session with running source files? > def bFunction(self): This line isn't indented properly. It must be further from the left margin than the class declaration. > print "We are in a base class" > > derivedClass.py: >>>> import baseClass as baseClassMod > reload(baseClassMod) reload() isn't safe to use, and especially on a module that's been renamed while it was first imported. It's a trick to speed up debugging in the interactive interpreter, and when something goes wrong, you exit the interpreter and try again. > class derivedClass(baseClassMod): > def dFunction(self): > print "We are in a derived Class" > > buwhen I'm trying to run derivedClass.py I get this error : Again, what are you actually doing? Running that file, or playing around in the interpreter? > TypeError: Error when calling the metaclass bases > module.__init__() takes at most 2 arguments (3 given) > > Interesting thing is that if I run baseClass.py and then run : If you run baseClass.py, and get back to the bash prompt, then nothing will affect subsequent tests. >>>> class derivedClass(baseClass): > def dFunction(self): > print "We are in a derived Class" > It works fin Either use the interpreter or run from the shell. This mixing of the two is mighty confusing. -- DaveA From oscar.j.benjamin at gmail.com Tue Nov 6 09:35:13 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 6 Nov 2012 14:35:13 +0000 Subject: Multi-dimensional list initialization In-Reply-To: <5098A55C.3090201@r3dsolutions.com> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098A55C.3090201@r3dsolutions.com> Message-ID: On Nov 6, 2012 6:00 AM, "Andrew Robinson" wrote: > > On 11/05/2012 06:30 PM, Oscar Benjamin wrote: >> >> stuff = [[obj] * n] * m >> >> I thought that the multiplication of the inner list ([obj] * n) by m >> could create a new list of lists using copies. On closer inspection I >> see that the list being multiplied is in fact [[obj] * n] and that >> this list can only know that it is a list of lists by inspecting its >> element(s) which makes things more complicated. >> >> I retract my claim that this change would be easy to implement. > > In general, people don't use element multiplication (that I have *ever* seen) to make lists where all elements of the outer most list point to the same sub-*list* by reference. The most common use of the multiplication is to fill an array with a constant, or short list of constants; Hence, almost everyone has to work around the issue as the initial poster did by using a much longer construction. That's what I have seen as well. I've never seen an example where someone wanted this behaviour. > > The most compact notation in programming really ought to reflect the most *commonly* desired operation. Otherwise, we're really just making people do extra typing for no reason. It's not so much the typing as the fact that this a common gotcha. Apparently many people expect different behaviour here. I seem to remember finding this surprising at first. > > Further, list comprehensions take quite a bit longer to run than low level copies; by a factor of roughly 10. SO, it really would be worth implementing the underlying logic -- even if it wasn't super easy. > > I really don't think doing a shallow copy of lists would break anyone's program. > The non-list elements, whatever they are, can be left as reference copies -- but any element which is a list ought to be shallow copied. The behavior observed in the opening post where modifying one element of a sub-list, modifies all elements of all sub-lists is never desired as far as I have ever witnessed. It is a semantic change that would, I imagine, break many things in subtle ways. > > The underlying implementation of Python can check an object type trivially, and the only routine needed is a shallow list copy. So, no it really isn't a complicated operation to do shallow copies of lists. Yes but if you're inspecting the object to find out whether to copy it what do you test for? If you check for a list type what about subclasses? What if someone else has a custom list type that is not a subclass? Should there be a dunder method for this? I don't think it's such a simple problem. Oscar -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Tue Nov 6 09:45:14 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 06 Nov 2012 14:45:14 +0000 Subject: delete value in list in indexing In-Reply-To: References: Message-ID: On 06/11/2012 06:17, Amit Agrawal wrote: > i want to delete list in upper and lower some specific value > I think you want to delete a slice so read about slicing here http://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range -- Cheers. Mark Lawrence. From cyberirakli at gmail.com Tue Nov 6 10:03:01 2012 From: cyberirakli at gmail.com (cyberirakli at gmail.com) Date: Tue, 6 Nov 2012 07:03:01 -0800 (PST) Subject: Base class and Derived class question In-Reply-To: References: Message-ID: <23df4f00-2285-44cc-a9aa-aeb319d000fb@googlegroups.com> > in what Python version ? Python 2.7.3 > How did all those angle brackets get into the file? Are you confusing > > an interactive interpreter session with running source files? I've used angle brackets just for posting here,becauze this forum doesn't support [code][/code] I have a file called baseClass.py with code abouve and indentation is correct. Then I open python IDLE and type : import baseClass as baseClassMod reload(baseClassMod class derivedClass(baseClassMod): def dFunction(self): print "We are in a derived Class" After that i get the error above. But if I paste in Python IDLE a code from baseClass.py and just run: class derivedClass(baseClass): def dFunction(self): print "We are in a derived Class" it works perfectly. Why happen this? From cyberirakli at gmail.com Tue Nov 6 10:03:01 2012 From: cyberirakli at gmail.com (cyberirakli at gmail.com) Date: Tue, 6 Nov 2012 07:03:01 -0800 (PST) Subject: Base class and Derived class question In-Reply-To: References: Message-ID: <23df4f00-2285-44cc-a9aa-aeb319d000fb@googlegroups.com> > in what Python version ? Python 2.7.3 > How did all those angle brackets get into the file? Are you confusing > > an interactive interpreter session with running source files? I've used angle brackets just for posting here,becauze this forum doesn't support [code][/code] I have a file called baseClass.py with code abouve and indentation is correct. Then I open python IDLE and type : import baseClass as baseClassMod reload(baseClassMod class derivedClass(baseClassMod): def dFunction(self): print "We are in a derived Class" After that i get the error above. But if I paste in Python IDLE a code from baseClass.py and just run: class derivedClass(baseClass): def dFunction(self): print "We are in a derived Class" it works perfectly. Why happen this? From cyberirakli at gmail.com Tue Nov 6 10:08:57 2012 From: cyberirakli at gmail.com (cyberirakli at gmail.com) Date: Tue, 6 Nov 2012 07:08:57 -0800 (PST) Subject: Base class and Derived class question In-Reply-To: References: Message-ID: <775c0f45-ab55-4683-b9f5-2bbed981ee2d@googlegroups.com> Just got answer, I didn't call a class it's self. Correct code is: class derivedClass(baseClassMod.baseClass): def ...... From ian.g.kelly at gmail.com Tue Nov 6 10:34:44 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 6 Nov 2012 08:34:44 -0700 Subject: Base class and Derived class question In-Reply-To: <23df4f00-2285-44cc-a9aa-aeb319d000fb@googlegroups.com> References: <23df4f00-2285-44cc-a9aa-aeb319d000fb@googlegroups.com> Message-ID: On Tue, Nov 6, 2012 at 8:03 AM, wrote: > I've used angle brackets just for posting here,becauze this forum doesn't support [code][/code] This is a Usenet group, not a web forum. > Just got answer, I didn't call a class it's self. Correct code is: > class derivedClass(baseClassMod.baseClass): > def ...... Better style would be to import the class from the module in the first place: from baseClass import baseClass # ... class derivedClass(baseClass): # ... Better yet would be to put both classes in the same file in the first place. Python isn't Java, where each class is an independent compilation unit. There is no reason to put each class in its own separate module, and it tends to cause namespace confusion as you have discovered. From cyberirakli at gmail.com Tue Nov 6 11:22:31 2012 From: cyberirakli at gmail.com (cyberirakli at gmail.com) Date: Tue, 6 Nov 2012 08:22:31 -0800 (PST) Subject: Base class and Derived class question In-Reply-To: References: <23df4f00-2285-44cc-a9aa-aeb319d000fb@googlegroups.com> Message-ID: <25864595-7a5b-44c6-b0d3-ee5f57590515@googlegroups.com> On Tuesday, November 6, 2012 4:35:47 PM UTC+1, Ian wrote: > On Tue, Nov 6, 2012 at 8:03 AM, > > > I've used angle brackets just for posting here,becauze this forum doesn't support [code][/code] > > > > This is a Usenet group, not a web forum. > > > > > Just got answer, I didn't call a class it's self. Correct code is: > > > class derivedClass(baseClassMod.baseClass): > > > def ...... > > > > Better style would be to import the class from the module in the first place: > > > > from baseClass import baseClass > > > > # ... > > > > class derivedClass(baseClass): > > # ... > > > > Better yet would be to put both classes in the same file in the first > > place. Python isn't Java, where each class is an independent > > compilation unit. There is no reason to put each class in its own > > separate module, and it tends to cause namespace confusion as you have > > discovered. On Tuesday, November 6, 2012 4:35:47 PM UTC+1, Ian wrote: > On Tue, Nov 6, 2012 at 8:03 AM, > > > I've used angle brackets just for posting here,becauze this forum doesn't support [code][/code] > > > > This is a Usenet group, not a web forum. > > > > > Just got answer, I didn't call a class it's self. Correct code is: > > > class derivedClass(baseClassMod.baseClass): > > > def ...... > > > > Better style would be to import the class from the module in the first place: > > > > from baseClass import baseClass > > > > # ... > > > > class derivedClass(baseClass): > > # ... > > > > Better yet would be to put both classes in the same file in the first > > place. Python isn't Java, where each class is an independent > > compilation unit. There is no reason to put each class in its own > > separate module, and it tends to cause namespace confusion as you have > > discovered. On Tuesday, November 6, 2012 4:35:47 PM UTC+1, Ian wrote: > > > I've used angle brackets just for posting here,becauze this forum doesn't support [code][/code] > > > > This is a Usenet group, not a web forum. > > > > > Just got answer, I didn't call a class it's self. Correct code is: > > > class derivedClass(baseClassMod.baseClass): > > > def ...... > > > > Better style would be to import the class from the module in the first place: > > > > from baseClass import baseClass > > > > # ... > > > > class derivedClass(baseClass): > > # ... > > > > Better yet would be to put both classes in the same file in the first > > place. Python isn't Java, where each class is an independent > > compilation unit. There is no reason to put each class in its own > > separate module, and it tends to cause namespace confusion as you have > > discovered. Thank you for reply. Of course, import just a class from the module. The reason of have each class in separate file is that I have a base class with basic functionality and a lot of derived classes from it with custom functionality for each class. Also, the program is modular and periodically will need adding some new modules. So, for better organisation of all this stuff I have put them in separate files. From cyberirakli at gmail.com Tue Nov 6 11:22:31 2012 From: cyberirakli at gmail.com (cyberirakli at gmail.com) Date: Tue, 6 Nov 2012 08:22:31 -0800 (PST) Subject: Base class and Derived class question In-Reply-To: References: <23df4f00-2285-44cc-a9aa-aeb319d000fb@googlegroups.com> Message-ID: <25864595-7a5b-44c6-b0d3-ee5f57590515@googlegroups.com> On Tuesday, November 6, 2012 4:35:47 PM UTC+1, Ian wrote: > On Tue, Nov 6, 2012 at 8:03 AM, > > > I've used angle brackets just for posting here,becauze this forum doesn't support [code][/code] > > > > This is a Usenet group, not a web forum. > > > > > Just got answer, I didn't call a class it's self. Correct code is: > > > class derivedClass(baseClassMod.baseClass): > > > def ...... > > > > Better style would be to import the class from the module in the first place: > > > > from baseClass import baseClass > > > > # ... > > > > class derivedClass(baseClass): > > # ... > > > > Better yet would be to put both classes in the same file in the first > > place. Python isn't Java, where each class is an independent > > compilation unit. There is no reason to put each class in its own > > separate module, and it tends to cause namespace confusion as you have > > discovered. On Tuesday, November 6, 2012 4:35:47 PM UTC+1, Ian wrote: > On Tue, Nov 6, 2012 at 8:03 AM, > > > I've used angle brackets just for posting here,becauze this forum doesn't support [code][/code] > > > > This is a Usenet group, not a web forum. > > > > > Just got answer, I didn't call a class it's self. Correct code is: > > > class derivedClass(baseClassMod.baseClass): > > > def ...... > > > > Better style would be to import the class from the module in the first place: > > > > from baseClass import baseClass > > > > # ... > > > > class derivedClass(baseClass): > > # ... > > > > Better yet would be to put both classes in the same file in the first > > place. Python isn't Java, where each class is an independent > > compilation unit. There is no reason to put each class in its own > > separate module, and it tends to cause namespace confusion as you have > > discovered. On Tuesday, November 6, 2012 4:35:47 PM UTC+1, Ian wrote: > > > I've used angle brackets just for posting here,becauze this forum doesn't support [code][/code] > > > > This is a Usenet group, not a web forum. > > > > > Just got answer, I didn't call a class it's self. Correct code is: > > > class derivedClass(baseClassMod.baseClass): > > > def ...... > > > > Better style would be to import the class from the module in the first place: > > > > from baseClass import baseClass > > > > # ... > > > > class derivedClass(baseClass): > > # ... > > > > Better yet would be to put both classes in the same file in the first > > place. Python isn't Java, where each class is an independent > > compilation unit. There is no reason to put each class in its own > > separate module, and it tends to cause namespace confusion as you have > > discovered. Thank you for reply. Of course, import just a class from the module. The reason of have each class in separate file is that I have a base class with basic functionality and a lot of derived classes from it with custom functionality for each class. Also, the program is modular and periodically will need adding some new modules. So, for better organisation of all this stuff I have put them in separate files. From hansmu at xs4all.nl Tue Nov 6 11:33:10 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Tue, 06 Nov 2012 17:33:10 +0100 Subject: Base class and Derived class question In-Reply-To: <31d86df5-8cfe-46b8-ae54-84fbfd82e98e@googlegroups.com> References: <31d86df5-8cfe-46b8-ae54-84fbfd82e98e@googlegroups.com> Message-ID: <50993bc7$0$6967$e4fe514c@news2.news.xs4all.nl> On 6/11/12 14:47:03, cyberirakli at gmail.com wrote: > Hey guys, > I'm trying to understand how is working base class and derived class. > So, I have to files baseClass.py and derivedClass.py. > baseClass.py : > [CODE]class baseClass(): > def bFunction(self): > print "We are in a base class"[/CODE] > > derivedClass.py: > [CODE]import baseClass as baseClassMod > reload(baseClassMod) > > class derivedClass(baseClassMod): This line is wrong: baseClassMod is a module, not a class. You cannot derive a class from a module, only from another class. If you import baseClass as baseClassMod, then the name of the class defined inside the module is baseClassMod.baseClass, so this line should be: class derivedClass(baseClassMod.baseClass): > def dFunction(self): > print "We are in a derived Class" [/CODE] > > buwhen I'm trying to run derivedClass.py I get this error : > [CODE][COLOR=Red]TypeError: Error when calling the metaclass bases > module.__init__() takes at most 2 arguments (3 given)[/COLOR][/CODE] > > Interesting thing is that if I run baseClass.py and then run : > [CODE]class derivedClass(baseClass): > def dFunction(self): > print "We are in a derived Class"[/CODE] > It works fine Alternative solutions that also work: import baseClass class derivedClass(baseClass.baseClass): def dFunction(self): print "We are in a derived Class" Or you can import the class rather than the module: from baseClass import baseClass class derivedClass(baseClass): def dFunction(self): print "We are in a derived Class" If you're trying to learn about derived classes, then it might be a good idea to avoid learning about the pitfalls of importing at the same time. If you simply put both classes in the same file, the problem disappears: class baseClass(): def bFunction(self): print "We are in a base class" class derivedClass(baseClass): def dFunction(self): print "We are in a derived Class" instance = derivedClass() instance.bFunction() instance.dFunction() This works as you'd expect. Incidentally, the recommended way to avoid confusion between modules and classes it to use lower case names for your modules abd names with an initial capital for your classes, for example: class BaseClass(object): def bMethod(self): print "We are in a base class" class DerivedClass(BaseClass): def dMethod(self): print "We are in a derived Class" instance = DerivedClass() instance.bMethod() instance.dMethod() Hope this helps, -- HansM From no-one at unflyingobject.com Tue Nov 6 11:40:17 2012 From: no-one at unflyingobject.com (filipp) Date: Tue, 6 Nov 2012 18:40:17 +0200 Subject: What the diferences : web.py Tornado Twisted ?! References: Message-ID: <2012110618401790695-noone@unflyingobjectcom> On 2012-11-02 15:50:30 +0000, nepaul said: > What the diferences : web.py Tornado Twisted ?! There really is a lot of material on this online, including numerous videos. But in my experience Tornado is basically a (really cool and easy to use) programmable web server whereas Twisted is a framework for building any kind of network server. I guess you could say one could use Twisted to build Tornado, but not the other way around. :) Just my 2c, haven't used web.py myself. Google is your friend and as always - concentrate on what you're trying to solve and then choose the right tool for the job, not the other way around. :-) hth, -flip -- -filipp http://unflyingobject.com "The future is here. It's just not evenly distributed yet." -- William Gibson From GangGreene at example.com Tue Nov 6 11:51:03 2012 From: GangGreene at example.com (GangGreene) Date: Tue, 6 Nov 2012 11:51:03 -0500 Subject: Obnoxious postings from Google Groups References: <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> <50989a16$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, 06 Nov 2012 08:52:36 -0500, Roy Smith wrote: [putolin] > Programming languages are designed to write programs. Not only will the > code be {used, read, maintained} for a much longer period of time, it > will be used by people other than the original author, and on inputs > other than originally intended. It needs to be more robust. > > The problem is that shells got pressed into service as programming > languages. At that, they suck. Sure, putting a few commands into a > file for reuse was great. Adding a few bells and whistles like > variables and conditional execution added greatly to the power of the > tool. But, by the time we got to 100 (or 1000!) line shell scripts with > functions, loops, arithmetic, etc, things had clearly gone off into the > weeds. It's just the wrong tool for that. Really? I have just finished a 251 line bash shell script that builds my linux distro from scratch. It uses other bash shell scripts that have more lines per file/script and sources the individual package build bash scripts. I used C for the package manager which is only non bash script part of the entire package management system. The only thing I would like to see in bash is to be able to return a string from a function. From ramit.prasad at jpmorgan.com Tue Nov 6 11:52:03 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Tue, 6 Nov 2012 16:52:03 +0000 Subject: Obnoxious postings from Google Groups In-Reply-To: References: <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF47416780689@SCACMX008.exchad.jpmchase.net> Grant Edwards wrote: > On 2012-11-05, Roy Smith wrote: > > In article , > > Chris Angelico wrote: > > > >> It's nothing to do with operating system. File names are names, and > >> spaces in them are seldom worth the hassle unless you manipulate those > >> files solely using a GUI. > > > > That's a very ascii-esqe attitude. In a fully unicode world, I could > > easily see using U+00A0 (NO-BREAK SPACE) in file names, and still have > > space-delimited CLI work just fine. > > No, it wouldn't work just fine. You'd never know when looking at > names whether it was a "regular" space or a "no-break space", and > names would be visually ambiguous. Visually ambiguous names are > horrible. Not to mention that in the GUI I (usually) want the space to be a "break space". > > > But, yeah, in the world we live in today, I try to avoid spaces in > > filenames. But, instead of turning "My File Name" into MyFileName, I'll > > usually do it as My-File-Name or My_File_Name. ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From ramit.prasad at jpmorgan.com Tue Nov 6 12:16:44 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Tue, 6 Nov 2012 17:16:44 +0000 Subject: Obnoxious postings from Google Groups In-Reply-To: <50989613$0$29980$c3e8da3$5496439d@news.astraweb.com> References: <50989613$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF47416780784@SCACMX008.exchad.jpmchase.net> Steven D'Aprano wrote: > > On Mon, 05 Nov 2012 14:47:47 -0500, Dennis Lee Bieber wrote: > [snip] > > Nevertheless, I do tend to prefer underscores to spaces, simply because I > often use naive tools that treat spaces as separators. That is, command > line shells. I visually prefer spaces but it is easier to work with underscores. Thankfully there are plenty of command line utilities for pattern renaming that let me shift to and from spaces as necessary. > > For what it's worth, you can enter any control character in Unix/Linux > systems with readline in bash using the C-q key combination. Newline > needs a bit of special treatment: you need to wrap the name in single > quotes to stop the newline from being interpreted as the end of the > command. > > [steve at ando temp]$ touch 'foo > bar' > > To enter the newline, I typed Ctrl-Q to tell bash to treat the next > character as a literal, and then typed Ctrl-J to get a newline. That sounds complicated, my version of bash lets me type 'foobar' for the same effect. ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From ramit.prasad at jpmorgan.com Tue Nov 6 12:32:12 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Tue, 6 Nov 2012 17:32:12 +0000 Subject: Multi-dimensional list initialization In-Reply-To: References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098A55C.3090201@r3dsolutions.com> <5098C873.2000200@r3dsolutions.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF4741678180D@SCACMX008.exchad.jpmchase.net> Ian Kelly wrote: > > On Tue, Nov 6, 2012 at 1:21 AM, Andrew Robinson > [snip] > > See if you can find *any* python program where people desired the > > multiplication to have the die effect that changing an object in one of the > > sub lists -- changes all the objects in the other sub lists. > > > > I'm sure you're not going to find it -- and even if you do, it's going to be > > 1 program in 1000's. > > Per the last thread where we discussed extremely rare scenarios, > shouldn't you be rounding "1 in 1000s" up to 20%? ;-) Actually, I would be surprised if it was even 1 in 1000. Of course, consistency makes it easier to learn and *remember*. I value that far more than a minor quirk that is unlikely to bother me now that I know of it. Well, at least not as long as I do not forget my morning coffee/tea :) ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From ramit.prasad at jpmorgan.com Tue Nov 6 12:58:21 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Tue, 6 Nov 2012 17:58:21 +0000 Subject: Logging output to be redirected to a particular folder In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF474167818DD@SCACMX008.exchad.jpmchase.net> Dennis Lee Bieber wrote: > > On Tue, 06 Nov 2012 13:26:11 +0100, Peter Otten <__peter__ at web.de> > declaimed the following in gmane.comp.python.general: > > > anuradha.raghupathy2010 at gmail.com wrote: [snip] > > > def main(): > > > logging.basicConfig(Filename='c://myapp.log', level=logging.ERROR) > > > > Python is case-sensitive. Try: > > > > logging.basicConfig(filename='c://myapp.log', level=logging.ERROR) > > > The double forward slashes might also be confusing... At the least, > unneeded... > > >>> import os.path > >>> print os.path.normpath("c://somefile.log") > c:\somefile.log > >>> print os.path.normpath("c:\\somefile.log") > c:\somefile.log > >>> print os.path.normpath("c:\\tryfile.log") > c:\tryfile.log > >>> print os.path.normpath("c:\tryfile.log") > c: ryfile.log > >>> print os.path.normpath("c:/tryfile.log") > c:\tryfile.log > >>> > > Doubling back-slashes is needed to avoid the problem of literal > escapes corrupting the intent... Or use the raw literal form r"c:\tryfile.log". I know several people that prefer to use forward slashes as it works in both Windows and *nix. ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From ramit.prasad at jpmorgan.com Tue Nov 6 13:00:02 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Tue, 6 Nov 2012 18:00:02 +0000 Subject: who can give me some practical tutorials on django 1.4 or 1.5? In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF47416781903@SCACMX008.exchad.jpmchase.net> Levi Nie wrote: > > Who can give me some practical tutorials on django 1.4 or 1.5? > Thank you. Maybe this will help: http://gettingstartedwithdjango.com/resources/ ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From woooee at gmail.com Tue Nov 6 14:59:50 2012 From: woooee at gmail.com (woooee) Date: Tue, 6 Nov 2012 11:59:50 -0800 (PST) Subject: error References: Message-ID: <78f26f00-2c91-47b3-aef0-3eaeb8ae6f91@u4g2000pbo.googlegroups.com> >From this line, "data" appears to be a class if 0 < ix < data.width and 0 < iy < data.height: >From this line, "data" appears to be a list, although a two dimensional list would be accessed as data[ix][iy] ? ? ? ? point = data[ix, iy] Also, returning a list from a function is a matter of preference. Some people argue that it should be returned to make it obvious. If you do not know the difference between what is mutable and what is not mutable, then return everything until you do. From Nicolas.Graner at u-psud.fr Tue Nov 6 16:09:18 2012 From: Nicolas.Graner at u-psud.fr (Nicolas Graner) Date: Tue, 6 Nov 2012 22:09:18 +0100 Subject: clicking on turtle Message-ID: <201211062109.qA6L9IvA006105@hydra.calcul> I have a problem with the standard "turtle" module. When a turtle has a custom shape of type "compound", it doesn't seem to respond to click events. No problem with polygon shapes. Running python 3.2.3, turtle version 1.1b on Windows XP. Here is my test file: ################################################## import turtle square = ((0,0),(0,20),(20,20),(20,0)) turtle.addshape("sq1", square) # sq1 = polygon shape s = turtle.Shape("compound") s.addcomponent(square, "red") turtle.addshape("sq2", s) # sq2 = compound shape t1 = turtle.Turtle(shape="sq1") t2 = turtle.Turtle(shape="sq2") t2.fd(20) # set the turtles side by side def click(x,y): print("click at",x,y) t1.onclick(click) t2.onclick(click) turtle.mainloop() ################################################## When I run this and click on the black square (i.e. t1), the message "click at..." gets printed on the console. When I click on the red square (i.e. t2), nothing happens. Bug or feature? --Nicolas From python at graner.name Tue Nov 6 16:13:21 2012 From: python at graner.name (python at graner.name) Date: Tue, 6 Nov 2012 22:13:21 +0100 Subject: clicking on turtle Message-ID: <201211062113.qA6LDLGW006119@hydra.calcul> I have a problem with the standard "turtle" module. When a turtle has a custom shape of type "compound", it doesn't seem to respond to click events. No problem with polygon shapes. Running python 3.2.3, turtle version 1.1b on Windows XP. Here is my test file: ################################################## import turtle square = ((0,0),(0,20),(20,20),(20,0)) turtle.addshape("sq1", square) # sq1 = polygon shape s = turtle.Shape("compound") s.addcomponent(square, "red") turtle.addshape("sq2", s) # sq2 = compound shape t1 = turtle.Turtle(shape="sq1") t2 = turtle.Turtle(shape="sq2") t2.fd(20) # set the turtles side by side def click(x,y): print("click at",x,y) t1.onclick(click) t2.onclick(click) turtle.mainloop() ################################################## When I run this and click on the black square (i.e. t1), the message "click at..." gets printed on the console. When I click on the red square (i.e. t2), nothing happens. Bug or feature? --Nicolas From andrew3 at r3dsolutions.com Tue Nov 6 16:14:10 2012 From: andrew3 at r3dsolutions.com (Andrew Robinson) Date: Tue, 06 Nov 2012 13:14:10 -0800 Subject: Multi-dimensional list initialization In-Reply-To: References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098A55C.3090201@r3dsolutions.com> Message-ID: <50997DA2.20401@r3dsolutions.com> On 11/06/2012 06:35 AM, Oscar Benjamin wrote: > > > In general, people don't use element multiplication (that I have > *ever* seen) to make lists where all elements of the outer most list > point to the same sub-*list* by reference. The most common use of the > multiplication is to fill an array with a constant, or short list of > constants; Hence, almost everyone has to work around the issue as > the initial poster did by using a much longer construction. > > That's what I have seen as well. I've never seen an example where > someone wanted this behaviour. > > > > > The most compact notation in programming really ought to reflect the > most *commonly* desired operation. Otherwise, we're really just > making people do extra typing for no reason. > > It's not so much the typing as the fact that this a common gotcha. > Apparently many people expect different behaviour here. I seem to > remember finding this surprising at first. > :) That's true as well. > > > > > Further, list comprehensions take quite a bit longer to run than low > level copies; by a factor of roughly 10. SO, it really would be worth > implementing the underlying logic -- even if it wasn't super easy. > > > > I really don't think doing a shallow copy of lists would break > anyone's program. > > The non-list elements, whatever they are, can be left as reference > copies -- but any element which is a list ought to be shallow copied. > The behavior observed in the opening post where modifying one element > of a sub-list, modifies all elements of all sub-lists is never desired > as far as I have ever witnessed. > > It is a semantic change that would, I imagine, break many things in > subtle ways. > ?? Do you have any guesses, how ? > > > > > The underlying implementation of Python can check an object type > trivially, and the only routine needed is a shallow list copy. So, no > it really isn't a complicated operation to do shallow copies of lists. > > Yes but if you're inspecting the object to find out whether to copy it > what do you test for? If you check for a list type what about > subclasses? What if someone else has a custom list type that is not a > subclass? Should there be a dunder method for this? > No dunder methods. :) Custom non-subclass list types aren't a common usage for list multiplication in any event. At present one has to do list comprehensions for that, and that would simply remain so. Subclasses, however, are something I hadn't considered... > I don't think it's such a simple problem. > > Oscar > You made a good point, Oscar; I'll have to think about the subclassing a bit. :) From andrew3 at r3dsolutions.com Tue Nov 6 16:19:51 2012 From: andrew3 at r3dsolutions.com (Andrew Robinson) Date: Tue, 06 Nov 2012 13:19:51 -0800 Subject: Multi-dimensional list initialization In-Reply-To: <5B80DD153D7D744689F57F4FB69AF4741678180D@SCACMX008.exchad.jpmchase.net> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098A55C.3090201@r3dsolutions.com> <5098C873.2000200@r3dsolutions.com> <5B80DD153D7D744689F57F4FB69AF4741678180D@SCACMX008.exchad.jpmchase.net> Message-ID: <50997EF7.5000007@r3dsolutions.com> On 11/06/2012 09:32 AM, Prasad, Ramit wrote: > Ian Kelly wrote: >> On Tue, Nov 6, 2012 at 1:21 AM, Andrew Robinson >> > [snip] >>> See if you can find *any* python program where people desired the >>> multiplication to have the die effect that changing an object in one of the >>> sub lists -- changes all the objects in the other sub lists. >>> >>> I'm sure you're not going to find it -- and even if you do, it's going to be >>> 1 program in 1000's. >> Per the last thread where we discussed extremely rare scenarios, >> shouldn't you be rounding "1 in 1000s" up to 20%? ;-) :D -- Ian -- also consider that I *am* willing to use extra memory. Not everything can be shrunk to nothing and still remain functional. :) So, it isn't *all* about *micro* optimization -- it's also about psychology and flexibility. > Actually, I would be surprised if it was even 1 in 1000. > Of course, consistency makes it easier to learn and *remember*. > I value that far more than a minor quirk that is unlikely to > bother me now that I know of it. Well, at least not as long as > I do not forget my morning coffee/tea :) But, having it copy lists -- when the only purpose of multiplication is for lists; is only a minor quirk as well. > > > ~Ramit From andrew3 at r3dsolutions.com Tue Nov 6 16:36:10 2012 From: andrew3 at r3dsolutions.com (Andrew Robinson) Date: Tue, 06 Nov 2012 13:36:10 -0800 Subject: Multi-dimensional list initialization In-Reply-To: References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098A55C.3090201@r3dsolutions.com> <5098C873.2000200@r3dsolutions.com> Message-ID: <509982CA.7050701@r3dsolutions.com> On 11/06/2012 01:19 AM, Ian Kelly wrote: > On Tue, Nov 6, 2012 at 1:21 AM, Andrew Robinson > >> If you nest it another time; >> [[[None]]]*4, the same would happen; all lists would be independent -- but >> the objects which aren't lists would be refrenced-- not copied. >> >> a=[[["alpha","beta"]]]*4 would yield: >> a=[[['alpha', 'beta']], [['alpha', 'beta']], [['alpha', 'beta']], [['alpha', >> 'beta']]] >> and a[0][0]=1 would give [[1],[['alpha', 'beta']], [['alpha', 'beta']], >> [['alpha', 'beta']]]] >> rather than a=[[1], [1], [1], [1]] >> >> Or at another level down: a[0][0][0]=1 would give: a=[[[1, 'beta']], >> [['alpha', 'beta']], [['alpha', 'beta']], [['alpha', 'beta']] ] >> rather than a=[[[1, 'beta']], [[1, 'beta']], [[1, 'beta']], [[1, 'beta']]] > You wrote "shallow copy". When the outer-level list is multiplied, > the mid-level lists would be copied. Because the copies are shallow, > although the mid-level lists are copied, their contents are not. Thus > the inner-level lists would still be all referencing the same list. > To demonstrate: I meant all lists are shallow copied from the innermost level out. Equivalently, it's a deep copy of list objects -- but a shallow copy of any list contents except other lists. > >>>> from copy import copy >>>> class ShallowCopyList(list): > ... def __mul__(self, number): > ... new_list = ShallowCopyList() > ... for _ in range(number): > ... new_list.extend(map(copy, self)) > ... return new_list > ... That type of copy is not equivalent to what I meant; It's a shallow copy only of non-list objects. > This shows that assignments at the middle level are independent with a > shallow copy on multiplication, but assignments at the inner level are > not. In order to achieve the behavior you describe, a deep copy would > be needed. Yes, it can be considered a deep copy of *all* list objects -- but not of non list contents. It's a terminology issue -- and you're right -- I need to be more precise. >> That really is what people *generally* want. >> If the entire list is meant to be read only -- the change would affect >> *nothing* at all. > The time and memory cost of the multiplication operation would become > quadratic instead of linear. Perhaps, but the copy would still not be _nearly_ as slow as a list comprehension !!! Being super fast when no one uses the output -- is , "going nowhere fast." I think It's better to get at the right place at a medium speed than nowhere fast; List comprehensions *do* get to the right place, but *quite* slowly. They are both quadratic, *and* multiple tokenized steps. :) -------------- next part -------------- An HTML attachment was scrubbed... URL: From andrew3 at r3dsolutions.com Tue Nov 6 17:41:24 2012 From: andrew3 at r3dsolutions.com (Andrew Robinson) Date: Tue, 06 Nov 2012 14:41:24 -0800 Subject: Multi-dimensional list initialization In-Reply-To: <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50999214.50100@r3dsolutions.com> On 11/06/2012 01:04 AM, Steven D'Aprano wrote: > On Mon, 05 Nov 2012 21:51:24 -0800, Andrew Robinson wrote: > >> The most compact notation in programming really ought to reflect the >> most *commonly* desired operation. Otherwise, we're really just making >> people do extra typing for no reason. > There are many reasons not to put minimizing of typing ahead of all other > values: I didn't. I put it ahead of *some* values for the sake of practicality and human psychology. " Practicality beats purity. " > > * Typically, code is written once and read many times. Minimizing > typing might save you a second or two once, and then cost you many > seconds every time you read the code. That's why we tell people to > choose meaningful variable names, instead of naming everything "a" > and "b". Yes. But this isn't going to cost any more time than figuring out whether or not the list multiplication is going to cause quirks, itself. Human psychology *tends* (it's a FAQ!) to automatically assume the purpose of the list multiplication is to pre-allocate memory for the equivalent (using lists) of a multi-dimensional array. Note the OP even said "4d array". The OP's original construction was simple, elegant, easy to read and very commonly done by newbies learning the language because it's *intuitive*. His second try was still intuitive, but less easy to read, and not as elegant. > > * Consistency of semantics is better than a plethora of special > cases. Python has a very simple and useful rule: objects should > not be copied unless explicitly requested to be copied. This is > much better than having to remember whether this operation or > that operation makes a copy. The answer is consistent: Bull. Even in the last thread I noted the range() object produces special cases. >>> range(0,5)[1] 1 >>> range(0,5)[1:3] range(1, 3) >>> The principle involved is that it gives you what you *usually* want; I read some of the documentation on why Python 3 chose to implement it this way. > > (pardon me for belabouring the point here) > > Q: Does [0]*10 make ten copies of the integer object? > A: No, list multiplication doesn't make copies of elements. Neither would my idea for the vast majority of things on your first list. Q: What about [[]]*10? A: No, the elements are never copied. YES! For the obvious reason that such a construction is making mutable lists that the user wants to populate later. If they *didn't* want to populate them later, they ought to have used tuples -- which take less overhead. Who even does this thing you are suggesting?! >>> a=[[]]*10 >>> a [[], [], [], [], [], [], [], [], [], []] >>> a[0].append(1) >>> a [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]] Oops! Damn, not what anyone normal wants.... > Q: How about if the elements are subclasses of list? > A: No, the elements are never copied. Another poster brought that point up -- it's something I would have to study before answering. It's a valid objection. > > Q: What about other mutable objects like sets or dicts? > A: No, the elements are never copied. They aren't list multiplication compatible in any event! It's a total nonsense objection. If these are inconsistent in my idea -- OBVIOUSLY -- they are inconsistent in Python's present implementation. You can't even reference duplicate them NOW. >>> { 1:'a', 2:'b', 3:'c' } * 2 Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type(s) for *: 'dict' and 'int' > Q: How about on Tuesdays? I bet they're copied on Tuesdays. > A: No, the elements are never copied. That's really a stupid objection, and everyone knows it. " Although that way may not be obvious at first unless you're Dutch. " > > Your proposal throws away consistency for a trivial benefit on a rare use- > case, and replaces it with a bunch of special cases: RARE!!!! You are NUTS!!!! > Q: What about [[]]*10? > A: Oh yeah, I forgot about lists, they're copied. Yup. > Q: How about if the elements are subclasses of list? > A: Hmmm, that's a good one, I'm not actually sure. > > Q: How about if I use delegation to proxy a list? > A: Oh no, they definitely won't be copied. Give an example usage of why someone would want to do this. Then we can discuss it. > Q: What about other mutable objects like sets or dicts? > A: No, definitely not. Unless people complain enough. now you're just repeating yourself to make your contrived list longer -- but there's no new objections... > Losing consistency in favour of saving a few characters for something as > uncommon as list multiplication is a poor tradeoff. That's why this > proposal has been rejected again and again and again every time it has > been suggested. Please link to the objection being proposed to the developers, and their reasoning for rejecting it. I think you are exaggerating. > List multiplication [x]*n is conceptually equivalent to: > > This is nice and simple and efficient. No it isn't efficient. It's *slow* when done as in your example. > Copying other objects is slow and inefficient. Keeping list > multiplication consistent, and fast, is MUCH more important than making > it work as expected for the rare case of 2D arrays: I don't think so -- again, look at range(); it was made to work inconsistent for a "common" case. Besides, 2D arrays are *not* rare and people *have* to copy internals of them very often. The copy speed will be the same or *faster*, and the typing less -- and the psychological mistakes *less*, the elegance more. It's hardly going to confuse anyone to say that lists are copied with list multiplication, but the elements are not. Every time someone passes a list to a function, they *know* that the list is passed by value -- and the elements are passed by reference. People in Python are USED to lists being "the" way to weird behavior that other languages don't do. > > Copying those elements does not come for free. > > It is true that list multiplication can be much faster than a list comp. > But that's because the list multiply doesn't have to inspect the > elements, copy them, or engage the iteration machinery. Avoiding copying > gives you a big saving: > > > [steve at ando ~]$ python3.3 -m timeit -s "x = range(1000)" > "[x for _ in range(100)]" # not copied > 100000 loops, best of 3: 11.9 usec per loop > > [steve at ando utilities]$ python3.3 -m timeit -s "x = range(1000)" > "[x[:] for _ in range(100)]" # copied > 10000 loops, best of 3: 103 usec per loop > > So there's a factor of ten difference right there. If list multiplication > had to make copies, it would lose much of its speed advantage. And when multiplication doesn't make copies of *lists*, it's going "nowhere fast", because people don't want the results that gives. So what difference does it make? People won't make the construction unless they wanted to make the copies in the first place. If they want the copies, well -- copies are *slow*. Big deal. > For large > enough lists, or complicated enough objects, it would become slower than > a list comprehension. Huh? You're nuts. > It would be even slower if list multiplication had to inspect each > element first and decide whether or not to copy. A single pointer comparison in a 'C' for loop takes less than 5 nano seconds on a 1Ghz machine. (I'll bet yours is faster than that...!) Consider: list objects have a pointer which points back to the generic list object -- that's all it takes to determine what the "type" is. Your measured loop times, doing list comprehensions takes over 10 microseconds *per loop*. Compared to what you're proposing -- The pointer compare is a mere 0.05% change; You can't even measure that with "timeit!". BUT: The increase in speed for not running tokenized "for" loops is *much* bigger than the loss for a single pointer compare; so it will *usually* be a *serious* net gain. >> I really don't think doing a shallow copy of lists would break anyone's >> program. > Anyone who is currently using list multiplication with mutable objects is > expecting that they will be the same object, and relying on that fact. > Otherwise they wouldn't be using list multiplication. yes, and I'm not changing that -- except for lists; and *no* one is using that. Find two examples of it from existing non contrived web examples of Python code. *ask* around. > > You're suggesting a semantic change. Therefore they will be expecting > something different from what actually happens. Result: broken code. Even if it was; So are many semantic changes happening between python 2 and python 3. Look at what python 2 did: >>> range(0,5)[0] 0 >>> range(0,5)[1:3] [1, 2] That's a *semantic* change. Also; if you complain that xrange has been renamed range; then look: >>> xrange(0,5)[0] 0 >>> xrange(0,5)[1:3] Traceback (most recent call last): File "", line 1, in TypeError: sequence index must be integer, not 'slice' WOW. WOW. WOW. An even BIGGER semantic change. > It's not just mutable objects. It's also objects that can't be copied. > Result: mylist*3 used to work, now it raises an exception. And > performance issues: what used to be fast is now slow. Where do you get off??; a list can be copied -- the contents might not. > Even if this change was allowed, it would have to go through a multi-year > process. Fine. if that's normal -- then let them process it the normal way. That's not my concern in the slightest. > to get the behaviour you want, and then in Python 3.5 it would become the > default. That's three years until it becomes the standard. Meanwhile, > there will still be millions of people using Python 2.7 or 3.2, and their > code will behave differently from your code. Uh, they aren't *using* the construction I am proposing now -- they are avoiding it like the plague. Hence, it will merely become a new ability in a few years -- not 'differently' behaving code. The rest of your repetitive nonsense has been deleted. :( From ian.g.kelly at gmail.com Tue Nov 6 17:46:52 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 6 Nov 2012 15:46:52 -0700 Subject: Multi-dimensional list initialization In-Reply-To: <509982CA.7050701@r3dsolutions.com> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098A55C.3090201@r3dsolutions.com> <5098C873.2000200@r3dsolutions.com> <509982CA.7050701@r3dsolutions.com> Message-ID: On Tue, Nov 6, 2012 at 2:36 PM, Andrew Robinson wrote: > I meant all lists are shallow copied from the innermost level out. > Equivalently, it's a deep copy of list objects -- but a shallow copy of any list contents except other lists. Why only list objects, though? When a user writes [[]] * 10, they probably want a list containing ten distinct nested lists. Likewise, when a user writes [{}] * 10, they probably want a list containing ten distinct dicts, which is not at all an uncommon thing to want. It seems very inconsistent that the former should work while the latter should not. This is especially true when you start mixing the two paradigms; the user might expect [[{}] * 10] * 10 to create a a 10x10 matrix where each element is a distinct dict, but this still would not work, even though the nested lists would all have different identities. What about ([],) * 10? This is perhaps best interpreted as a request to create a matrix of ten rows where the rows themselves are mutable but the collection of rows is not. If list multiplication were to copy nested lists, then should tuple multiplication do the same? From steve+comp.lang.python at pearwood.info Tue Nov 6 17:50:59 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 06 Nov 2012 22:50:59 GMT Subject: Obnoxious postings from Google Groups References: <50989613$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50999452$0$29980$c3e8da3$5496439d@news.astraweb.com> On Tue, 06 Nov 2012 17:16:44 +0000, Prasad, Ramit wrote: >> To enter the newline, I typed Ctrl-Q to tell bash to treat the next >> character as a literal, and then typed Ctrl-J to get a newline. > > That sounds complicated, my version of bash lets me type > 'foobar' for the same effect. Well, I learned something new about bash. On the other hand, the Ctrl-Q next-char-is-literal trick works for entering control characters that otherwise don't have a key on the keyboard. -- Steven From ramit.prasad at jpmorgan.com Tue Nov 6 18:08:11 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Tue, 6 Nov 2012 23:08:11 +0000 Subject: Obnoxious postings from Google Groups In-Reply-To: <50999452$0$29980$c3e8da3$5496439d@news.astraweb.com> References: <50989613$0$29980$c3e8da3$5496439d@news.astraweb.com> <50999452$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF47416783A68@SCACMX008.exchad.jpmchase.net> Steven D'Aprano wrote: > > On Tue, 06 Nov 2012 17:16:44 +0000, Prasad, Ramit wrote: > > >> To enter the newline, I typed Ctrl-Q to tell bash to treat the next > >> character as a literal, and then typed Ctrl-J to get a newline. > > > > That sounds complicated, my version of bash lets me type > > 'foobar' for the same effect. > > Well, I learned something new about bash. > > On the other hand, the Ctrl-Q next-char-is-literal trick works for > entering control characters that otherwise don't have a key on the > keyboard. > Would you mind elaborating on how this works? I know it's not a bash list, but I do not understand how ctrl-J is considered a literal. Obviously, I must have a different definition of "literal". Where can I find a list of other literals? My Google-fu is being weak today. :( ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From redstone-cold at 163.com Tue Nov 6 18:12:46 2012 From: redstone-cold at 163.com (iMath) Date: Tue, 6 Nov 2012 15:12:46 -0800 (PST) Subject: How to only get a list of the names of the non-directory files in current directory ('.')? In-Reply-To: References: Message-ID: <31a0765f-c366-4f19-8d6c-bc95e0e51265@googlegroups.com> ? 2012?11?6????UTC+8??1?24?41??Chris Angelico??? > On Tue, Nov 6, 2012 at 4:19 PM, iMath wrote: > > > How to only get a list of the names of the non-directory files in current directory ('.')? > > > (Note excluding its subdirectories ). > > > > > > I need the code : ) > > > > Start by getting a list of names of everything in the current directory. > > > > Then filter that list by testing each one to see if it's a directory. > > > > Tip: The second step can be done with os.path.isdir > > > > Put some code together and try it. If you have trouble, post your > > code, any exception traceback you get, and what you're having > > difficulty with, and we'll go on from there. > > > > Have fun! > > > > ChrisA how to get a list of names of everything in the current directory ? From redstone-cold at 163.com Tue Nov 6 18:12:46 2012 From: redstone-cold at 163.com (iMath) Date: Tue, 6 Nov 2012 15:12:46 -0800 (PST) Subject: How to only get a list of the names of the non-directory files in current directory ('.')? In-Reply-To: References: Message-ID: <31a0765f-c366-4f19-8d6c-bc95e0e51265@googlegroups.com> ? 2012?11?6????UTC+8??1?24?41??Chris Angelico??? > On Tue, Nov 6, 2012 at 4:19 PM, iMath wrote: > > > How to only get a list of the names of the non-directory files in current directory ('.')? > > > (Note excluding its subdirectories ). > > > > > > I need the code : ) > > > > Start by getting a list of names of everything in the current directory. > > > > Then filter that list by testing each one to see if it's a directory. > > > > Tip: The second step can be done with os.path.isdir > > > > Put some code together and try it. If you have trouble, post your > > code, any exception traceback you get, and what you're having > > difficulty with, and we'll go on from there. > > > > Have fun! > > > > ChrisA how to get a list of names of everything in the current directory ? From steve+comp.lang.python at pearwood.info Tue Nov 6 18:14:40 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 06 Nov 2012 23:14:40 GMT Subject: Obnoxious postings from Google Groups References: <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> <50989a16$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: <509999e0$0$29980$c3e8da3$5496439d@news.astraweb.com> On Tue, 06 Nov 2012 11:51:03 -0500, GangGreene wrote: > I have just finished a 251 line bash shell script that builds my linux > distro from scratch. "From scratch"? So if you run it on bare metal with no OS, it works? :-P But seriously -- bash is a mature, powerful shell. It works well for what it does. It has been designed to make it easy[1] to automate system admin tasks. It would be astonishing if an experienced, competent bash programmer couldn't write an installation tool in bash. By comparison, few general purpose programming languages (with the obvious exception of perl) are designed for system administration as their primary purpose. But... how robust is your script? How many bugs does it contain? Chances are you will only use it a handful of times, on the same system. That's not a lot of testing to be sure that it is free of bugs, and robust against unexpected input. Hell, even widely used and distributed install scripts written by companies with the resources of Ubuntu and Red Hat have a distressing tendency to break. Or worse, to behave in unexpected ways. Unless you are some sort of bash-scripting ?ber-geek superhero with powers beyond those of mortal men, chances are that your script is much more buggy and fragile than you imagine. How well does it recover from errors? Does it leave you with a broken system, half installed? How easily can you maintain it after three months? Will it work in the presence of filenames with newlines in them? [1] For some definition of "easy". In my opinion, control structures like "for" and "if" in bash are hard to use, hard to read, easy to get wrong, give cryptic error messages when you do get them wrong, and are ugly. Tests are obfuscated, and as always, the fact that everything is text in bash means it is way harder than it needs to be to use rich, modern data structures. -- Steven From steve+comp.lang.python at pearwood.info Tue Nov 6 18:20:25 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 06 Nov 2012 23:20:25 GMT Subject: clicking on turtle References: Message-ID: <50999b39$0$29980$c3e8da3$5496439d@news.astraweb.com> On Tue, 06 Nov 2012 22:13:21 +0100, python wrote: > I have a problem with the standard "turtle" module. When a turtle has a > custom shape of type "compound", it doesn't seem to respond to click > events. No problem with polygon shapes. [...] > When I run this and click on the black square (i.e. t1), the message > "click at..." gets printed on the console. When I click on the red > square (i.e. t2), nothing happens. I don't know enough about turtle graphics to tell whether it's a bug in your code, or a bug in the turtle, but I can confirm that the same behaviour occurs in Python 2.7 on Linux. -- Steven From gordon at panix.com Tue Nov 6 18:23:07 2012 From: gordon at panix.com (John Gordon) Date: Tue, 6 Nov 2012 23:23:07 +0000 (UTC) Subject: How to only get a list of the names of the non-directory files in current directory ('.')? References: Message-ID: In iMath writes: > how to get a list of names of everything in the current directory ? Try os.listdir() . -- John Gordon A is for Amy, who fell down the stairs gordon at panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" From ramit.prasad at jpmorgan.com Tue Nov 6 18:39:17 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Tue, 6 Nov 2012 23:39:17 +0000 Subject: Multi-dimensional list initialization In-Reply-To: <50999214.50100@r3dsolutions.com> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <50999214.50100@r3dsolutions.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF47416783AF3@SCACMX008.exchad.jpmchase.net> Andrew Robinson wrote: > > On 11/06/2012 01:04 AM, Steven D'Aprano wrote: > > On Mon, 05 Nov 2012 21:51:24 -0800, Andrew Robinson wrote: > > [snip] > > Q: What about other mutable objects like sets or dicts? > > A: No, the elements are never copied. > They aren't list multiplication compatible in any event! It's a total > nonsense objection. > > If these are inconsistent in my idea -- OBVIOUSLY -- they are > inconsistent in Python's present implementation. You can't even > reference duplicate them NOW. > > >>> { 1:'a', 2:'b', 3:'c' } * 2 > Traceback (most recent call last): > File "", line 1, in > TypeError: unsupported operand type(s) for *: 'dict' and 'int' >>> z = [ {'a':1} ]*10 >>> z[0]['b'] = 4 >>> z [{'a': 1, 'b': 4}, {'a': 1, 'b': 4}, {'a': 1, 'b': 4},{'a': 1, 'b': 4}, {'a': 1, 'b': 4}, {'a': 1, 'b': 4}, {'a': 1, 'b': 4}, {'a': 1, 'b': 4}, {'a': 1, 'b': 4}, {'a': 1, 'b': 4}] Should that copy the dictionary? According to logical reasoning it should copy the dictionary as well. How do you draw the line of what should be copied and what should not? > > > Q: How about on Tuesdays? I bet they're copied on Tuesdays. > > A: No, the elements are never copied. > That's really a stupid objection, and everyone knows it. Agreed. [snip] > > Q: How about if I use delegation to proxy a list? > > A: Oh no, they definitely won't be copied. > Give an example usage of why someone would want to do this. Then we can > discuss it. IIRC, someone wanted to do something very similar for dictionaries to prevent editing of global variables. > > Q: What about other mutable objects like sets or dicts? > > A: No, definitely not. Unless people complain enough. > now you're just repeating yourself to make your contrived list longer -- > but there's no new objections... This is my main objection and one of the flaws of your argument. You want to handle one type of mutable objects completely separately than other mutable objects. Why is list any different than dictionary in this respect? The only reason I can imagine is because lists end up being used for 2d (or higher) "matrices". > > > Losing consistency in favour of saving a few characters for something as > > uncommon as list multiplication is a poor tradeoff. That's why this > > proposal has been rejected again and again and again every time it has > > been suggested. > Please link to the objection being proposed to the developers, and their > reasoning for rejecting it. > I think you are exaggerating. I reject (as a developer) it because it forces me to remember a very specific quirk versus a simple (logical) rule that applies to all objects. Not to mention that the quirk is not even that useful except for beginners. > > > List multiplication [x]*n is conceptually equivalent to: > > > > This is nice and simple and efficient. > No it isn't efficient. It's *slow* when done as in your example. > > > Copying other objects is slow and inefficient. Keeping list > > multiplication consistent, and fast, is MUCH more important than making > > it work as expected for the rare case of 2D arrays: > I don't think so -- again, look at range(); it was made to work > inconsistent for a "common" case. > > Besides, 2D arrays are *not* rare and people *have* to copy internals of > them very often. > The copy speed will be the same or *faster*, and the typing less -- and > the psychological mistakes *less*, the elegance more. > > It's hardly going to confuse anyone to say that lists are copied with > list multiplication, but the elements are not. > > Every time someone passes a list to a function, they *know* that the > list is passed by value -- and the elements are passed by reference. > People in Python are USED to lists being "the" way to weird behavior > that other languages don't do. I think you just lost 90% of your credibility (with me). When did lists get passed by value? Python uses call by sharing[0]. Terminology aside, lists are handled exactly the same way as all other objects; the rules regarding their mutability in the callee are the same as dictionaries, sets, or any mutable type (including non-builtins). > > > > > Copying those elements does not come for free. > > > > It is true that list multiplication can be much faster than a list comp. > > But that's because the list multiply doesn't have to inspect the > > elements, copy them, or engage the iteration machinery. Avoiding copying > > gives you a big saving: > > > > > > [steve at ando ~]$ python3.3 -m timeit -s "x = range(1000)" > > "[x for _ in range(100)]" # not copied > > 100000 loops, best of 3: 11.9 usec per loop > > > > [steve at ando utilities]$ python3.3 -m timeit -s "x = range(1000)" > > "[x[:] for _ in range(100)]" # copied > > 10000 loops, best of 3: 103 usec per loop > > > > So there's a factor of ten difference right there. If list multiplication > > had to make copies, it would lose much of its speed advantage. > And when multiplication doesn't make copies of *lists*, it's going > "nowhere fast", because people don't want the results that gives. > > So what difference does it make? People won't make the construction > unless they wanted to make the copies in the first place. If they want > the copies, well -- copies are *slow*. Big deal. > > > For large > > enough lists, or complicated enough objects, it would become slower than > > a list comprehension. > Huh? You're nuts. > > > It would be even slower if list multiplication had to inspect each > > element first and decide whether or not to copy. > A single pointer comparison in a 'C' for loop takes less than 5 nano > seconds on a 1Ghz machine. > (I'll bet yours is faster than that...!) > Consider: list objects have a pointer which points back to the generic > list object -- that's all it takes to determine what the "type" is. > > Your measured loop times, doing list comprehensions takes over 10 > microseconds *per loop*. > Compared to what you're proposing -- The pointer compare is a mere 0.05% > change; You can't even measure that with "timeit!". BUT: The increase > in speed for not running tokenized "for" loops is *much* bigger than the > loss for a single pointer compare; so it will *usually* be a *serious* > net gain. > > >> I really don't think doing a shallow copy of lists would break anyone's > >> program. > > Anyone who is currently using list multiplication with mutable objects is > > expecting that they will be the same object, and relying on that fact. > > Otherwise they wouldn't be using list multiplication. > yes, and I'm not changing that -- except for lists; and *no* one is > using that. > Find two examples of it from existing non contrived web examples of > Python code. > *ask* around. I am positive that majority of code is not examples--web or otherwise. > > > > > You're suggesting a semantic change. Therefore they will be expecting > > something different from what actually happens. Result: broken code. > Even if it was; So are many semantic changes happening between python 2 > and python 3. > Look at what python 2 did: > > >>> range(0,5)[0] > 0 > >>> range(0,5)[1:3] > [1, 2] > > That's a *semantic* change. > Also; if you complain that xrange has been renamed range; then look: > > >>> xrange(0,5)[0] > 0 > >>> xrange(0,5)[1:3] > Traceback (most recent call last): > File "", line 1, in > TypeError: sequence index must be integer, not 'slice' > > WOW. WOW. WOW. An even BIGGER semantic change. So because one thing has a semantic change that gives license for semantic changes everywhere? Bah, ridiculous! [snip] > > > to get the behaviour you want, and then in Python 3.5 it would become the > > default. That's three years until it becomes the standard. Meanwhile, > > there will still be millions of people using Python 2.7 or 3.2, and their > > code will behave differently from your code. > Uh, they aren't *using* the construction I am proposing now -- they are > avoiding it like the plague. > Hence, it will merely become a new ability in a few years -- not > 'differently' behaving code. How in the name of do you have any clue about that? Granted, as an educated you *may* be right, but you may not be. I have no idea how you could know this definitively or with any great degree of certainty. [snip] ~Ramit [0] http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From ramit.prasad at jpmorgan.com Tue Nov 6 18:41:53 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Tue, 6 Nov 2012 23:41:53 +0000 Subject: How to only get a list of the names of the non-directory files in current directory ('.')? In-Reply-To: <31a0765f-c366-4f19-8d6c-bc95e0e51265@googlegroups.com> References: <31a0765f-c366-4f19-8d6c-bc95e0e51265@googlegroups.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF47416783B15@SCACMX008.exchad.jpmchase.net> iMath wrote: > how to get a list of names of everything in the current directory ? http://lmgtfy.com/?q=python+get+files+in+directory ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From ian.g.kelly at gmail.com Tue Nov 6 18:52:00 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 6 Nov 2012 16:52:00 -0700 Subject: Multi-dimensional list initialization In-Reply-To: <50999214.50100@r3dsolutions.com> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <50999214.50100@r3dsolutions.com> Message-ID: On Tue, Nov 6, 2012 at 3:41 PM, Andrew Robinson wrote: >> Q: What about other mutable objects like sets or dicts? >> A: No, the elements are never copied. > > They aren't list multiplication compatible in any event! It's a total > nonsense objection. > > If these are inconsistent in my idea -- OBVIOUSLY -- they are inconsistent > in Python's present implementation. You can't even reference duplicate them > NOW. > >>>> { 1:'a', 2:'b', 3:'c' } * 2 > > Traceback (most recent call last): > File "", line 1, in > TypeError: unsupported operand type(s) for *: 'dict' and 'int' The objection is not nonsense; you've merely misconstrued it. If [[1,2,3]] * 4 is expected to create a mutable matrix of 1s, 2s, and 3s, then one would expect [[{}]] * 4 to create a mutable matrix of dicts. If the dicts are not copied, then this fails for the same reason >> Q: How about if I use delegation to proxy a list? >> A: Oh no, they definitely won't be copied. > > Give an example usage of why someone would want to do this. Then we can > discuss it. Seriously? Read a book on design patterns. You might start at SO: http://stackoverflow.com/questions/832536/when-to-use-delegation-instead-of-inheritance >> Losing consistency in favour of saving a few characters for something as >> uncommon as list multiplication is a poor tradeoff. That's why this >> proposal has been rejected again and again and again every time it has >> been suggested. > > Please link to the objection being proposed to the developers, and their > reasoning for rejecting it. > I think you are exaggerating. >From Google: http://bugs.python.org/issue1408 http://bugs.python.org/issue12597 http://bugs.python.org/issue9108 http://bugs.python.org/issue7823 Note that in two out of these four cases, the reporter was trying to multiply lists of dicts, not just lists of lists. > Besides, 2D arrays are *not* rare and people *have* to copy internals of > them very often. > The copy speed will be the same or *faster*, and the typing less -- and the > psychological mistakes *less*, the elegance more. List multiplication is not potentially useful for copying 2D lists, only for initializing them. For copying an existing nested list, you're still stuck with either copy.deepcopy() or a list comprehension. > It's hardly going to confuse anyone to say that lists are copied with list > multiplication, but the elements are not. > > Every time someone passes a list to a function, they *know* that the list is > passed by value -- and the elements are passed by reference. People in > Python are USED to lists being "the" way to weird behavior that other > languages don't do. Incorrect. Python uses what is commonly known as call-by-object, not call-by-value or call-by-reference. Passing the list by value would imply that the list is copied, and that appends or removes to the list inside the function would not affect the original list. This is not what Python does; the list inside the function and the list passed in are the same list. At the same time, the function does not have access to the original reference to the list and cannot reassign it by reassigning its own reference, so it is not call-by-reference semantics either. From steve+comp.lang.python at pearwood.info Tue Nov 6 19:13:47 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 07 Nov 2012 00:13:47 GMT Subject: Obnoxious postings from Google Groups References: <50989613$0$29980$c3e8da3$5496439d@news.astraweb.com> <50999452$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5099a7ba$0$29980$c3e8da3$5496439d@news.astraweb.com> On Tue, 06 Nov 2012 23:08:11 +0000, Prasad, Ramit wrote: > Steven D'Aprano wrote: >> >> On Tue, 06 Nov 2012 17:16:44 +0000, Prasad, Ramit wrote: >> >> >> To enter the newline, I typed Ctrl-Q to tell bash to treat the next >> >> character as a literal, and then typed Ctrl-J to get a newline. >> > >> > That sounds complicated, my version of bash lets me type >> > 'foobar' for the same effect. >> >> Well, I learned something new about bash. >> >> On the other hand, the Ctrl-Q next-char-is-literal trick works for >> entering control characters that otherwise don't have a key on the >> keyboard. >> >> > Would you mind elaborating on how this works? I know it's not a bash > list, but I do not understand how ctrl-J is considered a literal. > Obviously, I must have a different definition of "literal". Where can I > find a list of other literals? My Google-fu is being weak today. :( I'm not an expert, so the following may not be exactly correct. As I understand it, when you hit a key on the keyboard, it sends the character you typed to the operating system. (The OS can then remap keys, generate keyboard events including a timestamp, etc.) Hit the J key, and the event includes character "j". Hit Shift-J, and character "J" is sent. Hit Ctrl-J, and the character sent is the ASCII control character ^J, or newline. (Technically, the name for ASCII 10 is "linefeed" rather than "newline".) Similarly, other control character combinations send other control codes: ^A = ASCII 0x01 Start Of Heading ^L = ASCII 0xFF Formfeed \f ^M = ASCII 0x0D Carriage Return \r etc. http://en.wikipedia.org/wiki/C0_and_C1_control_codes When readline is enabled in bash, one of the standard editing commands is that C-q (usually ctrl-Q on the keyboard) instructs readline to treat the next key as a literal. So Ctrl-Q followed by Backspace won't delete the previous character, but insert a literal DEL 0x7F character. (One of those historical quirks is that on most(?) keyboards, the Backspace key generates a DEL character rather than the ^H backspace control code, and the Delete key generates an escape sequence. Go figure.) -- Steven From GangGreene at example.com Tue Nov 6 19:16:48 2012 From: GangGreene at example.com (GangGreene) Date: Tue, 6 Nov 2012 19:16:48 -0500 Subject: Obnoxious postings from Google Groups References: <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> <50989a16$0$29980$c3e8da3$5496439d@news.astraweb.com> <509999e0$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, 06 Nov 2012 23:14:40 +0000, Steven D'Aprano wrote: > On Tue, 06 Nov 2012 11:51:03 -0500, GangGreene wrote: > >> I have just finished a 251 line bash shell script that builds my linux >> distro from scratch. > > "From scratch"? So if you run it on bare metal with no OS, it works? It has a host system for build/bootstraping. Then it goes onto an USB drive, both as the OS and installation packages (with package manager) capable of install on a system without an OS. Hard drive needed ;) > > But seriously -- bash is a mature, powerful shell. It works well for > what it does. It has been designed to make it easy[1] to automate system > admin tasks. It would be astonishing if an experienced, competent bash > programmer couldn't write an installation tool in bash. By comparison, > few general purpose programming languages (with the obvious exception of > perl) are designed for system administration as their primary purpose. > > But... how robust is your script? How many bugs does it contain? Chances > are you will only use it a handful of times, on the same system. That's > not a lot of testing to be sure that it is free of bugs, and robust > against unexpected input. I have used it for several years and others have used it to build their own systems. I have used it on 586 to the latest multi core systems. > > Hell, even widely used and distributed install scripts written by > companies with the resources of Ubuntu and Red Hat have a distressing > tendency to break. Or worse, to behave in unexpected ways. I am not Redhat or Ubuntu (commercial distros stink) ;) > > Unless you are some sort of bash-scripting ?ber-geek superhero with > powers beyond those of mortal men, chances are that your script is much > more buggy and fragile than you imagine. How well does it recover from > errors? It stops on all critical build errors and skips the package on minimal errors, giving me a list of skipped packages and status on them at the end of the build sequence (so I can fix my errors). It will also download the source package if it is not present in the build directory. If I incur any breakage/error ( it can happen a lot ) it stops and notifies me. I then fix the package build script and lather/rinse/repeat until I get the package build correct. It logs all the information into build/testing/file listing/dependency requirements into log files. Then the package goes off to a repository holding all the other packages. The directory layout is like this: build +--section - programming/X window etc +----package - python gcc coreutils and contains the build script ( bash script ) and source package. The build system ( bash scripts ) recurses this directory structure build the packages and placing them into repository +-----base - base system packages kernel coreutils etc +-----extra - programming packages, xorg etc here The package manager (written in C) then looks to the repository to install/update from the repository. > Does it leave you with a broken system, half installed? No. It builds all of the packages and then creates binary/devel packages to be installed a new system. The finished packages go into a repository. If I update a package only that package is built and placed into the repository so all machine can be updated by my package manager. And yes the build script automatically takes care of all build dependencies as well as installed binary dependencies for the host installs. It will even build itself. >How easily can you maintain it after three months? Will it work in the > presence of filenames with newlines in them? I have maintained it for 8 years, and it can and does handle file names with spaces, any valid *nix file name character can be used. > > > > [1] For some definition of "easy". In my opinion, control structures > like "for" and "if" in bash are hard to use, hard to read, easy to get > wrong, give cryptic error messages when you do get them wrong, and are > ugly. Tests are obfuscated, and as always, the fact that everything is > text in bash means it is way harder than it needs to be to use rich, > modern data structures. Only from a certain points of view. I use many for loops and no if statements. There is a good alternate for if, easily understandable if you know C and boolean logic. My background is in C and Delphi/Pascal and I didn't have problems with bash scripting. I want to have a go at python. Arch linux "pacman package management/build/delevopment system" is very similar to mine. If you wish to take a look at it. It is also mostly bash scripts with some C and python2/3 to boot. From python at mrabarnett.plus.com Tue Nov 6 19:23:44 2012 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 07 Nov 2012 00:23:44 +0000 Subject: Multi-dimensional list initialization In-Reply-To: References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <50999214.50100@r3dsolutions.com> Message-ID: <5099AA10.1040900@mrabarnett.plus.com> On 2012-11-06 23:52, Ian Kelly wrote: > On Tue, Nov 6, 2012 at 3:41 PM, Andrew Robinson > wrote: >>> Q: What about other mutable objects like sets or dicts? >>> A: No, the elements are never copied. >> >> They aren't list multiplication compatible in any event! It's a total >> nonsense objection. >> >> If these are inconsistent in my idea -- OBVIOUSLY -- they are inconsistent >> in Python's present implementation. You can't even reference duplicate them >> NOW. >> >>>>> { 1:'a', 2:'b', 3:'c' } * 2 >> >> Traceback (most recent call last): >> File "", line 1, in >> TypeError: unsupported operand type(s) for *: 'dict' and 'int' > > The objection is not nonsense; you've merely misconstrued it. If > [[1,2,3]] * 4 is expected to create a mutable matrix of 1s, 2s, and > 3s, then one would expect [[{}]] * 4 to create a mutable matrix of > dicts. If the dicts are not copied, then this fails for the same > reason > >>> Q: How about if I use delegation to proxy a list? >>> A: Oh no, they definitely won't be copied. >> >> Give an example usage of why someone would want to do this. Then we can >> discuss it. > > Seriously? Read a book on design patterns. You might start at SO: > > http://stackoverflow.com/questions/832536/when-to-use-delegation-instead-of-inheritance > >>> Losing consistency in favour of saving a few characters for something as >>> uncommon as list multiplication is a poor tradeoff. That's why this >>> proposal has been rejected again and again and again every time it has >>> been suggested. >> >> Please link to the objection being proposed to the developers, and their >> reasoning for rejecting it. >> I think you are exaggerating. > >>From Google: > > http://bugs.python.org/issue1408 > http://bugs.python.org/issue12597 > http://bugs.python.org/issue9108 > http://bugs.python.org/issue7823 > > Note that in two out of these four cases, the reporter was trying to > multiply lists of dicts, not just lists of lists. > >> Besides, 2D arrays are *not* rare and people *have* to copy internals of >> them very often. >> The copy speed will be the same or *faster*, and the typing less -- and the >> psychological mistakes *less*, the elegance more. > > List multiplication is not potentially useful for copying 2D lists, > only for initializing them. For copying an existing nested list, > you're still stuck with either copy.deepcopy() or a list > comprehension. > >> It's hardly going to confuse anyone to say that lists are copied with list >> multiplication, but the elements are not. >> >> Every time someone passes a list to a function, they *know* that the list is >> passed by value -- and the elements are passed by reference. People in >> Python are USED to lists being "the" way to weird behavior that other >> languages don't do. > > Incorrect. Python uses what is commonly known as call-by-object, not > call-by-value or call-by-reference. Passing the list by value would > imply that the list is copied, and that appends or removes to the list > inside the function would not affect the original list. This is not > what Python does; the list inside the function and the list passed in > are the same list. At the same time, the function does not have > access to the original reference to the list and cannot reassign it by > reassigning its own reference, so it is not call-by-reference > semantics either. > I prefer the term "reference semantics". From scbunn at sbunn.org Tue Nov 6 19:52:35 2012 From: scbunn at sbunn.org (Stephen Bunn) Date: Wed, 7 Nov 2012 09:52:35 +0900 Subject: multiprocessing help Message-ID: I'm looking for some help with multiprocessing. Essentially what I'm trying to do is the following: 1. create a main process that gets daemonized 2. spawn two subprocess that live for the life of the daemon 3. each subprocess creates children that do heavy work and exit when the work is done I am not having issues with creating the processes but I can't get them to die. Sometimes it works and they all die and sometimes they don't. Obviously I do not understand the multiprocessing documentation so I'm hoping that somebody can point out what I'm doing wrong. I also understand that my code might be strict adherence to PEP8 and there might be a number of flaws with the code; all of which would be helpful to know, but I'm particularly looking for help with getting what I'm trying to do to work. I'm starting to lose my mind.. I just want to say all processes that spawned from here $@%@$% DIE. Thanks in advance for anybody that has some spare time to point me in the right direction. I am grateful. Thanks. Very Respectfully, Stephen Bunn scbunn at sbunn.org -------------- next part -------------- A non-text attachment was scrubbed... Name: sqlimport.py Type: application/octet-stream Size: 3062 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: daemon.py Type: application/octet-stream Size: 4750 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: maindaemon.py Type: application/octet-stream Size: 5579 bytes Desc: not available URL: From steve+comp.lang.python at pearwood.info Tue Nov 6 20:55:10 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 07 Nov 2012 01:55:10 GMT Subject: Multi-dimensional list initialization References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5099bf7d$0$29980$c3e8da3$5496439d@news.astraweb.com> On Tue, 06 Nov 2012 14:41:24 -0800, Andrew Robinson wrote: > Yes. But this isn't going to cost any more time than figuring out > whether or not the list multiplication is going to cause quirks, itself. > Human psychology *tends* (it's a FAQ!) to automatically assume the > purpose of the list multiplication is to pre-allocate memory for the > equivalent (using lists) of a multi-dimensional array. Note the OP even > said "4d array". I'm not entirely sure what your point is here. The OP screwed up -- he didn't generate a 4-dimensional array. He generated a 2-dimensional array. If his intuition about the number of dimensions is so poor, why should his intuition about list multiplication be treated as sacrosanct? As they say, the only truly intuitive interface is the nipple. There are many places where people's intuition about programming fail. And many places where Fred's intuition is the opposite of Barney's intuition. Even more exciting, there are places where people's intuition is *inconsistent*, where they expect a line of code to behave differently depending on their intention, rather than on the code. And intuition is often sub-optimal: e.g. isn't it intuitively obvious that "42" + 1 should give 43? (Unless it is intuitively obvious that it should give 421.) So while I prefer intuitively obvious behaviour where possible, it is not the holy grail, and I am quite happy to give it up. > The OP's original construction was simple, elegant, easy to read and > very commonly done by newbies learning the language because it's > *intuitive*. His second try was still intuitive, but less easy to read, > and not as elegant. Yes. And list multiplication is one of those areas where intuition is suboptimal -- it produces a worse outcome overall, even if one minor use- case gets a better outcome. I'm not disputing that [[0]*n]*m is intuitively obvious and easy. I'm disputing that this matters. Python would be worse off if list multiplication behaved intuitively. An analogy: the intuitively obvious thing to do with a screw is to bang it in with a hammer. It's long, thin, has a point at the end, and a flat head that just screams "hit me". But if you do the intuitive thing, your carpentry will be *much worse* than the alternatives -- a hammered in screw holds much less strongly than either a nail or a screwed in screw. The surface area available for gripping is about 2% compared to a nail and about 0.01% compared to a screw used correctly. Having list multiplication copy has consequences beyond 2D arrays. Those consequences make the intuitive behaviour you are requesting a negative rather than a positive. If that means that newbie programmers have to learn not to hammer screws in, so be it. It might be harder, slower, and less elegant to drill a pilot hole and then screw the screw in, but the overall result is better. >> * Consistency of semantics is better than a plethora of special >> cases. Python has a very simple and useful rule: objects should not >> be copied unless explicitly requested to be copied. This is much >> better than having to remember whether this operation or that >> operation makes a copy. The answer is consistent: > > Bull. Even in the last thread I noted the range() object produces > special cases. > >>> range(0,5)[1] > 1 > >>> range(0,5)[1:3] > range(1, 3) What's the special case here? What do you think is copied? You take a slice of a tuple, you get a new tuple. You take a slice of a list, you get a new list. You take a slice of a range object, you get a new range object. I'm honestly not getting what you think is inconsistent about this. > The principle involved is that it gives you what you *usually* want; Who is the "you" that decides what "you" usually want? And how do they know what is "usual"? Two-dimensional arrays in Python using lists are quite rare. Anyone who is doing serious numeric work where they need 2D arrays is using numpy, not lists. There are millions of people using Python, so it's hardly surprising that once or twice a year some newbie trips over this. But it's not something that people tend to trip over again and again and again, like C's "assignment is an expression" misfeature. > I read some of the documentation on why Python 3 chose to implement it > this way. What documentation is this? Because this is a design decision that goes all the way back to at least Python 1.5: [steve at ando ~]$ python1.5 Python 1.5.2 (#1, Aug 27 2012, 09:09:18) [GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux2 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>> x = [[0]*5]*3 >>> x[0][1] = 99 >>> x [[0, 99, 0, 0, 0], [0, 99, 0, 0, 0], [0, 99, 0, 0, 0]] So I expect the design decision for Python 3 was "we made the right decision before, there's no need to change it". >> (pardon me for belabouring the point here) >> >> Q: Does [0]*10 make ten copies of the integer object? A: No, list >> multiplication doesn't make copies of elements. > > Neither would my idea for the vast majority of things on your first > list. Um, yes? The point is that "vast majority" is not "everything". Hence, your suggested behaviour is inconsistent. > Q: What about [[]]*10? > A: No, the elements are never copied. > > YES! For the obvious reason that such a construction is making mutable > lists that the user wants to populate later. If they *didn't* want to > populate them later, they ought to have used tuples -- which take less > overhead. Who even does this thing you are suggesting?! Who knows? Who cares? Nobody does: n -= n instead of just n=0, but that doesn't mean that we should give it some sort of special meaning different from n -= m. If it turns out that the definition of list multiplication is such that NOBODY, EVER, uses [[]]*n, that is *still* not a good reason for special-casing it. All it means is that this will be a less-obscure example of the billions of things which can be done in Python but nobody wants to. You have quoted from the Zen of Python a few times in this post. Perhaps you missed one of the most critical ones? Special cases aren't special enough to break the rules. There are perfectly good ways to generate a 2D array out of lists, and even better reasons not to use lists for that in the first place. (Numpy arrays are much better suited for serious work.) >> Q: What about other mutable objects like sets or dicts? A: No, the >> elements are never copied. > > They aren't list multiplication compatible in any event! It's a total > nonsense objection. I'm afraid you've just lost an awful lot of credibility there. py> x = [{}]*5 py> x [{}, {}, {}, {}, {}] py> x[0]['key'] = 1 py> x [{'key': 1}, {'key': 1}, {'key': 1}, {'key': 1}, {'key': 1}] And similarly for any other mutable object. If you don't understand that lists can contain other mutable objects apart from lists, then you really shouldn't be discussing this issue. >> Your proposal throws away consistency for a trivial benefit on a rare >> use- case, and replaces it with a bunch of special cases: > > RARE!!!! You are NUTS!!!! Yes, rare. I base that on about 15 years of Python coding and many thousands (tens of thousands?) of hours on Python forums like this one. What's your opinion based on? List multiplication is rare enough, but when it is used, it is usually used to generate a 1D array like this: values = [None]*n # or 0 is another popular starting value Using it twice to generate a 2D array is even rarer. >> Q: How about if I use delegation to proxy a list? A: Oh no, they >> definitely won't be copied. > > Give an example usage of why someone would want to do this. Then we can > discuss it. Proxying objects is hardly a rare scenario. Delegation is less common since you can subclass built-ins, but it is still used. It is a standard design pattern. >> Losing consistency in favour of saving a few characters for something >> as uncommon as list multiplication is a poor tradeoff. That's why this >> proposal has been rejected again and again and again every time it has >> been suggested. > > Please link to the objection being proposed to the developers, and their > reasoning for rejecting it. > I think you are exaggerating. Python is a twenty year old language. Do you really think this is the first time somebody has noticed it? It's hard to search for discussions on the dev list, because the obvious search terms bring up many false positives. But here are a couple of bug reports closed as "won't fix": http://bugs.python.org/issue1408 http://bugs.python.org/issue12597 I suspect it is long past time for a PEP so this can be rejected once and for all. >> List multiplication [x]*n is conceptually equivalent to: >> This is nice and simple and efficient. > No it isn't efficient. It's *slow* when done as in your example. Well of course it is slow*er* when you move it from low-level C to high level Python, but it is still fast. >> Copying other objects is slow and inefficient. Keeping list >> multiplication consistent, and fast, is MUCH more important than making >> it work as expected for the rare case of 2D arrays: > > I don't think so -- again, look at range(); it was made to work > inconsistent for a "common" case. You mentioned range before, but it isn't clear to me what you think is inconsistent about it. > Besides, 2D arrays are *not* rare and people *have* to copy internals of > them very often. So you say. > The copy speed will be the same or *faster*, and the typing less -- and > the psychological mistakes *less*, the elegance more. You think that it is *faster* to copy a list than to make a new pointer to it? Your credibility is not looking too good here. > It's hardly going to confuse anyone to say that lists are copied with > list multiplication, but the elements are not. Well, that confuses me. What about a list where the elements are lists? Are they copied? What about other mutable objects? Are they copied? What about mutable objects which are uncopyable, like file objects? > Every time someone passes a list to a function, they *know* that the > list is passed by value -- and the elements are passed by reference. And there goes the last of your credibility. *You* might "know" this, but that doesn't make it so. Python does not use either call-by-value or call-by-reference, and it certainly doesn't use different calling conventions for different arguments or parts of arguments. Everything is passed using the same calling convention. Start here: http://mail.python.org/pipermail/tutor/2010-December/080505.html > People in Python are USED to lists being "the" way to weird behavior > that other languages don't do. Python's calling behaviour is identical to that used by languages including Java (excluding unboxed primitives) and Ruby, to mention only two. You're starting to shout and yell, so perhaps it's best if I finish this here. -- Steven From wuwei23 at gmail.com Tue Nov 6 21:10:28 2012 From: wuwei23 at gmail.com (alex23) Date: Tue, 6 Nov 2012 18:10:28 -0800 (PST) Subject: Base class and Derived class question References: <775c0f45-ab55-4683-b9f5-2bbed981ee2d@googlegroups.com> Message-ID: <8b17c0fe-b114-4f53-a82e-4439de8a4da4@v9g2000pbi.googlegroups.com> On Nov 7, 1:08?am, cyberira... at gmail.com wrote: > Just got answer, I didn't call a class it's self. ?Correct code is: > class derivedClass(baseClassMod.baseClass): > ? ? def ...... Incidentally, this is why it's recommended to give modules lowercase names - baseclass - and classes camelcased ones - BaseClass. It makes it more obvious to what you're holding a reference. From naruto0.1 at live.cn Tue Nov 6 22:14:00 2012 From: naruto0.1 at live.cn (jack) Date: Wed, 7 Nov 2012 11:14:00 +0800 Subject: How to specify a field name dynamically Message-ID: I have three tables: table1 |???????| | id | f1 | |???????| table2 |???????| | id | f2 | |???????| table3 |???????| | id | f3 | |???????| I want define a function to insert records to someone,but I don't know how to specify a field name dynamically. I had a try like this / def insert_record(table, field, goods):// // return db.insert(table, field=goods//)/ or / def insert_record(table, **kv):// // return db.insert(table, **kv)/ but it does not works -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Tue Nov 6 22:36:18 2012 From: d at davea.name (Dave Angel) Date: Tue, 06 Nov 2012 22:36:18 -0500 Subject: How to specify a field name dynamically In-Reply-To: References: Message-ID: <5099D732.10003@davea.name> On 11/06/2012 10:14 PM, jack wrote: > I have three tables: What's a table? I'll assume you're using Python, but what version, and what extra libraries do you have installed ? At least show your import statements, so we might have a chance at guessing. I'll assume the db stands for database, but what database, or what library??? if it really is a database question, and you specify what library you're using, then maybe somebody who uses that will jump in. > table1 > |???????| > | id | f1 | > |???????| > > table2 > |???????| > | id | f2 | > |???????| > > table3 > |???????| > | id | f3 | > |???????| > > > I want define a function to insert records to someone,but I don't know > how to specify a field name dynamically. > I had a try like this > > / def insert_record(table, field, goods):// > // return db.insert(table, field=goods//)/ > or > / def insert_record(table, **kv):// > // return db.insert(table, **kv)/ > > but it does not works > That's not much of a clue. Do you mean you get an exception? If so, paste it into a message, the full traceback. Or you mean it returns the wrong data? Or it crashes your OS? My first guess would be that those slashes are causing syntax errors. But if that's a bug in your OS's copy/paste, then I'd say you have indentation problems. Or maybe these are not just functions, but methods inside a class. And in that case, I might guess that you're missing the self argument, and therefore getting an exception of wrong number of arguments. -- DaveA From tjreedy at udel.edu Tue Nov 6 23:04:56 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 06 Nov 2012 23:04:56 -0500 Subject: multiprocessing help In-Reply-To: References: Message-ID: <5099DDE8.7050608@udel.edu> On 11/6/2012 7:52 PM, Stephen Bunn wrote: > I'm looking for some help with multiprocessing. Questions about using Python go to python-list. python-dev is for developing future versions of Python. -- Terry Jan Reedy From rustompmody at gmail.com Tue Nov 6 23:11:45 2012 From: rustompmody at gmail.com (rusi) Date: Tue, 6 Nov 2012 20:11:45 -0800 (PST) Subject: Multi-dimensional list initialization References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <50999214.50100@r3dsolutions.com> Message-ID: <83e321db-65e1-424f-9166-5261caa12be5@px4g2000pbc.googlegroups.com> On Nov 7, 5:26?am, MRAB wrote: > I prefer the term "reference semantics". Ha! That hits the nail on the head. To go back to the OP: On Nov 5, 11:28 am, Demian Brecht wrote: > So, here I was thinking "oh, this is a nice, easy way to initialize a 4D matrix" (running 2.7.3, non-core libs not allowed): > > m = [[None] * 4] * 4 > > The way to get what I was after was: > > m = [[None] * 4, [None] * 4, [None] * 4, [None * 4]] > > (Obviously, I could have just hardcoded the initialization, but I'm too lazy to type all that out ;)) > > The behaviour I encountered seems a little contradictory to me. [None] * 4 creates four distinct elements in a single array while [[None] * 4] * 4 creates one distinct array of four distinct elements, with three references to it: > > >>> a = [None] * 4 > >>> a[0] = 'a' > >>> a > > ['a', None, None, None] > > >>> m = [[None] * 4] * 4 > >>> m[0][0] = 'm' > >>> m > > [['m', None, None, None], ['m', None, None, None], ['m', None, None, None], ['m', None, None, None]] > > Is this expected behaviour and if so, why? In my mind either result makes sense, but the inconsistency is what throws me off. > m=[[None] * 2] * 3 is the same as m=[[None]*2, [None]*2, [None]*2] until one starts doing things like m[0][0] = 'm' So dont do it! And to get python to help you by saying the same that I am saying do m=((None) * 2) * 3 (well almost... its a bit more messy in practice) m=(((None,) * 2),)*3 After that try assigning to m[0][0] and python will kindly say NO! tl;dr version: reference semantics is ok assignment is ok (well up to a point) assignment + reference semantics is not From tjreedy at udel.edu Tue Nov 6 23:11:59 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 06 Nov 2012 23:11:59 -0500 Subject: multiprocessing help In-Reply-To: <5099DDE8.7050608@udel.edu> References: <5099DDE8.7050608@udel.edu> Message-ID: <5099DF8F.2020109@udel.edu> On 11/6/2012 11:04 PM, Terry Reedy wrote: > On 11/6/2012 7:52 PM, Stephen Bunn wrote: >> I'm looking for some help with multiprocessing. > > Questions about using Python go to python-list. > python-dev is for developing future versions of Python. My apologies. I intended to and thought I had opened python-dev (as gmane newsgroup) and clicked send a couple of seconds too soon (or realized my mistake a couple of seconds too late). -- Terry Jan Reedy From steve+comp.lang.python at pearwood.info Wed Nov 7 00:05:34 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 07 Nov 2012 05:05:34 GMT Subject: Multi-dimensional list initialization References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <50999214.50100@r3dsolutions.com> Message-ID: <5099ec1d$0$21759$c3e8da3$76491128@news.astraweb.com> On Wed, 07 Nov 2012 00:23:44 +0000, MRAB wrote: >> Incorrect. Python uses what is commonly known as call-by-object, not >> call-by-value or call-by-reference. Passing the list by value would >> imply that the list is copied, and that appends or removes to the list >> inside the function would not affect the original list. This is not >> what Python does; the list inside the function and the list passed in >> are the same list. At the same time, the function does not have access >> to the original reference to the list and cannot reassign it by >> reassigning its own reference, so it is not call-by-reference semantics >> either. >> > I prefer the term "reference semantics". Oh good, because what the world needs is yet another name for the same behaviour. - call by sharing - call by object sharing - call by object reference - call by object - call by value, where "values" are references (according to the Java community) - call by reference, where "references" refer to objects, not variables (according to the Ruby community) - reference semantics Anything else? http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing -- Steven From roy at panix.com Wed Nov 7 00:12:18 2012 From: roy at panix.com (Roy Smith) Date: Wed, 07 Nov 2012 00:12:18 -0500 Subject: Multi-dimensional list initialization References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <50999214.50100@r3dsolutions.com> <5099ec1d$0$21759$c3e8da3$76491128@news.astraweb.com> Message-ID: In article <5099ec1d$0$21759$c3e8da3$76491128 at news.astraweb.com>, Steven D'Aprano wrote: > On Wed, 07 Nov 2012 00:23:44 +0000, MRAB wrote: > > >> Incorrect. Python uses what is commonly known as call-by-object, not > >> call-by-value or call-by-reference. Passing the list by value would > >> imply that the list is copied, and that appends or removes to the list > >> inside the function would not affect the original list. This is not > >> what Python does; the list inside the function and the list passed in > >> are the same list. At the same time, the function does not have access > >> to the original reference to the list and cannot reassign it by > >> reassigning its own reference, so it is not call-by-reference semantics > >> either. > >> > > I prefer the term "reference semantics". > > > Oh good, because what the world needs is yet another name for the same > behaviour. > > - call by sharing > - call by object sharing > - call by object reference > - call by object > - call by value, where "values" are references > (according to the Java community) > - call by reference, where "references" refer to objects, not variables > (according to the Ruby community) > - reference semantics > > > Anything else? > > http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing Call by social network? The called function likes the object. Depending on how it feels, it can also comment on some of the object's attributes. From naruto0.1 at live.cn Wed Nov 7 00:16:09 2012 From: naruto0.1 at live.cn (jack) Date: Wed, 7 Nov 2012 13:16:09 +0800 Subject: How to specify a field name dynamically In-Reply-To: <5099D732.10003@davea.name> References: <5099D732.10003@davea.name> Message-ID: On 2012/11/7 11:36, Dave Angel wrote: > On 11/06/2012 10:14 PM, jack wrote: >> I have three tables: > What's a table? I'll assume you're using Python, but what version, and > what extra libraries do you have installed ? At least show your import > statements, so we might have a chance at guessing. I'll assume the db > stands for database, but what database, or what library??? if it really > is a database question, and you specify what library you're using, then > maybe somebody who uses that will jump in. yeah, I'm sorry for that I didn't offer enough information about my question. >> table1 >> |???????| >> | id | f1 | >> |???????| >> >> table2 >> |???????| >> | id | f2 | >> |???????| >> >> table3 >> |???????| >> | id | f3 | >> |???????| >> >> >> I want define a function to insert records to someone,but I don't know >> how to specify a field name dynamically. >> I had a try like this >> >> / def insert_record(table, field, goods):// >> // return db.insert(table, field=goods//)/ >> or >> / def insert_record(table, **kv):// >> // return db.insert(table, **kv)/ >> >> but it does not works >> > That's not much of a clue. Do you mean you get an exception? If so, > paste it into a message, the full traceback. Or you mean it returns the > wrong data? Or it crashes your OS? > > My first guess would be that those slashes are causing syntax errors. > But if that's a bug in your OS's copy/paste, then I'd say you have > indentation problems. Or maybe these are not just functions, but methods > inside a class. And in that case, I might guess that you're missing the > self argument, and therefore getting an exception of wrong number of > arguments. > # I want to insert something to a database using web.py-0.37 & mysql. / import web// // db = web.database(dbn='mysql', user='username', passwd='password', db='test'//)/ # I found the tables I created have similar structure,so I wanted define one function to serve all tables / create table if not exists table1(id integer auto_increment primary key, num integer, table1_cxt text);// // create table if not exists table2(id integer auto_increment primary key, num integer, table2_cxt text);// // create table if not exists table3(id integer auto_increment primary key, num integer, table3_cxt text);/ # I known that: / def insert_record(num=None, table1_cxt=None):// // db.insert('table1', num=num, table1_cxt=table1_cxt ) / so I think have some way to pass args instead of the hard code('table1', table1_cxt). ///def insert_record(table, num, field, goods): // ////return db.insert(table, num=num, field=goods)/ this is my way.I do a experiment: ///>>> def func(arg1 = 'None', arg2 = 'None'):// // ... print arg1// // ... print arg2// // ...// // >>> def func1(a1, a2, v1, v2):// // ... func(a1=v1, a2=v2)// // ...//// // >>> func1('arg1', 'arg2', 3, 4)// // Traceback (most recent call last):// // File "", line 1, in // // File "", line 2, in func1// // TypeError: func() got an unexpected keyword argument 'a1'//// /obviously, it's wrong. **kv works for me: / >>> def func1(**kv):// // ... func(**kv)// ////>>> func1(arg1='123', arg2='456')// // 123// // 456/ Maybe it's right, so I should change before saying -------------- next part -------------- An HTML attachment was scrubbed... URL: From greg.ewing at canterbury.ac.nz Wed Nov 7 00:32:03 2012 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Wed, 07 Nov 2012 18:32:03 +1300 Subject: Multi-dimensional list initialization In-Reply-To: References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <50999214.50100@r3dsolutions.com> <5099ec1d$0$21759$c3e8da3$76491128@news.astraweb.com> Message-ID: Roy Smith wrote: > Call by social network? The called function likes the object. > Depending on how it feels, it can also comment on some of the object's > attributes. And then finds that it has inadvertently shared all its private data with other functions accessing the object. -- Greg From greg.ewing at canterbury.ac.nz Wed Nov 7 00:34:09 2012 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Wed, 07 Nov 2012 18:34:09 +1300 Subject: Multi-dimensional list initialization In-Reply-To: References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098A55C.3090201@r3dsolutions.com> <5098C873.2000200@r3dsolutions.com> <509982CA.7050701@r3dsolutions.com> Message-ID: If anything is to be done in this area, it would be better as an extension of list comprehensions, e.g. [[None times 5] times 10] which would be equivalent to [[None for _i in xrange(5)] for _j in xrange(10)] -- Greg From greg.ewing at canterbury.ac.nz Wed Nov 7 00:39:02 2012 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Wed, 07 Nov 2012 18:39:02 +1300 Subject: Coordination between developers in the Python project In-Reply-To: References: <50980ED4.7030705@udel.edu> Message-ID: Ian Kelly wrote: > Although I find it a bit easier to just use > something like unshorten.com, which is the first Google hit for "url > unshortener". Assuming that you trust that site to not be hosting > malware itself. :-) Ah yes, beware the Hungarian unshortening service that redirects every request to a random porn page. They will plead incompetence, of course. -- Greg From greg.ewing at canterbury.ac.nz Wed Nov 7 00:52:16 2012 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Wed, 07 Nov 2012 18:52:16 +1300 Subject: Obnoxious postings from Google Groups In-Reply-To: <50989a16$0$29980$c3e8da3$5496439d@news.astraweb.com> References: <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> <50989a16$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > The downside is that if spaces are not argument separators, then you need > something else to be an argument separator. Or you need argument > delimiters. Or strings need to be quoted. Programming languages do these > things because they are designed to be correct. Shell do not because they > are designed for lazy users and merely aim to be "good enough". That's overly judgemental. In the environment where shells originated, not being able to easily put spaces in file names wasn't considered a problem. File names weren't thought of as names in the natural language sense, but as identifiers in the programming sense. You don't complain that you can't put spaces in identifiers in a Python program, do you? No, because that would require all identifiers to be quoted somehow, which would drive you crazy. In the same way, requiring all filenames to be quoted would drive shell users crazy. -- Greg From roy at panix.com Wed Nov 7 01:04:44 2012 From: roy at panix.com (Roy Smith) Date: Wed, 07 Nov 2012 01:04:44 -0500 Subject: Obnoxious postings from Google Groups References: <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> <50989a16$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article , Gregory Ewing wrote: > Steven D'Aprano wrote: > > The downside is that if spaces are not argument separators, then you need > > something else to be an argument separator. Or you need argument > > delimiters. Or strings need to be quoted. Programming languages do these > > things because they are designed to be correct. Shell do not because they > > are designed for lazy users and merely aim to be "good enough". > > That's overly judgemental. In the environment where shells originated, > not being able to easily put spaces in file names wasn't considered a > problem. File names weren't thought of as names in the natural language > sense, but as identifiers in the programming sense. > > You don't complain that you can't put spaces in identifiers in a > Python program, do you? No, because that would require all identifiers > to be quoted somehow, which would drive you crazy. In the same way, > requiring all filenames to be quoted would drive shell users crazy. On the other hand, if you *wanted* to put a space in a Python identifier, you just can't. If you want to put a space in a file name in the shell, all you need do is put spaces around the name. Or, if you prefer, escape the space with a backslash. Oh, wait. This blows my mind... >>> f = Foo() >>> setattr(f, "x y", "xyz") >>> dir(f) ['__doc__', '__module__', 'x y'] I did not expect this to work. Not quite sure what I've created here. From anuradha.raghupathy2010 at gmail.com Wed Nov 7 01:39:05 2012 From: anuradha.raghupathy2010 at gmail.com (anuradha.raghupathy2010 at gmail.com) Date: Tue, 6 Nov 2012 22:39:05 -0800 (PST) Subject: Logging output to be redirected to a particular folder In-Reply-To: References: Message-ID: <58708428-cea7-4ff7-8665-c31c7e6dac3d@googlegroups.com> Thanks ...this works perfectly fine now. On Tuesday, November 6, 2012 11:28:46 PM UTC+5:30, Prasad, Ramit wrote: > Dennis Lee Bieber wrote: > > > > > > On Tue, 06 Nov 2012 13:26:11 +0100, Peter Otten <__peter__ at web.de> > > > declaimed the following in gmane.comp.python.general: > > > > > > > anuradha.raghupathy2010 at gmail.com wrote: > > [snip] > > > > > def main(): > > > > > logging.basicConfig(Filename='c://myapp.log', level=logging.ERROR) > > > > > > > > Python is case-sensitive. Try: > > > > > > > > logging.basicConfig(filename='c://myapp.log', level=logging.ERROR) > > > > > > > The double forward slashes might also be confusing... At the least, > > > unneeded... > > > > > > >>> import os.path > > > >>> print os.path.normpath("c://somefile.log") > > > c:\somefile.log > > > >>> print os.path.normpath("c:\\somefile.log") > > > c:\somefile.log > > > >>> print os.path.normpath("c:\\tryfile.log") > > > c:\tryfile.log > > > >>> print os.path.normpath("c:\tryfile.log") > > > c: ryfile.log > > > >>> print os.path.normpath("c:/tryfile.log") > > > c:\tryfile.log > > > >>> > > > > > > Doubling back-slashes is needed to avoid the problem of literal > > > escapes corrupting the intent... > > > > Or use the raw literal form r"c:\tryfile.log". I know several > > people that prefer to use forward slashes as it works in both > > Windows and *nix. > > > > > > ~Ramit > > > > > > This email is confidential and subject to important disclaimers and > > conditions including on offers for the purchase or sale of > > securities, accuracy and completeness of information, viruses, > > confidentiality, legal privilege, and legal entity disclaimers, > > available at http://www.jpmorgan.com/pages/disclosures/email. From anuradha.raghupathy2010 at gmail.com Wed Nov 7 01:39:05 2012 From: anuradha.raghupathy2010 at gmail.com (anuradha.raghupathy2010 at gmail.com) Date: Tue, 6 Nov 2012 22:39:05 -0800 (PST) Subject: Logging output to be redirected to a particular folder In-Reply-To: References: Message-ID: <58708428-cea7-4ff7-8665-c31c7e6dac3d@googlegroups.com> Thanks ...this works perfectly fine now. On Tuesday, November 6, 2012 11:28:46 PM UTC+5:30, Prasad, Ramit wrote: > Dennis Lee Bieber wrote: > > > > > > On Tue, 06 Nov 2012 13:26:11 +0100, Peter Otten <__peter__ at web.de> > > > declaimed the following in gmane.comp.python.general: > > > > > > > anuradha.raghupathy2010 at gmail.com wrote: > > [snip] > > > > > def main(): > > > > > logging.basicConfig(Filename='c://myapp.log', level=logging.ERROR) > > > > > > > > Python is case-sensitive. Try: > > > > > > > > logging.basicConfig(filename='c://myapp.log', level=logging.ERROR) > > > > > > > The double forward slashes might also be confusing... At the least, > > > unneeded... > > > > > > >>> import os.path > > > >>> print os.path.normpath("c://somefile.log") > > > c:\somefile.log > > > >>> print os.path.normpath("c:\\somefile.log") > > > c:\somefile.log > > > >>> print os.path.normpath("c:\\tryfile.log") > > > c:\tryfile.log > > > >>> print os.path.normpath("c:\tryfile.log") > > > c: ryfile.log > > > >>> print os.path.normpath("c:/tryfile.log") > > > c:\tryfile.log > > > >>> > > > > > > Doubling back-slashes is needed to avoid the problem of literal > > > escapes corrupting the intent... > > > > Or use the raw literal form r"c:\tryfile.log". I know several > > people that prefer to use forward slashes as it works in both > > Windows and *nix. > > > > > > ~Ramit > > > > > > This email is confidential and subject to important disclaimers and > > conditions including on offers for the purchase or sale of > > securities, accuracy and completeness of information, viruses, > > confidentiality, legal privilege, and legal entity disclaimers, > > available at http://www.jpmorgan.com/pages/disclosures/email. From demianbrecht at gmail.com Wed Nov 7 01:56:33 2012 From: demianbrecht at gmail.com (Demian Brecht) Date: Tue, 6 Nov 2012 22:56:33 -0800 Subject: Multi-dimensional list initialization In-Reply-To: <5099bf7d$0$29980$c3e8da3$5496439d@news.astraweb.com> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <5099bf7d$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: <98C863CB-F92A-4997-86E3-85F2F909B3EA@gmail.com> On 2012-11-06, at 5:55 PM, Steven D'Aprano wrote: > I'm not entirely sure what your point is here. The OP screwed up -- he > didn't generate a 4-dimensional array. He generated a 2-dimensional > array. If his intuition about the number of dimensions is so poor, why > should his intuition about list multiplication be treated as sacrosanct? Yep, I may have mis-worded the explanation a bit (although I *did* express that it was a 4D matrix in the OP). I was using a 2D list to represent a 4D matrix in order to easily iterate over 90 degree rotations with zip(*matrix[::-1]). It wasn't for production code (otherwise I *would* be using numpy), it was for an online programming challenge in which external libs are not supported. > As they say, the only truly intuitive interface is the nipple. There are > many places where people's intuition about programming fail. And many > places where Fred's intuition is the opposite of Barney's intuition. I couldn't agree more with this. My question was *not* based on what I perceive to be intuitive (although most of this thread has now seemed to devolve into that and become more of a philosophical debate), but was based on what I thought may have been inconsistent behaviour (which was quickly cleared up with None being immutable and causing it to *seem* that the behaviour was inconsistent to the forgetful mind). As you touch on here, "intuition" is entirely subjective. If you're coming from a C/C++ background, I'd think that your intuition would be that everything's passed by value unless explicitly stated. Someone coming from another background (Lua perhaps?) would likely have entirely different intuition. > So while I prefer intuitively obvious behaviour where possible, it is not > the holy grail, and I am quite happy to give it up. I fail to see where there has been any giving up on intuitiveness in the context of this particular topic. In my mind, intuitiveness is generally born of repetitiveness and consistency. As everything in Python is a reference, it would seem to me to be inconsistent to treat expressions such as [[obj]*4]*4 un-semantically (Pythonically speaking) and making it *less* intuitive. I agree that Python would definitely be worse off. Demian Brecht @demianbrecht http://demianbrecht.github.com From steve+comp.lang.python at pearwood.info Wed Nov 7 03:15:07 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 07 Nov 2012 08:15:07 GMT Subject: Obnoxious postings from Google Groups References: <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> <50989a16$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: <509a188a$0$21759$c3e8da3$76491128@news.astraweb.com> On Wed, 07 Nov 2012 18:52:16 +1300, Gregory Ewing wrote: > Steven D'Aprano wrote: >> The downside is that if spaces are not argument separators, then you >> need something else to be an argument separator. Or you need argument >> delimiters. Or strings need to be quoted. Programming languages do >> these things because they are designed to be correct. Shell do not >> because they are designed for lazy users and merely aim to be "good >> enough". > > That's overly judgemental. Judgemental, sure. Overly judgemental? Not in my opinion. Besides, to some degree, all progress depends on the lazy person. It's less work to have the computer do it than to do it yourself. > In the environment where shells originated, > not being able to easily put spaces in file names wasn't considered a > problem. File names weren't thought of as names in the natural language > sense, but as identifiers in the programming sense. What you say may be true, but the question is, *why* did they think this? The closest analogue to computer files are paper files, which have always been treated as names in the natural language sense, and spaces allowed. "Miss Jones, fetch me the Acme Television Company file!" sort of thing. And this is exactly why people want spaces in file names, and have to be trained or prevented from doing so. So why did early shells ignore the (implied) business requirement that files represent natural names and instead treat them as programming identifiers? Because it was the easy thing to do. > You don't complain that you can't put spaces in identifiers in a Python > program, do you? I would if I could. But that would require the language to be... smarter? Less easy to use? *More* easy to use? It would require a major paradigm shift, and so I don't expect to treat identifiers as names, and use either CamelCase or names_with_underscores instead, even when the result is ugly. But that's not a fixed law of nature. If Inform 7 can include spaces in identifiers, so could other languages. http://www.ifwiki.org/index.php/Inform_7_for_Programmers/Part_1 -- Steven From warem2888 at gmail.com Wed Nov 7 03:49:05 2012 From: warem2888 at gmail.com (Constantine) Date: Wed, 7 Nov 2012 00:49:05 -0800 (PST) Subject: chocolate moonpies Message-ID: <823ee605-fcf2-469b-a8b5-9761e7c3a18d@googlegroups.com> chocolate moonpies http://www.google.com/search?hl=en&q=chocolate+moonpies+site:vandaydiigkij.blogspot.com&btnI=I%27m+Feeling+Lucky From jpiitula at ling.helsinki.fi Wed Nov 7 03:52:49 2012 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 07 Nov 2012 10:52:49 +0200 Subject: Multi-dimensional list initialization References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <50999214.50100@r3dsolutions.com> <5099ec1d$0$21759$c3e8da3$76491128@news.astraweb.com> Message-ID: Steven D'Aprano writes: > On Wed, 07 Nov 2012 00:23:44 +0000, MRAB wrote: > > I prefer the term "reference semantics". > > Oh good, because what the world needs is yet another name for the > same behaviour. > > - call by sharing > - call by object sharing > - call by object reference > - call by object > - call by value, where "values" are references > (according to the Java community) > - call by reference, where "references" refer to objects, not variables > (according to the Ruby community) > - reference semantics > > Anything else? > > http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing Something else: There's a call-by-* versus pass-by-* distinction, where the call-by-* would be rather different from any of the above: - call-by-value is what most languages now use: argument expressions are reduced to values before they are passed to the function / procedure / method / whatever. - call-by-name was something Algol 60 had by default: something like evaluating the argument expression every time its value is needed - call-by-need: argument expression is reduced to a value the first time its value is needed (if ever) - call-by-lazy (increasingly silly terminology, and I don't quite have an idea what it means in contrast to call-by-need) The modern confusions would then be mostly over the pass-by-* family, invariably using call-by-value in the above sense. The terminology for these tends to produce more heat than light, but I think the relevant distinctions are mostly just these: - can one modify the argument effectively [Python: yes] - can one modify the parameter with abandon [Python: don't] - can one swap [Python: no] - possibly: is it expensive to pass large objects? [Python: no] The actual rule in Scheme, Java, and Python is the same simple and sane rule: what are passed are values (argument expressions are fully evaluated before the actual call takes place), parameter passing does not involve any (observable) copying, and the arguments are bound to fresh variables (no aliasing of variables). Different communities use different words. Sometimes they use the same words about different things. Resulting in more heat than light :( (I'd have a few more things in the something-else department, but this is already much longer than I thought. Ends.) From wxjmfauth at gmail.com Wed Nov 7 03:57:29 2012 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Wed, 7 Nov 2012 00:57:29 -0800 (PST) Subject: Multi-dimensional list initialization In-Reply-To: <5099bf7d$0$29980$c3e8da3$5496439d@news.astraweb.com> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <5099bf7d$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: Le mercredi 7 novembre 2012 02:55:10 UTC+1, Steven D'Aprano a ?crit?: > > > > > > > Two-dimensional arrays in Python using lists are quite rare. Anyone who > > is doing serious numeric work where they need 2D arrays is using numpy, > > not lists. There are millions of people using Python, so it's hardly > > surprising that once or twice a year some newbie trips over this. But > > it's not something that people tend to trip over again and again and > > again, like C's "assignment is an expression" misfeature. > > -------------------- >>> from vecmat6 import * >>> from vmio5 import * Traceback (most recent call last): File "", line 1, in ImportError: No module named vmio5 >>> from vmio6 import * >>> from svdecomp6 import * >>> mm = NewMat(3, 3) >>> mm [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]] >>> mm[0][0] = 1.0; mm[0][1] = 2.0; mm[0][2] = 3.0 >>> mm[1][0] = 11.0; mm[1][1] = 12.0; mm[1][2] = 13.0 >>> mm[2][0] = 21.0; mm[2][1] = 22.0; mm[2][2] = 23.0 >>> pr(mm, 'mm=') mm= ( 1.00000e+000 2.00000e+000 3.00000e+000 ) ( 1.10000e+001 1.20000e+001 1.30000e+001 ) ( 2.10000e+001 2.20000e+001 2.30000e+001 ) >>> aa, b, cc = SVDecomp(mm) >>> pr(aa, 'aa=') aa= ( -8.08925e-002 -9.09280e-001 4.08248e-001 ) ( -4.77811e-001 -3.24083e-001 -8.16497e-001 ) ( -8.74730e-001 2.61114e-001 4.08248e-001 ) >>> pr(b, 'b=') b= ( 4.35902e+001 1.37646e+000 1.93953e-016 ) >>> pr(cc, 'cc=') cc= ( -5.43841e-001 7.33192e-001 4.08248e-001 ) ( -5.76726e-001 2.68499e-002 -8.16497e-001 ) ( -6.09610e-001 -6.79492e-001 4.08248e-001 ) >>> bb = VecToDiagMat(b) >>> cct = TransposeMat(cc) >>> oo = MatMulMatMulMat(aa, bb, cct) >>> pr(oo, 'aa * bb * cct=') aa * bb * cct= ( 1.00000e+000 2.00000e+000 3.00000e+000 ) ( 1.10000e+001 1.20000e+001 1.30000e+001 ) ( 2.10000e+001 2.20000e+001 2.30000e+001 ) >>> >>> # or >>> oo [[0.9999999999999991, 1.9999999999999993, 2.9999999999999982], [10.999999999999995, 11.99999999999999, 12.999999999999996], [20.999999999999986, 21.999999999999975, 22.999999999999986]] jmf From kushal.kumaran+python at gmail.com Wed Nov 7 04:38:06 2012 From: kushal.kumaran+python at gmail.com (Kushal Kumaran) Date: Wed, 07 Nov 2012 15:08:06 +0530 Subject: Obnoxious postings from Google Groups In-Reply-To: <5B80DD153D7D744689F57F4FB69AF47416783A68@SCACMX008.exchad.jpmchase.net> References: <50989613$0$29980$c3e8da3$5496439d@news.astraweb.com> <50999452$0$29980$c3e8da3$5496439d@news.astraweb.com> <5B80DD153D7D744689F57F4FB69AF47416783A68@SCACMX008.exchad.jpmchase.net> Message-ID: <87390ln475.fsf@nitrogen.i-did-not-set--mail-host-address--so-tickle-me> "Prasad, Ramit" writes: > Steven D'Aprano wrote: >> >> On Tue, 06 Nov 2012 17:16:44 +0000, Prasad, Ramit wrote: >> >> >> To enter the newline, I typed Ctrl-Q to tell bash to treat the next >> >> character as a literal, and then typed Ctrl-J to get a newline. >> > >> > That sounds complicated, my version of bash lets me type >> > 'foobar' for the same effect. >> >> Well, I learned something new about bash. >> >> On the other hand, the Ctrl-Q next-char-is-literal trick works for >> entering control characters that otherwise don't have a key on the >> keyboard. >> > > Would you mind elaborating on how this works? I know it's not a bash > list, but I do not understand how ctrl-J is considered a literal. > Obviously, I must have a different definition of "literal". Where > can I find a list of other literals? My Google-fu is being weak > today. :( > It's a readline thing, when you've configured it to use emacs keybindings. You can look at the emacs manual about the quoted-insert function if you want. It's useful in emacs because people like to bind ordinary keystrokes to do esoteric stuff (such as binding the TAB key to insert appropriate amount of spaces), which means that you need a way to override it (if you want to insert a literal TAB character, for example). -- regards, kushal From j-p at sunrise.ch Wed Nov 7 05:36:20 2012 From: j-p at sunrise.ch (Jean-Pierre Miceli) Date: Wed, 7 Nov 2012 11:36:20 +0100 Subject: how-to use readline.set_completion_display_matches_hook()? Message-ID: Hi all, I'm working on a tool which enable support of tab completion using the readline modul. And I have a problem with set_completion_display_matches_hook function I've created a display hook function and registered it. It is called and it prints the desire messages. But once it has completed, readline/python does not display the prompt. I've got to press the 'return' key to see the prompt again. What should the display hook function do to return correctly and let the prompt be displayed? Example: def completerHook(self, substitution, matches, longest_match_length): print "" print "Test of the display hook function" for i in range(len(matches)): print matches[i], I use Python 2.7.3 and done some tests on Mac OS 10.7 (Lion) and Ubuntu. I got the same issue on both system. Thanks for your help J-P From oscar.j.benjamin at gmail.com Wed Nov 7 06:11:15 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Wed, 7 Nov 2012 11:11:15 +0000 Subject: Multi-dimensional list initialization In-Reply-To: References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098A55C.3090201@r3dsolutions.com> <5098C873.2000200@r3dsolutions.com> <509982CA.7050701@r3dsolutions.com> Message-ID: On Nov 7, 2012 5:41 AM, "Gregory Ewing" wrote: > > If anything is to be done in this area, it would be better > as an extension of list comprehensions, e.g. > > [[None times 5] times 10] > > which would be equivalent to > > [[None for _i in xrange(5)] for _j in xrange(10)] I think you're right that the meaning of list-int multiplication can't/shouldn't be changed if this way. A multidimensional list comprehension would be useful even for people who are using numpy as it's common to use a list comprehension to initialise a numpy array. A more modest addition for the limited case described in this thread could be to use exponentiation: >>> [0] ** (2, 3) [[0, 0, 0], [0, 0, 0]] Oscar -------------- next part -------------- An HTML attachment was scrubbed... URL: From apprentice3d at gmail.com Wed Nov 7 07:40:45 2012 From: apprentice3d at gmail.com (Apprentice3D) Date: Wed, 7 Nov 2012 07:40:45 -0500 Subject: multiprocessing help (Terry Reedy) In-Reply-To: References: Message-ID: Sent from my iPad On 2012-11-07, at 12:15 AM, python-list-request at python.org wrote: > Re: multiprocessing help (Terry Reedy) From __peter__ at web.de Wed Nov 7 07:54:05 2012 From: __peter__ at web.de (Peter Otten) Date: Wed, 07 Nov 2012 13:54:05 +0100 Subject: clicking on turtle References: <201211062109.qA6L9IvA006105@hydra.calcul> Message-ID: Nicolas Graner wrote: > I have a problem with the standard "turtle" module. When a turtle has > a custom shape of type "compound", it doesn't seem to respond to click > events. No problem with polygon shapes. > > Running python 3.2.3, turtle version 1.1b on Windows XP. > > Here is my test file: > > ################################################## > import turtle > square = ((0,0),(0,20),(20,20),(20,0)) > turtle.addshape("sq1", square) # sq1 = polygon shape > s = turtle.Shape("compound") > s.addcomponent(square, "red") > turtle.addshape("sq2", s) # sq2 = compound shape > t1 = turtle.Turtle(shape="sq1") > t2 = turtle.Turtle(shape="sq2") > t2.fd(20) # set the turtles side by side > def click(x,y): print("click at",x,y) > t1.onclick(click) > t2.onclick(click) > turtle.mainloop() > ################################################## > > When I run this and click on the black square (i.e. t1), the message > "click at..." gets printed on the console. When I click on the red > square (i.e. t2), nothing happens. > > Bug or feature? I believe it's a bug. Please report it on http://bugs.python.org As a quick-fix here's a monkey patch that seems to work: import turtle def _onclick(self, item, fun, num=1, add=None): if isinstance(item, list): for item in item: self._onclick(item, fun, num, add) return if fun is None: self.cv.tag_unbind(item, "" % num) else: def eventfun(event): x, y = (self.cv.canvasx(event.x)/self.xscale, -self.cv.canvasy(event.y)/self.yscale) fun(x, y) self.cv.tag_bind(item, "" % num, eventfun, add) turtle.TurtleScreenBase._onclick = _onclick square = ((0,0),(0,20),(20,20),(20,0)) turtle.addshape("sq1", square) # sq1 = polygon shape s = turtle.Shape("compound") s.addcomponent(square, "red") turtle.addshape("sq2", s) # sq2 = compound shape t1 = turtle.Turtle(shape="sq1") t2 = turtle.Turtle(shape="sq2") t2.fd(20) # set the turtles side by side def click(x,y): print("click at",x,y) t1.onclick(click) t2.onclick(click) turtle.mainloop() From stefan at epy.co.at Wed Nov 7 08:14:56 2012 From: stefan at epy.co.at (Stefan H. Holek) Date: Wed, 7 Nov 2012 14:14:56 +0100 Subject: how-to use readline.set_completion_display_matches_hook()? In-Reply-To: References: Message-ID: On 07.11.2012, at 11:36, Jean-Pierre Miceli wrote: > Hi all, > > I'm working on a tool which enable support of tab completion using the > readline modul. > And I have a problem with set_completion_display_matches_hook function > > I've created a display hook function and registered it. It is called > and it prints the desire messages. But once it has completed, > readline/python does not display the prompt. > I've got to press the 'return' key to see the prompt again. > What should the display hook function do to return correctly and let > the prompt be displayed? > > Example: > def completerHook(self, substitution, matches, longest_match_length): > print "" > print "Test of the display hook function" > for i in range(len(matches)): > print matches[i], > > I use Python 2.7.3 and done some tests on Mac OS 10.7 (Lion) and > Ubuntu. I got the same issue on both system. This always happens when you print newlines behind readline's back. To update the prompt area, you'd have to call rl_forced_update_display() which is AFAIK not exposed by the stdlib's readline bindings. There is a more complete implementation of the GNU Readline APIs at http://pypi.python.org/pypi/rl. With rl you can fix the prompt by calling rl.readline.redisplay(force=True) after the hook has returned. Hope this helps, Stefan -- Stefan H. Holek stefan at epy.co.at From joshua.landau.ws at gmail.com Wed Nov 7 08:39:57 2012 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 7 Nov 2012 13:39:57 +0000 Subject: Multi-dimensional list initialization In-Reply-To: References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098A55C.3090201@r3dsolutions.com> <5098C873.2000200@r3dsolutions.com> <509982CA.7050701@r3dsolutions.com> Message-ID: On 7 November 2012 11:11, Oscar Benjamin wrote: > On Nov 7, 2012 5:41 AM, "Gregory Ewing" > wrote: > > > > If anything is to be done in this area, it would be better > > as an extension of list comprehensions, e.g. > > > > [[None times 5] times 10] > > > > which would be equivalent to > > > > [[None for _i in xrange(5)] for _j in xrange(10)] > > I think you're right that the meaning of list-int multiplication > can't/shouldn't be changed if this way. > > A multidimensional list comprehension would be useful even for people who > are using numpy as it's common to use a list comprehension to initialise a > numpy array. > > A more modest addition for the limited case described in this thread could > be to use exponentiation: > > >>> [0] ** (2, 3) > [[0, 0, 0], [0, 0, 0]] > Hold on: why not just use multiplication? >>> [0] * (2, 3) is an error now, and it makes total sense. Additionally, it's not breaking the "no copy -- _ever_" rule because none of the lists existed before. The values inside the list would be by reference, as before, so lst * (x,) would be the same as lst * x if x is an integer. *I* would use this a lot. This is the first thing on this thread that makes a lot of sense to me. We do have to think of the potential problems, though. There are definitely some. For one, code that relies on lst * x throwing an error would break. It may confuse others - although I don't see how. But I don't see any big problems, so I really do like this idea. -------------- next part -------------- An HTML attachment was scrubbed... URL: From oscar.j.benjamin at gmail.com Wed Nov 7 09:00:27 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Wed, 7 Nov 2012 14:00:27 +0000 Subject: Multi-dimensional list initialization In-Reply-To: References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098A55C.3090201@r3dsolutions.com> <5098C873.2000200@r3dsolutions.com> <509982CA.7050701@r3dsolutions.com> Message-ID: On 7 November 2012 13:39, Joshua Landau wrote: > > On 7 November 2012 11:11, Oscar Benjamin wrote: >> >> A more modest addition for the limited case described in this thread could >> be to use exponentiation: >> >> >>> [0] ** (2, 3) >> [[0, 0, 0], [0, 0, 0]] > > Hold on: why not just use multiplication? > >>>> [0] * (2, 3) > > is an error now, and it makes total sense. Additionally, it's not breaking > the "no copy -- _ever_" rule because none of the lists existed before. The > values inside the list would be by reference, as before, so lst * (x,) would > be the same as lst * x if x is an integer. The problem is that this operation is asymmetric. Currently int/list multiplication is commutative so that: ['a', 'b'] * 2 == 2 * ['a', 'b'] If you use this kind of multiplication what happens to the other cases? e.g. what do you give for: >>> [0] * [2, 3] >>> [2, 3] * [0] >>> (2, 3) * [0] >>> (2, 3) * (4, 5) and so on. Although Python does not guarantee commutativity of multiplication in general I think that since for lists it has always been commutative it would be bad to change that. Exponentiation is expected to be asymmetric and is currently unused so there is no ambiguity. The problem is if someone has already subclassed list and added an exponentiation method. Oscar From ethan at stoneleaf.us Wed Nov 7 09:47:53 2012 From: ethan at stoneleaf.us (Ethan Furman) Date: Wed, 07 Nov 2012 06:47:53 -0800 Subject: Multi-dimensional list initialization In-Reply-To: References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098A55C.3090201@r3dsolutions.com> <5098C873.2000200@r3dsolutions.com> <509982CA.7050701@r3dsolutions.com> Message-ID: <509A7499.40401@stoneleaf.us> Oscar Benjamin wrote: > On Nov 7, 2012 5:41 AM, "Gregory Ewing" > wrote: > > > > If anything is to be done in this area, it would be better > > as an extension of list comprehensions, e.g. > > > > [[None times 5] times 10] > > > > which would be equivalent to > > > > [[None for _i in xrange(5)] for _j in xrange(10)] > > I think you're right that the meaning of list-int multiplication > can't/shouldn't be changed if this way. > > A multidimensional list comprehension would be useful even for people > who are using numpy as it's common to use a list comprehension to > initialise a numpy array. > > A more modest addition for the limited case described in this thread > could be to use exponentiation: > > >>> [0] ** (2, 3) > [[0, 0, 0], [0, 0, 0]] What would happen with --> [{}] ** (2, 3) or --> [my_custom_container()] ** (2, 3) ? ~Ethan~ From invalid at invalid.invalid Wed Nov 7 10:13:49 2012 From: invalid at invalid.invalid (Grant Edwards) Date: Wed, 7 Nov 2012 15:13:49 +0000 (UTC) Subject: Obnoxious postings from Google Groups References: <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> <50989a16$0$29980$c3e8da3$5496439d@news.astraweb.com> <509999e0$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2012-11-06, Steven D'Aprano wrote: > On Tue, 06 Nov 2012 11:51:03 -0500, GangGreene wrote: > >> I have just finished a 251 line bash shell script that builds my linux >> distro from scratch. > > "From scratch"? So if you run it on bare metal with no OS, it works? > >:-P > > But seriously -- bash is a mature, powerful shell. It works well for > what it does. It has been designed to make it easy[1] to automate > system admin tasks. And even people who have been writing bourne/korn/bash shell sripts for 30 years (crap, I'm old...) still occasionally (or even regularly) fall into the "spaces in filenames hole". It's just too easy to type $Var instead of "$Var". And not setting the "nounset" option can result in hours of fun trying to find that one place where a variable name is mistyped... > It would be astonishing if an experienced, competent bash > programmer couldn't write an installation tool in bash. Indeed. But when the good folks at RedHat sat down to write an installation tool back in '95 or so, they chose Python. > By comparison, few general purpose programming languages (with the > obvious exception of perl) are designed for system administration as > their primary purpose. > > But... how robust is your script? How many bugs does it contain? > Chances are you will only use it a handful of times, on the same > system. That's not a lot of testing to be sure that it is free of > bugs, and robust against unexpected input. > > Hell, even widely used and distributed install scripts written by > companies with the resources of Ubuntu and Red Hat have a distressing > tendency to break. Or worse, to behave in unexpected ways. That's not really helping your argument, since RedHat's install scripts have been written in Pyton since forever... > In my opinion, control structures like "for" and "if" in bash are > hard to use, hard to read, easy to get wrong, give cryptic error > messages when you do get them wrong, and are ugly. Tests are > obfuscated, and as always, the fact that everything is text in bash > means it is way harder than it needs to be to use rich, modern data > structures. I think anybody who writes anything of substance in bash would have to agree. However when the task involves mainly manipulating files and running other programs, the clumsy control structures are a small enough price to pay for the ease with which bash deals with manipulating files/paths/programs. OTOH, you can use C shell or PHP and get the worst of both worlds... -- Grant Edwards grant.b.edwards Yow! I'm wet! I'm wild! at gmail.com From ethan at stoneleaf.us Wed Nov 7 10:23:12 2012 From: ethan at stoneleaf.us (Ethan Furman) Date: Wed, 07 Nov 2012 07:23:12 -0800 Subject: Multi-dimensional list initialization In-Reply-To: <50999214.50100@r3dsolutions.com> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <50999214.50100@r3dsolutions.com> Message-ID: <509A7CE0.7080100@stoneleaf.us> After this post the only credibility you have left (with me, anyway) is that you seem to be willing to learn. So learn the way Python works before you try to reimplement it. ~Ethan~ From ramit.prasad at jpmorgan.com Wed Nov 7 10:57:38 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Wed, 7 Nov 2012 15:57:38 +0000 Subject: Multi-dimensional list initialization In-Reply-To: References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <50999214.50100@r3dsolutions.com> <5099ec1d$0$21759$c3e8da3$76491128@news.astraweb.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF474167850E0@SCACMX008.exchad.jpmchase.net> Gregory Ewing wrote: > > Roy Smith wrote: > > Call by social network? The called function likes the object. > > Depending on how it feels, it can also comment on some of the object's > > attributes. > > And then finds that it has inadvertently shared all its > private data with other functions accessing the object. And this is where Dihedral 8888 (or whatever the bot is called) tells you that Python has no "private" variables. :) ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From devashishrocker at gmail.com Wed Nov 7 11:07:17 2012 From: devashishrocker at gmail.com (Devashish Tyagi) Date: Wed, 7 Nov 2012 08:07:17 -0800 (PST) Subject: Pickling a dictionary Message-ID: So I want to store the current state of a InteractiveInterpreter Object in database. In order to achieve this I tried this obj = InteractiveInterpreter() local = obj.locals() pickle.dump(local, open('obj.dump','rw')) But I received an error say TypeError: can't pickle ellipsis objects >From what I understand this shouldn't occur as local is a dictionary. Any particular reason for this behaviour? From oscar.j.benjamin at gmail.com Wed Nov 7 11:15:17 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Wed, 7 Nov 2012 16:15:17 +0000 Subject: Multi-dimensional list initialization In-Reply-To: <509A7499.40401@stoneleaf.us> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098A55C.3090201@r3dsolutions.com> <5098C873.2000200@r3dsolutions.com> <509982CA.7050701@r3dsolutions.com> <509A7499.40401@stoneleaf.us> Message-ID: On Nov 7, 2012 3:55 PM, "Ethan Furman" wrote: > > Oscar Benjamin wrote: >> >> A more modest addition for the limited case described in this thread could be to use exponentiation: >> >> >>> [0] ** (2, 3) >> [[0, 0, 0], [0, 0, 0]] > > > What would happen with > > --> [{}] ** (2, 3) The list being exponentiated does nothing with its elements. The exponentiation just tells it to create a list of distinct lists. In this case each element of each sublist is the same dict. However if you assign to an element of the sublist (rather than into the dict) it replaces the dict in that sublist and not the others. > > or > > --> [my_custom_container()] ** (2, 3) Ditto Oscar -------------- next part -------------- An HTML attachment was scrubbed... URL: From ian.g.kelly at gmail.com Wed Nov 7 11:16:34 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 7 Nov 2012 09:16:34 -0700 Subject: Pickling a dictionary In-Reply-To: References: Message-ID: On Wed, Nov 7, 2012 at 9:07 AM, Devashish Tyagi wrote: > So I want to store the current state of a InteractiveInterpreter Object in database. In order to achieve this I tried this > > obj = InteractiveInterpreter() > local = obj.locals() > pickle.dump(local, open('obj.dump','rw')) > > But I received an error say > TypeError: can't pickle ellipsis objects > > From what I understand this shouldn't occur as local is a dictionary. Any particular reason for this behaviour? The contents of the dictionary need to be pickleable as well. You probably have an ellipsis object in the dict somewhere. From __peter__ at web.de Wed Nov 7 11:26:23 2012 From: __peter__ at web.de (Peter Otten) Date: Wed, 07 Nov 2012 17:26:23 +0100 Subject: Pickling a dictionary References: Message-ID: Devashish Tyagi wrote: > So I want to store the current state of a InteractiveInterpreter Object in > database. In order to achieve this I tried this > > obj = InteractiveInterpreter() > local = obj.locals() > pickle.dump(local, open('obj.dump','rw')) Assuming InteractiveInterpreter is imported from the code module the above will fail with a TypeError. Please copy-and paste code snippets to avoid guessing games. > But I received an error say > TypeError: can't pickle ellipsis objects > > From what I understand this shouldn't occur as local is a dictionary. Any > particular reason for this behaviour? For a dict to be pickled all its keys and values have to be pickled, too. From ian.g.kelly at gmail.com Wed Nov 7 11:37:37 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 7 Nov 2012 09:37:37 -0700 Subject: Pickling a dictionary In-Reply-To: References: Message-ID: On Wed, Nov 7, 2012 at 9:16 AM, Ian Kelly wrote: > On Wed, Nov 7, 2012 at 9:07 AM, Devashish Tyagi > wrote: >> So I want to store the current state of a InteractiveInterpreter Object in database. In order to achieve this I tried this >> >> obj = InteractiveInterpreter() >> local = obj.locals() >> pickle.dump(local, open('obj.dump','rw')) >> >> But I received an error say >> TypeError: can't pickle ellipsis objects >> >> From what I understand this shouldn't occur as local is a dictionary. Any particular reason for this behaviour? > > The contents of the dictionary need to be pickleable as well. You > probably have an ellipsis object in the dict somewhere. By the way, if you use Python 3 and pickle protocol 3, then Ellipsis *is* pickleable: Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import pickle >>> pickle.dumps(Ellipsis, protocol=pickle.HIGHEST_PROTOCOL) b'\x80\x03cbuiltins\nEllipsis\nq\x00.' Cheers, Ian From mmorrigan at gmail.com Wed Nov 7 11:52:35 2012 From: mmorrigan at gmail.com (Martha Morrigan) Date: Wed, 7 Nov 2012 08:52:35 -0800 (PST) Subject: Printing a text over an image Message-ID: <2e801261-9ec6-4660-bbd1-6168c3dce8ed@v3g2000yqb.googlegroups.com> Hi guys, Using python, wxpython and sqlite in a windows system, Im trying to print some certificates/diplomas/cards with a image at background with the name of person/text over it. I know the basic steps to print the text using win32print from Pywin32 but...: 1) I dont know how to add an image and set it to background. while ..... ..... # Query sqlite rows and collumn name and set the self.text for each certificate ..... # Now send to printer DC = win32ui.CreateDC() DC.CreatePrinterDC(win32print.GetDefaultPrinter()) DC.SetMapMode(win32con.MM_TWIPS) DC.StartDoc("Certificates Job") DC.StartPage() ux = 1000 uy = -1000 lx = 5500 ly = -55000 DC.DrawText(self.text, (ux, uy, lx, ly),win32con.DT_LEFT) DC.EndPage() DC.EndDoc() This printer-code is inside a while loop calling each people name from a sqlite database per check condition. 2) All the names of database was printed at same page... how i command the printer to spit out 1 page per name from the database? 3) Any more simple approach or module to deals with printers (paper and/or pdf) will be welcome. Thanks in advance, Martha From andrea.crotti.0 at gmail.com Wed Nov 7 12:13:47 2012 From: andrea.crotti.0 at gmail.com (andrea crotti) Date: Wed, 7 Nov 2012 17:13:47 +0000 Subject: creating size-limited tar files Message-ID: Simple problem, given a lot of data in many files/directories, I should create a tar file splitted in chunks <= a given size. The simplest way would be to compress the whole thing and then split. At the moment the actual script which I'm replacing is doing a "system('split..')", which is not that great, so I would like to do it while compressing. So I thought about (in pseudocode) while remaining_files: tar_file.addfile(remaining_files.pop()) if size(tar_file) >= limit: close(tar_file) tar_file = new_tar_file() which might work maybe, but how do I get the current size? There should be tarinfo.size but it doesn't exist on a TarFile opened in write mode, so should I do a stat after each flush? Any other better ideas otherwise? thanks From python at mrabarnett.plus.com Wed Nov 7 12:17:02 2012 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 07 Nov 2012 17:17:02 +0000 Subject: Multi-dimensional list initialization In-Reply-To: <5099ec1d$0$21759$c3e8da3$76491128@news.astraweb.com> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <50999214.50100@r3dsolutions.com> <5099ec1d$0$21759$c3e8da3$76491128@news.astraweb.com> Message-ID: <509A978E.7060107@mrabarnett.plus.com> On 2012-11-07 05:05, Steven D'Aprano wrote: > On Wed, 07 Nov 2012 00:23:44 +0000, MRAB wrote: > >>> Incorrect. Python uses what is commonly known as call-by-object, not >>> call-by-value or call-by-reference. Passing the list by value would >>> imply that the list is copied, and that appends or removes to the list >>> inside the function would not affect the original list. This is not >>> what Python does; the list inside the function and the list passed in >>> are the same list. At the same time, the function does not have access >>> to the original reference to the list and cannot reassign it by >>> reassigning its own reference, so it is not call-by-reference semantics >>> either. >>> >> I prefer the term "reference semantics". > > > Oh good, because what the world needs is yet another name for the same > behaviour. > > - call by sharing > - call by object sharing > - call by object reference > - call by object > - call by value, where "values" are references > (according to the Java community) > - call by reference, where "references" refer to objects, not variables > (according to the Ruby community) > - reference semantics > > > Anything else? > > http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing > The disadvantage of calling it "call by ..." is that it suggests that you're just talking about calling functions. What about binding in general, eg "x = y"? Does it make sense to still call it "call by ..."? From devashishrocker at gmail.com Wed Nov 7 12:40:26 2012 From: devashishrocker at gmail.com (Devashish Tyagi) Date: Wed, 7 Nov 2012 09:40:26 -0800 (PST) Subject: Pickling a dictionary In-Reply-To: References: Message-ID: On Wednesday, 7 November 2012 21:57:05 UTC+5:30, Peter Otten wrote: > Devashish Tyagi wrote: > > > > > So I want to store the current state of a InteractiveInterpreter Object in > > > database. In order to achieve this I tried this > > > > > > obj = InteractiveInterpreter() > > > local = obj.locals() > > > pickle.dump(local, open('obj.dump','rw')) > > > > Assuming InteractiveInterpreter is imported from the code module the above > > will fail with a TypeError. Please copy-and paste code snippets to avoid > > guessing games. Here is the code from code import InteractiveInterpreter import StringIO import pickle src = StringIO.StringIO() inter = InteractiveInterpreter() inter.runcode('a = 5') local = inter.locals pickle.dump(local,open('obj.dump','wb')) Here is the error Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.7/pickle.py", line 1370, in dump Pickler(file, protocol).dump(obj) File "/usr/lib/python2.7/pickle.py", line 224, in dump self.save(obj) File "/usr/lib/python2.7/pickle.py", line 286, in save f(self, obj) # Call unbound method with explicit self File "/usr/lib/python2.7/pickle.py", line 649, in save_dict self._batch_setitems(obj.iteritems()) File "/usr/lib/python2.7/pickle.py", line 663, in _batch_setitems save(v) File "/usr/lib/python2.7/pickle.py", line 286, in save f(self, obj) # Call unbound method with explicit self File "/usr/lib/python2.7/pickle.py", line 649, in save_dict self._batch_setitems(obj.iteritems()) File "/usr/lib/python2.7/pickle.py", line 663, in _batch_setitems save(v) File "/usr/lib/python2.7/pickle.py", line 306, in save rv = reduce(self.proto) File "/usr/lib/python2.7/copy_reg.py", line 70, in _reduce_ex raise TypeError, "can't pickle %s objects" % base.__name__ TypeError: can't pickle ellipsis objects > > > > > But I received an error say > > > TypeError: can't pickle ellipsis objects > > > > > > From what I understand this shouldn't occur as local is a dictionary. Any > > > particular reason for this behaviour? > > > > For a dict to be pickled all its keys and values have to be pickled, too. From devashishrocker at gmail.com Wed Nov 7 12:40:26 2012 From: devashishrocker at gmail.com (Devashish Tyagi) Date: Wed, 7 Nov 2012 09:40:26 -0800 (PST) Subject: Pickling a dictionary In-Reply-To: References: Message-ID: On Wednesday, 7 November 2012 21:57:05 UTC+5:30, Peter Otten wrote: > Devashish Tyagi wrote: > > > > > So I want to store the current state of a InteractiveInterpreter Object in > > > database. In order to achieve this I tried this > > > > > > obj = InteractiveInterpreter() > > > local = obj.locals() > > > pickle.dump(local, open('obj.dump','rw')) > > > > Assuming InteractiveInterpreter is imported from the code module the above > > will fail with a TypeError. Please copy-and paste code snippets to avoid > > guessing games. Here is the code from code import InteractiveInterpreter import StringIO import pickle src = StringIO.StringIO() inter = InteractiveInterpreter() inter.runcode('a = 5') local = inter.locals pickle.dump(local,open('obj.dump','wb')) Here is the error Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.7/pickle.py", line 1370, in dump Pickler(file, protocol).dump(obj) File "/usr/lib/python2.7/pickle.py", line 224, in dump self.save(obj) File "/usr/lib/python2.7/pickle.py", line 286, in save f(self, obj) # Call unbound method with explicit self File "/usr/lib/python2.7/pickle.py", line 649, in save_dict self._batch_setitems(obj.iteritems()) File "/usr/lib/python2.7/pickle.py", line 663, in _batch_setitems save(v) File "/usr/lib/python2.7/pickle.py", line 286, in save f(self, obj) # Call unbound method with explicit self File "/usr/lib/python2.7/pickle.py", line 649, in save_dict self._batch_setitems(obj.iteritems()) File "/usr/lib/python2.7/pickle.py", line 663, in _batch_setitems save(v) File "/usr/lib/python2.7/pickle.py", line 306, in save rv = reduce(self.proto) File "/usr/lib/python2.7/copy_reg.py", line 70, in _reduce_ex raise TypeError, "can't pickle %s objects" % base.__name__ TypeError: can't pickle ellipsis objects > > > > > But I received an error say > > > TypeError: can't pickle ellipsis objects > > > > > > From what I understand this shouldn't occur as local is a dictionary. Any > > > particular reason for this behaviour? > > > > For a dict to be pickled all its keys and values have to be pickled, too. From ian.g.kelly at gmail.com Wed Nov 7 13:03:11 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 7 Nov 2012 11:03:11 -0700 Subject: Pickling a dictionary In-Reply-To: References: Message-ID: On Wed, Nov 7, 2012 at 10:40 AM, Devashish Tyagi wrote: > Here is the code > > from code import InteractiveInterpreter > import StringIO > import pickle > > src = StringIO.StringIO() > inter = InteractiveInterpreter() > inter.runcode('a = 5') > local = inter.locals > > pickle.dump(local,open('obj.dump','wb')) After calling runcode it seems that the __builtins__ dict containing an 'Ellipsis' binding has been added to inter.locals. You probably don't want to include the contents of __builtins__ in your pickle anyway, so just delete it: del local['__builtins__'] pickle.dump(local, ...) From laurent.pointal at free.fr Wed Nov 7 13:16:58 2012 From: laurent.pointal at free.fr (Laurent Pointal) Date: Wed, 07 Nov 2012 19:16:58 +0100 Subject: Printing a text over an image References: <2e801261-9ec6-4660-bbd1-6168c3dce8ed@v3g2000yqb.googlegroups.com> Message-ID: <509aa59a$0$1835$426a74cc@news.free.fr> Martha Morrigan wrote: > > 3) Any more simple approach or module to deals with printers (paper > and/or pdf) will be welcome. For pdf, you can take a look at ReportLab's Toolkit. I used it to build a "trombinoscope" (ie an employee directory with each member's photo on top of his name and room number). http://www.reportlab.com/software/opensource/ Once created the pdf, you must find a solution to send it to the printer... > > Thanks in advance, > > Martha A+ Laurent. From neilc at norwich.edu Wed Nov 7 13:40:14 2012 From: neilc at norwich.edu (Neil Cerutti) Date: 7 Nov 2012 18:40:14 GMT Subject: creating size-limited tar files References: Message-ID: On 2012-11-07, andrea crotti wrote: > Simple problem, given a lot of data in many files/directories, I > should create a tar file splitted in chunks <= a given size. > > The simplest way would be to compress the whole thing and then split. > > At the moment the actual script which I'm replacing is doing a > "system('split..')", which is not that great, so I would like to do it > while compressing. > > So I thought about (in pseudocode) > > while remaining_files: > tar_file.addfile(remaining_files.pop()) > if size(tar_file) >= limit: > close(tar_file) > tar_file = new_tar_file() > I have not used this module before, but what you seem to be asking about is: TarFile.gettarinfo().size But your algorithm stops after the file is already too big. -- Neil Cerutti From news at blinne.net Wed Nov 7 14:05:30 2012 From: news at blinne.net (Alexander Blinne) Date: Wed, 07 Nov 2012 20:05:30 +0100 Subject: creating size-limited tar files In-Reply-To: References: Message-ID: <509ab0fa$0$6636$9b4e6d93@newsspool2.arcor-online.net> I don't know the best way to find the current size, I only have a general remark. This solution is not so good if you have to impose a hard limit on the resulting file size. You could end up having a tar file of size "limit + size of biggest file - 1 + overhead" in the worst case if the tar is at limit - 1 and the next file is the biggest file. Of course that may be acceptable in many cases or it may be acceptable to do something about it by adjusting the limit. My Idea: Assuming tar_file works on some object with a file-like interface one could implement a "transparent splitting file" class which would have to use some kind of buffering mechanism. It would represent a virtual big file that is stored in many pieces of fixed size (except the last) and would allow you to just add all files to one tar_file and have it split up transparently by the underlying file-object, something like tar_file = TarFile(SplittingFile(names='archiv.tar-%03d', chunksize= chunksize, mode='wb')) while remaining_files: tar_file.addfile(remaining_files.pop()) and the splitting_file would automatically create chunks with size chunksize and filenames archiv.tar-001, archiv.tar-002, ... The same class could be used to put it back together, it may even implement transparent seeking over a set of pieces of a big file. I would like to have such a class around for general usage. greetings From andrew3 at r3dsolutions.com Wed Nov 7 14:51:48 2012 From: andrew3 at r3dsolutions.com (Andrew Robinson) Date: Wed, 07 Nov 2012 11:51:48 -0800 Subject: Multi-dimensional list initialization In-Reply-To: References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <50999214.50100@r3dsolutions.com> Message-ID: <509ABBD4.7020200@r3dsolutions.com> Hi IAN! On 11/06/2012 03:52 PM, Ian Kelly wrote: > On Tue, Nov 6, 2012 at 3:41 PM, Andrew Robinson > > The objection is not nonsense; you've merely misconstrued it. If > [[1,2,3]] * 4 is expected to create a mutable matrix of 1s, 2s, and > 3s, then one would expect [[{}]] * 4 to create a mutable matrix of > dicts. If the dicts are not copied, then this fails for the same > reason :) The idea does create a multable list of dicts; just not a mutable list of different dicts. > >>> Q: How about if I use delegation to proxy a list? >>> A: Oh no, they definitely won't be copied. >> Give an example usage of why someone would want to do this. Then we can >> discuss it. > Seriously? Read a book on design patterns. You might start at SO: > > http://stackoverflow.com/questions/832536/when-to-use-delegation-instead-of-inheritance :) I wasn't discarding the argument, I was asking for a use case to examine. I know what a delegation *is*; but I'm not spending lots of times thinking about this issue. (Besides this thread just went more or less viral, and I can't keep up) I have a book on design patterns -- in fact, the one called "Design Patterns" by Gamma, Helm, Johnson, Vlissides. (Is it out of date already or something?) >> Please link to the objection being proposed to the developers, and >> their reasoning for rejecting it. I think you are exaggerating. > > From Google: > > http://bugs.python.org/issue1408 > http://bugs.python.org/issue12597 > http://bugs.python.org/issue9108 > http://bugs.python.org/issue7823 > > Note that in two out of these four cases, the reporter was trying to > multiply lists of dicts, not just lists of lists. That's helpful. Thanks. I'll look into these. >> Besides, 2D arrays are *not* rare and people *have* to copy internals of >> them very often. >> The copy speed will be the same or *faster*, and the typing less -- and the >> psychological mistakes *less*, the elegance more. > List multiplication is not potentially useful for copying 2D lists, > only for initializing them. For copying an existing nested list, > you're still stuck with either copy.deepcopy() or a list > comprehension. Yes, I totally agree. But, as far as I know -- the primary use of list multiplication is initialization. That was my point about the most compact notation ought to be for the most common case. Initialization is a very common use case. List comprehensions are appropriate for the other's. Even D'Aprano thought the * operator was not a common operation; and I suppose that when compared to other operations done in a program (relative counting) he's correct; most programs are not primarily matrix or initialization oriented. > >> It's hardly going to confuse anyone to say that lists are copied with list >> multiplication, but the elements are not. >> >> Every time someone passes a list to a function, they *know* that the list is >> passed by value -- and the elements are passed by reference. People in >> Python are USED to lists being "the" way to weird behavior that other >> languages don't do. > Incorrect. Python uses what is commonly known as call-by-object, not > call-by-value or call-by-reference. Passing the list by value would > imply that the list is copied, and that appends or removes to the list > inside the function would not affect the original list. Interesting, you avoided the main point "lists are copied with list multiplication". But, in any event: _Pass_ by value (not call by value) is a term stretching back 30 years; eg: when I learned the meaning of the words. Rewording it as "Call by value" is something that happened later, and the nuance is lost on those without a very wide programming knowledge *and* age. In any event: All objects in Python are based on pointers; all parameters passed to functions, etc, are *copies* of those pointers; (by pointer value). I made the distinction between contents of the list and the list object itself for that reason; I gave an explicit correction to the _pass_ by "value" generalization by saying: ("the elements are passed by reference"). The concept I gave, although archaically stated -- still correctly represents what actually happens in Python and can be seen from it's source code(s). The point I am making is not generally true of everyone learning Python; For some people obviously learn it from scratch. But, for people who learn the language after a transition, this is a common FAQ; how do I modify the variables by reference and not by value; -- the answer is, you can't -- you must embed the return value in another object; parameters are always passed the *same* way. Every function written, then, has to decide when objects are passed to it -- whether to modify or copy the object (internals) when modifying it. That's all I meant. -------------- next part -------------- An HTML attachment was scrubbed... URL: From roy at panix.com Wed Nov 7 15:32:23 2012 From: roy at panix.com (Roy Smith) Date: Wed, 07 Nov 2012 15:32:23 -0500 Subject: creating size-limited tar files References: <509ab0fa$0$6636$9b4e6d93@newsspool2.arcor-online.net> Message-ID: In article <509ab0fa$0$6636$9b4e6d93 at newsspool2.arcor-online.net>, Alexander Blinne wrote: > I don't know the best way to find the current size, I only have a > general remark. > This solution is not so good if you have to impose a hard limit on the > resulting file size. You could end up having a tar file of size "limit + > size of biggest file - 1 + overhead" in the worst case if the tar is at > limit - 1 and the next file is the biggest file. Of course that may be > acceptable in many cases or it may be acceptable to do something about > it by adjusting the limit. If you truly have a hard limit, one possible solution would be to use tell() to checkpoint the growing archive after each addition. If adding a new file unexpectedly causes you exceed your hard limit, you can seek() back to the previous spot and truncate the file there. Whether this is worth the effort is an exercise left for the reader. From ian.g.kelly at gmail.com Wed Nov 7 16:01:19 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 7 Nov 2012 14:01:19 -0700 Subject: Multi-dimensional list initialization In-Reply-To: <509ABBD4.7020200@r3dsolutions.com> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <50999214.50100@r3dsolutions.com> <509ABBD4.7020200@r3dsolutions.com> Message-ID: On Wed, Nov 7, 2012 at 12:51 PM, Andrew Robinson wrote: > Interesting, you avoided the main point "lists are copied with list > multiplication". It seems that each post is longer than the last. If we each responded to every point made, this thread would fill a book. Anyway, your point was to suggest that people would not be confused by having list multiplication copy lists but not other objects, because passing lists into functions as parameters works in basically the same way. Except that it does not work the same way, because when lists are passed into functions, they are not copied at all. Nor are are any of their contents copied, lists or not. So actually I did address this point with the "call-by-object" tangent; I just did not explicitly link it back to your thesis. > But, in any event: > Pass by value (not call by value) is a term stretching back 30 years; eg: > when I learned the meaning of the words. Rewording it as "Call by value" is > something that happened later, and the nuance is lost on those without a > very wide programming knowledge *and* age. Potayto, potahto. The distinction that you're describing is between "strict" versus "non-strict" evaluation strategies. Hinging the distinction on the non-descriptive words "call" and "pass" is lazy terminology that should never have been introduced in the first place. > In any event: > All objects in Python are based on pointers; all parameters passed to > functions, etc, are *copies* of those pointers; (by pointer value). No, all parameters passed to functions are *objects*. Python itself has no concept of pointers. What you describe is true as an implementation detail for CPython but not necessarily true for other implementations, and not true at all for an abstract (implementation-independent) view of the language. > I made the distinction between contents of the list and the list object > itself for that reason; I gave an explicit correction to the pass by "value" > generalization by saying: ("the elements are passed by reference"). The elements are not passed anywhere. Only the list object is passed to the function, which is completely agnostic of the fact that the list object happens to contain other objects. From andrea.crotti.0 at gmail.com Wed Nov 7 16:52:18 2012 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Wed, 07 Nov 2012 21:52:18 +0000 Subject: creating size-limited tar files In-Reply-To: References: <509ab0fa$0$6636$9b4e6d93@newsspool2.arcor-online.net> Message-ID: <509AD812.2060605@gmail.com> On 11/07/2012 08:32 PM, Roy Smith wrote: > In article <509ab0fa$0$6636$9b4e6d93 at newsspool2.arcor-online.net>, > Alexander Blinne wrote: > >> I don't know the best way to find the current size, I only have a >> general remark. >> This solution is not so good if you have to impose a hard limit on the >> resulting file size. You could end up having a tar file of size "limit + >> size of biggest file - 1 + overhead" in the worst case if the tar is at >> limit - 1 and the next file is the biggest file. Of course that may be >> acceptable in many cases or it may be acceptable to do something about >> it by adjusting the limit. > If you truly have a hard limit, one possible solution would be to use > tell() to checkpoint the growing archive after each addition. If adding > a new file unexpectedly causes you exceed your hard limit, you can > seek() back to the previous spot and truncate the file there. > > Whether this is worth the effort is an exercise left for the reader. So I'm not sure if it's an hard limit or not, but I'll check tomorrow. But in general for the size I could also take the size of the files and simply estimate the size of all of them, pushing as many as they should fit in a tarfile. With compression I might get a much smaller file maybe, but it would be much easier.. But the other problem is that at the moment the people that get our chunks reassemble the file with a simple: cat file1.tar.gz file2.tar.gz > file.tar.gz which I suppose is not going to work if I create 2 different tar files, since it would recreate the header in all of the them, right? So or I give also a script to reassemble everything or I have to split in a more "brutal" way.. Maybe after all doing the final split was not too bad, I'll first check if it's actually more expensive for the filesystem (which is very very slow) or it's not a big deal... From andrew3 at r3dsolutions.com Wed Nov 7 17:02:22 2012 From: andrew3 at r3dsolutions.com (Andrew Robinson) Date: Wed, 07 Nov 2012 14:02:22 -0800 Subject: Multi-dimensional list initialization In-Reply-To: <5099bf7d$0$29980$c3e8da3$5496439d@news.astraweb.com> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <5099bf7d$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: <509ADA6E.10403@r3dsolutions.com> On 11/06/2012 05:55 PM, Steven D'Aprano wrote: > On Tue, 06 Nov 2012 14:41:24 -0800, Andrew Robinson wrote: > >> Yes. But this isn't going to cost any more time than figuring out >> whether or not the list multiplication is going to cause quirks, itself. >> Human psychology *tends* (it's a FAQ!) to automatically assume the >> purpose of the list multiplication is to pre-allocate memory for the >> equivalent (using lists) of a multi-dimensional array. Note the OP even >> said "4d array". > I'm not entirely sure what your point is here. The OP screwed up -- he > didn't generate a 4-dimensional array. He generated a 2-dimensional > array. If his intuition about the number of dimensions is so poor, why > should his intuition about list multiplication be treated as sacrosanct? Yes he did screw up. There is a great deal of value in studying how people screw up, and designing interfaces which tend to discourage it. "Candy machine interfaces". > As they say, the only truly intuitive interface is the nipple. No it's not -- that interface really sucks. :) Have you ever seen a cat trying to suck a human nipple -- ? Or, have you ever asked a young child who was weaned early and doesn't remember nursing -- what a breast is for ? Once the oral stage is left, remaining behavior must be re-learned. > There are > many places where people's intuition about programming fail. And many > places where Fred's intuition is the opposite of Barney's intuition. OK. But that doesn't mean that *all* places have opposite intuition; Nor does it mean that one intuition which is statistically *always* wrong shouldn't be discouraged, or re-routed into useful behavior. Take the candy machine, if the items being sold are listed by number -- and the prices are also numbers; it's very easy to type in the price instead of the object number because one *forgets* that the numbers have different meaning and the machine can't always tell from the price, which object a person wanted (duplicate prices...); Hence a common mistake... people get the wrong item, by typing in the price. By merely avoiding a numeric keypad -- the user is re-routed into choosing the correct item by not being able to make the mistake. For this reason, Python tends to *like* things such as named parameters and occasionally enforces their use. etc. > Even more exciting, there are places where people's intuition is > *inconsistent*, where they expect a line of code to behave differently > depending on their intention, rather than on the code. And intuition is > often sub-optimal: e.g. isn't it intuitively obvious that "42" + 1 should > give 43? (Unless it is intuitively obvious that it should give 421.) I agree, and in places where an *exception* can be raised; it's appropriate to do so. Ambiguity, like the candy machine, is *bad*. > So while I prefer intuitively obvious behaviour where possible, it is not > the holy grail, and I am quite happy to give it up. "where possible"; OK, fine -- I agree. I'm not "happy" to give it up; but I am willing. I don't like the man hours wasted on ambiguous behavior; and I don't ever think that should make someone "happy". >> The OP's original construction was simple, elegant, easy to read and >> very commonly done by newbies learning the language because it's >> *intuitive*. His second try was still intuitive, but less easy to read, >> and not as elegant. > Yes. And list multiplication is one of those areas where intuition is > suboptimal -- it produces a worse outcome overall, even if one minor use- > case gets a better outcome. > > I'm not disputing that [[0]*n]*m is intuitively obvious and easy. I'm > disputing that this matters. Python would be worse off if list > multiplication behaved intuitively. How would it be worse off? I can agree, for example, that in "C" -- realloc -- is too general. One can't look at the line where realloc is being used, and decide if it is: 1) mallocing 2) deleting 3) resizing Number (3) is the only non-redundant behavior the function provides. There is, perhaps, a very clear reason that I haven't discovered why the extra functionality in list multiplication would be bad; That reason is *not* because list multiplication is unable to solve all the copying problems in the word; (realloc is bad, precisely because of that); But a function ought to do at least *one* thing well. Draw up some use cases for the multiplication operator (I'm calling on your experience, let's not trust mine, right?); What are all the Typical ways people *Do* to use it now? If those use cases do not *primarily* center around *wanting* an effect explicitly caused by reference duplication -- then it may be better to abolish list multiplication all together; and rather, improve the list comprehensions to overcome the memory, clarity, and speed pitfalls in the most common case of initializing a list. For example, in initialization use cases; often the variable of a for loop isn't needed and all the initializers have parameters which only need to be evaluated *once* (no side effects). Hence, there is an opportunity for speed and memory gains,while maintaining clarity and *consistency*. Some ideas of use cases: [ (0) in xrange(10) ] # The function to create a tuple cache's the parameter '0', makes 10 (0)'s [ dict.__new__(dict) in xrange(10) ] # dict.__new__, The dict parameter is cached -- makes 10 dicts. [ lambda x:(0) in xrange(10) ] # lambda caches (0), returns a *reference* to it multiple times. > An analogy: the intuitively obvious thing to do with a screw is to bang > it in with a hammer. It's long, thin, has a point at the end, and a flat > head that just screams "hit me". But if you do the intuitive thing, your > carpentry will be *much worse* than the alternatives. :) I agree. Good point and Good "thin point". > Having list multiplication copy has consequences beyond 2D arrays. Those > consequences make the intuitive behaviour you are requesting a negative > rather than a positive. If that means that newbie programmers have to > learn not to hammer screws in, so be it. It might be harder, slower, and > less elegant to drill a pilot hole and then screw the screw in, but the > overall result is better. no, the overall result is still bad. If the answer is *don't* hammer nails, then it's better to raise an exception when it's tried. There's no way to do that with list multiplication. >>> * Consistency of semantics is better than a plethora of special >>> cases. Python has a very simple and useful rule: objects should not >>> be copied unless explicitly requested to be copied. This is much >>> better than having to remember whether this operation or that >>> operation makes a copy. The answer is consistent: >> Bull. Even in the last thread I noted the range() object produces >> special cases. >> >>> range(0,5)[1] >> 1 >> >>> range(0,5)[1:3] >> range(1, 3) > What's the special case here? What do you think is copied? > > > You take a slice of a range object, you get a new range object. You were'nt paying attention, OCCASIONALLY, get an integer, or a list. >>> range(3)[2] 2 LOOOOK! That's not a range object, that's an integer. Use Python 3.2 and try it. > I'm honestly not getting what you think is inconsistent about this. How about now? > Two-dimensional arrays in Python using lists are quite rare. Anyone > who is doing serious numeric work where they need 2D arrays is using > numpy, not lists. Game programmers routinely use 2D lists to represent the screen layout; For example, they might use 'b' to represent a brick tile, and 'w' to represent a water tile. This is quite common in simple games; I have seen several use 2D lists (or tuples) to do this. Serious numeric work is not needed in most simple games; especially if motion is not involved. There are *many* non serious uses of matrix mathematics and 2D lists. Numpy isn't desired even if it would work. Cost benefit analysis.... Crossword puzzles, periodic table of the elements with different sources of weights listed under each element, etc. (That can also be done with a dict, but I've seen an implementation do it the other way.) etc. > There are millions of people using Python, so it's hardly surprising > that once or twice a year some newbie trips over this. But it's not > something that people tend to trip over again and again and again, > like C's "assignment is an expression" misfeature. Good point. I don't have a statistic -- except the handful of times I searched for some other topic -- and I have seen it three times already.... >> I read some of the documentation on why Python 3 chose to implement it >> this way. > What documentation is this? The documentation for range() -- which I just studied read because of another thread we both were in. You're misconstruing the subject -- which was "inconsistency" of Python is allowed; not "is list multiplication inconsistent." >> Q: What about [[]]*10? >> A: No, the elements are never copied. >> >> YES! For the obvious reason that such a construction is making mutable >> lists that the user wants to populate later. If they *didn't* want to >> populate them later, they ought to have used tuples -- which take less >> overhead. Who even does this thing you are suggesting?! > Who knows? Who cares? Nobody does: exactly !!!! But I do care, even though I don't do it (because it doesn't *work*) > > n -= n > > instead of just n=0, but that doesn't mean that we should give it some > sort of special meaning different from n -= m. If it turns out that the > definition of list multiplication is such that NOBODY, EVER, uses [[]]*n, > that is *still* not a good reason for special-casing it. Ahh... but some people *DO* try to use it for another purpose. Your example is a bad analogy. > > Special cases aren't special enough to break the rules. finish the sentence: ALTHOUGH practicality beats purity. > There are perfectly good ways to generate a 2D array out of lists, and > even better reasons not to use lists for that in the first place. (Numpy > arrays are much better suited for serious work.) Duh... I answered that.... > I'm afraid you've just lost an awful lot of credibility there. > > py> x = [{}]*5 > py> x > [{}, {}, {}, {}, {}] No, I showed what happed when you do {}*3; That *DOESN'T* work; You aren't multiplying the dictionary, you are multiplying the LIST of dictionaries. Very different things. You were complaining that my method doesn't multiply them -- well, gee -- either mine DOES or python DOESN'T. Double standards are *crap*. > py> x[0]['key'] = 1 > py> x > [{'key': 1}, {'key': 1}, {'key': 1}, {'key': 1}, {'key': 1}] > > And similarly for any other mutable object. > > If you don't understand that lists can contain other mutable objects > apart from lists, then you really shouldn't be discussing this issue. I do; that's why I DEMONSTRATED this issue in my own replies. > >>> Your proposal throws away consistency for a trivial benefit on a rare >>> use- case, and replaces it with a bunch of special cases: >> RARE!!!! You are NUTS!!!! > Yes, rare. I base that on about 15 years of Python coding and many > thousands (tens of thousands?) of hours on Python forums like this one. > What's your opinion based on? Which opinion? 2D lists are NOT rare; I've seen them in dozens of python programs not written by me. As to my other opinion regarding why change it, there are two separate issues: One was that a poster asked if it would be difficult to do without introducing bugs; That's the question I answered affirmatively. The _OP_ problem can be removed using a simple fix which isn't going to break any major number of programs. That's a fact by your admission as well. The second issue is would it be consistent to make the change (and NOTE I wasn't asked that, and wasn't answering that) You and others brought it up tangentially. I agree, There is a problem, as I noted, with subclassing. I also will note that ([],[],[]) is effectively the same question as sub-classing -- () are merely lists that are immutable. As to how often people make the mistake -- There's a big difference between how often someone makes the mistake, and how often it shows up on a forum. The issue shows up in a forum when someone can't figure out what they did wrong after a long debugging session. There is more than one way to resolve such an issue, even if a person doesn't know why their construction is wrong. If it is easy to see the construction produces the wrong result, one can simply try list comprehensions which will work correctly. Then, either the person learns why they made the mistake -- or they don't. If they are able to get it to work sometimes, but sometimes they can't -- they may or not stop using it. I cite Java programming where the API is notorious for that kind of inconsistent behavior; Yet Java programmers feel compelled to still use those constructions. etc. > values = [None]*n # or 0 is another popular starting value > > Using it twice to generate a 2D array is even rarer. Sure, and if it is used that way -- I doubt it is ever used like [ {} ]*n, because that object will have a side effect. SO again, if this is the *main* use case, the default behavior is not the main reason they use it -- but they can often work around the default behavior by using it with specially thought about data. > >>> Q: How about if I use delegation to proxy a list? A: Oh no, they >>> definitely won't be copied. >> Give an example usage of why someone would want to do this. Then we can >> discuss it. > Proxying objects is hardly a rare scenario. Delegation is less common > since you can subclass built-ins, but it is still used. It is a standard > design pattern. I was not judging you; I was asking for an example to discuss. > Python is a twenty year old language. Do you really think this is the > first time somebody has noticed it? It's hard to search for > discussions on the dev list, because the obvious search terms bring up > many false positives. No, I don't think it's the first time. > But here are a couple of bug reports closed as "won't fix": > http://bugs.python.org/issue1408 http://bugs.python.org/issue12597 I > suspect it is long past time for a PEP so this can be rejected once > and for all. Yeah, that'd be good -- and perhaps they'd abolish it. :) >> The copy speed will be the same or *faster*, and the typing less -- and >> the psychological mistakes *less*, the elegance more. > You think that it is *faster* to copy a list than to make a new pointer > to it? Your credibility is not looking too good here. YES -- WHEN copying by reference is a BUG, then copying is NOT by reference. That's the use case I spelled out. You're changing the subject to make me look dumb? or being purposely facile to hide being destroyed on the substance of the argument ? When true copying is desired, then doing it at the "C" level is better than the interpreter Level. ergo: You're not looking too intelligent either. >> It's hardly going to confuse anyone to say that lists are copied with >> list multiplication, but the elements are not. > Well, that confuses me. What about a list where the elements are lists? > Are they copied? YES! It's a LIST copy; the all lists, and only the lists, are copied; the rest are referenced. > What about other mutable objects? Are they copied? No. It's a list copy, not random mutable object copy. > What about mutable objects which are uncopyable, like file objects? No. It's a list copy, not a file copy. > >> Every time someone passes a list to a function, they *know* that the >> list is passed by value -- and the elements are passed by reference. > And there goes the last of your credibility. *You* might "know" this, but > that doesn't make it so. No, it's not gone. You saying so, doesn't make it gone. People aren't ignorant of the passing mechanism just because they didn't transfer from another language like "C", or "Pascal", "ADA", "Fortran", etc. But when they do transfer from a language which makes a distinction -- then, yes, it's weird. > Python's calling behaviour is identical to that used by languages > including Java (excluding unboxed primitives) and Ruby, to mention > only two. You're starting to shout and yell, so perhaps it's best if I > finish this here. Huh? I'm not yelling any more than you are. Are ???YOU??? yelling? :-\ -------------- next part -------------- An HTML attachment was scrubbed... URL: From smaran.harihar at gmail.com Wed Nov 7 17:11:30 2012 From: smaran.harihar at gmail.com (Smaran Harihar) Date: Wed, 7 Nov 2012 15:11:30 -0700 Subject: Read number of CSV files Message-ID: Hi Guys, I am able to read through a CSV File and fetch the data inside the CSV file but I have a really big list of CSV files and I wish to do the same particular code in all the CSV files. Is there some way that I can loops through all these files, which are in a single folder, and get my code to read the files? -- Thanks & Regards Smaran Harihar -------------- next part -------------- An HTML attachment was scrubbed... URL: From joshua.landau.ws at gmail.com Wed Nov 7 17:16:23 2012 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 7 Nov 2012 22:16:23 +0000 Subject: Multi-dimensional list initialization In-Reply-To: References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098A55C.3090201@r3dsolutions.com> <5098C873.2000200@r3dsolutions.com> <509982CA.7050701@r3dsolutions.com> Message-ID: *Spoiler:* You've convinced me. On 7 November 2012 14:00, Oscar Benjamin wrote: > On 7 November 2012 13:39, Joshua Landau > wrote: > > On 7 November 2012 11:11, Oscar Benjamin > wrote: > >> A more modest addition for the limited case described in this thread > could > >> be to use exponentiation: > >> > >> >>> [0] ** (2, 3) > >> [[0, 0, 0], [0, 0, 0]] > > > > Hold on: why not just use multiplication? > > > >>>> [0] * (2, 3) > > > > is an error now, and it makes total sense. Additionally, it's not > breaking > > the "no copy -- _ever_" rule because none of the lists existed before. > The > > values inside the list would be by reference, as before, so lst * (x,) > would > > be the same as lst * x if x is an integer. > > The problem is that this operation is asymmetric. Currently int/list > multiplication is commutative so that: > > ['a', 'b'] * 2 == 2 * ['a', 'b'] > I see. I agree that that is a valid point. Remember, though, that we could just keep this behaviour: [0] * (2, 3) == (2, 3) * [0] > If you use this kind of multiplication what happens to the other > cases? e.g. what do you give for: > > >>> [0] * [2, 3] > Nothing. If you allowed lists to multiply this time, why not with your suggestion? We should require a tuple and a list. > >>> [2, 3] * [0] > Same. > >>> (2, 3) * [0] > == [0] * (2, 3) > >>> (2, 3) * (4, 5) > Nothing. > and so on. Although Python does not guarantee commutativity of > multiplication in general I think that since for lists it has always > been commutative it would be bad to change that. > Agreed > Exponentiation is expected to be asymmetric and is currently unused so > there is no ambiguity. The problem is if someone has already > subclassed list and added an exponentiation method. How is that a problem? They just wont get the functionality. That said, losing: [0] * (2, 3) == [0] * [2, 3] would mean losing duck-typing in general. *Thus*, I fully agree with your choice of exponentiation. -------------- next part -------------- An HTML attachment was scrubbed... URL: From aschneiderman at asha.org Wed Nov 7 17:17:42 2012 From: aschneiderman at asha.org (Anders) Date: Wed, 7 Nov 2012 14:17:42 -0800 (PST) Subject: Right solution to unicode error? Message-ID: <09a3d20b-5871-47f4-9218-df119698e405@m4g2000yqf.googlegroups.com> I've run into a Unicode error, and despite doing some googling, I can't figure out the right way to fix it. I have a Python 2.6 script that reads my Outlook 2010 task list. I'm able to read the tasks from Outlook and store them as a list of objects without a hitch. But when I try to print the tasks' subjects, one of the tasks is generating an error: Traceback (most recent call last): File "outlook_tasks.py", line 66, in my_tasks.dump_today_tasks() File "C:\Users\Anders\code\Task List\tasks.py", line 29, in dump_today_tasks print task.subject UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position 42: ordinal not in range(128) (where task.subject was previously assigned the value of task.Subject, aka the Subject property of an Outlook 2010 TaskItem) >From what I understand from reading online, the error is telling me that the subject line contains an en dash and that Python is trying to convert to ascii and failing (as it should). Here's where I'm getting stuck. In the code above I was just printing the subject so I can see whether the script is working properly. Ultimately what I want to do is parse the tasks I'm interested in and then create an HTML file containing those tasks. Given that, what's the best way to fix this problem? BTW, if there's a clear description of the best solution for this particular problem ? i.e., where I want to ultimately display the results as HTML ? please feel free to refer me to the link. I tried reading a number of docs on the web but still feel pretty lost. Thanks, Anders From andrew3 at r3dsolutions.com Wed Nov 7 17:22:35 2012 From: andrew3 at r3dsolutions.com (Andrew Robinson) Date: Wed, 07 Nov 2012 14:22:35 -0800 Subject: Multi-dimensional list initialization In-Reply-To: <98C863CB-F92A-4997-86E3-85F2F909B3EA@gmail.com> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <5099bf7d$0$29980$c3e8da3$5496439d@news.astraweb.com> <98C863CB-F92A-4997-86E3-85F2F909B3EA@gmail.com> Message-ID: <509ADF2B.8020908@r3dsolutions.com> On 11/06/2012 10:56 PM, Demian Brecht wrote: > My question was *not* based on what I perceive to be intuitive > (although most of this thread has now seemed to devolve into that and > become more of a philosophical debate), but was based on what I > thought may have been inconsistent behaviour (which was quickly > cleared up with None being immutable and causing it to *seem* that the > behaviour was inconsistent to the forgetful mind). I originally brought up "intuitive"; and I don't consider the word to mean an "exclusive" BEST way -- I meant it to mean easily guessed or understood. An intelligent person can see when there may be more than one reasonable explanation -- ergo: I just called your OP intelligent, even if you were wrong; and D'Aprano ripped you for being wrong. The debate is degenerating because people are _subjectively_ judging other people's intelligence. The less intelligent a person is, the more black and white their judgements _tend_ to be. > As you touch on here, "intuition" is entirely subjective. If you're > coming from a C/C++ background, I'd think that your intuition would be > that everything's passed by value unless explicitly stated. Yup -- that's my achillies heel and bias, I'm afraid. I learned basic, then assembly, and then pascal, and then fortran77 with C (historically in that order) In my view, pass by value vs. reference always exists at the hardware/CPU level regarless of the language; and regardless of whether the language hides the implementation details or not; I'm an EE; I took software engineering to understand the clients who use my hardware, and to make my hardware drivers understandable to them by good programming practices. An EE's perspective often lead to doing efficient things which are hard to understand; That's why I look for a consensus (not a compromise) before implementing speed/memory improvements and ways to clarify what is being done. > Someone coming from another background (Lua perhaps?) would likely > have entirely different intuition. Yes, they might be ignorant of what LUA is doing at the hardware level; even though it *is* doing it. >> So while I prefer intuitively obvious behaviour where possible, it is not >> the holy grail, and I am quite happy to give it up. > I fail to see where there has been any giving up on intuitiveness in the context of this particular topic. In my mind, intuitiveness is generally born of repetitiveness and consistency. YES!!!! I think a good synonym would be habit; and when a habit is good -- it's called strength, or "virtue"; When it's bad it's called "vice" or "sin" or "bad programming habit." :) Virtues don't waste people's time in debugging. > As everything in Python is a reference, it would seem to me to be inconsistent to treat expressions such as [[obj]*4]*4 un-semantically (Pythonically speaking) and making it *less* intuitive. I agree that Python would definitely be worse off. That's a fair opinion. I was pleasantly surprised when the third poster actually answered the "WHY" question with the idea that Python always copies by reference unless forced to do deep copy. That's intuitive, and as a habit (not a requirement) Python implements things that way. I've already raised the question about why one would want a multiplier at all, if it were found that the main desired use case never *wants* all objects to change together. I laid out a potential modification of list comprensions; which, BTW, copy by re-instantiating rather than reference; so the paradigm of Python is wrong in that case.... But, I think the modifications in that context can't be argued against as easily as list multiplication (For the same reason that comprehensions already break the copy by reference mold !!!! ) -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Wed Nov 7 17:27:26 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 07 Nov 2012 22:27:26 +0000 Subject: Multi-dimensional list initialization In-Reply-To: <509ADA6E.10403@r3dsolutions.com> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <5099bf7d$0$29980$c3e8da3$5496439d@news.astraweb.com> <509ADA6E.10403@r3dsolutions.com> Message-ID: On 07/11/2012 22:02, Andrew Robinson wrote: > You're doing extremely well, you've overtaken Xah Lee as the biggest waste of space on this list. -- Cheers. Mark Lawrence. From sadzak at gmail.com Wed Nov 7 17:30:20 2012 From: sadzak at gmail.com (Adnan Sadzak) Date: Wed, 7 Nov 2012 23:30:20 +0100 Subject: Read number of CSV files In-Reply-To: References: Message-ID: Maybe os.listdir() can help You, and then go through files and do whatever You want. On Wed, Nov 7, 2012 at 11:11 PM, Smaran Harihar wrote: > Hi Guys, > > I am able to read through a CSV File and fetch the data inside the CSV > file but I have a really big list of CSV files and I wish to do the same > particular code in all the CSV files. > > Is there some way that I can loops through all these files, which are in a > single folder, and get my code to read the files? > > -- > Thanks & Regards > Smaran Harihar > > > -- > http://mail.python.org/mailman/listinfo/python-list > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Wed Nov 7 17:36:53 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 07 Nov 2012 22:36:53 GMT Subject: Multi-dimensional list initialization References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <50999214.50100@r3dsolutions.com> <5099ec1d$0$21759$c3e8da3$76491128@news.astraweb.com> Message-ID: <509ae285$0$29980$c3e8da3$5496439d@news.astraweb.com> On Wed, 07 Nov 2012 17:17:02 +0000, MRAB wrote: > The disadvantage of calling it "call by ..." is that it suggests that > you're just talking about calling functions. *shrug* There are already two synonyms for this, "call by ..." and "pass by ...". They are old, venerable terms dating back to Algol and possibly even older. All the way back to Fortran perhaps? > What about binding in general, eg "x = y"? Does it make sense to still > call it "call by ..."? Sure, why not? The person who prepares beef tartare or sushimi is still called the cook. -- Steven From oscar.j.benjamin at gmail.com Wed Nov 7 18:06:27 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Wed, 7 Nov 2012 23:06:27 +0000 Subject: Multi-dimensional list initialization In-Reply-To: References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098A55C.3090201@r3dsolutions.com> <5098C873.2000200@r3dsolutions.com> <509982CA.7050701@r3dsolutions.com> Message-ID: On 7 November 2012 22:16, Joshua Landau wrote: > On 7 November 2012 14:00, Oscar Benjamin wrote: >> On 7 November 2012 13:39, Joshua Landau >> wrote: >> > On 7 November 2012 11:11, Oscar Benjamin >> > wrote: >> >> A more modest addition for the limited case described in this thread >> >> could >> >> be to use exponentiation: >> >> >> >> >>> [0] ** (2, 3) >> >> [[0, 0, 0], [0, 0, 0]] >> >> Exponentiation is expected to be asymmetric and is currently unused so >> there is no ambiguity. The problem is if someone has already >> subclassed list and added an exponentiation method. > > How is that a problem? They just wont get the functionality. This is absolutely contrived but: Library A defines a subclass of list that adds an exponentiation operator thinking that it's okay to still use these objects as lists. Library B has an API that expects a list and tries to use the list copy-exponentiation on its input. A user passes a list type object from library A into library B and hopefully gets an error but possibly gets a subtle bug that is hard to track down. It doesn't sound plausible to me but at least in principle there is a backward compatibility problem. > That said, losing: > [0] * (2, 3) == [0] * [2, 3] > would mean losing duck-typing in general. *Thus*, I fully agree with your > choice of exponentiation. Also there's no reason why tuples couldn't have the same exponentiation operator (although for them it would be no different from repeated multiplication). Oscar From ramit.prasad at jpmorgan.com Wed Nov 7 18:07:33 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Wed, 7 Nov 2012 23:07:33 +0000 Subject: Right solution to unicode error? In-Reply-To: <09a3d20b-5871-47f4-9218-df119698e405@m4g2000yqf.googlegroups.com> References: <09a3d20b-5871-47f4-9218-df119698e405@m4g2000yqf.googlegroups.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF474167873EF@SCACMX008.exchad.jpmchase.net> Anders wrote: > > I've run into a Unicode error, and despite doing some googling, I > can't figure out the right way to fix it. I have a Python 2.6 script > that reads my Outlook 2010 task list. I'm able to read the tasks from > Outlook and store them as a list of objects without a hitch. But when > I try to print the tasks' subjects, one of the tasks is generating an > error: > > Traceback (most recent call last): > File "outlook_tasks.py", line 66, in > my_tasks.dump_today_tasks() > File "C:\Users\Anders\code\Task List\tasks.py", line 29, in > dump_today_tasks > print task.subject > UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in > position 42: ordinal not in range(128) > > (where task.subject was previously assigned the value of > task.Subject, aka the Subject property of an Outlook 2010 TaskItem) > > From what I understand from reading online, the error is telling me > that the subject line contains an en dash and that Python is trying > to convert to ascii and failing (as it should). > > Here's where I'm getting stuck. In the code above I was just printing > the subject so I can see whether the script is working properly. > Ultimately what I want to do is parse the tasks I'm interested in and > then create an HTML file containing those tasks. Given that, what's > the best way to fix this problem? > > BTW, if there's a clear description of the best solution for this > particular problem - i.e., where I want to ultimately display the > results as HTML - please feel free to refer me to the link. I tried > reading a number of docs on the web but still feel pretty lost. > You can always encode in a non-ASCII codec. `print task.subject.encode()` where is something that supports the characters you want e.g. latin1. The list of built in codecs can be found: http://docs.python.org/library/codecs.html#standard-encodings ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From rosuav at gmail.com Wed Nov 7 18:14:35 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 8 Nov 2012 10:14:35 +1100 Subject: [Python-ideas] sys.py3k In-Reply-To: References: <5096ED46.20502@pearwood.info> <20121105063008.GA14836@ando> Message-ID: On Thu, Nov 8, 2012 at 5:35 AM, anatoly techtonik wrote: > I thought of sys.py3k check as an explicit way to guard the code that should > be maintained extra carefully for Python 3 compatibility, so that you can > grep the source for this constant and remove all the hacks (such as bytes to > string conversion) required to maintain the compatibility when the time > comes to switch. I agree about greppability, it's a huge help. Hence the code comment; as long as you're consistent and you pick a keyword long enough or unusual enough to not occur anywhere else, you can easily do a "find across files" or "grep XYZ *" to find them all. And if you put the comment on the most significant line of code, line-based tools will be more useful. # Unideal: # py3k try: reload except NameError: from imp import reload # Better: try: # py3k reload except NameError: from imp import reload # Best: try: reload # py3k except NameError: from imp import reload # Also best: try: reload except NameError: from imp import reload # py3k Taking just the line with the keyword "py3k" on it will tell you exactly what that file is doing. ChrisA From oscar.j.benjamin at gmail.com Wed Nov 7 18:15:14 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Wed, 7 Nov 2012 23:15:14 +0000 Subject: creating size-limited tar files In-Reply-To: <509AD812.2060605@gmail.com> References: <509ab0fa$0$6636$9b4e6d93@newsspool2.arcor-online.net> <509AD812.2060605@gmail.com> Message-ID: On 7 November 2012 21:52, Andrea Crotti wrote: > On 11/07/2012 08:32 PM, Roy Smith wrote: >> >> In article <509ab0fa$0$6636$9b4e6d93 at newsspool2.arcor-online.net>, >> Alexander Blinne wrote: >> >>> I don't know the best way to find the current size, I only have a >>> general remark. >>> This solution is not so good if you have to impose a hard limit on the >>> resulting file size. You could end up having a tar file of size "limit + >>> size of biggest file - 1 + overhead" in the worst case if the tar is at >>> limit - 1 and the next file is the biggest file. Of course that may be >>> acceptable in many cases or it may be acceptable to do something about >>> it by adjusting the limit. > > But the other problem is that at the moment the people that get our chunks > reassemble the file with a simple: > > cat file1.tar.gz file2.tar.gz > file.tar.gz > > which I suppose is not going to work if I create 2 different tar files, > since it would recreate the header in all of the them, right? Correct. But if you read the rest of Alexander's post you'll find a suggestion that would work in this case and that can guarantee to give files of the desired size. You just need to define your own class that implements a write() method and then distributes any data it receives to separate files. You can then pass this as the fileobj argument to the tarfile.open function: http://docs.python.org/2/library/tarfile.html#tarfile.open Oscar From smaran.harihar at gmail.com Wed Nov 7 18:17:22 2012 From: smaran.harihar at gmail.com (Smaran Harihar) Date: Wed, 7 Nov 2012 16:17:22 -0700 Subject: Invalid syntax Message-ID: Hi guys, I am stuck in one of those non identifiable error location in the code. The code keeps giving invalid syntax. This is my code . I am using the same code for another code and not sure why this is not working. This is the traceback that I am getting. Any idea where am I going wrong? -- Thanks & Regards Smaran Harihar -------------- next part -------------- An HTML attachment was scrubbed... URL: From demianbrecht at gmail.com Wed Nov 7 18:22:29 2012 From: demianbrecht at gmail.com (Demian Brecht) Date: Wed, 7 Nov 2012 15:22:29 -0800 Subject: Invalid syntax In-Reply-To: References: Message-ID: <220983A5-4028-46E6-999C-3D211F0B8B4C@gmail.com> On 2012-11-07, at 3:17 PM, Smaran Harihar wrote: > Any idea where am I going wrong? Looks like you're missing a closing parenthesis: w.record(collection[i][0], MAT[0], TSD[0], AnnTMin[0], ANNPREC[0], float(collection[i][2]), float(collection[i][1]) should be w.record(collection[i][0], MAT[0], TSD[0], AnnTMin[0], ANNPREC[0], float(collection[i][2]), float(collection[i][1])) Demian Brecht @demianbrecht http://demianbrecht.github.com From michael.weylandt at gmail.com Wed Nov 7 18:22:44 2012 From: michael.weylandt at gmail.com (R. Michael Weylandt) Date: Wed, 7 Nov 2012 23:22:44 +0000 Subject: Invalid syntax In-Reply-To: References: Message-ID: On Wed, Nov 7, 2012 at 11:17 PM, Smaran Harihar wrote: > Hi guys, > > I am stuck in one of those non identifiable error location in the code. The > code keeps giving invalid syntax. This is my code. > > I am using the same code for another code and not sure why this is not > working. This is the traceback that I am getting. > > Any idea where am I going wrong? If the dpaste link is correct, you're missing a close-parenthesis at the end of the w.record line: w.record(collection[i][0], MAT[0], TSD[0], AnnTMin[0], ANNPREC[0], float(collection[i][2]), float(collection[i][1]) ## <<--- Hereabouts Cheers, Michael From oscar.j.benjamin at gmail.com Wed Nov 7 18:27:17 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Wed, 7 Nov 2012 23:27:17 +0000 Subject: Right solution to unicode error? In-Reply-To: <09a3d20b-5871-47f4-9218-df119698e405@m4g2000yqf.googlegroups.com> References: <09a3d20b-5871-47f4-9218-df119698e405@m4g2000yqf.googlegroups.com> Message-ID: On 7 November 2012 22:17, Anders wrote: > > Traceback (most recent call last): > File "outlook_tasks.py", line 66, in > my_tasks.dump_today_tasks() > File "C:\Users\Anders\code\Task List\tasks.py", line 29, in > dump_today_tasks > print task.subject > UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in > position 42: ordinal not in range(128) > > Here's where I'm getting stuck. In the code above I was just printing > the subject so I can see whether the script is working properly. > Ultimately what I want to do is parse the tasks I'm interested in and > then create an HTML file containing those tasks. Given that, what's > the best way to fix this problem? Are you using cmd.exe (standard Windows terminal)? If so, it does not support unicode and Python is telling you that it cannot encode the string in a way that can be understood by your terminal. You can try using chcp to set the code page to something that works with your script. If you are only printing it for debugging purposes you can just print the repr() of the string which will be ascii and will come out fine in your terminal. If you want to write it to a html file you should encode the string with whatever encoding (probably utf-8) you use in the html file. If you really just want your script to be able to print unicode characters then you need to use something other than cmd.exe (such as IDLE). Oscar From roy at panix.com Wed Nov 7 18:31:10 2012 From: roy at panix.com (Roy Smith) Date: Wed, 07 Nov 2012 18:31:10 -0500 Subject: Invalid syntax References: Message-ID: In article , Demian Brecht wrote: > On 2012-11-07, at 3:17 PM, Smaran Harihar wrote: > > Any idea where am I going wrong? > > Looks like you're missing a closing parenthesis: What I find is useful in situations like this is to just let emacs auto-indent the code. When it starts indenting (or coloring) differently than I would have expected, that's a clue that I've messed up some punctuation somewhere. This doesn't only work in emacs. Vim, eclipse, etc, all have similar syntax-coloring and auto-indenting features. The same idea works with any of them. From ian.g.kelly at gmail.com Wed Nov 7 18:39:55 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 7 Nov 2012 16:39:55 -0700 Subject: Multi-dimensional list initialization In-Reply-To: <509ADA6E.10403@r3dsolutions.com> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <5099bf7d$0$29980$c3e8da3$5496439d@news.astraweb.com> <509ADA6E.10403@r3dsolutions.com> Message-ID: On Wed, Nov 7, 2012 at 3:02 PM, Andrew Robinson wrote: > Draw up some use cases for the multiplication operator (I'm calling on your > experience, let's not trust mine, right?); What are all the Typical ways > people *Do* to use it now? > > If those use cases do not *primarily* center around *wanting* an effect > explicitly caused by reference duplication -- then it may be better to > abolish list multiplication all together; and rather, improve the list > comprehensions to overcome the memory, clarity, and speed pitfalls in the > most common case of initializing a list. Why? Just to get rid of an FAQ? Here's one of the more interesting uses from my own code: values = zip(samples, times * num_groups) if len(values) < len(times) * num_groups: # raise an error Converting that multiplication to a generator expression would look like this: values = zip(samples, (t for _ in range(num_groups) for t in times)) That's not particularly hairy, but I do assert that it is substantially less readable, and more so because it loses the symmetry with the following if condition. The recipes in the itertools docs also include this example, which notably depends on the list containing multiple references to the same iterator: def grouper(n, iterable, fillvalue=None): "Collect data into fixed-length chunks or blocks" # grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx args = [iter(iterable)] * n return izip_longest(fillvalue=fillvalue, *args) Replacing the list multiplication in that function with a list comprehension would be awkward, as the obvious replacement of [iter(iterable) for _ in range(n)] would produce different results. > For example, in initialization use cases; often the variable of a for loop > isn't needed and all the initializers have parameters which only need to be > evaluated *once* (no side effects). > > Hence, there is an opportunity for speed and memory gains,while maintaining > clarity and *consistency*. > > Some ideas of use cases: > [ (0) in xrange(10) ] # The function to create a tuple cache's the > parameter '0', makes 10 (0)'s > [ dict.__new__(dict) in xrange(10) ] # dict.__new__, The dict parameter is > cached -- makes 10 dicts. > [ lambda x:(0) in xrange(10) ] # lambda caches (0), returns a *reference* to > it multiple times. How exactly do you propose to indicate to the compiler which parts of the expressions are meant to be cached, and which are not? >>> Bull. Even in the last thread I noted the range() object produces >>> special cases. >>> >>> range(0,5)[1] >>> 1 >>> >>> range(0,5)[1:3] >>> range(1, 3) >> >> What's the special case here? What do you think is copied? >> >> >> You take a slice of a range object, you get a new range object. > > You were'nt paying attention, OCCASIONALLY, get an integer, or a list. >>>> range(3)[2] > 2 > > LOOOOK! That's not a range object, that's an integer. Use Python 3.2 and > try it. Of course you got an integer. You took an index of the range object, not a slice. The rule is that taking an index of a sequence returns an element; taking a slice of a sequence returns a sub-sequence. You still have not shown any inconsistency here. > Game programmers routinely use 2D lists to represent the screen layout; > For example, they might use 'b' to represent a brick tile, and 'w' to > represent a water tile. In many cases it may be simpler to use a plain list of strings: screen = [ "sssss", "ssbss", "sbbbs", "bbbbb", ] > py> x = [{}]*5 > py> x > [{}, {}, {}, {}, {}] > > No, I showed what happed when you do {}*3; > That *DOESN'T* work; You aren't multiplying the dictionary, you are > multiplying the LIST of dictionaries. Very different things. > You were complaining that my method doesn't multiply them -- well, gee -- > either mine DOES or python DOESN'T. Double standards are *crap*. No, he wasn't. He was talking about multiplying lists of dicts, and whether the dicts are then copied or not, just like every other Q&A item in that dialogue was concerning whether item X in a list should expect to be copied when the containing list is multiplied. You are the only one talking about applying the multiplication operator to dicts. > Huh? > I'm not yelling any more than you are. Are ???YOU??? yelling? Perhaps you're not aware that on the Internet, TYPING IN ALL CAPS is commonly construed as SHOUTING. From bahamutzero8825 at gmail.com Wed Nov 7 18:51:11 2012 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Wed, 07 Nov 2012 17:51:11 -0600 Subject: Right solution to unicode error? In-Reply-To: References: <09a3d20b-5871-47f4-9218-df119698e405@m4g2000yqf.googlegroups.com> Message-ID: <509AF3EF.8050108@gmail.com> On 2012.11.07 17:27, Oscar Benjamin wrote: > Are you using cmd.exe (standard Windows terminal)? If so, it does not > support unicode Actually, it does. Code page 65001 is UTF-8. I know that doesn't help the OP since Python versions below 3.3 don't support cp65001, but I think it's important to point out that the Windows command line system (it is not unique to cmd) does in fact support Unicode. -- CPython 3.3.0 | Windows NT 6.1.7601.17835 From steve+comp.lang.python at pearwood.info Wed Nov 7 18:53:49 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 07 Nov 2012 23:53:49 GMT Subject: Right solution to unicode error? References: <09a3d20b-5871-47f4-9218-df119698e405@m4g2000yqf.googlegroups.com> Message-ID: <509af48d$0$29980$c3e8da3$5496439d@news.astraweb.com> On Wed, 07 Nov 2012 14:17:42 -0800, Anders wrote: > I've run into a Unicode error, and despite doing some googling, I can't > figure out the right way to fix it. I have a Python 2.6 script that > reads my Outlook 2010 task list. I'm able to read the tasks from Outlook > and store them as a list of objects without a hitch. But when I try to > print the tasks' subjects, one of the tasks is generating an error: > > Traceback (most recent call last): > File "outlook_tasks.py", line 66, in > my_tasks.dump_today_tasks() > File "C:\Users\Anders\code\Task List\tasks.py", line 29, in > dump_today_tasks > print task.subject > UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in > position 42: ordinal not in range(128) This error confuses me. Is that an exact copy and paste of the error, or have you edited it or reconstructed it? Because it seems to me that if task.subject is a unicode string, as it appears to be, calling print on it should succeed: py> s = u'ABC\u2013DEF' py> print s ABC?DEF What does type(task.subject) return? -- Steven From andrew3 at r3dsolutions.com Wed Nov 7 18:55:30 2012 From: andrew3 at r3dsolutions.com (Andrew Robinson) Date: Wed, 07 Nov 2012 15:55:30 -0800 Subject: Multi-dimensional list initialization In-Reply-To: References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098A55C.3090201@r3dsolutions.com> <5098C873.2000200@r3dsolutions.com> <509982CA.7050701@r3dsolutions.com> Message-ID: <509AF4F2.3060406@r3dsolutions.com> On 11/07/2012 05:39 AM, Joshua Landau wrote: > > > On 7 November 2012 11:11, Oscar Benjamin wrote: > > On Nov 7, 2012 5:41 AM, "Gregory Ewing" wrote: > > > > If anything is to be done in this area, it would be better > > as an extension of list comprehensions, e.g. > > > > [[None times 5] times 10] > > > > which would be equivalent to > > > > [[None for _i in xrange(5)] for _j in xrange(10)] > Oscar, I'm really in agreement with you; I think that it's better to group all *special* array/list constructions into a single logical unit which will show up in the same part of the Python documentation. > > A multidimensional list comprehension would be useful even for > people who are using numpy as it's common to use a list > comprehension to initialise a numpy array. > I hadn't paid that much attention; but I think that's true of people using the newer releases of Numpy. A Very interesting point... Thank you for mentioning it. > A more modest addition for the limited case described in this > thread could be to use exponentiation: > > >>> [0] ** (2, 3) > [[0, 0, 0], [0, 0, 0]] > I'm against over using the math operators, for the reason that matrix and vector algebra have meanings mathematicians desire (rightly) to maintain. Numpy users might find matricies overloaded to do these things in the future -- and then it becomes unclear whether an initialization is happening or a mathematical operation. I think it best just not to set up an accident waiting to happen in the first place. > Hold on: why not just use multiplication? > >>> [0] * (2, 3) Would you consider that better than [0].nest(2).nest(3) ? or [0].nest(2,3) ? (I'm against multiplication, but I'm still interested in what you find attractive about it.) > We do have to think of the potential problems, though. There are > definitely some. For one, code that relies on lst * x throwing an > error would break. It may confuse others - although I don't see how. Excellent observation: People relying on an exception, would be in the try: operation. So, since lst * int does not cause an exception; they would need a reason to be concerned that someone passed in a list instead of an integer. Semantically, the same KIND of result happens, lst is in some way duplicated; so if the result is accepted, it likely would work in place of an integer. So, the concern would be where someone wanted to detect the difference between an integer and a list, so as to run some alternate algorithm. Eg, say a vector multiply, or similar operation. The design would want to shadow * and call a method to do the multiply; You'd have a fragment possibly like the following: ... try: ret = map( lambda x: x*rightSide, leftSide ) except TypeError: for i in rightSide: self.__mul__( rightSide, i ) # recursive call to __mul__ ... That's a common technique for type checking dating from earlier releases of Python, where the "type" attribute wasn't available. It also works based on functionality, not specific type -- so objects which "work" alike (subclasses, alternate reinventions of the wheel) also can be handled. -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Wed Nov 7 18:56:35 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 07 Nov 2012 23:56:35 GMT Subject: [Python-ideas] sys.py3k References: <5096ED46.20502@pearwood.info> <20121105063008.GA14836@ando> Message-ID: <509af533$0$29980$c3e8da3$5496439d@news.astraweb.com> On Thu, 08 Nov 2012 10:14:35 +1100, Chris Angelico wrote: > On Thu, Nov 8, 2012 at 5:35 AM, anatoly techtonik > wrote: >> I thought of sys.py3k check ... Chris, you regularly reply to the wrong mailing list, and you've just done it again. This is not python-ideas. -- Steven From steve+comp.lang.python at pearwood.info Wed Nov 7 19:00:50 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 08 Nov 2012 00:00:50 GMT Subject: Multi-dimensional list initialization References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <50999214.50100@r3dsolutions.com> <509ABBD4.7020200@r3dsolutions.com> Message-ID: <509af632$0$29980$c3e8da3$5496439d@news.astraweb.com> Andrew, it appears that your posts are being eaten or rejected by my ISP's news server, because they aren't showing up for me. Possibly a side- effect of your dates being in the distant past? So if you have replied to any of my posts, I haven't seen them. In any case, I wanted to ask a question: On Wed, 07 Nov 2012 14:01:19 -0700, Ian Kelly wrote: > On Wed, Nov 7, 2012 at 12:51 PM, Andrew Robinson > wrote: [...] >> But, in any event: >> Pass by value (not call by value) is a term stretching back 30 years; >> eg: when I learned the meaning of the words. Rewording it as "Call by >> value" is something that happened later, and the nuance is lost on >> those without a very wide programming knowledge *and* age. Every now and again I come across somebody who tries to distinguish between "call by foo" and "pass by foo", but nobody has been able to explain the difference (if any) to me. When you CALL a function, you PASS values to it. Hence the two terms are effectively synonyms, and both refer to the evaluation strategy when binding arguments to parameters. If you believe that is incorrect, can you point me to something explaining the difference? -- Steven From smaran.harihar at gmail.com Wed Nov 7 19:01:21 2012 From: smaran.harihar at gmail.com (Smaran Harihar) Date: Wed, 7 Nov 2012 17:01:21 -0700 Subject: Invalid syntax In-Reply-To: References: Message-ID: Thanks a lot guys. Seriously when u get stuck on such issues it really drives u nuts and that is when this awesome comes to the rescue. This python mailing list rocks. On Wed, Nov 7, 2012 at 4:31 PM, Roy Smith wrote: > In article , > Demian Brecht wrote: > > > On 2012-11-07, at 3:17 PM, Smaran Harihar > wrote: > > > Any idea where am I going wrong? > > > > Looks like you're missing a closing parenthesis: > > What I find is useful in situations like this is to just let emacs > auto-indent the code. When it starts indenting (or coloring) > differently than I would have expected, that's a clue that I've messed > up some punctuation somewhere. > > This doesn't only work in emacs. Vim, eclipse, etc, all have similar > syntax-coloring and auto-indenting features. The same idea works with > any of them. > -- > http://mail.python.org/mailman/listinfo/python-list > -- Thanks & Regards Smaran Harihar -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Wed Nov 7 19:06:03 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 8 Nov 2012 11:06:03 +1100 Subject: [Python-ideas] sys.py3k In-Reply-To: <509af533$0$29980$c3e8da3$5496439d@news.astraweb.com> References: <5096ED46.20502@pearwood.info> <20121105063008.GA14836@ando> <509af533$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Thu, Nov 8, 2012 at 10:56 AM, Steven D'Aprano wrote: > On Thu, 08 Nov 2012 10:14:35 +1100, Chris Angelico wrote: > >> On Thu, Nov 8, 2012 at 5:35 AM, anatoly techtonik >> wrote: >>> I thought of sys.py3k check ... > > Chris, you regularly reply to the wrong mailing list, and you've just > done it again. This is not python-ideas. Ack! Sorry all. I was sure I checked.... whoops. Egg-on-face-ly yours... ChrisA From frednotbob at hotmail.ca Wed Nov 7 19:07:27 2012 From: frednotbob at hotmail.ca (Graham Fielding) Date: Wed, 7 Nov 2012 16:07:27 -0800 Subject: Issues with python/libtcod Message-ID: Hey, all. I'm trying to program a roguelike, using the wonderful tutorials written by Jo?o F. Henriques (a.k.a. Jotaf), but I've stumbled onto a bit of a problem setting up the game's inventory system, and I was hoping someone could help me out. Here's a code snippet, including the affected line. #def menu(header, options, width): #if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options.') #calculate total height for the header (after auto-wrap) and one line per option #header_height = libtcod.console_height_left_rect(con, 0, 0, width, SCREEN_HEIGHT, header) #height = len(options) + header_height In a nutshell: def menu() is an all-encompassing 'menu processor' function -- it contains a standardized menu layout that can be called, from anywhere in the stack, to create a generic menu. When I launch the game to check things out, everything runs smoothly until I try to open the inventory window -- at which point, IDLE tosses up an AttributeError: File "C:/Python Project/Roguelike.py", line 602, in player_action = handle_keys() File "C:/Python Project/Roguelike.py", line 531, in handle_keys chosen_item = inventory_menu('Press the key next to an item to use it, or any other to cancel.\n') File "C:/Python Project/Roguelike.py", line 487, in inventory_menu index = menu(header, options, INVENTORY_WIDTH) File "C:/Python Project/Roguelike.py", line 447, in menu header_height = libtcod.console_height_left_rect(con, 0, 0, width, SCREEN_HEIGHT, header) AttributeError: 'module' object has no attribute 'console_height_left_rect' I've tried moving the entire def menu() function to various sections of the stack, I've changed its arguments manually, and I've even removed the affected line entirely to see if that would help; nothing seems to work, and it's only when def menu() is called that this happens. Anyone got any ideas what could be going flooey? -------------- next part -------------- An HTML attachment was scrubbed... URL: From andrew3 at r3dsolutions.com Wed Nov 7 19:24:22 2012 From: andrew3 at r3dsolutions.com (Andrew Robinson) Date: Wed, 07 Nov 2012 16:24:22 -0800 Subject: Multi-dimensional list initialization In-Reply-To: References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <50999214.50100@r3dsolutions.com> <509ABBD4.7020200@r3dsolutions.com> Message-ID: <509AFBB6.9040008@r3dsolutions.com> On 11/07/2012 01:01 PM, Ian Kelly wrote: > On Wed, Nov 7, 2012 at 12:51 PM, Andrew Robinson > wrote: >> Interesting, you avoided the main point "lists are copied with list >> multiplication". > It seems that each post is longer than the last. If we each responded > to every point made, this thread would fill a book. It already is :) > > Anyway, your point was to suggest that people would not be confused by > having list multiplication copy lists but not other objects, because > passing lists into functions as parameters works in basically the same > way. Not quite; Although I wasn't clear; The variable passed in is by *value* in contradistinction to the list which is by reference. Python does NOT always default copy by reference *when it could*; that's the point. Hence the programmer has to remember in foo( x,y ), the names x and y when assigned to -- *DONT* affect the variables from which they came. But any object internals do affect the objects everywhere. A single exception exists; My thesis is for a single exception as well -- I think Python allows that kind of thinking. > So actually I did address > this point with the "call-by-object" tangent; I just did not > explicitly link it back to your thesis. My apology for not proof reading my statements for clarity. It was definitely time for a nap back then. > Potayto, potahto. The distinction that you're describing is between > "strict" versus "non-strict" evaluation strategies. Hinging the > distinction on the non-descriptive words "call" and "pass" is lazy > terminology that should never have been introduced in the first place. I would do it again. Other's have already begun to discuss terminology with you -- I won't double team you. From oscar.j.benjamin at gmail.com Wed Nov 7 19:30:53 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 8 Nov 2012 00:30:53 +0000 Subject: Multi-dimensional list initialization In-Reply-To: <509af632$0$29980$c3e8da3$5496439d@news.astraweb.com> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <50999214.50100@r3dsolutions.com> <509ABBD4.7020200@r3dsolutions.com> <509af632$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 8 November 2012 00:00, Steven D'Aprano wrote: > Andrew, it appears that your posts are being eaten or rejected by my > ISP's news server, because they aren't showing up for me. Possibly a side- > effect of your dates being in the distant past? So if you have replied to > any of my posts, I haven't seen them. > > In any case, I wanted to ask a question: > > > On Wed, 07 Nov 2012 14:01:19 -0700, Ian Kelly wrote: >> On Wed, Nov 7, 2012 at 12:51 PM, Andrew Robinson >> wrote: > > [...] > >>> But, in any event: >>> Pass by value (not call by value) is a term stretching back 30 years; >>> eg: when I learned the meaning of the words. Rewording it as "Call by >>> value" is something that happened later, and the nuance is lost on >>> those without a very wide programming knowledge *and* age. > > Every now and again I come across somebody who tries to distinguish > between "call by foo" and "pass by foo", but nobody has been able to > explain the difference (if any) to me. When you CALL a function, you PASS > values to it. Hence the two terms are effectively synonyms, and both > refer to the evaluation strategy when binding arguments to parameters. > > If you believe that is incorrect, can you point me to something > explaining the difference? Did you also miss MRAB's post above? It made sense to me. MRAB wrote: > The disadvantage of calling it "call by ..." is that it suggests that > you're just talking about calling functions. > > What about binding in general, eg "x = y"? Does it make sense to still > call it "call by ..."? From oscar.j.benjamin at gmail.com Wed Nov 7 19:44:26 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 8 Nov 2012 00:44:26 +0000 Subject: Right solution to unicode error? In-Reply-To: <509AF3EF.8050108@gmail.com> References: <09a3d20b-5871-47f4-9218-df119698e405@m4g2000yqf.googlegroups.com> <509AF3EF.8050108@gmail.com> Message-ID: On 7 November 2012 23:51, Andrew Berg wrote: > On 2012.11.07 17:27, Oscar Benjamin wrote: >> Are you using cmd.exe (standard Windows terminal)? If so, it does not >> support unicode > Actually, it does. Code page 65001 is UTF-8. I know that doesn't help > the OP since Python versions below 3.3 don't support cp65001, but I > think it's important to point out that the Windows command line system > (it is not unique to cmd) does in fact support Unicode. I have tried to use code page 65001 and it didn't work for me even if I did use a version of Python (possibly 3.3 alpha) that claimed to support it. It turned out that there were other Windows related problems with using the codepage so that I had to do something like chcp 65001 && python myscript.py && chcp 2521 (It was important for all those commands to be on the same line) I'm not on Windows right now and I can't remember all the details but I seem to remember that even with that awkwardness and changing the font it still didn't actually work. If you know how to make it work, I'd be interested to know. Oscar From joshua.landau.ws at gmail.com Wed Nov 7 19:46:03 2012 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Thu, 8 Nov 2012 00:46:03 +0000 Subject: Multi-dimensional list initialization In-Reply-To: <509AF4F2.3060406@r3dsolutions.com> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098A55C.3090201@r3dsolutions.com> <5098C873.2000200@r3dsolutions.com> <509982CA.7050701@r3dsolutions.com> <509AF4F2.3060406@r3dsolutions.com> Message-ID: On 7 November 2012 23:55, Andrew Robinson wrote: > On 11/07/2012 05:39 AM, Joshua Landau wrote: > > A more modest addition for the limited case described in this thread >> could be to use exponentiation: >> >> >>> [0] ** (2, 3) >> [[0, 0, 0], [0, 0, 0]] >> > I'm against over using the math operators, for the reason that matrix > and vector algebra have meanings mathematicians desire (rightly) to > maintain. Numpy users might find matricies overloaded to do these things > in the future -- and then it becomes unclear whether an initialization is > happening or a mathematical operation. I think it best just not to set up > an accident waiting to happen in the first place. > I'm going to say right now that I'm very much fond of the exponentiation proposal. Multiplication on Numpy arrays is already completely disjoint to multiplication on lists, and that is probably completely disjoint to all sorts of mathematical meanings. I don't personally feel that anyone who knows what [0] * 3 is would *assume* (although they may suppose) that exponentiation will be a maths operator. When I saw [0] ** (2, 3), I knew what it did before I read anything else. I know I had the context of the posts above, so it isn't a fair comparison, but it seems really obvious an extension. It's so closely linked to * (if not for the ambiguities, I would have preferred multiplication) that it makes total sense. Even if you think of "4 ** 5" as "4 * 4, 5 times", there is a direct mental link to what is happening. > Hold on: why not just use multiplication? > > >>> [0] * (2, 3) > > Would you consider that better than [0].nest(2).nest(3) ? or [0].nest(2,3) > ? > (I'm against multiplication, but I'm still interested in what you find > attractive about it.) > Yes. Having [0] * 2 with a distinct but fundamentally the same (it's just gotten extra dimensions) partner that is called in a very similar way is a good thing. I'd feel equally unhappy with "4 * 3" partnering with "(4).pow(3)"* as I would with your .nest(2, 3) and I like the iterated ones even less because I don't see it as obviously possible for them to even work. [0].nest(2) -> [[0], [0]] ? [[0], [0]].nest(3) -> [[0,0,0], [0,0,0]] ??? (what about 3d?) * Even if you could write that as "4.pow(3)" because floats didn't exist or something. -------------- next part -------------- An HTML attachment was scrubbed... URL: From greg.ewing at canterbury.ac.nz Wed Nov 7 20:29:59 2012 From: greg.ewing at canterbury.ac.nz (Greg Ewing) Date: Thu, 08 Nov 2012 14:29:59 +1300 Subject: Multi-dimensional list initialization In-Reply-To: References: <5098A55C.3090201@r3dsolutions.com> <5098C873.2000200@r3dsolutions.com> <509982CA.7050701@r3dsolutions.com> Message-ID: <509B0B17.3040704@canterbury.ac.nz> On 08/11/12 12:06, Oscar Benjamin wrote: > On 7 November 2012 22:16, Joshua Landau wrote: >> That said, losing: >> [0] * (2, 3) == [0] * [2, 3] >> would mean losing duck-typing in general. There are precedents for this kind of thing; the string % operator treats tuples specially, for example. I don't think it's all that bad if you regard the tuple as effectively part of the syntax. -- Greg From andrew3 at r3dsolutions.com Wed Nov 7 22:13:25 2012 From: andrew3 at r3dsolutions.com (Andrew Robinson) Date: Wed, 07 Nov 2012 19:13:25 -0800 Subject: Multi-dimensional list initialization In-Reply-To: References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <5099bf7d$0$29980$c3e8da3$5496439d@news.astraweb.com> <509ADA6E.10403@r3dsolutions.com> Message-ID: <509B2355.60407@r3dsolutions.com> On 11/07/2012 03:39 PM, Ian Kelly wrote: > Why? Just to get rid of an FAQ? :-) > Here's one of the more interesting uses from my own code: OK, and is this a main use case? (I'm not saying it isn't I'm asking.) > Replacing the list multiplication in that function with a list > comprehension would be awkward, as the obvious replacement of > [iter(iterable) for _ in range(n)] would produce different results. Yes. I have a thought on that. > How exactly do you propose to indicate to the compiler which parts of > the expressions are meant to be cached, and which are not? Exactly? OK; Here's what I would consider a safe implementation -- but it could be improved later. There is a special keyword which signals the new type of comprehension; A normal comprehension would say eg: '[ foo for i in xrange ]'; but when the 'for i in' is reduced to a specific keyword such as 'ini' (instead of problematic 'in') the caching form of list comprehension would start. So, then, just like a comprehension -- the interpreter will begin to evaluate the code from the opening bracket '['; But anything other than a function/method will raise a type error (people might want to change that, but it's safe). The interpreter then caches all functions/initialiser methods it comes into contact with. Since every function/method has a parameter list (even if empty); The interpreter would evaluate the parameter list on the first pass through the comprehension, and cache each parameter list with it's respective function. When the 'ini' keyword is parsed a second time, Python would then evaluate each cached function on its cached parameter list; and the result would be stored in the created list. This cached execution would be repeated as many times as is needed. Now, for your example: > values = zip(samples, times * num_groups) > if len(values)< len(times) * num_groups: > # raise an error Might be done with: values = zip( samples, [ lambda:times, ini xrange(num_groups) ] ) if len(values) < len(times) * num_groups The comma after the lambda is questionable, and this construction would be slower since lambda automatically invokes the interpreter; but it's correct. If you could provide a built in which returns a reference to the parameter passed to it; that would run at max system speed; by default, all built-in object initializers are maximally fast. The key difference is that the ini syntax evaluates the parameter lists only once; and the ini's purpose is for repeating an initialization of the same kind of object in multiple different places. As an aside, how would you do the lambda inside a list comprehension? [lambda:6 for i in xrange(10) ] # Nope. Generic lists allow a spurrious comma, so that [ 3,3,3, ] = [3,3,3] dropped; [lambda:6, for i in xrange(10) ] # but this is no good. I have to do: def ref(): return 6 [ref(x) for i in xrange(10) ] > Of course you got an integer. You took an index of the range object, > not a slice. The rule is that taking an index of a sequence returns an > element; taking a slice of a sequence returns a sub-sequence. You > still have not shown any inconsistency here. Because it's an arbitrary rule which operates differently than the traditional idea shown in python docs? slice.indices() is *for* (QUOTE)"representing the _set of indices_ specified by _range_(start, stop, step)" http://docs.python.org/2/library/functions.html#slice There are examples of python doing this; use Google... They use slice indices() to convert negative indexes into positive ones _compatible with range()_. some_getitem_method_in_a_subclass_foo( self, range ): ret=[] for i in xrange( range.indices( len(self) ) ): ret.append( self.thing[i] ) return ret The return is equivalent to a range object in the sense that it is an iterator object, but it's not the same iterator object. It will still work with legacy code.... since different iterators can be interchanged so long as they return the same values. > No, he wasn't. He was talking about multiplying lists of dicts, and > whether the dicts are then copied or not, just like every other Q&A > item in that dialogue was concerning whether item X in a list should > expect to be copied when the containing list is multiplied. I already told him several times before that what the answer was; It doesn't copy anything except the list itself. Then he asks, "does it multiply dicts" and no mention of it being inside a list. He's browbeating a dead horse. > Perhaps you're not aware that on the Internet, TYPING IN ALL CAPS is > commonly construed as SHOUTING. Sure, and people say: THIS IS YELLING, AND I AM DOING IT HERE AS AN EXAMPLE. This is STRESS. This is SHOCK! I don't recall typing any _full sentence_ in all caps, if I did, I'm awfully sorry. I didn't mean it. Yes, he is beginning to get condescendingly exasperating. Everyone else seems to understand 85+% of what I say, correctly. He doesn't; and now replying to him appears to confuse everyone else. -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Wed Nov 7 22:47:40 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 08 Nov 2012 03:47:40 GMT Subject: Multi-dimensional list initialization References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <50999214.50100@r3dsolutions.com> <509ABBD4.7020200@r3dsolutions.com> <509af632$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: <509b2b5c$0$29885$c3e8da3$5496439d@news.astraweb.com> On Thu, 08 Nov 2012 00:30:53 +0000, Oscar Benjamin wrote: >> Every now and again I come across somebody who tries to distinguish >> between "call by foo" and "pass by foo", but nobody has been able to >> explain the difference (if any) to me. When you CALL a function, you >> PASS values to it. Hence the two terms are effectively synonyms, and >> both refer to the evaluation strategy when binding arguments to >> parameters. >> >> If you believe that is incorrect, can you point me to something >> explaining the difference? > > Did you also miss MRAB's post above? It made sense to me. You mean MRABs post which I replied to? Yes, I must have missed it :-P But seriously, no I didn't miss it. He doesn't give any evidence that there is a difference between "call by ..." and "pass by ..." when talking about binding arguments to formal parameters. His objection to "call by ..." is that it doesn't make it clear that the evaluation rules apply to simple binding/assignment as well as calling functions. -- Steven From steve+comp.lang.python at pearwood.info Wed Nov 7 23:20:24 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 08 Nov 2012 04:20:24 GMT Subject: Multi-dimensional list initialization References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <50999214.50100@r3dsolutions.com> <509ABBD4.7020200@r3dsolutions.com> Message-ID: <509b3308$0$29885$c3e8da3$5496439d@news.astraweb.com> On Wed, 07 Nov 2012 16:24:22 -0800, Andrew Robinson wrote: > On 11/07/2012 01:01 PM, Ian Kelly wrote: [...] >> Anyway, your point was to suggest that people would not be confused by >> having list multiplication copy lists but not other objects, because >> passing lists into functions as parameters works in basically the same >> way. > > Not quite; Although I wasn't clear; The variable passed in is by > *value* in contradistinction to the list which is by reference. Python > does NOT always default copy by reference *when it could*; that's the > point. It isn't clear to me whether you are describing what you think Python *actually* does, versus what you wish it *would* do, or what it *could* do in some abstract hypothetical sense. It certainly is not true that Python passes "the variable" by value, and lists "by reference". Arguments are not passed to functions either by value or by reference. There is a trivial test for pass-by-value semantics: does the value get copied? We can see that Python does not copy arguments: py> def test(x): ... print id(x) ... py> spam = [] py> print id(spam); test(spam) 3071264556 3071264556 The argument is not copied, therefore Python is not pass-by-value. There is also an easy test for pass-by-reference semantics: can you write a procedure which, given two variables, swaps the contents of the variables? In Pascal, that is trivial. procedure swap(var a: int, var b: int): var tmp: int; begin tmp := a; a := b; b := a; end; swap(x, y); (if I've remembered my Pascal syntax correctly). In Python, you can swap two values like this: a, b = b, a but that's not sufficient. The test is to do the swap inside a function: def swap(a, b): return b, a b, a = swap(a, b) But that fails too, since the assignment is still taking place outside the function. It turns out that there is no way in Python to write such a swap function. Tricks such as passing the variable names as strings, then using exec, are hacks and don't count. Python is not pass by reference either. > Hence the programmer has to remember in foo( x,y ), the names x and y > when assigned to -- *DONT* affect the variables from which they came. > But any object internals do affect the objects everywhere. Ummm.... yes? The programmer has to remember Python's execution model in order to correctly predict what Python will do. What's your point? > A single exception exists; There is no such exception in Python. Python always uses the same argument passing (parameter binding) semantics. -- Steven From andrew3 at r3dsolutions.com Wed Nov 7 23:51:04 2012 From: andrew3 at r3dsolutions.com (Andrew Robinson) Date: Wed, 07 Nov 2012 20:51:04 -0800 Subject: Multi-dimensional list initialization In-Reply-To: <509af632$0$29980$c3e8da3$5496439d@news.astraweb.com> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <50999214.50100@r3dsolutions.com> <509ABBD4.7020200@r3dsolutions.com> <509af632$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: <509B3A38.3000109@r3dsolutions.com> On 11/07/2012 04:00 PM, Steven D'Aprano wrote: > Andrew, it appears that your posts are being eaten or rejected by my > ISP's news server, because they aren't showing up for me. Possibly a side- > effect of your dates being in the distant past? Date has been corrected since two days ago. It will remain until a reboot.... Ignorance, though, might be bliss... > Every now and again I come across somebody who tries to distinguish > between "call by foo" and "pass by foo", but nobody has been able to > explain the difference (if any) to me. I think the "Call by foo" came into vogue around the time of C++; Eg: It's in books like C++ for C programmers; I never saw it used before then so I *really* don't know for sure... I know "Pass by value" existed all the way back to the 1960's. I see "pass by" in my professional books from those times and even most newer ones; but I only find "Call by value" in popular programming books of more recent times. (Just my experience) So -- I "guess" the reason is that when invoking a subroutine, early hardware often had an assembler mnemonic by the name "call". See for example: Intelx86 hardware books from the 1970's; Most early processors (like the MC6809E, and 8080) allow both direct and indirect *references* to a function (C would call them function pointers); So, occasionally early assembly programs comment things like: "; dynamic VESA libraries are called by value in register D."; And they meant that register D is storing a function call address from two or more vesa cards. It had little to do with the function's parameters, (which might be globals anyway) (It procedural dynamic binding!) Today, I don't know for sure -- so I just don't use it. "pass" indicates a parameter of the present call; but not the present call itself. From j-p at sunrise.ch Thu Nov 8 00:53:09 2012 From: j-p at sunrise.ch (Jean-Pierre Miceli) Date: Thu, 8 Nov 2012 06:53:09 +0100 Subject: how-to use readline.set_completion_display_matches_hook()? In-Reply-To: References: Message-ID: <801C96D9-23EA-4417-89E8-FD17156DB13E@sunrise.ch> Thanks, I will try rl. Have a nice day J-P Le 7 nov. 2012 ? 14:14, Stefan H. Holek a ?crit : > On 07.11.2012, at 11:36, Jean-Pierre Miceli wrote: > >> Hi all, >> >> I'm working on a tool which enable support of tab completion using the >> readline modul. >> And I have a problem with set_completion_display_matches_hook function >> >> I've created a display hook function and registered it. It is called >> and it prints the desire messages. But once it has completed, >> readline/python does not display the prompt. >> I've got to press the 'return' key to see the prompt again. >> What should the display hook function do to return correctly and let >> the prompt be displayed? >> >> Example: >> def completerHook(self, substitution, matches, longest_match_length): >> print "" >> print "Test of the display hook function" >> for i in range(len(matches)): >> print matches[i], >> >> I use Python 2.7.3 and done some tests on Mac OS 10.7 (Lion) and >> Ubuntu. I got the same issue on both system. > > This always happens when you print newlines behind readline's back. To update the prompt area, you'd have to call rl_forced_update_display() which is AFAIK not exposed by the stdlib's readline bindings. > > There is a more complete implementation of the GNU Readline APIs at http://pypi.python.org/pypi/rl. With rl you can fix the prompt by calling rl.readline.redisplay(force=True) after the hook has returned. > > Hope this helps, > Stefan > > -- > Stefan H. Holek > stefan at epy.co.at > From ian.g.kelly at gmail.com Thu Nov 8 02:09:35 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 8 Nov 2012 00:09:35 -0700 Subject: Multi-dimensional list initialization In-Reply-To: <509B2355.60407@r3dsolutions.com> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <5099bf7d$0$29980$c3e8da3$5496439d@news.astraweb.com> <509ADA6E.10403@r3dsolutions.com> <509B2355.60407@r3dsolutions.com> Message-ID: On Wed, Nov 7, 2012 at 8:13 PM, Andrew Robinson wrote: > OK, and is this a main use case? (I'm not saying it isn't I'm asking.) I have no idea what is a "main" use case. > There is a special keyword which signals the new type of comprehension; A > normal comprehension would say eg: '[ foo for i in xrange ]'; but when the > 'for i in' is reduced to a specific keyword such as 'ini' (instead of > problematic 'in') the caching form of list comprehension would start. FYI, the Python devs are not very fond of adding new keywords. Any time a new keyword is added, existing code that uses that word as a name is broken. 'ini' is particularly bad, because 1) it's not a word, and 2) it's the name of a common type of configuration file and is probably frequently used as a variable name in relation to such files. > So, then, just like a comprehension -- the interpreter will begin to > evaluate the code from the opening bracket '['; But anything other than a > function/method will raise a type error (people might want to change that, > but it's safe). > > The interpreter then caches all functions/initialiser methods it comes into > contact with. > Since every function/method has a parameter list (even if empty); The > interpreter would evaluate the parameter list on the first pass through the > comprehension, and cache each parameter list with it's respective function. > > When the 'ini' keyword is parsed a second time, Python would then evaluate > each cached function on its cached parameter list; and the result would be > stored in the created list. > This cached execution would be repeated as many times as is needed. > > Now, for your example: > > values = zip(samples, times * num_groups) > if len(values) < len(times) * num_groups: > # raise an error > > Might be done with: > > values = zip( samples, [ lambda:times, ini xrange(num_groups) ] ) > > if len(values) < len(times) * num_groups > > The comma after the lambda is questionable, and this construction would be > slower since lambda automatically invokes the interpreter; but it's correct. How is this any better than the ordinary list comprehension I already suggested as a replacement? For that matter, how is this any better than list multiplication? Your basic complaint about list multiplication as I understand it is that the non-copying semantics are unintuitive. Well, the above is even less intuitive. It is excessively complicated and almost completely opaque. If I were to come across it outside the context of this thread, I would have no idea what it is meant to be doing. > As an aside, how would you do the lambda inside a list comprehension? As a general rule, I wouldn't. I would use map instead. > [lambda:6 for i in xrange(10) ] # Nope. Thak constructs a list of 10 functions and never calls them. If you want to actually call the lambda, then: [(lambda: 6)() for i in range(10)] or: map(lambda i: 6, range(10)) But note that the former creates equivalent 10 functions and calls each of them once, whereas the latter creates one function and calls it ten times. >> Of course you got an integer. You took an index of the range object, not a >> slice. The rule is that taking an index of a sequence returns an element; >> taking a slice of a sequence returns a sub-sequence. You still have not >> shown any inconsistency here. > > > Because it's an arbitrary rule which operates differently than the > traditional idea shown in python docs? > > slice.indices() is *for* (QUOTE)"representing the set of indices specified > by range(start, stop, step)" > http://docs.python.org/2/library/functions.html#slice slice.indices() has nothing to do with it. Indexing a sequence and calling the .indices() method on a slice are entirely different operations. The slice.indices method is a utility method meant to be called by __getitem__ implementations when doing slicing, not an implementation of indexing. When a sequence is indexed, there is no slice. That method is not related in any way to the semantics of indexing a sequence. From varma.nikhil22 at gmail.com Thu Nov 8 02:09:37 2012 From: varma.nikhil22 at gmail.com (Nikhil Verma) Date: Thu, 8 Nov 2012 12:39:37 +0530 Subject: get weekday as week number in a month Message-ID: Hi I was converting simple string in django into a standard datetime object and creating two separate list one of date and other of time. I want to ask this :- date_object = datetime.strptime('November 9 2012 11:30PM', '%B %d %Y %I:%M%p') which gives me :- datetime.datetime(2012, 11, 9, 23, 30) and this is correct. What i want to know is if i convert it to date_object = datetime.strptime(' Friday November 9 2012 11:30PM', '%u %B %d %Y %I:%M%p' ) It is giving me ValueError saying u is unsupported directive ! How can i get weekday as week number ? Thanks in advance ! -- Regards Nikhil Verma +91-958-273-3156 -------------- next part -------------- An HTML attachment was scrubbed... URL: From ian.g.kelly at gmail.com Thu Nov 8 02:23:07 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 8 Nov 2012 00:23:07 -0700 Subject: get weekday as week number in a month In-Reply-To: References: Message-ID: On Thu, Nov 8, 2012 at 12:09 AM, Nikhil Verma wrote: > What i want to know is if i convert it to > > date_object = datetime.strptime(' Friday November 9 2012 11:30PM', '%u %B %d > %Y %I:%M%p' ) > > It is giving me ValueError saying u is unsupported directive ! Use '%A' to match 'Friday', not '%u'. The full list of directives can be found at: http://docs.python.org/2/library/time.html#time.strftime > How can i get weekday as week number ? Is this what you're looking for? >>> datetime.strptime('Friday November 9 2012 11:30PM', '%A %B %d %Y %I:%M%p').weekday() 4 From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Thu Nov 8 02:38:42 2012 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Thu, 08 Nov 2012 08:38:42 +0100 Subject: Immutability and Python In-Reply-To: References: Message-ID: Am 29.10.2012 16:20 schrieb andrea crotti: > Now on one hand I would love to use only immutable data in my code, but > on the other hand I wonder if it makes so much sense in Python. You can have both. Many mutable types distinguish between them with their operators. To pick up your example, class NumWrapper(object): def __init__(self, number): self.number = number def __iadd__(self, x): self.number += x return self def __add__(self, x): return NumWrapper(self.number + x) So with number += 1 you keep the same object and modify it, while with number = number + 1 or new_number = number + 1 you create a new object. > But more importantly normally classes are way more complicated than my > stupid example, so recreating a new object with the modified state might > be quite complex. > > Any comments about this? What do you prefer and why? That's why I generally prefer mutable objects, but it can depend. Thomas From varma.nikhil22 at gmail.com Thu Nov 8 03:04:56 2012 From: varma.nikhil22 at gmail.com (Nikhil Verma) Date: Thu, 8 Nov 2012 13:34:56 +0530 Subject: Unconverted data remains Message-ID: Hi My Problem I have a list :- L = ['Sunday November 11 2012 9:00pm ', 'Thursday November 15 2012 7:00pm ',\ 'Friday November 16 2012 7:00pm ', 'Monday November 19 2012 7:30pm ', \ 'Friday November 23 2012 7:30pm ', 'Sunday November 25 2012 8:00pm ',\ 'Monday November 262012 7:00pm ', 'Thursday November 29 2012 6:30pm ',\ 'Saturday December 1 20125:30pm ', 'Thursday December 6 2012 9:00pm ',\ 'Sunday December 9 2012 7:00pm ', 'Friday November 9 2012 6:00pm ', \ 'Friday November 9 2012 7:00pm ', 'Friday November 9 2012 7:00pm '] final_event_time = [datetime.strptime(iterable, '%A %B %d %Y %I:%M%p') for iterable in L] and having this error Unconverted data remains . I am trying to convert all these in datetime object. -- Regards Nikhil Verma +91-958-273-3156 -------------- next part -------------- An HTML attachment was scrubbed... URL: From andrew3 at r3dsolutions.com Thu Nov 8 03:26:53 2012 From: andrew3 at r3dsolutions.com (Andrew Robinson) Date: Thu, 08 Nov 2012 00:26:53 -0800 Subject: Multi-dimensional list initialization In-Reply-To: References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <5099bf7d$0$29980$c3e8da3$5496439d@news.astraweb.com> <509ADA6E.10403@r3dsolutions.com> <509B2355.60407@r3dsolutions.com> Message-ID: <509B6CCD.2010302@r3dsolutions.com> On 11/07/2012 11:09 PM, Ian Kelly wrote: > On Wed, Nov 7, 2012 at 8:13 PM, Andrew Robinson > wrote: >> OK, and is this a main use case? (I'm not saying it isn't I'm asking.) > I have no idea what is a "main" use case. Well, then we can't evaluate if it's worth keeping a list multiplier around at all. You don't even know how it is routinely used. > FYI, the Python devs are not very fond of adding new keywords. Any > time a new keyword is added, existing code that uses that word as a > name is broken. 'ini' is particularly bad, because 1) it's not a > word, and 2) it's the name of a common type of configuration file and > is probably frequently used as a variable name in relation to such > files. Fine; Its a keyword TBD then; I should have said 'foo'. in is worse than ini, ini is worse than something else -- at the end of the rainbow, maybe there is something.... > values = zip( samples, [ lambda:times, ini xrange(num_groups) ] ) > > if len(values)< len(times) * num_groups > > > How is this any better than the ordinary list comprehension I already > suggested as a replacement? For that matter, how is this any better > than list multiplication? You _asked it to implement_ a list multiplication of the traditional kind; By doing copies by *REFERENCE*; so of course it's not better. My intentions were for copying issues, not-non copying ones. > Your basic complaint about list > multiplication as I understand it is that the non-copying semantics > are unintuitive. No. 1) My basic complaint is that people (I think from watching) primarily use it to make initializer lists, and independent mutable place holders; List multiplication doesn't do that well. 2) If it is indeed very rare (as D'Aprano commented) then -- it has a second defect in looking to casual inspection to be the same as vector multiplication; which opacifies which operation is being done when matrix packages are potentially being used. > Well, the above is even less intuitive. It is > excessively complicated and almost completely opaque. If I were to > come across it outside the context of this thread, I would have no > idea what it is meant to be doing. Nor would I *know* what this list multiplier look alike does [1,2,3]*aValue without checking to see if someone imported a vector library and the variable aValue has a special multiplication operator. >> As an aside, how would you do the lambda inside a list comprehension? > As a general rule, I wouldn't. I would use map instead. OK: Then copy by reference using map....: values = zip( map( lambda:times, xrange(num_groups) ) ) if len(values) < len(times) * num_groups ... Done. It's clearer than a list comprehension and you still really don't need a list multiply. I''m not going to bother explaining what the construction I offered would be really good at. It's pointless to explain to the disinterested. > Thak constructs a list of 10 functions and never calls them. If you > want to actually call the lambda, then: Yep, I was very tired. > slice.indices() has nothing to do with it. Indexing a sequence and > calling the .indices() method on a slice are entirely different > operations. Yes, but you're very blind to history and code examples implementing the slice operation. slice usually depends on index; index does not depend on slice. Slice is suggested to be implemented by multiple calls to single indexes in traditional usage and documentation. The xrange(,,)[:] implementation breaks the tradition, because it doesn't call index multiple times; nor does it return a result equivalent identical to doing that. It's different. period. You're not convincing in the slightest by splitting hairs. -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Thu Nov 8 03:44:58 2012 From: __peter__ at web.de (Peter Otten) Date: Thu, 08 Nov 2012 09:44:58 +0100 Subject: Read number of CSV files References: Message-ID: Smaran Harihar wrote: > I am able to read through a CSV File and fetch the data inside the CSV > file but I have a really big list of CSV files and I wish to do the same > particular code in all the CSV files. > > Is there some way that I can loops through all these files, which are in a > single folder, and get my code to read the files? import glob import csv def process(filename): with open(filename, "rb") as f: rows = csv.reader(f) for row in rows: ... # whatever you want for filename in glob.glob("/path/to/*.csv"): process(filename) From __peter__ at web.de Thu Nov 8 03:57:16 2012 From: __peter__ at web.de (Peter Otten) Date: Thu, 08 Nov 2012 09:57:16 +0100 Subject: Unconverted data remains References: Message-ID: Nikhil Verma wrote: > I have a list :- > L = ['Sunday November 11 2012 9:00pm ', 'Thursday November 15 2012 > 7:00pm ',\ [...] > 2012 7:00pm '] > final_event_time = [datetime.strptime(iterable, '%A %B %d %Y %I:%M%p') for > iterable in L] > > and having this error Unconverted data remains . I am trying to convert > all these in datetime object. Your date strings have trailing whitespace, try changing the format to final_event_time = [datetime.strptime(s, '%A %B %d %Y %I:%M%p ') for s in L] or apply strip to the date strings: final_event_time = [datetime.strptime(s.strip(), '%A %B %d %Y %I:%M%p') for s in L] From andrea.crotti.0 at gmail.com Thu Nov 8 05:11:44 2012 From: andrea.crotti.0 at gmail.com (andrea crotti) Date: Thu, 8 Nov 2012 10:11:44 +0000 Subject: creating size-limited tar files In-Reply-To: References: <509ab0fa$0$6636$9b4e6d93@newsspool2.arcor-online.net> <509AD812.2060605@gmail.com> Message-ID: 2012/11/7 Oscar Benjamin : > > Correct. But if you read the rest of Alexander's post you'll find a > suggestion that would work in this case and that can guarantee to give > files of the desired size. > > You just need to define your own class that implements a write() > method and then distributes any data it receives to separate files. > You can then pass this as the fileobj argument to the tarfile.open > function: > http://docs.python.org/2/library/tarfile.html#tarfile.open > > > Oscar Yes yes I saw the answer, but now I was thinking that what I need is simply this: tar czpvf - /path/to/archive | split -d -b 100M - tardisk since it should run only on Linux it's probably way easier, my script will then only need to create the list of files to tar.. The only doubt is if this is more or less reliably then doing it in Python, when can this fail with some bad broken pipe? (the filesystem is not very good as I said and it's mounted with NFS) From andrea.crotti.0 at gmail.com Thu Nov 8 05:29:36 2012 From: andrea.crotti.0 at gmail.com (andrea crotti) Date: Thu, 8 Nov 2012 10:29:36 +0000 Subject: creating size-limited tar files In-Reply-To: References: <509ab0fa$0$6636$9b4e6d93@newsspool2.arcor-online.net> <509AD812.2060605@gmail.com> Message-ID: 2012/11/8 andrea crotti : > > > > Yes yes I saw the answer, but now I was thinking that what I need is > simply this: > tar czpvf - /path/to/archive | split -d -b 100M - tardisk > > since it should run only on Linux it's probably way easier, my script > will then only need to create the list of files to tar.. > > The only doubt is if this is more or less reliably then doing it in > Python, when can this fail with some bad broken pipe? > (the filesystem is not very good as I said and it's mounted with NFS) In the meanwhile I tried a couple of things, and using the pipe on Linux actually works very nicely, it's even faster than simple tar for some reasons.. [andrea at andreacrotti isos]$ time tar czpvf - file1.avi file2.avi | split -d -b 1000M - inchunks file1.avi file2.avi real 1m39.242s user 1m14.415s sys 0m7.140s [andrea at andreacrotti isos]$ time tar czpvf total.tar.gz file1.avi file2.avi file1.avi file2.avi real 1m41.190s user 1m13.849s sys 0m5.723s [andrea at andreacrotti isos]$ time split -d -b 1000M total.tar.gz inchunks real 0m55.282s user 0m0.020s sys 0m3.553s From wxjmfauth at gmail.com Thu Nov 8 06:01:14 2012 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Thu, 8 Nov 2012 03:01:14 -0800 (PST) Subject: Right solution to unicode error? In-Reply-To: <09a3d20b-5871-47f4-9218-df119698e405@m4g2000yqf.googlegroups.com> References: <09a3d20b-5871-47f4-9218-df119698e405@m4g2000yqf.googlegroups.com> Message-ID: Le mercredi 7 novembre 2012 23:17:42 UTC+1, Anders a ?crit?: > I've run into a Unicode error, and despite doing some googling, I > > can't figure out the right way to fix it. I have a Python 2.6 script > > that reads my Outlook 2010 task list. I'm able to read the tasks from > > Outlook and store them as a list of objects without a hitch. But when > > I try to print the tasks' subjects, one of the tasks is generating an > > error: > > > > Traceback (most recent call last): > > File "outlook_tasks.py", line 66, in > > my_tasks.dump_today_tasks() > > File "C:\Users\Anders\code\Task List\tasks.py", line 29, in > > dump_today_tasks > > print task.subject > > UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in > > position 42: ordinal not in range(128) > > > > (where task.subject was previously assigned the value of > > task.Subject, aka the Subject property of an Outlook 2010 TaskItem) > > > > From what I understand from reading online, the error is telling me > > that the subject line contains an en dash and that Python is trying > > to convert to ascii and failing (as it should). > > > > Here's where I'm getting stuck. In the code above I was just printing > > the subject so I can see whether the script is working properly. > > Ultimately what I want to do is parse the tasks I'm interested in and > > then create an HTML file containing those tasks. Given that, what's > > the best way to fix this problem? > > > > BTW, if there's a clear description of the best solution for this > > particular problem ? i.e., where I want to ultimately display the > > results as HTML ? please feel free to refer me to the link. I tried > > reading a number of docs on the web but still feel pretty lost. > > > > Thanks, > > Anders ---------- The problem is not on the Python side or specific to Python. It is on the side of the "coding of characters". 1) Unicode is an abstract entity, it has to be encoded for the system/device that will host it. Using Python: .encode(host_coding) 2) The host_coding scheme may not contain the character (glyph/grapheme) corresponding to the "unicode character". In that case, 2 possible solutions, "ignore" it ou "replace" it with a substitution character. Using Python: .encode(host_coding, "ignore") .encode(host_coding, "replace") 3) Detecting the host_coding, the most difficult task. Either you have to hard-code it or you may expect Python find it via its sys.encoding. 4) Due to the nature of unicode, it the unique way to do it correctly. Expectedly failing and not failing examples. Mainly Py3, but it doesn't matter. Note: Py3 encodes and creates a byte string, which has to be decoded to produce a native (unicode) string, here with cp1252. Py2 >>> u'?l?phant\u2013abc'.encode('ascii') Traceback (most recent call last): File "", line 1, in u'?l?phant\u2013abc'.encode('ascii') UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 0: ordinal not in range(128) >>> print(u'?l?phant\u2013abc'.encode('cp1252')) ?l?phant?abc >>> Py3 >>> '?l?phant\u2013abc'.encode('ascii') Traceback (most recent call last): File "", line 1, in UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 0: ordinal not in range(128) >>> '?l?phant\u2013abc'.encode('ascii', 'ignore') b'lphantabc' >>> '?l?phant\u2013abc'.encode('ascii', 'replace') b'?l?phant?abc' >>> '?l?phant\u2013abc'.encode('ascii', 'ignore').decode('cp1252') 'lphantabc' >>> '?l?phant\u2013abc'.encode('ascii', 'replace').decode('cp1252') '?l?phant?abc' >>> >>> '?l?phant\u2013abc'.encode('cp1252').decode('cp1252') '?l?phant?abc' >>> sys.stdout.encoding 'cp1252' >>> '?l?phant\u2013abc'.encode(sys.stdout.encoding).decode('cp1252') '?l?phant?abc' etc jmf From hansmu at xs4all.nl Thu Nov 8 06:40:11 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Thu, 08 Nov 2012 12:40:11 +0100 Subject: Right solution to unicode error? In-Reply-To: <509af48d$0$29980$c3e8da3$5496439d@news.astraweb.com> References: <09a3d20b-5871-47f4-9218-df119698e405@m4g2000yqf.googlegroups.com> <509af48d$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: <509b9a1b$0$6841$e4fe514c@news2.news.xs4all.nl> On 8/11/12 00:53:49, Steven D'Aprano wrote: > This error confuses me. Is that an exact copy and paste of the error, or > have you edited it or reconstructed it? Because it seems to me that if > task.subject is a unicode string, as it appears to be, calling print on > it should succeed: > > py> s = u'ABC\u2013DEF' > py> print s > ABC?DEF That would depend on whether python thinks sys.stdout can handle UTF8. For example, on my MacOS X box: $ python2.6 -c 'print u"abc\u2013def"' abc?def $ python2.6 -c 'print u"abc\u2013def"' | cat Traceback (most recent call last): File "", line 1, in UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position 3: ordinal not in range(128) This is because python knows that my terminal is capable of handling UTF8, but it has no idea whether the program at the other end of a pipe had that ability, so it'll fall back to ASCII only if sys.stdout goes to a pipe. Apparently the OP has a terminal that doesn't handle UTF8, or one that Python doesn't know about. Hope this helps, -- HansM From ulrich.eckhardt at dominolaser.com Thu Nov 8 07:05:22 2012 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Thu, 08 Nov 2012 13:05:22 +0100 Subject: isinstance(.., file) for Python 3 Message-ID: <2pjsm9-p8h.ln1@satorlaser.homedns.org> Hi! I have two problems that are related and that I'd like to solve together. Firstly, I have code that allows either a file or a string representing its content as parameter. If the parameter is a file, the content is read from the file. In Python 2, I used "isinstance(p, file)" to determine whether the parameter p is a file. In Python 3, the returnvalue of open() is of type _io.TextIOWrapper, while the built-in class file doesn't exist, so I can't use that code. Secondly, checking for the type is kind-of ugly, because it means that I can't use an object that fits but that doesn't have the right type. In other words, it breaks duck-typing. This is already broken in the Python 2 code, but since I have to touch the code anyway, I might as well fix it on the way. If possible, I'm looking for a solution that works for Pythons 2 and 3, since I'm not fully through the conversion yet and have clients that might use the older snake for some time before shedding their skin. Suggestions? Uli From python at mrabarnett.plus.com Thu Nov 8 07:54:15 2012 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 08 Nov 2012 12:54:15 +0000 Subject: isinstance(.., file) for Python 3 In-Reply-To: <2pjsm9-p8h.ln1@satorlaser.homedns.org> References: <2pjsm9-p8h.ln1@satorlaser.homedns.org> Message-ID: <509BAB77.3030609@mrabarnett.plus.com> On 2012-11-08 12:05, Ulrich Eckhardt wrote: > Hi! > > I have two problems that are related and that I'd like to solve together. > > Firstly, I have code that allows either a file or a string representing > its content as parameter. If the parameter is a file, the content is > read from the file. In Python 2, I used "isinstance(p, file)" to > determine whether the parameter p is a file. In Python 3, the > returnvalue of open() is of type _io.TextIOWrapper, while the built-in > class file doesn't exist, so I can't use that code. > > Secondly, checking for the type is kind-of ugly, because it means that I > can't use an object that fits but that doesn't have the right type. In > other words, it breaks duck-typing. This is already broken in the Python > 2 code, but since I have to touch the code anyway, I might as well fix > it on the way. > > If possible, I'm looking for a solution that works for Pythons 2 and 3, > since I'm not fully through the conversion yet and have clients that > might use the older snake for some time before shedding their skin. > > Suggestions? > Instead of checking whether it's a file, check whether it's a string! From python at mrabarnett.plus.com Thu Nov 8 07:56:14 2012 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 08 Nov 2012 12:56:14 +0000 Subject: Unconverted data remains In-Reply-To: References: Message-ID: <509BABEE.40203@mrabarnett.plus.com> On 2012-11-08 08:04, Nikhil Verma wrote: > Hi > > My Problem > > > I have a list :- > L = ['Sunday November 11 2012 9:00pm ', 'Thursday November 15 2012 > 7:00pm ',\ > 'Friday November 16 2012 7:00pm ', 'Monday November > 19 2012 7:30pm ', \ > 'Friday November 23 2012 7:30pm ', 'Sunday November > 25 2012 8:00pm ',\ > 'Monday November 262012 7:00pm ', 'Thursday November > 29 2012 6:30pm ',\ > 'Saturday December 1 20125:30pm ', 'Thursday December > 6 2012 9:00pm ',\ > 'Sunday December 9 2012 7:00pm ', 'Friday November 9 > 2012 6:00pm ', \ > 'Friday November 9 2012 7:00pm ', 'Friday November 9 > 2012 7:00pm '] > > final_event_time = [datetime.strptime(iterable, '%A %B %d %Y %I:%M%p') > for iterable in L] > > and having this error Unconverted data remains . I am trying to convert > all these in datetime object. > It's complaining about the trailing space in the strings. Try removing it. From steve+comp.lang.python at pearwood.info Thu Nov 8 07:56:57 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 08 Nov 2012 12:56:57 GMT Subject: isinstance(.., file) for Python 3 References: <2pjsm9-p8h.ln1@satorlaser.homedns.org> Message-ID: <509bac19$0$29980$c3e8da3$5496439d@news.astraweb.com> On Thu, 08 Nov 2012 13:05:22 +0100, Ulrich Eckhardt wrote: > Firstly, I have code that allows either a file or a string representing > its content as parameter. If the parameter is a file, the content is > read from the file. In Python 2, I used "isinstance(p, file)" to > determine whether the parameter p is a file. In Python 3, the > returnvalue of open() is of type _io.TextIOWrapper, Incorrect. py> type(open('x', 'wb')) The type returned by open will depend on what you open and how you open it. > while the built-in > class file doesn't exist, so I can't use that code. import io file = io._IOBase will probably work. But consider it a little smelly, since you're relying on an implementation detail. > Secondly, checking for the type is kind-of ugly, because it means that I > can't use an object that fits but that doesn't have the right type. In > other words, it breaks duck-typing. This is already broken in the Python > 2 code, but since I have to touch the code anyway, I might as well fix > it on the way. if hasattr(obj, 'read'): # object is file-like enough to treat as a file pass That means that you can also use io.StringIO objects as pseudo-files too. -- Steven From rosuav at gmail.com Thu Nov 8 07:57:00 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 8 Nov 2012 23:57:00 +1100 Subject: isinstance(.., file) for Python 3 In-Reply-To: <2pjsm9-p8h.ln1@satorlaser.homedns.org> References: <2pjsm9-p8h.ln1@satorlaser.homedns.org> Message-ID: On Thu, Nov 8, 2012 at 11:05 PM, Ulrich Eckhardt wrote: > Firstly, I have code that allows either a file or a string representing its > content as parameter. If the parameter is a file, the content is read from > the file. In Python 2, I used "isinstance(p, file)" to determine whether the > parameter p is a file... Can you use the inverted check "isinstance(p, str)"? It's more likely that you'll want to pass a file-like object than a string-like object. This would work on Python 2 as well, though it's semantically different; to safely check for both Unicode and bytes strings on both Py2 and Py3, this may work: # Once-off: try: basestring except NameError: basestring = (str, bytes) # Is p a string? if isinstance(p, basestring): pass It abuses the fact that isinstance will happily accept the 'basestring' common supertype of both strings in Python 2, but will equally happily accept a tuple of types. ChrisA From __peter__ at web.de Thu Nov 8 08:02:23 2012 From: __peter__ at web.de (Peter Otten) Date: Thu, 08 Nov 2012 14:02:23 +0100 Subject: isinstance(.., file) for Python 3 References: <2pjsm9-p8h.ln1@satorlaser.homedns.org> Message-ID: Ulrich Eckhardt wrote: > Hi! > > I have two problems that are related and that I'd like to solve together. > > Firstly, I have code that allows either a file or a string representing > its content as parameter. If the parameter is a file, the content is > read from the file. In Python 2, I used "isinstance(p, file)" to > determine whether the parameter p is a file. In Python 3, the > returnvalue of open() is of type _io.TextIOWrapper, while the built-in > class file doesn't exist, so I can't use that code. > > Secondly, checking for the type is kind-of ugly, because it means that I > can't use an object that fits but that doesn't have the right type. In > other words, it breaks duck-typing. This is already broken in the Python > 2 code, but since I have to touch the code anyway, I might as well fix > it on the way. > > If possible, I'm looking for a solution that works for Pythons 2 and 3, > since I'm not fully through the conversion yet and have clients that > might use the older snake for some time before shedding their skin. > > Suggestions? In order of obviousness: hasattr(p, "read") not isinstance(p, str) iter(p) is p Or you change the interface def f(*, contents=None, file=None): if contents is None: with open(file) as f: contents = f.read() ... # work with contents From as at sci.fi Thu Nov 8 08:09:22 2012 From: as at sci.fi (Anssi Saari) Date: Thu, 08 Nov 2012 15:09:22 +0200 Subject: Numpy module References: <5a86d3fc-1dff-4119-bda9-aa6e631758d5@googlegroups.com> Message-ID: farrellpolymath at gmail.com writes: > Hello to the group! > > I've learned a lot about Ubuntu just trying to install numpy for Python 3.2.3. I've finally managed to put it in the Python3.2 directory but when I try to import it, I still get there's "no module named numpy." There are other modules in the same directory, like 'email' and it imports fine. > > Does Numpy 1.6.2 not run with Python 3.2.3? > > Can anybody help? Thank you in advance. What's the goal of this exercise? Ubuntu packages Python 3 and Numpy so all you need to do is install packages python3 and python3-numpy. From wrw at mac.com Thu Nov 8 08:26:52 2012 From: wrw at mac.com (wrw at mac.com) Date: Thu, 08 Nov 2012 08:26:52 -0500 Subject: Multi-dimensional list initialization In-Reply-To: <509B3A38.3000109@r3dsolutions.com> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <50999214.50100@r3dsolutions.com> <509ABBD4.7020200@r3dsolutions.com> <509af632$0$29980$c3e8da3$5496439d@news.astraweb.com> <509B3A38.3000109@r3dsolutions.com> Message-ID: <352D70D2-BC31-41C9-81ED-149BA7D873CB@mac.com> On Nov 7, 2012, at 11:51 PM, Andrew Robinson wrote: > On 11/07/2012 04:00 PM, Steven D'Aprano wrote: >> Andrew, it appears that your posts are being eaten or rejected by my >> ISP's news server, because they aren't showing up for me. Possibly a side- >> effect of your dates being in the distant past? > Date has been corrected since two days ago. It will remain until a reboot.... > Ignorance, though, might be bliss... > >> Every now and again I come across somebody who tries to distinguish between "call by foo" and "pass by foo", but nobody has been able to explain the difference (if any) to me. > I think the "Call by foo" came into vogue around the time of C++; Eg: It's in books like C++ for C programmers; I never saw it used before then so I *really* don't know for sure? > Just as an aside - there is a famous quote from Niklaus Wirt, who, when asked how he pronounced his name, is said to have replied: "Well you can call me by name, Veert, or you can call me by value, Worth." That would have been sometime in the early 60s, when he was at Stanford. -Bill From insideshoes at gmail.com Thu Nov 8 08:47:37 2012 From: insideshoes at gmail.com (inshu chauhan) Date: Thu, 8 Nov 2012 14:47:37 +0100 Subject: error In-Reply-To: <78f26f00-2c91-47b3-aef0-3eaeb8ae6f91@u4g2000pbo.googlegroups.com> References: <78f26f00-2c91-47b3-aef0-3eaeb8ae6f91@u4g2000pbo.googlegroups.com> Message-ID: Actually data is neither a zanzibar nor a class nor a list.. its a yml image. with pixels I am trying to access pixels of a 3D image through this programme.. On Tue, Nov 6, 2012 at 8:59 PM, woooee wrote: > From this line, "data" appears to be a class > if 0 < ix < data.width and 0 < iy < data.height: > From this line, "data" appears to be a list, although a two > dimensional list would be accessed as data[ix][iy] > point = data[ix, iy] > > Also, returning a list from a function is a matter of preference. > Some people argue that it should be returned to make it obvious. If > you do not know the difference between what is mutable and what is not > mutable, then return everything until you do. > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From duncan.booth at invalid.invalid Thu Nov 8 08:58:11 2012 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 8 Nov 2012 13:58:11 GMT Subject: isinstance(.., file) for Python 3 References: <2pjsm9-p8h.ln1@satorlaser.homedns.org> Message-ID: Ulrich Eckhardt wrote: > If possible, I'm looking for a solution that works for Pythons 2 and 3, > since I'm not fully through the conversion yet and have clients that > might use the older snake for some time before shedding their skin. > > Suggestions? Why bother checking types at all? def foo(file_or_string): try: data = file_or_string.read() except AttributeError: data = file_or_string ... use data ... -- Duncan Booth http://kupuguy.blogspot.com From ASchneiderman at asha.org Thu Nov 8 09:00:43 2012 From: ASchneiderman at asha.org (Anders Schneiderman) Date: Thu, 8 Nov 2012 09:00:43 -0500 Subject: Right solution to unicode error? In-Reply-To: References: <09a3d20b-5871-47f4-9218-df119698e405@m4g2000yqf.googlegroups.com> Message-ID: <93268EE5694C0A4BBB888C2EEFEEEC220A80E90862@EXCH2008.hq.asha.org> Thanks, Oscar and Ramit! This is exactly what I was looking for. Anders > -----Original Message----- > From: Oscar Benjamin [mailto:oscar.j.benjamin at gmail.com] > Sent: Wednesday, November 07, 2012 6:27 PM > To: Anders Schneiderman > Cc: python-list at python.org > Subject: Re: Right solution to unicode error? > > On 7 November 2012 22:17, Anders wrote: > > > > Traceback (most recent call last): > > File "outlook_tasks.py", line 66, in > > my_tasks.dump_today_tasks() > > File "C:\Users\Anders\code\Task List\tasks.py", line 29, in > > dump_today_tasks > > print task.subject > > UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in > > position 42: ordinal not in range(128) > > > > Here's where I'm getting stuck. In the code above I was just printing > > the subject so I can see whether the script is working properly. > > Ultimately what I want to do is parse the tasks I'm interested in and > > then create an HTML file containing those tasks. Given that, what's > > the best way to fix this problem? > > Are you using cmd.exe (standard Windows terminal)? If so, it does not > support unicode and Python is telling you that it cannot encode the string in a > way that can be understood by your terminal. You can try using chcp to set > the code page to something that works with your script. > > If you are only printing it for debugging purposes you can just print the repr() > of the string which will be ascii and will come out fine in your terminal. If you > want to write it to a html file you should encode the string with whatever > encoding (probably utf-8) you use in the html file. If you really just want your > script to be able to print unicode characters then you need to use something > other than cmd.exe (such as IDLE). > > > Oscar From oscar.j.benjamin at gmail.com Thu Nov 8 09:06:42 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 8 Nov 2012 14:06:42 +0000 Subject: Right solution to unicode error? In-Reply-To: References: <09a3d20b-5871-47f4-9218-df119698e405@m4g2000yqf.googlegroups.com> <509AF3EF.8050108@gmail.com> Message-ID: On 8 November 2012 00:44, Oscar Benjamin wrote: > On 7 November 2012 23:51, Andrew Berg wrote: >> On 2012.11.07 17:27, Oscar Benjamin wrote: >>> Are you using cmd.exe (standard Windows terminal)? If so, it does not >>> support unicode >> Actually, it does. Code page 65001 is UTF-8. I know that doesn't help >> the OP since Python versions below 3.3 don't support cp65001, but I >> think it's important to point out that the Windows command line system >> (it is not unique to cmd) does in fact support Unicode. > > I have tried to use code page 65001 and it didn't work for me even if > I did use a version of Python (possibly 3.3 alpha) that claimed to > support it. I stand corrected. I've just checked and codepage 65001 does work in cmd.exe (on this machine): O:\>Q:\tools\Python33\python -c print('abc\u2013def') Traceback (most recent call last): File "", line 1, in File "Q:\tools\Python33\lib\encodings\cp850.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_map)[0] UnicodeEncodeError: 'charmap' codec can't encode character '\u2013' in position 3: character maps to O:\>chcp 65001 Active code page: 65001 O:\>Q:\tools\Python33\python -c print('abc\u2013def') abc-def O:\>Q:\tools\Python33\python -c print('\u03b1') ? It would be a lot better though if it just worked straight away without me needing to set the code page (like the terminal in every other OS I use). Oscar From cjw at ncf.ca Thu Nov 8 09:24:25 2012 From: cjw at ncf.ca (Colin J. Williams) Date: Thu, 08 Nov 2012 09:24:25 -0500 Subject: Numpy module In-Reply-To: References: <5a86d3fc-1dff-4119-bda9-aa6e631758d5@googlegroups.com> Message-ID: On 08/11/2012 8:09 AM, Anssi Saari wrote: > farrellpolymath at gmail.com writes: > >> [snip] >> >> Does Numpy 1.6.2 not run with Python 3.2.3? >> It does on the Raspberry Pi, which uses a variant of Debian. Colin W. From kdawg44 at gmail.com Thu Nov 8 09:25:47 2012 From: kdawg44 at gmail.com (Kevin Holleran) Date: Thu, 8 Nov 2012 09:25:47 -0500 Subject: Executing .exe on a remote Windows machine Message-ID: Good morning, I wrote a python script to connect out to a bunch of my remote machines that are running some software. It modifies a bunch of the config files for me. After making the changes, I need to restart the software. The way to do this is to call an .exe passing in a argument 'restart' Simply restarting services is NOT acceptable & rebooting the machine isn't either. I was trying to find a way to simply call the .exe on the remote machine with subprocess but how can I get it to execute on the remote machine? These machines do not have SSH. Is there a way to do this or am I stuck coordinating a time when the machines can be rebooted? (because this would be faster than asking anyone on site to execute the command....) Thanks for your help. Kevin -------------- next part -------------- An HTML attachment was scrubbed... URL: From mail at timgolden.me.uk Thu Nov 8 09:31:31 2012 From: mail at timgolden.me.uk (Tim Golden) Date: Thu, 08 Nov 2012 14:31:31 +0000 Subject: Executing .exe on a remote Windows machine In-Reply-To: References: Message-ID: <509BC243.6040407@timgolden.me.uk> On 08/11/2012 14:25, Kevin Holleran wrote: > Good morning, > > I wrote a python script to connect out to a bunch of my remote machines > that are running some software. It modifies a bunch of the config files > for me. After making the changes, I need to restart the software. The > way to do this is to call an .exe passing in a argument 'restart' > Simply restarting services is NOT acceptable & rebooting the machine > isn't either. > > I was trying to find a way to simply call the .exe on the remote machine > with subprocess but how can I get it to execute on the remote machine? > These machines do not have SSH. WMI can usually help with this (although there are limitations on what you can execute via WMI). Also people recommend sysinternals' psexec. (I've never tried it myself). TJG From kdawg44 at gmail.com Thu Nov 8 09:43:01 2012 From: kdawg44 at gmail.com (Kevin Holleran) Date: Thu, 8 Nov 2012 09:43:01 -0500 Subject: Executing .exe on a remote Windows machine In-Reply-To: <509BC243.6040407@timgolden.me.uk> References: <509BC243.6040407@timgolden.me.uk> Message-ID: My goodness.... psexec. thanks.... can't believe that didn't come to me... -- Kevin Holleran Master of Science, Computer Information Systems Grand Valley State University Master of Business Administration Western Michigan University SANS GCFE, CCNA, ISA, MCSA, MCDST, MCP My Paleo & Fitness Blog "Do today what others won't, do tomorrow what others can't" - SEALFit "We are what we repeatedly do. Excellence, then, is not an act, but a habit." - Aristotle On Thu, Nov 8, 2012 at 9:31 AM, Tim Golden wrote: > On 08/11/2012 14:25, Kevin Holleran wrote: > > Good morning, > > > > I wrote a python script to connect out to a bunch of my remote machines > > that are running some software. It modifies a bunch of the config files > > for me. After making the changes, I need to restart the software. The > > way to do this is to call an .exe passing in a argument 'restart' > > Simply restarting services is NOT acceptable & rebooting the machine > > isn't either. > > > > I was trying to find a way to simply call the .exe on the remote machine > > with subprocess but how can I get it to execute on the remote machine? > > These machines do not have SSH. > > WMI can usually help with this (although there are limitations on what > you can execute via WMI). Also people recommend sysinternals' psexec. > (I've never tried it myself). > > TJG > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From wxjmfauth at gmail.com Thu Nov 8 10:05:14 2012 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Thu, 8 Nov 2012 07:05:14 -0800 (PST) Subject: Right solution to unicode error? In-Reply-To: References: <09a3d20b-5871-47f4-9218-df119698e405@m4g2000yqf.googlegroups.com> <509AF3EF.8050108@gmail.com> Message-ID: <65910cea-f145-409c-a579-9f0cda499546@googlegroups.com> Le jeudi 8 novembre 2012 15:07:23 UTC+1, Oscar Benjamin a ?crit?: > On 8 November 2012 00:44, Oscar Benjamin wrote: > > > On 7 November 2012 23:51, Andrew Berg wrote: > > >> On 2012.11.07 17:27, Oscar Benjamin wrote: > > >>> Are you using cmd.exe (standard Windows terminal)? If so, it does not > > >>> support unicode > > >> Actually, it does. Code page 65001 is UTF-8. I know that doesn't help > > >> the OP since Python versions below 3.3 don't support cp65001, but I > > >> think it's important to point out that the Windows command line system > > >> (it is not unique to cmd) does in fact support Unicode. > > > > > > I have tried to use code page 65001 and it didn't work for me even if > > > I did use a version of Python (possibly 3.3 alpha) that claimed to > > > support it. > > > > I stand corrected. I've just checked and codepage 65001 does work in > > cmd.exe (on this machine): > > > > O:\>Q:\tools\Python33\python -c print('abc\u2013def') > > Traceback (most recent call last): > > File "", line 1, in > > File "Q:\tools\Python33\lib\encodings\cp850.py", line 19, in encode > > return codecs.charmap_encode(input,self.errors,encoding_map)[0] > > UnicodeEncodeError: 'charmap' codec can't encode character '\u2013' in > > position 3: character maps to > > > > > > O:\>chcp 65001 > > Active code page: 65001 > > > > O:\>Q:\tools\Python33\python -c print('abc\u2013def') > > abc-def > > > > > > O:\>Q:\tools\Python33\python -c print('\u03b1') > > ? > > > > It would be a lot better though if it just worked straight away > > without me needing to set the code page (like the terminal in every > > other OS I use). > > > > > > Oscar ---------- It *WORKS* straight away. The problem is that people do not wish to use unicode correctly (eg. Mulder's example). Read the point 1) and 4) in my previous post. Unicode and in general the coding of the characters have nothing to do with the os's or programming languages. jmf From stefan_ml at behnel.de Thu Nov 8 10:14:37 2012 From: stefan_ml at behnel.de (Stefan Behnel) Date: Thu, 08 Nov 2012 16:14:37 +0100 Subject: isinstance(.., file) for Python 3 In-Reply-To: References: <2pjsm9-p8h.ln1@satorlaser.homedns.org> Message-ID: Duncan Booth, 08.11.2012 14:58: > Ulrich Eckhardt wrote: >> If possible, I'm looking for a solution that works for Pythons 2 and 3, >> since I'm not fully through the conversion yet and have clients that >> might use the older snake for some time before shedding their skin. >> >> Suggestions? > > Why bother checking types at all? > > def foo(file_or_string): > try: > data = file_or_string.read() > except AttributeError: > data = file_or_string > ... use data ... Or, a tiny bit more safely: try: read = file_or_string.read except AttributeError: data = file_or_string else: data = read() I'd rather go with one of the previous solutions, though. Stefan From kdawg44 at gmail.com Thu Nov 8 10:37:35 2012 From: kdawg44 at gmail.com (Kevin Holleran) Date: Thu, 8 Nov 2012 10:37:35 -0500 Subject: Executing .exe on a remote Windows machine In-Reply-To: References: <509BC243.6040407@timgolden.me.uk> Message-ID: On Thu, Nov 8, 2012 at 9:43 AM, Kevin Holleran wrote: > My goodness.... psexec. > > thanks.... can't believe that didn't come to me... > > > > > On Thu, Nov 8, 2012 at 9:31 AM, Tim Golden wrote: > >> On 08/11/2012 14:25, Kevin Holleran wrote: >> > Good morning, >> > >> > I wrote a python script to connect out to a bunch of my remote machines >> > that are running some software. It modifies a bunch of the config files >> > for me. After making the changes, I need to restart the software. The >> > way to do this is to call an .exe passing in a argument 'restart' >> > Simply restarting services is NOT acceptable & rebooting the machine >> > isn't either. >> > >> > I was trying to find a way to simply call the .exe on the remote machine >> > with subprocess but how can I get it to execute on the remote machine? >> > These machines do not have SSH. >> >> WMI can usually help with this (although there are limitations on what >> you can execute via WMI). Also people recommend sysinternals' psexec. >> (I've never tried it myself). >> >> TJG >> -- >> http://mail.python.org/mailman/listinfo/python-list >> > > OK, not quite resolved yet.... My code.... [code] try: print("Attempting to restart Splunk...") subprocess.call(["psexec", "\\\\" + host, "'c:\\Program Files\\Splunk\\bin\\splunk.exe'", "restart"]) [/code] & am getting: [output] Attempting to restart Splunk... PsExec v1.98 - Execute processes remotely Copyright (C) 2001-2010 Mark Russinovich Sysinternals - www.sysinternals.com PsExec could not start 'c:\Program Files\Splunk\bin\splunk.exe' restart on [IP_ADDRESS]: The filename, directory name, or volume label syntax is incorrect. [/output] I am simply trying to restart the splunk forwarder instance.... Any thoughts?? Thanks. Kevin -------------- next part -------------- An HTML attachment was scrubbed... URL: From ulrich.eckhardt at dominolaser.com Thu Nov 8 10:55:16 2012 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Thu, 08 Nov 2012 16:55:16 +0100 Subject: int.__init__ incompatible in Python 3.3 Message-ID: <481tm9-k6i.ln1@satorlaser.homedns.org> Hi! Preparing for an upgrade from 2.7 to 3, I stumbled across an incompatibility between 2.7 and 3.2 on one hand and 3.3 on the other: class X(int): def __init__(self, value): super(X, self).__init__(value) X(42) On 2.7 and 3.2, the above code works. On 3.3, it gives me a "TypeError: object.__init__() takes no parameters". To some extent, this makes sense to me, because the int subobject is not initialized in __init__ but in __new__. As a workaround, I can simple drop the parameter from the call. However, breaking backward compatibility is another issue, so I wonder if that should be considered as a bug. Bug? Feature? Other suggestions? Uli From ian.g.kelly at gmail.com Thu Nov 8 10:58:35 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 8 Nov 2012 08:58:35 -0700 Subject: Multi-dimensional list initialization In-Reply-To: <509B6CCD.2010302@r3dsolutions.com> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <5099bf7d$0$29980$c3e8da3$5496439d@news.astraweb.com> <509ADA6E.10403@r3dsolutions.com> <509B2355.60407@r3dsolutions.com> <509B6CCD.2010302@r3dsolutions.com> Message-ID: On Thu, Nov 8, 2012 at 1:26 AM, Andrew Robinson wrote: > OK: Then copy by reference using map....: > > values = zip( map( lambda:times, xrange(num_groups) ) ) > if len(values) < len(times) * num_groups ... > > Done. It's clearer than a list comprehension and you still really don't > need a list multiply. That is not equivalent to the original. Even had you not omitted some parts: values = zip(samples, map(lambda i: times, range(num_groups))) This still has the problem that map returns a list of num_groups elements, each of which is times. The desired value to be passed into zip is a *single* sequence containing len(times) * num_groups elements. This is easily handled by list multiplication, but not so easily by map or by a single list comprehension. Looking back at the 'ini' solution you proposed before, I see that this also would be a problem there. Fixing the above, it would have to be something like: values = zip(samples, reduce(operator.add, map(lambda i: times, range(num_groups)), [])) Or from how I understand the 'ini' syntax to work: values = zip(samples, reduce(operator.add, [lambda: times, ini xrange(num_groups)], [])) Which brings to mind another point that I want to get to in a moment. But when I said that I would use map instead, I meant that *if* the body of the list comprehension is just a function application, then I would prefer to use map over the list comprehension. But in the above I see no benefit in using a lambda in the first place. Getting back to that other point, notice what we ended up doing in both of those constructions above: repeated list concatenation as a substitute for multiplication. In fact, when we multiply (aList * 5), this should be the equivalent of (aList + aList + aList + aList +aList), should it not? Clearly, however, there should be no implicit copying involved in mere list concatenation. For one thing, if the user wants to concatenate copies, that is quite easily done explicitly: (aList[:] + aList[:]) instead of (aList + aList). For another, list concatenation is less likely to be used for an initialization process. If list multiplication were to copy nested lists, then, this would break the intuitive notion that list multiplication is equivalent to repeated list concatenation. > Yes, but you're very blind to history and code examples implementing the > slice operation. > slice usually depends on index; index does not depend on slice. > Slice is suggested to be implemented by multiple calls to single indexes in > traditional usage and documentation. ...and then by composing the elements located at those indexes into a subsequence. > The xrange(,,)[:] implementation breaks the tradition, because it doesn't > call index multiple times; nor does it return a result equivalent identical > to doing that. Whether any given __getitem__ slicing implementation recursively calls __getitem__ with a series of indexes or not is an implementation detail. If it were possible to index a range object multiple times and then stuff the results into another range object, then the slicing result would be equivalent. The only reason it is not is that you cannot construct a range object in that fashion. I think that what you're expecting is that range(5)[:] should return a list in Python 3 because it returns a list in Python 2. This does not represent a change in slicing behavior -- in fact, all you got by slicing an xrange object in Python 2 was a TypeError. This represents an intentional break in backward compatibility between Python 2 and Python 3, which was the purpose of Python 3 -- to fix a lot of existing warts in Python by breaking them all at once, rather than progressively over a long string of versions. Users porting their scripts from Python 2 to Python 3 are advised to replace "range(...)" with "list(range(...))" if what they actually want is a list, and I believe the 2to3 tool does this automatically. Once the range object is converted to a list, there is no further break with Python 2 -- slicing a list gives you a list, just as it always has. In a nutshell, yes: range(...)[:] produces a different result in Python 3 than in Python 2, just as it does without the slicing operation tacked on. It was never intended that scripts written for Python 2 should be able to run in Python 3 unchanged without careful attention to detail. From mail at timgolden.me.uk Thu Nov 8 11:21:40 2012 From: mail at timgolden.me.uk (Tim Golden) Date: Thu, 08 Nov 2012 16:21:40 +0000 Subject: Executing .exe on a remote Windows machine In-Reply-To: References: <509BC243.6040407@timgolden.me.uk> Message-ID: <509BDC14.1040203@timgolden.me.uk> On 08/11/2012 15:37, Kevin Holleran wrote: > [code] > try: > print("Attempting to restart Splunk...") > subprocess.call(["psexec", "\\\\" + host, "'c:\\Program > Files\\Splunk\\bin\\splunk.exe'", "restart"]) > [/code] > > & am getting: > > [output] > Attempting to restart Splunk... > > PsExec v1.98 - Execute processes remotely > Copyright (C) 2001-2010 Mark Russinovich > Sysinternals - www.sysinternals.com > > > PsExec could not start 'c:\Program Files\Splunk\bin\splunk.exe' restart > on [IP_ADDRESS]: > The filename, directory name, or volume label syntax is incorrect. > [/output] > > I am simply trying to restart the splunk forwarder instance.... > > Any thoughts?? Lose the nested quotes in the .exe path; subprocess will take care of that: subprocess.call(["psexec", "\\\\" + host, "c:\\Program Files\\Splunk\\bin\\splunk.exe", "restart"]) TJG From clp2 at rebertia.com Thu Nov 8 11:32:41 2012 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 8 Nov 2012 08:32:41 -0800 Subject: Executing .exe on a remote Windows machine In-Reply-To: References: <509BC243.6040407@timgolden.me.uk> Message-ID: On Thursday, November 8, 2012, Kevin Holleran wrote: > On Thu, Nov 8, 2012 at 9:43 AM, Kevin Holleran > > wrote: > >> My goodness.... psexec. >> >> thanks.... can't believe that didn't come to me... >> >> >> >> >> On Thu, Nov 8, 2012 at 9:31 AM, Tim Golden >> > wrote: >> >>> On 08/11/2012 14:25, Kevin Holleran wrote: >>> > Good morning, >>> > >>> > I wrote a python script to connect out to a bunch of my remote machines >>> > that are running some software. It modifies a bunch of the config >>> files >>> > for me. After making the changes, I need to restart the software. The >>> > way to do this is to call an .exe passing in a argument 'restart' >>> > Simply restarting services is NOT acceptable & rebooting the machine >>> > isn't either. >>> > >>> > I was trying to find a way to simply call the .exe on the remote >>> machine >>> > with subprocess but how can I get it to execute on the remote machine? >>> > These machines do not have SSH. >>> >>> WMI can usually help with this (although there are limitations on what >>> you can execute via WMI). Also people recommend sysinternals' psexec. >>> (I've never tried it myself). >>> >>> TJG >>> -- >>> http://mail.python.org/mailman/listinfo/python-list >>> >> >> > > OK, not quite resolved yet.... > > My code.... > > [code] > try: > print("Attempting to restart Splunk...") > subprocess.call(["psexec", "\\\\" + host, "'c:\\Program > Files\\Splunk\\bin\\splunk.exe'", "restart"]) > [/code] > > & am getting: > > [output] > Attempting to restart Splunk... > > PsExec v1.98 - Execute processes remotely > Copyright (C) 2001-2010 Mark Russinovich > Sysinternals - www.sysinternals.com > > > PsExec could not start 'c:\Program Files\Splunk\bin\splunk.exe' restart on > [IP_ADDRESS]: > The filename, directory name, or volume label syntax is incorrect. > [/output] > > I am simply trying to restart the splunk forwarder instance.... > > Any thoughts?? > Remove the apostrophes surrounding the path to Splunk's executable. The subprocess module already takes care of the quoting for you, so the apostrophes are unnecessary and are being interpreted literally. -- Cheers, Chris -- http://rebertia.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Thu Nov 8 11:33:53 2012 From: d at davea.name (Dave Angel) Date: Thu, 08 Nov 2012 11:33:53 -0500 Subject: error In-Reply-To: References: <78f26f00-2c91-47b3-aef0-3eaeb8ae6f91@u4g2000pbo.googlegroups.com> Message-ID: <509BDEF1.50706@davea.name> On 11/08/2012 08:47 AM, inshu chauhan wrote: > Actually data is neither a zanzibar nor a class nor a list.. its a yml > image. with pixels > I am trying to access pixels of a 3D image through this programme.. > You want us to keep guessing? And without supplying any new information? There's no such thing as a yml image in Python, though there might be some library that supports such a thing with some class structure. And you just might have downloaded it from the http://www.ymlgroup.com/obscurehiddenlocation/downloads site, and imported it using from notzanzibar import DataType. Then you'd have instantiated it in some code like data = DataType(filename) and then the type of data would be notzanzibar.DataType and the docs would be probably available somewhere on www.invalid.com/docs The only new guess: A 3D image would presumably have 3 subscripts, and you're only supplying two. If you want help, be explicit: 1) what version of CPython are you using, and on what OS? 2) what web site did you download some extra library from ? 3) what import statement did you use ? 4) How are all those global variables initialized ? 5) What command did you use to start the script ? Did you run it from command.com, or from some IDE ? 5) Exactly what error message did you get, including the traceback ? 6) What have you done to try to figure out your own error? -- DaveA From kdawg44 at gmail.com Thu Nov 8 11:40:50 2012 From: kdawg44 at gmail.com (Kevin Holleran) Date: Thu, 8 Nov 2012 11:40:50 -0500 Subject: Executing .exe on a remote Windows machine In-Reply-To: References: <509BC243.6040407@timgolden.me.uk> Message-ID: On Thu, Nov 8, 2012 at 11:32 AM, Chris Rebert wrote: > On Thursday, November 8, 2012, Kevin Holleran wrote: > >> On Thu, Nov 8, 2012 at 9:43 AM, Kevin Holleran wrote: >> >>> My goodness.... psexec. >>> >>> thanks.... can't believe that didn't come to me... >>> >>> >>> >>> >>> On Thu, Nov 8, 2012 at 9:31 AM, Tim Golden wrote: >>> >>>> On 08/11/2012 14:25, Kevin Holleran wrote: >>>> > Good morning, >>>> > >>>> > I wrote a python script to connect out to a bunch of my remote >>>> machines >>>> > that are running some software. It modifies a bunch of the config >>>> files >>>> > for me. After making the changes, I need to restart the software. >>>> The >>>> > way to do this is to call an .exe passing in a argument 'restart' >>>> > Simply restarting services is NOT acceptable & rebooting the machine >>>> > isn't either. >>>> > >>>> > I was trying to find a way to simply call the .exe on the remote >>>> machine >>>> > with subprocess but how can I get it to execute on the remote machine? >>>> > These machines do not have SSH. >>>> >>>> WMI can usually help with this (although there are limitations on what >>>> you can execute via WMI). Also people recommend sysinternals' psexec. >>>> (I've never tried it myself). >>>> >>>> TJG >>>> -- >>>> http://mail.python.org/mailman/listinfo/python-list >>>> >>> >>> >> >> OK, not quite resolved yet.... >> >> My code.... >> >> [code] >> try: >> print("Attempting to restart Splunk...") >> subprocess.call(["psexec", "\\\\" + host, "'c:\\Program >> Files\\Splunk\\bin\\splunk.exe'", "restart"]) >> [/code] >> >> & am getting: >> >> [output] >> Attempting to restart Splunk... >> >> PsExec v1.98 - Execute processes remotely >> Copyright (C) 2001-2010 Mark Russinovich >> Sysinternals - www.sysinternals.com >> >> >> PsExec could not start 'c:\Program Files\Splunk\bin\splunk.exe' restart >> on [IP_ADDRESS]: >> The filename, directory name, or volume label syntax is incorrect. >> [/output] >> >> I am simply trying to restart the splunk forwarder instance.... >> >> Any thoughts?? >> > > Remove the apostrophes surrounding the path to Splunk's executable. The > subprocess module already takes care of the quoting for you, so the > apostrophes are unnecessary and are being interpreted literally. > > > -- > Cheers, > Chris > -- > http://rebertia.com > Thanks Tim & Chris... you guys are my heroes for today.... :) -------------- next part -------------- An HTML attachment was scrubbed... URL: From malaclypse2 at gmail.com Thu Nov 8 11:41:22 2012 From: malaclypse2 at gmail.com (Jerry Hill) Date: Thu, 8 Nov 2012 11:41:22 -0500 Subject: error In-Reply-To: References: <78f26f00-2c91-47b3-aef0-3eaeb8ae6f91@u4g2000pbo.googlegroups.com> Message-ID: On Thu, Nov 8, 2012 at 8:47 AM, inshu chauhan wrote: > Actually data is neither a zanzibar nor a class nor a list.. its a yml > image. with pixels > I am trying to access pixels of a 3D image through this programme.. When you're having trouble debugging a program, and decide to turn to a mailing list for help, it can be hard to know what information people are going to need to help you. The easiest way to know that people have enough information to help debug your program is to provide a short, self contained, example of your code that demonstrates the problem that you're having. See http://sscce.org/ for more on why it's helpful, and how to turn a long program into sample code that will let us help you. -- Jerry From silideba at gmail.com Thu Nov 8 12:05:40 2012 From: silideba at gmail.com (Debashish Saha) Date: Thu, 8 Nov 2012 22:35:40 +0530 Subject: accuracy problem in calculation Message-ID: (1500000000+1.00067968)-(1500000000+1.00067961) Out[102]: 2.384185791015625e-07 1.00067968-(1.00067961) Out[103]: 7.000000001866624e-08 above i am showing the two different results,though the two outputs should be same if we do it in copy(the lass one is acceptable value). so my question is how to increase the accuracy(windows7(32bit) ,python2.7.2) From ian.g.kelly at gmail.com Thu Nov 8 12:13:09 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 8 Nov 2012 10:13:09 -0700 Subject: int.__init__ incompatible in Python 3.3 In-Reply-To: <481tm9-k6i.ln1@satorlaser.homedns.org> References: <481tm9-k6i.ln1@satorlaser.homedns.org> Message-ID: On Thu, Nov 8, 2012 at 8:55 AM, Ulrich Eckhardt wrote: > Hi! > > Preparing for an upgrade from 2.7 to 3, I stumbled across an incompatibility > between 2.7 and 3.2 on one hand and 3.3 on the other: > > class X(int): > def __init__(self, value): > super(X, self).__init__(value) > X(42) > > On 2.7 and 3.2, the above code works. On 3.3, it gives me a "TypeError: > object.__init__() takes no parameters". To some extent, this makes sense to > me, because the int subobject is not initialized in __init__ but in __new__. > As a workaround, I can simple drop the parameter from the call. However, > breaking backward compatibility is another issue, so I wonder if that should > be considered as a bug. > > Bug? Feature? Other suggestions? A similar change was made to object.__init__ in 2.6, so this could just be bringing the behavior of int into line with object. There's nothing about it in the whatsnew document, though. I say open a bug report and let the devs sort it out. From rosuav at gmail.com Thu Nov 8 12:13:33 2012 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 9 Nov 2012 04:13:33 +1100 Subject: accuracy problem in calculation In-Reply-To: References: Message-ID: On Fri, Nov 9, 2012 at 4:05 AM, Debashish Saha wrote: > (1500000000+1.00067968)-(1500000000+1.00067961) > Out[102]: 2.384185791015625e-07 > > 1.00067968-(1.00067961) > Out[103]: 7.000000001866624e-08 > > above i am showing the two different results,though the two outputs > should be same if we do it in copy(the lass one is acceptable value). > so my question is how to increase the accuracy(windows7(32bit) > ,python2.7.2) Welcome to floating point. You're working with very large and very small numbers, and you _will_ lose accuracy. There are a few options. It's possible that a 64-bit build of Python will give you more accuracy, but better would be to separate your huge numbers from your tiny ones and work with them separately. Alternatively, switch to the Decimal or Fraction types, but be aware that your script will probably run a lot slower. >>> from decimal import Decimal >>> (Decimal("1500000000")+Decimal("1.00067968"))-(Decimal("1500000000")+Decimal("1.00067961")) Decimal('7E-8') >>> Decimal("1.00067968")-Decimal("1.00067961") Decimal('7E-8') Unless something's tying you to Python 2, consider moving to Python 3. You may find that, on Python 3.3, you can switch to Decimal without losing too much performance. ChrisA From andriy.kornatskyy at live.com Thu Nov 8 12:30:02 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Thu, 8 Nov 2012 20:30:02 +0300 Subject: duck typing assert Message-ID: People who come from strongly typed languages that offer interfaces often are confused by lack of one in Python. Python, being dynamic typing programming language, follows duck typing principal. It can as simple as this: assert looks(Foo).like(IFoo) The post below shows how programmer can assert duck typing between two Python classes: mindref.blogspot.com/2012/11/python-duck-typing-assert.html Comments or suggestions are welcome. Thanks. Andriy Kornatskyy From andriy.kornatskyy at live.com Thu Nov 8 12:34:58 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Thu, 8 Nov 2012 20:34:58 +0300 Subject: =?windows-1256?Q?duck_typin?= =?windows-1256?Q?g_assert=FE?= Message-ID: People who come from strongly typed languages that offer interfaces often are confused by lack of one in Python. Python, being dynamic typing programming language, follows duck typing principal. It can as simple as this: ? assert looks(Foo).like(IFoo) ? The post below shows how programmer can assert duck typing between two Python classes: ? http://mindref.blogspot.com/2012/11/python-duck-typing-assert.html ? Comments or suggestions are welcome. ? Thanks. ? Andriy Kornatskyy From d at davea.name Thu Nov 8 12:37:52 2012 From: d at davea.name (Dave Angel) Date: Thu, 08 Nov 2012 12:37:52 -0500 Subject: accuracy problem in calculation In-Reply-To: References: Message-ID: <509BEDF0.6070806@davea.name> On 11/08/2012 12:05 PM, Debashish Saha wrote: > (1500000000+1.00067968)-(1500000000+1.00067961) > Out[102]: 2.384185791015625e-07 > > 1.00067968-(1.00067961) > Out[103]: 7.000000001866624e-08 > > above i am showing the two different results,though the two outputs > should be same if we do it in copy(the lass one is acceptable value). > so my question is how to increase the accuracy(windows7(32bit) > ,python2.7.2) To improve accuracy, do some algebra before coding brute force. If you know you'll be subtracting two values that are nearly equal, see if you can factor out the large part, and only subtract the small parts. If you have a processor with 16 digits of float accuracy, and you calculate an 18 digit sum, you're going to lose 3 digts at a minimum. Each sum has the same problem. So now you have lopped off all the digits that are different. So subtracting them is meaningless. Picture needing to know how thick a leaf is. So measure the distance from one side of it to the sun. Then measure the distance from the other side to the sun. Now just subtract the answers. Silly, isn't it? You have (a+b) - (a+c), where a is much larger than either b or c. So simplify it to b-c before programming it. Oh, yeah, that's your second approach. More generally, if you have a sum of 3 or more values (which you can get by just stating the problem as a + b + -a + -c), you can usually minimize error by reordering the arithmetic, based on the sizes of the items. Fortunately Python has a library function that knows how to do that: http://docs.python.org/2/library/math.html#math.fsum If you want to postpone the problem so instead of hitting at 15 digits or so, it hits at a few hundred digits, you could use decimal.Decimal, which is a standard library in Python. It permits you to specify your own precision, instead of being what Intel (professor Kahn) decided on in about 1985. They constrained themselves to what could be encoded in 8 bytes. Naturally, if you ask for much higher precision with Decimal, you'll pay for it with space and/or time. But apparently in 3.3, they did a very good job minimizing the time it takes, for reasonable precision. Interestingly, I got a similar question in about 1977, concerning a trig package I had microcoded. A man was measuring the flatness of a hypothetical level table (which must be curved, of course). And he did it with trig, and effectively by subtracting two measurements to the center of the earth. I was able to simplify his problem using some geometry (similar triangles) to get all the accuracy he needed. And strangely enough, the trig canceled out and was unnecessary. -- DaveA From jkn_gg at nicorp.f9.co.uk Thu Nov 8 13:05:11 2012 From: jkn_gg at nicorp.f9.co.uk (jkn) Date: Thu, 8 Nov 2012 10:05:11 -0800 (PST) Subject: how to simulate tar filename substitution across piped subprocess.Popen() calls? Message-ID: Hi All i am trying to build up a set of subprocess.Ponen calls to replicate the effect of a horribly long shell command. I'm not clear how I can do one part of this and wonder if anyone can advise. I'm on Linux, fairly obviously. I have a command which (simplified) is a tar -c command piped through to xargs: tar -czvf myfile.tgz -c $MYDIR mysubdir/ | xargs -I '{}' sh -c "test - f $MYDIR/'{}'" (The full command is more complicated than this; I got it from a shell guru). IIUC, when called like this, the two occurences of '{}' in the xargs command will get replaced with the file being added to the tarfile. Also IIUC, I will need two calls to subprocess.Popen() and use subprocess.stdin on the second to receive the output from the first. But how can I achive the substitution of the '{}' construction across these two calls? Apologies if I've made any howlers in this description - it's very likely... Cheers J^n From invalid at invalid.invalid Thu Nov 8 13:17:48 2012 From: invalid at invalid.invalid (Grant Edwards) Date: Thu, 8 Nov 2012 18:17:48 +0000 (UTC) Subject: accuracy problem in calculation References: Message-ID: On 2012-11-08, Chris Angelico wrote: > On Fri, Nov 9, 2012 at 4:05 AM, Debashish Saha wrote: >> (1500000000+1.00067968)-(1500000000+1.00067961) >> Out[102]: 2.384185791015625e-07 >> >> 1.00067968-(1.00067961) >> Out[103]: 7.000000001866624e-08 >> >> above i am showing the two different results,though the two outputs >> should be same if we do it in copy (the lass one is acceptable value). Then do it the way you did the last one. Seriously, that's the answer they teach you in numerical analysis classes. >> so my question is how to increase the accuracy(windows7(32bit) >> ,python2.7.2) > > Welcome to floating point. You're working with very large and very > small numbers, and you _will_ lose accuracy. > > There are a few options. It's possible that a 64-bit build of Python > will give you more accuracy, Pretty doubtful. 64-bit and 32-bit builds on all common OSes and hardware are both going to use 64-bit IEEE floating point. > but better would be to separate your huge numbers from your tiny ones > and work with them separately. > Alternatively, switch to the Decimal or Fraction types, but be aware > that your script will probably run a lot slower. Or admit to yourself that the measurements that produce your input data just aren't that accurate anyway and forget about it. :) -- Grant Edwards grant.b.edwards Yow! Bo Derek ruined at my life! gmail.com From oscar.j.benjamin at gmail.com Thu Nov 8 13:32:11 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 8 Nov 2012 18:32:11 +0000 Subject: Right solution to unicode error? In-Reply-To: <65910cea-f145-409c-a579-9f0cda499546@googlegroups.com> References: <09a3d20b-5871-47f4-9218-df119698e405@m4g2000yqf.googlegroups.com> <509AF3EF.8050108@gmail.com> <65910cea-f145-409c-a579-9f0cda499546@googlegroups.com> Message-ID: On 8 November 2012 15:05, wrote: > Le jeudi 8 novembre 2012 15:07:23 UTC+1, Oscar Benjamin a ?crit : >> On 8 November 2012 00:44, Oscar Benjamin wrote: >> > On 7 November 2012 23:51, Andrew Berg wrote: >> >> On 2012.11.07 17:27, Oscar Benjamin wrote: >> >> >>> Are you using cmd.exe (standard Windows terminal)? If so, it does not >> >>> support unicode >> >> >> Actually, it does. Code page 65001 is UTF-8. I know that doesn't help >> >> the OP since Python versions below 3.3 don't support cp65001, but I >> >> think it's important to point out that the Windows command line system >> >> (it is not unique to cmd) does in fact support Unicode. >> >> > I have tried to use code page 65001 and it didn't work for me even if >> > I did use a version of Python (possibly 3.3 alpha) that claimed to >> > support it. >> >> I stand corrected. I've just checked and codepage 65001 does work in >> cmd.exe (on this machine): >> >> O:\>chcp 65001 >> Active code page: 65001 >> >> O:\>Q:\tools\Python33\python -c print('abc\u2013def') >> abc-def >> >> O:\>Q:\tools\Python33\python -c print('\u03b1') >> ? >> >> It would be a lot better though if it just worked straight away >> without me needing to set the code page (like the terminal in every >> other OS I use). > > It *WORKS* straight away. The problem is that > people do not wish to use unicode correctly > (eg. Mulder's example). > Read the point 1) and 4) in my previous post. > > Unicode and in general the coding of the characters > have nothing to do with the os's or programming languages. I don't know what you mean that it works "straight away". The default code page on my machine is cp850. O:\>chcp Active code page: 850 cp850 doesn't understand utf-8. It just prints garbage: O:\>Q:\tools\Python33\python -c "import sys; sys.stdout.buffer.write('\u03b1\n'.encode('utf-8'))" ?? Using the correct encoding doesn't help: O:\>Q:\tools\Python33\python -c "import sys; sys.stdout.buffer.write('\u03b1\n'.encode('cp850'))" Traceback (most recent call last): File "", line 1, in File "Q:\tools\Python33\lib\encodings\cp850.py", line 12, in encode return codecs.charmap_encode(input,errors,encoding_map) UnicodeEncodeError: 'charmap' codec can't encode character '\u03b1' in position 0: character maps to O:\>Q:\tools\Python33\python -c "import sys; sys.stdout.buffer.write('\u03b1\n'.encode(sys.stdout.en coding))" Traceback (most recent call last): File "", line 1, in File "Q:\tools\Python33\lib\encodings\cp850.py", line 12, in encode return codecs.charmap_encode(input,errors,encoding_map) UnicodeEncodeError: 'charmap' codec can't encode character '\u03b1' in position 0: character maps to If I want the other characters to work I need to change the code page: O:\>chcp 65001 Active code page: 65001 O:\>Q:\tools\Python33\python -c "import sys; sys.stdout.buffer.write('\u03b1\n'.encode('utf-8'))" ? O:\>Q:\tools\Python33\python -c "import sys; sys.stdout.buffer.write('\u03b1\n'.encode(sys.stdout.en coding))" ? Oscar From ian.g.kelly at gmail.com Thu Nov 8 13:34:45 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 8 Nov 2012 11:34:45 -0700 Subject: duck typing assert In-Reply-To: References: Message-ID: On Thu, Nov 8, 2012 at 10:30 AM, Andriy Kornatskyy wrote: > > People who come from strongly typed languages that offer interfaces often are confused by lack of one in Python. Python, being dynamic typing programming language, follows duck typing principal. It can as simple as this: > > assert looks(Foo).like(IFoo) > > The post below shows how programmer can assert duck typing between two Python classes: > > mindref.blogspot.com/2012/11/python-duck-typing-assert.html > > Comments or suggestions are welcome. Overall, it looks potentially useful to me. Looking over the wheezy.core.introspection source, it appears to ignore special method names. For example, if you do: class IFoo(object): def __len__(self): pass class Foo(object): pass assert looks(Foo).like(IFoo) I would expect this to fail, but judging from the code it would not, as it ignores all names starting with '_'. That makes some sense for private names (but on the other hand, why would you declare private names in an interface unless you want to enforce their presence?), but names like __len__ should not be considered private. Another comment I have is on properties, and descriptors in general. Does this allow something declared as a property to be implemented as a simple class attribute, and vice versa? Or can something declared as a property be implemented with some custom descriptor class? I think the answer is "no" to both, as the check it performs is "t2.__class__ is not t.__class__". I assert though that in general the type of a descriptor (that is not a simple class attribute) is not as important in duck testing as its API -- and all descriptors have basically the same API of __get__, __set__, and __delete__. From ian.g.kelly at gmail.com Thu Nov 8 13:48:23 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 8 Nov 2012 11:48:23 -0700 Subject: Right solution to unicode error? In-Reply-To: References: <09a3d20b-5871-47f4-9218-df119698e405@m4g2000yqf.googlegroups.com> <509AF3EF.8050108@gmail.com> <65910cea-f145-409c-a579-9f0cda499546@googlegroups.com> Message-ID: On Thu, Nov 8, 2012 at 11:32 AM, Oscar Benjamin wrote: > If I want the other characters to work I need to change the code page: > > O:\>chcp 65001 > Active code page: 65001 > > O:\>Q:\tools\Python33\python -c "import sys; > sys.stdout.buffer.write('\u03b1\n'.encode('utf-8'))" > ? > > O:\>Q:\tools\Python33\python -c "import sys; > sys.stdout.buffer.write('\u03b1\n'.encode(sys.stdout.en > coding))" > ? I find that I also need to change the font. With the default font, printing '\u2013' gives me: ??? The only alternative font option I have in Windows XP is Lucida Console, which at least works correctly, although it seems to be lacking a lot of glyphs. From andriy.kornatskyy at live.com Thu Nov 8 14:29:10 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Thu, 8 Nov 2012 22:29:10 +0300 Subject: duck typing assert In-Reply-To: References: , Message-ID: Ian, Thank you for the comments. There is definitely a room for improvement, however there are limits. One of them is related to decorator that replaces decorated method arguments with something like *args, **kwargs. Here is an example. def x(): ??? def decorate(m): ??????? def x(*args, **kwargs): ??????????? pass ??????? return x ??? return decorate class Foo(object): ??? @x ??? def bar(self, a): ??????? pass Another one challenge is related to inheritance, or even more complex case: multiple inheritance. I do not believe there is a need to scan whole hierarchy, better apply checks separately, the assert will be more readable. class IFoo(object): ??? def foo(self): ??????? pass class IBar(IFoo): ??? def bar(self): ??????? pass class Bar(IBar): ??? def foo(self): ??????? pass ??? def bar(self): ??????? pass assert looks(Bar).like(IBar) assert looks(Bar).like(IFoo) I agree, special methods __?__ should not be considered private... but what is right: know which one are special (how about some custom? e.g. __ctx__) or enforce check for it by explicitly asking for such check? # 1 assert looks(Foo).like(IFoo, notice=['__len__']) # 2 assert looks(Foo, notice=['__len__']).like(IFoo) I believe if IFoo.foo is decorated as property, it must remain property, otherwise you need exclude it from checks, thus this way you pay code reviewer attention to that. Thanks. Andriy Kornatskyy ---------------------------------------- > From: ian.g.kelly at gmail.com > Date: Thu, 8 Nov 2012 11:34:45 -0700 > Subject: Re: duck typing assert > To: python-list at python.org > > On Thu, Nov 8, 2012 at 10:30 AM, Andriy Kornatskyy > wrote: > > > > People who come from strongly typed languages that offer interfaces often are confused by lack of one in Python. Python, being dynamic typing programming language, follows duck typing principal. It can as simple as this: > > > > assert looks(Foo).like(IFoo) > > > > The post below shows how programmer can assert duck typing between two Python classes: > > > > mindref.blogspot.com/2012/11/python-duck-typing-assert.html > > > > Comments or suggestions are welcome. > > Overall, it looks potentially useful to me. Looking over the > wheezy.core.introspection source, it appears to ignore special method > names. For example, if you do: > > class IFoo(object): > def __len__(self): > pass > > class Foo(object): > pass > > assert looks(Foo).like(IFoo) > > I would expect this to fail, but judging from the code it would not, > as it ignores all names starting with '_'. That makes some sense for > private names (but on the other hand, why would you declare private > names in an interface unless you want to enforce their presence?), but > names like __len__ should not be considered private. > > Another comment I have is on properties, and descriptors in general. > Does this allow something declared as a property to be implemented as > a simple class attribute, and vice versa? Or can something declared > as a property be implemented with some custom descriptor class? I > think the answer is "no" to both, as the check it performs is > "t2.__class__ is not t.__class__". I assert though that in general > the type of a descriptor (that is not a simple class attribute) is not > as important in duck testing as its API -- and all descriptors have > basically the same API of __get__, __set__, and __delete__. > -- > http://mail.python.org/mailman/listinfo/python-list From wxjmfauth at gmail.com Thu Nov 8 14:30:37 2012 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Thu, 8 Nov 2012 11:30:37 -0800 (PST) Subject: Right solution to unicode error? In-Reply-To: References: <09a3d20b-5871-47f4-9218-df119698e405@m4g2000yqf.googlegroups.com> <509AF3EF.8050108@gmail.com> <65910cea-f145-409c-a579-9f0cda499546@googlegroups.com> Message-ID: <08b2c7a7-a5df-45cb-a1b8-1aebe01d46e7@googlegroups.com> Le jeudi 8 novembre 2012 19:32:14 UTC+1, Oscar Benjamin a ?crit?: > On 8 November 2012 15:05, wrote: > > > Le jeudi 8 novembre 2012 15:07:23 UTC+1, Oscar Benjamin a ?crit : > > >> On 8 November 2012 00:44, Oscar Benjamin wrote: > > >> > On 7 November 2012 23:51, Andrew Berg wrote: > > >> >> On 2012.11.07 17:27, Oscar Benjamin wrote: > > >> > > >> >>> Are you using cmd.exe (standard Windows terminal)? If so, it does not > > >> >>> support unicode > > >> > > >> >> Actually, it does. Code page 65001 is UTF-8. I know that doesn't help > > >> >> the OP since Python versions below 3.3 don't support cp65001, but I > > >> >> think it's important to point out that the Windows command line system > > >> >> (it is not unique to cmd) does in fact support Unicode. > > >> > > >> > I have tried to use code page 65001 and it didn't work for me even if > > >> > I did use a version of Python (possibly 3.3 alpha) that claimed to > > >> > support it. > > >> > > >> I stand corrected. I've just checked and codepage 65001 does work in > > >> cmd.exe (on this machine): > > >> > > >> O:\>chcp 65001 > > >> Active code page: 65001 > > >> > > >> O:\>Q:\tools\Python33\python -c print('abc\u2013def') > > >> abc-def > > >> > > >> O:\>Q:\tools\Python33\python -c print('\u03b1') > > >> ? > > >> > > >> It would be a lot better though if it just worked straight away > > >> without me needing to set the code page (like the terminal in every > > >> other OS I use). > > > > > > It *WORKS* straight away. The problem is that > > > people do not wish to use unicode correctly > > > (eg. Mulder's example). > > > Read the point 1) and 4) in my previous post. > > > > > > Unicode and in general the coding of the characters > > > have nothing to do with the os's or programming languages. > > > > I don't know what you mean that it works "straight away". > > > > The default code page on my machine is cp850. > > > > O:\>chcp > > Active code page: 850 > > > > cp850 doesn't understand utf-8. It just prints garbage: > > > > O:\>Q:\tools\Python33\python -c "import sys; > > sys.stdout.buffer.write('\u03b1\n'.encode('utf-8'))" > > ?? > > > > Using the correct encoding doesn't help: > > > > O:\>Q:\tools\Python33\python -c "import sys; > > sys.stdout.buffer.write('\u03b1\n'.encode('cp850'))" > > Traceback (most recent call last): > > File "", line 1, in > > File "Q:\tools\Python33\lib\encodings\cp850.py", line 12, in encode > > return codecs.charmap_encode(input,errors,encoding_map) > > UnicodeEncodeError: 'charmap' codec can't encode character '\u03b1' in > > position 0: character maps to > > > > > > O:\>Q:\tools\Python33\python -c "import sys; > > sys.stdout.buffer.write('\u03b1\n'.encode(sys.stdout.en > > coding))" > > Traceback (most recent call last): > > File "", line 1, in > > File "Q:\tools\Python33\lib\encodings\cp850.py", line 12, in encode > > return codecs.charmap_encode(input,errors,encoding_map) > > UnicodeEncodeError: 'charmap' codec can't encode character '\u03b1' in > > position 0: character maps to > > > > > > If I want the other characters to work I need to change the code page: > > > > O:\>chcp 65001 > > Active code page: 65001 > > > > O:\>Q:\tools\Python33\python -c "import sys; > > sys.stdout.buffer.write('\u03b1\n'.encode('utf-8'))" > > ? > > > > O:\>Q:\tools\Python33\python -c "import sys; > > sys.stdout.buffer.write('\u03b1\n'.encode(sys.stdout.en > > coding))" > > ? > > > > > > Oscar You are confusing two things. The coding of the characters and the set of the characters (glyphes/graphemes) of a coding scheme. It is always possible to encode safely an unicode, but the target coding may not contain the character. Take a look at the output of this "special" interactive interpreter" where the host coding (sys.stdout.encoding) can be change on the fly. >>> s = '?l?phant\u2013abc???' >>> sys.stdout.encoding '' >>> s '?l?phant?abc???' >>> >>> sys.stdout.encoding = 'cp1252' >>> s.encode('cp1252') '?l?phant?abc???' >>> sys.stdout.encoding = 'cp850' >>> s.encode('cp850') Traceback (most recent call last): File "", line 1, in File "C:\Python32\lib\encodings\cp850.py", line 12, in encode return codecs.charmap_encode(input,errors,encoding_map) UnicodeEncodeError: 'charmap' codec can't encode character '\u2013' in position 8: character maps to >>> # but >>> s.encode('cp850', 'replace') '?l?phant?abc???' >>> >>> sys.stdout.encoding = 'utf-8' >>> s '??l??phant???abc???????' >>> s.encode('utf-8') '?l?phant?abc???' >>> >>> sys.stdout.encoding = 'utf-16-le' <<<<<<<<< >>> s ' ? l ? p h a n t  a b c ? S ? ' >>> s.encode('utf-16-le') '?l?phant?abc???' <<<<<<<<<<< some cheating here do to the mail system, it really looks like this. jmf From wxjmfauth at gmail.com Thu Nov 8 14:30:37 2012 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Thu, 8 Nov 2012 11:30:37 -0800 (PST) Subject: Right solution to unicode error? In-Reply-To: References: <09a3d20b-5871-47f4-9218-df119698e405@m4g2000yqf.googlegroups.com> <509AF3EF.8050108@gmail.com> <65910cea-f145-409c-a579-9f0cda499546@googlegroups.com> Message-ID: <08b2c7a7-a5df-45cb-a1b8-1aebe01d46e7@googlegroups.com> Le jeudi 8 novembre 2012 19:32:14 UTC+1, Oscar Benjamin a ?crit?: > On 8 November 2012 15:05, wrote: > > > Le jeudi 8 novembre 2012 15:07:23 UTC+1, Oscar Benjamin a ?crit : > > >> On 8 November 2012 00:44, Oscar Benjamin wrote: > > >> > On 7 November 2012 23:51, Andrew Berg wrote: > > >> >> On 2012.11.07 17:27, Oscar Benjamin wrote: > > >> > > >> >>> Are you using cmd.exe (standard Windows terminal)? If so, it does not > > >> >>> support unicode > > >> > > >> >> Actually, it does. Code page 65001 is UTF-8. I know that doesn't help > > >> >> the OP since Python versions below 3.3 don't support cp65001, but I > > >> >> think it's important to point out that the Windows command line system > > >> >> (it is not unique to cmd) does in fact support Unicode. > > >> > > >> > I have tried to use code page 65001 and it didn't work for me even if > > >> > I did use a version of Python (possibly 3.3 alpha) that claimed to > > >> > support it. > > >> > > >> I stand corrected. I've just checked and codepage 65001 does work in > > >> cmd.exe (on this machine): > > >> > > >> O:\>chcp 65001 > > >> Active code page: 65001 > > >> > > >> O:\>Q:\tools\Python33\python -c print('abc\u2013def') > > >> abc-def > > >> > > >> O:\>Q:\tools\Python33\python -c print('\u03b1') > > >> ? > > >> > > >> It would be a lot better though if it just worked straight away > > >> without me needing to set the code page (like the terminal in every > > >> other OS I use). > > > > > > It *WORKS* straight away. The problem is that > > > people do not wish to use unicode correctly > > > (eg. Mulder's example). > > > Read the point 1) and 4) in my previous post. > > > > > > Unicode and in general the coding of the characters > > > have nothing to do with the os's or programming languages. > > > > I don't know what you mean that it works "straight away". > > > > The default code page on my machine is cp850. > > > > O:\>chcp > > Active code page: 850 > > > > cp850 doesn't understand utf-8. It just prints garbage: > > > > O:\>Q:\tools\Python33\python -c "import sys; > > sys.stdout.buffer.write('\u03b1\n'.encode('utf-8'))" > > ?? > > > > Using the correct encoding doesn't help: > > > > O:\>Q:\tools\Python33\python -c "import sys; > > sys.stdout.buffer.write('\u03b1\n'.encode('cp850'))" > > Traceback (most recent call last): > > File "", line 1, in > > File "Q:\tools\Python33\lib\encodings\cp850.py", line 12, in encode > > return codecs.charmap_encode(input,errors,encoding_map) > > UnicodeEncodeError: 'charmap' codec can't encode character '\u03b1' in > > position 0: character maps to > > > > > > O:\>Q:\tools\Python33\python -c "import sys; > > sys.stdout.buffer.write('\u03b1\n'.encode(sys.stdout.en > > coding))" > > Traceback (most recent call last): > > File "", line 1, in > > File "Q:\tools\Python33\lib\encodings\cp850.py", line 12, in encode > > return codecs.charmap_encode(input,errors,encoding_map) > > UnicodeEncodeError: 'charmap' codec can't encode character '\u03b1' in > > position 0: character maps to > > > > > > If I want the other characters to work I need to change the code page: > > > > O:\>chcp 65001 > > Active code page: 65001 > > > > O:\>Q:\tools\Python33\python -c "import sys; > > sys.stdout.buffer.write('\u03b1\n'.encode('utf-8'))" > > ? > > > > O:\>Q:\tools\Python33\python -c "import sys; > > sys.stdout.buffer.write('\u03b1\n'.encode(sys.stdout.en > > coding))" > > ? > > > > > > Oscar You are confusing two things. The coding of the characters and the set of the characters (glyphes/graphemes) of a coding scheme. It is always possible to encode safely an unicode, but the target coding may not contain the character. Take a look at the output of this "special" interactive interpreter" where the host coding (sys.stdout.encoding) can be change on the fly. >>> s = '?l?phant\u2013abc???' >>> sys.stdout.encoding '' >>> s '?l?phant?abc???' >>> >>> sys.stdout.encoding = 'cp1252' >>> s.encode('cp1252') '?l?phant?abc???' >>> sys.stdout.encoding = 'cp850' >>> s.encode('cp850') Traceback (most recent call last): File "", line 1, in File "C:\Python32\lib\encodings\cp850.py", line 12, in encode return codecs.charmap_encode(input,errors,encoding_map) UnicodeEncodeError: 'charmap' codec can't encode character '\u2013' in position 8: character maps to >>> # but >>> s.encode('cp850', 'replace') '?l?phant?abc???' >>> >>> sys.stdout.encoding = 'utf-8' >>> s '??l??phant???abc???????' >>> s.encode('utf-8') '?l?phant?abc???' >>> >>> sys.stdout.encoding = 'utf-16-le' <<<<<<<<< >>> s ' ? l ? p h a n t  a b c ? S ? ' >>> s.encode('utf-16-le') '?l?phant?abc???' <<<<<<<<<<< some cheating here do to the mail system, it really looks like this. jmf From wxjmfauth at gmail.com Thu Nov 8 14:54:23 2012 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Thu, 8 Nov 2012 11:54:23 -0800 (PST) Subject: Right solution to unicode error? In-Reply-To: References: <09a3d20b-5871-47f4-9218-df119698e405@m4g2000yqf.googlegroups.com> <509AF3EF.8050108@gmail.com> <65910cea-f145-409c-a579-9f0cda499546@googlegroups.com> Message-ID: Le jeudi 8 novembre 2012 19:49:24 UTC+1, Ian a ?crit?: > On Thu, Nov 8, 2012 at 11:32 AM, Oscar Benjamin > > wrote: > > > If I want the other characters to work I need to change the code page: > > > > > > O:\>chcp 65001 > > > Active code page: 65001 > > > > > > O:\>Q:\tools\Python33\python -c "import sys; > > > sys.stdout.buffer.write('\u03b1\n'.encode('utf-8'))" > > > ? > > > > > > O:\>Q:\tools\Python33\python -c "import sys; > > > sys.stdout.buffer.write('\u03b1\n'.encode(sys.stdout.en > > > coding))" > > > ? > > > > I find that I also need to change the font. With the default font, > > printing '\u2013' gives me: > > > > ??? > > > > The only alternative font option I have in Windows XP is Lucida > > Console, which at least works correctly, although it seems to be > > lacking a lot of glyphs. -------- Font has nothing to do here. You are "simply" wrongly encoding your "unicode". >>> '\u2013' '?' >>> '\u2013'.encode('utf-8') b'\xe2\x80\x93' >>> '\u2013'.encode('utf-8').decode('cp1252') '???' jmf From wxjmfauth at gmail.com Thu Nov 8 14:54:23 2012 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Thu, 8 Nov 2012 11:54:23 -0800 (PST) Subject: Right solution to unicode error? In-Reply-To: References: <09a3d20b-5871-47f4-9218-df119698e405@m4g2000yqf.googlegroups.com> <509AF3EF.8050108@gmail.com> <65910cea-f145-409c-a579-9f0cda499546@googlegroups.com> Message-ID: Le jeudi 8 novembre 2012 19:49:24 UTC+1, Ian a ?crit?: > On Thu, Nov 8, 2012 at 11:32 AM, Oscar Benjamin > > wrote: > > > If I want the other characters to work I need to change the code page: > > > > > > O:\>chcp 65001 > > > Active code page: 65001 > > > > > > O:\>Q:\tools\Python33\python -c "import sys; > > > sys.stdout.buffer.write('\u03b1\n'.encode('utf-8'))" > > > ? > > > > > > O:\>Q:\tools\Python33\python -c "import sys; > > > sys.stdout.buffer.write('\u03b1\n'.encode(sys.stdout.en > > > coding))" > > > ? > > > > I find that I also need to change the font. With the default font, > > printing '\u2013' gives me: > > > > ??? > > > > The only alternative font option I have in Windows XP is Lucida > > Console, which at least works correctly, although it seems to be > > lacking a lot of glyphs. -------- Font has nothing to do here. You are "simply" wrongly encoding your "unicode". >>> '\u2013' '?' >>> '\u2013'.encode('utf-8') b'\xe2\x80\x93' >>> '\u2013'.encode('utf-8').decode('cp1252') '???' jmf From tjreedy at udel.edu Thu Nov 8 15:29:38 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 08 Nov 2012 15:29:38 -0500 Subject: int.__init__ incompatible in Python 3.3 In-Reply-To: References: <481tm9-k6i.ln1@satorlaser.homedns.org> Message-ID: On 11/8/2012 12:13 PM, Ian Kelly wrote: > On Thu, Nov 8, 2012 at 8:55 AM, Ulrich Eckhardt > wrote: >> Preparing for an upgrade from 2.7 to 3, I stumbled across an incompatibility >> between 2.7 and 3.2 on one hand and 3.3 on the other: >> >> class X(int): >> def __init__(self, value): >> super(X, self).__init__(value) This is a bug. Subclasses of immutables should not define __init__. >>> int.__init__ is object.__init__ True object.__init__(self) is a dummy placeholder function that takes no args and does nothing. >> X(42) >> >> On 2.7 and 3.2, the above code works. That is a bug. It is documented that calling with the wrong number of args is an error. >> On 3.3, it gives me a "TypeError: object.__init__() takes no parameters". >> To some extent, this makes sense to >> me, because the int subobject is not initialized in __init__ but in __new__. >> As a workaround, I can simple drop the parameter from the call. Just drop the do-nothing call. >> breaking backward compatibility is another issue, so I wonder if that should >> be considered as a bug. Every bug fix breaks backward compatibility with code that depends on the bug. Such breakage is not a bug, but, as in this case, some fixes are not put in bugfix releases because of such breakage. >> Bug? Feature? Other suggestions? Intentional bugfix. http://bugs.python.org/issue1683368 There was additional discussion on pydev or python-ideas lists before the final commit. This fix was not back-ported to 2.7 or 3.2. > A similar change was made to object.__init__ in 2.6, so this could > just be bringing the behavior of int into line with object. There's > nothing about it in the whatsnew document, though. What's New is a summary of *new* features. It does not list bug fixes. At the top it says " For full details, see the Misc/NEWS file." The last patch on the issue added this entry. ''' Core and Builtins ----------------- - Issue #1683368: object.__new__ and object.__init__ raise a TypeError if they are passed arguments and their complementary method is not overridden. ''' > I say open a bug report and let the devs sort it out. Please do not. The current situation is the result of 'sorting it out' over several years. -- Terry Jan Reedy From tjreedy at udel.edu Thu Nov 8 15:39:24 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 08 Nov 2012 15:39:24 -0500 Subject: duck typing =?UTF-8?B?YXNzZXJ04oCP?= In-Reply-To: References: Message-ID: On 11/8/2012 12:34 PM, Andriy Kornatskyy wrote: > > People who come from strongly typed languages that offer interfaces > often are confused by lack of one in Python. Python, being dynamic > typing programming language, follows duck typing principal. It can as > simple as this: > > assert looks(Foo).like(IFoo) > > The post below shows how programmer can assert duck typing between > two Python classes: > > http://mindref.blogspot.com/2012/11/python-duck-typing-assert.html > > Comments or suggestions are welcome. From the post: ''' So far so good. Let fix it and take a look at properties: from wheezy.core.introspection import looks class IFoo(object): def foo(self, a, b=None): pass @property def bar(self): pass class Foo(object): def foo(self, a, b=None): pass def bar(self): pass assert looks(Foo).like(IFoo) Here is output: test.py:21: UserWarning: 'bar': is not property. assert looks(Foo).like(IFoo) Traceback (most recent call last): File "test.py", line 21, in assert looks(Foo).like(IFoo) AssertionError ''' I view this check as an error. Properties are intended to be transparent to the user. One use of properties is to make something that is not a Mallard act like a Mallard. So this check breaks duck typing. -- Terry Jan Reedy From ian.g.kelly at gmail.com Thu Nov 8 15:41:37 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 8 Nov 2012 13:41:37 -0700 Subject: Right solution to unicode error? In-Reply-To: References: <09a3d20b-5871-47f4-9218-df119698e405@m4g2000yqf.googlegroups.com> <509AF3EF.8050108@gmail.com> <65910cea-f145-409c-a579-9f0cda499546@googlegroups.com> Message-ID: On Thu, Nov 8, 2012 at 12:54 PM, wrote: > Font has nothing to do here. > You are "simply" wrongly encoding your "unicode". > >>>> '\u2013' > '?' >>>> '\u2013'.encode('utf-8') > b'\xe2\x80\x93' >>>> '\u2013'.encode('utf-8').decode('cp1252') > '???' No, it seriously is the font. This is what I get using the default ("Raster") font: C:\>chcp 65001 Active code page: 65001 C:\>c:\python33\python Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> '\u2013' '???' >>> import sys >>> sys.stdout.buffer.write('\u2013\n'.encode('utf-8')) ??? 4 I should note here that the characters copied and pasted do not correspond to the glyphs actually displayed in my terminal window. In the terminal window I actually see: ??? If I change the font to Lucida Console and run the *exact same code*, I get this: C:\>chcp 65001 Active code page: 65001 C:\>c:\python33\python Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> '\u2013' '?' >>> import sys >>> sys.stdout.buffer.write('\u2013\n'.encode('utf-8')) ? 4 Why is the font important? I have no idea. Blame Microsoft. From ramit.prasad at jpmorgan.com Thu Nov 8 15:54:23 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Thu, 8 Nov 2012 20:54:23 +0000 Subject: Right solution to unicode error? In-Reply-To: References: <09a3d20b-5871-47f4-9218-df119698e405@m4g2000yqf.googlegroups.com> <509AF3EF.8050108@gmail.com> <65910cea-f145-409c-a579-9f0cda499546@googlegroups.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF4741678B1A0@SCACMX008.exchad.jpmchase.net> wxjmfauth at gmail.com wrote: > > Le jeudi 8 novembre 2012 19:49:24 UTC+1, Ian a ?crit?: > > On Thu, Nov 8, 2012 at 11:32 AM, Oscar Benjamin > > > > wrote: > > > > > If I want the other characters to work I need to change the code page: > > > > > > O:\>chcp 65001 > > > Active code page: 65001 > > > > > > O:\>Q:\tools\Python33\python -c "import sys; > > > sys.stdout.buffer.write('\u03b1\n'.encode('utf-8'))" > > > ? > > > > > > O:\>Q:\tools\Python33\python -c "import sys; > > > sys.stdout.buffer.write('\u03b1\n'.encode(sys.stdout.en > > > coding))" > > > ? > > > > I find that I also need to change the font. With the default font, > > > > printing '\u2013' gives me: > > ??? > > > > The only alternative font option I have in Windows XP is Lucida > > Console, which at least works correctly, although it seems to be > > lacking a lot of glyphs. > > -------- > > Font has nothing to do here. > You are "simply" wrongly encoding your "unicode". > Why would font not matter? Unicode is the abstract definition of all characters right? From that we map the abstract character to a code page/set, which gives real values for an abstract character. From that code page we then visually display the "real value" based on the font. If that font does not have a glyph for a specific character page (or a different glyph) then that is a problem and not related encoding. Unicode->code page->font > >>> '\u2013' > '?' > >>> '\u2013'.encode('utf-8') > b'\xe2\x80\x93' > >>> '\u2013'.encode('utf-8').decode('cp1252') > '???' > This is a mismatched translation between code pages; not font related but is instead one abstraction "level" up. This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From ian.g.kelly at gmail.com Thu Nov 8 16:07:15 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 8 Nov 2012 14:07:15 -0700 Subject: Right solution to unicode error? In-Reply-To: <5B80DD153D7D744689F57F4FB69AF4741678B1A0@SCACMX008.exchad.jpmchase.net> References: <09a3d20b-5871-47f4-9218-df119698e405@m4g2000yqf.googlegroups.com> <509AF3EF.8050108@gmail.com> <65910cea-f145-409c-a579-9f0cda499546@googlegroups.com> <5B80DD153D7D744689F57F4FB69AF4741678B1A0@SCACMX008.exchad.jpmchase.net> Message-ID: On Thu, Nov 8, 2012 at 1:54 PM, Prasad, Ramit wrote: > Why would font not matter? Unicode is the abstract definition > of all characters right? From that we map the abstract > character to a code page/set, which gives real values for an > abstract character. From that code page we then visually display > the "real value" based on the font. If that font does > not have a glyph for a specific character page (or a different > glyph) then that is a problem and not related encoding. Usually though when the font is missing a glyph for a Unicode character, you just get a missing glyph symbol, such as an empty rectangle. For some reason when using the default font, cmd seemingly ignores the active code page, skips decoding the characters, and tries to print the individual bytes as if using code page 437. From oscar.j.benjamin at gmail.com Thu Nov 8 16:37:48 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 8 Nov 2012 21:37:48 +0000 Subject: Right solution to unicode error? In-Reply-To: References: <09a3d20b-5871-47f4-9218-df119698e405@m4g2000yqf.googlegroups.com> <509AF3EF.8050108@gmail.com> <65910cea-f145-409c-a579-9f0cda499546@googlegroups.com> Message-ID: On 8 November 2012 19:54, wrote: > Le jeudi 8 novembre 2012 19:49:24 UTC+1, Ian a ?crit : >> On Thu, Nov 8, 2012 at 11:32 AM, Oscar Benjamin >> >> wrote: >> >> > If I want the other characters to work I need to change the code page: >> >> > >> >> > O:\>chcp 65001 >> >> > Active code page: 65001 >> >> > >> >> > O:\>Q:\tools\Python33\python -c "import sys; >> >> I find that I also need to change the font. With the default font, >> >> printing '\u2013' gives me: >> >> ??? >> >> >> >> The only alternative font option I have in Windows XP is Lucida >> >> Console, which at least works correctly, although it seems to be >> >> lacking a lot of glyphs. > > Font has nothing to do here. > You are "simply" wrongly encoding your "unicode". > >>>> '\u2013' > '?' >>>> '\u2013'.encode('utf-8') > b'\xe2\x80\x93' >>>> '\u2013'.encode('utf-8').decode('cp1252') > '???' You have correctly identified that the displayed characters are the result of accidentally interpreting utf-8 bytes as if they were cp1252 or similar. However, it is not Ian or Python that is confusing the encoding. It is cmd.exe that is confusing the encoding in a font-dependent way. I also had to change the font as Ian describes though I did it some time ago and forgot to mention it here. jmf, can you please trim the text you quote removing the parts you are not responding to and then any remaining blank lines that were inserted by your reader/editor? Oscar From pengyu.ut at gmail.com Thu Nov 8 18:12:58 2012 From: pengyu.ut at gmail.com (Peng Yu) Date: Thu, 8 Nov 2012 15:12:58 -0800 (PST) Subject: How to print python commands automatically? Message-ID: Hi, In bash, set -v will print the command executed. For example, the following screen output shows that the "echo" command is printed automatically. Is there a similar thing in python? ~/linux/test/bash/man/builtin/set/-v$ cat main.sh #!/usr/bin/env bash set -v echo "Hello World!" ~/linux/test/bash/man/builtin/set/-v$ ./main.sh echo "Hello World!" Hello World! Regards, Peng From dihedral88888 at googlemail.com Thu Nov 8 18:18:11 2012 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Thu, 8 Nov 2012 15:18:11 -0800 (PST) Subject: Multi-dimensional list initialization In-Reply-To: References: Message-ID: On Monday, November 5, 2012 3:07:12 PM UTC+8, Chris Rebert wrote: > On Sun, Nov 4, 2012 at 10:27 PM, Demian Brecht wrote: > > > So, here I was thinking "oh, this is a nice, easy way to initialize a 4D matrix" (running 2.7.3, non-core libs not allowed): > > > > > > m = [[None] * 4] * 4 This is not clear in a name binding objective programming language. b=[1,2,3,4]*4 mb=[ b]*4 # check the behaviors and usages of reference copies # and shadow value copies and deep-value copies > > > > > > The way to get what I was after was: > > > > > > m = [[None] * 4, [None] * 4, [None] * 4, [None * 4]] > > > > > > (Obviously, I could have just hardcoded the initialization, but I'm too lazy to type all that out ;)) > > > > > > The behaviour I encountered seems a little contradictory to me. > > > [None] * 4 creates four distinct elements in a single array > > > while [[None] * 4] * 4 creates one distinct array of four distinct elements, with three references to it: > > > > Incorrect. In /both/ cases, the result is a list of length 4, whose > > elements are 4 (references to) the exact same object as the original > > list's element. > > Put simply, the list multiplication operator never copies objects; it > > just makes additional references to them. > > > > However, unlike a list object (as in your latter example), the object > > `None` is completely immutable (and what's more, a singleton value), > > so you just-so-happen *not to be able to* run into the same problem of > > mutating an object (assignment to an index of a list constitutes > > mutation of that list) that is referenced in multiple places, for you > > cannot mutate None in the first place!: > > >>> x = None > > >>> x.a = 42 > > Traceback (most recent call last): > > File "", line 1, in > > AttributeError: 'NoneType' object has no attribute 'a' > > >>> # it doesn't overload any mutating operators: > > >>> type(None).__dict__.keys() > > ['__hash__', '__repr__', '__doc__'] > > >>> # and it obviously has no instance variables, > > >>> # so, we can't modify it in any way whatsoever! > > (Lists, on the other hand, define item assignment, .pop(), .remove(), > > and a few other mutator methods.) > > > > >>>> a = [None] * 4 > > >>>> a[0] = 'a' > > >>>> a > > > ['a', None, None, None] > > > > > >>>> m = [[None] * 4] * 4 > > >>>> m[0][0] = 'm' > > >>>> m > > > [['m', None, None, None], ['m', None, None, None], ['m', None, None, None], ['m', None, None, None]] > > > > > > Is this expected behavior > > > > Yes. It's also a FAQ: > > http://docs.python.org/2/faq/programming.html#how-do-i-create-a-multidimensional-list > > > > > and if so, why? > > > > It's a general (albeit AFAIK unstated) principle that Python never > > copies objects unless you explicitly ask it to. You have encountered > > one example of this rule in action. > > > > > In my mind either result makes sense, but the inconsistency is what throws me off. > > > > It is perfectly consistent, once you understand what list > > multiplication actually does. > > > > Cheers, > > Chris > > -- > > http://rebertia.com From dihedral88888 at googlemail.com Thu Nov 8 18:18:11 2012 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Thu, 8 Nov 2012 15:18:11 -0800 (PST) Subject: Multi-dimensional list initialization In-Reply-To: References: Message-ID: On Monday, November 5, 2012 3:07:12 PM UTC+8, Chris Rebert wrote: > On Sun, Nov 4, 2012 at 10:27 PM, Demian Brecht wrote: > > > So, here I was thinking "oh, this is a nice, easy way to initialize a 4D matrix" (running 2.7.3, non-core libs not allowed): > > > > > > m = [[None] * 4] * 4 This is not clear in a name binding objective programming language. b=[1,2,3,4]*4 mb=[ b]*4 # check the behaviors and usages of reference copies # and shadow value copies and deep-value copies > > > > > > The way to get what I was after was: > > > > > > m = [[None] * 4, [None] * 4, [None] * 4, [None * 4]] > > > > > > (Obviously, I could have just hardcoded the initialization, but I'm too lazy to type all that out ;)) > > > > > > The behaviour I encountered seems a little contradictory to me. > > > [None] * 4 creates four distinct elements in a single array > > > while [[None] * 4] * 4 creates one distinct array of four distinct elements, with three references to it: > > > > Incorrect. In /both/ cases, the result is a list of length 4, whose > > elements are 4 (references to) the exact same object as the original > > list's element. > > Put simply, the list multiplication operator never copies objects; it > > just makes additional references to them. > > > > However, unlike a list object (as in your latter example), the object > > `None` is completely immutable (and what's more, a singleton value), > > so you just-so-happen *not to be able to* run into the same problem of > > mutating an object (assignment to an index of a list constitutes > > mutation of that list) that is referenced in multiple places, for you > > cannot mutate None in the first place!: > > >>> x = None > > >>> x.a = 42 > > Traceback (most recent call last): > > File "", line 1, in > > AttributeError: 'NoneType' object has no attribute 'a' > > >>> # it doesn't overload any mutating operators: > > >>> type(None).__dict__.keys() > > ['__hash__', '__repr__', '__doc__'] > > >>> # and it obviously has no instance variables, > > >>> # so, we can't modify it in any way whatsoever! > > (Lists, on the other hand, define item assignment, .pop(), .remove(), > > and a few other mutator methods.) > > > > >>>> a = [None] * 4 > > >>>> a[0] = 'a' > > >>>> a > > > ['a', None, None, None] > > > > > >>>> m = [[None] * 4] * 4 > > >>>> m[0][0] = 'm' > > >>>> m > > > [['m', None, None, None], ['m', None, None, None], ['m', None, None, None], ['m', None, None, None]] > > > > > > Is this expected behavior > > > > Yes. It's also a FAQ: > > http://docs.python.org/2/faq/programming.html#how-do-i-create-a-multidimensional-list > > > > > and if so, why? > > > > It's a general (albeit AFAIK unstated) principle that Python never > > copies objects unless you explicitly ask it to. You have encountered > > one example of this rule in action. > > > > > In my mind either result makes sense, but the inconsistency is what throws me off. > > > > It is perfectly consistent, once you understand what list > > multiplication actually does. > > > > Cheers, > > Chris > > -- > > http://rebertia.com From steve+comp.lang.python at pearwood.info Thu Nov 8 18:33:01 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 08 Nov 2012 23:33:01 GMT Subject: duck typing =?UTF-8?b?YXNzZXJ04oCP?= References: Message-ID: <509c412d$0$29980$c3e8da3$5496439d@news.astraweb.com> On Thu, 08 Nov 2012 20:34:58 +0300, Andriy Kornatskyy wrote: > People who come from strongly typed languages that offer interfaces > often are confused by lack of one in Python. Python, being dynamic > typing programming language, follows duck typing principal. It can as > simple as this: > ? > assert looks(Foo).like(IFoo) How very cute. And I don't mean that in a good way. Why is this a class with a method, instead of a function that takes two class arguments (plus any optional arguments needed)? looks_like(Foo, IFoo) is less "cute", reads better to English speakers, and much more Pythonic. This isn't Java, not everything needs to be a class. > The post below shows how programmer can assert duck typing between two > Python classes: > ? > http://mindref.blogspot.com/2012/11/python-duck-typing-assert.html I don't understand the emphasis on assert for this code. It is enough that looks() return a flag. The caller can then use that as an assertion: assert looks(Spam).like(Ham) or as a conditional: if looks(food).like(Meat): ... else: ... Assertions are only one use for this check, and in my opinion, the least useful one. And why the warnings? In my opinion, using the warning mechanism as a way to communicate the differences between the classes is an abuse of warnings: they're not *warnings*, they are *diagnostic information*. It is also fragile: the caller may have filtered warnings, and will not see the messages you generate. Lastly, I do not understand the purpose of this "wheezy.core" package. Does it have something to do with Ubuntu Wheezy? The documentation is unclear -- it refers to it as a "Python package" that "provides core features", but doesn't say what the purpose of the package is: core features of *what*? And the examples.rst file doesn't show any examples. https://bitbucket.org/akorn/wheezy.core/src/ca5b902e9605/doc/examples.rst -- Steven From steve+comp.lang.python at pearwood.info Thu Nov 8 18:40:19 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 08 Nov 2012 23:40:19 GMT Subject: duck typing =?UTF-8?b?YXNzZXJ04oCP?= References: Message-ID: <509c42e3$0$29980$c3e8da3$5496439d@news.astraweb.com> On Thu, 08 Nov 2012 15:39:24 -0500, Terry Reedy wrote: [...] > test.py:21: UserWarning: 'bar': is not property. > assert looks(Foo).like(IFoo) > Traceback (most recent call last): > File "test.py", line 21, in > assert looks(Foo).like(IFoo) > AssertionError > ''' > > I view this check as an error. Properties are intended to be transparent > to the user. One use of properties is to make something that is not a > Mallard act like a Mallard. So this check breaks duck typing. Properties and methods do not have the same interface: IFoo.bar # returns a computed property Foo.bar() # calls a method Since the interfaces are different, duck-typing will fail. It will actually fail in a potentially nasty way: x = Foo.bar # doesn't raise an exception, gives the method object # ... much later do_something_with(x) # blows up potentially far, far away -- Steven From ian.g.kelly at gmail.com Thu Nov 8 20:00:58 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 8 Nov 2012 18:00:58 -0700 Subject: duck typing assert In-Reply-To: <509c412d$0$29980$c3e8da3$5496439d@news.astraweb.com> References: <509c412d$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Thu, Nov 8, 2012 at 4:33 PM, Steven D'Aprano wrote: > On Thu, 08 Nov 2012 20:34:58 +0300, Andriy Kornatskyy wrote: > >> People who come from strongly typed languages that offer interfaces >> often are confused by lack of one in Python. Python, being dynamic >> typing programming language, follows duck typing principal. It can as >> simple as this: >> >> assert looks(Foo).like(IFoo) > > How very cute. And I don't mean that in a good way. > > Why is this a class with a method, instead of a function that takes two > class arguments (plus any optional arguments needed)? > > looks_like(Foo, IFoo) > > is less "cute", reads better to English speakers, and much more Pythonic. > This isn't Java, not everything needs to be a class. I disagree. Does that test whether Foo looks like IFoo, or IFoo looks like Foo? Of course, given the naming convention and the example, it's easy to work out, but when you're trying to *write* that from memory, it could be a nuisance to remember the proper order. This is already a wart of isinstance and issubclass. looks(Foo).like(IFoo), on the other hand, is crystal clear about which argument is which. From hansmu at xs4all.nl Thu Nov 8 20:12:42 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Fri, 09 Nov 2012 02:12:42 +0100 Subject: how to simulate tar filename substitution across piped subprocess.Popen() calls? In-Reply-To: References: Message-ID: <509c588a$0$6904$e4fe514c@news2.news.xs4all.nl> On 8/11/12 19:05:11, jkn wrote: > Hi All > i am trying to build up a set of subprocess.Ponen calls to > replicate the effect of a horribly long shell command. I'm not clear > how I can do one part of this and wonder if anyone can advise. I'm on > Linux, fairly obviously. > > I have a command which (simplified) is a tar -c command piped through > to xargs: > > tar -czvf myfile.tgz -c $MYDIR mysubdir/ | xargs -I '{}' sh -c "test - > f $MYDIR/'{}'" > > (The full command is more complicated than this; I got it from a shell > guru). > > IIUC, when called like this, the two occurences of '{}' in the xargs > command will get replaced with the file being added to the tarfile. > > Also IIUC, I will need two calls to subprocess.Popen() and use > subprocess.stdin on the second to receive the output from the first. > But how can I achive the substitution of the '{}' construction across > these two calls? That's what 'xargs' will do for you. All you need to do, is invoke xargs with arguments containing '{}'. I.e., something like: cmd1 = ['tar', '-czvf', 'myfile.tgz', '-c', mydir, 'mysubdir'] first_process = subprocess.Popen(cmd1, stdout=subprocess.PIPE) cmd2 = ['xargs', '-I', '{}', 'sh', '-c', "test -f %s/'{}'" % mydir] second_process = subprocess.Popen(cmd2, stdin=first_process.stdout) > Apologies if I've made any howlers in this description - it's very > likely... I think the second '-c' argument to tar should have been a '-C'. I'm not sure I understand what the second command is trying to achieve. On my system, nothing happens, because tar writes the names of the files it is adding to stderr, so xargs receives no input at all. If I send the stderr from tar to the stdin of xargs, then it still doesn't seem to do anything sensible. Perhaps your real xargs command is more complicated and more sensible. Hope this helps, -- HansM From breamoreboy at yahoo.co.uk Thu Nov 8 20:39:40 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 09 Nov 2012 01:39:40 +0000 Subject: Multi-dimensional list initialization In-Reply-To: <5099bf7d$0$29980$c3e8da3$5496439d@news.astraweb.com> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <5099bf7d$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 07/11/2012 01:55, Steven D'Aprano wrote: > > Who knows? Who cares? Nobody does: > > n -= n > But I've seen this scattered through code: x := x - x - x -- Cheers. Mark Lawrence. From rustompmody at gmail.com Thu Nov 8 21:50:08 2012 From: rustompmody at gmail.com (rusi) Date: Thu, 8 Nov 2012 18:50:08 -0800 (PST) Subject: How to print python commands automatically? References: Message-ID: <603f952f-a1a2-48a3-ab55-bdc7982a1f2a@i7g2000pbf.googlegroups.com> On Nov 9, 4:12?am, Peng Yu wrote: > Hi, > > In bash, set -v will print the command executed. For example, the > following screen output shows that the "echo" command is printed > automatically. Is there a similar thing in python? > > ~/linux/test/bash/man/builtin/set/-v$ cat main.sh > #!/usr/bin/env bash > > set -v > echo "Hello World!" > ~/linux/test/bash/man/builtin/set/-v$ ./main.sh > echo "Hello World!" > Hello World! > > Regards, > Peng Is this what you want? http://docs.python.org/2/library/trace.html From bahamutzero8825 at gmail.com Thu Nov 8 22:30:46 2012 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Thu, 08 Nov 2012 21:30:46 -0600 Subject: Right solution to unicode error? In-Reply-To: References: <09a3d20b-5871-47f4-9218-df119698e405@m4g2000yqf.googlegroups.com> <509AF3EF.8050108@gmail.com> Message-ID: <509C78E6.5060609@gmail.com> On 2012.11.08 08:06, Oscar Benjamin wrote: > It would be a lot better though if it just worked straight away > without me needing to set the code page (like the terminal in every > other OS I use). The crude equivalent of .bashrc/.zshrc/whatever shell startup script for cmd is setting a string value (REG_SZ) in HKCU\Software\Microsoft\Command Processor named autorun and setting that with whatever command(s) you want to run whenever the shell starts. Mine has a value of '@chcp 65001>nul'. I actually run zsh when practical (gotta love Cygwin) and I have an equivalent command in my .zshrc. Getting unicode to work in a Windows is a hassle, but it /can/ work. CPython does have a bug that makes it annoying at times, though - http://bugs.python.org/issue1602 -- CPython 3.3.0 | Windows NT 6.1.7601.17835 From tjreedy at udel.edu Thu Nov 8 23:44:54 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 08 Nov 2012 23:44:54 -0500 Subject: duck typing =?UTF-8?B?YXNzZXJ04oCP?= In-Reply-To: <509c42e3$0$29980$c3e8da3$5496439d@news.astraweb.com> References: <509c42e3$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 11/8/2012 6:40 PM, Steven D'Aprano wrote: > On Thu, 08 Nov 2012 15:39:24 -0500, Terry Reedy wrote: > > [...] >> test.py:21: UserWarning: 'bar': is not property. >> assert looks(Foo).like(IFoo) >> Traceback (most recent call last): >> File "test.py", line 21, in >> assert looks(Foo).like(IFoo) >> AssertionError >> ''' >> >> I view this check as an error. Properties are intended to be transparent >> to the user. One use of properties is to make something that is not a >> Mallard act like a Mallard. So this check breaks duck typing. > > Properties and methods do not have the same interface: Of course not, properties mimic instance attributes, accessed via the instance, not calls of methods. I believe the attributes are most often used to micic data attributes. The classical example is giving x,y properties to points with r,theta attributes so that they look like and can be substituted for points with actual x,y attributes. This is the kind of duck typing I was referring to, and it would be broken by the property check. But if an instance method is being mimicked, so that inst.meth is a bound instance method when meth is an instance method attribute of the class of inst, then meth.get(inst) of a meth property must also return a bound instance method. (I am not exactly sure when one would want to do this, but since you brought up methods in relation to properties ...) from types import MethodType as bm class C: def __init__(self, x = 0): self.x = x def double(self): return 2 * self.x class Cp: def __init__(self, x = 0): self.x = x @property def double(self): return bm(lambda self: 2 * self.x, self) c, cp = C(3), Cp(3) print(c.double, cp.double, c.double(), cp.double(), sep = '\n') # > of <__main__.Cp object at 0x0000000003455A58>> 6 6 > IFoo.bar # returns a computed property Assuming IFoo is a class and bar is a property attribute of the class, IFoo.bar is the property object itself, not the computed property of an instance. > Foo.bar() # calls a method Assuming Foo is a class, this only works if bar is a class method, static method, or pre-bound instance method (as returned by types.MethodType). If bar is a function intended to be a regular instance method, it has to be called on the instance or given an instance as an arguement. > Since the interfaces are different, duck-typing will fail. It will > actually fail in a potentially nasty way: I don't understand what you mean, assuming that the property is used as intended. > x = Foo.bar # doesn't raise an exception, why should it? > gives the method object if bar is a method (function), of course, just as IFoo.bar gives the property object. > # ... much later > do_something_with(x) # blows up potentially far, far away Transparency applies to immediate access via an instance. If you extract different machinery from behind two class curtains and save them for later use, then they are different. So what? This is not an issue for instance data attributes. Instance methods of classes are intended to be accessed via an instance, at which point the result is a bound method that can be called either immediately or later (possible many times). -- Terry Jan Reedy From rosuav at gmail.com Fri Nov 9 01:07:09 2012 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 9 Nov 2012 17:07:09 +1100 Subject: Multi-dimensional list initialization In-Reply-To: References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <5099bf7d$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Nov 9, 2012 at 12:39 PM, Mark Lawrence wrote: > On 07/11/2012 01:55, Steven D'Aprano wrote: >> >> >> Who knows? Who cares? Nobody does: >> >> n -= n >> > > But I've seen this scattered through code: > > x := x - x - x Can you enlighten us as to how this is better than either: x := -x or x := 0 - x ? I'm not seeing it. And I'm not seeing any nonnumeric that would benefit from being subtracted from itself twice (strings, arrays, sets, you can subtract them from one another but not usefully more than once). ChrisA From rosuav at gmail.com Fri Nov 9 01:14:49 2012 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 9 Nov 2012 17:14:49 +1100 Subject: duck typing assert In-Reply-To: References: <509c412d$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Nov 9, 2012 at 12:00 PM, Ian Kelly wrote: > looks(Foo).like(IFoo), on the other hand, is crystal clear about which > argument is which. I'm not so sure that it is, tbh. If you read it like an English sentence, it's clearly testing whether Foo matches the template in IFoo, but which are you more likely to do: test one class to see if it satisfies lots of templates, or test one template against every class you meet? I think probably the latter is, if not more likely than the former, at least sufficiently plausible as to create confusion. It makes very good sense to say: duckmatch(IFoo).compare(Foo) ie with the arguments the other way. ChrisA From steve+comp.lang.python at pearwood.info Fri Nov 9 01:30:24 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 09 Nov 2012 06:30:24 GMT Subject: duck typing =?UTF-8?b?YXNzZXJ04oCP?= References: <509c42e3$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: <509ca300$0$29980$c3e8da3$5496439d@news.astraweb.com> On Thu, 08 Nov 2012 23:44:54 -0500, Terry Reedy wrote: > On 11/8/2012 6:40 PM, Steven D'Aprano wrote: [...] >> IFoo.bar # returns a computed property > > Assuming IFoo is a class and bar is a property attribute of the class, > IFoo.bar is the property object itself, not the computed property of an > instance. Given the context we were discussing, namely duck-typing, the examples I gave should have been understood as indications, not literal code snippets. Yes, it is true that "IFoo.bar" returns a property object, and "Foo.bar" returns an unbound method (a function in Python 3). But they are meant as illustrations, not working code samples. Much the same way that we will often talk about "list.append" when what we actually mean is the bound append method on some specific, context-dependent list instance. I am sorry that I did not make that clear and that my terminology was sloppy. But in context, duck-typing classes normally is intended to substitute an instance of one class for an instance of another class. In that case, if IFoo.bar is a property, and Foo.bar is a method, then you cannot substitute an IFoo instance for a Foo instance, or vice versa: ifoo = IFoo() ifoo.bar # returns a computed attribute foo = Foo() foo.bar() # calls the method In the general case, you cannot use ifoo.bar() where foo.bar() is expected, nor can you use foo.bar where ifoo.bar is expected. Just in case it isn't clear what I mean: Suppose the expected interface is that instance.bar is a method that takes no arguments. foo.bar() matches that interface, because bar is a method. But ifoo.bar is a property. Suppose it computes an int result. Then ifoo.bar() will try to call an int, and raise TypeError. So ifoo cannot be used in place of foo, and types IFoo and Foo are not duck-type compatible. Likewise if the expected interface is for a property or attribute, such as ifoo.bar would give. Then foo.bar returns an unbound method. Instead of getting an error there and then, you might not get an error until much later, say: integers = [1, 3, ifoo.bar, foo.bar, 42] # much later y = sum(integers) # raises TypeError because foo.bar is a method So, duck-typing classes IFoo (with bar a property) and Foo (with bar a method) will not in general work, and looks(IFoo).like(Foo) should return False. -- Steven From steve+comp.lang.python at pearwood.info Fri Nov 9 01:37:41 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 09 Nov 2012 06:37:41 GMT Subject: Multi-dimensional list initialization References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <5099bf7d$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: <509ca4b5$0$29980$c3e8da3$5496439d@news.astraweb.com> On Fri, 09 Nov 2012 17:07:09 +1100, Chris Angelico wrote: > On Fri, Nov 9, 2012 at 12:39 PM, Mark Lawrence > wrote: >> On 07/11/2012 01:55, Steven D'Aprano wrote: >>> >>> >>> Who knows? Who cares? Nobody does: >>> >>> n -= n >>> >>> >> But I've seen this scattered through code: >> >> x := x - x - x > > Can you enlighten us as to how this is better than either: > x := -x > or > x := 0 - x > ? I'm not seeing it. I'm hoping that Mark intended it as an example of crappy code he has spotted in some other language rather than a counter-example of something you would do. To be pedantic... there may very well be some (rare) cases where you actually do want x -= x rather than just x = 0. Consider the case where x could be an INF or NAN. Then x -= x should give x = NAN rather than zero. That may be desirable in some cases. At the very least, the compiler should NOT optimize away x = x - x to x = 0 if x could be a float, complex or Decimal. > And I'm not seeing any nonnumeric that would > benefit from being subtracted from itself twice (strings, arrays, sets, > you can subtract them from one another but not usefully more than once). How do you subtract strings? -- Steven From rosuav at gmail.com Fri Nov 9 01:59:36 2012 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 9 Nov 2012 17:59:36 +1100 Subject: Multi-dimensional list initialization In-Reply-To: <509ca4b5$0$29980$c3e8da3$5496439d@news.astraweb.com> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <5099bf7d$0$29980$c3e8da3$5496439d@news.astraweb.com> <509ca4b5$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Nov 9, 2012 at 5:37 PM, Steven D'Aprano wrote: > On Fri, 09 Nov 2012 17:07:09 +1100, Chris Angelico wrote: >> Can you enlighten us as to how this is better than either: >> x := -x >> or >> x := 0 - x >> ? I'm not seeing it. > > I'm hoping that Mark intended it as an example of crappy code he has > spotted in some other language rather than a counter-example of something > you would do. Ohh. Yeah, that figures. Huh. > To be pedantic... there may very well be some (rare) cases where you > actually do want x -= x rather than just x = 0. Consider the case where x > could be an INF or NAN. Then x -= x should give x = NAN rather than zero. > That may be desirable in some cases. > > At the very least, the compiler should NOT optimize away x = x - x to > x = 0 if x could be a float, complex or Decimal. Yep. In the specific case of integers, though, and in the specific instance of CPU registers in assembly language, it's reasonable to optimize it the *other* way - MOV reg,0 is a one-byte opcode and 1, 2, or 4 bytes of immediate data, while SUB reg,reg (or XOR reg,reg) is a two-byte operation regardless of data size. But that's microoptimization that makes, uhh, itself-subtracted-from-itself sense in Python. >> And I'm not seeing any nonnumeric that would >> benefit from being subtracted from itself twice (strings, arrays, sets, >> you can subtract them from one another but not usefully more than once). > > How do you subtract strings? The same way you subtract sets. Same with arrays. Python doesn't do either, but Python also doesn't do the ":=" operator that the example code demonstrated, so I didn't assume Python. Pike v7.8 release 700 running Hilfe v3.5 (Incremental Pike Frontend) > "Hello, world!"-"l"; (1) Result: "Heo, word!" > ({1,2,3,3,2,3,1,2,1})-({2}); (2) Result: ({ /* 6 elements */ 1, 3, 3, 3, 1, 1 }) Python spells it differently: >>> "Hello, world!".replace("l","") 'Heo, word!' Not sure how to do array subtraction other than with filter: >>> list(filter(lambda x: x!=2,[1,2,3,3,2,3,1,2,1])) [1, 3, 3, 3, 1, 1] But there's probably a way (list.remove only takes out the first occurrence, so it's not equivalent). In any case, subtracting something from _itself_ is only going to give you an empty string, array, set, or whatever, and doing so a second time is going to achieve nothing. Hence my comment. But poor code we will always have with us, to paraphrase the Gospel of Matthew. ChrisA From frednotbob at hotmail.ca Fri Nov 9 02:20:54 2012 From: frednotbob at hotmail.ca (Graham Fielding) Date: Thu, 8 Nov 2012 23:20:54 -0800 Subject: Writing game-state data... Message-ID: Hey, folks, me again! I've been puzzling over this for a while now: I'm trying to write data to a file to save the state of my game using the following function: def save_game(): #open a new empty shelve (possibly overwriting an old one) to write the game data file_object = open('savegame.sav', 'wb') file['map'] = map file['objects'] = objects file['player_index'] = objects.index(player) #index of player in objects list file['inventory'] = inventory file['game_msgs'] = game_msgs file['game_state'] = game_state file['stairs_index'] = objects.index(stairs) file['dungeon_level'] = dungeon_level file.close() However, while 'savegame.sav' is created in the directory I specify, the function dies on file['map'] = map. This is the end of the stack trace: File "C:\Python Project\Roguelike.py", line 966, in save_game file['map'] = map TypeError: 'type' object does not support item assignment Now, the map is randomly generated -- could that be an issue? Should I just scrap the current system and use pickle? -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Fri Nov 9 02:27:35 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 09 Nov 2012 07:27:35 +0000 Subject: Multi-dimensional list initialization In-Reply-To: <509ca4b5$0$29980$c3e8da3$5496439d@news.astraweb.com> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <5099bf7d$0$29980$c3e8da3$5496439d@news.astraweb.com> <509ca4b5$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 09/11/2012 06:37, Steven D'Aprano wrote: > On Fri, 09 Nov 2012 17:07:09 +1100, Chris Angelico wrote: > >> On Fri, Nov 9, 2012 at 12:39 PM, Mark Lawrence >> wrote: >>> On 07/11/2012 01:55, Steven D'Aprano wrote: >>>> >>>> >>>> Who knows? Who cares? Nobody does: >>>> >>>> n -= n >>>> >>>> >>> But I've seen this scattered through code: >>> >>> x := x - x - x >> >> Can you enlighten us as to how this is better than either: >> x := -x >> or >> x := 0 - x >> ? I'm not seeing it. > > I'm hoping that Mark intended it as an example of crappy code he has > spotted in some other language rather than a counter-example of something > you would do. Correct, CORAL 66 and pointed out to me by a colleague when another team member had resigned. > > To be pedantic... there may very well be some (rare) cases where you > actually do want x -= x rather than just x = 0. Consider the case where x > could be an INF or NAN. Then x -= x should give x = NAN rather than zero. > That may be desirable in some cases. Interesting what comes up when we get chatting here. I hope we don't get punished for going off topic :) > > At the very least, the compiler should NOT optimize away x = x - x to > x = 0 if x could be a float, complex or Decimal. > X was an int so almost certainly optimised away by the SDL compiler on VMS of 1986 or 1987. -- Cheers. Mark Lawrence. From breamoreboy at yahoo.co.uk Fri Nov 9 02:37:56 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 09 Nov 2012 07:37:56 +0000 Subject: Writing game-state data... In-Reply-To: References: Message-ID: On 09/11/2012 07:20, Graham Fielding wrote: > > Hey, folks, me again! I've been puzzling over this for a while now: I'm trying to write data to a file to save the state of my game using the following function: def save_game(): > #open a new empty shelve (possibly overwriting an old one) to write the game data > file_object = open('savegame.sav', 'wb') > file['map'] = map > file['objects'] = objects > file['player_index'] = objects.index(player) #index of player in objects list > file['inventory'] = inventory > file['game_msgs'] = game_msgs > file['game_state'] = game_state > file['stairs_index'] = objects.index(stairs) > file['dungeon_level'] = dungeon_level > file.close() However, while 'savegame.sav' is created in the directory I specify, the function dies on file['map'] = map. This is the end of the stack trace: > File "C:\Python Project\Roguelike.py", line 966, in save_game > file['map'] = map > TypeError: 'type' object does not support item assignment Now, the map is randomly generated -- could that be an issue? Should I just scrap the current system and use pickle? > Please always give the complete stack trace, it's provided for a purpose. Here I'll grope around in the dark and guess that you need file_object = shelve.open(... -- Cheers. Mark Lawrence. From ulrich.eckhardt at dominolaser.com Fri Nov 9 02:56:22 2012 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Fri, 09 Nov 2012 08:56:22 +0100 Subject: int.__init__ incompatible in Python 3.3 In-Reply-To: References: <481tm9-k6i.ln1@satorlaser.homedns.org> Message-ID: <6ipum9-oim.ln1@satorlaser.homedns.org> Am 08.11.2012 21:29, schrieb Terry Reedy: > On Thu, Nov 8, 2012 at 8:55 AM, Ulrich Eckhardt > wrote: >>> On 3.3, it gives me a "TypeError: object.__init__() takes no >>> parameters". To some extent, this makes sense to me, because the >>> int subobject is not initialized in __init__ but in __new__. As a >>> workaround, I can simple drop the parameter from the call. > > Just drop the do-nothing call. Wait: Which call exactly? Do you suggest that I shouldn't override __init__? The problem is that I need to attach additional info to the int and that I just pass this to the class on contstruction. Or, do you suggest I don't call super().__init__()? That would seem unclean to me. Just for your info, the class mimics a C enumeration, roughly it looks like this: class Foo(int): def __init__(self, value, name): super(Foo, self).__init__(value) self.name = name def __str__(self): return self.name Foo.AVALUE = Foo(1, 'AVALUE') Foo.BVALUE = Foo(2, 'BVALUE') Note that even though I derive from an immutable class, the resulting class is not formally immutable. Maybe exactly that is the thing that the developers did not want me to do? I didn't understand all the implications in the bug ticket you quoted, to be honest. Thank you for your time! Uli From ian.g.kelly at gmail.com Fri Nov 9 03:13:45 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 9 Nov 2012 01:13:45 -0700 Subject: Writing game-state data... In-Reply-To: References: Message-ID: On Fri, Nov 9, 2012 at 12:20 AM, Graham Fielding wrote: > file_object = open('savegame.sav', 'wb') Here you open a file and assign it to "file_object". > file['map'] = map Here you attempt to write to "file" instead of "file_object". "file" is the name of a built-in type, hence your error message. Since you seem to be trying to use shelve, you should also probably be calling shelve.open to open the file, not just open. > file['objects'] = objects > file['player_index'] = objects.index(player) #index of player in > objects list > file['inventory'] = inventory > file['game_msgs'] = game_msgs > file['game_state'] = game_state > file['stairs_index'] = objects.index(stairs) > file['dungeon_level'] = dungeon_level > file.close() Same issue for all these other statements. From jarausch at igpm.rwth-aachen.de Fri Nov 9 04:18:12 2012 From: jarausch at igpm.rwth-aachen.de (Helmut Jarausch) Date: 9 Nov 2012 09:18:12 GMT Subject: Python3.3 str() bug? Message-ID: Hi, probably I'm missing something. Using str(Arg) works just fine if Arg is a list. But str([],encoding='latin-1') gives the error TypeError: coercing to str: need bytes, bytearray or buffer-like object, list found If this isn't a bug how can I use str(Arg,encoding='latin-1') in general. Do I need to flatten any data structure which is normally excepted by str() ? Many thanks for a hint, Helmut. From stefan_ml at behnel.de Fri Nov 9 04:37:11 2012 From: stefan_ml at behnel.de (Stefan Behnel) Date: Fri, 09 Nov 2012 10:37:11 +0100 Subject: Python3.3 str() bug? In-Reply-To: References: Message-ID: Helmut Jarausch, 09.11.2012 10:18: > probably I'm missing something. > > Using str(Arg) works just fine if Arg is a list. > But > str([],encoding='latin-1') > > gives the error > TypeError: coercing to str: need bytes, bytearray or buffer-like object, > list found > > If this isn't a bug how can I use str(Arg,encoding='latin-1') in general. > Do I need to flatten any data structure which is normally excepted by str() ? Funny idea to call this a bug in Python. What your code is asking for is to decode the object you pass in using the "latin-1" encoding. Since a list is not something that is "encoded", let alone in latin-1, you get an error, and actually a rather clear one. Note that this is not specific to Python3.3 or even 3.x. It's the same thing in Py2 when you call the equivalent unicode() function. Stefan From hansmu at xs4all.nl Fri Nov 9 04:49:41 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Fri, 09 Nov 2012 10:49:41 +0100 Subject: Obnoxious postings from Google Groups In-Reply-To: <50999452$0$29980$c3e8da3$5496439d@news.astraweb.com> References: <50989613$0$29980$c3e8da3$5496439d@news.astraweb.com> <50999452$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: <509cd1b5$0$6957$e4fe514c@news2.news.xs4all.nl> On 6/11/12 23:50:59, Steven D'Aprano wrote: > On Tue, 06 Nov 2012 17:16:44 +0000, Prasad, Ramit wrote: > >>> To enter the newline, I typed Ctrl-Q to tell bash to treat the next >>> character as a literal, and then typed Ctrl-J to get a newline. >> >> That sounds complicated, my version of bash lets me type >> 'foobar' for the same effect. > > Well, I learned something new about bash. > > On the other hand, the Ctrl-Q next-char-is-literal trick works for > entering control characters that otherwise don't have a key on the > keyboard. How does that trick work? If I need a control character that is not available in my current keyboard mapping, how would I enter such a character using this Ctrl-Q trick? Just wondering, -- HansM From wxjmfauth at gmail.com Fri Nov 9 05:06:05 2012 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Fri, 9 Nov 2012 02:06:05 -0800 (PST) Subject: Right solution to unicode error? In-Reply-To: References: <09a3d20b-5871-47f4-9218-df119698e405@m4g2000yqf.googlegroups.com> <509AF3EF.8050108@gmail.com> <65910cea-f145-409c-a579-9f0cda499546@googlegroups.com> Message-ID: <65d2286f-78dc-4eb8-945c-d15fb41a8232@googlegroups.com> Le jeudi 8 novembre 2012 21:42:58 UTC+1, Ian a ?crit?: > On Thu, Nov 8, 2012 at 12:54 PM, wrote: > > > Font has nothing to do here. > > > You are "simply" wrongly encoding your "unicode". > > > > > >>>> '\u2013' > > > '?' > > >>>> '\u2013'.encode('utf-8') > > > b'\xe2\x80\x93' > > >>>> '\u2013'.encode('utf-8').decode('cp1252') > > > '???' > > > > No, it seriously is the font. This is what I get using the default > > ("Raster") font: > > > > C:\>chcp 65001 > > Active code page: 65001 > > > > C:\>c:\python33\python > > Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 > > 32 bit (Intel)] on win32 > > Type "help", "copyright", "credits" or "license" for more information. > > >>> '\u2013' > > '???' > > >>> import sys > > >>> sys.stdout.buffer.write('\u2013\n'.encode('utf-8')) > > ??? > > 4 > > > > I should note here that the characters copied and pasted do not > > correspond to the glyphs actually displayed in my terminal window. In > > the terminal window I actually see: > > > > ??? > > > > If I change the font to Lucida Console and run the *exact same code*, > > I get this: > > > > C:\>chcp 65001 > > Active code page: 65001 > > > > C:\>c:\python33\python > > Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 > > 32 bit (Intel)] on win32 > > Type "help", "copyright", "credits" or "license" for more information. > > >>> '\u2013' > > '?' > > > > >>> import sys > > >>> sys.stdout.buffer.write('\u2013\n'.encode('utf-8')) > > ? > > 4 > > > > Why is the font important? I have no idea. Blame Microsoft. --------- If you have something like this '???'; in Unicode nomenclature: >>> import unicodedata as ud >>> for c in '???': ... ud.name(c) ... 'GREEK CAPITAL LETTER GAMMA' 'LATIN CAPITAL LETTER C WITH CEDILLA' 'LATIN SMALL LETTER O WITH CIRCUMFLEX' it is a sign of a "cp437" somewhere. >>> '\u2013'.encode('utf-8').decode('cp437') '???' On Windows 7. I do not remember having once a "coding of the caracters" issue on XP. jmf From frednotbob at hotmail.ca Fri Nov 9 05:12:41 2012 From: frednotbob at hotmail.ca (Graham Fielding) Date: Fri, 9 Nov 2012 02:12:41 -0800 Subject: Writing game-state data... In-Reply-To: References: , Message-ID: > To: python-list at python.org > From: breamoreboy at yahoo.co.uk > Subject: Re: Writing game-state data... > Date: Fri, 9 Nov 2012 07:37:56 +0000 > > On 09/11/2012 07:20, Graham Fielding wrote: > > > > Hey, folks, me again! I've been puzzling over this for a while now: I'm trying to write data to a file to save the state of my game using the following function: def save_game(): > > #open a new empty shelve (possibly overwriting an old one) to write the game data > > file_object = open('savegame.sav', 'wb') > > file['map'] = map > > file['objects'] = objects > > file['player_index'] = objects.index(player) #index of player in objects list > > file['inventory'] = inventory > > file['game_msgs'] = game_msgs > > file['game_state'] = game_state > > file['stairs_index'] = objects.index(stairs) > > file['dungeon_level'] = dungeon_level > > file.close() However, while 'savegame.sav' is created in the directory I specify, the function dies on file['map'] = map. This is the end of the stack trace: > > File "C:\Python Project\Roguelike.py", line 966, in save_game > > file['map'] = map > > TypeError: 'type' object does not support item assignment Now, the map is randomly generated -- could that be an issue? Should I just scrap the current system and use pickle? > > > > Please always give the complete stack trace, it's provided for a > purpose. Here I'll grope around in the dark and guess that you need > file_object = shelve.open(... > > -- > Cheers. > > Mark Lawrence. > > -- > http://mail.python.org/mailman/listinfo/python-list Here's the full stack trace: File "C:\Python Project\Roguelike.py", line 1048, in main_menu() File "C:\Python Project\Roguelike.py", line 1030, in main_menu play_game() File "C:\Python Project\Roguelike.py", line 1007, in play_game player_action = handle_keys() File "C:\Python Project\Roguelike.py", line 717, in handle_keys quit_menu() #exit game File "C:\Python Project\Roguelike.py", line 698, in quit_menu save_game() File "C:\Python Project\Roguelike.py", line 909, in save_game file['map'] = map TypeError: 'type' object does not support item assignment >>> What I'm trying to do is figure out a way to get the game to save its state; I compiled it to an EXE using py2exe, but after that, the game refuses to hold onto its data. Thanks for all the help! -------------- next part -------------- An HTML attachment was scrubbed... URL: From andriy.kornatskyy at live.com Fri Nov 9 05:26:36 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Fri, 9 Nov 2012 13:26:36 +0300 Subject: duck typing assert In-Reply-To: References: , <509c412d$0$29980$c3e8da3$5496439d@news.astraweb.com>, , Message-ID: Thank you for all comments. > It?makes very good sense to say: > > duckmatch(IFoo).compare(Foo) Since we do duck match of IFoo... but there is no `duck match`, there is `duck test`.?I believe instead of `compare` is more readable with `equals`. Than it is more from?mathematics - precise answer...?that you can not guarantee at all in dynamic programming language. So it false to use such wording to reflect this check. We can only make an assumption that one looks like the other (similar)... with some limitation of cause... understanding what is `duck test`. http://en.wikipedia.org/wiki/Duck_test The intent is to make such language `construct` so it reads as English sentence that make sense, and not mandatory `pythonic` way (readability counts, java smokes aside). is_similar(Foo).to(IFoo) # <= but we lost `duck test` sense here? Words `looks` and `like` are coming from duck test and point also direction: # 1 looks(Foo).like(IFoo, notice=['__len__'], ignore_funcs=['foo'], ignore_argspec['bar']) English sentence equivalent: if functions in Foo looks like one in IFoo than, probably, IFoo can be replaced with Foo; notice to check __len__, it is safe to ignore function `foo` and arguments passed to `bar`. # 2 looks(Foo, notice=['__len__'], ignore_funcs=['foo'], ignore_argspec['bar']).like(IFoo) English sentence equivalent:?while looking at Foo notice to check `__len__`, it is safe to ignore function `foo` and?arguments passed to `bar`, than probably it like IFoo. I think #1 is easier to understand once it is written. Thoughts? Also construction?looks(Foo).like(IFoo) points direction of check. It you need the two be replaceable you need two asserts: assert looks(Foo).like(IFoo) assert looks(IFoo).like(Foo) Thanks. Andriy Kornatskyy ---------------------------------------- > Date: Fri, 9 Nov 2012 17:14:49 +1100 > Subject: Re: duck typing assert > From: rosuav at gmail.com > To: python-list at python.org > > On Fri, Nov 9, 2012 at 12:00 PM, Ian Kelly wrote: > > looks(Foo).like(IFoo), on the other hand, is crystal clear about which > > argument is which. > > I'm not so sure that it is, tbh. If you read it like an English > sentence, it's clearly testing whether Foo matches the template in > IFoo, but which are you more likely to do: test one class to see if it > satisfies lots of templates, or test one template against every class > you meet? I think probably the latter is, if not more likely than the > former, at least sufficiently plausible as to create confusion. It > makes very good sense to say: > > duckmatch(IFoo).compare(Foo) > > ie with the arguments the other way. > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list From andrea.crotti.0 at gmail.com Fri Nov 9 05:39:21 2012 From: andrea.crotti.0 at gmail.com (andrea crotti) Date: Fri, 9 Nov 2012 10:39:21 +0000 Subject: creating size-limited tar files In-Reply-To: References: <509ab0fa$0$6636$9b4e6d93@newsspool2.arcor-online.net> <509AD812.2060605@gmail.com> Message-ID: Anyway in the meanwhile I implemented this tar and split in this way below. It works very well and it's probably much faster, but the downside is that I give away control to tar and split.. def tar_and_split(inputfile, output, bytes_size=None): """Take the file containing all the files to compress, the bytes desired for the split and the base name of the output file """ # cleanup first for fname in glob(output + "*"): logger.debug("Removing old file %s" % fname) remove(fname) out = '-' if bytes_size else (output + '.tar.gz') cmd = "tar czpf {} $(cat {})".format(out, inputfile) if bytes_size: cmd += "| split -b {} -d - {}".format(bytes_size, output) logger.info("Running command %s" % cmd) proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = proc.communicate() if err: logger.error("Got error messages %s" % err) logger.info("Output %s" % out) if proc.returncode != 0: logger.error("Something failed running %s, need to re-run" % cmd) return False From dfnsonfsduifb at gmx.de Fri Nov 9 05:48:10 2012 From: dfnsonfsduifb at gmx.de (Johannes Bauer) Date: Fri, 09 Nov 2012 11:48:10 +0100 Subject: Relative imports in packages Message-ID: Hi list, I've these two minor problems which bothered me for quite some time, maybe you can help me. I'm using Python 3.2. For some project I have a component in its own package. Let's say the structure looks like this: pkg/__init__.py pkg/Foo.py pkg/Bar.py Foo.py and Bar.py contain their classes "Foo" and "Bar", __init__.py looks like this: from .Foo import Foo from .Bar import Bar This allows me to "naturally" access classes, i.e. from my main program: import pkg pkg.Foo("initme") or from pkg import Foo Foo("initme") So far, so good. Now let's say Bar uses Foo, i.e. in Bar's header is something like: from .Foo import Foo If this all weren't a package the declaration would just read "from Foo import Foo" and I could easily append a small unit-test to Bar: if __name__ == "__main__": # test pass However, when using a package this fails: Obviously, when I directly go into the pkg/ subdirectory and try to execute Bar.py, the import of Foo fails and it doesn't work. Is there a nice solution to this or am I doing it all wrong? Then another minor question: Let's say my __init__.py contains a constant: VERSION = "0.01" >From my main program I can easily import that: from pkg import VERSION print(VERSION) However, from Foo.py, I cannot seem to get the syntax right: from . import VERSION File "Foo.py", line 10, in from . import VERSION ImportError: cannot import name VERSION How do I do this right? Thanks for your advice, Best regards, Johannes -- >> Wo hattest Du das Beben nochmal GENAU vorhergesagt? > Zumindest nicht ?ffentlich! Ah, der neueste und bis heute genialste Streich unsere gro?en Kosmologen: Die Geheim-Vorhersage. - Karl Kaos ?ber R?diger Thomas in dsa From insideshoes at gmail.com Fri Nov 9 05:53:56 2012 From: insideshoes at gmail.com (inshu chauhan) Date: Fri, 9 Nov 2012 11:53:56 +0100 Subject: error In-Reply-To: <509BDEF1.50706@davea.name> References: <78f26f00-2c91-47b3-aef0-3eaeb8ae6f91@u4g2000pbo.googlegroups.com> <509BDEF1.50706@davea.name> Message-ID: http://www.ymlgroup.com/obscurehiddenlocation/downloads site, and > imported it using from notzanzibar import DataType. > No i haven't downloaded it.. and this site is not opening... > > Then you'd have instantiated it in some code like > data = DataType(filename) > > and then the type of data would be notzanzibar.DataType and the docs > would be probably available somewhere on www.invalid.com/docs > This site is also blocked here. > > The only new guess: > > A 3D image would presumably have 3 subscripts, and you're only supplying > two. > Yes a 3D image has 3 subscripts but m trying to access all three through using only 2 subsscripts i.e X and Y > > If you want help, be explicit: > > 1) what version of CPython are you using, and on what OS? > I am using 2.7.3 on windows 7 2) what web site did you download some extra library from ? > The only extra libary i am using is Opencv , downloaded from http://sourceforge.net/projects/opencvlibrary/ > 3) what import statement did you use ? > import cv 4) How are all those global variables initialized ? > see attached file > 5) What command did you use to start the script ? Did you run it from > command.com, or from some IDE ? > Yes I am running it through IDLE GUI > 5) Exactly what error message did you get, including the traceback ? > Traceback (most recent call last): File "Z:\modules\Masking_an_image_dynamically.py", line 155, in AccessPixels(data) File "Z:\modules\.py", line 147, in AccessPixels CreateMask(data, x, y) File "Z:\modules\new_classification.py", line 110, in CreateMask point = data[iy, ix ] error: index is out of range The line numbers here and the file attached may be different because I have removed a lot of print statements which I was using to debug the error.. > 6) What have you done to try to figure out your own error? > I have trying print out variables and Indices at each step.. Zero Piraeus : Where are you ? -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: Masking_an_image_dynamically.py Type: application/octet-stream Size: 2984 bytes Desc: not available URL: From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Fri Nov 9 06:02:31 2012 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Fri, 09 Nov 2012 12:02:31 +0100 Subject: Obnoxious postings from Google Groups In-Reply-To: References: <52847c35-388c-4758-b72d-1d1859b788a3@googlegroups.com> <8370412f-5176-45ff-87eb-72cbca5149d5@googlegroups.com> <5055a1a6$0$29981$c3e8da3$5496439d@news.astraweb.com> <4f9d9a0b-539a-4b6a-af3e-b02d1f4006ee@googlegroups.com> <87wqzum7h6.fsf_-_@benfinney.id.au> <6c732de0-b10f-4d40-853c-f62682970117@rg9g2000pbc.googlegroups.com> Message-ID: Am 31.10.2012 06:39 schrieb Robert Miles: > For those of you running Linux: You may want to look into whether > NoCeM is compatible with your newsreader and your version of Linux. This sounds as if it was intrinsically impossible to evaluate NoCeMs in Windows. If someone writes a software for it, it can be run wherever desired. Thomas From jarausch at igpm.rwth-aachen.de Fri Nov 9 06:08:14 2012 From: jarausch at igpm.rwth-aachen.de (Helmut Jarausch) Date: 9 Nov 2012 11:08:14 GMT Subject: Python3.3 str() bug? References: Message-ID: On Fri, 09 Nov 2012 10:37:11 +0100, Stefan Behnel wrote: > Helmut Jarausch, 09.11.2012 10:18: >> probably I'm missing something. >> >> Using str(Arg) works just fine if Arg is a list. >> But >> str([],encoding='latin-1') >> >> gives the error >> TypeError: coercing to str: need bytes, bytearray or buffer-like object, >> list found >> >> If this isn't a bug how can I use str(Arg,encoding='latin-1') in general. >> Do I need to flatten any data structure which is normally excepted by str() ? > > Funny idea to call this a bug in Python. What your code is asking for is to > decode the object you pass in using the "latin-1" encoding. Since a list is > not something that is "encoded", let alone in latin-1, you get an error, > and actually a rather clear one. > > Note that this is not specific to Python3.3 or even 3.x. It's the same > thing in Py2 when you call the equivalent unicode() function. > For me it's not funny, at all. Whenever Python3 encounters a bytestring it needs an encoding to convert it to a string. If I feed a list of bytestrings or a list of list of bytestrings to 'str' , etc, it should use the encoding for each bytestring component of the given data structure. How can I convert a data strucure of arbitrarily complex nature, which contains bytestrings somewhere, to a string? This problem has arisen while converting a working Python2 script to Python3.3. Since Python2 doesn't have bytestrings it just works. Tell me how to convert str(obj) from Python2 to Python3 if obj is an arbitrarily complex data structure containing bytestrings somewhere which have to be converted to strings with a given encoding? Helmut. From steve+comp.lang.python at pearwood.info Fri Nov 9 06:30:42 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 09 Nov 2012 11:30:42 GMT Subject: Obnoxious postings from Google Groups References: <50989613$0$29980$c3e8da3$5496439d@news.astraweb.com> <50999452$0$29980$c3e8da3$5496439d@news.astraweb.com> <509cd1b5$0$6957$e4fe514c@news2.news.xs4all.nl> Message-ID: <509ce962$0$29980$c3e8da3$5496439d@news.astraweb.com> On Fri, 09 Nov 2012 10:49:41 +0100, Hans Mulder wrote: > On 6/11/12 23:50:59, Steven D'Aprano wrote: >> On Tue, 06 Nov 2012 17:16:44 +0000, Prasad, Ramit wrote: >> >>>> To enter the newline, I typed Ctrl-Q to tell bash to treat the next >>>> character as a literal, and then typed Ctrl-J to get a newline. >>> >>> That sounds complicated, my version of bash lets me type >>> 'foobar' for the same effect. >> >> Well, I learned something new about bash. >> >> On the other hand, the Ctrl-Q next-char-is-literal trick works for >> entering control characters that otherwise don't have a key on the >> keyboard. > > How does that trick work? If I need a control character that is not > available in my current keyboard mapping, how would I enter such a > character using this Ctrl-Q trick? This only works if you are running a Linux or Unix shell with the libreadline library installed. This should work on nearly any modern Linux system with the bash shell. I don't know about other shells. On Mac OS, the relevant library is called libedit instead, and the details may be different. On Windows, you're out of luck. Anyway, using Linux and bash: at the shell, if I type Ctrl-U, that is interpreted by the shell to mean "clear the line currently being edited". So if I type Ctrl-U, the line is cleared. But if I type Ctrl-Q first, then Ctrl-U, instead readline enters a literal ^U character (ASCII value 0x15 = NAK Negative AcKnowledgment) into the line editing buffer. The same trick should work in the Python interactive editor: >>> ord('^U') # type Ctrl-Q Ctrl-U to get the ^U char 21 Note that this may or may not work in IDEs such as IDLE. Many IDEs do their own thing for editing, and there's no guarantee they will support this functionality. One last comment: readline is very configurable, and the command to insert the next character could be just about anything. But Ctrl-Q is the standard. -- Steven From hansmu at xs4all.nl Fri Nov 9 06:34:27 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Fri, 09 Nov 2012 12:34:27 +0100 Subject: Obnoxious postings from Google Groups In-Reply-To: <5099a7ba$0$29980$c3e8da3$5496439d@news.astraweb.com> References: <50989613$0$29980$c3e8da3$5496439d@news.astraweb.com> <50999452$0$29980$c3e8da3$5496439d@news.astraweb.com> <5099a7ba$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: <509cea43$0$6900$e4fe514c@news2.news.xs4all.nl> On 7/11/12 01:13:47, Steven D'Aprano wrote: > On Tue, 06 Nov 2012 23:08:11 +0000, Prasad, Ramit wrote: > >> Steven D'Aprano wrote: >>> >>> On Tue, 06 Nov 2012 17:16:44 +0000, Prasad, Ramit wrote: >>> >>>>> To enter the newline, I typed Ctrl-Q to tell bash to treat the next >>>>> character as a literal, and then typed Ctrl-J to get a newline. >>>> >>>> That sounds complicated, my version of bash lets me type >>>> 'foobar' for the same effect. >>> >>> Well, I learned something new about bash. >>> >>> On the other hand, the Ctrl-Q next-char-is-literal trick works for >>> entering control characters that otherwise don't have a key on the >>> keyboard. >>> >>> >> Would you mind elaborating on how this works? I know it's not a bash >> list, but I do not understand how ctrl-J is considered a literal. >> Obviously, I must have a different definition of "literal". Where can I >> find a list of other literals? My Google-fu is being weak today. :( > > I'm not an expert, so the following may not be exactly correct. As I > understand it, when you hit a key on the keyboard, it sends the character > you typed to the operating system. (The OS can then remap keys, generate > keyboard events including a timestamp, etc.) > > Hit the J key, and the event includes character "j". Hit Shift-J, and > character "J" is sent. Hit Ctrl-J, and the character sent is the ASCII > control character ^J, or newline. (Technically, the name for ASCII 10 is > "linefeed" rather than "newline".) Actually, the correct name for this character is OS-dependant: The ASCII standard prescribes that if an OS chooses to use a single character as its line terminator, then it must be this one, and one should call it "newline". Otherwise, it's name is "linefeed". So, the correct name is "newline" on Posix system, but "linefeed" on Windows. > Similarly, other control character combinations send other control codes: > > ^A = ASCII 0x01 Start Of Heading > ^L = ASCII 0xFF Formfeed \f > ^M = ASCII 0x0D Carriage Return \r > > etc. > > http://en.wikipedia.org/wiki/C0_and_C1_control_codes > > > When readline is enabled in bash, one of the standard editing commands is > that C-q (usually ctrl-Q on the keyboard) instructs readline to treat the > next key as a literal. So Ctrl-Q followed by Backspace won't delete the > previous character, but insert a literal DEL 0x7F character. It depends on what mode bash is in. In Emacs mode, C-q works as you describe, but in Vi mode you'd use C-v. Doesn't everybody run bash in Vi mode :-? > (One of those historical quirks is that on most(?) keyboards, the > Backspace key generates a DEL character rather than the ^H backspace > control code, and the Delete key generates an escape sequence. Go figure.) Another quirk is that on most keyboards the "enter" key generates a Carriage Return, which the terminal driver than converts to a Newline, if icrlf mode is active. (Shouldn't that be called "icrnl" mode?) Hope this helps, -- HansM From steve+comp.lang.python at pearwood.info Fri Nov 9 06:37:45 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 09 Nov 2012 11:37:45 GMT Subject: int.__init__ incompatible in Python 3.3 References: <481tm9-k6i.ln1@satorlaser.homedns.org> <6ipum9-oim.ln1@satorlaser.homedns.org> Message-ID: <509ceb08$0$29980$c3e8da3$5496439d@news.astraweb.com> On Fri, 09 Nov 2012 08:56:22 +0100, Ulrich Eckhardt wrote: > Am 08.11.2012 21:29, schrieb Terry Reedy: >> On Thu, Nov 8, 2012 at 8:55 AM, Ulrich Eckhardt >> wrote: >>>> On 3.3, it gives me a "TypeError: object.__init__() takes no >>>> parameters". To some extent, this makes sense to me, because the int >>>> subobject is not initialized in __init__ but in __new__. As a >>>> workaround, I can simple drop the parameter from the call. >> >> Just drop the do-nothing call. > > Wait: Which call exactly? > > Do you suggest that I shouldn't override __init__? The problem is that I > need to attach additional info to the int and that I just pass this to > the class on contstruction. No, of course not. If you need to override __init__, you need to override __init__. > Or, do you suggest I don't call super().__init__()? That would seem > unclean to me. On the contrary: calling super().__init__ when the superclass does something you don't want (i.e. raises an exception) is unclean. Since the superclass __init__ does nothing, you don't need to call it. Only inherit behaviour that you actually *want*. In Python 3.3: py> class X(int): ... def __init__(self, *args): ... super().__init__(*args) # does nothing, call it anyway ... py> x = X(22) Traceback (most recent call last): File "", line 1, in File "", line 3, in __init__ TypeError: object.__init__() takes no parameters It is apparently an oversight, or a bug, that it ever worked in older versions. > Note that even though I derive from an immutable class, the resulting > class is not formally immutable. Maybe exactly that is the thing that > the developers did not want me to do? Nope, that's irrelevant. Attaching attributes to an otherwise immutable object is fine. -- Steven From insideshoes at gmail.com Fri Nov 9 06:47:35 2012 From: insideshoes at gmail.com (inshu chauhan) Date: Fri, 9 Nov 2012 12:47:35 +0100 Subject: error In-Reply-To: References: <78f26f00-2c91-47b3-aef0-3eaeb8ae6f91@u4g2000pbo.googlegroups.com> <509BDEF1.50706@davea.name> Message-ID: I attached a wrong file...Right file is attached here On Fri, Nov 9, 2012 at 11:53 AM, inshu chauhan wrote: > > > > > http://www.ymlgroup.com/obscurehiddenlocation/downloads site, and >> imported it using from notzanzibar import DataType. >> > > No i haven't downloaded it.. and this site is not opening... > >> >> Then you'd have instantiated it in some code like >> data = DataType(filename) >> >> and then the type of data would be notzanzibar.DataType and the docs >> would be probably available somewhere on www.invalid.com/docs >> > > This site is also blocked here. > >> >> The only new guess: >> >> A 3D image would presumably have 3 subscripts, and you're only supplying >> two. >> > > Yes a 3D image has 3 subscripts but m trying to access all three through > using only 2 subsscripts i.e X and Y > > >> >> If you want help, be explicit: >> >> 1) what version of CPython are you using, and on what OS? >> > > I am using 2.7.3 on windows 7 > > 2) what web site did you download some extra library from ? >> > > The only extra libary i am using is Opencv , downloaded from > http://sourceforge.net/projects/opencvlibrary/ > > > >> 3) what import statement did you use ? >> > > import cv > > 4) How are all those global variables initialized ? >> > see attached file > >> 5) What command did you use to start the script ? Did you run it from >> command.com, or from some IDE ? >> > > Yes I am running it through IDLE GUI > > >> 5) Exactly what error message did you get, including the traceback ? >> > > Traceback (most recent call last): > File "Z:\modules\Masking_an_image_dynamically.py", line 155, in > AccessPixels(data) > File "Z:\modules\.py", line 147, in AccessPixels > CreateMask(data, x, y) > File "Z:\modules\new_classification.py", line 110, in CreateMask > point = data[iy, ix ] > > error: index is out of range > > The line numbers here and the file attached may be different because I > have removed a lot of print statements which I was using to debug the > error.. > > > >> 6) What have you done to try to figure out your own error? >> > > I have trying print out variables and Indices at each step.. > > > Zero Piraeus : Where are you ? > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: Masking_an_image_dynamically.py Type: application/octet-stream Size: 3259 bytes Desc: not available URL: From rosuav at gmail.com Fri Nov 9 07:22:04 2012 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 9 Nov 2012 23:22:04 +1100 Subject: Python3.3 str() bug? In-Reply-To: References: Message-ID: On Fri, Nov 9, 2012 at 10:08 PM, Helmut Jarausch wrote: > For me it's not funny, at all. His description "funny" was in reference to the fact that you described this as a bug. This is a heavily-used mature language; bugs as fundamental as you imply are unlikely to exist (consequences of design decisions there will be, but not outright bugs, usually); extraordinary claims require extraordinary evidence. > Whenever Python3 encounters a bytestring it needs an encoding to convert it to > a string. If I feed a list of bytestrings or a list of list of bytestrings to > 'str' , etc, it should use the encoding for each bytestring component of the > given data structure. > > How can I convert a data strucure of arbitrarily complex nature, which contains > bytestrings somewhere, to a string? Okay, now we're getting somewhere. What you really should be doing is not transforming the whole structure, but explicitly transforming each part inside it. I recommend you stop fighting the language and start thinking about your data as either *bytes* or *characters* and using the appropriate data types (bytes or str) everywhere. You'll then find that it makes perfect sense to explicitly translate (en/decode) from one to another, but it doesn't make sense to encode a list in UTF-8 or decode a dictionary from Latin-1. > This problem has arisen while converting a working Python2 script to Python3.3. > Since Python2 doesn't have bytestrings it just works. Actually it does; it just calls them "str". And there's a Unicode string type, called "unicode", which is (more or less) the thing that Python 3 calls "str". You may be able to do some kind of recursive cast that, in one sweep of your data structure, encodes all str objects into bytes using a given encoding (or the reverse thereof). But I don't think this is the best way to do things. ChrisA From ulrich.eckhardt at dominolaser.com Fri Nov 9 07:52:22 2012 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Fri, 09 Nov 2012 13:52:22 +0100 Subject: int.__init__ incompatible in Python 3.3 In-Reply-To: <509ceb08$0$29980$c3e8da3$5496439d@news.astraweb.com> References: <481tm9-k6i.ln1@satorlaser.homedns.org> <6ipum9-oim.ln1@satorlaser.homedns.org> <509ceb08$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: <6tavm9-usn.ln1@satorlaser.homedns.org> Am 09.11.2012 12:37, schrieb Steven D'Aprano: > On Fri, 09 Nov 2012 08:56:22 +0100, Ulrich Eckhardt wrote: >> Or, do you suggest I don't call super().__init__()? That would seem >> unclean to me. > > On the contrary: calling super().__init__ when the superclass does > something you don't want (i.e. raises an exception) is unclean. > > Since the superclass __init__ does nothing, you don't need to call it. > Only inherit behaviour that you actually *want*. That one's hard to swallow for me, but maybe this is because I don't understand the Python object model sufficiently. The problem I have here is that not forwarding the __init__() to the baseclass could mean that necessary initializations are not performed, although in this very specify case I see that there aren't any. It still seems a bit like relying on an implementation details. Anyhow, I'll have to do some more reading on the the construction of objects in Python, maybe then it'll all make sense. Until then, thanks everybody for nudging me in the right direction! Uli From artie.ziff at gmail.com Fri Nov 9 07:54:43 2012 From: artie.ziff at gmail.com (Artie Ziff) Date: Fri, 09 Nov 2012 04:54:43 -0800 Subject: xml data or other? Message-ID: <509CFD13.9080206@gmail.com> Hello, I want to process XML-like data like this: ACPI (Advanced Control Power & Integration) testscript for 2.5 kernels. <\description> ltp/testcases/kernel/device-drivers/acpi/ltpacpi.sh <\test_location> <\testname> After manually editing the data above, the python module xml.etree.ElementTree parses it without failing due to error in the data structure. Edits were substituting '/' for '\' on the end tags, and adding the following structure: ... <\testname> Is there a name for the format above (perhaps xhtml)? I'd like to find a python module that can translate it to proper xml. Does one exist? etree? Many thanks! az From jarausch at igpm.rwth-aachen.de Fri Nov 9 08:13:58 2012 From: jarausch at igpm.rwth-aachen.de (Helmut Jarausch) Date: 9 Nov 2012 13:13:58 GMT Subject: Python3.3 str() bug? References: Message-ID: On Fri, 09 Nov 2012 23:22:04 +1100, Chris Angelico wrote: > On Fri, Nov 9, 2012 at 10:08 PM, Helmut Jarausch > wrote: >> For me it's not funny, at all. > > His description "funny" was in reference to the fact that you > described this as a bug. This is a heavily-used mature language; bugs > as fundamental as you imply are unlikely to exist (consequences of > design decisions there will be, but not outright bugs, usually); > extraordinary claims require extraordinary evidence. Just for the record. I first discovered a real bug with Python3 when using os.walk on a file system containing non-ascii characters in file names. I encountered a very strange behavior (I still would call it a bug) when trying to put non-ascii characters in email headers. This has only been solved satisfactorily in Python3.3. > >> Whenever Python3 encounters a bytestring it needs an encoding to convert it to >> a string. If I feed a list of bytestrings or a list of list of bytestrings to >> 'str' , etc, it should use the encoding for each bytestring component of the >> given data structure. >> >> How can I convert a data strucure of arbitrarily complex nature, which contains >> bytestrings somewhere, to a string? > > Okay, now we're getting somewhere. > > What you really should be doing is not transforming the whole > structure, but explicitly transforming each part inside it. I > recommend you stop fighting the language and start thinking about your > data as either *bytes* or *characters* and using the appropriate data > types (bytes or str) everywhere. You'll then find that it makes > perfect sense to explicitly translate (en/decode) from one to another, > but it doesn't make sense to encode a list in UTF-8 or decode a > dictionary from Latin-1. > >> This problem has arisen while converting a working Python2 script to Python3.3. >> Since Python2 doesn't have bytestrings it just works. > > Actually it does; it just calls them "str". And there's a Unicode > string type, called "unicode", which is (more or less) the thing that > Python 3 calls "str". > > You may be able to do some kind of recursive cast that, in one sweep > of your data structure, encodes all str objects into bytes using a > given encoding (or the reverse thereof). But I don't think this is the > best way to do things. Thanks, but in my case the (complex) object is returned via ctypes from the aspell library. I still think that a standard function in Python3 which is able to 'stringify' objects should take an encoding parameter. Thanks, Helmut. From steve+comp.lang.python at pearwood.info Fri Nov 9 08:36:39 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 09 Nov 2012 13:36:39 GMT Subject: duck typing assert References: <509c412d$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: <509d06e7$0$29980$c3e8da3$5496439d@news.astraweb.com> On Thu, 08 Nov 2012 18:00:58 -0700, Ian Kelly wrote: > On Thu, Nov 8, 2012 at 4:33 PM, Steven D'Aprano > wrote: >> On Thu, 08 Nov 2012 20:34:58 +0300, Andriy Kornatskyy wrote: >> >>> People who come from strongly typed languages that offer interfaces >>> often are confused by lack of one in Python. Python, being dynamic >>> typing programming language, follows duck typing principal. It can as >>> simple as this: >>> >>> assert looks(Foo).like(IFoo) >> >> How very cute. And I don't mean that in a good way. >> >> Why is this a class with a method, instead of a function that takes two >> class arguments (plus any optional arguments needed)? >> >> looks_like(Foo, IFoo) >> >> is less "cute", reads better to English speakers, and much more >> Pythonic. This isn't Java, not everything needs to be a class. > > I disagree. Does that test whether Foo looks like IFoo, or IFoo looks > like Foo? What's the difference? "Looks like" is a symmetric comparison, like "equal" and "almost equal", but not "subset", "less than" etc. If x looks like y, then necessarily y must look like x. If Katy Perry looks like Zooey Deschanel, then it stands to reason that Zooey Deschanel looks like Katy Perry. James Woods looks like Erwin Schroedinger, and Erwin Schroedinger looks like James Woods. http://tvrefill.com/wp-content/uploads/2010/12/zooey-deschanel.jpg http://cheezburger.com/6704400128 So in that sense, looks(Spam).like(Ham) must always be the same as looks(Ham).like(Spam), and the order of operators doesn't matter. But that's not what we want! And to his credit, that's not what Andriy Kornatskyy's code actually implements. The problem is with the name, which is actively misleading by suggesting a symmetrical comparison for one which is not symmetrical. Suppose we want to make a movie with Zooey Deschanel, but she's not available, so we want a replacement who is duck-type compatible with her. The replacement doesn't need to be a Deschanel sister, but she does need to do *at least* everything Zooey can do. If she can do more, that's fine, but she can't do less. Since Katy Perry can do everything Zooey can do, PLUS she sings, we can replace Zooey with Katy: Katy is duck-type compatible with Zooey. But we can't replace Katy with Zooey, because Zooey can't sing.[1] (I think... I really don't actually know if Zooey Deschanel can sing or not. Just go along with the example.) The point I am making is that "looks like" is not a good description for this function. "Looks like" must be symmetrical, but the function we actually want is not symmetrical. It is actually a "subset" type relationship: given "looks(Zooey).like(Katy)", it checks that the public methods etc. of Zooey are a subset of the methods of Katy. That is, that instances of Katy can be used instead of instances of Zooey. Or is it the other way? Damned if I know. Let's find out: class Zooey: def act(self): pass class Katy: def act(self): pass def sing(self): pass py> looks(Zooey).like(Katy) __main__:2: UserWarning: 'sing': is missing. False I guessed wrong. The looks.like method as implemented tests that the right-hand size operand is a subset of the right-hand-side: py> looks(Katy).like(Zooey) True I argue that this is the wrong way around. (Even if it isn't the wrong way around, it certainly isn't clear or obvious which way you have to write the operands!) Consider this use-case: candidates = [Hilary, Jennifer, Katy] expected = looks(Zooey) # instantiate the looks class once only for actor in candidates: if expected.like(actor): make_movie_with(actor()) That's much nicer and more efficient than the way you have to write it now: candidates = [Hilary, Jennifer, Katy] for actor in candidates: # instantiate the looks class every time we want to test another class if looks(actor).like(Zooey): make_movie_with(actor()) So... it's a cute name, that sounds English-like. But it doesn't actually describe what the function does, it is wasteful for at least one useful use-case, and it's not clear which order you have to supply the two arguments. > looks(Foo).like(IFoo), on the other hand, is crystal clear about which > argument is which. I hope that by now you can see why I say that it is as clear as mud. [1] Some people might argue that neither can Katy Perry. -- Steven From rustompmody at gmail.com Fri Nov 9 08:50:15 2012 From: rustompmody at gmail.com (rusi) Date: Fri, 9 Nov 2012 05:50:15 -0800 (PST) Subject: xml data or other? References: Message-ID: <96b24715-cb4b-4588-844e-fc2e2f51a170@m4g2000pbd.googlegroups.com> On Nov 9, 5:54?pm, Artie Ziff wrote: > Hello, > > I want to process XML-like data like this: > Edits were substituting '/' for '\' on the end tags, and adding the > following structure: If thats all you want, you can try the following: # obviously this should come from a file input= """ ACPI (Advanced Control Power & Integration) testscript for 2.5 kernels. <\description> ltp/testcases/kernel/device-drivers/acpi/ltpacpi.sh <\test_location> <\testname>""" prefix = """ """ postfix = """""" correctedInput = prefix + input.replace("\\", "/") + postfix # submit correctedinput to etree From insideshoes at gmail.com Fri Nov 9 08:59:24 2012 From: insideshoes at gmail.com (inshu chauhan) Date: Fri, 9 Nov 2012 14:59:24 +0100 Subject: error In-Reply-To: References: <78f26f00-2c91-47b3-aef0-3eaeb8ae6f91@u4g2000pbo.googlegroups.com> <509BDEF1.50706@davea.name> Message-ID: Please Ignore the above two files attached,,, See this one On Fri, Nov 9, 2012 at 12:47 PM, inshu chauhan wrote: > I attached a wrong file...Right file is attached here > > > On Fri, Nov 9, 2012 at 11:53 AM, inshu chauhan wrote: > >> >> >> >> >> http://www.ymlgroup.com/obscurehiddenlocation/downloads site, and >>> imported it using from notzanzibar import DataType. >>> >> >> No i haven't downloaded it.. and this site is not opening... >> >>> >>> Then you'd have instantiated it in some code like >>> data = DataType(filename) >>> >>> and then the type of data would be notzanzibar.DataType and the docs >>> would be probably available somewhere on www.invalid.com/docs >>> >> >> This site is also blocked here. >> >>> >>> The only new guess: >>> >>> A 3D image would presumably have 3 subscripts, and you're only supplying >>> two. >>> >> >> Yes a 3D image has 3 subscripts but m trying to access all three through >> using only 2 subsscripts i.e X and Y >> >> >>> >>> If you want help, be explicit: >>> >>> 1) what version of CPython are you using, and on what OS? >>> >> >> I am using 2.7.3 on windows 7 >> >> 2) what web site did you download some extra library from ? >>> >> >> The only extra libary i am using is Opencv , downloaded from >> http://sourceforge.net/projects/opencvlibrary/ >> >> >> >>> 3) what import statement did you use ? >>> >> >> import cv >> >> 4) How are all those global variables initialized ? >>> >> see attached file >> >>> 5) What command did you use to start the script ? Did you run it from >>> command.com, or from some IDE ? >>> >> >> Yes I am running it through IDLE GUI >> >> >>> 5) Exactly what error message did you get, including the traceback ? >>> >> >> Traceback (most recent call last): >> File "Z:\modules\Masking_an_image_dynamically.py", line 155, in >> AccessPixels(data) >> File "Z:\modules\.py", line 147, in AccessPixels >> CreateMask(data, x, y) >> File "Z:\modules\new_classification.py", line 110, in CreateMask >> point = data[iy, ix ] >> >> error: index is out of range >> >> The line numbers here and the file attached may be different because I >> have removed a lot of print statements which I was using to debug the >> error.. >> >> >> >>> 6) What have you done to try to figure out your own error? >>> >> >> I have trying print out variables and Indices at each step.. >> >> >> Zero Piraeus : Where are you ? >> >> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: Masking_an_image_dynamically.py Type: application/octet-stream Size: 3359 bytes Desc: not available URL: From andriy.kornatskyy at live.com Fri Nov 9 09:01:19 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Fri, 9 Nov 2012 17:01:19 +0300 Subject: duck typing assert In-Reply-To: <509d06e7$0$29980$c3e8da3$5496439d@news.astraweb.com> References: , <509c412d$0$29980$c3e8da3$5496439d@news.astraweb.com>, , <509d06e7$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: 1. In looks-like we check features of Foo (that may be superset) of what IFoo offers. assert looks(Foo).like(IFoo) 2. We can check if Foo is limited to IFoo only: assert looks(IFoo).like(Foo) So it valid to have both asserts. Thanks. Andriy Kornatskyy ---------------------------------------- > From: steve+comp.lang.python at pearwood.info > Subject: Re: duck typing assert > Date: Fri, 9 Nov 2012 13:36:39 +0000 > To: python-list at python.org > > On Thu, 08 Nov 2012 18:00:58 -0700, Ian Kelly wrote: > > > On Thu, Nov 8, 2012 at 4:33 PM, Steven D'Aprano > > wrote: > >> On Thu, 08 Nov 2012 20:34:58 +0300, Andriy Kornatskyy wrote: > >> > >>> People who come from strongly typed languages that offer interfaces > >>> often are confused by lack of one in Python. Python, being dynamic > >>> typing programming language, follows duck typing principal. It can as > >>> simple as this: > >>> > >>> assert looks(Foo).like(IFoo) > >> > >> How very cute. And I don't mean that in a good way. > >> > >> Why is this a class with a method, instead of a function that takes two > >> class arguments (plus any optional arguments needed)? > >> > >> looks_like(Foo, IFoo) > >> > >> is less "cute", reads better to English speakers, and much more > >> Pythonic. This isn't Java, not everything needs to be a class. > > > > I disagree. Does that test whether Foo looks like IFoo, or IFoo looks > > like Foo? > > What's the difference? "Looks like" is a symmetric comparison, like > "equal" and "almost equal", but not "subset", "less than" etc. If x looks > like y, then necessarily y must look like x. If Katy Perry looks like > Zooey Deschanel, then it stands to reason that Zooey Deschanel looks like > Katy Perry. James Woods looks like Erwin Schroedinger, and Erwin > Schroedinger looks like James Woods. > > http://tvrefill.com/wp-content/uploads/2010/12/zooey-deschanel.jpg > > http://cheezburger.com/6704400128 > > > So in that sense, looks(Spam).like(Ham) must always be the same as > looks(Ham).like(Spam), and the order of operators doesn't matter. > > But that's not what we want! And to his credit, that's not what Andriy > Kornatskyy's code actually implements. The problem is with the name, > which is actively misleading by suggesting a symmetrical comparison for > one which is not symmetrical. > > Suppose we want to make a movie with Zooey Deschanel, but she's not > available, so we want a replacement who is duck-type compatible with her. > The replacement doesn't need to be a Deschanel sister, but she does need > to do *at least* everything Zooey can do. If she can do more, that's > fine, but she can't do less. > > Since Katy Perry can do everything Zooey can do, PLUS she sings, we can > replace Zooey with Katy: Katy is duck-type compatible with Zooey. But we > can't replace Katy with Zooey, because Zooey can't sing.[1] > > (I think... I really don't actually know if Zooey Deschanel can sing or > not. Just go along with the example.) > > The point I am making is that "looks like" is not a good description for > this function. "Looks like" must be symmetrical, but the function we > actually want is not symmetrical. It is actually a "subset" type > relationship: given "looks(Zooey).like(Katy)", it checks that the public > methods etc. of Zooey are a subset of the methods of Katy. That is, that > instances of Katy can be used instead of instances of Zooey. > > Or is it the other way? Damned if I know. Let's find out: > > class Zooey: > def act(self): pass > > class Katy: > def act(self): pass > def sing(self): pass > > > py> looks(Zooey).like(Katy) > __main__:2: UserWarning: 'sing': is missing. > False > > I guessed wrong. The looks.like method as implemented tests that the > right-hand size operand is a subset of the right-hand-side: > > py> looks(Katy).like(Zooey) > True > > > I argue that this is the wrong way around. (Even if it isn't the wrong > way around, it certainly isn't clear or obvious which way you have to > write the operands!) > > Consider this use-case: > > candidates = [Hilary, Jennifer, Katy] > expected = looks(Zooey) # instantiate the looks class once only > for actor in candidates: > if expected.like(actor): > make_movie_with(actor()) > > > That's much nicer and more efficient than the way you have to write it > now: > > candidates = [Hilary, Jennifer, Katy] > for actor in candidates: > # instantiate the looks class every time we want to test another class > if looks(actor).like(Zooey): > make_movie_with(actor()) > > > So... it's a cute name, that sounds English-like. But it doesn't actually > describe what the function does, it is wasteful for at least one useful > use-case, and it's not clear which order you have to supply the two > arguments. > > > > looks(Foo).like(IFoo), on the other hand, is crystal clear about which > > argument is which. > > I hope that by now you can see why I say that it is as clear as mud. > > > > > > [1] Some people might argue that neither can Katy Perry. > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list From insideshoes at gmail.com Fri Nov 9 09:08:28 2012 From: insideshoes at gmail.com (inshu chauhan) Date: Fri, 9 Nov 2012 15:08:28 +0100 Subject: error In-Reply-To: References: <78f26f00-2c91-47b3-aef0-3eaeb8ae6f91@u4g2000pbo.googlegroups.com> <509BDEF1.50706@davea.name> Message-ID: Actually this one.. and its the last.. On Fri, Nov 9, 2012 at 2:59 PM, inshu chauhan wrote: > Please Ignore the above two files attached,,, See this one > > > On Fri, Nov 9, 2012 at 12:47 PM, inshu chauhan wrote: > >> I attached a wrong file...Right file is attached here >> >> >> On Fri, Nov 9, 2012 at 11:53 AM, inshu chauhan wrote: >> >>> >>> >>> >>> >>> http://www.ymlgroup.com/obscurehiddenlocation/downloads site, and >>>> imported it using from notzanzibar import DataType. >>>> >>> >>> No i haven't downloaded it.. and this site is not opening... >>> >>>> >>>> Then you'd have instantiated it in some code like >>>> data = DataType(filename) >>>> >>>> and then the type of data would be notzanzibar.DataType and the docs >>>> would be probably available somewhere on www.invalid.com/docs >>>> >>> >>> This site is also blocked here. >>> >>>> >>>> The only new guess: >>>> >>>> A 3D image would presumably have 3 subscripts, and you're only supplying >>>> two. >>>> >>> >>> Yes a 3D image has 3 subscripts but m trying to access all three through >>> using only 2 subsscripts i.e X and Y >>> >>> >>>> >>>> If you want help, be explicit: >>>> >>>> 1) what version of CPython are you using, and on what OS? >>>> >>> >>> I am using 2.7.3 on windows 7 >>> >>> 2) what web site did you download some extra library from ? >>>> >>> >>> The only extra libary i am using is Opencv , downloaded from >>> http://sourceforge.net/projects/opencvlibrary/ >>> >>> >>> >>>> 3) what import statement did you use ? >>>> >>> >>> import cv >>> >>> 4) How are all those global variables initialized ? >>>> >>> see attached file >>> >>>> 5) What command did you use to start the script ? Did you run it from >>>> command.com, or from some IDE ? >>>> >>> >>> Yes I am running it through IDLE GUI >>> >>> >>>> 5) Exactly what error message did you get, including the traceback ? >>>> >>> >>> Traceback (most recent call last): >>> File "Z:\modules\Masking_an_image_dynamically.py", line 155, in >>> >>> AccessPixels(data) >>> File "Z:\modules\.py", line 147, in AccessPixels >>> CreateMask(data, x, y) >>> File "Z:\modules\new_classification.py", line 110, in CreateMask >>> point = data[iy, ix ] >>> >>> error: index is out of range >>> >>> The line numbers here and the file attached may be different because I >>> have removed a lot of print statements which I was using to debug the >>> error.. >>> >>> >>> >>>> 6) What have you done to try to figure out your own error? >>>> >>> >>> I have trying print out variables and Indices at each step.. >>> >>> >>> Zero Piraeus : Where are you ? >>> >>> >>> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: Masking_an_image_dynamically.py Type: application/octet-stream Size: 3391 bytes Desc: not available URL: From rosuav at gmail.com Fri Nov 9 09:15:36 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 10 Nov 2012 01:15:36 +1100 Subject: duck typing assert In-Reply-To: References: <509c412d$0$29980$c3e8da3$5496439d@news.astraweb.com> <509d06e7$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, Nov 10, 2012 at 1:01 AM, Andriy Kornatskyy wrote: > > 1. In looks-like we check features of Foo (that may be superset) of what IFoo offers. > > assert looks(Foo).like(IFoo) > > 2. We can check if Foo is limited to IFoo only: > > assert looks(IFoo).like(Foo) > > So it valid to have both asserts. You'll almost never need #2, but since there's no difference between a "class" and an "interface", it's perfectly legal to switch them around. But I would generally expect that unrecognized methods are never a problem (assuming they don't collide with anything) - that, as in Steven's example, it's fine to have an actor who can sing when you don't need her to. When you post job openings, you don't normally ask for someone with "5+ years Python experience and unable to program in REXX" [1]. You're checking for a minimum set of requirements. [1] Though I suppose you might ask for someone who's unable to program in Pascal. Might save you some hassle. ChrisA From andriy.kornatskyy at live.com Fri Nov 9 09:37:11 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Fri, 9 Nov 2012 17:37:11 +0300 Subject: duck typing assert In-Reply-To: References: , <509c412d$0$29980$c3e8da3$5496439d@news.astraweb.com>, , <509d06e7$0$29980$c3e8da3$5496439d@news.astraweb.com>, , Message-ID: There is sense for #2. Let me explain. There is basic IFoo implementation and improved Foo. While I switched to Foo, I still want to be as close to IFoo as possible, since there might be sense to switch to Foo2 later, which conform to IFoo. Here is the problem: if I will not assert #2 potentially I will use some `features` that are not in IFoo, thus that breaks my code switching to Foo2 later. That might not apply for 100% usability cases, just wanted to point that out as reasonable thing. Thanks. Andriy Kornatskyy ---------------------------------------- > Date: Sat, 10 Nov 2012 01:15:36 +1100 > Subject: Re: duck typing assert > From: rosuav at gmail.com > To: python-list at python.org > > On Sat, Nov 10, 2012 at 1:01 AM, Andriy Kornatskyy > wrote: > > > > 1. In looks-like we check features of Foo (that may be superset) of what IFoo offers. > > > > assert looks(Foo).like(IFoo) > > > > 2. We can check if Foo is limited to IFoo only: > > > > assert looks(IFoo).like(Foo) > > > > So it valid to have both asserts. > > You'll almost never need #2, but since there's no difference between a > "class" and an "interface", it's perfectly legal to switch them > around. > > But I would generally expect that unrecognized methods are never a > problem (assuming they don't collide with anything) - that, as in > Steven's example, it's fine to have an actor who can sing when you > don't need her to. When you post job openings, you don't normally ask > for someone with "5+ years Python experience and unable to program in > REXX" [1]. You're checking for a minimum set of requirements. > > [1] Though I suppose you might ask for someone who's unable to program > in Pascal. Might save you some hassle. > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list From rustompmody at gmail.com Fri Nov 9 10:05:37 2012 From: rustompmody at gmail.com (rusi) Date: Fri, 9 Nov 2012 07:05:37 -0800 (PST) Subject: Multi-dimensional list initialization References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <5099bf7d$0$29980$c3e8da3$5496439d@news.astraweb.com> <509ca4b5$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Nov 9, 11:37?am, Steven D'Aprano wrote: > On Fri, 09 Nov 2012 17:07:09 +1100, Chris Angelico wrote: > > On Fri, Nov 9, 2012 at 12:39 PM, Mark Lawrence > > wrote: > >> On 07/11/2012 01:55, Steven D'Aprano wrote: > > >>> Who knows? Who cares? Nobody does: > > >>> n -= n > > >> But I've seen this scattered through code: > > >> x := x - x - x > > > Can you enlighten us as to how this is better than either: > > ?x := -x > > or > > ?x := 0 - x > > ? I'm not seeing it. > > I'm hoping that Mark intended it as an example of crappy code he has > spotted in some other language rather than a counter-example of something > you would do. > > To be pedantic... there may very well be some (rare) cases where you > actually do want x -= x rather than just x = 0. Consider the case where x > could be an INF or NAN. Then x -= x should give x = NAN rather than zero. > That may be desirable in some cases. In x86 assembler mov ax, 0 is 4 bytes sub ax, ax is 2 and therefore better (at least for those brought up on Peter Norton); the most common being xor ax, ax From rosuav at gmail.com Fri Nov 9 10:23:35 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 10 Nov 2012 02:23:35 +1100 Subject: Multi-dimensional list initialization In-Reply-To: References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <5099bf7d$0$29980$c3e8da3$5496439d@news.astraweb.com> <509ca4b5$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, Nov 10, 2012 at 2:05 AM, rusi wrote: > In x86 assembler > mov ax, 0 > is 4 bytes Three bytes actually, B8 00 00 if my memory hasn't failed me. BA for DX, B9 ought to be BX and BB CX, I think. But yes, the xor or sub is two bytes and one clock. ChrisA From jeandubois314 at gmail.com Fri Nov 9 11:19:10 2012 From: jeandubois314 at gmail.com (Jean Dubois) Date: Fri, 9 Nov 2012 08:19:10 -0800 (PST) Subject: [newbie] problem with module PyVisa Message-ID: <2f065a8d-5cf6-455f-b559-d1b78d225fca@10g2000vbu.googlegroups.com> I'm trying to control a programmable power supply via USB using python. After doing some googling I thought I should use PyVisa for this purpose, so I installed it as follows: tar xvfz PyVISA-1.4.tar.gz cd PyVISA-1.4 python setup.py install Installation seems to finish without errors. When I start a python session things go wrong as you can see in the output below, can anyone here tell me how to proceed correctly? thanks in advance. >>> import visa Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python2.6/dist-packages/PyVISA-1.4-py2.6.egg/ visa.py", line 1, in from pyvisa.visa import * File "/usr/local/lib/python2.6/dist-packages/PyVISA-1.4-py2.6.egg/ pyvisa/visa.py", line 231, in resource_manager = ResourceManager() File "/usr/local/lib/python2.6/dist-packages/PyVISA-1.4-py2.6.egg/ pyvisa/vpp43.py", line 105, in __new__ it.init(*args, **kwds) File "/usr/local/lib/python2.6/dist-packages/PyVISA-1.4-py2.6.egg/ pyvisa/visa.py", line 227, in init self.session = self.vi = vpp43.open_default_resource_manager() File "/usr/local/lib/python2.6/dist-packages/PyVISA-1.4-py2.6.egg/ pyvisa/vpp43.py", line 758, in open_default_resource_manager visa_library().viOpenDefaultRM(byref(session)) File "/usr/local/lib/python2.6/dist-packages/PyVISA-1.4-py2.6.egg/ pyvisa/vpp43.py", line 175, in __call__ self.load_library() File "/usr/local/lib/python2.6/dist-packages/PyVISA-1.4-py2.6.egg/ pyvisa/vpp43.py", line 146, in load_library self.__lib = self.__cdecl_lib = cdll.LoadLibrary(path) File "/usr/lib/python2.6/ctypes/__init__.py", line 431, in LoadLibrary return self._dlltype(name) File "/usr/lib/python2.6/ctypes/__init__.py", line 353, in __init__ self._handle = _dlopen(self._name, mode) OSError: /usr/local/vxipnp/linux/bin/libvisa.so.7: cannot open shared object file: No such file or directory From ian.g.kelly at gmail.com Fri Nov 9 11:30:22 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 9 Nov 2012 09:30:22 -0700 Subject: int.__init__ incompatible in Python 3.3 In-Reply-To: <509ceb08$0$29980$c3e8da3$5496439d@news.astraweb.com> References: <481tm9-k6i.ln1@satorlaser.homedns.org> <6ipum9-oim.ln1@satorlaser.homedns.org> <509ceb08$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Nov 9, 2012 at 4:37 AM, Steven D'Aprano wrote: > In Python 3.3: > > py> class X(int): > ... def __init__(self, *args): > ... super().__init__(*args) # does nothing, call it anyway > ... > py> x = X(22) > Traceback (most recent call last): > File "", line 1, in > File "", line 3, in __init__ > TypeError: object.__init__() takes no parameters > > > It is apparently an oversight, or a bug, that it ever worked in older > versions. After reading through the bug history, I think that this change to int is incorrect, or at least incomplete. The goal of the change to object.__init__ is to enable checking for unused arguments when doing cooperative multiple inheritance, with the idea that each class in the hierarchy will remove the arguments it uses and pass the rest along. By the time object.__init__ is reached, any arguments remaining are unused and extraneous. In the case of int, int.__new__ takes up to two arguments. Due to the nature of the type system, these same two arguments are also passed to int.__init__. If each subclass removes its own arguments per the convention, then by the time int.__init__ is reached, there are still up to two *expected* arguments remaining. It should not be the responsibility of the subclasses (which one? all of them?) to remove these arguments before calling super().__init__(). The int class should have the responsibility of accepting and removing these two arguments *and then* checking that there is nothing left over. In Python 3.2, int.__init__ happily accepted the int arguments, but also incorrectly accepted anything else you might pass to it, which was suboptimal for cooperative multiple inheritance. In Python 3.3, it no longer accepts unused arguments, but it also rejects arguments intended for its own class that it should accept, which as I see it makes int.__init__ *unusable* for cooperative multiple inheritance. I realize that the recommendation in the bug comments is to use __new__ instead of __init__ for subclasses of immutable types. But then why have them call __init__ in the first place? Why even fuss over what arguments int.__init__ does or does not accept if we're not supposed to be calling it at all? And why is that deprecation not mentioned anywhere in the documentation, that I can find? From ian.g.kelly at gmail.com Fri Nov 9 11:37:25 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 9 Nov 2012 09:37:25 -0700 Subject: duck typing assert In-Reply-To: <509d06e7$0$29980$c3e8da3$5496439d@news.astraweb.com> References: <509c412d$0$29980$c3e8da3$5496439d@news.astraweb.com> <509d06e7$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Nov 9, 2012 at 6:36 AM, Steven D'Aprano wrote: > (I think... I really don't actually know if Zooey Deschanel can sing or > not. Just go along with the example.) Not only does she sing, she's in a band. http://en.wikipedia.org/wiki/She_%26_Him I take your point about the "looks like" terminology, though. From rodrick.brown at gmail.com Fri Nov 9 11:39:57 2012 From: rodrick.brown at gmail.com (Rodrick Brown) Date: Fri, 9 Nov 2012 11:39:57 -0500 Subject: [newbie] problem with module PyVisa In-Reply-To: <2f065a8d-5cf6-455f-b559-d1b78d225fca@10g2000vbu.googlegroups.com> References: <2f065a8d-5cf6-455f-b559-d1b78d225fca@10g2000vbu.googlegroups.com> Message-ID: <-6375307293957127232@unknownmsgid> It seems pretty obvious from the error. Try installing the missing lib packages. OSError: /usr/local/vxipnp/linux/bin/libvisa.so.7: cannot open shared object file: No such file or directory Sent from my iPhone On Nov 9, 2012, at 11:22 AM, Jean Dubois wrote: > OSError: /usr/local/vxipnp/linux/bin/libvisa.so.7: cannot open shared > object file: No such file or directory From bnrj.rudra at gmail.com Fri Nov 9 11:53:21 2012 From: bnrj.rudra at gmail.com (Rudra Banerjee) Date: Fri, 09 Nov 2012 16:53:21 +0000 Subject: awk like usage in python Message-ID: <1352480001.30761.5.camel@roddur> Friends, I am in process learning python. I basically use shell scripts for text formatting and trying my hand on python. where I am fighting is awk's functionality in python. Say, one of my real tiny code looks like: #!/bin/bash TMP=store for i in $1/MnBi_EOS_* do # echo $i grep -A 15 "T(est)" $i/out-Dy-eos2 >$TMP var_T=`awk '/T\(est\)/{printf $2}' $TMP` var_s1=`awk '/s1,torque/{print $6;exit}' $TMP` var_t=`awk '/s1,torque/{print $7;exit}' $TMP` echo $var_T $var_s1 $var_t >>tfl # echo "" # echo "" done sort -n tfl >$2 rm -i $TMP tfl where the store looks like: T(est)= 266.58K TOTDOS= 0.48669E+02n_Ef= 0.62856E+02 Ebnd-0.11707E+02 spec,subl= 1 1N= 0.72132E+01s1c=-0.50284E+00 spec,subl= 1 1 lined-up= 0.99999E+00 species,subl,cmp= 1 1 1 s1,torque= 0.59382E-02 0.36773E-04 species,sublat,cmp= 1 1 1 sp-mom= 0.14449E+01 species,sublat,cmp= 1 1 1 orbmom= 0.41075E-01 species,subl,cmp= 1 1 2 s1,torque=-0.33939E-12 0.20885E-12 species,sublat,cmp= 1 1 2 sp-mom= 0.54080E+00 species,sublat,cmp= 1 1 2 orbmom= 0.14921E-01 species,subl,cmp= 1 1 3 s1,torque= 0.60002E-02 0.15728E-02 species,sublat,cmp= 1 1 3 sp-mom= 0.14448E+01 species,sublat,cmp= 1 1 3 orbmom= 0.43989E-01 spec,subl= 1 2N= 0.72132E+01s1c=-0.50284E+00 spec,subl= 1 2 lined-up= 0.99999E+00 species,subl,cmp= 1 2 1 s1,torque= 0.59378E-02 0.36850E-04 How can I import the awk functionality in python? From tjreedy at udel.edu Fri Nov 9 12:03:11 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 09 Nov 2012 12:03:11 -0500 Subject: duck typing =?UTF-8?B?YXNzZXJ04oCP?= In-Reply-To: <509ca300$0$29980$c3e8da3$5496439d@news.astraweb.com> References: <509c42e3$0$29980$c3e8da3$5496439d@news.astraweb.com> <509ca300$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 11/9/2012 1:30 AM, Steven D'Aprano wrote: > On Thu, 08 Nov 2012 23:44:54 -0500, Terry Reedy wrote: > >> On 11/8/2012 6:40 PM, Steven D'Aprano wrote: > [...] >>> IFoo.bar # returns a computed property >> >> Assuming IFoo is a class and bar is a property attribute of the class, >> IFoo.bar is the property object itself, not the computed property of an >> instance. > > Given the context we were discussing, namely duck-typing, the examples I > gave should have been understood as indications, not literal code > snippets. For the situation we are discussing, details matter. 'Indications' are not sufficient. > But in context, duck-typing classes normally is intended to substitute an > instance of one class for an instance of another class. This we agree on. > In that case, if > IFoo.bar is a property, and Foo.bar is a method, then you cannot > substitute an IFoo instance for a Foo instance, or vice versa: If the property is properly written, this is wrong, as I showed in the working code you snipped and apparently ignored. Or at least you have not shown a problem with the code I posted. > ifoo = IFoo() > ifoo.bar # returns a computed attribute If the computed attribute is a method, ifoo.bar() # calls the method > foo = Foo() > foo.bar() # calls the method > In the general case, you cannot use ifoo.bar() where foo.bar() is > expected, nor can you use foo.bar where ifoo.bar is expected. In my actual code example, one can make the substitution in typical usage. 'In general', no substitution will work in all possible use cases, with unlimited introspection. But that is besides the point. The point of duck typing is to worry about the details that matter and ignore the differences that do not matter. What matters in a specific case depend on the case. In many cases in Python, using isinstance, for instance, is looking too closely at details that do not matter. But in some cases, the actual class does matter and then we do use isinstance. > Suppose the expected interface is that instance.bar is a method that > takes no arguments. This is exactly the situation for my code example. Here it is again: --- from types import MethodType as bm class C: def __init__(self, x = 0): self.x = x def double(self): return 2 * self.x class Cp: def __init__(self, x = 0): self.x = x @property def double(self): return bm(lambda self: 2 * self.x, self) c, cp = C(3), Cp(3) print(c.double, cp.double, c.double(), cp.double(), sep = '\n') --- >>> > of <__main__.Cp object at 0x0000000003185A58>> 6 6 --- If the interface requires isinstance(inst.double.__self__, C) # or inst.double.__func__.__name__ == 'double' then cp is not a substitute for c. But we would normally consider that an over-specified interface. > foo.bar() matches that interface, because bar is a > method. But ifoo.bar is a property. Not in the sense that matters here. It is the result of calling the .get method of the Ifoo.bar property. If that result is a bound instance method, just as with foo.bar, then what is your problem with it, for the interface specified? > Suppose it computes an int result. If the object resulting from evaluating ifoo.bar does not match the expected interface, IT DOES NOT MATTER whether the object is the result of normal attribute access or of customized access via either __getattr__ or a property. Anyway, I am supposing that Ifoo is written properly to match the expected interface. Here, that means that the property computes a bound method. -- Terry Jan Reedy From stefan_ml at behnel.de Fri Nov 9 12:07:51 2012 From: stefan_ml at behnel.de (Stefan Behnel) Date: Fri, 09 Nov 2012 18:07:51 +0100 Subject: Python3.3 str() bug? In-Reply-To: References: Message-ID: Helmut Jarausch, 09.11.2012 14:13: > On Fri, 09 Nov 2012 23:22:04 +1100, Chris Angelico wrote: >> What you really should be doing is not transforming the whole >> structure, but explicitly transforming each part inside it. I >> recommend you stop fighting the language and start thinking about your >> data as either *bytes* or *characters* and using the appropriate data >> types (bytes or str) everywhere. You'll then find that it makes >> perfect sense to explicitly translate (en/decode) from one to another, >> but it doesn't make sense to encode a list in UTF-8 or decode a >> dictionary from Latin-1. >> >>> This problem has arisen while converting a working Python2 script to Python3.3. >>> Since Python2 doesn't have bytestrings it just works. >> >> Actually it does; it just calls them "str". And there's a Unicode >> string type, called "unicode", which is (more or less) the thing that >> Python 3 calls "str". >> >> You may be able to do some kind of recursive cast that, in one sweep >> of your data structure, encodes all str objects into bytes using a >> given encoding (or the reverse thereof). But I don't think this is the >> best way to do things. > > Thanks, but in my case the (complex) object is returned via ctypes from the > aspell library. > I still think that a standard function in Python3 which is able to 'stringify' > objects should take an encoding parameter. And how would that work? Would it recursively run through all data structures you pass in or stop at some level or at some type of object? Would it simply concatenate the substrings (and with what separator?), or does the chaining depend on the objects found? Should it use the same separator for everything or different separators for each level of the data structure? Should it use str() for everything or repr() for some? Is str() the right thing or are there special objects that need more than just a call to str(), some kind of further preprocessing? There are so many ways to do something like this, and it's so straight forward to do in a given use case, that it's IMHO useless to even think about adding a "general solution" for this to the stdlib. Stefan From danielkleinad at gmail.com Fri Nov 9 12:17:53 2012 From: danielkleinad at gmail.com (danielk) Date: Fri, 9 Nov 2012 09:17:53 -0800 (PST) Subject: Printing characters outside of the ASCII range Message-ID: <3d4644f8-ab88-41c5-9a52-2a5678dd64c0@googlegroups.com> I'm converting an application to Python 3. The app works fine on Python 2. Simply put, this simple one-liner: print(chr(254)) errors out with: Traceback (most recent call last): File "D:\home\python\tst.py", line 1, in print(chr(254)) File "C:\Python33\lib\encodings\cp437.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_map)[0] UnicodeEncodeError: 'charmap' codec can't encode character '\xfe' in position 0: character maps to I'm using this character as a delimiter in my application. What do I have to do to convert this string so that it does not error out? From ramit.prasad at jpmorgan.com Fri Nov 9 12:23:06 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Fri, 9 Nov 2012 17:23:06 +0000 Subject: Writing game-state data... In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF4741678CDA4@SCACMX008.exchad.jpmchase.net> Graham Fielding wrote: > > Hey, folks, me again! > > I've been puzzling over this for a while now: > > I'm trying to write data to a file to save the state of my game using the following function: > > def save_game(): > ??? #open a new empty shelve (possibly overwriting an old one) to write the game data > ??? file_object = open('savegame.sav', 'wb') > ??? file['map'] = map > ??? file['objects'] = objects > ??? file['player_index'] = objects.index(player)? #index of player in objects list > ??? file['inventory'] = inventory > ??? file['game_msgs'] = game_msgs > ??? file['game_state'] = game_state > ??? file['stairs_index'] = objects.index(stairs) > ??? file['dungeon_level'] = dungeon_level > ??? file.close() > > However, while 'savegame.sav' is created in the directory I specify, the function dies on file['map'] = map. > This is the end of the stack trace: > > > ? File "C:\Python Project\Roguelike.py", line 966, in save_game > ??? file['map'] = map > TypeError: 'type' object does not support item assignment > `file` is the built-in for file objects. I would say you need to use file_object[] instead, but it is a file object and is not meant for this usage. You can write directly to a file but it is easier to use sqllite or shelve/pickle libraries. I will use shelve in my example since your code is already doing something similar. Do not forget to import shelve in your own code. def save(): shelf = shelve.open('savegame.sav', protocol=2) # Change pickle protocol if you use Python < 2.3 shelf['map'] = map shelf['objects'] = objects shelf['player_index'] = objects.index(player) shelf['inventory'] = inventory shelf['game_msgs'] = game_msgs shelf['game_state'] = game_state shelf['stairs_index'] = objects.index(stairs) shelf['dungeon_level'] = dungeon_level shelf.close() > Now, the map is randomly generated -- could that be an issue? > Both "file" and "map" are built-in keywords and using those names for you own variables is called shadowing a built-in. Shadowing a built-in can be interesting and useful but should be avoided. Also, it seems like save() is not in a class nor having anything passed in; are all the game states variables stored at the module level or something? > Should I just scrap the current system and use pickle? You are almost there, so I would not bother. Normally I would use pickle over shelve; I have not needed any of the advantages of shelve and why use a library on top of pickle when I only need pickle? Of course, YMMV. ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From ian.g.kelly at gmail.com Fri Nov 9 12:25:43 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 9 Nov 2012 10:25:43 -0700 Subject: Python3.3 str() bug? In-Reply-To: References: Message-ID: On Fri, Nov 9, 2012 at 2:18 AM, Helmut Jarausch wrote: > Hi, > > probably I'm missing something. > > Using str(Arg) works just fine if Arg is a list. > But > str([],encoding='latin-1') > > gives the error > TypeError: coercing to str: need bytes, bytearray or buffer-like object, > list found > > If this isn't a bug how can I use str(Arg,encoding='latin-1') in general. > Do I need to flatten any data structure which is normally excepted by str() ? In general, the __str__ implementations of complex data structures will call repr() on their components, not str(). repr() normally does not do any decoding in the first place and does not take an encoding argument, so even if str([]) accepted an encoding, it wouldn't be useful for anything. From ian.g.kelly at gmail.com Fri Nov 9 12:34:51 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 9 Nov 2012 10:34:51 -0700 Subject: Printing characters outside of the ASCII range In-Reply-To: <3d4644f8-ab88-41c5-9a52-2a5678dd64c0@googlegroups.com> References: <3d4644f8-ab88-41c5-9a52-2a5678dd64c0@googlegroups.com> Message-ID: On Fri, Nov 9, 2012 at 10:17 AM, danielk wrote: > I'm converting an application to Python 3. The app works fine on Python 2. > > Simply put, this simple one-liner: > > print(chr(254)) > > errors out with: > > Traceback (most recent call last): > File "D:\home\python\tst.py", line 1, in > print(chr(254)) > File "C:\Python33\lib\encodings\cp437.py", line 19, in encode > return codecs.charmap_encode(input,self.errors,encoding_map)[0] > UnicodeEncodeError: 'charmap' codec can't encode character '\xfe' in position 0: character maps to > > I'm using this character as a delimiter in my application. > > What do I have to do to convert this string so that it does not error out? In Python 2, chr(254) means the byte 254. In Python 3, chr(254) means the Unicode character with code point 254, which is "?". This character does not exist in CP 437, so it fails to encode it for output. If what you really want is the byte, then use b'\xfe' or bytes([254]) instead. From ramit.prasad at jpmorgan.com Fri Nov 9 12:37:29 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Fri, 9 Nov 2012 17:37:29 +0000 Subject: duck typing assert In-Reply-To: References: , <509c412d$0$29980$c3e8da3$5496439d@news.astraweb.com>, , Message-ID: <5B80DD153D7D744689F57F4FB69AF4741678CE1A@SCACMX008.exchad.jpmchase.net> Andriy Kornatskyy wrote: > > Thank you for all comments. > > > It?makes very good sense to say: > > > > duckmatch(IFoo).compare(Foo) > > Since we do duck match of IFoo... but there is no `duck match`, there is `duck test`.?I believe instead of > `compare` is more readable with `equals`. Than it is more from?mathematics - precise answer...?that you can not > guarantee at all in dynamic programming language. So it false to use such wording to reflect this check. We can > only make an assumption that one looks like the other (similar)... with some limitation of cause... > understanding what is `duck test`. > > http://en.wikipedia.org/wiki/Duck_test > > The intent is to make such language `construct` so it reads as English sentence that make sense, and not > mandatory `pythonic` way (readability counts, java smokes aside). > > is_similar(Foo).to(IFoo) # <= but we lost `duck test` sense here? > > Words `looks` and `like` are coming from duck test and point also direction: > > # 1 > looks(Foo).like(IFoo, notice=['__len__'], ignore_funcs=['foo'], ignore_argspec['bar']) > > English sentence equivalent: if functions in Foo looks like one in IFoo than, probably, IFoo can be replaced > with Foo; notice to check __len__, it is safe to ignore function `foo` and arguments passed to `bar`. > > # 2 > looks(Foo, notice=['__len__'], ignore_funcs=['foo'], ignore_argspec['bar']).like(IFoo) > > English sentence equivalent:?while looking at Foo notice to check `__len__`, it is safe to ignore function `foo` > and?arguments passed to `bar`, than probably it like IFoo. What about? duck(Foo).equivalent_to(IFoo, ) duck(Foo).matches(IFoo, ) This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From bahamutzero8825 at gmail.com Fri Nov 9 12:39:50 2012 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Fri, 09 Nov 2012 11:39:50 -0600 Subject: Printing characters outside of the ASCII range In-Reply-To: <3d4644f8-ab88-41c5-9a52-2a5678dd64c0@googlegroups.com> References: <3d4644f8-ab88-41c5-9a52-2a5678dd64c0@googlegroups.com> Message-ID: <509D3FE6.5030905@gmail.com> On 2012.11.09 11:17, danielk wrote: > I'm converting an application to Python 3. The app works fine on Python 2. > > Simply put, this simple one-liner: > > print(chr(254)) > > errors out with: > > Traceback (most recent call last): > File "D:\home\python\tst.py", line 1, in > print(chr(254)) > File "C:\Python33\lib\encodings\cp437.py", line 19, in encode > return codecs.charmap_encode(input,self.errors,encoding_map)[0] > UnicodeEncodeError: 'charmap' codec can't encode character '\xfe' in position 0: character maps to > > I'm using this character as a delimiter in my application. > > What do I have to do to convert this string so that it does not error out? > That character is outside of cp437 - the default terminal encoding on many Windows systems. You will either need to change the code page to something that supports the character (if you're going to change it, you might as well change it to cp65001 since you are using 3.3), catch the error and replace the character with something that is in the current codepage (don't assume cp437; it is not the default everywhere), or use a different character completely. If it works on Python 2, it's probably changing the character automatically to a replacement character or you were using IDLE, which is graphical and is not subject to the weird encoding system of terminals. -- CPython 3.3.0 | Windows NT 6.1.7601.17835 From d at davea.name Fri Nov 9 12:47:42 2012 From: d at davea.name (Dave Angel) Date: Fri, 09 Nov 2012 12:47:42 -0500 Subject: Printing characters outside of the ASCII range In-Reply-To: <3d4644f8-ab88-41c5-9a52-2a5678dd64c0@googlegroups.com> References: <3d4644f8-ab88-41c5-9a52-2a5678dd64c0@googlegroups.com> Message-ID: <509D41BE.8020507@davea.name> On 11/09/2012 12:17 PM, danielk wrote: > I'm converting an application to Python 3. The app works fine on Python 2. > > Simply put, this simple one-liner: > > print(chr(254)) > > errors out with: > > Traceback (most recent call last): > File "D:\home\python\tst.py", line 1, in > print(chr(254)) > File "C:\Python33\lib\encodings\cp437.py", line 19, in encode > return codecs.charmap_encode(input,self.errors,encoding_map)[0] > UnicodeEncodeError: 'charmap' codec can't encode character '\xfe' in position 0: character maps to > > I'm using this character as a delimiter in my application. > > What do I have to do to convert this string so that it does not error out? What character do you want? What characters do your console handle directly? What does a "delimiter" mean for your particular console? Or are you just printing it for the fun of it, and the real purpose is for further processing, which will not go to the console? What kind of things will it be separating? (strings, bytes ?) Clearly you originally picked it as something unlikely to occur in those elements. When those things are combined with a separator between, how are the results going to be used? Saved to a file? Printed to console? What? -- DaveA From ramit.prasad at jpmorgan.com Fri Nov 9 12:47:51 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Fri, 9 Nov 2012 17:47:51 +0000 Subject: Python3.3 str() bug? In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF4741678CE6A@SCACMX008.exchad.jpmchase.net> Chris Angelico wrote: > > What you really should be doing is not transforming the whole > structure, but explicitly transforming each part inside it. I > recommend you stop fighting the language and start thinking about your > data as either *bytes* or *characters* and using the appropriate data > types (bytes or str) everywhere. You'll then find that it makes > perfect sense to explicitly translate (en/decode) from one to another, > but it doesn't make sense to encode a list in UTF-8 or decode a > dictionary from Latin-1. > [snip] > > You may be able to do some kind of recursive cast that, in one sweep > of your data structure, encodes all str objects into bytes using a > given encoding (or the reverse thereof). But I don't think this is the > best way to do things. I would think the best way is to convert as you load the data. That way everything is in the correct format as you manipulate and generate new data. ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From andriy.kornatskyy at live.com Fri Nov 9 12:55:03 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Fri, 9 Nov 2012 20:55:03 +0300 Subject: duck typing assert In-Reply-To: <5B80DD153D7D744689F57F4FB69AF4741678CE1A@SCACMX008.exchad.jpmchase.net> References: , , <509c412d$0$29980$c3e8da3$5496439d@news.astraweb.com>, , , , , , <5B80DD153D7D744689F57F4FB69AF4741678CE1A@SCACMX008.exchad.jpmchase.net> Message-ID: duck(Foo).match(IFoo, ) duck(Foo).like(IFoo, ) Hm... function name in most cases is read as verb... this may cause confusion: duck => synonyms => immerse, dip Thanks. Andriy Kornatskyy ---------------------------------------- > From: ramit.prasad at jpmorgan.com > To: andriy.kornatskyy at live.com; python-list at python.org > Subject: RE: duck typing assert > Date: Fri, 9 Nov 2012 17:37:29 +0000 > > Andriy Kornatskyy wrote: > > > > Thank you for all comments. > > > > > It makes very good sense to say: > > > > > > duckmatch(IFoo).compare(Foo) > > > > Since we do duck match of IFoo... but there is no `duck match`, there is `duck test`. I believe instead of > > `compare` is more readable with `equals`. Than it is more from mathematics - precise answer... that you can not > > guarantee at all in dynamic programming language. So it false to use such wording to reflect this check. We can > > only make an assumption that one looks like the other (similar)... with some limitation of cause... > > understanding what is `duck test`. > > > > http://en.wikipedia.org/wiki/Duck_test > > > > The intent is to make such language `construct` so it reads as English sentence that make sense, and not > > mandatory `pythonic` way (readability counts, java smokes aside). > > > > is_similar(Foo).to(IFoo) # <= but we lost `duck test` sense here? > > > > Words `looks` and `like` are coming from duck test and point also direction: > > > > # 1 > > looks(Foo).like(IFoo, notice=['__len__'], ignore_funcs=['foo'], ignore_argspec['bar']) > > > > English sentence equivalent: if functions in Foo looks like one in IFoo than, probably, IFoo can be replaced > > with Foo; notice to check __len__, it is safe to ignore function `foo` and arguments passed to `bar`. > > > > # 2 > > looks(Foo, notice=['__len__'], ignore_funcs=['foo'], ignore_argspec['bar']).like(IFoo) > > > > English sentence equivalent: while looking at Foo notice to check `__len__`, it is safe to ignore function `foo` > > and arguments passed to `bar`, than probably it like IFoo. > > What about? > > duck(Foo).equivalent_to(IFoo, ) > duck(Foo).matches(IFoo, ) > > This email is confidential and subject to important disclaimers and > conditions including on offers for the purchase or sale of > securities, accuracy and completeness of information, viruses, > confidentiality, legal privilege, and legal entity disclaimers, > available at http://www.jpmorgan.com/pages/disclosures/email. From ramit.prasad at jpmorgan.com Fri Nov 9 15:31:47 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Fri, 9 Nov 2012 20:31:47 +0000 Subject: Multi-dimensional list initialization In-Reply-To: References: <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <5099bf7d$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF4741678D229@SCACMX008.exchad.jpmchase.net> Dennis Lee Bieber wrote: > > On Fri, 9 Nov 2012 17:07:09 +1100, Chris Angelico > declaimed the following in gmane.comp.python.general: > > > On Fri, Nov 9, 2012 at 12:39 PM, Mark Lawrence wrote: > > > On 07/11/2012 01:55, Steven D'Aprano wrote: > > >> > > >> > > >> Who knows? Who cares? Nobody does: > > >> > > >> n -= n > > >> > > > > > > But I've seen this scattered through code: > > > > > > x := x - x - x > > > > Can you enlighten us as to how this is better than either: > > x := -x > > or > > x := 0 - x > > Of course, if one has a language that, for some reason, evaluates > right-to-left (APL, anyone), then > > x := x - x - x > > becomes > > x := x - 0 > Is that not the same as x:=-x? ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From jeandubois314 at gmail.com Fri Nov 9 15:43:03 2012 From: jeandubois314 at gmail.com (Jean Dubois) Date: Fri, 9 Nov 2012 12:43:03 -0800 (PST) Subject: problem with module PyVisa References: <2f065a8d-5cf6-455f-b559-d1b78d225fca@10g2000vbu.googlegroups.com> Message-ID: <2325633a-640c-42f6-b8e7-930e475b7217@c20g2000vbz.googlegroups.com> On 9 nov, 17:40, Rodrick Brown wrote: > It seems pretty obvious from the error. Try installing the missing lib packages. > > OSError: /usr/local/vxipnp/linux/bin/libvisa.so.7: cannot open shared > object file: No such file or directory > > Sent from my iPhone > > On Nov 9, 2012, at 11:22 AM, Jean Dubois wrote: > > > > > > > > > OSError: /usr/local/vxipnp/linux/bin/libvisa.so.7: cannot open shared > > object file: No such file or directory The error may be obvious but finding this file and how to install it is not unfortunately. It seems I have to install it from the National Instruments site but Debian Linux doesn't seem to be supported... and I doubt whether just copying this file will be sufficient to make PyVisa work. I wonder whether there might be another way to communicate via USB with a Keithley programmable power supply using Python. best regards, Jean From pengyu.ut at gmail.com Fri Nov 9 16:03:58 2012 From: pengyu.ut at gmail.com (Peng Yu) Date: Fri, 9 Nov 2012 15:03:58 -0600 Subject: How to print python commands automatically? In-Reply-To: <603f952f-a1a2-48a3-ab55-bdc7982a1f2a@i7g2000pbf.googlegroups.com> References: <603f952f-a1a2-48a3-ab55-bdc7982a1f2a@i7g2000pbf.googlegroups.com> Message-ID: > Is this what you want? > http://docs.python.org/2/library/trace.html I'm not able to get the mixing of the python command screen output on stdout. Is there a combination of options for this purpose? ~/linux/test/python/man/library/trace$ cat main1.py #!/usr/bin/env python def f(): print "Hello World!" f() ~/linux/test/python/man/library/trace$ cat main.sh #!/usr/bin/env bash python -m trace --count -C . main1.py -t ~/linux/test/python/man/library/trace$ ./main.sh Hello World! ~/linux/test/python/man/library/trace$ cat main1.cover #!/usr/bin/env python 1: def f(): 1: print "Hello World!" 1: f() -- Regards, Peng From wrw at mac.com Fri Nov 9 16:14:16 2012 From: wrw at mac.com (wrw at mac.com) Date: Fri, 09 Nov 2012 16:14:16 -0500 Subject: problem with module PyVisa In-Reply-To: <2325633a-640c-42f6-b8e7-930e475b7217@c20g2000vbz.googlegroups.com> References: <2f065a8d-5cf6-455f-b559-d1b78d225fca@10g2000vbu.googlegroups.com> <2325633a-640c-42f6-b8e7-930e475b7217@c20g2000vbz.googlegroups.com> Message-ID: <98D6A5D5-CA2E-483C-AD96-7100E6BC83C3@mac.com> On Nov 9, 2012, at 3:43 PM, Jean Dubois wrote: > > The error may be obvious but finding this file and how to install it > is not unfortunately. > It seems I have to install it from the National Instruments site but > Debian Linux doesn't seem to be supported... > and I doubt whether just copying this file will be sufficient to make > PyVisa work. > I wonder whether there might be another way to communicate via USB > with a Keithley programmable power supply using Python. > > best regards, > Jean > > > > -- > http://mail.python.org/mailman/listinfo/python-list I've been using pyserial quite successfully to control a USB-to-serial converter. That is, controlling a couple of RS232 serial devices via the USB port through a KeySpan USB-to-Serial converter. Pyserial seems to make communication through the USB port quite transparent, at least on my OS-X system. -Bill From ramit.prasad at jpmorgan.com Fri Nov 9 16:16:32 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Fri, 9 Nov 2012 21:16:32 +0000 Subject: problem with module PyVisa In-Reply-To: <2325633a-640c-42f6-b8e7-930e475b7217@c20g2000vbz.googlegroups.com> References: <2f065a8d-5cf6-455f-b559-d1b78d225fca@10g2000vbu.googlegroups.com> <2325633a-640c-42f6-b8e7-930e475b7217@c20g2000vbz.googlegroups.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF4741678D379@SCACMX008.exchad.jpmchase.net> Jean Dubois wrote: > > On 9 nov, 17:40, Rodrick Brown wrote: > > It seems pretty obvious from the error. Try installing the missing lib packages. > > > > OSError: /usr/local/vxipnp/linux/bin/libvisa.so.7: cannot open shared > > object file: No such file or directory > > > > Sent from my iPhone > > > > On Nov 9, 2012, at 11:22 AM, Jean Dubois wrote: > > > > > OSError: /usr/local/vxipnp/linux/bin/libvisa.so.7: cannot open shared > > > object file: No such file or directory > > The error may be obvious but finding this file and how to install it > is not unfortunately. > It seems I have to install it from the National Instruments site but > Debian Linux doesn't seem to be supported... > and I doubt whether just copying this file will be sufficient to make > PyVisa work. > I wonder whether there might be another way to communicate via USB > with a Keithley programmable power supply using Python. > Here are some reference links that might help. http://stackoverflow.com/questions/8140248/linux-implementation-of-visa-api https://decibel.ni.com/content/message/37590 Hope that helps, Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From danielkleinad at gmail.com Fri Nov 9 16:17:39 2012 From: danielkleinad at gmail.com (danielk) Date: Fri, 9 Nov 2012 13:17:39 -0800 (PST) Subject: Printing characters outside of the ASCII range In-Reply-To: References: <3d4644f8-ab88-41c5-9a52-2a5678dd64c0@googlegroups.com> Message-ID: <99d5bd83-35ab-4801-b953-391c497c35bf@googlegroups.com> On Friday, November 9, 2012 12:48:05 PM UTC-5, Dave Angel wrote: > On 11/09/2012 12:17 PM, danielk wrote: > > > I'm converting an application to Python 3. The app works fine on Python 2. > > > > > > Simply put, this simple one-liner: > > > > > > print(chr(254)) > > > > > > errors out with: > > > > > > Traceback (most recent call last): > > > File "D:\home\python\tst.py", line 1, in > > > print(chr(254)) > > > File "C:\Python33\lib\encodings\cp437.py", line 19, in encode > > > return codecs.charmap_encode(input,self.errors,encoding_map)[0] > > > UnicodeEncodeError: 'charmap' codec can't encode character '\xfe' in position 0: character maps to > > > > > > I'm using this character as a delimiter in my application. > > > > > > What do I have to do to convert this string so that it does not error out? > > > > What character do you want? What characters do your console handle > > directly? What does a "delimiter" mean for your particular console? > > > > Or are you just printing it for the fun of it, and the real purpose is > > for further processing, which will not go to the console? > > > > What kind of things will it be separating? (strings, bytes ?) Clearly > > you originally picked it as something unlikely to occur in those elements. > > > > When those things are combined with a separator between, how are the > > results going to be used? Saved to a file? Printed to console? What? > > > > -- > > > > DaveA The database I'm using stores information as a 3-dimensional array. The delimiters between elements are chr(252), chr(253) and chr(254). So a record can look like this (example only uses one of the delimiters for simplicity): name + chr(254) + address + chr(254) + city + chr(254) + st + chr(254) + zip The other delimiters can be embedded within each field. For example, if there were multiple addresses for 'name' then the 'address' field would look like this: addr1 + chr(253) + addr2 + chr(253) + addr3 + etc ... I use Python to connect to the database using subprocess.Popen to run a server process. Python requests 'actions' like 'read' and 'write' to the server process, whereby the server process performs the actions. Some actions require that the server send back information in the form of records that contain those delimiters. I have __str__ and __repr__ methods in the classes but Python is choking on those characters. Surely, I could convert those characters on the server before sending them to Python and that is what I'm probably going to do, so guess I've answered my own question. On Python 2, it just printed the 'extended' ASCII representation. I guess the question I have is: How do you tell Python to use a specific encoding for 'print' statements when I know there will be characters outside of the ASCII range of 0-127? From danielkleinad at gmail.com Fri Nov 9 16:17:39 2012 From: danielkleinad at gmail.com (danielk) Date: Fri, 9 Nov 2012 13:17:39 -0800 (PST) Subject: Printing characters outside of the ASCII range In-Reply-To: References: <3d4644f8-ab88-41c5-9a52-2a5678dd64c0@googlegroups.com> Message-ID: <99d5bd83-35ab-4801-b953-391c497c35bf@googlegroups.com> On Friday, November 9, 2012 12:48:05 PM UTC-5, Dave Angel wrote: > On 11/09/2012 12:17 PM, danielk wrote: > > > I'm converting an application to Python 3. The app works fine on Python 2. > > > > > > Simply put, this simple one-liner: > > > > > > print(chr(254)) > > > > > > errors out with: > > > > > > Traceback (most recent call last): > > > File "D:\home\python\tst.py", line 1, in > > > print(chr(254)) > > > File "C:\Python33\lib\encodings\cp437.py", line 19, in encode > > > return codecs.charmap_encode(input,self.errors,encoding_map)[0] > > > UnicodeEncodeError: 'charmap' codec can't encode character '\xfe' in position 0: character maps to > > > > > > I'm using this character as a delimiter in my application. > > > > > > What do I have to do to convert this string so that it does not error out? > > > > What character do you want? What characters do your console handle > > directly? What does a "delimiter" mean for your particular console? > > > > Or are you just printing it for the fun of it, and the real purpose is > > for further processing, which will not go to the console? > > > > What kind of things will it be separating? (strings, bytes ?) Clearly > > you originally picked it as something unlikely to occur in those elements. > > > > When those things are combined with a separator between, how are the > > results going to be used? Saved to a file? Printed to console? What? > > > > -- > > > > DaveA The database I'm using stores information as a 3-dimensional array. The delimiters between elements are chr(252), chr(253) and chr(254). So a record can look like this (example only uses one of the delimiters for simplicity): name + chr(254) + address + chr(254) + city + chr(254) + st + chr(254) + zip The other delimiters can be embedded within each field. For example, if there were multiple addresses for 'name' then the 'address' field would look like this: addr1 + chr(253) + addr2 + chr(253) + addr3 + etc ... I use Python to connect to the database using subprocess.Popen to run a server process. Python requests 'actions' like 'read' and 'write' to the server process, whereby the server process performs the actions. Some actions require that the server send back information in the form of records that contain those delimiters. I have __str__ and __repr__ methods in the classes but Python is choking on those characters. Surely, I could convert those characters on the server before sending them to Python and that is what I'm probably going to do, so guess I've answered my own question. On Python 2, it just printed the 'extended' ASCII representation. I guess the question I have is: How do you tell Python to use a specific encoding for 'print' statements when I know there will be characters outside of the ASCII range of 0-127? From ramit.prasad at jpmorgan.com Fri Nov 9 16:26:08 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Fri, 9 Nov 2012 21:26:08 +0000 Subject: How to print python commands automatically? In-Reply-To: References: <603f952f-a1a2-48a3-ab55-bdc7982a1f2a@i7g2000pbf.googlegroups.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF4741678D3A6@SCACMX008.exchad.jpmchase.net> Peng Yu wrote: > > > Is this what you want? > > http://docs.python.org/2/library/trace.html > > I'm not able to get the mixing of the python command screen output on > stdout. Is there a combination of options for this purpose? > > ~/linux/test/python/man/library/trace$ cat main1.py > #!/usr/bin/env python > > def f(): > print "Hello World!" > > f() > ~/linux/test/python/man/library/trace$ cat main.sh > #!/usr/bin/env bash > > python -m trace --count -C . main1.py -t > > ~/linux/test/python/man/library/trace$ ./main.sh > Hello World! > ~/linux/test/python/man/library/trace$ cat main1.cover > #!/usr/bin/env python > > 1: def f(): > 1: print "Hello World!" > > 1: f() > Try with just --trace? C:\ramit>python.exe -m trace test.py C:\ramit\Python27\lib\trace.py: must specify one of --trace, --count, --report, --listfuncs, or --trackcalls C:\ramit>python -m trace --trace test.py --- modulename: test, funcname: test.py(2): def f(): test.py(5): f() --- modulename: test, funcname: f test.py(3): print "Hello World!" Hello World! --- modulename: trace, funcname: _unsettrace trace.py(80): sys.settrace(None) ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From ramit.prasad at jpmorgan.com Fri Nov 9 16:34:01 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Fri, 9 Nov 2012 21:34:01 +0000 Subject: Printing characters outside of the ASCII range In-Reply-To: <99d5bd83-35ab-4801-b953-391c497c35bf@googlegroups.com> References: <3d4644f8-ab88-41c5-9a52-2a5678dd64c0@googlegroups.com> <99d5bd83-35ab-4801-b953-391c497c35bf@googlegroups.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF4741678D3D0@SCACMX008.exchad.jpmchase.net> danielk wrote: > > The database I'm using stores information as a 3-dimensional array. The delimiters between elements are > chr(252), chr(253) and chr(254). So a record can look like this (example only uses one of the delimiters for > simplicity): > > name + chr(254) + address + chr(254) + city + chr(254) + st + chr(254) + zip > > The other delimiters can be embedded within each field. For example, if there were multiple addresses for 'name' > then the 'address' field would look like this: > > addr1 + chr(253) + addr2 + chr(253) + addr3 + etc ... > > I use Python to connect to the database using subprocess.Popen to run a server process. Python requests > 'actions' like 'read' and 'write' to the server process, whereby the server process performs the actions. Some > actions require that the server send back information in the form of records that contain those delimiters. > > I have __str__ and __repr__ methods in the classes but Python is choking on those characters. Surely, I could > convert those characters on the server before sending them to Python and that is what I'm probably going to do, > so guess I've answered my own question. On Python 2, it just printed the 'extended' ASCII representation. > > I guess the question I have is: How do you tell Python to use a specific encoding for 'print' statements when I > know there will be characters outside of the ASCII range of 0-127? You just need to change the string to one that is not trying to use the ASCII codec when printing. print(chr(253).decode('latin1')) # change latin1 to your # chosen encoding. ? ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From bahamutzero8825 at gmail.com Fri Nov 9 16:39:13 2012 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Fri, 09 Nov 2012 15:39:13 -0600 Subject: Printing characters outside of the ASCII range In-Reply-To: <99d5bd83-35ab-4801-b953-391c497c35bf@googlegroups.com> References: <3d4644f8-ab88-41c5-9a52-2a5678dd64c0@googlegroups.com> <99d5bd83-35ab-4801-b953-391c497c35bf@googlegroups.com> Message-ID: <509D7801.4070908@gmail.com> On 2012.11.09 15:17, danielk wrote: > I guess the question I have is: How do you tell Python to use a specific encoding for 'print' statements when I know there will be characters outside of the ASCII range of 0-127? You don't. It's raising that exception because the terminal cannot display that character, not because it's using the wrong encoding. As Ian mentioned, chr() on Python 2 and chr() on Python 3 return two different things. I'm not very familiar with the oddities of Python 2, but I suspect sending bytes to the terminal could work since that is what chr() on Python 2 returns. -- CPython 3.3.0 | Windows NT 6.1.7601.17835 From danielkleinad at gmail.com Fri Nov 9 16:46:42 2012 From: danielkleinad at gmail.com (danielk) Date: Fri, 9 Nov 2012 13:46:42 -0800 (PST) Subject: Printing characters outside of the ASCII range In-Reply-To: References: <3d4644f8-ab88-41c5-9a52-2a5678dd64c0@googlegroups.com> <99d5bd83-35ab-4801-b953-391c497c35bf@googlegroups.com> Message-ID: On Friday, November 9, 2012 4:34:19 PM UTC-5, Prasad, Ramit wrote: > danielk wrote: > > > > > > The database I'm using stores information as a 3-dimensional array. The delimiters between elements are > > > chr(252), chr(253) and chr(254). So a record can look like this (example only uses one of the delimiters for > > > simplicity): > > > > > > name + chr(254) + address + chr(254) + city + chr(254) + st + chr(254) + zip > > > > > > The other delimiters can be embedded within each field. For example, if there were multiple addresses for 'name' > > > then the 'address' field would look like this: > > > > > > addr1 + chr(253) + addr2 + chr(253) + addr3 + etc ... > > > > > > I use Python to connect to the database using subprocess.Popen to run a server process. Python requests > > > 'actions' like 'read' and 'write' to the server process, whereby the server process performs the actions. Some > > > actions require that the server send back information in the form of records that contain those delimiters. > > > > > > I have __str__ and __repr__ methods in the classes but Python is choking on those characters. Surely, I could > > > convert those characters on the server before sending them to Python and that is what I'm probably going to do, > > > so guess I've answered my own question. On Python 2, it just printed the 'extended' ASCII representation. > > > > > > I guess the question I have is: How do you tell Python to use a specific encoding for 'print' statements when I > > > know there will be characters outside of the ASCII range of 0-127? > > > > You just need to change the string to one that is not > > trying to use the ASCII codec when printing. > > > > print(chr(253).decode('latin1')) # change latin1 to your > > # chosen encoding. > > ? > > > > > > ~Ramit > > > > > > This email is confidential and subject to important disclaimers and > > conditions including on offers for the purchase or sale of > > securities, accuracy and completeness of information, viruses, > > confidentiality, legal privilege, and legal entity disclaimers, > > available at http://www.jpmorgan.com/pages/disclosures/email. D:\home\python>pytest.py Traceback (most recent call last): File "D:\home\python\pytest.py", line 1, in print(chr(253).decode('latin1')) AttributeError: 'str' object has no attribute 'decode' Do I need to import something? From danielkleinad at gmail.com Fri Nov 9 16:46:42 2012 From: danielkleinad at gmail.com (danielk) Date: Fri, 9 Nov 2012 13:46:42 -0800 (PST) Subject: Printing characters outside of the ASCII range In-Reply-To: References: <3d4644f8-ab88-41c5-9a52-2a5678dd64c0@googlegroups.com> <99d5bd83-35ab-4801-b953-391c497c35bf@googlegroups.com> Message-ID: On Friday, November 9, 2012 4:34:19 PM UTC-5, Prasad, Ramit wrote: > danielk wrote: > > > > > > The database I'm using stores information as a 3-dimensional array. The delimiters between elements are > > > chr(252), chr(253) and chr(254). So a record can look like this (example only uses one of the delimiters for > > > simplicity): > > > > > > name + chr(254) + address + chr(254) + city + chr(254) + st + chr(254) + zip > > > > > > The other delimiters can be embedded within each field. For example, if there were multiple addresses for 'name' > > > then the 'address' field would look like this: > > > > > > addr1 + chr(253) + addr2 + chr(253) + addr3 + etc ... > > > > > > I use Python to connect to the database using subprocess.Popen to run a server process. Python requests > > > 'actions' like 'read' and 'write' to the server process, whereby the server process performs the actions. Some > > > actions require that the server send back information in the form of records that contain those delimiters. > > > > > > I have __str__ and __repr__ methods in the classes but Python is choking on those characters. Surely, I could > > > convert those characters on the server before sending them to Python and that is what I'm probably going to do, > > > so guess I've answered my own question. On Python 2, it just printed the 'extended' ASCII representation. > > > > > > I guess the question I have is: How do you tell Python to use a specific encoding for 'print' statements when I > > > know there will be characters outside of the ASCII range of 0-127? > > > > You just need to change the string to one that is not > > trying to use the ASCII codec when printing. > > > > print(chr(253).decode('latin1')) # change latin1 to your > > # chosen encoding. > > ? > > > > > > ~Ramit > > > > > > This email is confidential and subject to important disclaimers and > > conditions including on offers for the purchase or sale of > > securities, accuracy and completeness of information, viruses, > > confidentiality, legal privilege, and legal entity disclaimers, > > available at http://www.jpmorgan.com/pages/disclosures/email. D:\home\python>pytest.py Traceback (most recent call last): File "D:\home\python\pytest.py", line 1, in print(chr(253).decode('latin1')) AttributeError: 'str' object has no attribute 'decode' Do I need to import something? From ethan at stoneleaf.us Fri Nov 9 16:49:48 2012 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 09 Nov 2012 13:49:48 -0800 Subject: Multi-dimensional list initialization In-Reply-To: <5B80DD153D7D744689F57F4FB69AF4741678D229@SCACMX008.exchad.jpmchase.net> References: <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <5099bf7d$0$29980$c3e8da3$5496439d@news.astraweb.com> <5B80DD153D7D744689F57F4FB69AF4741678D229@SCACMX008.exchad.jpmchase.net> Message-ID: <509D7A7C.8060403@stoneleaf.us> Prasad, Ramit wrote: > Dennis Lee Bieber wrote: >> Of course, if one has a language that, for some reason, evaluates >> right-to-left (APL, anyone), then >> >> x := x - x - x >> >> becomes >> >> x := x - 0 > > Is that not the same as x:=-x? No, its the same as 'x = x'. ~Ethan~ From ian.g.kelly at gmail.com Fri Nov 9 17:10:39 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 9 Nov 2012 15:10:39 -0700 Subject: Printing characters outside of the ASCII range In-Reply-To: References: <3d4644f8-ab88-41c5-9a52-2a5678dd64c0@googlegroups.com> <99d5bd83-35ab-4801-b953-391c497c35bf@googlegroups.com> Message-ID: On Fri, Nov 9, 2012 at 2:46 PM, danielk wrote: > D:\home\python>pytest.py > Traceback (most recent call last): > File "D:\home\python\pytest.py", line 1, in > print(chr(253).decode('latin1')) > AttributeError: 'str' object has no attribute 'decode' > > Do I need to import something? Ramit should have written "encode", not "decode". But the above still would not work, because chr(253) gives you the character at *Unicode* code point 253, not the character with CP437 ordinal 253 that your terminal can actually print. The Unicode equivalents of those characters are: >>> list(map(ord, bytes([252, 253, 254]).decode('cp437'))) [8319, 178, 9632] So these are what you would need to encode to CP437 for printing. >>> print(chr(8319)) ? >>> print(chr(178)) ? >>> print(chr(9632)) ? That's probably not the way you want to go about printing them, though, unless you mean to be inserting them manually. Is the data you get from your database a string, or a bytes object? If the former, just do: print(data.encode('cp437')) If the latter, then it should be printable as is, unless it is in some other encoding than CP437. From jabba.laci at gmail.com Fri Nov 9 17:25:37 2012 From: jabba.laci at gmail.com (Jabba Laci) Date: Fri, 9 Nov 2012 23:25:37 +0100 Subject: fabric question Message-ID: Hi, I'm trying to use fabric to run a command on another Linux machine. When I call "fab remote_info" (using the example from its documentation), this is what I get: local$ fab remote_info [remote] Executing task 'remote_info' [remote] run: uname -a [remote] out: remote at path$ That is, it logs in successfully, but I get no output and I get a prompt on the remote machine, i.e. it doesn't log out automatically after calling the remote command. I also tried to specify the absolute path of uname, same result. Any idea? Thanks, Laszlo From pengyu.ut at gmail.com Fri Nov 9 17:40:29 2012 From: pengyu.ut at gmail.com (Peng Yu) Date: Fri, 9 Nov 2012 16:40:29 -0600 Subject: How to print python commands automatically? In-Reply-To: <5B80DD153D7D744689F57F4FB69AF4741678D3A6@SCACMX008.exchad.jpmchase.net> References: <603f952f-a1a2-48a3-ab55-bdc7982a1f2a@i7g2000pbf.googlegroups.com> <5B80DD153D7D744689F57F4FB69AF4741678D3A6@SCACMX008.exchad.jpmchase.net> Message-ID: > Try with just --trace? > > > C:\ramit>python.exe -m trace test.py > C:\ramit\Python27\lib\trace.py: must specify one of --trace, --count, --report, --listfuncs, or --trackcalls > > C:\ramit>python -m trace --trace test.py > --- modulename: test, funcname: > test.py(2): def f(): > test.py(5): f() > --- modulename: test, funcname: f > test.py(3): print "Hello World!" > Hello World! > --- modulename: trace, funcname: _unsettrace > trace.py(80): sys.settrace(None) I have to explicitly specify the modules I want to ignore. Is there a way to ignore all the modules by default? ~/linux/test/python/man/library/trace/import$ cat.sh main.py main.sh test.py ==> main.py <== #!/usr/bin/env python import test test.test() ==> main.sh <== #!/usr/bin/env bash python -m trace --trace main.py ==> test.py <== def test1(): print "Hello World!" def test(): test1() ~/linux/test/python/man/library/trace/import$ python -m trace --trace --ignore-module=test main.py --- modulename: main, funcname: main.py(3): import test main.py(5): test.test() Hello World! --- modulename: trace, funcname: _unsettrace trace.py(80): sys.settrace(None) -- Regards, Peng From miki.tebeka at gmail.com Fri Nov 9 17:49:27 2012 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Fri, 9 Nov 2012 14:49:27 -0800 (PST) Subject: fabric question In-Reply-To: References: Message-ID: <3c5afb03-c3bf-44e5-ae5e-a157c11b755a@googlegroups.com> > local$ fab remote_info > [remote] Executing task 'remote_info' > [remote] run: uname -a > [remote] out: remote at path$ What happens when you ssh to the machine and run 'uname -a'? (The out: ... is the output) From miki.tebeka at gmail.com Fri Nov 9 17:49:27 2012 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Fri, 9 Nov 2012 14:49:27 -0800 (PST) Subject: fabric question In-Reply-To: References: Message-ID: <3c5afb03-c3bf-44e5-ae5e-a157c11b755a@googlegroups.com> > local$ fab remote_info > [remote] Executing task 'remote_info' > [remote] run: uname -a > [remote] out: remote at path$ What happens when you ssh to the machine and run 'uname -a'? (The out: ... is the output) From tjreedy at udel.edu Fri Nov 9 18:35:46 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 09 Nov 2012 18:35:46 -0500 Subject: Python3.3 str() bug? In-Reply-To: References: Message-ID: On 11/9/2012 8:13 AM, Helmut Jarausch wrote: > Just for the record. > I first discovered a real bug with Python3 when using os.walk on a file system > containing non-ascii characters in file names. > > I encountered a very strange behavior (I still would call it a bug) when trying > to put non-ascii characters in email headers. > This has only been solved satisfactorily in Python3.3. Most bugs, such as the above, are in library modules. There have been many related to unicode. In my opinion, 3.3 is the first version to handle unicode decently well. >>> How can I convert a data strucure of arbitrarily complex nature, which contains >>> bytestrings somewhere, to a string? > Thanks, but in my case the (complex) object is returned via ctypes from the > aspell library. > I still think that a standard function in Python3 which is able to 'stringify' > objects should take an encoding parameter. This is an interesting idea, which I have not seen before. It is more sensible in Python 3 than in Python 2. (For py2, unicode(str(object), encoding='xxx') does what you want.) Try presenting it here or on python-ideas as an enhancement request, rather than as a bug report ;-). In the meanwhile, if you cannot have the object constructed with strings rather than bytes, I suggest you write a custom converter function that understands the structure and replaces bytes with strings. -- Terry Jan Reedy From jeff.jeffries.iii at gmail.com Fri Nov 9 19:00:04 2012 From: jeff.jeffries.iii at gmail.com (Jeff Jeffries) Date: Fri, 9 Nov 2012 19:00:04 -0500 Subject: Want to add dictionary keys to namespace? Message-ID: Smart people, Is there a way I can add a dictionaries keys to the python namespace? It would just be temporary as I am working with a large dictionary, and it would speed up work using an IDE. I look and find nothing... none of the keys have spaces and none are common names within the module. http://stackoverflow.com/questions/2597278/python-load-variables-in-a-dict-into-namespace I do this: #Do this? dictionary = {"AppleSeed": None, "Has": None,"Horrible" :None,"Art"} for key in dictionary.keys(): eval("%s=None"%key) #or do this? locals().update(dictionary) Any ideas? -------------- next part -------------- An HTML attachment was scrubbed... URL: From aahz at pythoncraft.com Fri Nov 9 19:42:43 2012 From: aahz at pythoncraft.com (Aahz) Date: 9 Nov 2012 16:42:43 -0800 Subject: how to get os.py to use an ./ntpath.py instead of Lib/ntpath.py References: <83af64e3-bc26-4217-8afa-e4f6d45b604d@googlegroups.com> <504eb3fc$0$29890$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article , Thomas Rachel wrote: >Am 11.09.2012 05:46 schrieb Steven D'Aprano: >> >> Good for you. (Sorry, that comes across as more condescending than it is >> intended as.) Monkey-patching often gets used for quick scripts and tiny >> pieces of code because it works. >> >> Just beware that if you extend that technique to larger bodies of code, >> say when using a large framework, or multiple libraries, your experience >> may not be quite so good. Especially if *they* are monkey-patching too, >> as some very large frameworks sometimes do. (Or so I am lead to believe.) > >This sonds like a good use case for a context manager, like the one in >decimal.Context.get_manager(). Note that because get_manager() applies to a specific Context instance it is safe in a threaded application, which is NOT true for monkey-patching modules even with a context manager. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "....Normal is what cuts off your sixth finger and your tail..." --Siobhan From bruceg113355 at gmail.com Fri Nov 9 19:48:02 2012 From: bruceg113355 at gmail.com (bruceg113355 at gmail.com) Date: Fri, 9 Nov 2012 16:48:02 -0800 (PST) Subject: Is there a simpler way to modify all arguments in a function before using the arguments? Message-ID: <18134e77-9b02-4aec-afb0-794ed900d194@googlegroups.com> Is there a simpler way to modify all arguments in a function before using the arguments? For example, can the below code, in the modify arguments section be made into a few statements? def someComputation (aa, bb, cc, dd, ee, ff, gg, hh): # modify arguments # ---------------------- aa = aa.replace (?_? , ??) bb= bb.replace (?_? , ??) cc = cc.replace (?_? , ??) dd = dd.replace (?_? , ??) ee = ee.replace (?_? , ??) ff = ff.replace (?_? , ??) gg = gg.replace (?_? , ??) hh = hh.replace (?_? , ??) # use the arguments # ----------------- # ? From rosuav at gmail.com Fri Nov 9 19:52:39 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 10 Nov 2012 11:52:39 +1100 Subject: Want to add dictionary keys to namespace? In-Reply-To: References: Message-ID: On Sat, Nov 10, 2012 at 11:00 AM, Jeff Jeffries wrote: > Smart people, Is there a way I can add a dictionaries keys to the python > namespace? It would just be temporary as I am working with a large > dictionary, and it would speed up work using an IDE. I look and find > nothing... none of the keys have spaces and none are common names within the > module. > > #Do this? > dictionary = {"AppleSeed": None, "Has": None,"Horrible" :None,"Art"} > for key in dictionary.keys(): > eval("%s=None"%key) I would strongly recommend not. > #or do this? > locals().update(dictionary) That doesn't work in a function, but outside of a function, you should be able to use: globals().update(dictionary) However, I would advise using the dictionary explicitly. Give it a shorter name and it'll be easier, but don't go for namespace pollution. The PHP folks finally realized that register_globals is a bad idea. ChrisA From roy at panix.com Fri Nov 9 20:05:26 2012 From: roy at panix.com (Roy Smith) Date: Fri, 09 Nov 2012 20:05:26 -0500 Subject: Is there a simpler way to modify all arguments in a function before using the arguments? References: <18134e77-9b02-4aec-afb0-794ed900d194@googlegroups.com> Message-ID: In article <18134e77-9b02-4aec-afb0-794ed900d194 at googlegroups.com>, bruceg113355 at gmail.com wrote: > Is there a simpler way to modify all arguments in a function before using the > arguments? > > For example, can the below code, in the modify arguments section be made into > a few statements? > > def someComputation (aa, bb, cc, dd, ee, ff, gg, hh): > # modify arguments > # ---------------------- > aa = aa.replace (?_? , ??) > bb= bb.replace (?_? , ??) > cc = cc.replace (?_? , ??) > dd = dd.replace (?_? , ??) > ee = ee.replace (?_? , ??) > ff = ff.replace (?_? , ??) > gg = gg.replace (?_? , ??) > hh = hh.replace (?_? , ??) > > # use the arguments > # ----------------- > # ? You could do something like (not error checked)... def someComputation(*args): new_args = [arg.replace("_", "") for arg in args] aa, bb, cc, dd, ee, ff, gg, hh = new_args but that's pretty weird. I suspect you just want to pass a list instead of a bunch of discrete arguments. From steve+comp.lang.python at pearwood.info Fri Nov 9 20:16:12 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 10 Nov 2012 01:16:12 GMT Subject: Is there a simpler way to modify all arguments in a function before using the arguments? References: <18134e77-9b02-4aec-afb0-794ed900d194@googlegroups.com> Message-ID: <509daadc$0$29980$c3e8da3$5496439d@news.astraweb.com> On Fri, 09 Nov 2012 20:05:26 -0500, Roy Smith wrote: > In article <18134e77-9b02-4aec-afb0-794ed900d194 at googlegroups.com>, > bruceg113355 at gmail.com wrote: > >> Is there a simpler way to modify all arguments in a function before >> using the arguments? >> >> For example, can the below code, in the modify arguments section be >> made into a few statements? >> >> def someComputation (aa, bb, cc, dd, ee, ff, gg, hh): >> # modify arguments >> # ---------------------- >> aa = aa.replace (?_? , ??) >> bb= bb.replace (?_? , ??) >> cc = cc.replace (?_? , ??) >> dd = dd.replace (?_? , ??) >> ee = ee.replace (?_? , ??) >> ff = ff.replace (?_? , ??) >> gg = gg.replace (?_? , ??) >> hh = hh.replace (?_? , ??) >> >> # use the arguments >> # ----------------- >> # ? > > You could do something like (not error checked)... > > def someComputation(*args): > new_args = [arg.replace("_", "") for arg in args] aa, bb, cc, dd, > ee, ff, gg, hh = new_args > > but that's pretty weird. I suspect you just want to pass a list instead > of a bunch of discrete arguments. I agree with everything you say except that it is pretty weird. As far as I am concerned, it isn't weird at all. If you need named parameters: def someComputation(aa, bb, cc, dd, ee, ff, gg, hh): aa, bb, cc, dd, ee, ff, gg, hh = [arg.replace("_", "") for arg in (aa. bb, cc, dd, ee, ff, gg, hh)] ... -- Steven From no.email at nospam.invalid Fri Nov 9 21:52:35 2012 From: no.email at nospam.invalid (Paul Rubin) Date: Fri, 09 Nov 2012 18:52:35 -0800 Subject: Is there a simpler way to modify all arguments in a function before using the arguments? References: <18134e77-9b02-4aec-afb0-794ed900d194@googlegroups.com> Message-ID: <7xlieadv9o.fsf@ruckus.brouhaha.com> bruceg113355 at gmail.com writes: > Is there a simpler way to modify all arguments in a function before > using the arguments? Why do you want to do that? > For example, can the below code, in the modify arguments section be > made into a few statements? Whenever someone uses that many variables one always has to ask whether a table would be better. But, for > def someComputation (aa, bb, cc, dd, ee, ff, gg, hh): > # modify arguments > # ---------------------- > aa = aa.replace (?_? , ??) > bb= bb.replace (?_? , ??) > cc = cc.replace (?_? , ??) > dd = dd.replace (?_? , ??) > ee = ee.replace (?_? , ??) > ff = ff.replace (?_? , ??) > gg = gg.replace (?_? , ??) > hh = hh.replace (?_? , ??) you could write (untested): def someComputation (aa, bb, cc, dd, ee, ff, gg, hh): def modify(s): return s.replace('_', '') aa,bb,cc,dd,ee,ff,gg,hh = \ map(modify,[aa,bb,cc,dd,ee,ff,gg,hh]) From rosuav at gmail.com Fri Nov 9 22:56:40 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 10 Nov 2012 14:56:40 +1100 Subject: Is there a simpler way to modify all arguments in a function before using the arguments? In-Reply-To: <7xlieadv9o.fsf@ruckus.brouhaha.com> References: <18134e77-9b02-4aec-afb0-794ed900d194@googlegroups.com> <7xlieadv9o.fsf@ruckus.brouhaha.com> Message-ID: On Sat, Nov 10, 2012 at 1:52 PM, Paul Rubin wrote: > bruceg113355 at gmail.com writes: >> Is there a simpler way to modify all arguments in a function before >> using the arguments? > > Why do you want to do that? > Contrived example: def send_email(from, to, subj, body, whatever, other, headers, you, like): # Okay, now translate all those into the appropriate encoding and with special characters escaped # We need to translate each one separately so that, for instance, a newline in the subject won't let you create additional headers ChrisA From no.email at nospam.invalid Fri Nov 9 23:05:45 2012 From: no.email at nospam.invalid (Paul Rubin) Date: Fri, 09 Nov 2012 20:05:45 -0800 Subject: Is there a simpler way to modify all arguments in a function before using the arguments? References: <18134e77-9b02-4aec-afb0-794ed900d194@googlegroups.com> <7xlieadv9o.fsf@ruckus.brouhaha.com> Message-ID: <7x390iw19i.fsf@ruckus.brouhaha.com> Chris Angelico writes: > Contrived example: > def send_email(from, to, subj, body, whatever, other, headers, you, like): That should be a dictionary with the header names as indexes. In fact there are already some email handling modules in the stdlib that represent headers that way. From miki.tebeka at gmail.com Fri Nov 9 23:17:08 2012 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Fri, 9 Nov 2012 20:17:08 -0800 (PST) Subject: Is there a simpler way to modify all arguments in a function before using the arguments? In-Reply-To: <18134e77-9b02-4aec-afb0-794ed900d194@googlegroups.com> References: <18134e77-9b02-4aec-afb0-794ed900d194@googlegroups.com> Message-ID: > Is there a simpler way to modify all arguments in a function before using the arguments? You can use a decorator: from functools import wraps def fix_args(fn): @wraps(fn) def wrapper(*args): args = (arg.replace('_', '') for arg in args) return fn(*args) return wrapper @fix_args def foo(x, y): print(x) print(y) From aahz at pythoncraft.com Fri Nov 9 23:30:03 2012 From: aahz at pythoncraft.com (Aahz) Date: 9 Nov 2012 20:30:03 -0800 Subject: Numpy combine channels References: Message-ID: In article , MRAB wrote: >On 10/09/2012 20:39, Wanderer wrote: >> >> I have an array generated by audiolab of left and right stereo >> channels. It looks like [[1,1],[1,2],[2,3]]. I would like to combine >> the left and right channels to get an array [2,3,5]. Is there a numpy >> command to do that? > >>>> import numpy >>>> numpy.array([[1,1],[1,2],[2,3]], dtype="i") >array([[1, 1], > [1, 2], > [2, 3]]) >>>> a[:, 0] >array([1, 1, 2]) >>>> a[:, 1] >array([1, 2, 3]) >>>> a[:, 0] + a[:, 1] >array([2, 3, 5]) > >But should they be added together to make mono? > >Suppose, for example, that both channels have a maximum value. Their >sum would be _twice_ the maximum. > >Therefore, I think that it should probably be the average. > > >>> (a[:, 0] + a[:, 1]) / 2 >array([1, 1, 2]) I'd actually think it should be the max. Consider a stereo where one side is playing a booming bass while the other side is playing a rest note -- should the mono combination be half as loud as as the bass? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "....Normal is what cuts off your sixth finger and your tail..." --Siobhan From moonhkt at gmail.com Fri Nov 9 23:51:47 2012 From: moonhkt at gmail.com (moonhkt) Date: Fri, 9 Nov 2012 20:51:47 -0800 (PST) Subject: skip Trackback error for ftp checking Message-ID: <75298ee5-9a24-4d93-91e9-955080e3f15d@vy11g2000pbb.googlegroups.com> HI All How to skip Trackback warning/error when input ftp address is not correct or reject ? AIX 5.3 from ftplib import FTP import ftplib import sys from optparse import OptionParser parser = OptionParser() parser.add_option("-a","--remote_host_address", dest="remote_host_address", help="REMOTE FTP HOST.",metavar="REMOTE FTP HOST") parser.add_option("-u","--username", dest="username", help="USERNAME for ftp sever.",metavar="USERNAME") parser.add_option("-p","--password", dest="password", help="PASSWORD for ftp server.",metavar="PASSWORD") (options, args ) = parser.parse_args () if not (options.remote_host_address): parser.error("REMOTE HOST are mandatory") if options.username and not options.password: parser.error("PASSWORD is mandatory if USERNAME is present") try: ftp = FTP(options.remote_host_address) except ftplib.error_perm,e: sys.exit(2) if options.username: try: ftp.login(options.username,options.password) except ftplib.error_perm,e: print "Login failed: %s" % e sys.exit(1) else: try: ftp.login() except ftplib.error_perm,e: print "Anonymous login failed: %s" % e sys.exit(1) try: print "LOGIN OK" finally: ftp.close() Command line ------------------------------- chkftp.py -a teseting Output as below ---------------------------- Traceback (most recent call last): File "...chkftp.py", line 33, in ftp = FTP(options.remote_host_address) File "/opt/freeware/lib/python2.6/ftplib.py", line 116, in __init__ self.connect(host) File "/opt/freeware/lib/python2.6/ftplib.py", line 131, in connect self.sock = socket.create_connection((self.host, self.port), self.timeout) File "/opt/freeware/lib/python2.6/socket.py", line 498, in create_connection for res in getaddrinfo(host, port, 0, SOCK_STREAM): socket.gaierror: [Errno 2] temporary failure in name resolution. From steve+comp.lang.python at pearwood.info Sat Nov 10 01:50:34 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 10 Nov 2012 06:50:34 GMT Subject: skip Trackback error for ftp checking References: <75298ee5-9a24-4d93-91e9-955080e3f15d@vy11g2000pbb.googlegroups.com> Message-ID: <509df939$0$29980$c3e8da3$5496439d@news.astraweb.com> On Fri, 09 Nov 2012 20:51:47 -0800, moonhkt wrote: > HI All > > How to skip Trackback warning/error when input ftp address is not > correct or reject ? The same way you would skip any other error when you do something wrong: catch the exception. -- Steven From steve+comp.lang.python at pearwood.info Sat Nov 10 01:57:03 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 10 Nov 2012 06:57:03 GMT Subject: Obnoxious postings from Google Groups References: <50989613$0$29980$c3e8da3$5496439d@news.astraweb.com> <50999452$0$29980$c3e8da3$5496439d@news.astraweb.com> <5099a7ba$0$29980$c3e8da3$5496439d@news.astraweb.com> <509cea43$0$6900$e4fe514c@news2.news.xs4all.nl> Message-ID: <509dfabf$0$29980$c3e8da3$5496439d@news.astraweb.com> On Fri, 09 Nov 2012 12:34:27 +0100, Hans Mulder wrote: > On 7/11/12 01:13:47, Steven D'Aprano wrote: >> Hit the J key, and the event includes character "j". Hit Shift-J, and >> character "J" is sent. Hit Ctrl-J, and the character sent is the ASCII >> control character ^J, or newline. (Technically, the name for ASCII 10 >> is "linefeed" rather than "newline".) > > Actually, the correct name for this character is OS-dependant: The ASCII > standard prescribes that if an OS chooses to use a single character as > its line terminator, then it must be this one, and one should call it > "newline". Otherwise, it's name is "linefeed". So, the correct name is > "newline" on Posix system, but "linefeed" on Windows. I find that hard to believe. Do you have a source for this claim? The ASCII standard has nothing to do with operating systems. It is a character encoding system, whether you are using computers or notches carved into pieces of wood, you can encode characters to values using ASCII. ASCII is operating system agnostic. Every source I have found describing the ASCII standard, and its equivalents from other standards bodies (e.g. ISO/IEC 646, EMCA 6) either directly refer to chr 10 as LF/Linefeed or refer back to the C0 control codes, which refers to it as LF/Linefeed. For example: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-006.pdf See also: http://www.terena.org/activities/multiling/euroml/section04.html which clearly shows char 10 as LF in all the given ISO 646 variants. If you have a source for this claim, I would like to see it, otherwise I will stand by my claim that the standard name for ASCII char 10 is "linefeed". -- Steven From timr at probo.com Sat Nov 10 02:50:29 2012 From: timr at probo.com (Tim Roberts) Date: Fri, 09 Nov 2012 23:50:29 -0800 Subject: awk like usage in python References: <1352480001.30761.5.camel@roddur> Message-ID: Rudra Banerjee wrote: > >Friends, >I am in process learning python. >I basically use shell scripts for text formatting and trying my hand on >python. >where I am fighting is awk's functionality in python. >Say, one of my real tiny code looks like: >#!/bin/bash >TMP=store >for i in $1/MnBi_EOS_* >do ># echo $i > grep -A 15 "T(est)" $i/out-Dy-eos2 >$TMP > var_T=`awk '/T\(est\)/{printf $2}' $TMP` > var_s1=`awk '/s1,torque/{print $6;exit}' $TMP` > var_t=`awk '/s1,torque/{print $7;exit}' $TMP` > echo $var_T $var_s1 $var_t >>tfl ># echo "" ># echo "" >done >sort -n tfl >$2 >rm -i $TMP tfl Well, describe your program in words. Then, you can convert it to Python. For every directory in the given directory whose name starts with MnBi_EOS_: extract the 15 lines starting with T(est) from the file out-Dy-eos2 to a temporary file extract the 2nd field from the line with T(est) in it extract the 6th field from the first line with "s1,torque" extract the 7th field from the first line with "s1,torque" print those three fields to a file sort that file -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From __peter__ at web.de Sat Nov 10 04:09:08 2012 From: __peter__ at web.de (Peter Otten) Date: Sat, 10 Nov 2012 10:09:08 +0100 Subject: Is there a simpler way to modify all arguments in a function before using the arguments? References: <18134e77-9b02-4aec-afb0-794ed900d194@googlegroups.com> Message-ID: Miki Tebeka wrote: >> Is there a simpler way to modify all arguments in a function before using >> the arguments? > You can use a decorator: > > from functools import wraps > > def fix_args(fn): > @wraps(fn) > def wrapper(*args): > args = (arg.replace('_', '') for arg in args) > return fn(*args) > > return wrapper > > @fix_args > def foo(x, y): > print(x) > print(y) I was tempted to post that myself, but he said /simpler/ ;) From __peter__ at web.de Sat Nov 10 04:19:00 2012 From: __peter__ at web.de (Peter Otten) Date: Sat, 10 Nov 2012 10:19 +0100 Subject: Want to add dictionary keys to namespace? References: Message-ID: Jeff Jeffries wrote: > Smart people, Is there a way I can add a dictionaries keys to the python > namespace? It would just be temporary as I am working with a large > dictionary, and it would speed up work using an IDE. I look and find > nothing... none of the keys have spaces and none are common names within > the module. > > http://stackoverflow.com/questions/2597278/python-load-variables-in-a- dict-into-namespace > > I do this: > > #Do this? > dictionary = {"AppleSeed": None, "Has": None,"Horrible" :None,"Art"} > for key in dictionary.keys(): > eval("%s=None"%key) > > #or do this? > locals().update(dictionary) > > Any ideas? You could instead use a dict subclass that lets you access values as attributes: >>> class Dict(dict): ... def __getattr__(self, name): ... return self[name] ... def __setattr__(self, name, value): ... self[name] = value ... >>> d = Dict({"AppleSeed": None, "Has": None, "Horrible" : None, "Art": 42}) >>> d.Art 42 >>> d.AppleSeed >>> d.AppleSeed = "spam" >>> d {'Has': None, 'Art': 42, 'AppleSeed': 'spam', 'Horrible': None} From rosuav at gmail.com Sat Nov 10 04:19:11 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 10 Nov 2012 20:19:11 +1100 Subject: Is there a simpler way to modify all arguments in a function before using the arguments? In-Reply-To: <7x390iw19i.fsf@ruckus.brouhaha.com> References: <18134e77-9b02-4aec-afb0-794ed900d194@googlegroups.com> <7xlieadv9o.fsf@ruckus.brouhaha.com> <7x390iw19i.fsf@ruckus.brouhaha.com> Message-ID: On Sat, Nov 10, 2012 at 3:05 PM, Paul Rubin wrote: > Chris Angelico writes: >> Contrived example: >> def send_email(from, to, subj, body, whatever, other, headers, you, like): > > That should be a dictionary with the header names as indexes. In fact > there are already some email handling modules in the stdlib that > represent headers that way. That's also plausible, but keyword arguments do make sense. And this was a top-of-the-head contrived example; I'm sure there are plenty of good use-cases. ChrisA From wxjmfauth at gmail.com Sat Nov 10 05:09:25 2012 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Sat, 10 Nov 2012 02:09:25 -0800 (PST) Subject: Printing characters outside of the ASCII range In-Reply-To: <3d4644f8-ab88-41c5-9a52-2a5678dd64c0@googlegroups.com> References: <3d4644f8-ab88-41c5-9a52-2a5678dd64c0@googlegroups.com> Message-ID: <8a3c7c1a-5e87-4195-8fa1-c6b5a447ae7f@googlegroups.com> Le vendredi 9 novembre 2012 18:17:54 UTC+1, danielk a ?crit?: > I'm converting an application to Python 3. The app works fine on Python 2. > > > > Simply put, this simple one-liner: > > > > print(chr(254)) > > > > errors out with: > > > > Traceback (most recent call last): > > File "D:\home\python\tst.py", line 1, in > > print(chr(254)) > > File "C:\Python33\lib\encodings\cp437.py", line 19, in encode > > return codecs.charmap_encode(input,self.errors,encoding_map)[0] > > UnicodeEncodeError: 'charmap' codec can't encode character '\xfe' in position 0: character maps to > > > > I'm using this character as a delimiter in my application. > > > > What do I have to do to convert this string so that it does not error out? ----- There is nothing wrong in having the character with the code point 0xfe in the cp437 coding scheme as a delimiter. If it is coming from a byte string, you should decode it properly >>> b'=\xfe=\xfe='.decode('cp437') '=?=?=' or you can use directly the unicode equivalent >>> '=\u25a0=\u25a0=' '=?=?=' That's for "input". For "output" see: http://groups.google.com/group/comp.lang.python/browse_thread/thread/c29f2f7f5a4962e8# The choice of that character as a delimiter is not wrong. It's a little bit unfortunate, because it falls high in the "unicode table". >>> import fourbiunicode as fu >>> fu.UnicodeBlock('\u25a0') 'Geometric Shapes' >>> >>> fu.UnicodeBlock(b'\xfe'.decode('cp437')) 'Geometric Shapes' (Another form of explanation) jmf From tobia.conforto at gmail.com Sat Nov 10 05:26:28 2012 From: tobia.conforto at gmail.com (Tobia Conforto) Date: Sat, 10 Nov 2012 02:26:28 -0800 (PST) Subject: Format specification mini-language for list joining Message-ID: <92ada057-5a72-4186-bd22-4ccb600b8e45@googlegroups.com> Hello Lately I have been writing a lot of list join() operations variously including (and included in) string format() operations. For example: temps = [24.369, 24.550, 26.807, 27.531, 28.752] out = 'Temperatures: {0} Celsius'.format( ', '.join('{0:.1f}'.format(t) for t in temps) ) # => 'Temperatures: 24.4, 24.6, 26.8, 27.5, 28.8 Celsius' This is just a simple example, my actual code has many more join and format operations, split into local variables as needed for clarity. Then I remembered that Ye Old Common Lisp's format operator had built-in list traversing capabilities[1]: (format t "Temperatures: ~{~1$~^, ~} Celsius" temps) That format string (the part in the middle that looks like line noise) is admittedly arcane, but it's parsed like this: ~{ take next argument (temp) and start iterating over its contents ~1$ output a floating point number with 1 digit precision ~^ break the loop if there are no more items available ", " (otherwise) output a comma and space ~} end of the loop body Now, as much as I appreciate the heritage of Lisp, I won't deny than its format string mini-language is EVIL. As a rule, format string placeholders should not include *imperative statements* such as for, break, continue, and if. We don't need a Turing-complete language in our format strings. Still, this is the grand^n-father of Python's format strings, so it's interesting to look at how it used to approach the list joining issue. Then I asked myself: can I take the list joining capability and port it over to Python's format(), doing away with the overall ugliness? Here is what I came up with: out = 'Temperatures: {0:", ":.1f} Celsius'.format(temps) # => 'Temperatures: 24.4, 24.6, 26.8, 27.5, 28.8 Celsius' Here ", " is the joiner between the items and <.1f> is the format string for each item. The way this would work is by defining a specific Format Specification Mini-Language for sequences (such as lists, tuples, and iterables). A Format Specification Mini-Language (format_spec) is whatever follows the first colon in a curly brace placeholder, and is defined by the argument's class, so that it can vary wildly among different types.[2] The root class (object) defines the generic format_spec we are accustomed to[3]: [[fill]align][sign][#][0][width][,][.precision][type] But that doesn't mean that more complex types should not define extensions or replacements. I propose this extended format_spec for sequences: seq_format_spec ::= join_string [":" item_format_spec] | format_spec join_string ::= '"' join_string_char* '"' | "'" join_string_char* "'" join_string_char ::= item_format_spec ::= format_spec That is, if the format_spec for a sequence starts with ' or " it would be interpreted as a join operation (eg. {0:", "} or {0:', '}) optionally followed by a format_spec for the single items: {0:", ":.1f} If the format_spec does not start with ' or ", of if the quote is not balanced (does not appear again in the format_spec), then it's assumed to be a generic format string and the implementation would call super(). This is meant for backwards compatibility with existing code that may be using the generic format_spec over various sequences. I do think that would be quite readable and useful. Look again at the example: out = 'Temperatures: {0:", ":.1f} Celsius'.format(temps) As a bonus, it allows nested joins, albeit only for simple cases. For example we could format a dictionary's items: temps = {'Rome': 26, 'Paris': 21, 'New York': 18} out = 'Temperatures: {0:", ":" ":s}'.format(temps.items()) # => 'Temperatures: Rome 26, Paris 21, New York 18' Here the format_spec for temps.items() is <", ":" ":s>. Then ", " would be used as a joiner between the item tuples and <" ":s> would be passed over as the format_spec for each tuple. This in turn would join the tuple's items using a single space and output each item with its simple string format. This could go on and on as needed, adding a colon and joiner string for each nested join operation. A more complicated mini-language would be needed to output dicts using different format strings for keys and values, but I think that would be veering over to unreadable territory. What do you think? I plan to write this as a module and propose it to Python's devs for inclusion in the main tree, but any criticism is welcome before I do that. -Tobia [1] http://www.gigamonkeys.com/book/a-few-format-recipes.html [2] http://docs.python.org/3/library/string.html#formatstrings [3] http://docs.python.org/3/library/string.html#formatspec From d at davea.name Sat Nov 10 08:05:00 2012 From: d at davea.name (Dave Angel) Date: Sat, 10 Nov 2012 08:05:00 -0500 Subject: Numpy combine channels In-Reply-To: References: Message-ID: <509E50FC.80903@davea.name> On 11/09/2012 11:30 PM, Aahz wrote: > In article , > MRAB wrote: >> >> >> But should they be added together to make mono? >> >> Suppose, for example, that both channels have a maximum value. Their >> sum would be _twice_ the maximum. >> >> Therefore, I think that it should probably be the average. >> >>>>> (a[:, 0] + a[:, 1]) / 2 >> array([1, 1, 2]) > I'd actually think it should be the max. Consider a stereo where one > side is playing a booming bass while the other side is playing a rest > note -- should the mono combination be half as loud as as the bass? max would sound awful. The right answer is to add them with weighting, then scale the whole waveform according to a new calculation of clipping. Just like a mixer, you have level controls on each input, then an overall gain. So if the inputs were x and y, the output would be gain *( x_scale * x + y_scale * y), but it'd normally be done in two passes, so as to minimize the places that are clipped, while maximizing the average. it's also possible to have gain vary across the time axis, like an agc. But you wouldn't want that as a default, as it'd destroy the dynamics of a musical piece. One more consideration. If these are unsigned values (eg. 0 to 255), then you should adjust both signals by 128 before storing them as signed values, do your arithmetic, and then adjust again by adding 128. You could do the algebraic equivalent, but the programming would be much simpler on signed values. -- DaveA From bruceg113355 at gmail.com Sat Nov 10 08:15:21 2012 From: bruceg113355 at gmail.com (bruceg113355 at gmail.com) Date: Sat, 10 Nov 2012 05:15:21 -0800 (PST) Subject: Is there a simpler way to modify all arguments in a function before using the arguments? In-Reply-To: <509daadc$0$29980$c3e8da3$5496439d@news.astraweb.com> References: <18134e77-9b02-4aec-afb0-794ed900d194@googlegroups.com> <509daadc$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: <8e17124b-63ee-4166-8964-80f47f5716d5@googlegroups.com> On Friday, November 9, 2012 8:16:12 PM UTC-5, Steven D'Aprano wrote: > On Fri, 09 Nov 2012 20:05:26 -0500, Roy Smith wrote: > > > > > In article <18134e77-9b02-4aec-afb0-794ed900d194 at googlegroups.com>, > > > bruceg113355 at gmail.com wrote: > > > > > >> Is there a simpler way to modify all arguments in a function before > > >> using the arguments? > > >> > > >> For example, can the below code, in the modify arguments section be > > >> made into a few statements? > > >> > > >> def someComputation (aa, bb, cc, dd, ee, ff, gg, hh): > > >> # modify arguments > > >> # ---------------------- > > >> aa = aa.replace (?_? , ??) > > >> bb= bb.replace (?_? , ??) > > >> cc = cc.replace (?_? , ??) > > >> dd = dd.replace (?_? , ??) > > >> ee = ee.replace (?_? , ??) > > >> ff = ff.replace (?_? , ??) > > >> gg = gg.replace (?_? , ??) > > >> hh = hh.replace (?_? , ??) > > >> > > >> # use the arguments > > >> # ----------------- > > >> # ? > > > > > > You could do something like (not error checked)... > > > > > > def someComputation(*args): > > > new_args = [arg.replace("_", "") for arg in args] aa, bb, cc, dd, > > > ee, ff, gg, hh = new_args > > > > > > but that's pretty weird. I suspect you just want to pass a list instead > > > of a bunch of discrete arguments. > > > > > > I agree with everything you say except that it is pretty weird. As far as > > I am concerned, it isn't weird at all. > > > > If you need named parameters: > > > > def someComputation(aa, bb, cc, dd, ee, ff, gg, hh): > > aa, bb, cc, dd, ee, ff, gg, hh = [arg.replace("_", "") > > for arg in (aa. bb, cc, dd, ee, ff, gg, hh)] > > ... > > > > > > > > -- > > Steven Thanks to all. Steve's example is the one I will try next week. Passing in lists, will work but it requires extra coding from the calling routines to build the list. Discrete arguments make sense. Also, what is the problem passing in 7 or more arguments? Thanks, Bruce From rustompmody at gmail.com Sat Nov 10 10:16:37 2012 From: rustompmody at gmail.com (rusi) Date: Sat, 10 Nov 2012 07:16:37 -0800 (PST) Subject: How to print python commands automatically? References: <603f952f-a1a2-48a3-ab55-bdc7982a1f2a@i7g2000pbf.googlegroups.com> <5B80DD153D7D744689F57F4FB69AF4741678D3A6@SCACMX008.exchad.jpmchase.net> Message-ID: On Nov 9, 10:41?pm, Peng Yu wrote: > I have to explicitly specify the modules I want to ignore. Is there a > way to ignore all the modules by default? Is this your problem? http://bugs.python.org/issue10685 From aahz at pythoncraft.com Sat Nov 10 10:35:11 2012 From: aahz at pythoncraft.com (Aahz) Date: 10 Nov 2012 07:35:11 -0800 Subject: Is there a simpler way to modify all arguments in a function before using the arguments? References: <18134e77-9b02-4aec-afb0-794ed900d194@googlegroups.com> Message-ID: In article , Peter Otten <__peter__ at web.de> wrote: >Miki Tebeka wrote: > >>> Is there a simpler way to modify all arguments in a function before using >>> the arguments? >> >> You can use a decorator: >> >> from functools import wraps >> >> def fix_args(fn): >> @wraps(fn) >> def wrapper(*args): >> args = (arg.replace('_', '') for arg in args) >> return fn(*args) >> >> return wrapper >> >> @fix_args >> def foo(x, y): >> print(x) >> print(y) > >I was tempted to post that myself, but he said /simpler/ ;) >From my POV, that *is* simpler. When you change the parameters for foo, you don't need to change the arg pre-processing. Also allows code reuse, probably any program needing this kind of processing once will need it again. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "....Normal is what cuts off your sixth finger and your tail..." --Siobhan From aahz at pythoncraft.com Sat Nov 10 10:41:19 2012 From: aahz at pythoncraft.com (Aahz) Date: 10 Nov 2012 07:41:19 -0800 Subject: Single leading dash in member variable names? References: <2cb240e2-7992-44eb-84cf-1aba5a411ee4@googlegroups.com> Message-ID: In article , Ian Kelly wrote: >On Tue, Sep 11, 2012 at 12:45 PM, wrote: >> >> Python noob here. Trying to understand a particular syntax: >> >> class stuff: >> def __init__(self): >> self._bongo = "BongoWorld" >> >> What is the significance of the leading underscore in "self._bongo"? >> I've seen this a few times and, after looking through PEP 8, I didn't >> see anything relevant, but I could have missed it. > >Single leading underscore is a convention indicating that the name >should be considered private and not used externally. It's a softer >version of the double leading underscore that means basically the same >thing but has syntactic significance. Note that the convention is rooted in an actual semantic meaning for single underscore: ``from foo import *`` ignores any module global names in foo that start with a single leading underscore. Obviously, this has little effect for most Python programs because you DON'T USE ``import *``. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "....Normal is what cuts off your sixth finger and your tail..." --Siobhan From jonathan.hayward at pobox.com Sat Nov 10 11:08:36 2012 From: jonathan.hayward at pobox.com (Jonathan Hayward) Date: Sat, 10 Nov 2012 10:08:36 -0600 Subject: Py3k Signal Handling Message-ID: I am working on writing a shell, but I want to have stubbed signal handlers when the user hits i.e. control-C or control-Z. My code is as follows; as I have investigated it, I can get it to not stop on a control-Z, but still suspend on a control-Z. The most recent published version of the code is at http://JonathansCorner.com/cjsh/download.cgi. The code I am working on is: def handle_signal(signal_number, frame): print (''' CJSH does not support the traditional and rather archaic methods of job control. This arises partly from implementation concerns and partly for reasons practical concerns. In ye olden dayes with the green glow of terminals, it was important to have a sophisticated job handling in place so jobs could be stopped, suspended, put in the foreground, and so on and so forth, and for that matter this feature is still used despite the fact that it was essentially a consolation prize for not having multiple windows. Don't try to suspend, resume, background, foreground, etc. There is a more excellent way. Now we have not the consolation prize but the real thing: unless you're on a netbook or smaller, you can open another window for another job. If you have no more use for a job, close the window. Just open another window, or maybe a tab. ''') signal.signal(signal.SIGTSTP, handle_signl) signal.signal(signal.SIGHUP, handle_signal) signal.signal(signal.SIGINT, handle_signal) signal.signal(signal.SIGQUIT, handle_signal) signal.signal(signal.SIGABRT, handle_signal) signal.signal(signal.SIGALRM, handle_signal) signal.signal(signal.SIGTERM, handle_signal) #signal.siginterrupt(signal.SIGTSTP, False)#signal.siginterrupt(signal.SIGHUP, False)#signal.siginterrupt(signal.SIGINT, False)#signal.siginterrupt(signal.SIGQUIT, False)#signal.siginterrupt(signal.SIGABRT, False)#signal.siginterrupt(signal.SIGALRM, False)#signal.siginterrupt(signal.SIGTERM, False) What needs changing here and how should I change it so that handle_signal() is called and then things keep on ticking? As posted to http://stackoverflow.com/questions/13318447/how-can-i-add-signal-handlers-to-a-shell-written-in-py3k-pythonwith only one response about the signal that is converted to a KeyboardInterrupt exception. -- [image: Christos Jonathan Hayward] Jonathan Hayward, an Orthodox Christian author. *Amazon * ? Author Bio ? *Email * ? Facebook ? Google Plus ? LinkedIn ? Twitter ? *Web * ? What's New? If you read just *one* of my books, you'll want *The Best of Jonathan's Corner *. -------------- next part -------------- An HTML attachment was scrubbed... URL: From oscar.j.benjamin at gmail.com Sat Nov 10 11:45:26 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Sat, 10 Nov 2012 16:45:26 +0000 Subject: Python3.3 str() bug? In-Reply-To: References: Message-ID: On 9 November 2012 11:08, Helmut Jarausch wrote: > On Fri, 09 Nov 2012 10:37:11 +0100, Stefan Behnel wrote: > >> Helmut Jarausch, 09.11.2012 10:18: >>> probably I'm missing something. >>> >>> Using str(Arg) works just fine if Arg is a list. >>> But >>> str([],encoding='latin-1') >>> >>> gives the error >>> TypeError: coercing to str: need bytes, bytearray or buffer-like object, >>> list found >>> >>> If this isn't a bug how can I use str(Arg,encoding='latin-1') in general. >>> Do I need to flatten any data structure which is normally excepted by str() ? >> >> Funny idea to call this a bug in Python. What your code is asking for is to >> decode the object you pass in using the "latin-1" encoding. Since a list is >> not something that is "encoded", let alone in latin-1, you get an error, >> and actually a rather clear one. >> >> Note that this is not specific to Python3.3 or even 3.x. It's the same >> thing in Py2 when you call the equivalent unicode() function. >> > > For me it's not funny, at all. I think the problem is that the str constructor does two fundamentally different things depending on whether you have supplied the encoding argument. From help(str) in Python 3.2: | str(object[, encoding[, errors]]) -> str | | Create a new string object from the given object. If encoding or | errors is specified, then the object must expose a data buffer | that will be decoded using the given encoding and error handler. | Otherwise, returns the result of object.__str__() (if defined) | or repr(object). | encoding defaults to sys.getdefaultencoding(). | errors defaults to 'strict'. So str(obj) returns obj.__str__() but str(obj, encoding='xxx') decodes a byte string (or a similar object) using a given encoding. In most cases obj will be a byte string and it will be equivalent to using obj.decode('xxx'). I think the help text is a little confusing. It says that encoding defaults to sys.getdefaultencoding() but doesn't clarify but this only applies if errors is given as a keyword argument since otherwise no decoding is performed. Perhaps the help text would be clearer if it listed the two operations as two separate cases e.g.: str(object) Returns a string object from object.__str__() if it is defined or otherwise object.__repr__(). Raises TypeError if the returned result is not a string object. str(bytes, [encoding[, errors]]) If either encoding or errors is supplied, creates a new string object by decoding bytes with the specified encoding. The bytes argument can be any object that supports the buffer interface. encoding defaults to sys.getdefaultencoding() and errors defaults to 'strict'. > Whenever Python3 encounters a bytestring it needs an encoding to convert it to > a string. Well actually Python 3.3 will happily convert it to a string using bytes.__repr__ if you don't supply the encoding argument: >>> str(b'this is a byte string') "b'this is a byte string'" > If I feed a list of bytestrings or a list of list of bytestrings to > 'str' , etc, it should use the encoding for each bytestring component of the > given data structure. You can always do: [str(obj, encoding='xxx') for obj in list_of_byte_strings] > How can I convert a data strucure of arbitrarily complex nature, which contains > bytestrings somewhere, to a string? Using str(obj) or repr(obj). Of course this relies on the author of type(obj) defining the appropriate methods and writing the code that actually converts the object into a string. > This problem has arisen while converting a working Python2 script to Python3.3. > Since Python2 doesn't have bytestrings it just works. In Python 2 ordinary strings are byte strings. > Tell me how to convert str(obj) from Python2 to Python3 if obj is an > arbitrarily complex data structure containing bytestrings somewhere > which have to be converted to strings with a given encoding? The str function when used to convert a non-string object into a string knows nothing about the object you provide except whether it has __str__ or __repr__ methods. The only processing that is done is to check that the returned result was actually a string: >>> class A: ... def __str__(self): ... return [] ... >>> a = A() >>> str(a) Traceback (most recent call last): File "", line 1, in TypeError: __str__ returned non-string (type list) Perhaps it would help if you would explain why you want the string object. I would only use str(complex_object) as something to print for debugging so I would actually want it to show me which strings were byte strings by marking them with a 'b' prefix and I would also want it to show non-ascii characters with a \x hex code as it already does: >>> a = [1, 2, b'caf\xe9'] >>> str(a) "[1, 2, b'caf\\xe9']" If I wanted to convert the object to a string in order to e.g. save it to a file or database then I would write a function to create the string that I wanted. I would only use str() to convert elementary types like int and float into strings. Oscar From no.email at nospam.invalid Sat Nov 10 11:51:33 2012 From: no.email at nospam.invalid (Paul Rubin) Date: Sat, 10 Nov 2012 08:51:33 -0800 Subject: Format specification mini-language for list joining References: <92ada057-5a72-4186-bd22-4ccb600b8e45@googlegroups.com> Message-ID: <7xwqxt765m.fsf@ruckus.brouhaha.com> Tobia Conforto writes: > Now, as much as I appreciate the heritage of Lisp, I won't deny than > its format string mini-language is EVIL. ... Still, this is the > grand^n-father of Python's format strings... Without having yet read the rest of your post carefully, I wonder the particular historical point above is correct. Python's format strings are pretty much the same as C's format strings, which go back to the beginnings of C in the 1970's, maybe even to some forerunner of C, like maybe FOCAL or something like that. It's possible that Common Lisp's format strings came from some earlier Lisp, but Common Lisp itself was a 1980's thing. Maybe some Lisp historian would know. From steve+comp.lang.python at pearwood.info Sat Nov 10 11:55:29 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 10 Nov 2012 16:55:29 GMT Subject: Format specification mini-language for list joining References: <92ada057-5a72-4186-bd22-4ccb600b8e45@googlegroups.com> Message-ID: <509e8700$0$29980$c3e8da3$5496439d@news.astraweb.com> On Sat, 10 Nov 2012 02:26:28 -0800, Tobia Conforto wrote: > Hello > > Lately I have been writing a lot of list join() operations variously > including (and included in) string format() operations. > > For example: > > temps = [24.369, 24.550, 26.807, 27.531, 28.752] > > out = 'Temperatures: {0} Celsius'.format( > ', '.join('{0:.1f}'.format(t) for t in temps) > ) > > # => 'Temperatures: 24.4, 24.6, 26.8, 27.5, 28.8 Celsius' > > This is just a simple example, my actual code has many more join and > format operations, split into local variables as needed for clarity. Good plan! But then you suggest: > Here is what I came up with: > out = 'Temperatures: {0:", ":.1f} Celsius'.format(temps) > # => 'Temperatures: 24.4, 24.6, 26.8, 27.5, 28.8 Celsius' > > Here ", " is the joiner between the items and <.1f> is the format string > for each item. And there goes all the clarity. Is saving a few words of Python code so important that you would prefer to read and write an overly-terse, cryptic mini-language? If you're worried about code re-use, write a simple helper function: def format_items(format, items): template = '{0:%s}' % format return ', '.join(template.format(item) for item in items) out = 'Temperatures: {0} Celsius'.format( format_items('.1f, temps) ) -- Steven From leegold at operamail.com Sat Nov 10 12:12:18 2012 From: leegold at operamail.com (goldtech) Date: Sat, 10 Nov 2012 09:12:18 -0800 (PST) Subject: How to install libxml2-devel and libxslt-devel in Windows ? Message-ID: <699e96d4-0141-4202-9b0b-deda6532d0a4@v9g2000yql.googlegroups.com> Hi, I have Windows XP and Python 2.7.x I download and install libxml2-python-2.7.7.win32-py2.7.exe, From here: http://users.skynet.be/sbi/libxml-python/ This file has both libxml2 AND libxslt. But, I also need libxml2-devel and libxslt-devel for python 2.7. Are binaries for win32 available for that? How do I to get these devel files? In Ubuntu fro example this is all in the repositories. But I guess I don't understand how to get this in Windows... Thank you very much. From kwpolska at gmail.com Sat Nov 10 12:13:47 2012 From: kwpolska at gmail.com (Kwpolska) Date: Sat, 10 Nov 2012 18:13:47 +0100 Subject: Format specification mini-language for list joining In-Reply-To: <7xwqxt765m.fsf@ruckus.brouhaha.com> References: <92ada057-5a72-4186-bd22-4ccb600b8e45@googlegroups.com> <7xwqxt765m.fsf@ruckus.brouhaha.com> Message-ID: On Sat, Nov 10, 2012 at 5:51 PM, Paul Rubin wrote: > [?] Python's format strings are pretty much the same as C's format strings [?] You?re thinking about the old % syntax, 'Hello %s!' % 'world'. The OP meant the new str.format syntax ('Hello {}!'.format('world')). --- IMO, the idea is useless. First of, format() exists since 2.6, which was released in 2008. So, it would be hard to use it anyways. Second of, which is more readable: out = 'Temperatures: {0:", ":.1f} Celsius'.format(temps) or out = 'Temperatures: {} Celsius'.format(', '.join(temps)) 101% of the Python community would opt for the second format. Because your format is cryptic. The current thing is already not-quite-easy-to-understand when you use magic (aligning, type converting etc.), but your proposition is much worse. And I hate to consult the docs while working on something. As I said, it?s hard to even get this one changed because str.format is 4 years old. -- Kwpolska stop html mail | always bottom-post www.asciiribbon.org | www.netmeister.org/news/learn2quote.html GPG KEY: 5EAAEA16 From bruceg113355 at gmail.com Sat Nov 10 12:56:04 2012 From: bruceg113355 at gmail.com (bruceg113355 at gmail.com) Date: Sat, 10 Nov 2012 09:56:04 -0800 (PST) Subject: Is there a simpler way to modify all arguments in a function before using the arguments? In-Reply-To: References: <18134e77-9b02-4aec-afb0-794ed900d194@googlegroups.com> Message-ID: <1f3e6587-3b7d-4e30-bbaa-42c30df83475@googlegroups.com> All, I never used decorators before. I saw Miki Tebeka's sample code and your rationale (Aahz) and I like it. For my application problem, decorators seem like a good solution. Thanks to all, Bruce On Saturday, November 10, 2012 10:35:12 AM UTC-5, Aahz wrote: > In article , > > Peter Otten <__peter__ at web.de> wrote: > > >Miki Tebeka wrote: > > > > > >>> Is there a simpler way to modify all arguments in a function before using > > >>> the arguments? > > >> > > >> You can use a decorator: > > >> > > >> from functools import wraps > > >> > > >> def fix_args(fn): > > >> @wraps(fn) > > >> def wrapper(*args): > > >> args = (arg.replace('_', '') for arg in args) > > >> return fn(*args) > > >> > > >> return wrapper > > >> > > >> @fix_args > > >> def foo(x, y): > > >> print(x) > > >> print(y) > > > > > >I was tempted to post that myself, but he said /simpler/ ;) > > > > From my POV, that *is* simpler. When you change the parameters for foo, > > you don't need to change the arg pre-processing. Also allows code reuse, > > probably any program needing this kind of processing once will need it > > again. > > -- > > Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ > > > > "....Normal is what cuts off your sixth finger and your tail..." --Siobhan From rosuav at gmail.com Sat Nov 10 13:33:58 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 11 Nov 2012 05:33:58 +1100 Subject: Is there a simpler way to modify all arguments in a function before using the arguments? In-Reply-To: <8e17124b-63ee-4166-8964-80f47f5716d5@googlegroups.com> References: <18134e77-9b02-4aec-afb0-794ed900d194@googlegroups.com> <509daadc$0$29980$c3e8da3$5496439d@news.astraweb.com> <8e17124b-63ee-4166-8964-80f47f5716d5@googlegroups.com> Message-ID: On Sun, Nov 11, 2012 at 12:15 AM, wrote: > Thanks to all. > Steve's example is the one I will try next week. > Passing in lists, will work but it requires extra coding from the calling routines to build the list. Not necessarily! Watch: def foo(*args): print(repr(args)) foo("Hello","world","!") ('Hello', 'world', '!') Okay, that's not technically a list, it's a tuple, but same diff. Your callers still see you as taking separate arguments, but you take them as a single collection. ChrisA From nameDOTportua at gmail.com Sat Nov 10 14:33:05 2012 From: nameDOTportua at gmail.com (Jennie) Date: Sat, 10 Nov 2012 20:33:05 +0100 Subject: Method default argument whose type is the class not yet defined Message-ID: What is the best solution to solve the following problem in Python 3.3? import math >>> class Point: ... def __init__(self, x=0, y=0): ... self.x = x ... self.y = y ... def __sub__(self, other): ... return Point(self.x - other.x, self.y - other.y) ... def distance(self, point=Point()): ... """Return the distance from `point`.""" ... return math.sqrt((self - point).x ** 2 + (self - point).y ** 2) ... Traceback (most recent call last): File "", line 1, in File "", line 5, in Point NameError: name 'Point' is not defined I propose three solutions. The first one: >>> class Point: ... def __init__(self, x=0, y=0): ... self.x = x ... self.y = y ... def __sub__(self, other): ... return Point(self.x - other.x, self.y - other.y) ... def distance(self, point=None): ... p = point if point else Point() ... return math.sqrt((self - p).x ** 2 + (self - p).y ** 2) ... >>> p = Point() >>> p.distance() 0.0 >>> p.distance(Point(3, 4)) 5.0 The second one: >>> class Point: ... def __init__(self, x=0, y=0): ... self.x = x ... self.y = y ... def __sub__(self, other): ... return Point(self.x - other.x, self.y - other.y) ... >>> def distance(self, point=Point()): ... return math.sqrt((self - point).x ** 2 + (self - point).y ** 2) ... >>> Point.distance = distance >>> p = Point() >>> p.distance(Point(3, 4)) 5.0 The last one: >>> class Point: ... def __init__(self, x=0, y=0): ... self.x = x ... self.y = y ... Point.distance = distance ... def __sub__(self, other): ... return Point(self.x - other.x, self.y - other.y) ... >>> def distance(self, point=Point()): ... return math.sqrt((self - point).x ** 2 + (self - point).y ** 2) ... >>> p = Point() >>> p.distance(Point(3, 4)) 5.0 Is there a better solution? -- Jennie From rosuav at gmail.com Sat Nov 10 14:56:17 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 11 Nov 2012 06:56:17 +1100 Subject: Method default argument whose type is the class not yet defined In-Reply-To: References: Message-ID: On Sun, Nov 11, 2012 at 6:33 AM, Jennie wrote: > ... def distance(self, point=None): > ... p = point if point else Point() I'd go with this one. Definitely not the third one, which mutates the class according to a current global every time a Point is instantiated - could be *extremely* confusing if the name distance were ever rebound. You could also fiddle with the default args: >>> class Point: def __init__(self, x=0, y=0): self.x = x self.y = y def __sub__(self, other): return Point(self.x - other.x, self.y - other.y) def distance(self, point="Point()"): return math.sqrt((self - p).x ** 2 + (self - p).y ** 2) >>> Point.distance.__defaults__=Point(), # has to be a tuple ChrisA From tjreedy at udel.edu Sat Nov 10 15:29:39 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 10 Nov 2012 15:29:39 -0500 Subject: Method default argument whose type is the class not yet defined In-Reply-To: References: Message-ID: On 11/10/2012 2:33 PM, Jennie wrote: > What is the best solution to solve the following problem in Python 3.3? > > import math > >>> class Point: > ... def __init__(self, x=0, y=0): > ... self.x = x > ... self.y = y > ... def __sub__(self, other): > ... return Point(self.x - other.x, self.y - other.y) > ... def distance(self, point=Point()): > ... """Return the distance from `point`.""" > ... return math.sqrt((self - point).x ** 2 + (self - point).y ** 2) > ... > Traceback (most recent call last): > File "", line 1, in > File "", line 5, in Point > NameError: name 'Point' is not defined > > I propose three solutions. The first one: > > >>> class Point: > ... def __init__(self, x=0, y=0): > ... self.x = x > ... self.y = y > ... def __sub__(self, other): > ... return Point(self.x - other.x, self.y - other.y) > ... def distance(self, point=None): > ... p = point if point else Point() > ... return math.sqrt((self - p).x ** 2 + (self - p).y ** 2) > ... > >>> p = Point() > >>> p.distance() > 0.0 > >>> p.distance(Point(3, 4)) > 5.0 What I do not like about this one is that it creates a new 0 point each time one is needed. Two solutions: change Point() to point0 in the distance function and create point0 = Point() after the class. -or- instead of p = line, px,py = point.x, point.y if point else 0.0, 0.0 > The second one: > > >>> class Point: > ... def __init__(self, x=0, y=0): > ... self.x = x > ... self.y = y > ... def __sub__(self, other): > ... return Point(self.x - other.x, self.y - other.y) > ... > >>> def distance(self, point=Point()): > ... return math.sqrt((self - point).x ** 2 + (self - point).y ** 2) > ... > >>> Point.distance = distance > >>> p = Point() > >>> p.distance(Point(3, 4)) > 5.0 my first thought > The last one: > > >>> class Point: > ... def __init__(self, x=0, y=0): > ... self.x = x > ... self.y = y > ... Point.distance = distance > ... def __sub__(self, other): > ... return Point(self.x - other.x, self.y - other.y) > ... > >>> def distance(self, point=Point()): > ... return math.sqrt((self - point).x ** 2 + (self - point).y ** 2) > ... > >>> p = Point() > >>> p.distance(Point(3, 4)) > 5.0 > > Is there a better solution? -- Terry Jan Reedy From marco.buttu at gmail.com Sat Nov 10 15:51:38 2012 From: marco.buttu at gmail.com (Jennie) Date: Sat, 10 Nov 2012 21:51:38 +0100 Subject: Method default argument whose type is the class not yet defined In-Reply-To: References: Message-ID: <509EBE5A.5080405@gmail.com> On 11/10/2012 09:29 PM, Terry Reedy wrote: > On 11/10/2012 2:33 PM, Jennie wrote: >> >> I propose three solutions. The first one: >> >> >>> class Point: >> ... def __init__(self, x=0, y=0): >> ... self.x = x >> ... self.y = y >> ... def __sub__(self, other): >> ... return Point(self.x - other.x, self.y - other.y) >> ... def distance(self, point=None): >> ... p = point if point else Point() >> ... return math.sqrt((self - p).x ** 2 + (self - p).y ** 2) > What I do not like about this one is that it creates a new 0 point each > time one is needed. Two solutions: > > change Point() to point0 in the distance function and create > point0 = Point() > after the class. > > -or- > instead of p = line, > px,py = point.x, point.y if point else 0.0, 0.0 Thanks, I like the second one :) -- Jennie From nameDOTportua at gmail.com Sat Nov 10 15:51:38 2012 From: nameDOTportua at gmail.com (Jennie) Date: Sat, 10 Nov 2012 21:51:38 +0100 Subject: Method default argument whose type is the class not yet defined References: Message-ID: <509EBE5A.5080405@gmail.com> On 11/10/2012 09:29 PM, Terry Reedy wrote: > On 11/10/2012 2:33 PM, Jennie wrote: >> >> I propose three solutions. The first one: >> >> >>> class Point: >> ... def __init__(self, x=0, y=0): >> ... self.x = x >> ... self.y = y >> ... def __sub__(self, other): >> ... return Point(self.x - other.x, self.y - other.y) >> ... def distance(self, point=None): >> ... p = point if point else Point() >> ... return math.sqrt((self - p).x ** 2 + (self - p).y ** 2) > What I do not like about this one is that it creates a new 0 point each > time one is needed. Two solutions: > > change Point() to point0 in the distance function and create > point0 = Point() > after the class. > > -or- > instead of p = line, > px,py = point.x, point.y if point else 0.0, 0.0 Thanks, I like the second one :) -- Jennie From tobia.conforto at gmail.com Sat Nov 10 16:11:10 2012 From: tobia.conforto at gmail.com (Tobia Conforto) Date: Sat, 10 Nov 2012 13:11:10 -0800 (PST) Subject: Format specification mini-language for list joining In-Reply-To: References: <92ada057-5a72-4186-bd22-4ccb600b8e45@googlegroups.com> <7xwqxt765m.fsf@ruckus.brouhaha.com> Message-ID: <963ca43d-acc9-44dc-a323-e4939ce28557@googlegroups.com> Kwpolska wrote: > > out = 'Temperatures: {0:", ":.1f} Celsius'.format(temps) > > [...] your format is cryptic. Thank you for your criticism, I'll think it over. The reason I find it readable (-enough) is because even without knowing what format language is supported by the temps object, you can tell that "it" (the 0th argument in this case) is what's going to be serialized in that place. Everything after the first colon is game anyways, meaning you'll have to look it up in the docs, because it's defined somewhere in the class hierarchy of the object being serialized. The fact that 99% of classes don't define a __format__ method and thus fall back on object's implementation, with it's alignment and padding operators, is IMHO irrelevant. It's still something you can't pretend to know out of the box, because it's supposed to be customizable by classes. Knowing this, if you know that the temps object is a list of floats, then I think it'd be pretty obvious what the ", " and the :.1f should do. > As I said, it?s hard to even get this one changed > because str.format is 4 years old. Again, I beg to differ. I'm not proposing any change to format (that would be madness). What I'm proposing is the addition of a customized __format__ method to a few types, namely lists and sequences, that currently lack it (as do 99% of classes) and fall back to object's implementation. Which is kind of pointless with lists, as joining is by far the thing most often done to them when formatting. Tobia From tobia.conforto at gmail.com Sat Nov 10 16:11:10 2012 From: tobia.conforto at gmail.com (Tobia Conforto) Date: Sat, 10 Nov 2012 13:11:10 -0800 (PST) Subject: Format specification mini-language for list joining In-Reply-To: References: <92ada057-5a72-4186-bd22-4ccb600b8e45@googlegroups.com> <7xwqxt765m.fsf@ruckus.brouhaha.com> Message-ID: <963ca43d-acc9-44dc-a323-e4939ce28557@googlegroups.com> Kwpolska wrote: > > out = 'Temperatures: {0:", ":.1f} Celsius'.format(temps) > > [...] your format is cryptic. Thank you for your criticism, I'll think it over. The reason I find it readable (-enough) is because even without knowing what format language is supported by the temps object, you can tell that "it" (the 0th argument in this case) is what's going to be serialized in that place. Everything after the first colon is game anyways, meaning you'll have to look it up in the docs, because it's defined somewhere in the class hierarchy of the object being serialized. The fact that 99% of classes don't define a __format__ method and thus fall back on object's implementation, with it's alignment and padding operators, is IMHO irrelevant. It's still something you can't pretend to know out of the box, because it's supposed to be customizable by classes. Knowing this, if you know that the temps object is a list of floats, then I think it'd be pretty obvious what the ", " and the :.1f should do. > As I said, it?s hard to even get this one changed > because str.format is 4 years old. Again, I beg to differ. I'm not proposing any change to format (that would be madness). What I'm proposing is the addition of a customized __format__ method to a few types, namely lists and sequences, that currently lack it (as do 99% of classes) and fall back to object's implementation. Which is kind of pointless with lists, as joining is by far the thing most often done to them when formatting. Tobia From solipsis at pitrou.net Sat Nov 10 17:28:17 2012 From: solipsis at pitrou.net (Antoine Pitrou) Date: Sat, 10 Nov 2012 22:28:17 +0000 (UTC) Subject: Py3k Signal Handling References: Message-ID: Jonathan Hayward pobox.com> writes: > > What needs changing here and how should I change it so that handle_signal() > is called and then things keep on ticking? So that it is called when exactly? Your message isn't clear as to what isn't working for you. Regards Antoine. From d at davea.name Sat Nov 10 17:30:14 2012 From: d at davea.name (Dave Angel) Date: Sat, 10 Nov 2012 17:30:14 -0500 Subject: Method default argument whose type is the class not yet defined In-Reply-To: <509EBE5A.5080405@gmail.com> References: <509EBE5A.5080405@gmail.com> Message-ID: <509ED576.5020909@davea.name> On 11/10/2012 03:51 PM, Jennie wrote: > On 11/10/2012 09:29 PM, Terry Reedy wrote: > >> On 11/10/2012 2:33 PM, Jennie wrote: >>> >>> I propose three solutions. The first one: >>> >>> >>> class Point: >>> ... def __init__(self, x=0, y=0): >>> ... self.x = x >>> ... self.y = y >>> ... def __sub__(self, other): >>> ... return Point(self.x - other.x, self.y - other.y) >>> ... def distance(self, point=None): >>> ... p = point if point else Point() >>> ... return math.sqrt((self - p).x ** 2 + (self - p).y ** 2) > >> What I do not like about this one is that it creates a new 0 point each >> time one is needed. Two solutions: >> >> change Point() to point0 in the distance function and create >> point0 = Point() >> after the class. >> >> -or- >> instead of p = line, >> px,py = point.x, point.y if point else 0.0, 0.0 > > Thanks, I like the second one :) > I like the first, once you fix the minor inefficiency in it; add the qualifier "is None" ... def distance(self, point=None): ... p = point if point is None else Point() ... return math.sqrt((self - p).x ** 2 + (self - p).y ** 2) The advantage it then has over the second one is that the whole class is defined inside the class. -- DaveA From roy at panix.com Sat Nov 10 17:58:14 2012 From: roy at panix.com (Roy Smith) Date: Sat, 10 Nov 2012 17:58:14 -0500 Subject: A gnarly little python loop Message-ID: I'm trying to pull down tweets with one of the many twitter APIs. The particular one I'm using (python-twitter), has a call: data = api.GetSearch(term="foo", page=page) The way it works, you start with page=1. It returns a list of tweets. If the list is empty, there are no more tweets. If the list is not empty, you can try to get more tweets by asking for page=2, page=3, etc. I've got: page = 1 while 1: r = api.GetSearch(term="foo", page=page) if not r: break for tweet in r: process(tweet) page += 1 It works, but it seems excessively fidgety. Is there some cleaner way to refactor this? From ian.g.kelly at gmail.com Sat Nov 10 18:17:08 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sat, 10 Nov 2012 16:17:08 -0700 Subject: A gnarly little python loop In-Reply-To: References: Message-ID: On Sat, Nov 10, 2012 at 3:58 PM, Roy Smith wrote: > I'm trying to pull down tweets with one of the many twitter APIs. The > particular one I'm using (python-twitter), has a call: > > data = api.GetSearch(term="foo", page=page) > > The way it works, you start with page=1. It returns a list of tweets. > If the list is empty, there are no more tweets. If the list is not > empty, you can try to get more tweets by asking for page=2, page=3, etc. > I've got: > > page = 1 > while 1: > r = api.GetSearch(term="foo", page=page) > if not r: > break > for tweet in r: > process(tweet) > page += 1 > > It works, but it seems excessively fidgety. Is there some cleaner way > to refactor this? I'd do something like this: def get_tweets(term): for page in itertools.count(1): r = api.GetSearch(term, page) if not r: break for tweet in r: yield tweet for tweet in get_tweets("foo"): process(tweet) From steve+comp.lang.python at pearwood.info Sat Nov 10 19:23:07 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 11 Nov 2012 00:23:07 GMT Subject: A gnarly little python loop References: Message-ID: <509eefeb$0$29980$c3e8da3$5496439d@news.astraweb.com> On Sat, 10 Nov 2012 17:58:14 -0500, Roy Smith wrote: > The way it works, you start with page=1. It returns a list of tweets. > If the list is empty, there are no more tweets. If the list is not > empty, you can try to get more tweets by asking for page=2, page=3, etc. > I've got: > > page = 1 > while 1: > r = api.GetSearch(term="foo", page=page) > if not r: > break > for tweet in r: > process(tweet) > page += 1 > > It works, but it seems excessively fidgety. Is there some cleaner way > to refactor this? Seems clean enough to me. It does exactly what you need: loop until there are no more tweets, process each tweet. If you're allergic to nested loops, move the inner for-loop into a function. Also you could get rid of the "if r: break". page = 1 r = ["placeholder"] while r: r = api.GetSearch(term="foo", page=page) process_all(tweets) # does nothing if r is empty page += 1 Another way would be to use a for list for the outer loop. for page in xrange(1, sys.maxint): r = api.GetSearch(term="foo", page=page) if not r: break process_all(r) -- Steven From steve+comp.lang.python at pearwood.info Sat Nov 10 20:13:26 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 11 Nov 2012 01:13:26 GMT Subject: Method default argument whose type is the class not yet defined References: Message-ID: <509efbb6$0$29980$c3e8da3$5496439d@news.astraweb.com> On Sat, 10 Nov 2012 20:33:05 +0100, Jennie wrote: [...] > I propose three solutions. The first one: > > >>> class Point: > ... def __init__(self, x=0, y=0): > ... self.x = x > ... self.y = y > ... def __sub__(self, other): > ... return Point(self.x - other.x, self.y - other.y) Don't do this, because it breaks subclassing. Your instance should dynamically get it's own class, not hard-code the name of Point. return self.__class__(self.x - other.x, self.y - other.y) That way, when you subclass Point, you can do arithmetic on the subclass instances and they will do the Right Thing. Note: Python's builtin numeric types don't do this, and it is a damned nuisance: py> class MyInt(int): ... pass ... py> a, b = MyInt(23), MyInt(42) py> assert type(a) is MyInt and type(b) is MyInt py> type(a + b) Back to your class: > ... def distance(self, point=None): > ... p = point if point else Point() > ... return math.sqrt((self - p).x ** 2 + (self - p).y ** 2) Almost but not quite. I assume that, in a full Point class, you would want Point(0, 0) to count as false in a boolean context. (A "falsey" value, like None, [], 0.0, etc.) So change the test to an explicit test for None, not just any falsey value: if point is None: point = self.__class__() # Allow subclassing to work. > The second one: > > >>> class Point: > ... def __init__(self, x=0, y=0): > ... self.x = x > ... self.y = y > ... def __sub__(self, other): > ... return Point(self.x - other.x, self.y - other.y) > ... > >>> def distance(self, point=Point()): > ... return math.sqrt((self - point).x ** 2 + (self - point).y ** 2) > ... > >>> Point.distance = distance Cute, but ugly and messy. You can inject methods into a class, of course, but that's an awfully big hammer to crack this tiny little nut. Your first solution is better. Here is a variation which, according to your tastes, may count as more or less ugly: inject the default value into the method: class Point: def distance(self, other=None): # None is a placeholder delta = self - other return math.sqrt(delta.x ** 2 + delta.y ** 2) Point.distance.__defaults__ = (Point(),) # In Python 2, use: # Point.distance.__func__.func_defaults = (Point(),) -- Steven From oscar.j.benjamin at gmail.com Sat Nov 10 20:23:08 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Sun, 11 Nov 2012 01:23:08 +0000 Subject: Method default argument whose type is the class not yet defined In-Reply-To: References: Message-ID: On 10 November 2012 19:33, Jennie wrote: > What is the best solution to solve the following problem in Python 3.3? > > import math >>>> class Point: > ... def __init__(self, x=0, y=0): > ... self.x = x > ... self.y = y > ... def __sub__(self, other): > ... return Point(self.x - other.x, self.y - other.y) > ... def distance(self, point=Point()): > ... """Return the distance from `point`.""" > ... return math.sqrt((self - point).x ** 2 + (self - point).y ** 2) > ... > Traceback (most recent call last): > File "", line 1, in > File "", line 5, in Point > NameError: name 'Point' is not defined I would use namedtuple and make it so that an ordinary tuple could be used as in place of a Point instance: >>> import math >>> from collections import namedtuple >>> class Point(namedtuple('Point', ['x', 'y'])): ... def distance(self, other=(0, 0)): ... (myx, myy), (theirx, theiry) = self, other ... return math.sqrt((myx - theirx) ** 2 + (myy - theiry) ** 2) ... >>> p = Point(3, 4) >>> p.distance() 5.0 >>> p2 = Point(4, 5) >>> p.distance(p2) 1.4142135623730951 Oscar From rosuav at gmail.com Sat Nov 10 21:13:51 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 11 Nov 2012 13:13:51 +1100 Subject: Method default argument whose type is the class not yet defined In-Reply-To: <509efbb6$0$29980$c3e8da3$5496439d@news.astraweb.com> References: <509efbb6$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, Nov 11, 2012 at 12:13 PM, Steven D'Aprano wrote: > Almost but not quite. I assume that, in a full Point class, you would > want Point(0, 0) to count as false in a boolean context. (A "falsey" > value, like None, [], 0.0, etc.) I would not assume that. The origin is a point, just like any other. With a Line class, you could deem a zero-length line to be like a zero-element list, but Point(0,0) is more like the tuple (0,0) which is definitely True. In any case, this would not even matter, beyond unnecessary work; the bug would occur only if you seek the distance to Point(0,0), at which point[1] the code would throw out the incoming Point and go with the default of 0,0. So it'd result in the same distance. ChrisA [1] Sorry, couldn't resist From ian.g.kelly at gmail.com Sat Nov 10 21:43:16 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sat, 10 Nov 2012 19:43:16 -0700 Subject: Method default argument whose type is the class not yet defined In-Reply-To: References: <509efbb6$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, Nov 10, 2012 at 7:13 PM, Chris Angelico wrote: > I would not assume that. The origin is a point, just like any other. > With a Line class, you could deem a zero-length line to be like a > zero-element list, but Point(0,0) is more like the tuple (0,0) which > is definitely True. It's more like the number 0 than the tuple (0,0). 0 is the origin on a 1-dimensional number line. (0,0) is the origin on a 2-dimensional number plane. In fact, it might be pointed out that Point(0, 0) is a generalization of 0+0j, which is equal to 0. From rosuav at gmail.com Sat Nov 10 21:47:47 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 11 Nov 2012 13:47:47 +1100 Subject: Method default argument whose type is the class not yet defined In-Reply-To: References: <509efbb6$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, Nov 11, 2012 at 1:43 PM, Ian Kelly wrote: > On Sat, Nov 10, 2012 at 7:13 PM, Chris Angelico wrote: >> I would not assume that. The origin is a point, just like any other. >> With a Line class, you could deem a zero-length line to be like a >> zero-element list, but Point(0,0) is more like the tuple (0,0) which >> is definitely True. > > It's more like the number 0 than the tuple (0,0). > > 0 is the origin on a 1-dimensional number line. > (0,0) is the origin on a 2-dimensional number plane. > > In fact, it might be pointed out that Point(0, 0) is a generalization > of 0+0j, which is equal to 0. Ah, good point. In any case, though, it'd be an utterly inconsequential bug. ChrisA From roy at panix.com Sat Nov 10 21:53:42 2012 From: roy at panix.com (Roy Smith) Date: Sat, 10 Nov 2012 21:53:42 -0500 Subject: Method default argument whose type is the class not yet defined References: <509efbb6$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article , Ian Kelly wrote: > On Sat, Nov 10, 2012 at 7:13 PM, Chris Angelico wrote: > > I would not assume that. The origin is a point, just like any other. > > With a Line class, you could deem a zero-length line to be like a > > zero-element list, but Point(0,0) is more like the tuple (0,0) which > > is definitely True. > > It's more like the number 0 than the tuple (0,0). > > 0 is the origin on a 1-dimensional number line. > (0,0) is the origin on a 2-dimensional number plane. > In fact, it might be pointed out that Point(0, 0) is a generalization > of 0+0j, which is equal to 0. If (0,0) is the origin on a plane, then (0,) should be the origin on a line. If you consider 0 + 0j to be the origin of a plane, then 0 is the origin of a line. Either way is plausible, but you need to be consistent. From showell at domaintools.com Sat Nov 10 22:03:42 2012 From: showell at domaintools.com (Steve Howell) Date: Sat, 10 Nov 2012 19:03:42 -0800 (PST) Subject: A gnarly little python loop References: Message-ID: <5a260a79-818d-47a8-9404-37b014587730@px4g2000pbc.googlegroups.com> On Nov 10, 2:58?pm, Roy Smith wrote: > I'm trying to pull down tweets with one of the many twitter APIs. ?The > particular one I'm using (python-twitter), has a call: > > data = api.GetSearch(term="foo", page=page) > > The way it works, you start with page=1. ?It returns a list of tweets. > If the list is empty, there are no more tweets. ?If the list is not > empty, you can try to get more tweets by asking for page=2, page=3, etc. > I've got: > > ? ? page = 1 > ? ? while 1: > ? ? ? ? r = api.GetSearch(term="foo", page=page) > ? ? ? ? if not r: > ? ? ? ? ? ? break > ? ? ? ? for tweet in r: > ? ? ? ? ? ? process(tweet) > ? ? ? ? page += 1 > > It works, but it seems excessively fidgety. ?Is there some cleaner way > to refactor this? I think your code is perfectly readable and clean, but you can flatten it like so: def get_tweets(term, get_page): page_nums = itertools.count(1) pages = itertools.imap(api.getSearch, page_nums) valid_pages = itertools.takewhile(bool, pages) tweets = itertools.chain.from_iterable(valid_pages) return tweets From ian.g.kelly at gmail.com Sun Nov 11 01:43:46 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sat, 10 Nov 2012 23:43:46 -0700 Subject: Method default argument whose type is the class not yet defined In-Reply-To: References: <509efbb6$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, Nov 10, 2012 at 7:53 PM, Roy Smith wrote: > In article , > Ian Kelly wrote: > >> On Sat, Nov 10, 2012 at 7:13 PM, Chris Angelico wrote: >> > I would not assume that. The origin is a point, just like any other. >> > With a Line class, you could deem a zero-length line to be like a >> > zero-element list, but Point(0,0) is more like the tuple (0,0) which >> > is definitely True. >> >> It's more like the number 0 than the tuple (0,0). >> >> 0 is the origin on a 1-dimensional number line. >> (0,0) is the origin on a 2-dimensional number plane. > >> In fact, it might be pointed out that Point(0, 0) is a generalization >> of 0+0j, which is equal to 0. > > > If (0,0) is the origin on a plane, then (0,) should be the origin on a > line. If you consider 0 + 0j to be the origin of a plane, then 0 is the > origin of a line. Either way is plausible, but you need to be > consistent. Where I wrote "(0,0) is the origin" above I was not referring to a point, not a tuple, but I can see how that was confusing. From ian.g.kelly at gmail.com Sun Nov 11 01:45:45 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sat, 10 Nov 2012 23:45:45 -0700 Subject: Method default argument whose type is the class not yet defined In-Reply-To: References: <509efbb6$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, Nov 10, 2012 at 11:43 PM, Ian Kelly wrote: > Where I wrote "(0,0) is the origin" above I was not referring to a > point, not a tuple, but I can see how that was confusing. What I meant to say is I *was* referring to a point. Gah! From stefan_ml at behnel.de Sun Nov 11 02:54:01 2012 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 11 Nov 2012 08:54:01 +0100 Subject: How to install libxml2-devel and libxslt-devel in Windows ? In-Reply-To: <699e96d4-0141-4202-9b0b-deda6532d0a4@v9g2000yql.googlegroups.com> References: <699e96d4-0141-4202-9b0b-deda6532d0a4@v9g2000yql.googlegroups.com> Message-ID: goldtech, 10.11.2012 18:12: > I have Windows XP and Python 2.7.x > > I download and install libxml2-python-2.7.7.win32-py2.7.exe, From > here: http://users.skynet.be/sbi/libxml-python/ > > This file has both libxml2 AND libxslt. > > But, I also need libxml2-devel and libxslt-devel for python 2.7. Are > binaries for win32 available for that? > > How do I to get these devel files? In Ubuntu fro example this is all > in the repositories. But I guess I don't understand how to get this in > Windows... Assuming that you are actually looking for lxml rather than the plain libxml2/libxslt Python bindings and seeing that you don't fear installing software from arbitrary web pages, you should try this binary installer: http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml Stefan From stefan_ml at behnel.de Sun Nov 11 02:56:10 2012 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 11 Nov 2012 08:56:10 +0100 Subject: A gnarly little python loop In-Reply-To: <5a260a79-818d-47a8-9404-37b014587730@px4g2000pbc.googlegroups.com> References: <5a260a79-818d-47a8-9404-37b014587730@px4g2000pbc.googlegroups.com> Message-ID: Steve Howell, 11.11.2012 04:03: > On Nov 10, 2:58 pm, Roy Smith wrote: >> I'm trying to pull down tweets with one of the many twitter APIs. The >> particular one I'm using (python-twitter), has a call: >> >> data = api.GetSearch(term="foo", page=page) >> >> The way it works, you start with page=1. It returns a list of tweets. >> If the list is empty, there are no more tweets. If the list is not >> empty, you can try to get more tweets by asking for page=2, page=3, etc. >> I've got: >> >> page = 1 >> while 1: >> r = api.GetSearch(term="foo", page=page) >> if not r: >> break >> for tweet in r: >> process(tweet) >> page += 1 >> >> It works, but it seems excessively fidgety. Is there some cleaner way >> to refactor this? > > I think your code is perfectly readable and clean, but you can flatten > it like so: > > def get_tweets(term, get_page): > page_nums = itertools.count(1) > pages = itertools.imap(api.getSearch, page_nums) > valid_pages = itertools.takewhile(bool, pages) > tweets = itertools.chain.from_iterable(valid_pages) > return tweets I'd prefer the original code ten times over this inaccessible beast. Stefan From nagurbasha55 at gmail.com Sun Nov 11 02:57:17 2012 From: nagurbasha55 at gmail.com (nagurbasha55 at gmail.com) Date: Sat, 10 Nov 2012 23:57:17 -0800 (PST) Subject: KAJOL SEX VIDEOS''''''''''''''''''' In-Reply-To: References: Message-ID: On Thursday, August 2, 2012 10:05:25 PM UTC+5:30, devi wrote: > KAJAL SEX VIDEOS > > http://maxworkerds.co.cc From cs at zip.com.au Sun Nov 11 03:48:36 2012 From: cs at zip.com.au (Cameron Simpson) Date: Sun, 11 Nov 2012 19:48:36 +1100 Subject: A gnarly little python loop In-Reply-To: References: Message-ID: <20121111084835.GA19579@cskk.homeip.net> On 11Nov2012 08:56, Stefan Behnel wrote: | Steve Howell, 11.11.2012 04:03: | > On Nov 10, 2:58 pm, Roy Smith wrote: | >> page = 1 | >> while 1: | >> r = api.GetSearch(term="foo", page=page) | >> if not r: | >> break | >> for tweet in r: | >> process(tweet) | >> page += 1 | >> | >> It works, but it seems excessively fidgety. Is there some cleaner way | >> to refactor this? | > | > I think your code is perfectly readable and clean, but you can flatten | > it like so: | > | > def get_tweets(term, get_page): | > page_nums = itertools.count(1) | > pages = itertools.imap(api.getSearch, page_nums) | > valid_pages = itertools.takewhile(bool, pages) | > tweets = itertools.chain.from_iterable(valid_pages) | > return tweets | | I'd prefer the original code ten times over this inaccessible beast. Me too. -- Cameron Simpson In an insane society, the sane man must appear insane. - Keith A. Schauer From no.email at nospam.invalid Sun Nov 11 04:09:37 2012 From: no.email at nospam.invalid (Paul Rubin) Date: Sun, 11 Nov 2012 01:09:37 -0800 Subject: A gnarly little python loop References: Message-ID: <7x4nkwzesu.fsf@ruckus.brouhaha.com> Cameron Simpson writes: > | I'd prefer the original code ten times over this inaccessible beast. > Me too. Me, I like the itertools version better. There's one chunk of data that goes through a succession of transforms each of which is very straightforward. From __peter__ at web.de Sun Nov 11 04:28:55 2012 From: __peter__ at web.de (Peter Otten) Date: Sun, 11 Nov 2012 10:28:55 +0100 Subject: Is there a simpler way to modify all arguments in a function before using the arguments? References: <18134e77-9b02-4aec-afb0-794ed900d194@googlegroups.com> Message-ID: Aahz wrote: > In article , > Peter Otten <__peter__ at web.de> wrote: >>Miki Tebeka wrote: >> >>>> Is there a simpler way to modify all arguments in a function before >>>> using the arguments? >>> >>> You can use a decorator: >>> >>> from functools import wraps >>> >>> def fix_args(fn): >>> @wraps(fn) >>> def wrapper(*args): >>> args = (arg.replace('_', '') for arg in args) >>> return fn(*args) >>> >>> return wrapper >>> >>> @fix_args >>> def foo(x, y): >>> print(x) >>> print(y) >> >>I was tempted to post that myself, but he said /simpler/ ;) > > From my POV, that *is* simpler. When you change the parameters for foo, > you don't need to change the arg pre-processing. Also allows code reuse, > probably any program needing this kind of processing once will need it > again. Typical changes would be @fix_args def bar(x, y=None): print(x) print(y) @fix_args def baz(file, x, y): print(s, file=file) Do you find it obvious what bar("a_b") bar("a_b", y="c_d") print? Do you find the traceback produced by the latter helpful? Moving complexity into a helper function often makes client code simpler because if the helper is well-tested and preferrably maintained by someone else the part that you have to deal with becomes simpler, but the overall complexity still increases. A fix_args() decorator is worthwhile only if you need it more than once or twice, and because it is hard to generalise I expect that yagni. From __peter__ at web.de Sun Nov 11 04:54:33 2012 From: __peter__ at web.de (Peter Otten) Date: Sun, 11 Nov 2012 10:54:33 +0100 Subject: A gnarly little python loop References: <7x4nkwzesu.fsf@ruckus.brouhaha.com> Message-ID: Paul Rubin wrote: > Cameron Simpson writes: >> | I'd prefer the original code ten times over this inaccessible beast. >> Me too. > > Me, I like the itertools version better. There's one chunk of data > that goes through a succession of transforms each of which > is very straightforward. [Steve Howell] > def get_tweets(term, get_page): > page_nums = itertools.count(1) > pages = itertools.imap(api.getSearch, page_nums) > valid_pages = itertools.takewhile(bool, pages) > tweets = itertools.chain.from_iterable(valid_pages) > return tweets But did you spot the bug(s)? My itertools-based version would look like this def get_tweets(term): pages = (api.GetSearch(term, pageno) for pageno in itertools.count(1)) for page in itertools.takewhile(bool, pages): yield from page but I can understand that it's not everybody's cup of tea. From danielkleinad at gmail.com Sun Nov 11 08:42:35 2012 From: danielkleinad at gmail.com (danielk) Date: Sun, 11 Nov 2012 05:42:35 -0800 (PST) Subject: Printing characters outside of the ASCII range In-Reply-To: References: <3d4644f8-ab88-41c5-9a52-2a5678dd64c0@googlegroups.com> <99d5bd83-35ab-4801-b953-391c497c35bf@googlegroups.com> Message-ID: <90c86fc7-a462-4a19-b883-17c64244c806@googlegroups.com> On Friday, November 9, 2012 5:11:12 PM UTC-5, Ian wrote: > On Fri, Nov 9, 2012 at 2:46 PM, danielk wrote: > > > D:\home\python>pytest.py > > > Traceback (most recent call last): > > > File "D:\home\python\pytest.py", line 1, in > > > print(chr(253).decode('latin1')) > > > AttributeError: 'str' object has no attribute 'decode' > > > > > > Do I need to import something? > > > > Ramit should have written "encode", not "decode". But the above still > > would not work, because chr(253) gives you the character at *Unicode* > > code point 253, not the character with CP437 ordinal 253 that your > > terminal can actually print. The Unicode equivalents of those > > characters are: > > > > >>> list(map(ord, bytes([252, 253, 254]).decode('cp437'))) > > [8319, 178, 9632] > > > > So these are what you would need to encode to CP437 for printing. > > > > >>> print(chr(8319)) > > ? > > >>> print(chr(178)) > > ? > > >>> print(chr(9632)) > > ? > > > > That's probably not the way you want to go about printing them, > > though, unless you mean to be inserting them manually. Is the data > > you get from your database a string, or a bytes object? If the > > former, just do: > > > > print(data.encode('cp437')) > > > > If the latter, then it should be printable as is, unless it is in some > > other encoding than CP437. Ian's solution gives me what I need (thanks Ian!). But I notice a difference between '__str__' and '__repr__'. class Pytest(str): def __init__(self, data = None): if data == None: data = "" self.data = data def __repr__(self): return (self.data).encode('cp437') >>> import pytest >>> p = pytest.Pytest("abc" + chr(178) + "def") >>> print(p) abc?def >>> print(p.data) abc?def >>> print(type(p.data)) If I change '__repr__' to '__str__' then I get: >>> import pytest >>> p = pytest.Pytest("abc" + chr(178) + "def") >>> print(p) Traceback (most recent call last): File "", line 1, in TypeError: __str__ returned non-string (type bytes) Why is '__str__' behaving differently than '__repr__' ? I'd like to be able to use '__str__' because the result is not executable code, it's just a string of the record contents. The documentation for the 'encode' method says: "Return an encoded version of the string as a bytes object." Yet when I displayed the type, it said it was , which I'm taking to be 'type string', or can a 'string' also be 'a string of bytes' ? I'm trying to get my head around all this codecs/unicode stuff. I haven't had to deal with it until now but I'm determined to not let it get the best of me :-) My goals are: a) display a 'raw' database record with the delimiters intact, and b) allow the client to create a string that represents a database record. So, if they know the record format then they should be able to create a database object like it does above, but with the chr(25x) characters. I will handle the conversion of the chr(25x) characters internally. From danielkleinad at gmail.com Sun Nov 11 08:42:35 2012 From: danielkleinad at gmail.com (danielk) Date: Sun, 11 Nov 2012 05:42:35 -0800 (PST) Subject: Printing characters outside of the ASCII range In-Reply-To: References: <3d4644f8-ab88-41c5-9a52-2a5678dd64c0@googlegroups.com> <99d5bd83-35ab-4801-b953-391c497c35bf@googlegroups.com> Message-ID: <90c86fc7-a462-4a19-b883-17c64244c806@googlegroups.com> On Friday, November 9, 2012 5:11:12 PM UTC-5, Ian wrote: > On Fri, Nov 9, 2012 at 2:46 PM, danielk wrote: > > > D:\home\python>pytest.py > > > Traceback (most recent call last): > > > File "D:\home\python\pytest.py", line 1, in > > > print(chr(253).decode('latin1')) > > > AttributeError: 'str' object has no attribute 'decode' > > > > > > Do I need to import something? > > > > Ramit should have written "encode", not "decode". But the above still > > would not work, because chr(253) gives you the character at *Unicode* > > code point 253, not the character with CP437 ordinal 253 that your > > terminal can actually print. The Unicode equivalents of those > > characters are: > > > > >>> list(map(ord, bytes([252, 253, 254]).decode('cp437'))) > > [8319, 178, 9632] > > > > So these are what you would need to encode to CP437 for printing. > > > > >>> print(chr(8319)) > > ? > > >>> print(chr(178)) > > ? > > >>> print(chr(9632)) > > ? > > > > That's probably not the way you want to go about printing them, > > though, unless you mean to be inserting them manually. Is the data > > you get from your database a string, or a bytes object? If the > > former, just do: > > > > print(data.encode('cp437')) > > > > If the latter, then it should be printable as is, unless it is in some > > other encoding than CP437. Ian's solution gives me what I need (thanks Ian!). But I notice a difference between '__str__' and '__repr__'. class Pytest(str): def __init__(self, data = None): if data == None: data = "" self.data = data def __repr__(self): return (self.data).encode('cp437') >>> import pytest >>> p = pytest.Pytest("abc" + chr(178) + "def") >>> print(p) abc?def >>> print(p.data) abc?def >>> print(type(p.data)) If I change '__repr__' to '__str__' then I get: >>> import pytest >>> p = pytest.Pytest("abc" + chr(178) + "def") >>> print(p) Traceback (most recent call last): File "", line 1, in TypeError: __str__ returned non-string (type bytes) Why is '__str__' behaving differently than '__repr__' ? I'd like to be able to use '__str__' because the result is not executable code, it's just a string of the record contents. The documentation for the 'encode' method says: "Return an encoded version of the string as a bytes object." Yet when I displayed the type, it said it was , which I'm taking to be 'type string', or can a 'string' also be 'a string of bytes' ? I'm trying to get my head around all this codecs/unicode stuff. I haven't had to deal with it until now but I'm determined to not let it get the best of me :-) My goals are: a) display a 'raw' database record with the delimiters intact, and b) allow the client to create a string that represents a database record. So, if they know the record format then they should be able to create a database object like it does above, but with the chr(25x) characters. I will handle the conversion of the chr(25x) characters internally. From oscar.j.benjamin at gmail.com Sun Nov 11 09:21:19 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Sun, 11 Nov 2012 14:21:19 +0000 Subject: Method default argument whose type is the class not yet defined In-Reply-To: References: <509efbb6$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 11 November 2012 02:47, Chris Angelico wrote: > On Sun, Nov 11, 2012 at 1:43 PM, Ian Kelly wrote: >> On Sat, Nov 10, 2012 at 7:13 PM, Chris Angelico wrote: >>> I would not assume that. The origin is a point, just like any other. >>> With a Line class, you could deem a zero-length line to be like a >>> zero-element list, but Point(0,0) is more like the tuple (0,0) which >>> is definitely True. >> >> It's more like the number 0 than the tuple (0,0). >> >> 0 is the origin on a 1-dimensional number line. >> (0,0) is the origin on a 2-dimensional number plane. >> >> In fact, it might be pointed out that Point(0, 0) is a generalization >> of 0+0j, which is equal to 0. > > Ah, good point. In any case, though, it'd be an utterly inconsequential bug. You were right the first time, Chris. A point that happens to coincide with the arbitrarily chosen origin is no more truthy or falsey than any other. A vector of length 0 on the other hand is a very different beast. The significance of zero in real algebra is not that it is the origin but rather that it is the additive and multiplicative zero: a + 0 = a for any real number a a * 0 = 0 for any real number a The same is true for a vector v0, of length 0: v + v0 = v for any vector v a * v0 = v0 for any scalar a There is however no meaningful sense in which points (as opposed to vectors) can be added to each other or multiplied by anything, so there is no zero point. The relationship between points and vectors is analogous to the relationship between datetimes and timedeltas. Having Vector(0, 0) evaluate to False is analogous to having timedelta(0) evaluate to False and is entirely sensible. Having Point(0, 0) evaluate to False is precisely the same conceptual folly that sees midnight evaluate as False. Oscar From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Sun Nov 11 09:40:18 2012 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Sun, 11 Nov 2012 15:40:18 +0100 Subject: Printing characters outside of the ASCII range In-Reply-To: <3d4644f8-ab88-41c5-9a52-2a5678dd64c0@googlegroups.com> References: <3d4644f8-ab88-41c5-9a52-2a5678dd64c0@googlegroups.com> Message-ID: Am 09.11.2012 18:17 schrieb danielk: > I'm using this character as a delimiter in my application. Then you probably use the *byte* 254 as opposed to the *character* 254. So it might be better to either switch to byte strings, or output the representation of the string instead of itself. So do print(repr(chr(254))) or, for byte strings, print(bytes([254])). Thomas From tinnews at isbd.co.uk Sun Nov 11 10:14:17 2012 From: tinnews at isbd.co.uk (tinnews at isbd.co.uk) Date: Sun, 11 Nov 2012 15:14:17 +0000 Subject: Question about email.message_from_string() and Message objects Message-ID: <9vr4n9-t2h.ln1@chris.zbmc.eu> I'm a little confused about the relationship between the Python email.parser convenience function email.message_from_string() and the mailbox.Message objects. If I want an mailbox.mboxMessage given the message as a stream of text is the right way to do it as follows (or at least a reasonable way to do it):- msg = email.message_from_string(messageAsAString) mbxMsg = mailbox.mboxMessage(msg) Or can I go direct to mbox by doing:- mbxMsg = mailbox.mboxMessage(messageAsAString) -- Chris Green From lele at metapensiero.it Sun Nov 11 12:09:31 2012 From: lele at metapensiero.it (Lele Gaifax) Date: Sun, 11 Nov 2012 18:09:31 +0100 Subject: Printing characters outside of the ASCII range References: <3d4644f8-ab88-41c5-9a52-2a5678dd64c0@googlegroups.com> <99d5bd83-35ab-4801-b953-391c497c35bf@googlegroups.com> <90c86fc7-a462-4a19-b883-17c64244c806@googlegroups.com> Message-ID: <87zk2oqd6c.fsf@metapensiero.it> danielk writes: > Ian's solution gives me what I need (thanks Ian!). But I notice a > difference between '__str__' and '__repr__'. > > class Pytest(str): > def __init__(self, data = None): > if data == None: data = "" > self.data = data > > def __repr__(self): > return (self.data).encode('cp437') > The correct way of comparing with None (and in general with ?singletons?) is with the ?is? operator, not with ?==?. > If I change '__repr__' to '__str__' then I get: > >>>> import pytest >>>> p = pytest.Pytest("abc" + chr(178) + "def") >>>> print(p) > Traceback (most recent call last): > File "", line 1, in > TypeError: __str__ returned non-string (type bytes) In Python 3.3 there is one kind of string, the one that under Python 2.x was called ?unicode?. When you encode such a string with a specific encoding you obtain a plain ?bytes array?. No surprise that the __str__() method complains, it's called like that for a reason :) > I'm trying to get my head around all this codecs/unicode stuff. I > haven't had to deal with it until now but I'm determined to not let it > get the best of me :-) Two good readings on the subject: - http://nedbatchelder.com/text/unipain.html - http://www.joelonsoftware.com/articles/Unicode.html ciao, lele. -- nickname: Lele Gaifax | Quando vivr? di quello che ho pensato ieri real: Emanuele Gaifas | comincer? ad aver paura di chi mi copia. lele at metapensiero.it | -- Fortunato Depero, 1929. From showell30 at yahoo.com Sun Nov 11 12:16:06 2012 From: showell30 at yahoo.com (Steve Howell) Date: Sun, 11 Nov 2012 09:16:06 -0800 (PST) Subject: A gnarly little python loop In-Reply-To: References: <7x4nkwzesu.fsf@ruckus.brouhaha.com> Message-ID: On Sunday, November 11, 2012 1:54:46 AM UTC-8, Peter Otten wrote: > Paul Rubin wrote: > > > > > Cameron Simpson writes: > > >> | I'd prefer the original code ten times over this inaccessible beast. > > >> Me too. > > > > > > Me, I like the itertools version better. There's one chunk of data > > > that goes through a succession of transforms each of which > > > is very straightforward. > > > > [Steve Howell] > > > def get_tweets(term, get_page): > > > page_nums = itertools.count(1) > > > pages = itertools.imap(api.getSearch, page_nums) > > > valid_pages = itertools.takewhile(bool, pages) > > > tweets = itertools.chain.from_iterable(valid_pages) > > > return tweets > > > > > > But did you spot the bug(s)? > My first version was sketching out the technique, and I don't have handy access to the API. Here is an improved version: def get_tweets(term): def get_page(page): return getSearch(term, page) page_nums = itertools.count(1) pages = itertools.imap(get_page, page_nums) valid_pages = itertools.takewhile(bool, pages) tweets = itertools.chain.from_iterable(valid_pages) return tweets for tweet in get_tweets("foo"): process(tweet) This is what I used to test it: def getSearch(term = "foo", page = 1): # simulate api for testing if page < 5: return [ 'page %d, tweet A for term %s' % (page, term), 'page %d, tweet B for term %s' % (page, term), ] else: return None def process(tweet): print tweet From showell30 at yahoo.com Sun Nov 11 12:16:06 2012 From: showell30 at yahoo.com (Steve Howell) Date: Sun, 11 Nov 2012 09:16:06 -0800 (PST) Subject: A gnarly little python loop In-Reply-To: References: <7x4nkwzesu.fsf@ruckus.brouhaha.com> Message-ID: On Sunday, November 11, 2012 1:54:46 AM UTC-8, Peter Otten wrote: > Paul Rubin wrote: > > > > > Cameron Simpson writes: > > >> | I'd prefer the original code ten times over this inaccessible beast. > > >> Me too. > > > > > > Me, I like the itertools version better. There's one chunk of data > > > that goes through a succession of transforms each of which > > > is very straightforward. > > > > [Steve Howell] > > > def get_tweets(term, get_page): > > > page_nums = itertools.count(1) > > > pages = itertools.imap(api.getSearch, page_nums) > > > valid_pages = itertools.takewhile(bool, pages) > > > tweets = itertools.chain.from_iterable(valid_pages) > > > return tweets > > > > > > But did you spot the bug(s)? > My first version was sketching out the technique, and I don't have handy access to the API. Here is an improved version: def get_tweets(term): def get_page(page): return getSearch(term, page) page_nums = itertools.count(1) pages = itertools.imap(get_page, page_nums) valid_pages = itertools.takewhile(bool, pages) tweets = itertools.chain.from_iterable(valid_pages) return tweets for tweet in get_tweets("foo"): process(tweet) This is what I used to test it: def getSearch(term = "foo", page = 1): # simulate api for testing if page < 5: return [ 'page %d, tweet A for term %s' % (page, term), 'page %d, tweet B for term %s' % (page, term), ] else: return None def process(tweet): print tweet From showell30 at yahoo.com Sun Nov 11 12:29:15 2012 From: showell30 at yahoo.com (Steve Howell) Date: Sun, 11 Nov 2012 09:29:15 -0800 (PST) Subject: A gnarly little python loop References: <7x4nkwzesu.fsf@ruckus.brouhaha.com> Message-ID: <8be50a3e-0ba6-439f-b445-7dedeacdc1c7@lg12g2000pbb.googlegroups.com> On Nov 11, 1:09?am, Paul Rubin wrote: > Cameron Simpson writes: > > | I'd prefer the original code ten times over this inaccessible beast. > > Me too. > > Me, I like the itertools version better. ?There's one chunk of data > that goes through a succession of transforms each of which > is very straightforward. Thanks, Paul. Even though I supplied the "inaccessible" itertools version, I can understand why folks find it inaccessible. As I said to the OP, there was nothing wrong with the original imperative approach; I was simply providing an alternative. It took me a while to appreciate itertools, but the metaphor that resonates with me is a Unix pipeline. It's just a metaphor, so folks shouldn't be too literal, but the idea here is this: page_nums -> pages -> valid_pages -> tweets The transforms are this: page_nums -> pages: call API via imap pages -> valid_pages: take while true valid_pages -> tweets: use chain.from_iterable to flatten results Here's the code again for context: def get_tweets(term): def get_page(page): return getSearch(term, page) page_nums = itertools.count(1) pages = itertools.imap(get_page, page_nums) valid_pages = itertools.takewhile(bool, pages) tweets = itertools.chain.from_iterable(valid_pages) return tweets From tinnews at isbd.co.uk Sun Nov 11 12:38:24 2012 From: tinnews at isbd.co.uk (tinnews at isbd.co.uk) Date: Sun, 11 Nov 2012 17:38:24 +0000 Subject: Question about email.message_from_string() and Message objects References: <9vr4n9-t2h.ln1@chris.zbmc.eu> Message-ID: tinnews at isbd.co.uk wrote: > I'm a little confused about the relationship between the Python > email.parser convenience function email.message_from_string() and the > mailbox.Message objects. > > If I want an mailbox.mboxMessage given the message as a stream of text > is the right way to do it as follows (or at least a reasonable way to > do it):- > > msg = email.message_from_string(messageAsAString) > > mbxMsg = mailbox.mboxMessage(msg) > > Or can I go direct to mbox by doing:- > > mbxMsg = mailbox.mboxMessage(messageAsAString) > Answered myself by just trying it, the simple, straightforward mbxMsg = mailbox.mboxMessage(messageAsAString) works perfectly. -- Chris Green From tinnews at isbd.co.uk Sun Nov 11 12:42:50 2012 From: tinnews at isbd.co.uk (tinnews at isbd.co.uk) Date: Sun, 11 Nov 2012 17:42:50 +0000 Subject: logging, can one get it to email messages over a certain level? Message-ID: I'm sure this must be possible but at the moment I can't see how to do it. I want to send an E-Mail when the logging module logs a message above a certain level (probably for ERROR and CRITICAL messages only). I.e. I want some sort of hook that will be called when these messages are logged (I can do the sendmail bit OK, I've got code that does that). -- Chris Green From showell30 at yahoo.com Sun Nov 11 12:45:50 2012 From: showell30 at yahoo.com (Steve Howell) Date: Sun, 11 Nov 2012 09:45:50 -0800 (PST) Subject: Is there a simpler way to modify all arguments in a function before using the arguments? References: <18134e77-9b02-4aec-afb0-794ed900d194@googlegroups.com> Message-ID: <49bda7cd-e218-46c4-a8c8-32b3b6f32735@pe9g2000pbc.googlegroups.com> On Nov 9, 4:48?pm, bruceg113... at gmail.com wrote: > Is there a simpler way to modify all arguments in a function before using the arguments? > > For example, can the below code, in the modify arguments section be made into a few statements? > > ? ? def someComputation (aa, bb, cc, dd, ee, ff, gg, hh): > ? ? ? ?# modify arguments > ? ? ? ?# ---------------------- > ? ? ? ? aa = aa.replace (?_? , ??) > ? ? ? ? bb= ?bb.replace (?_? , ??) > ? ? ? ? cc = cc.replace (?_? , ??) > ? ? ? ? dd = dd.replace (?_? , ??) > ? ? ? ? ee = ee.replace (?_? , ??) > ? ? ? ? ff = ff.replace (?_? , ??) > ? ? ? ? gg = gg.replace (?_? , ??) > ? ? ? ? hh = hh.replace (?_? , ??) > > ? ? ? ?# use the arguments > ? ? ? ?# ----------------- > ? ? ? ?# ? I would couch this problem in a little more specific terms than trying to make this "simpler." The word "simple" is a dangerous term, because it's so broad and subjective. By my mind, the code is already simple, but that's just my own two cents. The real problem with the code that it's a maintenance trap, because a careless developer could add the ii parameter and forget to clean the output. So the problem statement here might be more like "How do I make sure future developers don't forget to fix the underscores in future args?". That's still a controversial question, but at least it's a little more specific. The other problem with the current code is that all the boilerplate distracts from the real logic of the function. That's a valid concern, although it's likely that most maintainers of the code would simply page down past the boilerplate without too much complaint. From showell30 at yahoo.com Sun Nov 11 12:56:46 2012 From: showell30 at yahoo.com (Steve Howell) Date: Sun, 11 Nov 2012 09:56:46 -0800 (PST) Subject: logging, can one get it to email messages over a certain level? References: Message-ID: On Nov 11, 9:48?am, tinn... at isbd.co.uk wrote: > I'm sure this must be possible but at the moment I can't see how to do it. > > I want to send an E-Mail when the logging module logs a message above > a certain level (probably for ERROR and CRITICAL messages only). > > I.e. I want some sort of hook that will be called when these messages > are logged (I can do the sendmail bit OK, I've got code that does that). > > -- > Chris Green Scroll down to the Handlers section here: http://docs.python.org/2/howto/logging.html#logging-basic-tutorial I'm not sure this explains perfectly what you're gonna need to do, but I hope it gets you in the ballpark. From __peter__ at web.de Sun Nov 11 13:34:06 2012 From: __peter__ at web.de (Peter Otten) Date: Sun, 11 Nov 2012 19:34:06 +0100 Subject: A gnarly little python loop References: <7x4nkwzesu.fsf@ruckus.brouhaha.com> <8be50a3e-0ba6-439f-b445-7dedeacdc1c7@lg12g2000pbb.googlegroups.com> Message-ID: Steve Howell wrote: > On Nov 11, 1:09 am, Paul Rubin wrote: >> Cameron Simpson writes: >> > | I'd prefer the original code ten times over this inaccessible beast. >> > Me too. >> >> Me, I like the itertools version better. There's one chunk of data >> that goes through a succession of transforms each of which >> is very straightforward. > > Thanks, Paul. > > Even though I supplied the "inaccessible" itertools version, I can > understand why folks find it inaccessible. As I said to the OP, there > was nothing wrong with the original imperative approach; I was simply > providing an alternative. > > It took me a while to appreciate itertools, but the metaphor that > resonates with me is a Unix pipeline. It's just a metaphor, so folks > shouldn't be too literal, but the idea here is this: > > page_nums -> pages -> valid_pages -> tweets > > The transforms are this: > > page_nums -> pages: call API via imap > pages -> valid_pages: take while true > valid_pages -> tweets: use chain.from_iterable to flatten results > > Here's the code again for context: > > def get_tweets(term): > def get_page(page): > return getSearch(term, page) > page_nums = itertools.count(1) > pages = itertools.imap(get_page, page_nums) > valid_pages = itertools.takewhile(bool, pages) > tweets = itertools.chain.from_iterable(valid_pages) > return tweets > Actually you supplied the "accessible" itertools version. For reference, here's the inaccessible version: class api: """Twitter search API mock-up""" pages = [ ["a", "b", "c"], ["d", "e"], ] @staticmethod def GetSearch(term, page): assert term == "foo" assert page >= 1 if page > len(api.pages): return [] return api.pages[page-1] from collections import deque from functools import partial from itertools import chain, count, imap, takewhile def process(tweet): print tweet term = "foo" deque( imap( process, chain.from_iterable( takewhile(bool, imap(partial(api.GetSearch, term), count(1))))), maxlen=0) ;) From showell30 at yahoo.com Sun Nov 11 14:16:06 2012 From: showell30 at yahoo.com (Steve Howell) Date: Sun, 11 Nov 2012 11:16:06 -0800 (PST) Subject: A gnarly little python loop References: <7x4nkwzesu.fsf@ruckus.brouhaha.com> <8be50a3e-0ba6-439f-b445-7dedeacdc1c7@lg12g2000pbb.googlegroups.com> Message-ID: <3b0b8e3b-6f0a-4337-89ab-235e938952b2@y5g2000pbi.googlegroups.com> On Nov 11, 10:34?am, Peter Otten <__pete... at web.de> wrote: > Steve Howell wrote: > > On Nov 11, 1:09 am, Paul Rubin wrote: > >> Cameron Simpson writes: > >> > | I'd prefer the original code ten times over this inaccessible beast. > >> > Me too. > > >> Me, I like the itertools version better. ?There's one chunk of data > >> that goes through a succession of transforms each of which > >> is very straightforward. > > > Thanks, Paul. > > > Even though I supplied the "inaccessible" itertools version, I can > > understand why folks find it inaccessible. ?As I said to the OP, there > > was nothing wrong with the original imperative approach; I was simply > > providing an alternative. > > > It took me a while to appreciate itertools, but the metaphor that > > resonates with me is a Unix pipeline. ?It's just a metaphor, so folks > > shouldn't be too literal, but the idea here is this: > > > ? page_nums -> pages -> valid_pages -> tweets > > > The transforms are this: > > > ? page_nums -> pages: call API via imap > > ? pages -> valid_pages: take while true > > ? valid_pages -> tweets: use chain.from_iterable to flatten results > > > Here's the code again for context: > > > ? ? def get_tweets(term): > > ? ? ? ? def get_page(page): > > ? ? ? ? ? ? return getSearch(term, page) > > ? ? ? ? page_nums = itertools.count(1) > > ? ? ? ? pages = itertools.imap(get_page, page_nums) > > ? ? ? ? valid_pages = itertools.takewhile(bool, pages) > > ? ? ? ? tweets = itertools.chain.from_iterable(valid_pages) > > ? ? ? ? return tweets > > Actually you supplied the "accessible" itertools version. For reference, > here's the inaccessible version: > > class api: > ? ? """Twitter search API mock-up""" > ? ? pages = [ > ? ? ? ? ["a", "b", "c"], > ? ? ? ? ["d", "e"], > ? ? ? ? ] > ? ? @staticmethod > ? ? def GetSearch(term, page): > ? ? ? ? assert term == "foo" > ? ? ? ? assert page >= 1 > ? ? ? ? if page > len(api.pages): > ? ? ? ? ? ? return [] > ? ? ? ? return api.pages[page-1] > > from collections import deque > from functools import partial > from itertools import chain, count, imap, takewhile > > def process(tweet): > ? ? print tweet > > term = "foo" > > deque( > ? ? imap( > ? ? ? ? process, > ? ? ? ? chain.from_iterable( > ? ? ? ? ? ? takewhile(bool, imap(partial(api.GetSearch, term), count(1))))), > ? ? maxlen=0) > > ;) I know Peter's version is tongue in cheek, but I do think that it has a certain expressive power, and it highlights three mind-expanding Python modules. Here's a re-flattened take on Peter's version ("Flat is better than nested." -- PEP 20): term = "foo" search = partial(api.GetSearch, term) nums = count(1) paged_tweets = imap(search, nums) paged_tweets = takewhile(bool, paged_tweets) tweets = chain.from_iterable(paged_tweets) processed_tweets = imap(process, tweets) deque(processed_tweets, maxlen=0) The use of deque to exhaust an iterator is slightly overboard IMHO, but all the other lines of code can be fairly easily understood once you read the docs. partial: http://docs.python.org/2/library/functools.html count, imap, takewhile, chain.from_iterable: http://docs.python.org/2/library/itertools.html deque: http://docs.python.org/2/library/collections.html From roy at panix.com Sun Nov 11 14:23:46 2012 From: roy at panix.com (Roy Smith) Date: Sun, 11 Nov 2012 14:23:46 -0500 Subject: A gnarly little python loop References: <7x4nkwzesu.fsf@ruckus.brouhaha.com> <8be50a3e-0ba6-439f-b445-7dedeacdc1c7@lg12g2000pbb.googlegroups.com> Message-ID: In article , Peter Otten <__peter__ at web.de> wrote: > deque( > imap( > process, > chain.from_iterable( > takewhile(bool, imap(partial(api.GetSearch, term), count(1))))), > maxlen=0) > > ;) If I wanted STL, I would still be writing C++ :-) From jeandubois314 at gmail.com Sun Nov 11 14:30:31 2012 From: jeandubois314 at gmail.com (Jean Dubois) Date: Sun, 11 Nov 2012 11:30:31 -0800 (PST) Subject: problem with module PyVisa References: <2f065a8d-5cf6-455f-b559-d1b78d225fca@10g2000vbu.googlegroups.com> <2325633a-640c-42f6-b8e7-930e475b7217@c20g2000vbz.googlegroups.com> Message-ID: On 9 nov, 22:14, w... at mac.com wrote: > On Nov 9, 2012, at 3:43 PM, Jean Dubois wrote: > > > > > > > > > > > > > The error may be obvious but finding this file and how to install it > > is not unfortunately. > > It seems I have to install it from the National Instruments site but > > Debian Linux doesn't seem to be supported... > > and I doubt whether just copying this file will be sufficient to make > > PyVisa work. > > I wonder whether there might be another way to communicate via USB > > with a Keithley programmable power supply using Python. > > > best regards, > > Jean > > > -- > >http://mail.python.org/mailman/listinfo/python-list > > I've been using pyserial quite successfully to control a USB-to-serial converter. > > That is, controlling a couple of RS232 serial devices via the USB port through a KeySpan USB-to-Serial converter. > > Pyserial seems to make communication through the USB port quite transparent, at least on my OS-X system. > > -Bill Well, in fact I do have some working scripts using pyserial to control an older (and more expensive) Keithley sourcemeter in combination with a USB-to-serial converter. But the trouble started when buying a cheaper and newer Keithley model which does not have rs232 but only USB. I noticed they have put an extra layer above USB called USBTMC which complicates things further. I followed the instructions at http://www.home.agilent.com/upload/cmc_upload/All/usbtmc.htm?&cc=BE&lc=dut#3.Copyright%20Notice|outline and compiled and loaded the usbtmc-driver but I still can't communicate with the Keithley, hence I started looking for an alternative using PyVisa...and now I'm stuck jean From cantabile.03 at wanadoo.fr Sun Nov 11 17:24:14 2012 From: cantabile.03 at wanadoo.fr (Cantabile) Date: Sun, 11 Nov 2012 23:24:14 +0100 Subject: List comprehension for testing **params Message-ID: <50a0258f$0$21241$ba4acef3@reader.news.orange.fr> Hi, I'm writing a small mail library for my own use, and at the time I'm testing parameters like this: class Mail(object): def __init__(self, smtp, login, **params) blah blah required = ['Subject', 'From', 'To', 'msg'] for i in required: if not i in params.keys(): print "Error, \'%s\' argument is missing" %i exit(1) ... md = {'Subject' : 'Test', 'From' :'Me', 'To' :['You', 'Them'], 'msg' :my.txt"} m = Mail('smtp.myprovider.com', ["mylogin", "mypasswd"], **md) I'd like to do something like that instead of the 'for' loop in __init__: assert[key for key in required if key in params.keys()] but it doesn't work. It doesn't find anythin wrong if remove, say msg, from **md. I thought it should because I believed that this list comprehension would check that every keyword in required would have a match in params.keys. Could you explain why it doesn't work and do you have any idea of how it could work ? Thanks in advance :) Cheers, Cantabile From steve+comp.lang.python at pearwood.info Sun Nov 11 17:31:09 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 11 Nov 2012 22:31:09 GMT Subject: Method default argument whose type is the class not yet defined References: <509efbb6$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50a0272d$0$21756$c3e8da3$76491128@news.astraweb.com> On Sun, 11 Nov 2012 14:21:19 +0000, Oscar Benjamin wrote: > On 11 November 2012 02:47, Chris Angelico wrote: >> On Sun, Nov 11, 2012 at 1:43 PM, Ian Kelly >> wrote: >>> On Sat, Nov 10, 2012 at 7:13 PM, Chris Angelico >>> wrote: >>>> I would not assume that. The origin is a point, just like any other. >>>> With a Line class, you could deem a zero-length line to be like a >>>> zero-element list, but Point(0,0) is more like the tuple (0,0) which >>>> is definitely True. Don't conflate the set of all tuples of arbitrary length with points, which have fixed length. Points are not *sequences* -- in Python, we treat tuples as sequences first and records second, because that is a useful thing to do. (But it is not the only useful thing to do: if we treated them as records first and sequences second, we might want to say that a tuple t was falsey if all the fields in t were falsey.) In the case of a Point class, a Point is definitely not a sequence. That we put the x-coordinate first and the y-coordinate second is a mere convention, like writing left to right. The mathematical properties of points do not depend on the x-coordinate coming first. Since points should not be treated as sequences, the requirement that non-empty sequences be treated as truthy is irrelevant. >>> It's more like the number 0 than the tuple (0,0). >>> >>> 0 is the origin on a 1-dimensional number line. (0,0) is the origin on >>> a 2-dimensional number plane. >>> >>> In fact, it might be pointed out that Point(0, 0) is a generalization >>> of 0+0j, which is equal to 0. >> >> Ah, good point. In any case, though, it'd be an utterly inconsequential >> bug. > > You were right the first time, Chris. A point that happens to coincide > with the arbitrarily chosen origin is no more truthy or falsey than any > other. A vector of length 0 on the other hand is a very different beast. Nonsense. The length and direction of a vector is relative to the origin. If the origin is arbitrary, as you claim, then so is the length of the vector. Just because we can perform vector transformations on the plane to move the origin to some point other that (0,0) doesn't make (0,0) an "arbitrarily chosen origin". It is no more arbitrary than 0 as the origin of the real number line. And yes, we can perform 1D vector transformations on the real number line too. Here's a version of range that sets the origin to 42, not 0: def myrange(start, end=None, step=1): if end is None: start = 42 return range(start, end, step) Nevertheless, there really is something special about the point 0 on the real number line, the point (0,0) on the complex number plane, the point (0,0,0) in the 3D space, (0,0,0,0) in 4D space, etc. It is not just an arbitrary convention that we set the origin to 0. In other words: to the extent that your arguments that zero-vectors are special are correct, the same applies to zero-points, since vectors are defined as a magnitude and direction *from the origin*. To put it yet another way: The complex number a+bj is equivalent to the 2D point (a, b) which is equivalent to the 2D vector [a, b]. If (0, 0) shouldn't be considered falsey, neither should [0, 0]. > The significance of zero in real algebra is not that it is the origin > but rather that it is the additive and multiplicative zero: > > a + 0 = a for any real number a > a * 0 = 0 for any real number a I'm not sure what you mean by "additive and multiplicative zero", you appear to be conflating two different properties here. 0 is the additive *identity*, but 1 is the multiplicative identity: a + 0 = a a * 1 = a for any real number a. If the RHS must be zero, then there is a unique multiplicative zero, but no unique additive zero: a * 0 = 0 for any real number a a + -a = 0 for any real number a > The same is true for a vector v0, of length 0: > > v + v0 = v for any vector v > a * v0 = v0 for any scalar a Well that's a bogus analogy. Since you're talking about the domain of vectors, the relevant identify for the second line should be: v * v0 = v0 for any vector v except that doesn't work, since vector algebra doesn't define a vector multiplication operator.[1] It does define multiplication between a vector and a scalar, which represents a scale transformation. > There is however no meaningful sense in which points (as opposed to > vectors) can be added to each other or multiplied by anything, so there > is no zero point. I think that the great mathematician Carl Gauss would have something to say about that. Points in the plane are equivalent to complex numbers, and you can certainly add and multiply complex numbers. Adding two points is equivalent to a translation; multiplication of a scalar with a point is equivalent to a scale transformation. Multiplying two points is equivalent to complex multiplication, which is a scale + a rotation. Oh look, that's exactly the same geometric interpretation as for vectors. Hardly surprising, since vectors are the magnitude and direction of a line from the origin to a point. > The relationship between points and vectors is analogous to the > relationship between datetimes and timedeltas. Having Vector(0, 0) > evaluate to False is analogous to having timedelta(0) evaluate to False > and is entirely sensible. Having Point(0, 0) evaluate to False is > precisely the same conceptual folly that sees midnight evaluate as > False. If you are dealing with datetimes, then "midnight 2012-11-12" is not falsey. The only falsey datetime is the zero datetime. Since it would be awfully inconvenient to start counting times from the Big Bang, we pick an arbitrary zero point, the Epoch, which in Unix systems is midnight 1 January 1970, and according to the logic of Unix system administrators, that is so far in the distant past that it might as well be the Big Bang. (People with more demanding requirements don't use Unix or Windows timestamps for recording date times. E.g. astronomers use the Julian system, not to be confused with the Julian calendar.) The midnight problem only occurs when you deal with *times* on their own, not datetimes, in which case the relationship with timedeltas is not defined. How far apart is 1:00am and 2:00am? Well, it depends, doesn't it? It could be 1 hour, 25 hours, 49 hours, ... In any case, since times are modulo 24 hours, they aren't really relevant to what we are discussing. [1] There is no single meaningful definition of vector multiplication that works for all dimensions. In two dimensions, you can define the dot product of two vectors to give a scalar; in three dimensions you have a dot product and a vector product. Since vectors are equivalent to points, and points are equivalent to complex numbers, one could define a vector operation equivalent to complex multiplication. There is a natural geometric interpretation of this multiplication: it is a scaling + rotation. -- Steven From python.list at tim.thechases.com Sun Nov 11 17:49:50 2012 From: python.list at tim.thechases.com (Tim Chase) Date: Sun, 11 Nov 2012 16:49:50 -0600 Subject: List comprehension for testing **params In-Reply-To: <50a0258f$0$21241$ba4acef3@reader.news.orange.fr> References: <50a0258f$0$21241$ba4acef3@reader.news.orange.fr> Message-ID: <50A02B8E.60400@tim.thechases.com> > assert[key for key in required if key in params.keys()] ... > Could you explain why it doesn't work and do you have any idea of how it > could work ? Well, here, if any of the items are found, you get a list that is non-False'ish, so the assert passes. It sounds like you want all() (available as of 2.5) assert all(key in params for key in required) This is modulo any key normalization for "From" vs. "FROM" vs. "from", etc; but should be enough to pass your initial requirements. -tkc From ian.g.kelly at gmail.com Sun Nov 11 17:56:52 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sun, 11 Nov 2012 15:56:52 -0700 Subject: List comprehension for testing **params In-Reply-To: <50a0258f$0$21241$ba4acef3@reader.news.orange.fr> References: <50a0258f$0$21241$ba4acef3@reader.news.orange.fr> Message-ID: On Sun, Nov 11, 2012 at 3:24 PM, Cantabile wrote: > I'd like to do something like that instead of the 'for' loop in __init__: > > assert[key for key in required if key in params.keys()] A list evaluates as true if it is not empty. As long as at least one of the required parameters is present, the list will not be empty and will evaluate as true, and the assertion will pass. Try this instead: assert all(key in params.keys() for key in required) By the way, using assertions for input checking is generally not considered good practice. As soon as Python is invoked with the -O option, your input testing is tossed out the window. Assertions are good to use for internal logic testing, to avoid unexpected state. For input checking, use an explicit if test and exception: if any(key not in params.keys() for key in required): raise ValueError("Missing required keyword argument") Additionally, if the arguments are required, then they probably have no business being wrapped up in **params in the first place. Why not just define your method like: def __init__(self, smtp, login, subject, from, to, msg): # ... And then Python will do all the work of requiring them to be present for you. From steve+comp.lang.python at pearwood.info Sun Nov 11 18:18:12 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 11 Nov 2012 23:18:12 GMT Subject: List comprehension for testing **params References: <50a0258f$0$21241$ba4acef3@reader.news.orange.fr> Message-ID: <50a03233$0$21756$c3e8da3$76491128@news.astraweb.com> On Sun, 11 Nov 2012 23:24:14 +0100, Cantabile wrote: > Hi, > I'm writing a small mail library for my own use, and at the time I'm > testing parameters like this: > > class Mail(object): > def __init__(self, smtp, login, **params) > blah > blah > required = ['Subject', 'From', 'To', 'msg'] for i in required: > if not i in params.keys(): > print "Error, \'%s\' argument is missing" %i exit(1) > ... Eeek! Don't do that. Seriously dude, that is Evil. Why should a simple missing argument to a class *kill the entire application*???? How horrible would it be if you called "chr()" and instead of raising a TypeError, Python shut down? Don't do that. Raise a proper exception. Then you have the choice to either catch the exception and continue, or let it propagate as appropriate. But you shouldn't even be manually checking for required arguments. Just use real arguments: def __init__(self, smpt, login, Subject, From, To, msg, **params): blah See how easy it is? Problem solved. If you don't provide an argument, Python gives you an exception for free. Best of all, you can use inspection to see what arguments are required, and help(Mail) shows you what arguments are needed. You can still use **md as below, and Python will automatically do everything you're manually trying to do (and failing). Don't fight the language, even when you win, you lose. > md = {'Subject' : 'Test', 'From' :'Me', 'To' :['You', 'Them'], 'msg' > :my.txt"} > > m = Mail('smtp.myprovider.com', ["mylogin", "mypasswd"], **md) Conventionally, a tuple ("mylogin", "mypasswd") would express the intent better than a list. > I'd like to do something like that instead of the 'for' loop in > __init__: > > assert[key for key in required if key in params.keys()] > > but it doesn't work. It doesn't find anythin wrong if remove, say msg, > from **md. I thought it should because I believed that this list > comprehension would check that every keyword in required would have a > match in params.keys. No, you have three problems there. The first problem is that only the empty list is falsey: assert [] # this raises an exception assert ["Subject", "From"] # this doesn't, even though "To" and "msg" are missing. You could say: assert all([key in params for key in required]) but that leaves you with the next two problems: 2) Fixing the assert still leaves you with the wrong exception. You wouldn't raise a ZeroDivisionError, or a UnicodeDecodeError, or an IOError would you? No of course not. So why are you suggesting raising an AssertionError? That is completely inappropriate, the right exception to use is TypeError. Don't be lazy and treat assert as a quick and easy way to get exceptions for free. 3) Asserts are not guaranteed to run. Never, never, never use assert to test function arguments supplied by the caller, because you have no control over whether or not the asserts will even be called. The whole point of assertions is that the caller can disable them for speed. Asserts should be used for testing your code's internal logic, not user-provided arguments. Or for testing things which "can't possibly fail". But of course, since what can't go wrong does, you sometimes still want to test things. If you've ever written something like: if condition: process() else: # this cannot happen, but just in case! print "something unexpected went wrong!" that's a perfect candidate for an assertion: assert condition, "something unexpected went wrong!" process() -- Steven From tjreedy at udel.edu Sun Nov 11 18:37:05 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 11 Nov 2012 18:37:05 -0500 Subject: List comprehension for testing **params In-Reply-To: References: <50a0258f$0$21241$ba4acef3@reader.news.orange.fr> Message-ID: On 11/11/2012 5:56 PM, Ian Kelly wrote: > On Sun, Nov 11, 2012 at 3:24 PM, Cantabile wrote: >> I'd like to do something like that instead of the 'for' loop in __init__: >> >> assert[key for key in required if key in params.keys()] > > A list evaluates as true if it is not empty. As long as at least one > of the required parameters is present, the list will not be empty and > will evaluate as true, and the assertion will pass. Try this instead: > > assert all(key in params.keys() for key in required) > > By the way, using assertions for input checking is generally not > considered good practice. As soon as Python is invoked with the -O > option, your input testing is tossed out the window. Assertions are > good to use for internal logic testing, to avoid unexpected state. > For input checking, use an explicit if test and exception: > > if any(key not in params.keys() for key in required): > raise ValueError("Missing required keyword argument") > > Additionally, if the arguments are required, then they probably have > no business being wrapped up in **params in the first place. Why not > just define your method like: > > def __init__(self, smtp, login, subject, from, to, msg): or if you want them to be identified by keyword only (since 7 positional args is a bit much) def __init__(self, smtp, login, *, subject, from, to, msg): (I forget when this feature was added) > # ... > > And then Python will do all the work of requiring them to be present for you. > -- Terry Jan Reedy From steve+comp.lang.python at pearwood.info Sun Nov 11 18:41:46 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 11 Nov 2012 23:41:46 GMT Subject: List comprehension for testing **params References: <50a0258f$0$21241$ba4acef3@reader.news.orange.fr> Message-ID: <50a037ba$0$21756$c3e8da3$76491128@news.astraweb.com> On Sun, 11 Nov 2012 18:37:05 -0500, Terry Reedy wrote: > or if you want them to be identified by keyword only (since 7 positional > args is a bit much) > > def __init__(self, smtp, login, *, subject, from, to, msg): > > (I forget when this feature was added) It's a Python 3 feature. -- Steven From cantabile.03 at wanadoo.fr Sun Nov 11 18:53:57 2012 From: cantabile.03 at wanadoo.fr (Cantabile) Date: Mon, 12 Nov 2012 00:53:57 +0100 Subject: List comprehension for testing **params In-Reply-To: <50a0258f$0$21241$ba4acef3@reader.news.orange.fr> References: <50a0258f$0$21241$ba4acef3@reader.news.orange.fr> Message-ID: <50a03a95$0$18073$ba4acef3@reader.news.orange.fr> Thanks everyone for your answers. That's much clearer now. I see that I was somehow fighting python instead of using it. Lesson learned (for the time being at least) :) I'll probably get back with more questions... Cheers, Cantabile From ejsaiet at alaska.edu Sun Nov 11 19:05:52 2012 From: ejsaiet at alaska.edu (ejsaiet at alaska.edu) Date: Sun, 11 Nov 2012 16:05:52 -0800 (PST) Subject: writing a csv file Message-ID: <4467d3df-7d0c-4566-8fed-ba7fb8b54afe@googlegroups.com> Hello, I have the script below, which it extracts NOAA data from HTML and is planed writes it to a CSV file. Here is the script: import urllib2 from bs4 import BeautifulSoup from time import localtime, strftime import csv #This script is intended to retrive NOAA data and apend it to a csv file. # Wait 45 min #Need to work on this part... # Go into URL page = urllib2.urlopen("http://w1.weather.gov/obhistory/PAFA.html") soup = BeautifulSoup(page) datemonth=strftime("%m", localtime()) dateday=strftime("%d", localtime()) with open("/home/eyalak/Documents/weather/weather.csv", "wb") as f: writer = csv.writer(f) table = soup.findAll("table")[3] #print table for tr in table.findAll("tr", valign="top"): a={x.string for x in tr.findAll('td')} print str(a) writer.writerows([a]) It did not work unless I changed the line a={x.string for x in tr.findAll('td')} to a=list({x.string for x in tr.findAll('td')}) But that disorganizes the data. How can I write the data to a csv file without altering the order prior to the list function. Thanks E From python.list at tim.thechases.com Sun Nov 11 19:21:32 2012 From: python.list at tim.thechases.com (Tim Chase) Date: Sun, 11 Nov 2012 18:21:32 -0600 Subject: List comprehension for testing **params In-Reply-To: <50a03233$0$21756$c3e8da3$76491128@news.astraweb.com> References: <50a0258f$0$21241$ba4acef3@reader.news.orange.fr> <50a03233$0$21756$c3e8da3$76491128@news.astraweb.com> Message-ID: <50A0410C.30701@tim.thechases.com> On 11/11/12 17:18, Steven D'Aprano wrote: > but that leaves you with the next two problems: > > 2) Fixing the assert still leaves you with the wrong exception. You > wouldn't raise a ZeroDivisionError, or a UnicodeDecodeError, or an IOError > would you? No of course not. So why are you suggesting raising an > AssertionError? That is completely inappropriate, the right exception to > use is TypeError. Don't be lazy and treat assert as a quick and easy way > to get exceptions for free. I'd say that it depends on whether the dictionary/kwargs gets populated from user-supplied data (whether mail, a file, direct input, etc), or whether it's coder-supplied. I like to think of "assert" as a "hey, you bone-headed programmer, you did something that violated conditions for using this code properly!" and as such, entirely appropriate to use in the coder-supplied case. This rolls into... > 3) Asserts are not guaranteed to run. Never, never, never use assert to > test function arguments supplied by the caller, because you have no > control over whether or not the asserts will even be called. where, once the programmer is satisfied that the code runs, meeting all the appropriate tests/assertions, the assertions can then be turned off (if one really cares that much; can't say I've done it more than a couple times, mostly out of experimentation/testing to see if it made any grave difference in performance). I think you intend this in your > Or for testing things which "can't possibly fail". But of > course, since what can't go wrong does, you sometimes still want > to test things. -tkc From breamoreboy at yahoo.co.uk Sun Nov 11 19:27:52 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 12 Nov 2012 00:27:52 +0000 Subject: writing a csv file In-Reply-To: <4467d3df-7d0c-4566-8fed-ba7fb8b54afe@googlegroups.com> References: <4467d3df-7d0c-4566-8fed-ba7fb8b54afe@googlegroups.com> Message-ID: On 12/11/2012 00:05, ejsaiet at alaska.edu wrote: > > with open("/home/eyalak/Documents/weather/weather.csv", "wb") as f: > writer = csv.writer(f) > table = soup.findAll("table")[3] > #print table > for tr in table.findAll("tr", valign="top"): > a={x.string for x in tr.findAll('td')} > print str(a) > writer.writerows([a]) > > It did not work unless I changed the line a={x.string for x in tr.findAll('td')} to a=list({x.string for x in tr.findAll('td')}) > > But that disorganizes the data. How can I write the data to a csv file without altering the order prior to the list function. > Thanks > E > Change the line print str(a) to print type(a), a You'll see what the problem is and be able to fix it. -- Cheers. Mark Lawrence. From oscar.j.benjamin at gmail.com Sun Nov 11 19:31:53 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 12 Nov 2012 00:31:53 +0000 Subject: Method default argument whose type is the class not yet defined In-Reply-To: <50a0272d$0$21756$c3e8da3$76491128@news.astraweb.com> References: <509efbb6$0$29980$c3e8da3$5496439d@news.astraweb.com> <50a0272d$0$21756$c3e8da3$76491128@news.astraweb.com> Message-ID: On 11 November 2012 22:31, Steven D'Aprano wrote: > On Sun, 11 Nov 2012 14:21:19 +0000, Oscar Benjamin wrote: > >> On 11 November 2012 02:47, Chris Angelico wrote: >>> On Sun, Nov 11, 2012 at 1:43 PM, Ian Kelly >>> wrote: >>>> On Sat, Nov 10, 2012 at 7:13 PM, Chris Angelico >>>> wrote: >>>>> I would not assume that. The origin is a point, just like any other. >>>>> With a Line class, you could deem a zero-length line to be like a >>>>> zero-element list, but Point(0,0) is more like the tuple (0,0) which >>>>> is definitely True. >>>> It's more like the number 0 than the tuple (0,0). >>>> >>>> 0 is the origin on a 1-dimensional number line. (0,0) is the origin on >>>> a 2-dimensional number plane. >>>> >>>> In fact, it might be pointed out that Point(0, 0) is a generalization >>>> of 0+0j, which is equal to 0. >>> >>> Ah, good point. In any case, though, it'd be an utterly inconsequential >>> bug. >> >> You were right the first time, Chris. A point that happens to coincide >> with the arbitrarily chosen origin is no more truthy or falsey than any >> other. A vector of length 0 on the other hand is a very different beast. > > Nonsense. The length and direction of a vector is relative to the origin. > If the origin is arbitrary, as you claim, then so is the length of the > vector. Wrong on all counts. Neither the length not the direction os a vector are relative to any origin. When we choose to express a vector in Cartesian components our representation assumes an orientation for the axes of the coordinate system. Even in this sense, though, the origin itself does not affect the components of the vector. I have spent a fair few hours in the past few weeks persuading teenaged Engineering students to maintain a clear distinction between points, vectors and lines. One of the ways that I distinguish vectors from points is to say that a vector is like an arrow but its base has no particular position. A point on the other hand is quite simply a position. Given an origin (an arbitrarily chosen point) we can specify another point using a "position vector": a vector from the origin to the point in question. > Just because we can perform vector transformations on the plane to move > the origin to some point other that (0,0) doesn't make (0,0) an > "arbitrarily chosen origin". It is no more arbitrary than 0 as the origin > of the real number line. (0, 0) are the coordinates of the origin *relative to itself*. Had we chosen a different origin, the point that was previously called (0, 0) would now be called (a, b) for some other numbers a and b. > And yes, we can perform 1D vector transformations on the real number line > too. Here's a version of range that sets the origin to 42, not 0: > > def myrange(start, end=None, step=1): > if end is None: > start = 42 > return range(start, end, step) This is lost on me... > Nevertheless, there really is something special about the point 0 on the > real number line Agreed. > , the point (0,0) on the complex number plane, Also agreed. > the point > (0,0,0) in the 3D space, (0,0,0,0) in 4D space, etc. It is not just an > arbitrary convention that we set the origin to 0. Wrong. The point (0,0,0,...) in some ND space is an arbitrarily chosen position. By this I don't mean to say that the sequence of coordinates consisting of all zeros is arbitrary. The choice of the point *in the real/hypothetical space* that is designated by the sequence of zero coordinates is arbitrary. > In other words: to the extent that your arguments that zero-vectors are > special are correct, the same applies to zero-points, since vectors are > defined as a magnitude and direction *from the origin*. Plain wrong. Vectors are not defined *from any origin*. > To put it yet another way: > > The complex number a+bj is equivalent to the 2D point (a, b) which is > equivalent to the 2D vector [a, b]. If (0, 0) shouldn't be considered > falsey, neither should [0, 0]. a+bj is not equivalent to the 2D point (a, b). It is possible to define a mapping between complex numbers and a 2D space so that a+bj corresponds to the point (a, b) *under that map*. However there are an infinite number of such possible mappings between the two spaces including a+bj -> (a+1, b+1). >> The significance of zero in real algebra is not that it is the origin >> but rather that it is the additive and multiplicative zero: >> >> a + 0 = a for any real number a >> a * 0 = 0 for any real number a > > I'm not sure what you mean by "additive and multiplicative zero", you > appear to be conflating two different properties here. 0 is the additive > *identity*, but 1 is the multiplicative identity: I mean that it has the properties that zero has when used in addition and multiplication: http://en.wikipedia.org/wiki/0_%28number%29#Elementary_algebra > > a + 0 = a > a * 1 = a > for any real number a. No. I meant the two properties that I listed. > > If the RHS must be zero, then there is a unique multiplicative zero, but > no unique additive zero: > > a * 0 = 0 for any real number a > a + -a = 0 for any real number a That is not the same as: a + 0 = a >> The same is true for a vector v0, of length 0: >> >> v + v0 = v for any vector v >> a * v0 = v0 for any scalar a > > Well that's a bogus analogy. Since you're talking about the domain of > vectors, the relevant identify for the second line should be: > > v * v0 = v0 for any vector v > > except that doesn't work, since vector algebra doesn't define a vector > multiplication operator.[1] It does define multiplication between a > vector and a scalar, which represents a scale transformation. That is precisely the multiplication operation that I was referring to. There are other senses of vector multiplication between vectors for which v0 will also behave as a "zero" under multiplication: v . v0 = 0 for any vector v v x v0 = 0 for any vector v >> There is however no meaningful sense in which points (as opposed to >> vectors) can be added to each other or multiplied by anything, so there >> is no zero point. > > I think that the great mathematician Carl Gauss would have something to > say about that. Is the text below a quote? > Points in the plane are equivalent to complex numbers, and you can > certainly add and multiply complex numbers. Adding two points is > equivalent to a translation; multiplication of a scalar with a point is > equivalent to a scale transformation. Multiplying two points is > equivalent to complex multiplication, which is a scale + a rotation. The last point is bizarre. Complex multiplication makes no sense when you're trying to think about vectors. Draw a 2D plot and convince yourself that the square of the point (0, 1) is (-1, 0). > Oh look, that's exactly the same geometric interpretation as for vectors. > Hardly surprising, since vectors are the magnitude and direction of a > line from the origin to a point. Here it becomes clear that you have conflated "position vectors" with vectors in general. Let me list some other examples of vectors that are clearly not "from the origin to a point": velocity acceleration force electric field angular momentum wave vector (I could go on) >> The relationship between points and vectors is analogous to the >> relationship between datetimes and timedeltas. Having Vector(0, 0) >> evaluate to False is analogous to having timedelta(0) evaluate to False >> and is entirely sensible. Having Point(0, 0) evaluate to False is >> precisely the same conceptual folly that sees midnight evaluate as >> False. > > If you are dealing with datetimes, then "midnight 2012-11-12" is not > falsey. The only falsey datetime is the zero datetime. Since it would be > awfully inconvenient to start counting times from the Big Bang, we pick > an arbitrary zero point, the Epoch, which in Unix systems is midnight 1 > January 1970, and according to the logic of Unix system administrators, > that is so far in the distant past that it might as well be the Big Bang. > > (People with more demanding requirements don't use Unix or Windows > timestamps for recording date times. E.g. astronomers use the Julian > system, not to be confused with the Julian calendar.) > > The midnight problem only occurs when you deal with *times* on their own, > not datetimes, in which case the relationship with timedeltas is not > defined. How far apart is 1:00am and 2:00am? Well, it depends, doesn't > it? It could be 1 hour, 25 hours, 49 hours, ... > > In any case, since times are modulo 24 hours, they aren't really relevant > to what we are discussing. They are relevant. The point is that conflating points and vectors is the same as conflating datetime and timedelta objects. The zero of datetime.timedelta objects is not arbitrary but the zero of datetime.time objects is. > Since vectors are equivalent to points, and points are equivalent to > complex numbers, one could define a vector operation equivalent to > complex multiplication. There is a natural geometric interpretation of > this multiplication: it is a scaling + rotation. Vectors, points and complex numbers are not equivalent. There are cases in which it is reasonable to think of them as equivalent for a particular purpose. That does not diminish the fundamental differences between them. Oscar From showell30 at yahoo.com Sun Nov 11 19:32:46 2012 From: showell30 at yahoo.com (Steve Howell) Date: Sun, 11 Nov 2012 16:32:46 -0800 (PST) Subject: Method default argument whose type is the class not yet defined References: Message-ID: <5eac62e4-9fe9-4185-be4f-1035d42cd165@me7g2000pbb.googlegroups.com> On Nov 10, 11:33?am, Jennie wrote: > What is the best solution to solve the following problem in Python 3.3? > > import math > ?>>> class Point: > ... ? ? def __init__(self, x=0, y=0): > ... ? ? ? ? self.x = x > ... ? ? ? ? self.y = y > ... ? ? def __sub__(self, other): > ... ? ? ? ? return Point(self.x - other.x, self.y - other.y) > ... ? ? def distance(self, point=Point()): > ... ? ? ? ? """Return the distance from `point`.""" > ... ? ? ? ? return math.sqrt((self - point).x ** 2 + (self - point).y ** 2) Before you do anything else, introduce a Vector class into your app. The difference between two Points is not a Point; it's a Vector. Create a magnitude() method in your Vector class, then make your Point.distance return the results of Vector.magnitude(self - other). To define the distance of a point from the origin, don't make your distance() method have default arguments; instead, define another method called distance_from_origin(). From cs at zip.com.au Sun Nov 11 19:43:56 2012 From: cs at zip.com.au (Cameron Simpson) Date: Mon, 12 Nov 2012 11:43:56 +1100 Subject: A gnarly little python loop In-Reply-To: <3b0b8e3b-6f0a-4337-89ab-235e938952b2@y5g2000pbi.googlegroups.com> References: <3b0b8e3b-6f0a-4337-89ab-235e938952b2@y5g2000pbi.googlegroups.com> Message-ID: <20121112004355.GA18626@cskk.homeip.net> On 11Nov2012 11:16, Steve Howell wrote: | On Nov 11, 10:34?am, Peter Otten <__pete... at web.de> wrote: | > Steve Howell wrote: | > > On Nov 11, 1:09 am, Paul Rubin wrote: | > >> Cameron Simpson writes: | > >> > | I'd prefer the original code ten times over this inaccessible beast. | > >> > Me too. | > | > >> Me, I like the itertools version better. ?There's one chunk of data | > >> that goes through a succession of transforms each of which | > >> is very straightforward. | > | > > Thanks, Paul. | > | > > Even though I supplied the "inaccessible" itertools version, I can | > > understand why folks find it inaccessible. ?As I said to the OP, there | > > was nothing wrong with the original imperative approach; I was simply | > > providing an alternative. | > | > > It took me a while to appreciate itertools, but the metaphor that | > > resonates with me is a Unix pipeline. [...] | > Actually you supplied the "accessible" itertools version. For reference, | > here's the inaccessible version: [...] | I know Peter's version is tongue in cheek, but I do think that it has | a certain expressive power, and it highlights three mind-expanding | Python modules. | Here's a re-flattened take on Peter's version ("Flat is better than | nested." -- PEP 20): [...] Ok, who's going to quiz the OP on his/her uptake of these techniques... -- Cameron Simpson It's hard to make a man understand something when his livelihood depends on him not understanding it. - Upton Sinclair From showell30 at yahoo.com Sun Nov 11 19:56:48 2012 From: showell30 at yahoo.com (Steve Howell) Date: Sun, 11 Nov 2012 16:56:48 -0800 (PST) Subject: Method default argument whose type is the class not yet defined References: <509efbb6$0$29980$c3e8da3$5496439d@news.astraweb.com> <50a0272d$0$21756$c3e8da3$76491128@news.astraweb.com> Message-ID: On Nov 11, 4:31?pm, Oscar Benjamin wrote: > On 11 November 2012 22:31, Steven D'Aprano > > Nonsense. The length and direction of a vector is relative to the origin. > > If the origin is arbitrary, as you claim, then so is the length of the > > vector. > > Wrong on all counts. Neither the length not the direction os a vector > are relative to any origin. When we choose to express a vector in > Cartesian components our representation assumes an orientation for the > axes of the coordinate system. Even in this sense, though, the origin > itself does not affect the components of the vector. > Thank you for pushing back on Steven's imprecise statement that the "direction of a vector is relative to the origin." You can't find an angle between two points. That's absurd. You need axes for context. > Vectors, points and complex numbers are not equivalent. There are > cases in which it is reasonable to think of them as equivalent for a > particular purpose. That does not diminish the fundamental differences > between them. > I looked to wikipedia for clarity, but the definition of a Euclidean vector is somewhat muddy: http://en.wikipedia.org/wiki/Euclidean_vector They say that the formal definition of a vector is a directed line segment. But then they define a "free vector" as an entity where only the magnitude and direction matter, not the initial point. As you say, it's not unreasonable to treat vectors, points, and complex numbers as equivalent in many circumstances. But, if you're gonna be pedantic, they really are different things. From breamoreboy at yahoo.co.uk Sun Nov 11 20:10:27 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 12 Nov 2012 01:10:27 +0000 Subject: Method default argument whose type is the class not yet defined In-Reply-To: References: <509efbb6$0$29980$c3e8da3$5496439d@news.astraweb.com> <50a0272d$0$21756$c3e8da3$76491128@news.astraweb.com> Message-ID: On 12/11/2012 00:31, Oscar Benjamin wrote: > > Plain wrong. Vectors are not defined *from any origin*. > So when the Captain says "full speed ahead, steer 245 degrees", you haven't the faintest idea where you're going, because you have no origin? -- Cheers. Mark Lawrence. From roy at panix.com Sun Nov 11 20:15:19 2012 From: roy at panix.com (Roy Smith) Date: Sun, 11 Nov 2012 20:15:19 -0500 Subject: Method default argument whose type is the class not yet defined References: <509efbb6$0$29980$c3e8da3$5496439d@news.astraweb.com> <50a0272d$0$21756$c3e8da3$76491128@news.astraweb.com> Message-ID: In article , Mark Lawrence wrote: > On 12/11/2012 00:31, Oscar Benjamin wrote: > > > > Plain wrong. Vectors are not defined *from any origin*. > > > > So when the Captain says "full speed ahead, steer 245 degrees", you > haven't the faintest idea where you're going, because you have no origin? Vectors have a length ("full speed ahead") and a direction ("245 degrees"). What they don't have is a fixed location in space. The captain didn't say, "Full speed ahead, steer 245 degrees, from 45.0N, 20.0W". In other words, you are correct. The order, "full speed ahead, steer 245 degrees", doesn't give you the faintest idea of where you're going. If you were the helmsman, after you executed that order, without any additional information (such as your current location), you would have no idea what piece of land you will hit, or when you will hit it, if you maintain your current course and speed. From oscar.j.benjamin at gmail.com Sun Nov 11 20:18:16 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 12 Nov 2012 01:18:16 +0000 Subject: Method default argument whose type is the class not yet defined In-Reply-To: References: <509efbb6$0$29980$c3e8da3$5496439d@news.astraweb.com> <50a0272d$0$21756$c3e8da3$76491128@news.astraweb.com> Message-ID: On 12 November 2012 01:10, Mark Lawrence wrote: > On 12/11/2012 00:31, Oscar Benjamin wrote: >> >> >> Plain wrong. Vectors are not defined *from any origin*. >> > > So when the Captain says "full speed ahead, steer 245 degrees", you haven't > the faintest idea where you're going, because you have no origin? As Steve has just explained, the origin has nothing to do with the orientation of the coordinate system. But then I'm assuming you meant that 245 degrees was a bearing relative to North. Was it supposed to be relative to my current angle? Truthfully I wouldn't know what to do without asking the captain a couple more questions. Oscar From breamoreboy at yahoo.co.uk Sun Nov 11 20:29:45 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 12 Nov 2012 01:29:45 +0000 Subject: Method default argument whose type is the class not yet defined In-Reply-To: References: <509efbb6$0$29980$c3e8da3$5496439d@news.astraweb.com> <50a0272d$0$21756$c3e8da3$76491128@news.astraweb.com> Message-ID: On 12/11/2012 01:18, Oscar Benjamin wrote: > On 12 November 2012 01:10, Mark Lawrence wrote: >> On 12/11/2012 00:31, Oscar Benjamin wrote: >>> >>> >>> Plain wrong. Vectors are not defined *from any origin*. >>> >> >> So when the Captain says "full speed ahead, steer 245 degrees", you haven't >> the faintest idea where you're going, because you have no origin? > > As Steve has just explained, the origin has nothing to do with the > orientation of the coordinate system. > > But then I'm assuming you meant that 245 degrees was a bearing > relative to North. Was it supposed to be relative to my current angle? > Truthfully I wouldn't know what to do without asking the captain a > couple more questions. > > > Oscar > The only good acedemic is a dead acedemic? -- Cheers. Mark Lawrence. From roy at panix.com Sun Nov 11 20:34:08 2012 From: roy at panix.com (Roy Smith) Date: Sun, 11 Nov 2012 20:34:08 -0500 Subject: Method default argument whose type is the class not yet defined References: <509efbb6$0$29980$c3e8da3$5496439d@news.astraweb.com> <50a0272d$0$21756$c3e8da3$76491128@news.astraweb.com> Message-ID: In article , Oscar Benjamin wrote: > But then I'm assuming you meant that 245 degrees was a bearing > relative to North. Was it supposed to be relative to my current angle? > Truthfully I wouldn't know what to do without asking the captain a > couple more questions. Granted, this requires some domain-specific knowledge, but an order to "steer 245 degrees" means relative to north (and, technically, it's a heading, not a bearing, but that's another discussion). If the captain wanted you to change you heading relative to your current heading, he would say something like, "turn left 10 degrees" (that may not be strictly the correct wording). From breamoreboy at yahoo.co.uk Sun Nov 11 20:35:04 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 12 Nov 2012 01:35:04 +0000 Subject: Method default argument whose type is the class not yet defined In-Reply-To: References: <509efbb6$0$29980$c3e8da3$5496439d@news.astraweb.com> <50a0272d$0$21756$c3e8da3$76491128@news.astraweb.com> Message-ID: On 12/11/2012 01:15, Roy Smith wrote: > In article , > Mark Lawrence wrote: > >> On 12/11/2012 00:31, Oscar Benjamin wrote: >>> >>> Plain wrong. Vectors are not defined *from any origin*. >>> >> >> So when the Captain says "full speed ahead, steer 245 degrees", you >> haven't the faintest idea where you're going, because you have no origin? > > Vectors have a length ("full speed ahead") and a direction ("245 > degrees"). What they don't have is a fixed location in space. The > captain didn't say, "Full speed ahead, steer 245 degrees, from 45.0N, > 20.0W". > > In other words, you are correct. The order, "full speed ahead, steer > 245 degrees", doesn't give you the faintest idea of where you're going. > If you were the helmsman, after you executed that order, without any > additional information (such as your current location), you would have > no idea what piece of land you will hit, or when you will hit it, if you > maintain your current course and speed. > Thank you for your explanation. -- Cheers. Mark Lawrence. From showell30 at yahoo.com Sun Nov 11 20:38:23 2012 From: showell30 at yahoo.com (Steve Howell) Date: Sun, 11 Nov 2012 17:38:23 -0800 (PST) Subject: A gnarly little python loop References: <3b0b8e3b-6f0a-4337-89ab-235e938952b2@y5g2000pbi.googlegroups.com> Message-ID: On Nov 11, 4:44?pm, Cameron Simpson wrote: > On 11Nov2012 11:16, Steve Howell wrote: > | On Nov 11, 10:34?am, Peter Otten <__pete... at web.de> wrote: > | > Steve Howell wrote: > | > > On Nov 11, 1:09 am, Paul Rubin wrote: > | > >> Cameron Simpson writes: > | > >> > | I'd prefer the original code ten times over this inaccessible beast. > | > >> > Me too. > | > > | > >> Me, I like the itertools version better. ?There's one chunk of data > | > >> that goes through a succession of transforms each of which > | > >> is very straightforward. > | > > | > > Thanks, Paul. > | > > | > > Even though I supplied the "inaccessible" itertools version, I can > | > > understand why folks find it inaccessible. ?As I said to the OP, there > | > > was nothing wrong with the original imperative approach; I was simply > | > > providing an alternative. > | > > | > > It took me a while to appreciate itertools, but the metaphor that > | > > resonates with me is a Unix pipeline. > [...] > | > Actually you supplied the "accessible" itertools version. For reference, > | > here's the inaccessible version: > [...] > | I know Peter's version is tongue in cheek, but I do think that it has > | a certain expressive power, and it highlights three mind-expanding > | Python modules. > | Here's a re-flattened take on Peter's version ("Flat is better than > | nested." -- PEP 20): > [...] > > Ok, who's going to quiz the OP on his/her uptake of these techniques... Cameron, with all due respect, I think you're missing the point. Roy posted this code: page = 1 while 1: r = api.GetSearch(term="foo", page=page) if not r: break for tweet in r: process(tweet) page += 1 In his own words, he described the loop as "gnarly" and the overall code as "fidgety." One way to eliminate the "while", the "if", and the "break" statements is to use higher level constructs that are shipped with all modern versions of Python, and which are well documented and well tested (and fast, I might add): search = partial(api.GetSearch, "foo") paged_tweets = imap(search, count(1)) paged_tweets = takewhile(bool, paged_tweets) tweets = chain.from_iterable(paged_tweets) for tweet in tweets: process(tweet) The moral of the story is that you can avoid brittle loops by relying on a well-tested library to work at a higher level of abstraction. For this particular use case, the imperative version is fine, but for more complex use cases, the loops are only gonna get more gnarly and fidgety. From oscar.j.benjamin at gmail.com Sun Nov 11 20:50:55 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 12 Nov 2012 01:50:55 +0000 Subject: Method default argument whose type is the class not yet defined In-Reply-To: References: <509efbb6$0$29980$c3e8da3$5496439d@news.astraweb.com> <50a0272d$0$21756$c3e8da3$76491128@news.astraweb.com> Message-ID: On 12 November 2012 01:29, Mark Lawrence wrote: > On 12/11/2012 01:18, Oscar Benjamin wrote: >> >> On 12 November 2012 01:10, Mark Lawrence wrote: >>> >>> On 12/11/2012 00:31, Oscar Benjamin wrote: >>>> >>>> Plain wrong. Vectors are not defined *from any origin*. >>> >>> So when the Captain says "full speed ahead, steer 245 degrees", you >>> haven't >>> the faintest idea where you're going, because you have no origin? >> >> >> As Steve has just explained, the origin has nothing to do with the >> orientation of the coordinate system. >> >> But then I'm assuming you meant that 245 degrees was a bearing >> relative to North. Was it supposed to be relative to my current angle? >> Truthfully I wouldn't know what to do without asking the captain a >> couple more questions. > > The only good acedemic is a dead acedemic? Is that what happens when people ask questions on your ship: "it's the plank for him with the questions-askin'!" I'm glad you're not my captain. Oscar From steve+comp.lang.python at pearwood.info Sun Nov 11 23:46:27 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 12 Nov 2012 04:46:27 GMT Subject: Method default argument whose type is the class not yet defined References: <509efbb6$0$29980$c3e8da3$5496439d@news.astraweb.com> <50a0272d$0$21756$c3e8da3$76491128@news.astraweb.com> Message-ID: <50a07f22$0$21742$c3e8da3$76491128@news.astraweb.com> On Mon, 12 Nov 2012 00:31:53 +0000, Oscar Benjamin wrote: [...] >>> You were right the first time, Chris. A point that happens to coincide >>> with the arbitrarily chosen origin is no more truthy or falsey than >>> any other. A vector of length 0 on the other hand is a very different >>> beast. >> >> Nonsense. The length and direction of a vector is relative to the >> origin. If the origin is arbitrary, as you claim, then so is the length >> of the vector. > > Wrong on all counts. Neither the length not the direction os a vector > are relative to any origin. When we choose to express a vector in > Cartesian components our representation assumes an orientation for the > axes of the coordinate system. Even in this sense, though, the origin > itself does not affect the components of the vector. Draw a set of axes and mark the vector [1, 1]. Here's a crappy ASCII art diagram, with X marking the head of the vector and a line drawn from the origin to the head. | | X | / | / |/ +------ Now draw a second set of axes with the origin set at the head of that vector. For reference, I leave the previous axis in place. As before, X represents the head of the vector. | | ----X----- | | | | | | +---|-- Note that the "body" of the vector -- the line from the origin to the head -- is gone. That's because the vector [1, 1] is transformed to the vector [0, 0] under a translation of one unit in both the X and Y directions. The magnitude of the vector under one coordinate system is 1, under the second it is 0. In a nutshell, you can't talk about either *distance* (magnitude) or *direction* without an answer to "distance from where? direction relative to what?". > I have spent a fair few hours in the past few weeks persuading teenaged > Engineering students to maintain a clear distinction between points, > vectors and lines. One of the ways that I distinguish vectors from > points is to say that a vector is like an arrow but its base has no > particular position. A point on the other hand is quite simply a > position. Given an origin (an arbitrarily chosen point) we can specify > another point using a "position vector": a vector from the origin to the > point in question. Just because you have spent a lot of time and effort giving people advice doesn't make it *good* advice. [...] > Wrong. The point (0,0,0,...) in some ND space is an arbitrarily chosen > position. By this I don't mean to say that the sequence of coordinates > consisting of all zeros is arbitrary. The choice of the point *in the > real/hypothetical space* that is designated by the sequence of zero > coordinates is arbitrary. So what? All you are saying is that there is more than one coordinate system, and we can choose the one we like for any problem. Of course we can, and that's a good thing. >>> The significance of zero in real algebra is not that it is the origin >>> but rather that it is the additive and multiplicative zero: >>> >>> a + 0 = a for any real number a >>> a * 0 = 0 for any real number a >> >> I'm not sure what you mean by "additive and multiplicative zero", you >> appear to be conflating two different properties here. 0 is the >> additive *identity*, but 1 is the multiplicative identity: > > I mean that it has the properties that zero has when used in addition > and multiplication: > http://en.wikipedia.org/wiki/0_%28number%29#Elementary_algebra I see no reference to "additive and multiplicative zero" there. Did you make up that terminology? The *identity* element is a common mathematical term, but 1 is the multiplicative identity element. What you are describing is generally known as the "absorbing element" over multiplication: if X*a = X for any a, then X is an absorbing element under multiplication. http://en.wikipedia.org/wiki/Absorbing_element And by the way, the vector [0, 0] (generalised to however many dimensions you need) is not necessarily the only null (zero) vector. Some vector spaces have many null vectors with non-zero components. http://en.wikipedia.org/wiki/Zero_vector but I digress. The exact terminology doesn't really change anything, since everything you say about vector [0,0] applies equally to the point (0,0) in the Cartesian plane. [...] >>> There is however no meaningful sense in which points (as opposed to >>> vectors) can be added to each other or multiplied by anything, so >>> there is no zero point. >> >> I think that the great mathematician Carl Gauss would have something to >> say about that. > > Is the text below a quote? No. >> Points in the plane are equivalent to complex numbers, and you can >> certainly add and multiply complex numbers. Adding two points is >> equivalent to a translation; multiplication of a scalar with a point is >> equivalent to a scale transformation. Multiplying two points is >> equivalent to complex multiplication, which is a scale + a rotation. > > The last point is bizarre. Complex multiplication makes no sense when > you're trying to think about vectors. Draw a 2D plot and convince > yourself that the square of the point (0, 1) is (-1, 0). Um, yes? It's a rotation of the point (0, 1) by 90? counter-clockwise, with a scale factor of 1. Does that confuse you? It's a straight-forward geometric interpretation of multiplication in the complex plane. http://www.clarku.edu/~djoyce/complex/mult.html If you take the vector [0, 1] and rotate it by 90? counter-clockwise, you get the vector [-1, 0]. >> Oh look, that's exactly the same geometric interpretation as for >> vectors. Hardly surprising, since vectors are the magnitude and >> direction of a line from the origin to a point. > > Here it becomes clear that you have conflated "position vectors" with > vectors in general. Let me list some other examples of vectors that are > clearly not "from the origin to a point": > > velocity > acceleration > force > electric field > angular momentum > wave vector > > (I could go on) But they are, all of them, without exception. E.g. velocity is relative to some frame of reference, that is, which sets the "zero velocity" relative to which all other velocities are measured. Electric fields are relative to the vacuum far from any electric charges. And so on. I quote: "The angular momentum L of a particle about a given origin is defined as: L = r ? p where r is the position vector of the particle relative to the origin, p is the linear momentum of the particle, and ? denotes the cross product." http://en.wikipedia.org/wiki/Angular_momentum Is there some part of "about a given origin" which needs additional explanation? -- Steven From rustompmody at gmail.com Mon Nov 12 02:09:31 2012 From: rustompmody at gmail.com (rusi) Date: Sun, 11 Nov 2012 23:09:31 -0800 (PST) Subject: A gnarly little python loop References: Message-ID: On Nov 11, 3:58?am, Roy Smith wrote: > I'm trying to pull down tweets with one of the many twitter APIs. ?The > particular one I'm using (python-twitter), has a call: > > data = api.GetSearch(term="foo", page=page) > > The way it works, you start with page=1. ?It returns a list of tweets. > If the list is empty, there are no more tweets. ?If the list is not > empty, you can try to get more tweets by asking for page=2, page=3, etc. > I've got: > > ? ? page = 1 > ? ? while 1: > ? ? ? ? r = api.GetSearch(term="foo", page=page) > ? ? ? ? if not r: > ? ? ? ? ? ? break > ? ? ? ? for tweet in r: > ? ? ? ? ? ? process(tweet) > ? ? ? ? page += 1 > > It works, but it seems excessively fidgety. ?Is there some cleaner way > to refactor this? This is a classic problem -- structure clash of parallel loops -- nd Steve Howell has given the classic solution using the fact that generators in python simulate/implement lazy lists. As David Beazley http://www.dabeaz.com/coroutines/ explains, coroutines are more general than generators and you can use those if you prefer. The classic problem used to be stated like this: There is an input in cards of 80 columns. It needs to be copied onto printer of 132 columns. The structure clash arises because after reading 80 chars a new card has to be read; after printing 132 chars a linefeed has to be given. To pythonize the problem, lets replace the 80,132 by 3,4, ie take the char-square abc def ghi and produce abcd efgh i The important difference (explained nicely by Beazley) is that in generators the for-loop pulls the generators, in coroutines, the 'generator' pushes the consuming coroutines. --------------- from __future__ import print_function s= ["abc", "def", "ghi"] # Coroutine-infrastructure from pep 342 def consumer(func): def wrapper(*args,**kw): gen = func(*args, **kw) gen.next() return gen return wrapper @consumer def endStage(): while True: for i in range(0,4): print((yield), sep='', end='') print("\n", sep='', end='') def genStage(s, target): for line in s: for i in range(0,3): target.send(line[i]) if __name__ == '__main__': genStage(s, endStage()) From steve+comp.lang.python at pearwood.info Mon Nov 12 02:32:02 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 12 Nov 2012 07:32:02 GMT Subject: List comprehension for testing **params References: <50a0258f$0$21241$ba4acef3@reader.news.orange.fr> <50a03233$0$21756$c3e8da3$76491128@news.astraweb.com> Message-ID: <50a0a5f1$0$21742$c3e8da3$76491128@news.astraweb.com> On Sun, 11 Nov 2012 18:21:32 -0600, Tim Chase wrote: > On 11/11/12 17:18, Steven D'Aprano wrote: >> but that leaves you with the next two problems: >> >> 2) Fixing the assert still leaves you with the wrong exception. You >> wouldn't raise a ZeroDivisionError, or a UnicodeDecodeError, or an >> IOError would you? No of course not. So why are you suggesting raising >> an AssertionError? That is completely inappropriate, the right >> exception to use is TypeError. Don't be lazy and treat assert as a >> quick and easy way to get exceptions for free. > > I'd say that it depends on whether the dictionary/kwargs gets populated > from user-supplied data (whether mail, a file, direct input, etc), or > whether it's coder-supplied. No, it doesn't matter if it is end-user supplied or coder-supplied. As usual, look at the standard library and Python builtins for best practices: py> len(42) # do we get an AssertionError? Traceback (most recent call last): File "", line 1, in TypeError: object of type 'int' has no len() > I like to think of "assert" as a "hey, you bone-headed programmer, you > did something that violated conditions for using this code properly!" You mean like passing a list to ord()? Or trying to add a number and a string? Raise the appropriate exception for the error: TypeError for type errors and missing function parameters, ValueErrors for objects of the right type but a bad argument, etc. Assertion errors are for asserting facts about internal program logic. "I assert that such-and-such is a fact", rather than "I require that such-and-such is a fact". For example, this might be a reasonable (although trivial) use of assert: def spam(n): if isinstance(n, int) and n > 0: result = ' '.join(["spam"]*n) assert result, "expected non-empty string but got empty" return result else: raise SpamError("No spam for you!!!") This gives a measure of protection against logic errors such as writing n >= 0. The presence of the assert is partly documentation and partly error checking, but the important thing is that it checks things which depend on the internal logic of the code itself, not on the input to the code. Given that you have already explicitly tested that n > 0, then the logical consequence is that "spam"*n will be non-empty. Hence an assertion "I assert that the string is not empty", rather than an overt test "Is the string empty?" Now, I accept that sometimes it is hard to draw a line between "internal program logic" and external input. E.g. if one function generates a value x, then calls another function with x as argument, the state of x is an internal detail to the first function and an external input to the other function. The rules I use are: * If the argument is a public parameter to a public function, then you must not use assert to validate it; * If the argument is a private parameter, or if the function is a private function, then you may use assert to validate it (but probably shouldn't); * You can use assert to check conditions of function internal variables (locals that you control); * Once you cross the boundary to another function, your objects should be treated as external again. E.g. I might have: def foo(a, b): # foo, a and b are public, so don't use assert if not (isinstance(a, int) and isinstance(b, int)): raise TypeError x = abs(a) + abs(b) # x is purely internal, so okay to assert assert x >= 0, "Hey McFly, you screwed up!" return bar(x) def bar(x): if x <= 0: raise ValueError("expected positive x") ... This now guards against something other than foo calling bar. -- Steven From moonhkt at gmail.com Mon Nov 12 04:25:19 2012 From: moonhkt at gmail.com (moonhkt) Date: Mon, 12 Nov 2012 01:25:19 -0800 (PST) Subject: skip Trackback error for ftp checking References: <75298ee5-9a24-4d93-91e9-955080e3f15d@vy11g2000pbb.googlegroups.com> <509df939$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Nov 10, 2:50?pm, Steven D'Aprano wrote: > On Fri, 09 Nov 2012 20:51:47 -0800, moonhkt wrote: > > HI All > > > How to skip Trackback warning/error when input ftp address is not > > correct or reject ? > > The same way you would skip any other error when you do something wrong: > catch the exception. > > -- > Steven Thank. Added below. try: ftp = FTP(options.remote_host_address) except : print "Host address not found." sys.exit(1) From insideshoes at gmail.com Mon Nov 12 04:34:36 2012 From: insideshoes at gmail.com (inshu chauhan) Date: Mon, 12 Nov 2012 10:34:36 +0100 Subject: Fwd: error In-Reply-To: References: <78f26f00-2c91-47b3-aef0-3eaeb8ae6f91@u4g2000pbo.googlegroups.com> <509BDEF1.50706@davea.name> Message-ID: No answers for my question ?? :O -------------- next part -------------- An HTML attachment was scrubbed... URL: From tinnews at isbd.co.uk Mon Nov 12 04:44:20 2012 From: tinnews at isbd.co.uk (tinnews at isbd.co.uk) Date: Mon, 12 Nov 2012 09:44:20 +0000 Subject: logging, can one get it to email messages over a certain level? References: Message-ID: Steve Howell wrote: > On Nov 11, 9:48?am, tinn... at isbd.co.uk wrote: > > I'm sure this must be possible but at the moment I can't see how to do it. > > > > I want to send an E-Mail when the logging module logs a message above > > a certain level (probably for ERROR and CRITICAL messages only). > > > > I.e. I want some sort of hook that will be called when these messages > > are logged (I can do the sendmail bit OK, I've got code that does that). > > > > -- > > Chris Green > > Scroll down to the Handlers section here: > > http://docs.python.org/2/howto/logging.html#logging-basic-tutorial > > I'm not sure this explains perfectly what you're gonna need to do, but > I hope it gets you in the ballpark. > Thank you, but yes I agree it's not terribly informative is it. However I think I understand what I need to do. I currently have a function to set up my logging:- def initLog(name): log = logging.getLogger(name) log.setLevel(logging.DEBUG) f = logging.handlers.RotatingFileHandler("/home/chris/tmp/mail.log", 'a', 1000000, 4) f.setLevel(logging.DEBUG) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') f.setFormatter(formatter) log.addHandler(f) return log As I understand it I just add another line like the 'f =' line but using the SMTPHandler and then set an appropriate level for that handler (and formatting). -- Chris Green From rosuav at gmail.com Mon Nov 12 05:23:00 2012 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 12 Nov 2012 21:23:00 +1100 Subject: skip Trackback error for ftp checking In-Reply-To: References: <75298ee5-9a24-4d93-91e9-955080e3f15d@vy11g2000pbb.googlegroups.com> <509df939$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Nov 12, 2012 at 8:25 PM, moonhkt wrote: > On Nov 10, 2:50 pm, Steven D'Aprano +comp.lang.pyt... at pearwood.info> wrote: >> >> The same way you would skip any other error when you do something wrong: >> catch the exception. > > Thank. Added below. > try: > ftp = FTP(options.remote_host_address) > except : > print "Host address not found." > sys.exit(1) Heh, I'm afraid that's not quite what Steven meant; what you're doing there is literally skipping _any other error_. A bare except is usually not a good thing. Catch the specific exception you want to catch - that way, if you typo a name or something, you don't get an obscure message about the host address when it's really a coding bug. ChrisA From emailkgnow at gmail.com Mon Nov 12 05:45:31 2012 From: emailkgnow at gmail.com (Khalid Al-Ghamdi) Date: Mon, 12 Nov 2012 02:45:31 -0800 (PST) Subject: Is there a way to creat a func that returns a cursor that can be used? Message-ID: <05f7724a-1e41-43ea-b837-88e476917f9a@googlegroups.com> Is there a way to create a func that returns a cursor that can be used to execute sql statements? I tried this (after importing sqlite3), but it gave me the error below: >>> def connect(): conn = sqlite3.connect(':memory:')#use sch3.db or sch4.db .... etc. cur = conn.cursor() cur.execute("create table schedule (teams integer, sn integer, badge integer ,name text, grp integer,\ major text, track text, stage text, tc text, subject text, course text, ws text, date text, \ time text, proctor text, code text, no integer,fl_time text, flag2 text, flag3 text, flag4 text, clash1 integer, clash2 integer)") return cur >>> connect() >>> cur.execute("select * from schedule") Traceback (most recent call last): File "", line 1, in cur.execute("select * from schedule") NameError: name 'cur' is not defined From ulrich.eckhardt at dominolaser.com Mon Nov 12 05:48:06 2012 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Mon, 12 Nov 2012 11:48:06 +0100 Subject: List comprehension for testing **params In-Reply-To: <50a0258f$0$21241$ba4acef3@reader.news.orange.fr> References: <50a0258f$0$21241$ba4acef3@reader.news.orange.fr> Message-ID: <6o07n9-hnd.ln1@satorlaser.homedns.org> Am 11.11.2012 23:24, schrieb Cantabile: > I'm writing a small mail library for my own use, and at the time I'm > testing parameters like this: Let's ignore the facts that there is an existing mail library, that you should use real parameters if they are required and that exit() is completely inappropriate. Others explained why sufficiently. [slightly shortened] > def function(**params) > required = ['Subject', 'From', 'To', 'msg'] > for i in required: > if not i in params.keys(): > print "Error, \'%s\' argument is missing" %i Let's start with the last line: If you use "Error, missing {!r} argument".format(i), you get the quotes automatically, plus possibly escaped unicode characters and newlines, although it's not necessary. Also, I would "from __future__ import print_function" in any new code, to ease upgrading to Python 3. Now, concerning the test whether the required parameters are passed to the function, you can use "if i in params", which is slightly shorter and IMHO similarly expressive. Also, it doesn't repeatedly create a list, checks for a single element inside that list and then throws the list away again. Further, you don't even need a list for the parameters, since order doesn't matter and duplicates shouldn't exist, so I would use a set instead: required = {'Subject', 'From', 'To', 'msg'} The advantage is slightly faster lookup (completely irrelevant for this small number of elements) but also that it makes the intention a bit clearer to people that know what a set is. In a second step, you compute the intersection between the set of required arguments and the set of supplied arguments and verify that all are present: if required.intersection(params.keys()) != required: missing = required - set(params.keys()) raise Exception("missing arguments {}".format( ', '.join(missing))) Happy hacking! Uli From __peter__ at web.de Mon Nov 12 05:54:49 2012 From: __peter__ at web.de (Peter Otten) Date: Mon, 12 Nov 2012 11:54:49 +0100 Subject: logging, can one get it to email messages over a certain level? References: Message-ID: tinnews at isbd.co.uk wrote: > Steve Howell wrote: >> On Nov 11, 9:48 am, tinn... at isbd.co.uk wrote: >> > I'm sure this must be possible but at the moment I can't see how to do >> > it. >> > >> > I want to send an E-Mail when the logging module logs a message above >> > a certain level (probably for ERROR and CRITICAL messages only). >> > >> > I.e. I want some sort of hook that will be called when these messages >> > are logged (I can do the sendmail bit OK, I've got code that does >> > that). >> > >> > -- >> > Chris Green >> >> Scroll down to the Handlers section here: >> >> http://docs.python.org/2/howto/logging.html#logging-basic-tutorial >> >> I'm not sure this explains perfectly what you're gonna need to do, but >> I hope it gets you in the ballpark. >> > Thank you, but yes I agree it's not terribly informative is it. Here's a minimal example: import logging from logging.handlers import SMTPHandler if __name__ == "__main__": logging.basicConfig(level=logging.INFO) mail_handler = SMTPHandler( "smtp.example.com", "navigator at example.com", "captain.nemo at example.com", "fyi") mail_handler.setLevel(logging.CRITICAL) root = logging.getLogger() root.addHandler(mail_handler) # will print amessage root.warn("this is fishy") # will print a message and send an email root.critical("giant squid sighted") From __peter__ at web.de Mon Nov 12 05:57:52 2012 From: __peter__ at web.de (Peter Otten) Date: Mon, 12 Nov 2012 11:57:52 +0100 Subject: Is there a way to creat a func that returns a cursor that can be used? References: <05f7724a-1e41-43ea-b837-88e476917f9a@googlegroups.com> Message-ID: Khalid Al-Ghamdi wrote: > Is there a way to create a func that returns a cursor that can be used to > execute sql statements? You should read an introductory text on Python, this is not specific to sqlite3. > I tried this (after importing sqlite3), but it gave me the error below: > >>>> def connect(): > conn = sqlite3.connect(':memory:')#use sch3.db or sch4.db .... etc. > cur = conn.cursor() > cur.execute("create table schedule (teams integer, sn integer, badge > integer ,name text, grp integer,\ > major text, track text, stage text, tc text, subject text, course > text, ws text, date text, \ time text, proctor text, code text, no > integer,fl_time text, flag2 text, flag3 text, flag4 text, clash1 > integer, clash2 integer)") return cur >>>> connect() connect() returns a cursor, but you discard the result. Try cur = connect() > >>>> cur.execute("select * from schedule") > Traceback (most recent call last): > File "", line 1, in > cur.execute("select * from schedule") > NameError: name 'cur' is not defined From rosuav at gmail.com Mon Nov 12 06:01:13 2012 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 12 Nov 2012 22:01:13 +1100 Subject: Is there a way to creat a func that returns a cursor that can be used? In-Reply-To: <05f7724a-1e41-43ea-b837-88e476917f9a@googlegroups.com> References: <05f7724a-1e41-43ea-b837-88e476917f9a@googlegroups.com> Message-ID: On Mon, Nov 12, 2012 at 9:45 PM, Khalid Al-Ghamdi wrote: > Is there a way to create a func that returns a cursor that can be used to execute sql statements? Yes, and you're almost there! > I tried this (after importing sqlite3), but it gave me the error below: > >>>> def connect(): > return cur >>>> connect() > >>>> cur.execute("select * from schedule") > Traceback (most recent call last): > File "", line 1, in > cur.execute("select * from schedule") > NameError: name 'cur' is not defined All you need to do is make use of the return value. Try this instead: cur = connect() That takes the returned cursor object and binds it to the name 'cur' in global scope. You can then use 'cur.execute...' and it'll be the same object. As a side point, thank you for posting so clearly. It's easy to help when your code and traceback are all there! ChrisA From tinnews at isbd.co.uk Mon Nov 12 06:36:47 2012 From: tinnews at isbd.co.uk (tinnews at isbd.co.uk) Date: Mon, 12 Nov 2012 11:36:47 +0000 Subject: logging, can one get it to email messages over a certain level? References: Message-ID: Peter Otten <__peter__ at web.de> wrote: > tinnews at isbd.co.uk wrote: > > > Steve Howell wrote: > >> On Nov 11, 9:48 am, tinn... at isbd.co.uk wrote: > >> > I'm sure this must be possible but at the moment I can't see how to do > >> > it. > >> > > >> > I want to send an E-Mail when the logging module logs a message above > >> > a certain level (probably for ERROR and CRITICAL messages only). > >> > > >> > I.e. I want some sort of hook that will be called when these messages > >> > are logged (I can do the sendmail bit OK, I've got code that does > >> > that). > >> > > >> > -- > >> > Chris Green > >> > >> Scroll down to the Handlers section here: > >> > >> http://docs.python.org/2/howto/logging.html#logging-basic-tutorial > >> > >> I'm not sure this explains perfectly what you're gonna need to do, but > >> I hope it gets you in the ballpark. > >> > > Thank you, but yes I agree it's not terribly informative is it. > > Here's a minimal example: > > import logging > from logging.handlers import SMTPHandler > > if __name__ == "__main__": > logging.basicConfig(level=logging.INFO) > > mail_handler = SMTPHandler( > "smtp.example.com", > "navigator at example.com", > "captain.nemo at example.com", > "fyi") > mail_handler.setLevel(logging.CRITICAL) > > root = logging.getLogger() > root.addHandler(mail_handler) > > # will print amessage > root.warn("this is fishy") > > # will print a message and send an email > root.critical("giant squid sighted") > Thank you. -- Chris Green From joel.goldstick at gmail.com Mon Nov 12 07:25:43 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 12 Nov 2012 07:25:43 -0500 Subject: Is there a way to creat a func that returns a cursor that can be used? In-Reply-To: References: <05f7724a-1e41-43ea-b837-88e476917f9a@googlegroups.com> Message-ID: On Mon, Nov 12, 2012 at 6:01 AM, Chris Angelico wrote: > On Mon, Nov 12, 2012 at 9:45 PM, Khalid Al-Ghamdi > wrote: > > Is there a way to create a func that returns a cursor that can be used > to execute sql statements? > > Yes, and you're almost there! > > > I tried this (after importing sqlite3), but it gave me the error below: > > > >>>> def connect(): > > return cur > >>>> connect() > > > >>>> cur.execute("select * from schedule") > > Traceback (most recent call last): > > File "", line 1, in > > cur.execute("select * from schedule") > > NameError: name 'cur' is not defined > > All you need to do is make use of the return value. Try this instead: > > cur = connect() > > That takes the returned cursor object and binds it to the name 'cur' > in global scope. You can then use 'cur.execute...' and it'll be the > same object. > > As a side point, thank you for posting so clearly. It's easy to help > when your code and traceback are all there! > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list > You asked this question yesterday. I answered as did several others. You seem to be stuck on how values are returned from a function. Chris gave you the same help that you got yesterday. The cur variable within the function disappears when the function ends. Read about python namespaces to learn more. So, you need to return the cur value in your function by using this statement: return cur Then when you call connect() is will return the cur value. You have to name it like this: cur = connect() You should go to python.org and read the tutorials, specifically about functions. good luck -- Joel Goldstick -------------- next part -------------- An HTML attachment was scrubbed... URL: From jkn_gg at nicorp.f9.co.uk Mon Nov 12 07:55:23 2012 From: jkn_gg at nicorp.f9.co.uk (jkn) Date: Mon, 12 Nov 2012 04:55:23 -0800 (PST) Subject: how to simulate tar filename substitution across piped subprocess.Popen() calls? References: <509c588a$0$6904$e4fe514c@news2.news.xs4all.nl> Message-ID: <5b39febc-fbb9-4d8e-b331-0401e78d2b7f@m13g2000vbd.googlegroups.com> Hi Hans thanks a lot for your reply: > That's what 'xargs' will do for you. ?All you need to do, is invoke > xargs with arguments containing '{}'. ?I.e., something like: > > cmd1 = ['tar', '-czvf', 'myfile.tgz', '-c', mydir, 'mysubdir'] > first_process = subprocess.Popen(cmd1, stdout=subprocess.PIPE) > > cmd2 = ['xargs', '-I', '{}', 'sh', '-c', "test -f %s/'{}'" % mydir] > second_process = subprocess.Popen(cmd2, stdin=first_process.stdout) > Hmm - that's pretty much what I've been trying. I will have to experiment a bit more and post the results in a bit more detail. > > Apologies if I've made any howlers in this description - it's very > > likely... > > I think the second '-c' argument to tar should have been a '-C'. You are correct, thanks. Serves me right for typing the simplified version in by hand. I actually use the equivalent "--directory=..." in the actual code. > I'm not sure I understand what the second command is trying to > achieve. ?On my system, nothing happens, because tar writes the > names of the files it is adding to stderr, so xargs receives no > input at all. ?If I send the stderr from tar to the stdin of > xargs, then it still doesn't seem to do anything sensible. That's interesting ... on my system, and all others that I know about, the file list goes to stdout. > Perhaps your real xargs command is more complicated and more > sensible. Yes, in fact the output from xargs is piped to a third process. But I realise this doesn't alter the result of your experiment; the xargs process should filter a subset of the files being fed to it. I will experiment a bit more and hopefully post some results. Thanks in the meantime... Regards Jon N From anthra.norell at bluewin.ch Mon Nov 12 08:12:08 2012 From: anthra.norell at bluewin.ch (F.R.) Date: Mon, 12 Nov 2012 14:12:08 +0100 Subject: Strange object identity problem Message-ID: <50A0F5A8.4080802@bluewin.ch> Hi all, Once in a while I write simple routine stuff and spend the next few hours trying to understand why it doesn't behave as I expect. Here is an example holding me up: I have a module "st" with a class "runs". In a loop I repeatedly create an object "ba" and call the method "ba.run ()" which processes the constructor's arguments. Next I store the object in a dictionary "bas". It then turns out that all keys hold the same object, namely the one created last in the loop. Verifying the identity of each object when it is being assigned to the dictionary reveals different identities. Repeating the verification after the loop is done shows the same object in all key positions: >>> bas = {} >>> for year in range (2010, 2013): ba = st.runs ('BA', '%d-01-01' % year, '%d-12-31' % year) ba.run () print year, id (ba) bas [year] = ba 2010 150289932 2011 150835852 2012 149727788 >>> for y in sorted (bas.keys ()): b = bas [year] print y, id (b) 2010 149727788 2011 149727788 2012 149727788 -------------------- The class "runs" has a bunch of attributes, among which an object "parameters" for tweaking processing runs and a object "quotes" containing a list of data base records. Both objects are created by "runs.__init__ (...)". Trying something similar with a simpler class works as expected: >>> class C: def __init__ (self, i): self.i = i def run (self): self.ii = self.i * self.i >>> cees = {} >>> for year in range (2010, 2013): c = C (year) c.run () print year, id (c) cees [year] = c 2010 150837804 2011 148275756 2012 146131212 >>> for year in sorted (cees.keys ()): print year, id (cees [year]), cees [year].ii 2010 150837804 4040100 2011 148275756 4044121 2012 146131212 4048144 -------------------- I have checked for name clashes and found none, wondering what to check next for. Desperate for suggestions. Frederic (Python 2.7 on Ubuntu 12.04) From joshua.landau.ws at gmail.com Mon Nov 12 08:23:23 2012 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Mon, 12 Nov 2012 13:23:23 +0000 Subject: List comprehension for testing **params In-Reply-To: <6o07n9-hnd.ln1@satorlaser.homedns.org> References: <50a0258f$0$21241$ba4acef3@reader.news.orange.fr> <6o07n9-hnd.ln1@satorlaser.homedns.org> Message-ID: Just a few tricks you may have missed: On 12 November 2012 10:48, Ulrich Eckhardt wrote: > Am 11.11.2012 23:24, schrieb Cantabile: if required.intersection(params.**keys()) != required: > if required.issubset(params): > missing = required - set(params.keys()) > missing = required.difference(params) > raise Exception("missing arguments {}".format( > ', '.join(missing))) (untested) -------------- next part -------------- An HTML attachment was scrubbed... URL: From joshua.landau.ws at gmail.com Mon Nov 12 08:25:17 2012 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Mon, 12 Nov 2012 13:25:17 +0000 Subject: List comprehension for testing **params In-Reply-To: References: <50a0258f$0$21241$ba4acef3@reader.news.orange.fr> <6o07n9-hnd.ln1@satorlaser.homedns.org> Message-ID: On 12 November 2012 13:23, Joshua Landau wrote: > Just a few tricks you may have missed: > > On 12 November 2012 10:48, Ulrich Eckhardt < > ulrich.eckhardt at dominolaser.com> wrote: > >> Am 11.11.2012 23:24, schrieb Cantabile: > > if required.intersection(params.**keys()) != required: >> > > if required.issubset(params): > *Ahem*: if *not* required.issubset(params): > > >> missing = required - set(params.keys()) >> > > missing = required.difference(params) > > >> raise Exception("missing arguments {}".format( >> ', '.join(missing))) > > > > (untested) > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ulrich.eckhardt at dominolaser.com Mon Nov 12 08:26:19 2012 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Mon, 12 Nov 2012 14:26:19 +0100 Subject: Strange object identity problem In-Reply-To: References: Message-ID: Am 12.11.2012 14:12, schrieb F.R.: > Once in a while I write simple routine stuff and spend the next few hours > trying to understand why it doesn't behave as I expect. Here is an example > holding me up: [...snip incomplete code...] > Trying something similar with a simpler class works as expected: [...snip example code...] Okay, that's almost a classic. You ask about code that fails, while providing code that works as example. Crystal balls are rare nowadays, so this is really hard to answer! In any case, here's what you could do: 1. use a debugger (import pdb...) 2. some more info could be retrieved by outputting the actual type along with the ID of the objects in question (see type() function) 3. reduce the non-working code until you have a minimal example that you can post here I'd bet that at latest while trying approach 3 above, you will find the error yourself. Good luck! Uli From franke.rob at gmail.com Mon Nov 12 08:27:29 2012 From: franke.rob at gmail.com (Robert Franke) Date: Mon, 12 Nov 2012 14:27:29 +0100 Subject: Strange object identity problem In-Reply-To: <50A0F5A8.4080802@bluewin.ch> References: <50A0F5A8.4080802@bluewin.ch> Message-ID: Hi Frederic, [...] >>>> bas = {} >>>> for year in range (2010, 2013): > ba = st.runs ('BA', '%d-01-01' % year, '%d-12-31' % year) > ba.run () > print year, id (ba) > bas [year] = ba > > 2010 150289932 > 2011 150835852 > 2012 149727788 > >>>> for y in sorted (bas.keys ()): > b = bas [year] Shouldn't that be b = bas[y]? > print y, id (b) > > 2010 149727788 > 2011 149727788 > 2012 149727788 > [...] Cheers, Robert From anthra.norell at bluewin.ch Mon Nov 12 08:40:59 2012 From: anthra.norell at bluewin.ch (F.R.) Date: Mon, 12 Nov 2012 14:40:59 +0100 Subject: Strange object identity problem In-Reply-To: References: <50A0F5A8.4080802@bluewin.ch> Message-ID: <50A0FC6B.60300@bluewin.ch> On 11/12/2012 02:27 PM, Robert Franke wrote: > Hi Frederic, > > [...] > >>>>> bas = {} >>>>> for year in range (2010, 2013): >> ba = st.runs ('BA', '%d-01-01' % year, '%d-12-31' % year) >> ba.run () >> print year, id (ba) >> bas [year] = ba >> >> 2010 150289932 >> 2011 150835852 >> 2012 149727788 >> >>>>> for y in sorted (bas.keys ()): >> b = bas [year] > Shouldn't that be b = bas[y]? Yes, it should, indeed! What's more, I should have closed and restarted IDLE. There must have been a name clash somewhere in the name space. The problem no longer exists. Sorry about that. And thanks to all who paused to reflect on this non-problem. - Frederic. > > >> print y, id (b) >> >> 2010 149727788 >> 2011 149727788 >> 2012 149727788 >> > [...] > > Cheers, > > Robert > From ulrich.eckhardt at dominolaser.com Mon Nov 12 08:48:56 2012 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Mon, 12 Nov 2012 14:48:56 +0100 Subject: int.__init__ incompatible in Python 3.3 In-Reply-To: <509ceb08$0$29980$c3e8da3$5496439d@news.astraweb.com> References: <481tm9-k6i.ln1@satorlaser.homedns.org> <6ipum9-oim.ln1@satorlaser.homedns.org> <509ceb08$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: <9bb7n9-kie.ln1@satorlaser.homedns.org> Am 09.11.2012 12:37, schrieb Steven D'Aprano: > In Python 3.3: > > py> class X(int): > ... def __init__(self, *args): > ... super().__init__(*args) # does nothing, call it anyway > ... > py> x = X(22) > Traceback (most recent call last): > File "", line 1, in > File "", line 3, in __init__ > TypeError: object.__init__() takes no parameters > > > It is apparently an oversight, or a bug, that it ever worked in older > versions. I'm not really convinced that the overall behaviour is sound: py> x = 42 py> x.__init__() py> x.__init__(1) py> x.__init__(1,2) py> x.__init__(1,2,3) py> x.__init__(1,2,3,4) Neither of these seem to care about the number and type of parameters. On the other hand: py> y = object() py> y.__init__() py> y.__init__(1) Traceback (most recent call last): File "", line 1, in TypeError: object.__init__() takes no parameters So, for some reason that I don't understand yet, my call to the superclass' init function skips a class, but only when called with super(). Confused greetings! Uli From d at davea.name Mon Nov 12 10:04:08 2012 From: d at davea.name (Dave Angel) Date: Mon, 12 Nov 2012 10:04:08 -0500 Subject: error In-Reply-To: References: <78f26f00-2c91-47b3-aef0-3eaeb8ae6f91@u4g2000pbo.googlegroups.com> <509BDEF1.50706@davea.name> Message-ID: <50A10FE8.1080006@davea.name> On 11/09/2012 09:08 AM, inshu chauhan wrote: > Actually this one.. and its the last.. > > >>>>> >>>> >>>> The only extra libary i am using is Opencv , downloaded from >>>> http://sourceforge.net/projects/opencvlibrary/ >>>> >>>> and numpy. >>>> >>>>> 3) what import statement did you use ? >>>>> >>>> >>>> import cv >>>> >>>> >>>>> 5) Exactly what error message did you get, including the traceback ? >>>>> >>>> >>>> Traceback (most recent call last): >>>> File "Z:\modules\Masking_an_image_dynamically.py", line 155, in >>>> >>>> AccessPixels(data) >>>> File "Z:\modules\.py", line 147, in AccessPixels >>>> CreateMask(data, x, y) >>>> File "Z:\modules\new_classification.py", line 110, in CreateMask >>>> point = data[iy, ix ] >>>> >>>> error: index is out of range >>>> >>>> The line numbers here and the file attached may be different because I >>>> have removed a lot of print statements which I was using to debug the >>>> error.. >>>> More than that is wrong. The file names are different. If I guess from your source file that all this code is in your own script, "Masking_an_image_dynamically.py" then two of the filenames in the traceback indicate big problems. Further, on the same assumption, the function CreateMask has no such line as 'point = data[iy, ix] I did spend some time with the website http://sourceforge.net/projects/opencvlibrary/ but all I see is C++ docs, and since it's enormous, I didn't think I'm likely to find out anything directly from there. The particular thing I was looking for was whether rows or columns are specified first when treating 'data" as a list of lists. In other words, I look at the two lines: CreateMask(data, x, y) and point = data[iy, ix] and try to figure out which one is unreasonable. Find out the convention used in the module, and stay consistent with it. In Photoshop. an 8x10 is a portrait layout, while 10x8 is landscape. In other words, row number is first. But i have no idea what Opencv uses. Since I've not used either Opencv nor numpy, somebody else had better jump in after you post the traceback that matches your source file. Perhaps it's IDLE that's getting confused. Have you tried running it from a cmd.exe prompt, and pasting from that cmd window into a message? -- DaveA From rustompmody at gmail.com Mon Nov 12 10:21:49 2012 From: rustompmody at gmail.com (rusi) Date: Mon, 12 Nov 2012 07:21:49 -0800 (PST) Subject: A gnarly little python loop References: Message-ID: On Nov 12, 12:09?pm, rusi wrote: > This is a classic problem -- structure clash of parallel loops Sorry wrong solution :D The fidgetiness is entirely due to python not allowing C-style loops like these: >> while ((c=getchar()!= EOF) { ... } Putting it into coroutine form, it becomes something like the following [Untested since I dont have the API]. Clearly the fidgetiness is there as before and now with extra coroutine plumbing def genStage(term, target): page = 1 while 1: r = api.GetSearch(term="foo", page=page) if not r: break for tweet in r: target.send(tweet) page += 1 @consumer def endStage(): while True: process((yield)) if __name__ == '__main__': genStage("foo", endStage()) From jkn_gg at nicorp.f9.co.uk Mon Nov 12 10:36:58 2012 From: jkn_gg at nicorp.f9.co.uk (jkn) Date: Mon, 12 Nov 2012 07:36:58 -0800 (PST) Subject: how to simulate tar filename substitution across piped subprocess.Popen() calls? References: <509c588a$0$6904$e4fe514c@news2.news.xs4all.nl> <5b39febc-fbb9-4d8e-b331-0401e78d2b7f@m13g2000vbd.googlegroups.com> Message-ID: slight followup ... I have made some progress; for now I'm using subprocess.communicate to read the output from the first subprocess, then writing it into the secodn subprocess. This way I at least get to see what is happening ... The reason 'we' weren't seeing any output from the second call (the 'xargs') is that as mentioned I had simplified this. The actual shell command was more like (in python-speak): "xargs -I {} sh -c \"test -f %s/{} && md5sum %s/{}\"" % (mydir, mydir) ie. I am running md5sum on each tar-file entry which passes the 'is this a file' test. My next problem; how to translate the command-string clause "test -f %s/{} && md5sum %s/{}" # ... into s parameter to subprocss.Popen(). I think it's the command chaining '&&' which is tripping me up... Cheers J^n From __peter__ at web.de Mon Nov 12 10:49:23 2012 From: __peter__ at web.de (Peter Otten) Date: Mon, 12 Nov 2012 16:49:23 +0100 Subject: A gnarly little python loop References: Message-ID: rusi wrote: > The fidgetiness is entirely due to python not allowing C-style loops > like these: > >>> while ((c=getchar()!= EOF) { ... } for c in iter(getchar, EOF): ... > Clearly the fidgetiness is there as before and now with extra coroutine > plumbing Hmm, very funny... From j.kleese at arcor.de Mon Nov 12 10:52:27 2012 From: j.kleese at arcor.de (Johannes Kleese) Date: Mon, 12 Nov 2012 16:52:27 +0100 Subject: Bugs: Content-Length not updated by reused urllib.request.Request / has_header() case-sensitive Message-ID: <50a12949$0$6566$9b4e6d93@newsspool3.arcor-online.net> Hi! (Yes, I did take a look at the issue tracker but couldn't find any corresponding bug, and no, I don't want to open a new account just for this one.) -------------------------------------------------------------------- I'm reusing a single urllib.request.Request object to HTTP-POST data to the same URL a number of times. While the data itself is sent as expected every time, the Content-Length header is not updated after the first request. Tested with Python 3.1.3 and Python 3.1.4. >>> opener = urllib.request.build_opener() >>> request = urllib.request.Request("http://example.com/", headers = {"Content-Type": "application/x-www-form-urlencoded"}) >>> opener.open(request, "1".encode("us-ascii")) >>> request.data b'1' >>> request.header_items() [('Content-length', '1'), ('Content-type', 'application/x-www-form-urlencoded'), ('Host', 'example.com'), ('User-agent', 'Python-urllib/3.1')] >>> opener.open(request, "123456789".encode("us-ascii")) >>> request.data b'123456789' >>> request.header_items() [('Content-length', '1'), ('Content-type', 'application/x-www-form-urlencoded'), ('Host', 'example.com'), ('User-agent', 'Python-urllib/3.1')] Note that after the second run, Content-Length stays "1", but should be "9", corresponding to the data b'123456789'. (Request data is not x-www-form-urlencoded to shorten the test case. Doesn't affect the bug, though.) -------------------------------------------------------------------- While at it, I noticed that urllib.request.Request.has_header() and .get_header() are case-sensitive, while HTTP headers are not (RFC 2616, 4.2). Thus the following, slightly unfortunate behaviour: >>> request.header_items() [('Content-length', '1'), ('Content-type', 'application/x-www-form-urlencoded'), ('Host', 'example.com'), ('User-agent', 'Python-urllib/3.1')] >>> request.has_header("Content-Type") False >>> request.has_header("Content-type") True >>> request.get_header("Content-Type") >>> request.get_header("Content-type") 'application/x-www-form-urlencoded' -------------------------------------------------------------------- Thanks for taking care. From showell30 at yahoo.com Mon Nov 12 11:09:16 2012 From: showell30 at yahoo.com (Steve Howell) Date: Mon, 12 Nov 2012 08:09:16 -0800 (PST) Subject: A gnarly little python loop References: Message-ID: On Nov 12, 7:21?am, rusi wrote: > On Nov 12, 12:09?pm, rusi wrote:> This is a classic problem -- structure clash of parallel loops > > > > Sorry wrong solution :D > > The fidgetiness is entirely due to python not allowing C-style loops > like these: > > >> while ((c=getchar()!= EOF) { ... } > [...] There are actually three fidgety things going on: 1. The API is 1-based instead of 0-based. 2. You don't know the number of pages in advance. 3. You want to process tweets, not pages of tweets. Here's yet another take on the problem: # wrap fidgety 1-based api def search(i): return api.GetSearch("foo", i+1) paged_tweets = (search(i) for i in count()) # handle sentinel paged_tweets = iter(paged_tweets.next, []) # flatten pages tweets = chain.from_iterable(paged_tweets) for tweet in tweets: process(tweet) From hansmu at xs4all.nl Mon Nov 12 11:35:43 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Mon, 12 Nov 2012 17:35:43 +0100 Subject: how to simulate tar filename substitution across piped subprocess.Popen() calls? In-Reply-To: References: <509c588a$0$6904$e4fe514c@news2.news.xs4all.nl> <5b39febc-fbb9-4d8e-b331-0401e78d2b7f@m13g2000vbd.googlegroups.com> Message-ID: <50a12560$0$6972$e4fe514c@news2.news.xs4all.nl> On 12/11/12 16:36:58, jkn wrote: > slight followup ... > > I have made some progress; for now I'm using subprocess.communicate to > read the output from the first subprocess, then writing it into the > secodn subprocess. This way I at least get to see what is > happening ... > > The reason 'we' weren't seeing any output from the second call (the > 'xargs') is that as mentioned I had simplified this. The actual shell > command was more like (in python-speak): > > "xargs -I {} sh -c \"test -f %s/{} && md5sum %s/{}\"" % (mydir, mydir) > > ie. I am running md5sum on each tar-file entry which passes the 'is > this a file' test. > > My next problem; how to translate the command-string clause > > "test -f %s/{} && md5sum %s/{}" # ... > > into s parameter to subprocss.Popen(). I think it's the command > chaining '&&' which is tripping me up... It is not really necessary to translate the '&&': you can just write: "test -f '%s/{}' && md5sum '%s/{}'" % (mydir, mydir) , and xargs will pass that to the shell, and then the shell will interpret the '&&' for you: you have shell=False in your subprocess.Popen call, but the arguments to xargs are -I {} sh -c "....", and this means that xargs ends up invoking the shell (after replacing the {} with the name of a file). Alternatively, you could translate it as: "if [ -f '%s/{}' ]; then md5sum '%s/{}'; fi" % (mydir, mydir) ; that might make the intent clearer to whoever gets to maintain your code. Hope this helps, -- HansM From puntabluda at gmail.com Mon Nov 12 11:58:16 2012 From: puntabluda at gmail.com (Rebelo) Date: Mon, 12 Nov 2012 08:58:16 -0800 (PST) Subject: how to simulate tar filename substitution across piped subprocess.Popen() calls? In-Reply-To: References: Message-ID: <96f4ba8b-c520-4ae4-a0ee-73365300bb9c@googlegroups.com> Dana ?etvrtak, 8. studenoga 2012. 19:05:12 UTC+1, korisnik jkn napisao je: > Hi All > > i am trying to build up a set of subprocess.Ponen calls to > > replicate the effect of a horribly long shell command. I'm not clear > > how I can do one part of this and wonder if anyone can advise. I'm on > > Linux, fairly obviously. > > J^n You should try to do it in pure python, avoiding shell altogether. The first step would be to actually write what it is you want to do. To filter files you want to add to tar file check tarfile (http://docs.python.org/2/library/tarfile.html?highlight=tar#module-tarfile), specifically : TarFile.add(name, arcname=None, recursive=True, exclude=None, filter=None) which takes filter paramter : "If filter is specified it must be a function that takes a TarInfo object argument and returns the changed TarInfo object. If it instead returns None the TarInfo object will be excluded from the archive." From buzzard at invalid.invalid Mon Nov 12 12:02:13 2012 From: buzzard at invalid.invalid (duncan smith) Date: Mon, 12 Nov 2012 17:02:13 +0000 Subject: Strange object identity problem In-Reply-To: References: <50A0F5A8.4080802@bluewin.ch> Message-ID: <50a12baa$0$12377$a8266bb1@newsreader.readnews.com> On 12/11/12 13:40, F.R. wrote: > On 11/12/2012 02:27 PM, Robert Franke wrote: >> Hi Frederic, >> >> [...] >> >>>>>> bas = {} >>>>>> for year in range (2010, 2013): >>> ba = st.runs ('BA', '%d-01-01' % year, '%d-12-31' % year) >>> ba.run () >>> print year, id (ba) >>> bas [year] = ba >>> >>> 2010 150289932 >>> 2011 150835852 >>> 2012 149727788 >>> >>>>>> for y in sorted (bas.keys ()): >>> b = bas [year] >> Shouldn't that be b = bas[y]? > Yes, it should, indeed! What's more, I should have closed and restarted > IDLE. There must have > been a name clash somewhere in the name space. The problem no longer > exists. Sorry > about that. And thanks to all who paused to reflect on this non-problem. > - Frederic. > >> >> >>> print y, id (b) >>> >>> 2010 149727788 >>> 2011 149727788 >>> 2012 149727788 >>> >> [...] >> >> Cheers, >> >> Robert >> > The problem was that year was bound to the integer 2013 from the first loop. When you subsequently looped over the keys you printed each key followed by id(bas[2013]). Restarting IDLE only helped because you presumably didn't repeat the error. Duncan From jkn_gg at nicorp.f9.co.uk Mon Nov 12 12:22:44 2012 From: jkn_gg at nicorp.f9.co.uk (jkn) Date: Mon, 12 Nov 2012 09:22:44 -0800 (PST) Subject: how to simulate tar filename substitution across piped subprocess.Popen() calls? References: <509c588a$0$6904$e4fe514c@news2.news.xs4all.nl> <5b39febc-fbb9-4d8e-b331-0401e78d2b7f@m13g2000vbd.googlegroups.com> <50a12560$0$6972$e4fe514c@news2.news.xs4all.nl> Message-ID: Hi Hans On Nov 12, 4:36?pm, Hans Mulder wrote: > On 12/11/12 16:36:58, jkn wrote: > > > > > > > > > > > slight followup ... > > > I have made some progress; for now I'm using subprocess.communicate to > > read the output from the first subprocess, then writing it into the > > secodn subprocess. This way I at least get to see what is > > happening ... > > > The reason 'we' weren't seeing any output from the second call (the > > 'xargs') is that as mentioned I had simplified this. The actual shell > > command was more like (in python-speak): > > > "xargs -I {} sh -c \"test -f %s/{} && md5sum %s/{}\"" % (mydir, mydir) > > > ie. I am running md5sum on each tar-file entry which passes the 'is > > this a file' test. > > > My next problem; how to translate the command-string clause > > > ? ? "test -f %s/{} && md5sum %s/{}" # ... > > > into s parameter to subprocss.Popen(). I think it's the command > > chaining '&&' which is tripping me up... > > It is not really necessary to translate the '&&': you can > just write: > > ? ? "test -f '%s/{}' && md5sum '%s/{}'" % (mydir, mydir) > > , and xargs will pass that to the shell, and then the shell > will interpret the '&&' for you: you have shell=False in your > subprocess.Popen call, but the arguments to xargs are -I {} > sh -c "....", and this means that xargs ends up invoking the > shell (after replacing the {} with the name of a file). > > Alternatively, you could translate it as: > > ? ? "if [ -f '%s/{}' ]; then md5sum '%s/{}'; fi" % (mydir, mydir) > > ; that might make the intent clearer to whoever gets to > maintain your code. Yes to both points; turns out that my problem was in building up the command sequence to subprocess.Popen() - when to use, and not use, quotes etc. It has ended up as (spelled out in longhand...) xargsproc = ['xargs'] xargsproc.append('-I') xargsproc.append("{}") xargsproc.append('sh') xargsproc.append('-c') xargsproc.append("test -f %s/{} && md5sum %s/{}" % (mydir, mydir)) As usual, breaking it all down for the purposes of clarification has helpd a lot, as has your input. Thanks a lot. Cheers Jon N From jkn_gg at nicorp.f9.co.uk Mon Nov 12 12:25:01 2012 From: jkn_gg at nicorp.f9.co.uk (jkn) Date: Mon, 12 Nov 2012 09:25:01 -0800 (PST) Subject: how to simulate tar filename substitution across piped subprocess.Popen() calls? References: <96f4ba8b-c520-4ae4-a0ee-73365300bb9c@googlegroups.com> Message-ID: <10544126-b0cb-49fe-8934-eb19f98fa7b1@r7g2000vbo.googlegroups.com> On Nov 12, 4:58?pm, Rebelo wrote: > Dana ?etvrtak, 8. studenoga 2012. 19:05:12 UTC+1, korisnik jkn napisao je: > > > Hi All > > > ? ? i am trying to build up a set of subprocess.Ponen calls to > > > replicate the effect of a horribly long shell command. I'm not clear > > > how I can do one part of this and wonder if anyone can advise. I'm on > > > Linux, fairly obviously. > > > ? ? J^n > > You should try to do it in pure python, avoiding shell altogether. > The first step would be to actually write what it is you want to do. > Hi Rebelo FWIW I intend to do exactly this - but I wanted to duplicate the existing shell action beforehand, so that I could get rid of the shell command. After I've tidied things up, that will be my next step. Cheers Jon N From __peter__ at web.de Mon Nov 12 12:50:25 2012 From: __peter__ at web.de (Peter Otten) Date: Mon, 12 Nov 2012 18:50:25 +0100 Subject: Read number of CSV files References: Message-ID: Peter Otten wrote: [please don't email me directly] > How is using glob different from os.listdir() Peter? glob retains the path and allows you to filter the files. Compare: >>> import os, glob >>> os.listdir("alpha") ['one.py', 'two.py', 'one.txt', 'three.py', 'three.txt', 'two.txt'] >>> glob.glob("alpha/*") ['alpha/one.py', 'alpha/two.py', 'alpha/one.txt', 'alpha/three.py', 'alpha/three.txt', 'alpha/two.txt'] >>> glob.glob("alpha/*.py") ['alpha/one.py', 'alpha/two.py', 'alpha/three.py'] See the documentation for more. From hansmu at xs4all.nl Mon Nov 12 13:30:05 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Mon, 12 Nov 2012 19:30:05 +0100 Subject: how to simulate tar filename substitution across piped subprocess.Popen() calls? In-Reply-To: References: <509c588a$0$6904$e4fe514c@news2.news.xs4all.nl> <5b39febc-fbb9-4d8e-b331-0401e78d2b7f@m13g2000vbd.googlegroups.com> <50a12560$0$6972$e4fe514c@news2.news.xs4all.nl> Message-ID: <50a1402d$0$6947$e4fe514c@news2.news.xs4all.nl> On 12/11/12 18:22:44, jkn wrote: > Hi Hans > > On Nov 12, 4:36 pm, Hans Mulder wrote: >> On 12/11/12 16:36:58, jkn wrote: >> >> >> >> >> >> >> >> >> >>> slight followup ... >> >>> I have made some progress; for now I'm using subprocess.communicate to >>> read the output from the first subprocess, then writing it into the >>> secodn subprocess. This way I at least get to see what is >>> happening ... >> >>> The reason 'we' weren't seeing any output from the second call (the >>> 'xargs') is that as mentioned I had simplified this. The actual shell >>> command was more like (in python-speak): >> >>> "xargs -I {} sh -c \"test -f %s/{} && md5sum %s/{}\"" % (mydir, mydir) >> >>> ie. I am running md5sum on each tar-file entry which passes the 'is >>> this a file' test. >> >>> My next problem; how to translate the command-string clause >> >>> "test -f %s/{} && md5sum %s/{}" # ... >> >>> into s parameter to subprocss.Popen(). I think it's the command >>> chaining '&&' which is tripping me up... >> >> It is not really necessary to translate the '&&': you can >> just write: >> >> "test -f '%s/{}' && md5sum '%s/{}'" % (mydir, mydir) >> >> , and xargs will pass that to the shell, and then the shell >> will interpret the '&&' for you: you have shell=False in your >> subprocess.Popen call, but the arguments to xargs are -I {} >> sh -c "....", and this means that xargs ends up invoking the >> shell (after replacing the {} with the name of a file). >> >> Alternatively, you could translate it as: >> >> "if [ -f '%s/{}' ]; then md5sum '%s/{}'; fi" % (mydir, mydir) >> >> ; that might make the intent clearer to whoever gets to >> maintain your code. > > Yes to both points; turns out that my problem was in building up the > command sequence to subprocess.Popen() - when to use, and not use, > quotes etc. It has ended up as (spelled out in longhand...) > > > xargsproc = ['xargs'] > > xargsproc.append('-I') > xargsproc.append("{}") > > xargsproc.append('sh') > xargsproc.append('-c') > > xargsproc.append("test -f %s/{} && md5sum %s/{}" % (mydir, > mydir)) This will break if there are spaces in the file name, or other characters meaningful to the shell. If you change if to xargsproc.append("test -f '%s/{}' && md5sum '%s/{}'" % (mydir, mydir)) , then it will only break if there are single quotes in the file name. As I understand, your plan is to rewrite this bit in pure Python, to get rid of any and all such problems. > As usual, breaking it all down for the purposes of clarification has > helpd a lot, as has your input. Thanks a lot. You're welcome. -- HansM From smaran.harihar at gmail.com Mon Nov 12 13:32:43 2012 From: smaran.harihar at gmail.com (Smaran Harihar) Date: Mon, 12 Nov 2012 11:32:43 -0700 Subject: Read number of CSV files In-Reply-To: References: Message-ID: Oh that is great. [apology on sending the mail directly] Thanks, Smaran On Mon, Nov 12, 2012 at 10:50 AM, Peter Otten <__peter__ at web.de> wrote: > Peter Otten wrote: > > [please don't email me directly] > > > How is using glob different from os.listdir() Peter? > > glob retains the path and allows you to filter the files. Compare: > > >>> import os, glob > >>> os.listdir("alpha") > ['one.py', 'two.py', 'one.txt', 'three.py', 'three.txt', 'two.txt'] > >>> glob.glob("alpha/*") > ['alpha/one.py', 'alpha/two.py', 'alpha/one.txt', 'alpha/three.py', > 'alpha/three.txt', 'alpha/two.txt'] > >>> glob.glob("alpha/*.py") > ['alpha/one.py', 'alpha/two.py', 'alpha/three.py'] > > See the documentation for more. > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Thanks & Regards Smaran Harihar -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjreedy at udel.edu Mon Nov 12 15:41:35 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 12 Nov 2012 15:41:35 -0500 Subject: Is there a way to creat a func that returns a cursor that can be used? In-Reply-To: References: <05f7724a-1e41-43ea-b837-88e476917f9a@googlegroups.com> Message-ID: On 11/12/2012 7:25 AM, Joel Goldstick wrote: > Chris gave you the same help that you got yesterday. ... > go to ://python.org> and read the tutorials, > specifically about functions. It is hard to see what is working and not with an empty database. But to drive the point home, running import sqlite3 def connect(): conn = sqlite3.connect(':memory:')#use sch3.db or sch4.db .... etc. cur = conn.cursor() cur.execute("create table schedule (teams integer, sn integer, badge integer ,name text, grp integer,\ major text, track text, stage text, tc text, subject text, course text, ws text, date text, \ time text, proctor text, code text, no integer,fl_time text, flag2 text, flag3 text,\ flag4 text, clash1 integer, clash2 integer)") return cur cur = connect() print(cur.fetchone()) cur.execute("select * from schedule") print(cur.fetchall()) # prints None [] So the change suggested by multiple people *does* work ;-). On a different note: use triple quote for multi-line strings and backslashes are not needed. cur.execute('''create table schedule (teams integer, sn integer, badge integer ,name text, grp integer, major text, track text, stage text, tc text, subject text, course text, ws text, date text, time text, proctor text, code text, no integer,fl_time text, flag2 text, flag3 text, flag4 text, clash1 integer, clash2 integer)''') works fine. SQL treats newlines in statements as whitespace. -- Terry Jan Reedy From antti.ylikoski at elisanet.fi Mon Nov 12 16:02:36 2012 From: antti.ylikoski at elisanet.fi (Juhani Ylikoski) Date: Mon, 12 Nov 2012 23:02:36 +0200 Subject: Writing Donald E. Knuth based code in Python, cont'd Message-ID: Following comes a working, debugged Python program which computes the permutations of the integers 1, 2, 3 - n after Donald E. Knuth. I present it as an example of writing straightforward, easy Knuth-based code in a language with no GOTO statement. The Python program has been written after the DFA construct I previously discussed in this newsgroup, and after Knuth's discussion of the solution of the problem; and according the (very good) discussions in this newsgroup. To my opinion, it no more is a "crow's nest" as they say in Finnish. This program was needed for a real problem, namely computing optimal tournament tables for a Bughouse (Tandem) chess tournament. See http://en.wikipedia.org/wiki/Bughouse_chess Knuth became criticized in the newsgroup; but to my opinion his books are still useful and nontrivially needed. ---------------------------------------------------------------------- class DFA(object): # Iteratively generate all permutations of n integers 1-n. # After Donald Knuth, The Art of Computer Programming, Vol4, Fascicle 2, # ISBN 0-201-85393-0, on Pages 39-40. def __init__(self, n): self.n = n self.listofPerm = [] # list of lists to collect permutations self.nextStat = self.E1 # next phase in Knuth's text self.a = list(range(0, n+1)) # [0, 1, 2, 3, 4, ..., n] -- see Knuth def E1(self): # Phase 1 in Knuth's text self.app = self.listofPerm.append(self.a[1:self.n+1]) return self.E2 # next state: E2 def E2(self): # Phase 2 in Knuth's text self.j = self.n - 1 while self.a[self.j] >= self.a[self.j+1]: self.j -= 1 if self.j == 0: return None # algorithm finishes else: return self.E3 # next state: E3 def E3(self): # Phase 3 in Knuth self.l = self.n while self.a[self.j] >= self.a[self.l]: self.l -= 1 self.temp = self.a[self.j] self.a[self.j] = self.a[self.l] self.a[self.l] = self.temp return self.E4 # next state: E4 def E4(self): # Phase 4 self.k = self.j + 1 self.l = self.n while self.k < self.l: self.temp = self.a[self.k] self.a[self.k] = self.a[self.l] self.a[self.l] = self.temp self.k += 1 self.l -= 1 return self.E1 # following phase: Phase 1 def runDFA(self): self.nextState = self.E1 while self.nextState is not None: self.nextState = self.nextState() return(self.listofPerm) ---------------------------------------------------------------------- yours sincerely, Antti J Ylikoski Helsinki, Finland PhD student in the Aalto University From tjreedy at udel.edu Mon Nov 12 16:35:32 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 12 Nov 2012 16:35:32 -0500 Subject: Bugs: Content-Length not updated by reused urllib.request.Request / has_header() case-sensitive In-Reply-To: <50a12949$0$6566$9b4e6d93@newsspool3.arcor-online.net> References: <50a12949$0$6566$9b4e6d93@newsspool3.arcor-online.net> Message-ID: On 11/12/2012 10:52 AM, Johannes Kleese wrote: > Hi! > > (Yes, I did take a look at the issue tracker but couldn't find any > corresponding bug, and no, I don't want to open a new account just for > this one.) You only have to open a tracker account just once. I am reluctant to report this myself as I do not use the module and cannot answer questions. > I'm reusing a single urllib.request.Request object to HTTP-POST data to > the same URL a number of times. While the data itself is sent as > expected every time, the Content-Length header is not updated after the > first request. Tested with Python 3.1.3 and Python 3.1.4. 3.1 only gets security fixes. Consider upgrading. In any case, suspected bugs need to be tested with the latest release, as patches get applied daily. As it happens, import urllib.request opener = urllib.request.build_opener() request = urllib.request.Request("http://example.com/", headers = {"Content-Type": "application/x-www-form-urlencoded"}) opener.open(request, "1".encode("us-ascii")) print(request.data, '\n', request.header_items()) opener.open(request, "123456789".encode("us-ascii")) print(request.data, '\n', request.header_items()) exhibits the same behavior in 3.3.0 of printing ('Content-length', '1') in the last output. I agree that that looks wrong, but I do not know if such re-use is supposed to be supported. > While at it, I noticed that urllib.request.Request.has_header() and > .get_header() are case-sensitive, Python is case sensitive. > while HTTP headers are not (RFC 2616, 4.2). > Thus the following, slightly unfortunate behaviour: > >>>> request.header_items() > [('Content-length', '1'), ('Content-type', > 'application/x-www-form-urlencoded'), ('Host', 'example.com'), > ('User-agent', 'Python-urllib/3.1')] > >>>> request.has_header("Content-Type") > False >>>> request.has_header("Content-type") > True >>>> request.get_header("Content-Type") # this return None, which is not printed >>>> request.get_header("Content-type") > 'application/x-www-form-urlencoded' Judging from 'Content-type', 'User-agent', 'Content-length', 'Host', urllib.request consistently capitalizes the first word of all header tags and expects them in that form. If that is not standard, it should be documented. -- Terry Jan Reedy From jkn_gg at nicorp.f9.co.uk Mon Nov 12 16:43:56 2012 From: jkn_gg at nicorp.f9.co.uk (jkn) Date: Mon, 12 Nov 2012 13:43:56 -0800 (PST) Subject: how to simulate tar filename substitution across piped subprocess.Popen() calls? References: <509c588a$0$6904$e4fe514c@news2.news.xs4all.nl> <5b39febc-fbb9-4d8e-b331-0401e78d2b7f@m13g2000vbd.googlegroups.com> <50a12560$0$6972$e4fe514c@news2.news.xs4all.nl> <50a1402d$0$6947$e4fe514c@news2.news.xs4all.nl> Message-ID: <328a120c-5e6d-4251-9773-a615c09d27ec@c20g2000vbz.googlegroups.com> Hi Hans [...] > > > ? ? ? ? xargsproc.append("test -f %s/{} && md5sum %s/{}" % (mydir, > > mydir)) > > This will break if there are spaces in the file name, or other > characters meaningful to the shell. ?If you change if to > > ? ? ? ? xargsproc.append("test -f '%s/{}' && md5sum '%s/{}'" > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?% (mydir, mydir)) > > , then it will only break if there are single quotes in the file name. Fair point. As it happens, I know that there are no 'unhelpful' characters in the filenames ... but it's still worth doing. > > As I understand, your plan is to rewrite this bit in pure Python, to > get rid of any and all such problems. Yep - as mentioned in another reply I wanted first to have something which duplicated the current action (which has taken longer than I expected), and then rework in a more pythonic way. Still, I've learned some things about the subprocess module, and also about the shell, so it's been far from wasted time. Regards Jon N From vincent.vandevyvre at swing.be Mon Nov 12 17:01:56 2012 From: vincent.vandevyvre at swing.be (Vincent Vande Vyvre) Date: Mon, 12 Nov 2012 23:01:56 +0100 Subject: Writing Donald E. Knuth based code in Python, cont'd In-Reply-To: References: Message-ID: <50A171D4.50604@swing.be> Le 12/11/12 22:02, Juhani Ylikoski a ?crit : > Following comes a working, debugged Python program which computes the > permutations of the integers 1, 2, 3 - n after Donald E. Knuth. I > present it as an example of writing straightforward, easy Knuth-based > code in a language with no GOTO statement. > > The Python program has been written after the DFA construct I > previously discussed in this newsgroup, and after Knuth's discussion > of the solution of the problem; and according the (very good) > discussions in this newsgroup. To my opinion, it no more is a "crow's > nest" as they say in Finnish. > > This program was needed for a real problem, namely computing optimal > tournament tables for a Bughouse (Tandem) chess tournament. See > > http://en.wikipedia.org/wiki/Bughouse_chess > > Knuth became criticized in the newsgroup; but to my opinion his books > are still useful and nontrivially needed. > > > --- > > > yours sincerely, Antti J Ylikoski > Helsinki, Finland > PhD student in the Aalto University > Thanks, One comment in: def E1(self): # Phase 1 in Knuth's text self.app = self.listofPerm.append(self.a[1:self.n+1]) return self.E2 # next state: E2 append() return None and self.app is no longer used in the code. Missing something ? -- Vincent V.V. Oqapy . Qarte . PaQager From briankp at yahoo.com Mon Nov 12 17:07:29 2012 From: briankp at yahoo.com (Beekeeper2020) Date: Mon, 12 Nov 2012 14:07:29 -0800 (PST) Subject: f python? In-Reply-To: <4f8177e0$0$29983$c3e8da3$5496439d@news.astraweb.com> References: <5d729516-89d5-44b0-9916-fcea22b1a610@v7g2000pbs.googlegroups.com> <4f8177e0$0$29983$c3e8da3$5496439d@news.astraweb.com> Message-ID: <1352758049876-4995649.post@n6.nabble.com> I totally agreed about the Python syntax. Why do I need to worry about the syntax which wasted hours to get it to work? Brain dead python designer! Maybe Guido need to learn it from the Master, "Go to Ruby, and see how elegant the language is done. Also, it is stupid of google to hire Guido to prolong the death of Python. No wonder a lot of engineers left google because they don't pay enough. Vote for your feet, googlers. Python eventually will die once they found out Ruby don't have this dumb issues. Guido, you're doing a wonderful job, wasting software programmers on hours solving your Python Stupid Syntax ( Your dumb Global variable is another example). Google is too dumb to understand these issues. -- View this message in context: http://python.6.n6.nabble.com/f-python-tp4708177p4995649.html Sent from the Python - python-list mailing list archive at Nabble.com. From cantabile.03 at wanadoo.fr Mon Nov 12 17:25:12 2012 From: cantabile.03 at wanadoo.fr (Cantabile) Date: Mon, 12 Nov 2012 23:25:12 +0100 Subject: List comprehension for testing **params In-Reply-To: <6o07n9-hnd.ln1@satorlaser.homedns.org> References: <50a0258f$0$21241$ba4acef3@reader.news.orange.fr> <6o07n9-hnd.ln1@satorlaser.homedns.org> Message-ID: <50a17748$0$18053$ba4acef3@reader.news.orange.fr> Wow, lots of things I had never heard of in your posts. I guess I need to do some homework... Cantabile From nickj1706 at googlemail.com Mon Nov 12 17:26:54 2012 From: nickj1706 at googlemail.com (NJ1706) Date: Mon, 12 Nov 2012 14:26:54 -0800 (PST) Subject: Help building a dictionary of lists Message-ID: <9f44d1de-bccf-41ad-8529-ff8a65d7e2b4@googlegroups.com> Chaps, I am new to Python & have inherited a test harness written in the language that I am trying to extend. The following code shows how dictionaries holding lists of commands are handled in the script... >>> Start of Code_1 <<< #! /usr/bin/python # List of tests TestList = ( 'Test_1', 'Test_2' ) # Initialise the dictionary of lists dict1 = { 'Test_1' : [], 'Test_2' : [], } instances = ('1') # Loop through the list of tests for Test in TestList: print print "Test: ", Test # Append to the list for each instance for instance in instances: print " instance: ", instance # Initialise our string list str_l = [] # Build string list str_l.append ('ID %s' % Test) str_l.append (' instance %s' % instance) # Convert to string str = ''.join (str_l) print " str: ", str # Assign to target list dict1[Test].append('%s' % str) print " dict1: ", dict1[Test] >>> End of Code_1 <<< This code produces the following output >>> Start of Output_1 <<< Test: Test_1 instance: 1 str: ID Test_1 instance 1 dict1: ['ID Test_1 instance 1'] Test: Test_2 instance: 1 str: ID Test_2 instance 1 dict1: ['ID Test_2 instance 1'] # YYY >>> End of Output_1 <<< Note that dict1 contains only the details of the particlare test, see YYY. This is a very cut down script compared to the real thing & in reality there are many more entries in the TestList and also there are many dictionaries. To make the script simpler to extend I would like to remove the need to manually create each of the dictionaries. After some reading around I found the dict.fromkeys() method & came up with the following... >>> Start of Code_2 <<< #! /usr/bin/python TestList = ( 'Test_1', 'Test_2' ) dict2 = dict.fromkeys (TestList, []) instances = ('1') for Test in TestList: print print "Test: ", Test for instance in instances: print " instance: ", instance # Initialise our string list str_l = [] # Build string list str_l.append ('ID %s' % Test) str_l.append (' instance %s' % instance) # Convert to string str = ''.join (str_l) print " str: ", str # Assign to target list dict2[Test].append('%s' % str) print " dict2: ", dict2[Test] >>> End of Code_2 <<< This produces the following output >>> Start of Ouput_2 <<< Test: Test_1 instance: 1 str: ID Test_1 instance 1 dict2: ['ID Test_1 instance 1'] Test: Test_2 instance: 1 str: ID Test_2 instance 1 dict2: ['ID Test_1 instance 1', 'ID Test_2 instance 1'] # XXX >>> End of Ouput_2 <<< This almost does what I want but dict2[Test_2] displayed at XXX contains the value for Test_1 as well as Test_2. I would be very grateful if someone can help me to get the line marked with XXX to be the same as YYY in code_1 at the start. I am using Python 2.6.8 on Cygwin 1.7.17 but get the same results on CentOS 6.3 From d at davea.name Mon Nov 12 17:37:50 2012 From: d at davea.name (Dave Angel) Date: Mon, 12 Nov 2012 17:37:50 -0500 Subject: f python? In-Reply-To: <1352758049876-4995649.post@n6.nabble.com> References: <5d729516-89d5-44b0-9916-fcea22b1a610@v7g2000pbs.googlegroups.com> <4f8177e0$0$29983$c3e8da3$5496439d@news.astraweb.com> <1352758049876-4995649.post@n6.nabble.com> Message-ID: <50A17A3E.2090409@davea.name> On 11/12/2012 05:07 PM, Beekeeper2020 wrote: > I totally agreed about the Python syntax. Why do I need to worry about the > syntax which wasted hours to get it to work? > Brain dead python designer! Maybe Guido need to learn it from the Master, > "Go to Ruby, and see how elegant the language is done. Also, it is stupid > of google to hire Guido to prolong the death of Python. No wonder a lot of > engineers left google because they don't pay enough. Vote for your feet, > googlers. > > Python eventually will die once they found out Ruby don't have this dumb > issues. Guido, you're doing a wonderful job, wasting software programmers > on hours solving your Python Stupid Syntax ( Your dumb Global variable is > another example). > > Google is too dumb to understand these issues. > > In case anybody is tempted to respond to this troll message, notice it's a first-time poster, and it replies to a 7-month old message, and gives no context. Useless. -- DaveA From steve+comp.lang.python at pearwood.info Mon Nov 12 17:48:28 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 12 Nov 2012 22:48:28 GMT Subject: f python? References: <5d729516-89d5-44b0-9916-fcea22b1a610@v7g2000pbs.googlegroups.com> <4f8177e0$0$29983$c3e8da3$5496439d@news.astraweb.com> <1352758049876-4995649.post@n6.nabble.com> Message-ID: <50a17cbc$0$29999$c3e8da3$5496439d@news.astraweb.com> On Mon, 12 Nov 2012 17:37:50 -0500, Dave Angel wrote: > On 11/12/2012 05:07 PM, Beekeeper2020 wrote: [...] >> Python eventually will die once troll troll troll troll troll... > In case anybody is tempted to respond to this troll message, Like you did? Without trimming? :-P -- Steven From cs at zip.com.au Mon Nov 12 17:59:08 2012 From: cs at zip.com.au (Cameron Simpson) Date: Tue, 13 Nov 2012 09:59:08 +1100 Subject: Bugs: Content-Length not updated by reused urllib.request.Request / has_header() case-sensitive In-Reply-To: References: Message-ID: <20121112225907.GA6983@cskk.homeip.net> On 12Nov2012 16:35, Terry Reedy wrote: | On 11/12/2012 10:52 AM, Johannes Kleese wrote: | > While at it, I noticed that urllib.request.Request.has_header() and | > .get_header() are case-sensitive, | | Python is case sensitive. But headers are not. I'd be very inclined to consider case sensitivity in has_header and get_header to be a bug, absent a compelling argument against it. -- Cameron Simpson When a man rides a Motorader he stays forever young. - German saying From joshua.landau.ws at gmail.com Mon Nov 12 18:41:59 2012 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Mon, 12 Nov 2012 23:41:59 +0000 Subject: Help building a dictionary of lists In-Reply-To: <9f44d1de-bccf-41ad-8529-ff8a65d7e2b4@googlegroups.com> References: <9f44d1de-bccf-41ad-8529-ff8a65d7e2b4@googlegroups.com> Message-ID: On 12 November 2012 22:26, NJ1706 wrote: > Chaps, > > I am new to Python & have inherited a test harness written in the language > that I am trying to extend. > > The following code shows how dictionaries holding lists of commands are > handled in the script... > > >>> Start of Code_1 <<< > >>> End of Output_1 <<< > > Note that dict1 contains only the details of the particlare test, see YYY. > > This is a very cut down script compared to the real thing & in reality > there are many more entries in the TestList and also there are many > dictionaries. To make the script simpler to extend I would like to remove > the need to manually create each of the dictionaries. > > After some reading around I found the dict.fromkeys() method & came up > with the following... > > >>> Start of Code_2 <<< >>> End of Ouput_2 <<< > > This almost does what I want but dict2[Test_2] displayed at XXX contains > the value for Test_1 as well as Test_2. I would be very grateful if someone > can help me to get the line marked with XXX to be the same as YYY in code_1 > at the start. > > I am using Python 2.6.8 on Cygwin 1.7.17 but get the same results on > CentOS 6.3 > First of all, thank you for being thorough. It's always a help. *However*, a minimalistic will be read faster usually, so you may want to crop down what you have. Additionally, it can result in you solving it yourself! The problem is as follows "{1:[], 2:[], 3:[]}" does not equal "dict.fromkeys([1, 2, 3], [])" You could have worked out this much very quickly, as it is the only thing you change in the code. Let's look: >>> {1:[], 2:[], 3:[]} {1: [], 2: [], 3: []} >>> dict.fromkeys([1,2,3], []) {1: [], 2: [], 3: []} >>> Oh no! They are the same! What has happened? Well, they're *not*. In the first example each of the lists are *unique*: >>> dct = {1:[], 2:[], 3:[]} >>> dct[1] is dct[2] False >>> dct[1] is dct[3] False >>> dct[2] is dct[3] False >>> In the second, they are all *the same list*: >>> dct = dict.fromkeys([1,2,3], []) >>> dct[1] is dct[2] True >>> dct[1] is dct[3] True >>> dct[2] is dct[3] True >>> What does this mean? >>> a = [] >>> b = [] >>> c = b >>> >>> a is b False >>> b is c True >>> >>> a.append(1) >>> b.append(2) >>> c.append(3) >>> >>> a [1] >>> b [2, 3] >>> c [2, 3] >>> Because b and c in this are *the same*, changing one *is* changing the other. Hence, you've appended *two* things to b and c, but only one to a. In your code you use .append on the items in your dict. This will have the same problem as with a, b and c above. If you want to make a dict, you can use a dictionary comprehension. I assume you understand what a list comprehension is. If not, look here: http://docs.python.org/2/tutorial/datastructures.html#list-comprehensions Dict comprehension: {i:[] for i in ["Test 1", "Test 2", "Test 3"]} -------------- next part -------------- An HTML attachment was scrubbed... URL: From joshua.landau.ws at gmail.com Mon Nov 12 19:04:16 2012 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Tue, 13 Nov 2012 00:04:16 +0000 Subject: Help building a dictionary of lists In-Reply-To: <9f44d1de-bccf-41ad-8529-ff8a65d7e2b4@googlegroups.com> References: <9f44d1de-bccf-41ad-8529-ff8a65d7e2b4@googlegroups.com> Message-ID: On 12 November 2012 22:26, NJ1706 wrote: > # List of tests > TestList = ( > 'Test_1', > 'Test_2' > ) Note that TestList is a *tuple*, not a list. You normally would want to write "test_names" instead of "TestList" for several reasons: * Unless it's a class, Python prefers lowercase_names_with_underscores (or sometimes camelCaseNamesThatLookLikeThis) * Python tries not to care what type you have - that your names are in a list is less important than that you can loop over it * It's definitely not a list. A list would look like this: TestList = [ 'Test_1', 'Test_2' ] Additionally, your comment here is somewhat pointless. The code says *exactly* what your comment does, so why write it twice? # List of tests Don't need # Initialise the dictionary of lists Don't need # Loop through the list of tests Don't need # Append to the list for each instance Not very helpful. If you comment here, you should explain *why* you are looping, not *that* you are looping. You write "append", but *what* and, more importantly,*why*? # Initialise our string list Don't need # Build string list Again, whilst it is nice to know more, you are not saying *what* you are building. What is the *purpose* of the list? # Convert to string Don't need # Assign to target list Don't need A good comment might just be one at the start that explains the goal, or one inside that explains the *point* of a piece of code. Consider these two commented pieces of code: # Define a function that takes a, b and c def f(a, b, c): # Set s to ((the square of b) minus four times a times c) all to the power of 0.5 s = (B**2 - 4 * a * c) ** 0.5 # Return the calculated values return (- b + s) / (2 * a), (- b - s) / (2 * a) Vs. # Finds x where the equation "ax**2 + bx + c" makes 0 def f(a, b, c): # Use the quadratic equation ( http://en.wikipedia.org/wiki/Quadratic_equation) # s is the square root part of the equation (where x**0.5 is the square root of x) s = (B**2 - 4 * a * c) ** 0.5 # Return two values because there are two possible solutions return (- b + s) / (2 * a), (- b - s) / (2 * a) -------------- next part -------------- An HTML attachment was scrubbed... URL: From cleuson.o at gmail.com Mon Nov 12 20:00:27 2012 From: cleuson.o at gmail.com (Cleuson Alves) Date: Mon, 12 Nov 2012 17:00:27 -0800 (PST) Subject: Division matrix Message-ID: <98b451e1-5cd5-46e9-8be4-59dcc835700b@googlegroups.com> Hello, I need to solve an exercise follows, first calculate the inverse matrix and then multiply the first matrix. I await help. Thank you. follows the code below incomplete. m = [[1,2,3],[4,5,6],[7,8,9]] x = [] for i in [0,1,2]: y = [] for linha in m: y.append(linha[i]) x.append(y) print x [[1, 4, 7], [2, 5, 8], [3, 6, 9]] def ProdMatrix(x,b): tamL = len(x) tamC = len(x[0]) c = nullMatrix(tamL,tamC) for i in range(tamL): for j in range(tamC): val = 0 for k in range(len(b)): val = val + x[i][l]*b[k][j] c[i][j] return c From ian.g.kelly at gmail.com Mon Nov 12 20:25:33 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 12 Nov 2012 18:25:33 -0700 Subject: Division matrix In-Reply-To: <98b451e1-5cd5-46e9-8be4-59dcc835700b@googlegroups.com> References: <98b451e1-5cd5-46e9-8be4-59dcc835700b@googlegroups.com> Message-ID: On Mon, Nov 12, 2012 at 6:00 PM, Cleuson Alves wrote: > Hello, I need to solve an exercise follows, first calculate the inverse matrix and then multiply the first matrix. > I await help. > Thank you. > follows the code below incomplete. So what is the specific problem with the code that you're looking for help with? > m = [[1,2,3],[4,5,6],[7,8,9]] > x = [] > for i in [0,1,2]: > y = [] > for linha in m: > y.append(linha[i]) > x.append(y) > > print x > [[1, 4, 7], [2, 5, 8], [3, 6, 9]] This calculates the transpose of the matrix, not the inverse. If the inverse is really what you're after, you should start by reading up on the math to compute that. Note that "for i in [0,1,2]:" could be more generally written as "for i in range(len(m[0])):", and then you don't need to rewrite your for loop if the dimensions of m change. If you actually do want the transpose, then I'll also mention in passing that there is a very nice one-liner using zip() to do this. I'll leave the details as an exercise. ;-) > def ProdMatrix(x,b): > tamL = len(x) > tamC = len(x[0]) > c = nullMatrix(tamL,tamC) > for i in range(tamL): > for j in range(tamC): > val = 0 > for k in range(len(b)): > val = val + x[i][l]*b[k][j] > c[i][j] > return c In the multiplication line you're using the variable 'l' as an index, but you haven't defined it. Probably you wanted 'k' here. You're also discarding 'val' after adding it up. If you want that value in the 'c' matrix, then you need to store it there before going on to the next loop. From tjreedy at udel.edu Mon Nov 12 20:41:36 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 12 Nov 2012 20:41:36 -0500 Subject: Bugs: Content-Length not updated by reused urllib.request.Request / has_header() case-sensitive In-Reply-To: <20121112225907.GA6983@cskk.homeip.net> References: <20121112225907.GA6983@cskk.homeip.net> Message-ID: On 11/12/2012 5:59 PM, Cameron Simpson wrote: > On 12Nov2012 16:35, Terry Reedy wrote: > | On 11/12/2012 10:52 AM, Johannes Kleese wrote: > | > While at it, I noticed that urllib.request.Request.has_header() and > | > .get_header() are case-sensitive, > | > | Python is case sensitive. To be more precise, Python string comparisons are by codepoints. If one wants normalized comparison, one usually has to do do the normalization oneself. > But headers are not. I'd be very inclined to consider case sensitivity > in has_header and get_header to be a bug, absent a compelling argument > against it. It appears that the behavior is not consistent with doc examples and other header name handling. I added a note to http://bugs.python.org/issue12455 -- Terry Jan Reedy From tjreedy at udel.edu Mon Nov 12 20:58:47 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 12 Nov 2012 20:58:47 -0500 Subject: Bugs: Content-Length not updated by reused urllib.request.Request / has_header() case-sensitive In-Reply-To: References: <50a12949$0$6566$9b4e6d93@newsspool3.arcor-online.net> Message-ID: On 11/12/2012 4:35 PM, Terry Reedy wrote: > import urllib.request > opener = urllib.request.build_opener() > request = urllib.request.Request("http://example.com/", headers = > {"Content-Type": "application/x-www-form-urlencoded"}) > > opener.open(request, "1".encode("us-ascii")) > print(request.data, '\n', request.header_items()) > > opener.open(request, "123456789".encode("us-ascii")) > print(request.data, '\n', request.header_items()) > > exhibits the same behavior in 3.3.0 of printing ('Content-length', '1') > in the last output. I agree that that looks wrong, but I do not know if > such re-use is supposed to be supported. I opened http://bugs.python.org/issue16464 -- Terry Jan Reedy From joshua.landau.ws at gmail.com Mon Nov 12 20:59:19 2012 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Tue, 13 Nov 2012 01:59:19 +0000 Subject: Division matrix In-Reply-To: <98b451e1-5cd5-46e9-8be4-59dcc835700b@googlegroups.com> References: <98b451e1-5cd5-46e9-8be4-59dcc835700b@googlegroups.com> Message-ID: On 13 November 2012 01:00, Cleuson Alves wrote: > Hello, I need to solve an exercise follows, first calculate the inverse > matrix and then multiply the first matrix. > This list isn't to give answers for homeworks, and this sounds like one. We *do* give help to those who have a specific problem and who preferably show that they are trying to help up help them. > I await help. > For what?! You haven't asked a question. 1) What do you need. Answer precisely, preferably giving an example output for the input you want 2) What techniques have you tried? You have given code below in an extremely un-obvious manner. What does it do, crash? Is it the wrong output? What is wrong about the output? 3) What do you *think* has gone wrong? What about the output seems to be wrong? > Thank you. > follows the code below incomplete. > On the basis that we can get some foreign people who maybe don't have English as a commonly used language, muddled grammar isn't that big deal. However, when asking for help it is worth checking that you've asked in a clear way. > m = [[1,2,3],[4,5,6],[7,8,9]] > x = [] > for i in [0,1,2]: > y = [] > for linha in m: > y.append(linha[i]) > x.append(y) > > print x > [[1, 4, 7], [2, 5, 8], [3, 6, 9]] > Is this the right output? Is this what you *meant* by inverse? As Ian Kelly (who is undoubtedly more learned in this area) said, this is a transpose, so you have just swapped the one axis with another. > def ProdMatrix(x,b): > tamL = len(x) > tamC = len(x[0]) > c = nullMatrix(tamL,tamC) > for i in range(tamL): > for j in range(tamC): > val = 0 > for k in range(len(b)): > val = val + x[i][l]*b[k][j] > c[i][j] > return c > You haven't given the full code. This crashes because we don't have "nullMatrix" defined. It would be nice if we had something we could try to run. Then, the error is: "NameError: global name 'l' is not defined". On the line "val = val + x[i][l]*b[k][j]" you use a variable "l" which doesn't exist. What should you be using, or should you have created it? *Then* you get an output of pure 0s. This is because you forgot to put your result into c, your new matrix. You probably just forgot to finish the line "c[i][j]", which does nothing now. You're actually really close on the second part. I have no idea if you're doing what you want for the first part, but it's not what's traditionally called an inverse. -------------- next part -------------- An HTML attachment was scrubbed... URL: From joyhou2010 at gmail.com Mon Nov 12 21:02:23 2012 From: joyhou2010 at gmail.com (Caroline Hou) Date: Mon, 12 Nov 2012 18:02:23 -0800 (PST) Subject: Simple Question regarding running .py program Message-ID: Hi all! I just started learning Python by myself and I have an extremely simple question now! I am in my Python interpreter now and I want to open/edit a program called nobel.py. But when I typed >>> python nobel.py, it gave me a "SyntaxError:invalid syntax?( I've changed to the correct directory)what should I do? I also want to run the program, but as I double-clicked the program, a command window pops up and closes immediately. How can I see the result of the program run? Could anyone help me please? I am pretty confused here...Thank you! From d at davea.name Mon Nov 12 21:24:38 2012 From: d at davea.name (Dave Angel) Date: Mon, 12 Nov 2012 21:24:38 -0500 Subject: Simple Question regarding running .py program In-Reply-To: References: Message-ID: <50A1AF66.2030202@davea.name> On 11/12/2012 09:02 PM, Caroline Hou wrote: > Hi all! > > I just started learning Python by myself and I have an extremely simple question now! > I am in my Python interpreter now and I want to open/edit a program called nobel.py. But when I typed >>> python nobel.py, it gave me a "SyntaxError:invalid syntax?( I've changed to the correct directory)what should I do? > I also want to run the program, but as I double-clicked the program, a command window pops up and closes immediately. How can I see the result of the program run? > Could anyone help me please? I am pretty confused here...Thank you! It'd be nice to specify that you're running Windows, and also what version of the interpreter, although in this case the latter doesn't matter. Go to a shell (cmd.exe), change to the directory containing that script, and type the command as you did. On linux: davea at think:~$ python nobel.py On Windows: c:\mydir\myscript > python nobel.py If you're already in the python interpreter, then running python is useless -- it's already running. In that case, you might want to use import. However, I recommend against it at first, as it opens up some other problems you haven't experience with yet. When you say you "double clicked the program', we have to guess you might have meant in MS Explorer. If you do that, it launches a cmd, it runs the python system, and it closes the cmd. Blame Windows for not reading your mind. If you want the cmd window to stick around, you COULD end your program with an raw_input function call, but frequently that won't work. The right answer is the first one above... Open a shell (perhaps with a menu like DOS BOX), change... That way, when the program finishes, you can see what happened, or didn't happen, and you can run it again using the uparrow key. BTW, you don't need to send email to both the python-list and to the newsgroup. The newsgroup is automatically fed from the list. But since you're posting from google groups, that's just one of the bugs. Many folks here simply filter out everything from google groups, so your post is invisible to them. -- DaveA From joyhou2010 at gmail.com Mon Nov 12 21:45:45 2012 From: joyhou2010 at gmail.com (Caroline Hou) Date: Mon, 12 Nov 2012 18:45:45 -0800 (PST) Subject: Simple Question regarding running .py program In-Reply-To: References: Message-ID: <22ae6963-6700-4ea7-b396-344d89ead7f2@googlegroups.com> On Monday, 12 November 2012 21:25:08 UTC-5, Dave Angel wrote: > On 11/12/2012 09:02 PM, Caroline Hou wrote: > > > Hi all! > > > > > > I just started learning Python by myself and I have an extremely simple question now! > > > I am in my Python interpreter now and I want to open/edit a program called nobel.py. But when I typed >>> python nobel.py, it gave me a "SyntaxError:invalid syntax?( I've changed to the correct directory)what should I do? > > > I also want to run the program, but as I double-clicked the program, a command window pops up and closes immediately. How can I see the result of the program run? > > > Could anyone help me please? I am pretty confused here...Thank you! > > > > It'd be nice to specify that you're running Windows, and also what > > version of the interpreter, although in this case the latter doesn't matter. > > > > > > Go to a shell (cmd.exe), change to the directory containing that script, > > and type the command as you did. > > > > On linux: davea at think:~$ python nobel.py > > On Windows: c:\mydir\myscript > python nobel.py > > > > If you're already in the python interpreter, then running python is > > useless -- it's already running. In that case, you might want to use > > import. However, I recommend against it at first, as it opens up some > > other problems you haven't experience with yet. > > > > When you say you "double clicked the program', we have to guess you > > might have meant in MS Explorer. If you do that, it launches a cmd, it > > runs the python system, and it closes the cmd. Blame Windows for not > > reading your mind. If you want the cmd window to stick around, you > > COULD end your program with an raw_input function call, but frequently > > that won't work. The right answer is the first one above... Open a > > shell (perhaps with a menu like DOS BOX), change... > > > > That way, when the program finishes, you can see what happened, or > > didn't happen, and you can run it again using the uparrow key. > > > > BTW, you don't need to send email to both the python-list and to the > > newsgroup. The newsgroup is automatically fed from the list. But since > > you're posting from google groups, that's just one of the bugs. Many > > folks here simply filter out everything from google groups, so your post > > is invisible to them. > > > > > > -- > > > > DaveA Hi Dave! thank you very much for your quick reply! I did manage to get the program run from cmd.exe. So does it mean that if I want to use python interactively,I should use the interpreter,while if I just want to run a python program, I should use DOS shell instead? Also, how could I edit my script? I have sth called "IDLE" installed along with python. Is it the right place to write/edit my script? Sorry about these semi-idiot questions but it is really hard to find an article or book that covers such basic stuffs! Thank you! Caroline Hou From joyhou2010 at gmail.com Mon Nov 12 21:45:45 2012 From: joyhou2010 at gmail.com (Caroline Hou) Date: Mon, 12 Nov 2012 18:45:45 -0800 (PST) Subject: Simple Question regarding running .py program In-Reply-To: References: Message-ID: <22ae6963-6700-4ea7-b396-344d89ead7f2@googlegroups.com> On Monday, 12 November 2012 21:25:08 UTC-5, Dave Angel wrote: > On 11/12/2012 09:02 PM, Caroline Hou wrote: > > > Hi all! > > > > > > I just started learning Python by myself and I have an extremely simple question now! > > > I am in my Python interpreter now and I want to open/edit a program called nobel.py. But when I typed >>> python nobel.py, it gave me a "SyntaxError:invalid syntax?( I've changed to the correct directory)what should I do? > > > I also want to run the program, but as I double-clicked the program, a command window pops up and closes immediately. How can I see the result of the program run? > > > Could anyone help me please? I am pretty confused here...Thank you! > > > > It'd be nice to specify that you're running Windows, and also what > > version of the interpreter, although in this case the latter doesn't matter. > > > > > > Go to a shell (cmd.exe), change to the directory containing that script, > > and type the command as you did. > > > > On linux: davea at think:~$ python nobel.py > > On Windows: c:\mydir\myscript > python nobel.py > > > > If you're already in the python interpreter, then running python is > > useless -- it's already running. In that case, you might want to use > > import. However, I recommend against it at first, as it opens up some > > other problems you haven't experience with yet. > > > > When you say you "double clicked the program', we have to guess you > > might have meant in MS Explorer. If you do that, it launches a cmd, it > > runs the python system, and it closes the cmd. Blame Windows for not > > reading your mind. If you want the cmd window to stick around, you > > COULD end your program with an raw_input function call, but frequently > > that won't work. The right answer is the first one above... Open a > > shell (perhaps with a menu like DOS BOX), change... > > > > That way, when the program finishes, you can see what happened, or > > didn't happen, and you can run it again using the uparrow key. > > > > BTW, you don't need to send email to both the python-list and to the > > newsgroup. The newsgroup is automatically fed from the list. But since > > you're posting from google groups, that's just one of the bugs. Many > > folks here simply filter out everything from google groups, so your post > > is invisible to them. > > > > > > -- > > > > DaveA Hi Dave! thank you very much for your quick reply! I did manage to get the program run from cmd.exe. So does it mean that if I want to use python interactively,I should use the interpreter,while if I just want to run a python program, I should use DOS shell instead? Also, how could I edit my script? I have sth called "IDLE" installed along with python. Is it the right place to write/edit my script? Sorry about these semi-idiot questions but it is really hard to find an article or book that covers such basic stuffs! Thank you! Caroline Hou From breamoreboy at yahoo.co.uk Mon Nov 12 22:08:54 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 13 Nov 2012 03:08:54 +0000 Subject: stackoverflow quote on Python Message-ID: http://stackoverflow.com/questions/tagged/python "Python has two major versions (2 and 3) in use which have significant differences." I believe that this is incorrect. The warts have been removed, but significant differences, not in my book. If there is agreement about there not being significant differences, should stackoverflow be asked to change their wording? -- Cheers. Mark Lawrence. From rodrick.brown at gmail.com Mon Nov 12 22:17:32 2012 From: rodrick.brown at gmail.com (Rodrick Brown) Date: Mon, 12 Nov 2012 22:17:32 -0500 Subject: stackoverflow quote on Python In-Reply-To: References: Message-ID: I believe this statement is correct given key differences do exist in underlying implementations even though such differences may be highly transparent to end users (developers). On Mon, Nov 12, 2012 at 10:08 PM, Mark Lawrence wrote: > http://stackoverflow.com/**questions/tagged/python > > "Python has two major versions (2 and 3) in use which have significant > differences." > > I believe that this is incorrect. The warts have been removed, but > significant differences, not in my book. If there is agreement about there > not being significant differences, should stackoverflow be asked to change > their wording? > > -- > Cheers. > > Mark Lawrence. > > -- > http://mail.python.org/**mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From rustompmody at gmail.com Mon Nov 12 23:14:04 2012 From: rustompmody at gmail.com (rusi) Date: Mon, 12 Nov 2012 20:14:04 -0800 (PST) Subject: A gnarly little python loop References: Message-ID: On Nov 12, 9:09?pm, Steve Howell wrote: > On Nov 12, 7:21?am, rusi wrote: > > > On Nov 12, 12:09?pm, rusi wrote:> This is a classic problem -- structure clash of parallel loops > > > > > > Sorry wrong solution :D > > > The fidgetiness is entirely due to python not allowing C-style loops > > like these: > > > >> while ((c=getchar()!= EOF) { ... } > > [...] > > There are actually three fidgety things going on: > > ?1. The API is 1-based instead of 0-based. > ?2. You don't know the number of pages in advance. > ?3. You want to process tweets, not pages of tweets. > > Here's yet another take on the problem: > > ? ? # wrap fidgety 1-based api > ? ? def search(i): > ? ? ? ? return api.GetSearch("foo", i+1) > > ? ? paged_tweets = (search(i) for i in count()) > > ? ? # handle sentinel > ? ? paged_tweets = iter(paged_tweets.next, []) > > ? ? # flatten pages > ? ? tweets = chain.from_iterable(paged_tweets) > ? ? for tweet in tweets: > ? ? ? ? process(tweet) [Steve Howell] Nice on the whole -- thanks Could not the 1-based-ness be dealt with by using count(1)? ie use paged_tweets = (api.GetSearch("foo", i) for i in count(1)) {Peter] > >>> while ((c=getchar()!= EOF) { ... } for c in iter(getchar, EOF): ... Thanks. Learnt something From ian.g.kelly at gmail.com Mon Nov 12 23:18:48 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 12 Nov 2012 21:18:48 -0700 Subject: stackoverflow quote on Python In-Reply-To: References: Message-ID: On Mon, Nov 12, 2012 at 8:08 PM, Mark Lawrence wrote: > http://stackoverflow.com/questions/tagged/python > > "Python has two major versions (2 and 3) in use which have significant > differences." > > I believe that this is incorrect. The warts have been removed, but > significant differences, not in my book. If there is agreement about there > not being significant differences, should stackoverflow be asked to change > their wording? They have differences, and those differences are significant in that they must often be taken into account when asking or answering questions about Python on stackoverflow. From steve+comp.lang.python at pearwood.info Tue Nov 13 00:42:18 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Nov 2012 05:42:18 GMT Subject: stackoverflow quote on Python References: Message-ID: <50a1ddba$0$21742$c3e8da3$76491128@news.astraweb.com> On Tue, 13 Nov 2012 03:08:54 +0000, Mark Lawrence wrote: > http://stackoverflow.com/questions/tagged/python > > "Python has two major versions (2 and 3) in use which have significant > differences." > > I believe that this is incorrect. The warts have been removed, but > significant differences, not in my book. If there is agreement about > there not being significant differences, should stackoverflow be asked > to change their wording? Define "significant". If StackOverflow mean "significant like the differences between Lisp and Cobol", then they are clearly wrong. On the other hand, if you are suggesting that nothing short of the differences between Lisp and Cobol count as significant, then I think you too are wrong. There are clear differences between the two versions, and the much-talked- about "print is now a function" is the least among them: * major reorganisation of parts of the standard library, with many libraries being removed, renamed, reorganised, or added; * file objects are completely re-implemented; * strings are now proper text strings (Unicode), not byte strings; * nonlocal; * keyword-only parameters for functions; * cannot use grouped parameters in functions, e.g. def spam(a, (b,c), d) no longer is allowed; * cannot use "import *" inside a function; * dict methods keys(), values(), items() are iterators; * so are map, reduce, zip; * builtins like reduce, reload have been moved to modules; * some itertools functions are now builtins; * sorted and list.sort no longer support comparison functions; * comparisons between different types may raise TypeError; * extended iterable unpacking; * function annotations; * dict comprehensions and set literals; * new metaclass syntax; * classic classes are gone; * automatic delegation doesn't work for __dunder__ methods; * backticks `x` gone; among others. Are these "significant" differences? Well, maybe. -- Steven From tjreedy at udel.edu Tue Nov 13 01:21:12 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 13 Nov 2012 01:21:12 -0500 Subject: Simple Question regarding running .py program In-Reply-To: <22ae6963-6700-4ea7-b396-344d89ead7f2@googlegroups.com> References: <22ae6963-6700-4ea7-b396-344d89ead7f2@googlegroups.com> Message-ID: On 11/12/2012 9:45 PM, Caroline Hou wrote: > Also, how could I edit my script? I have sth called "IDLE" installed > along with python. Is it the right place to write/edit my script? IDLE is one way to edit; I use it. When you want to run, hit F5 and stdout and stderr output goes to the shell window. -- Terry Jan Reedy From someukdeveloper at gmail.com Tue Nov 13 01:38:31 2012 From: someukdeveloper at gmail.com (Some Developer) Date: Tue, 13 Nov 2012 06:38:31 +0000 Subject: Getting module path string from a class instance Message-ID: <50A1EAE7.1090700@googlemail.com> I'm trying to find a way to get a string of the module path of a class. So for instance say I have class Foo and it is in a module called my.module. I want to be able to get a string that is equal to this: "my.module.Foo". I'm aware of the __repr__ method but it does not do what I want it to do in this case. Can anyone offer any advice at all? From anthra.norell at bluewin.ch Tue Nov 13 02:08:16 2012 From: anthra.norell at bluewin.ch (F.R.) Date: Tue, 13 Nov 2012 08:08:16 +0100 Subject: Strange object identity problem In-Reply-To: <50a12baa$0$12377$a8266bb1@newsreader.readnews.com> References: <50A0F5A8.4080802@bluewin.ch> <50a12baa$0$12377$a8266bb1@newsreader.readnews.com> Message-ID: <50A1F1E0.3030401@bluewin.ch> On 11/12/2012 06:02 PM, duncan smith wrote: > On 12/11/12 13:40, F.R. wrote: >> On 11/12/2012 02:27 PM, Robert Franke wrote: >>> Hi Frederic, >>> >>> [...] >>> >>>>>>> bas = {} >>>>>>> for year in range (2010, 2013): >>>> ba = st.runs ('BA', '%d-01-01' % year, '%d-12-31' % year) >>>> ba.run () >>>> print year, id (ba) >>>> bas [year] = ba >>>> >>>> 2010 150289932 >>>> 2011 150835852 >>>> 2012 149727788 >>>> >>>>>>> for y in sorted (bas.keys ()): >>>> b = bas [year] >>> Shouldn't that be b = bas[y]? >> Yes, it should, indeed! What's more, I should have closed and restarted >> IDLE. There must have >> been a name clash somewhere in the name space. The problem no longer >> exists. Sorry >> about that. And thanks to all who paused to reflect on this non-problem. >> - Frederic. >> >>> >>> >>>> print y, id (b) >>>> >>>> 2010 149727788 >>>> 2011 149727788 >>>> 2012 149727788 >>>> >>> [...] >>> >>> Cheers, >>> >>> Robert >>> >> > > The problem was that year was bound to the integer 2013 from the first > loop. When you subsequently looped over the keys you printed each key > followed by id(bas[2013]). Restarting IDLE only helped because you > presumably didn't repeat the error. > > Duncan > That's it! Isn't it strange how on occasion one doesn't see the most obvious and simple mistake, focusing beyond the realm of foolishness. Thanks all . . . Frederic From steve+comp.lang.python at pearwood.info Tue Nov 13 02:19:10 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Nov 2012 07:19:10 GMT Subject: Getting module path string from a class instance References: Message-ID: <50a1f46e$0$21742$c3e8da3$76491128@news.astraweb.com> On Tue, 13 Nov 2012 06:38:31 +0000, Some Developer wrote: > I'm trying to find a way to get a string of the module path of a class. > > So for instance say I have class Foo and it is in a module called > my.module. I want to be able to get a string that is equal to this: > "my.module.Foo". I'm aware of the __repr__ method but it does not do > what I want it to do in this case. > > Can anyone offer any advice at all? py> from multiprocessing.pool import Pool py> repr(Pool) "" Seems pretty close to what you ask for. You can either pull that string apart: py> s = repr(Pool) py> start = s.find("'") py> end = s.rfind("'") py> s[start+1:end] 'multiprocessing.pool.Pool' or you can construct it yourself: py> Pool.__module__ + '.' + Pool.__name__ 'multiprocessing.pool.Pool' -- Steven From j.kleese at arcor.de Tue Nov 13 02:24:47 2012 From: j.kleese at arcor.de (Johannes Kleese) Date: Tue, 13 Nov 2012 08:24:47 +0100 Subject: Bugs: Content-Length not updated by reused urllib.request.Request / has_header() case-sensitive In-Reply-To: References: <50a12949$0$6566$9b4e6d93@newsspool3.arcor-online.net> Message-ID: <50a203cd$0$9519$9b4e6d93@newsspool1.arcor-online.net> Terry Reedy wrote: > On 11/12/2012 10:52 AM, Johannes Kleese wrote: >> Tested with Python 3.1.3 and Python 3.1.4. > > 3.1 only gets security fixes. Consider upgrading. Stuck with Debian on a server, thus stuck with 3.1 on development machine. > exhibits the same behavior in 3.3.0 of printing ('Content-length', '1') > in the last output. I agree that that looks wrong, but I do not know if > such re-use is supposed to be supported. The Request object should then either get it right on re-use (which I'd prefer), or block re-use. >> While at it, I noticed that urllib.request.Request.has_header() and >> .get_header() are case sensitive, > > Python is case sensitive. True, of course, but >> HTTP headers are not (RFC 2616, 4.2). and the functions work on HTTP data, not Python data. After all, we are lucky to have functions here and not just a dictionary. Anyway, thanks for reporting! From antti.ylikoski at elisanet.fi Tue Nov 13 02:52:34 2012 From: antti.ylikoski at elisanet.fi (Juhani Ylikoski) Date: Tue, 13 Nov 2012 09:52:34 +0200 Subject: Writing Donald E. Knuth based code in Python, cont'd In-Reply-To: References: Message-ID: There were there two (2) bugs in the code that I posted. Thanks anyway. A. J. Y. "Vincent Vande Vyvre" kirjoitti viestiss?:mailman.3596.1352758176.27098.python-list at python.org... Le 12/11/12 22:02, Juhani Ylikoski a ?crit : > Following comes a working, debugged Python program which computes the > permutations of the integers 1, 2, 3 - n after Donald E. Knuth. I > present it as an example of writing straightforward, easy Knuth-based > code in a language with no GOTO statement. > > The Python program has been written after the DFA construct I > previously discussed in this newsgroup, and after Knuth's discussion > of the solution of the problem; and according the (very good) > discussions in this newsgroup. To my opinion, it no more is a "crow's > nest" as they say in Finnish. > > This program was needed for a real problem, namely computing optimal > tournament tables for a Bughouse (Tandem) chess tournament. See > > http://en.wikipedia.org/wiki/Bughouse_chess > > Knuth became criticized in the newsgroup; but to my opinion his books > are still useful and nontrivially needed. > > > --- > > > yours sincerely, Antti J Ylikoski > Helsinki, Finland > PhD student in the Aalto University > Thanks, One comment in: def E1(self): # Phase 1 in Knuth's text self.app = self.listofPerm.append(self.a[1:self.n+1]) return self.E2 # next state: E2 append() return None and self.app is no longer used in the code. Missing something ? -- Vincent V.V. Oqapy . Qarte . PaQager From someukdeveloper at gmail.com Tue Nov 13 02:54:32 2012 From: someukdeveloper at gmail.com (Some Developer) Date: Tue, 13 Nov 2012 07:54:32 +0000 Subject: Getting module path string from a class instance In-Reply-To: <50a1f46e$0$21742$c3e8da3$76491128@news.astraweb.com> References: <50a1f46e$0$21742$c3e8da3$76491128@news.astraweb.com> Message-ID: <50A1FCB8.7060301@googlemail.com> On 13/11/2012 07:19, Steven D'Aprano wrote: > On Tue, 13 Nov 2012 06:38:31 +0000, Some Developer wrote: > >> I'm trying to find a way to get a string of the module path of a class. >> >> So for instance say I have class Foo and it is in a module called >> my.module. I want to be able to get a string that is equal to this: >> "my.module.Foo". I'm aware of the __repr__ method but it does not do >> what I want it to do in this case. >> >> Can anyone offer any advice at all? > py> from multiprocessing.pool import Pool > py> repr(Pool) > "" > > Seems pretty close to what you ask for. You can either pull that string > apart: > > py> s = repr(Pool) > py> start = s.find("'") > py> end = s.rfind("'") > py> s[start+1:end] > 'multiprocessing.pool.Pool' > > or you can construct it yourself: > > py> Pool.__module__ + '.' + Pool.__name__ > 'multiprocessing.pool.Pool' > > Yeah I considered doing it this way but was wary of that method because of possible changes to the implementation of the __repr__ method in the upstream code. If the Django developers don't consider the __repr__ method a public API then it could change in the future breaking my code. Of course this might not happen but I was hoping that there was a more generic way of doing it that did not rely on a certain implementation being in existence. From steve+comp.lang.python at pearwood.info Tue Nov 13 03:49:42 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Nov 2012 08:49:42 GMT Subject: Getting module path string from a class instance References: <50a1f46e$0$21742$c3e8da3$76491128@news.astraweb.com> Message-ID: <50a209a6$0$21742$c3e8da3$76491128@news.astraweb.com> On Tue, 13 Nov 2012 07:54:32 +0000, Some Developer wrote: > On 13/11/2012 07:19, Steven D'Aprano wrote: >> On Tue, 13 Nov 2012 06:38:31 +0000, Some Developer wrote: >> >>> I'm trying to find a way to get a string of the module path of a >>> class. >>> >>> So for instance say I have class Foo and it is in a module called >>> my.module. I want to be able to get a string that is equal to this: >>> "my.module.Foo". I'm aware of the __repr__ method but it does not do >>> what I want it to do in this case. >>> >>> Can anyone offer any advice at all? >> py> from multiprocessing.pool import Pool py> repr(Pool) >> "" >> >> Seems pretty close to what you ask for. You can either pull that string >> apart: >> >> py> s = repr(Pool) >> py> start = s.find("'") >> py> end = s.rfind("'") >> py> s[start+1:end] >> 'multiprocessing.pool.Pool' >> >> or you can construct it yourself: >> >> py> Pool.__module__ + '.' + Pool.__name__ 'multiprocessing.pool.Pool' >> >> > Yeah I considered doing it this way but was wary of that method because > of possible changes to the implementation of the __repr__ method in the > upstream code. If the Django developers don't consider the __repr__ > method a public API then it could change in the future breaking my code. I didn't call SomeClass.__repr__. That is an implementation detail of SomeClass, and could change. I called repr(SomeClass), which calls the *metaclass* __repr__. That is less likely to change, although not impossible. If you're worried, just use the second way: SomeClass.__module__ + '.' + SomeClass.__name__ > Of course this might not happen but I was hoping that there was a more > generic way of doing it that did not rely on a certain implementation > being in existence. SomeClass.__name__ is the official way to get the name of a class; SomeClass.__module__ is the official way to get the name of the module or package it comes from. -- Steven From subhabangalore at gmail.com Tue Nov 13 04:05:37 2012 From: subhabangalore at gmail.com (subhabangalore at gmail.com) Date: Tue, 13 Nov 2012 01:05:37 -0800 (PST) Subject: Understanding Code Message-ID: <45923b5c-f0bd-4367-84f9-ba08a260da69@googlegroups.com> Dear Group, To improve my code writing I am trying to read good codes. Now, I have received a code,as given below,(apology for slight indentation errors) the code is running well. Now to comprehend the code, I am looking to understand it completely. class Calculate: def __init__(self): self.prior = {} self.total = {} self.count = 0 def add(self, cls, obs): self.prior[cls] = self.prior.get(cls, 0) + 1 for idx, val in enumerate(obs): key = cls, idx, val self.total[key] = self.total.get(key, 0) + 1 self.count += 1 def discr(self, cls, obs): result = self.prior[cls]/self.count for idx, val in enumerate(obs): freq = self.total.get((cls, idx, val), 0) result *= freq/self.prior[cls] return result def classify(self, obs): candidates = [(self.discr(c, obs), c) for c in self.prior] return max(candidates)[1] I am not understanding many parts of it, I am understanding many parts of it also. So I am looking for an exercise what are the things I should know to understand it, (please do not give answers I would get back with the answers in a week and would discuss even how to write better than this). If any one of the expert members of the room kindly help me to do this. Thanking You in Advance, Regards, Subhabrata. From d at davea.name Tue Nov 13 04:25:42 2012 From: d at davea.name (Dave Angel) Date: Tue, 13 Nov 2012 04:25:42 -0500 Subject: Simple Question regarding running .py program In-Reply-To: <22ae6963-6700-4ea7-b396-344d89ead7f2@googlegroups.com> References: <22ae6963-6700-4ea7-b396-344d89ead7f2@googlegroups.com> Message-ID: <50A21216.5070906@davea.name> On 11/12/2012 09:45 PM, Caroline Hou wrote: > > Hi Dave! > > thank you very much for your quick reply! I did manage to get the program run from cmd.exe. > So does it mean that if I want to use python interactively,I should use the interpreter,while if I just want to run a python program, I should use DOS shell instead? The DOS shell is one answer that settled both of your original questions. It's also how I run about 90% of the time. But different people have different preferences. The interpreter is great for experimenting. But if you have non-trivial code (presumably written in a file), then you have to import it. Which means references to the stuff there are done with mymodule.myfunction. That can get tiresome after a while. And if you have to change the source file, it's not always safe to reload it (so I never do). If I've imported something, and that something has changed, I quit the interpreter and start it over. When you're running the script as a whole from the DOS box, it's always a clean start. > Also, how could I edit my script? I have sth called "IDLE" installed along with python. Is it the right place to write/edit my script? Any text editor will do, but especially one with some knowledge of the Python syntax. Don't use Notepad. I've never used IDLE (I use a commercial one called Komodo IDE), so I can't say how good it is. Many people love IDLE, though. The thing is, any IDE will require some setup (setting directory paths, project settings, etc.), and some getting used to. Some don't work very well for GUI programs, others truncate traceback listings (maybe giving you a GUI view of the same information). Some apparently won't even let you copy/paste a traceback into a mail message. So it's very useful to also get thoroughly acquainted with the cmd prompt. > Sorry about these semi-idiot questions but it is really hard to find an article or book that covers such basic stuffs! Nothing wrong with those questions. Welcome to Python-list. -- DaveA From d at davea.name Tue Nov 13 04:36:30 2012 From: d at davea.name (Dave Angel) Date: Tue, 13 Nov 2012 04:36:30 -0500 Subject: Getting module path string from a class instance In-Reply-To: <50A1EAE7.1090700@googlemail.com> References: <50A1EAE7.1090700@googlemail.com> Message-ID: <50A2149E.303@davea.name> On 11/13/2012 01:38 AM, Some Developer wrote: > I'm trying to find a way to get a string of the module path of a class. > > So for instance say I have class Foo and it is in a module called > my.module. I want to be able to get a string that is equal to this: > "my.module.Foo". I'm aware of the __repr__ method but it does not do > what I want it to do in this case. > > Can anyone offer any advice at all? So you have: import my.module theclass = my.module.Foo print tellme(theClass) and you want to know how to write tellme? Why not just change it to take a string, and pass it "my.module.Foo" ? If you have stored the class away somewhere, and want to figure it out from there, you could look at the __module__ attribute. that'll tell you the module name, but not the class name. If you're really asking how to get that string from an INSTANCE of the class, then try the __class__ attribute of that instance. -- DaveA From andrea.crotti.0 at gmail.com Tue Nov 13 05:31:32 2012 From: andrea.crotti.0 at gmail.com (andrea crotti) Date: Tue, 13 Nov 2012 10:31:32 +0000 Subject: creating size-limited tar files In-Reply-To: References: <509ab0fa$0$6636$9b4e6d93@newsspool2.arcor-online.net> <509AD812.2060605@gmail.com> Message-ID: 2012/11/9 andrea crotti : > Anyway in the meanwhile I implemented this tar and split in this way below. > It works very well and it's probably much faster, but the downside is that > I give away control to tar and split.. > > def tar_and_split(inputfile, output, bytes_size=None): > """Take the file containing all the files to compress, the bytes > desired for the split and the base name of the output file > """ > # cleanup first > for fname in glob(output + "*"): > logger.debug("Removing old file %s" % fname) > remove(fname) > > out = '-' if bytes_size else (output + '.tar.gz') > cmd = "tar czpf {} $(cat {})".format(out, inputfile) > if bytes_size: > cmd += "| split -b {} -d - {}".format(bytes_size, output) > > logger.info("Running command %s" % cmd) > > proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, > stderr=subprocess.PIPE) > out, err = proc.communicate() > if err: > logger.error("Got error messages %s" % err) > > logger.info("Output %s" % out) > > if proc.returncode != 0: > logger.error("Something failed running %s, need to re-run" % cmd) > return False There is another problem with this solution, if I run something like this with Popen: cmd = "tar {bigc} -czpf - --files-from {inputfile} | split -b {bytes_size} -d - {output}" proc = subprocess.Popen(to_run, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) the proc.returncode will only be the one from "split", so I lose the ability to check if tar failed.. A solution would be something like this: { ls -dlkfjdsl; echo $? > tar.status; } | split but it's a bit ugly. I wonder if I can use the subprocess PIPEs to do the same thing, is it going to be as fast and work in the same way?? From __peter__ at web.de Tue Nov 13 05:41:55 2012 From: __peter__ at web.de (Peter Otten) Date: Tue, 13 Nov 2012 11:41:55 +0100 Subject: Understanding Code References: <45923b5c-f0bd-4367-84f9-ba08a260da69@googlegroups.com> Message-ID: subhabangalore at gmail.com wrote: > Dear Group, > To improve my code writing I am trying to read good codes. Now, I have > received a code,as given below,(apology for slight indentation errors) the > code is running well. Now to comprehend the code, I am looking to > understand it completely. > > class Calculate: > def __init__(self): > self.prior = {} > self.total = {} > self.count = 0 > def add(self, cls, obs): > self.prior[cls] = self.prior.get(cls, 0) + 1 > for idx, val in enumerate(obs): > key = cls, idx, val > self.total[key] = self.total.get(key, 0) + 1 > self.count += 1 > def discr(self, cls, obs): > result = self.prior[cls]/self.count > for idx, val in enumerate(obs): > freq = self.total.get((cls, idx, val), 0) > result *= freq/self.prior[cls] > return result > def classify(self, obs): > candidates = [(self.discr(c, obs), c) for c in self.prior] > return max(candidates)[1] > > I am not understanding many parts of it, I am understanding many parts of > it also. > > So I am looking for an exercise what are the things I should know to > understand it, (please do not give answers I would get back with the > answers in a week and would discuss even how to write better than this). Start with running the code for the simplest piece of the class: >>> c = Calculate() >>> c.add("x", [1,2,3]) Then inspect the attributes: >>> c.prior {'x': 1} >>> c.total {('x', 2, 3): 1, ('x', 1, 2): 1, ('x', 0, 1): 1} >>> c.count Now read the code for Calculate.add(). Do you understand what > self.prior[cls] = self.prior.get(cls, 0) + 1 does? Experiment with a dict and its get() method in the interactive interpreter. Next to the loop. > for idx, val in enumerate(obs): > key = cls, idx, val > self.total[key] = self.total.get(key, 0) + 1 > self.count += 1 Do you understand what enumerate() does? If not read its documentation with >>> help(enumerate) Do you understand what key looks like? If you don't add a print statement > for idx, val in enumerate(obs): > key = cls, idx, val print key > self.total[key] = self.total.get(key, 0) + 1 > self.count += 1 What does > self.total[key] = self.total.get(key, 0) + 1 do? Note that this line is very similar to > self.prior[cls] = self.prior.get(cls, 0) + 1 which you have studied before. > self.count += 1 This like the rest of your class is left as an exercise. The routine is always the same: - break parts that you don't understand into smaller parts - consult the documentation on unknown classes, functions, methods, preferrably with help(some_obj) or dir(some_obj) - run portions of the code or similar code in the interactive interpreter or with a little throw-away script. - add print statements to inspect variables at interesting points in your script. From jeanmichel at sequans.com Tue Nov 13 07:19:15 2012 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Tue, 13 Nov 2012 13:19:15 +0100 (CET) Subject: Passing functions as parameter (multiprocessing) Message-ID: <425807738.1035823.1352809155090.JavaMail.root@sequans.com> Fellows, I'm having problems understanding an issue with passing function as parameters. I'm sending some functions to the multiprocessing module (python 2.5 with the proper backport). I'm iterating on a list of functions, however it seems that only the last function implementation is used for all the subprocesses. Here's a code that triggers the issue: import multiprocessing def f1(): print 'I am f1' def f2(foo): print 'I am f2 %s' % foo workers = [ (f1,tuple()), (f2,(5,)), ] procs=[] for func, parameters in workers: # here it should be decorated, but for this example to be kept simple, the function is only wrapped, doing nothing special def subproc(*args, **kwargs): return func(*args, **kwargs) procs.append(multiprocessing.Process(target=subproc, args=parameters)) for proc in procs: proc.start() for proc in procs: proc.join() Here's the result: > run test.py Process Process-1: Traceback (most recent call last): File "/usr/lib/python2.5/site-packages/multiprocessing-2.6.2.1-py2.5-linux-i686.egg/multiprocessing/process.py", line 237, in _bootstrap self.run() File "/usr/lib/python2.5/site-packages/multiprocessing-2.6.2.1-py2.5-linux-i686.egg/multiprocessing/process.py", line 93, in run self._target(*self._args, **self._kwargs) File "test.py", line 17, in subproc return func(*args, **kwargs) TypeError: f2() takes exactly 1 argument (0 given) I am f2 5 It looks like the first subprocess is called with f2 instead of f1. Any idea ? JM -- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you. From cleuson.o at gmail.com Tue Nov 13 07:19:43 2012 From: cleuson.o at gmail.com (Cleuson Alves) Date: Tue, 13 Nov 2012 10:19:43 -0200 Subject: Division matrix In-Reply-To: References: <98b451e1-5cd5-46e9-8be4-59dcc835700b@googlegroups.com> Message-ID: Thanks, I'm starting to plan now, so I'm still confused with the production code, but what I need is to divide array 2x2 or 3x3. I still can not! 2012/11/12 Joshua Landau > On 13 November 2012 01:00, Cleuson Alves wrote: > >> Hello, I need to solve an exercise follows, first calculate the inverse >> matrix and then multiply the first matrix. >> > > This list isn't to give answers for homeworks, and this sounds like one. > We *do* give help to those who have a specific problem and who preferably > show that they are trying to help up help them. > > >> I await help. >> > > For what?! You haven't asked a question. > 1) What do you need. Answer precisely, preferably giving an example output > for the input you want > 2) What techniques have you tried? You have given code below in an > extremely un-obvious manner. What does it do, crash? Is it the wrong > output? What is wrong about the output? > 3) What do you *think* has gone wrong? What about the output seems to be > wrong? > > >> Thank you. >> follows the code below incomplete. >> > > On the basis that we can get some foreign people who maybe don't have > English as a commonly used language, muddled grammar isn't that big deal. > However, when asking for help it is worth checking that you've asked in a > clear way. > > >> m = [[1,2,3],[4,5,6],[7,8,9]] >> x = [] >> for i in [0,1,2]: >> y = [] >> for linha in m: >> y.append(linha[i]) >> x.append(y) >> >> print x >> [[1, 4, 7], [2, 5, 8], [3, 6, 9]] >> > > Is this the right output? Is this what you *meant* by inverse? As Ian > Kelly (who is undoubtedly more learned in this area) said, this is a > transpose, so you have just swapped the one axis with another. > > >> def ProdMatrix(x,b): >> tamL = len(x) >> tamC = len(x[0]) >> c = nullMatrix(tamL,tamC) >> for i in range(tamL): >> for j in range(tamC): >> val = 0 >> for k in range(len(b)): >> val = val + x[i][l]*b[k][j] >> c[i][j] >> return c >> > > You haven't given the full code. This crashes because we don't have > "nullMatrix" defined. It would be nice if we had something we could try to > run. > > Then, the error is: "NameError: global name 'l' is not defined". On the > line "val = val + x[i][l]*b[k][j]" you use a variable "l" which doesn't > exist. What should you be using, or should you have created it? > > *Then* you get an output of pure 0s. This is because you forgot to put > your result into c, your new matrix. > You probably just forgot to finish the line "c[i][j]", which does nothing > now. > > You're actually really close on the second part. I have no idea if you're > doing what you want for the first part, but it's not what's traditionally > called an inverse. > -- Cleuson de Oliveira Alves Rio de Janeiro - RJ -------------- next part -------------- An HTML attachment was scrubbed... URL: From maniandram01 at gmail.com Tue Nov 13 07:35:31 2012 From: maniandram01 at gmail.com (Ramchandra Apte) Date: Tue, 13 Nov 2012 04:35:31 -0800 (PST) Subject: Simple Question regarding running .py program In-Reply-To: <22ae6963-6700-4ea7-b396-344d89ead7f2@googlegroups.com> References: <22ae6963-6700-4ea7-b396-344d89ead7f2@googlegroups.com> Message-ID: <8085ebf2-9f08-4889-84c9-f786d6776001@googlegroups.com> On Tuesday, 13 November 2012 08:15:45 UTC+5:30, Caroline Hou wrote: > On Monday, 12 November 2012 21:25:08 UTC-5, Dave Angel wrote: > > > On 11/12/2012 09:02 PM, Caroline Hou wrote: > > > > > > > Hi all! > > > > > > > > > > > > > > I just started learning Python by myself and I have an extremely simple question now! > > > > > > > I am in my Python interpreter now and I want to open/edit a program called nobel.py. But when I typed >>> python nobel.py, it gave me a "SyntaxError:invalid syntax?( I've changed to the correct directory)what should I do? > > > > > > > I also want to run the program, but as I double-clicked the program, a command window pops up and closes immediately. How can I see the result of the program run? > > > > > > > Could anyone help me please? I am pretty confused here...Thank you! > > > > > > > > > > > > It'd be nice to specify that you're running Windows, and also what > > > > > > version of the interpreter, although in this case the latter doesn't matter. > > > > > > > > > > > > > > > > > > Go to a shell (cmd.exe), change to the directory containing that script, > > > > > > and type the command as you did. > > > > > > > > > > > > On linux: davea at think:~$ python nobel.py > > > > > > On Windows: c:\mydir\myscript > python nobel.py > > > > > > > > > > > > If you're already in the python interpreter, then running python is > > > > > > useless -- it's already running. In that case, you might want to use > > > > > > import. However, I recommend against it at first, as it opens up some > > > > > > other problems you haven't experience with yet. > > > > > > > > > > > > When you say you "double clicked the program', we have to guess you > > > > > > might have meant in MS Explorer. If you do that, it launches a cmd, it > > > > > > runs the python system, and it closes the cmd. Blame Windows for not > > > > > > reading your mind. If you want the cmd window to stick around, you > > > > > > COULD end your program with an raw_input function call, but frequently > > > > > > that won't work. The right answer is the first one above... Open a > > > > > > shell (perhaps with a menu like DOS BOX), change... > > > > > > > > > > > > That way, when the program finishes, you can see what happened, or > > > > > > didn't happen, and you can run it again using the uparrow key. > > > > > > > > > > > > BTW, you don't need to send email to both the python-list and to the > > > > > > newsgroup. The newsgroup is automatically fed from the list. But since > > > > > > you're posting from google groups, that's just one of the bugs. Many > > > > > > folks here simply filter out everything from google groups, so your post > > > > > > is invisible to them. > > > > > > > > > > > > > > > > > > -- > > > > > > > > > > > > DaveA > > > > Hi Dave! > > > > thank you very much for your quick reply! I did manage to get the program run from cmd.exe. > > So does it mean that if I want to use python interactively,I should use the interpreter,while if I just want to run a python program, I should use DOS shell instead? > > Also, how could I edit my script? I have sth called "IDLE" installed along with python. Is it the right place to write/edit my script? > > Sorry about these semi-idiot questions but it is really hard to find an article or book that covers such basic stuffs! > > Thank you! > > > > Caroline Hou IDLE is recommended for newbies like you because an IDE requires too much configuration. When you start writing a big project, you can use an IDE. From __peter__ at web.de Tue Nov 13 07:51:27 2012 From: __peter__ at web.de (Peter Otten) Date: Tue, 13 Nov 2012 13:51:27 +0100 Subject: Passing functions as parameter (multiprocessing) References: <425807738.1035823.1352809155090.JavaMail.root@sequans.com> Message-ID: Jean-Michel Pichavant wrote: > I'm having problems understanding an issue with passing function as > parameters. > Here's a code that triggers the issue: > > > import multiprocessing > > def f1(): > print 'I am f1' > def f2(foo): > print 'I am f2 %s' % foo > > workers = [ > (f1,tuple()), > (f2,(5,)), > ] > > procs=[] > for func, parameters in workers: > def subproc(*args, **kwargs): > return func(*args, **kwargs) > procs.append(multiprocessing.Process(target=subproc, args=parameters)) Python has late binding, and when the loop has finished the name func is bound to f2. You have created multiple subproc functions, but that doesn't matter as they all invoke func aka f2. A possible fix: def make_subproc(func): def subproc(*args, **kwargs): return func(*args, **kwargs) return subproc procs=[] for func, parameters in workers: procs.append(multiprocessing.Process(target=make_subproc(func), args=parameters)) From python at mrabarnett.plus.com Tue Nov 13 07:51:28 2012 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 13 Nov 2012 12:51:28 +0000 Subject: Passing functions as parameter (multiprocessing) In-Reply-To: <425807738.1035823.1352809155090.JavaMail.root@sequans.com> References: <425807738.1035823.1352809155090.JavaMail.root@sequans.com> Message-ID: <50A24250.4040701@mrabarnett.plus.com> On 2012-11-13 12:19, Jean-Michel Pichavant wrote: > Fellows, > > I'm having problems understanding an issue with passing function as parameters. > > I'm sending some functions to the multiprocessing module (python 2.5 with the proper backport). > I'm iterating on a list of functions, however it seems that only the last function implementation is used for > all the subprocesses. > > Here's a code that triggers the issue: > > > import multiprocessing > > def f1(): > print 'I am f1' > def f2(foo): > print 'I am f2 %s' % foo > > workers = [ > (f1,tuple()), > (f2,(5,)), > ] > > procs=[] > for func, parameters in workers: > # here it should be decorated, but for this example to be kept simple, the function is only wrapped, doing nothing special > def subproc(*args, **kwargs): > return func(*args, **kwargs) > procs.append(multiprocessing.Process(target=subproc, args=parameters)) > > for proc in procs: > proc.start() > for proc in procs: > proc.join() > > > Here's the result: >> run test.py > Process Process-1: > Traceback (most recent call last): > File "/usr/lib/python2.5/site-packages/multiprocessing-2.6.2.1-py2.5-linux-i686.egg/multiprocessing/process.py", line 237, in _bootstrap > self.run() > File "/usr/lib/python2.5/site-packages/multiprocessing-2.6.2.1-py2.5-linux-i686.egg/multiprocessing/process.py", line 93, in run > self._target(*self._args, **self._kwargs) > File "test.py", line 17, in subproc > return func(*args, **kwargs) > TypeError: f2() takes exactly 1 argument (0 given) > I am f2 5 > > It looks like the first subprocess is called with f2 instead of f1. > I believe the problem is that 'subproc' calls 'func', which is rebound on the each iteration. From alister.ware at ntlworld.com Tue Nov 13 08:13:10 2012 From: alister.ware at ntlworld.com (Alister) Date: Tue, 13 Nov 2012 13:13:10 GMT Subject: stackoverflow quote on Python References: Message-ID: On Tue, 13 Nov 2012 03:08:54 +0000, Mark Lawrence wrote: > http://stackoverflow.com/questions/tagged/python > > "Python has two major versions (2 and 3) in use which have significant > differences." > > I believe that this is incorrect. The warts have been removed, but > significant differences, not in my book. If there is agreement about > there not being significant differences, should stackoverflow be asked > to change their wording? I think you are confusing significant with fundamental they are significant differences because failing to take them into account may cause incorrect operation. they are not fundamental differences because someone familiar with on version should have minimal difficulty in understanding the other. -- Lee's Law: Mother said there would be days like this, but she never said that there'd be so many! From wxjmfauth at gmail.com Tue Nov 13 08:21:49 2012 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Tue, 13 Nov 2012 05:21:49 -0800 (PST) Subject: stackoverflow quote on Python In-Reply-To: <50a1ddba$0$21742$c3e8da3$76491128@news.astraweb.com> References: <50a1ddba$0$21742$c3e8da3$76491128@news.astraweb.com> Message-ID: Le mardi 13 novembre 2012 06:42:19 UTC+1, Steven D'Aprano a ?crit?: > On Tue, 13 Nov 2012 03:08:54 +0000, Mark Lawrence wrote: > > > > > * strings are now proper text strings (Unicode), not byte strings; > ---- Let me laugh. jmf From thbach at students.uni-mainz.de Tue Nov 13 08:53:55 2012 From: thbach at students.uni-mainz.de (Thomas Bach) Date: Tue, 13 Nov 2012 14:53:55 +0100 Subject: Help building a dictionary of lists In-Reply-To: References: <9f44d1de-bccf-41ad-8529-ff8a65d7e2b4@googlegroups.com> Message-ID: <20121113135351.GB3424@palmo.box> On Mon, Nov 12, 2012 at 11:41:59PM +0000, Joshua Landau wrote: > > Dict comprehension: > {i:[] for i in ["Test 1", "Test 2", "Test 3"]} In Python 2.6 this syntax is not supported. You can achieve the same there via dict((i, []) for i in ['Test 1', 'Test 2', 'Test 3']) Also have a look at ``collections.defaultdict``. Regards, Thomas. From shivers.paul at yahoo.co.uk Tue Nov 13 09:05:49 2012 From: shivers.paul at yahoo.co.uk (shivers.paul at yahoo.co.uk) Date: Tue, 13 Nov 2012 06:05:49 -0800 (PST) Subject: xml data or other? In-Reply-To: References: Message-ID: <834d312e-2fac-4e03-8533-2c4a6a33b9be@googlegroups.com> On Friday, November 9, 2012 12:54:56 PM UTC, Artie Ziff wrote: > Hello, > > > > I want to process XML-like data like this: > > > > > > > > ACPI (Advanced Control Power & Integration) testscript for 2.5 kernels. > > > > <\description> > > > > ltp/testcases/kernel/device-drivers/acpi/ltpacpi.sh > > <\test_location> > > <\testname> > > > > > > After manually editing the data above, the python module > > xml.etree.ElementTree parses it without failing due to error in the data > > structure. > > > > Edits were substituting '/' for '\' on the end tags, and adding the > > following structure: > > > > > > > > > > ... > > <\testname> > > > > > > > > Is there a name for the format above (perhaps xhtml)? > > I'd like to find a python module that can translate it to proper xml. > > Does one exist? etree? > > > > Many thanks! > > az maybe an xml tool would be better, a good list of xml tools here; http://www.xml-data.info From shivers.paul at yahoo.co.uk Tue Nov 13 09:05:49 2012 From: shivers.paul at yahoo.co.uk (shivers.paul at yahoo.co.uk) Date: Tue, 13 Nov 2012 06:05:49 -0800 (PST) Subject: xml data or other? In-Reply-To: References: Message-ID: <834d312e-2fac-4e03-8533-2c4a6a33b9be@googlegroups.com> On Friday, November 9, 2012 12:54:56 PM UTC, Artie Ziff wrote: > Hello, > > > > I want to process XML-like data like this: > > > > > > > > ACPI (Advanced Control Power & Integration) testscript for 2.5 kernels. > > > > <\description> > > > > ltp/testcases/kernel/device-drivers/acpi/ltpacpi.sh > > <\test_location> > > <\testname> > > > > > > After manually editing the data above, the python module > > xml.etree.ElementTree parses it without failing due to error in the data > > structure. > > > > Edits were substituting '/' for '\' on the end tags, and adding the > > following structure: > > > > > > > > > > ... > > <\testname> > > > > > > > > Is there a name for the format above (perhaps xhtml)? > > I'd like to find a python module that can translate it to proper xml. > > Does one exist? etree? > > > > Many thanks! > > az maybe an xml tool would be better, a good list of xml tools here; http://www.xml-data.info From msc at es.aau.dk Tue Nov 13 09:53:04 2012 From: msc at es.aau.dk (Martin Sand Christensen) Date: Tue, 13 Nov 2012 15:53:04 +0100 Subject: Templating and XML modelling Message-ID: <50a25ec9$0$281$14726298@news.sunsite.dk> Hi! At our IT department we've developed a basic templating system for web apps in the spirit of Meld3 (which appears to have been abandoned some time ago), based on lxml. Here's what we like about it: * It's just a library, not a template language * It uses templates that are valid XHTML * It's trivial to generate tables and forms from database metadata * It's trivial to fill named elements ? la format strings While we like it, it's more code to maintain. Between the time when we started coding this and now, more new templating systems have appeared than I can reasonably evaluate. So now the question is whether we can find a good replacement or whether we should publish our code and hope that more people will adopt and help maintain it. So... 1) Can you suggest a good alternative that sounds like a good fit? 2) Does our templating system sound like just what you've been looking for? -- Martin Sand Christensen IT Services, Dept. of Electronic Systems From cjw at ncf.ca Tue Nov 13 10:08:59 2012 From: cjw at ncf.ca (Colin J. Williams) Date: Tue, 13 Nov 2012 10:08:59 -0500 Subject: Error messages from format() Message-ID: Is there some way to get more informative error messages from the builtin format? Most messages are such as: Traceback (most recent call last): File "", line 1, in ValueError: Invalid conversion specification This example doesn't point to the first invalid case. [Dbg]>>> format((25, 31),'{0^9o} a(1:9x}') Traceback (most recent call last): File "", line 1, in ValueError: Invalid conversion specification Basically, I'm trying to make use of the format function with Python 3.2, but find little in the way of examples in the docs. Colin W. From oscar.j.benjamin at gmail.com Tue Nov 13 10:28:49 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 13 Nov 2012 15:28:49 +0000 Subject: Passing functions as parameter (multiprocessing) In-Reply-To: References: <425807738.1035823.1352809155090.JavaMail.root@sequans.com> Message-ID: On 13 November 2012 12:51, Peter Otten <__peter__ at web.de> wrote: > Jean-Michel Pichavant wrote: > >> I'm having problems understanding an issue with passing function as >> parameters. > >> Here's a code that triggers the issue: >> >> >> import multiprocessing >> >> def f1(): >> print 'I am f1' >> def f2(foo): >> print 'I am f2 %s' % foo >> >> workers = [ >> (f1,tuple()), >> (f2,(5,)), >> ] >> >> procs=[] >> for func, parameters in workers: > >> def subproc(*args, **kwargs): >> return func(*args, **kwargs) >> procs.append(multiprocessing.Process(target=subproc, args=parameters)) I don't know if this is to do with the way that the code was simplified before posting but this subproc function wrapper does nothing (even after Peter fixed it below). This code is needlessly complicated for what it does. > Python has late binding, and when the loop has finished the name func is > bound to f2. You have created multiple subproc functions, but that doesn't > matter as they all invoke func aka f2. > > A possible fix: > > def make_subproc(func): > def subproc(*args, **kwargs): > return func(*args, **kwargs) > return subproc > > procs=[] > for func, parameters in workers: > procs.append(multiprocessing.Process(target=make_subproc(func), > args=parameters)) The above is one way to wrap a function but it wraps the function into a function that is exactly the same as the original function. I assume that the reason for having the subproc function was to be able to bind parameters to the function like so: def make_subproc(func, args): def subproc(): return func(*args) return subproc procs=[] for func, parameters in workers: procs.append(multiprocessing.Process(target=make_subproc(func, parameters))) However, as you have already noticed, multiprocess.Process already has the machinery to do this. If you pass in a value for args you can leave out the subproc/make_subproc function and write: procs=[] for func, parameters in workers: procs.append(multiprocessing.Process(target=func, args=parameters)) A loop like this can just be a list comprehension: procs = [multiprocessing.Process(target=func, args=args) for func, args in workers] Oscar From breamoreboy at yahoo.co.uk Tue Nov 13 10:54:17 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 13 Nov 2012 15:54:17 +0000 Subject: stackoverflow quote on Python In-Reply-To: References: <50a1ddba$0$21742$c3e8da3$76491128@news.astraweb.com> Message-ID: On 13/11/2012 13:21, wxjmfauth at gmail.com wrote: > Le mardi 13 novembre 2012 06:42:19 UTC+1, Steven D'Aprano a ?crit : >> On Tue, 13 Nov 2012 03:08:54 +0000, Mark Lawrence wrote: >> >> * strings are now proper text strings (Unicode), not byte strings; >> > > ---- > > Let me laugh. > > jmf > Presumably because you're looking at yourself in a mirror, and have finally realised that you've completely misunderstood the work done with unicode in Python 3, specifically Python 3.3? -- Cheers. Mark Lawrence. From ethan at stoneleaf.us Tue Nov 13 11:04:31 2012 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 13 Nov 2012 08:04:31 -0800 Subject: stackoverflow quote on Python In-Reply-To: References: <50a1ddba$0$21742$c3e8da3$76491128@news.astraweb.com> Message-ID: <50A26F8F.1070101@stoneleaf.us> wxjmfauth at gmail.com wrote: > Le mardi 13 novembre 2012 06:42:19 UTC+1, Steven D'Aprano a ?crit : >> On Tue, 13 Nov 2012 03:08:54 +0000, Mark Lawrence wrote: >> >> * strings are now proper text strings (Unicode), not byte strings; > Let me laugh. *plonk* From ian.g.kelly at gmail.com Tue Nov 13 11:07:15 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 13 Nov 2012 09:07:15 -0700 Subject: creating size-limited tar files In-Reply-To: References: <509ab0fa$0$6636$9b4e6d93@newsspool2.arcor-online.net> <509AD812.2060605@gmail.com> Message-ID: On Tue, Nov 13, 2012 at 3:31 AM, andrea crotti wrote: > but it's a bit ugly. I wonder if I can use the subprocess PIPEs to do > the same thing, is it going to be as fast and work in the same way?? It'll look something like this: >>> p1 = subprocess.Popen(cmd1, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) >>> p2 = subprocess.Popen(cmd2, shell=True, stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE) >>> p1.communicate() ('', '') >>> p2.communicate() ('', '') >>> p1.wait() 0 >>> p2.wait() 0 Note that there's a subtle potential for deadlock here. During the p1.communicate() call, if the p2 output buffer fills up, then it will stop accepting input from p1 until p2.communicate() can be called, and then if that buffer also fills up, p1 will hang. Additionally, if p2 needs to wait on the parent process for some reason, then you end up effectively serializing the two processes. Solution would be to poll all the open-ended pipes in a select() loop instead of using communicate(), or perhaps make the two communicate calls simultaneously in separate threads. From someukdeveloper at gmail.com Tue Nov 13 11:15:13 2012 From: someukdeveloper at gmail.com (Some Developer) Date: Tue, 13 Nov 2012 16:15:13 +0000 Subject: Getting module path string from a class instance In-Reply-To: <50a209a6$0$21742$c3e8da3$76491128@news.astraweb.com> References: <50a1f46e$0$21742$c3e8da3$76491128@news.astraweb.com> <50a209a6$0$21742$c3e8da3$76491128@news.astraweb.com> Message-ID: <50A27211.2040705@googlemail.com> On 13/11/2012 08:49, Steven D'Aprano wrote: > On Tue, 13 Nov 2012 07:54:32 +0000, Some Developer wrote: > >> On 13/11/2012 07:19, Steven D'Aprano wrote: >>> On Tue, 13 Nov 2012 06:38:31 +0000, Some Developer wrote: >>> >>>> I'm trying to find a way to get a string of the module path of a >>>> class. >>>> >>>> So for instance say I have class Foo and it is in a module called >>>> my.module. I want to be able to get a string that is equal to this: >>>> "my.module.Foo". I'm aware of the __repr__ method but it does not do >>>> what I want it to do in this case. >>>> >>>> Can anyone offer any advice at all? >>> py> from multiprocessing.pool import Pool py> repr(Pool) >>> "" >>> >>> Seems pretty close to what you ask for. You can either pull that string >>> apart: >>> >>> py> s = repr(Pool) >>> py> start = s.find("'") >>> py> end = s.rfind("'") >>> py> s[start+1:end] >>> 'multiprocessing.pool.Pool' >>> >>> or you can construct it yourself: >>> >>> py> Pool.__module__ + '.' + Pool.__name__ 'multiprocessing.pool.Pool' >>> >>> >> Yeah I considered doing it this way but was wary of that method because >> of possible changes to the implementation of the __repr__ method in the >> upstream code. If the Django developers don't consider the __repr__ >> method a public API then it could change in the future breaking my code. > I didn't call SomeClass.__repr__. That is an implementation detail of > SomeClass, and could change. > > I called repr(SomeClass), which calls the *metaclass* __repr__. That is > less likely to change, although not impossible. > > > If you're worried, just use the second way: > > SomeClass.__module__ + '.' + SomeClass.__name__ > Ah, my mistake. Thanks. That sounds exactly like what I want. >> Of course this might not happen but I was hoping that there was a more >> generic way of doing it that did not rely on a certain implementation >> being in existence. > SomeClass.__name__ is the official way to get the name of a class; > SomeClass.__module__ is the official way to get the name of the module or > package it comes from. From __peter__ at web.de Tue Nov 13 11:18:01 2012 From: __peter__ at web.de (Peter Otten) Date: Tue, 13 Nov 2012 17:18:01 +0100 Subject: Passing functions as parameter (multiprocessing) References: <425807738.1035823.1352809155090.JavaMail.root@sequans.com> Message-ID: Oscar Benjamin wrote: > I don't know if this is to do with the way that the code was > simplified before posting but this subproc function wrapper does > nothing (even after Peter fixed it below). This code is needlessly > complicated for what it does. Jean-Michel's Post had the following comment preceding his subproc() definition >>> # here it should be decorated, but for this example to be kept >>> simple, the function is only wrapped, doing nothing special which I unhelpfully removed... From someukdeveloper at gmail.com Tue Nov 13 11:23:12 2012 From: someukdeveloper at gmail.com (Some Developer) Date: Tue, 13 Nov 2012 16:23:12 +0000 Subject: Getting module path string from a class instance In-Reply-To: <50A2149E.303@davea.name> References: <50A1EAE7.1090700@googlemail.com> <50A2149E.303@davea.name> Message-ID: <50A273F0.60002@googlemail.com> On 13/11/2012 09:36, Dave Angel wrote: > On 11/13/2012 01:38 AM, Some Developer wrote: >> I'm trying to find a way to get a string of the module path of a class. >> >> So for instance say I have class Foo and it is in a module called >> my.module. I want to be able to get a string that is equal to this: >> "my.module.Foo". I'm aware of the __repr__ method but it does not do >> what I want it to do in this case. >> >> Can anyone offer any advice at all? > So you have: > > import my.module > theclass = my.module.Foo > print tellme(theClass) > > and you want to know how to write tellme? Why not just change it to > take a string, and pass it "my.module.Foo" ? > > If you have stored the class away somewhere, and want to figure it out > from there, you could look at the __module__ attribute. that'll tell > you the module name, but not the class name. > > > If you're really asking how to get that string from an INSTANCE of the > class, then try the __class__ attribute of that instance. I'm actually writing a dynamic importer. Basically when the program starts it queries all the loaded modules for a certain package. It then stores the module and class information in a database and then when a certain view is called the specific data required is loaded by calling importlib.import_module('module.class'). This then populates the view with the required data. Basically the idea is to be able to display any data required without having to tightly couple the view code to a specific model type. Thanks for the help. From ian.g.kelly at gmail.com Tue Nov 13 11:25:20 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 13 Nov 2012 09:25:20 -0700 Subject: creating size-limited tar files In-Reply-To: References: <509ab0fa$0$6636$9b4e6d93@newsspool2.arcor-online.net> <509AD812.2060605@gmail.com> Message-ID: On Tue, Nov 13, 2012 at 9:07 AM, Ian Kelly wrote: > It'll look something like this: > >>>> p1 = subprocess.Popen(cmd1, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) >>>> p2 = subprocess.Popen(cmd2, shell=True, stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE) >>>> p1.communicate() > ('', '') >>>> p2.communicate() > ('', '') >>>> p1.wait() > 0 >>>> p2.wait() > 0 > > Note that there's a subtle potential for deadlock here. During the > p1.communicate() call, if the p2 output buffer fills up, then it will > stop accepting input from p1 until p2.communicate() can be called, and > then if that buffer also fills up, p1 will hang. Additionally, if p2 > needs to wait on the parent process for some reason, then you end up > effectively serializing the two processes. > > Solution would be to poll all the open-ended pipes in a select() loop > instead of using communicate(), or perhaps make the two communicate > calls simultaneously in separate threads. Sorry, the example I gave above is wrong. If you're calling p1.communicate(), then you need to first remove the p1.stdout pipe from the Popen object. Otherwise, the communicate() call will try to read data from it and may "steal" input from p2. It should look more like this: >>> p1 = subprocess.Popen(cmd1, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) >>> p2 = subprocess.Popen(cmd2, shell=True, stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE) >>> p1.stdout = None From ian.g.kelly at gmail.com Tue Nov 13 11:30:55 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 13 Nov 2012 09:30:55 -0700 Subject: creating size-limited tar files In-Reply-To: References: <509ab0fa$0$6636$9b4e6d93@newsspool2.arcor-online.net> <509AD812.2060605@gmail.com> Message-ID: On Tue, Nov 13, 2012 at 9:25 AM, Ian Kelly wrote: > Sorry, the example I gave above is wrong. If you're calling > p1.communicate(), then you need to first remove the p1.stdout pipe > from the Popen object. Otherwise, the communicate() call will try to > read data from it and may "steal" input from p2. It should look more > like this: > >>>> p1 = subprocess.Popen(cmd1, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) >>>> p2 = subprocess.Popen(cmd2, shell=True, stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE) >>>> p1.stdout = None Per the docs, that third line should be "p1.stdout.close()". :-P From d at davea.name Tue Nov 13 13:01:17 2012 From: d at davea.name (Dave Angel) Date: Tue, 13 Nov 2012 13:01:17 -0500 Subject: xml data or other? In-Reply-To: <509CFD13.9080206@gmail.com> References: <509CFD13.9080206@gmail.com> Message-ID: <50A28AED.1000209@davea.name> On 11/09/2012 07:54 AM, Artie Ziff wrote: > Hello, > > I want to process XML-like data like this: > > > > ACPI (Advanced Control Power & Integration) testscript for 2.5 > kernels. > > <\description> > > ltp/testcases/kernel/device-drivers/acpi/ltpacpi.sh > <\test_location> > <\testname> > > > > Is there a name for the format above (perhaps xhtml)? The only word I can think of is "broken." xml and html and xhtml all use forward slashes. > I'd like to find a python module that can translate it to proper xml. > Does one exist? etree? > I think you've already figured it out. Just take your description and turn it into Python. in other words, replace all "<\" with "" with " /", although your example doesn't happen to have any of these. Tack a xml header on, and try to parse it with etree. If you can't, then let someone manually fix it. Or better, fix the program upstream that's creating this mess. There isn't a reliable way to "fix" all the possible broken xml it might be creating, without reverse engineering it. -- DaveA From rriegel at akiban.com Tue Nov 13 13:12:50 2012 From: rriegel at akiban.com (Robert Riegel) Date: Tue, 13 Nov 2012 13:12:50 -0500 Subject: Upcoming Webinar "SQLAlchemy Creator Mike Bayer on How and Why to Integrate Akiban and SQLAlchemy" Message-ID: [image: Akiban] *Upcoming Webinar:* "SQLAlchemy Creator Mike Bayer on How and Why to Integrate Akiban and SQLAlchemy" Hi guys, I wanted to take a minute and invite you and the Python user group to our next free webinar on SQLAchemy: Presented by: Mike Bayer, Creator of SQLAchemy,Ori Herrnstadt, Creator of Akiban Date: Thursday, December 13, 2012[image: Register Here]Time: 2:30 pm ET / 11:30 am PT *SQLAlchemy + Akiban: A very interesting combo* What good is a database if you can?t use it with the language or framework you depend on? From a developer?s perspective, not very. Even though Akiban understands SQL, developers often work at higher level with Object-Relational Mappers (ORM) like Hibernate, SQLAlchemy, or ActiveRecord. Very few developers operate on pure SQL these days. Clearly, we?ve been thinking about this question at Akiban. As we?ve been developing our new database we?ve taken time to work closely with open source developers like Mike Bayer, creator of the SQLAlchemy project, to make sure that developers have access to libraries that understand the Akiban database. Akiban's unique storage approach means that not only does SQL run a lot faster because most joins are free, but also it is possible to access a full object directly within the database. Through the use of Akiban, businesses can quickly implement new services such as real-time dynamic search, social-local-mobile applications, relationship discovery and real-time insight. Akiban enables immediate access to your operational data without negatively impacting operational performance. This also eliminates the Extract-Transform-Load effort and time delay to transfer operational data into a data mart or data warehouse. SQLAlchemy is the first ORM we?ve approached about table grouping, our novel approach to storing data and returning nested result sets in JSON. As Bayer explains, while other databases have provided a JSON view of a relational database, Akiban?s unique approach is at a much deeper level. We?re not just returning data as JSON, we?ve decided to store our data in a hierarchical format on disk. It didn?t take Bayer long to adapt SQLAlchemy to these nested results, nor did it take long to write an extension to psyopg2 that could understand the output of a query that involved table groups. What you'll take away from this webinar: - Brief intro to Akiban. - Snapshot of SQLAlchemy 0.8, new features and additions. - Example of how easy it is to execute a query that returns a nested resultset in Python using SQLAlchemy. - Overview of two recently developed projects: 1. *Akiban for Python* - an extension for the psycopg2 DBAPI. 2. *SQLAlchemy Akiban* - an Akiban dialect and ORM extension for SQLAlchemy. Duration: 1 hour [image: Register] Hope to see you there. Regards, Akiban Team *P.S.* If you are unable to make it to the live webinar, register anyway, and we'll share the recording after the event. *Akiban* 560 Harrison Avenue, Suite 411 Boston, MA 02445 United States of America Contact Us -- Robert Riegel @akiban skype: robert.c.riegel.III | 617-826-9990 -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Tue Nov 13 13:16:45 2012 From: emile at fenx.com (emile) Date: Tue, 13 Nov 2012 10:16:45 -0800 Subject: How to only get a list of the names of the non-directory files in current directory ('.')? In-Reply-To: <31a0765f-c366-4f19-8d6c-bc95e0e51265@googlegroups.com> References: <31a0765f-c366-4f19-8d6c-bc95e0e51265@googlegroups.com> Message-ID: On 11/06/2012 03:12 PM, iMath wrote: > how to get a list of names of everything in the current directory ? Start by working through the tutorial to get familiar with python at http://docs.python.org/2/tutorial/ then for your specific question, review the content at http://www.diveintopython.net/file_handling/os_module.html Emile BTW, googling for "python how to get a list of names of everything in the current directory" yields some good information as well. Google is your friend for this level of question. Not sure anymore beyond that... From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Tue Nov 13 13:19:38 2012 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Tue, 13 Nov 2012 19:19:38 +0100 Subject: stackoverflow quote on Python In-Reply-To: References: <50a1ddba$0$21742$c3e8da3$76491128@news.astraweb.com> Message-ID: Am 13.11.2012 14:21 schrieb wxjmfauth at gmail.com: >> * strings are now proper text strings (Unicode), not byte strings; > > Let me laugh. Do so. Thomas From steve+comp.lang.python at pearwood.info Tue Nov 13 13:38:25 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Nov 2012 18:38:25 GMT Subject: Error messages from format() References: Message-ID: <50a293a0$0$29999$c3e8da3$5496439d@news.astraweb.com> On Tue, 13 Nov 2012 10:08:59 -0500, Colin J. Williams wrote: > Is there some way to get more informative error messages from the > builtin format? Yes -- post a feature request on the Python bug tracker, then wait until Python 3.4 comes out in about 16 months. :( > Most messages are such as: > Traceback (most recent call last): > File "", line 1, in > ValueError: Invalid conversion specification > > This example doesn't point to the first invalid case. Better error messages would be valuable. > [Dbg]>>> format((25, 31),'{0^9o} a(1:9x}') > Traceback (most recent call last): > File "", line 1, in > ValueError: Invalid conversion specification I see at least three problems. (1) The first brace substitution is missing the colon between the argument selector "0" and the format spec "^9o": should be "{0:^9o}". (2) The second format string has an opening round bracket instead of brace: (1:9x} (3) But you are confusing the str.format method with the format function. The format function doesn't take brace substitutions! The string format method takes a template including brace substitutions, plus multiple "objects to be substituted", like this: py> '{0:^9o} a{1:9x}'.format(25, 31) ' 31 a 1f' In this case, the template '{0:^9o} a{1:9x}' requires two arguments since it has two substitutions, {0} and {1}. Each substitution has a format spec following the colon: {0:^9o} and {1:9x} But the format function only takes a single "object to be substituted", and so doesn't take a brace substitution. Instead, it just takes the format spec part: py> format(25, '^9o') ' 31 ' py> format(31, '^9o') ' 37 ' format will not split a tuple into multiple arguments for you, since the tuple is considered a single object. -- Steven From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Tue Nov 13 13:52:30 2012 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Tue, 13 Nov 2012 19:52:30 +0100 Subject: how to simulate tar filename substitution across piped subprocess.Popen() calls? In-Reply-To: <509c588a$0$6904$e4fe514c@news2.news.xs4all.nl> References: <509c588a$0$6904$e4fe514c@news2.news.xs4all.nl> Message-ID: Am 09.11.2012 02:12 schrieb Hans Mulder: > That's what 'xargs' will do for you. All you need to do, is invoke > xargs with arguments containing '{}'. I.e., something like: > > cmd1 = ['tar', '-czvf', 'myfile.tgz', '-c', mydir, 'mysubdir'] > first_process = subprocess.Popen(cmd1, stdout=subprocess.PIPE) > > cmd2 = ['xargs', '-I', '{}', 'sh', '-c', "test -f %s/'{}'" % mydir] > second_process = subprocess.Popen(cmd2, stdin=first_process.stdout) After launching second_process, it might be useful to firstprocess.stdout.close(). If you fail to do so, your process is a second reader which might break things apart. At least, I once hat issues with it; I currently cannot recapitulate what these were nor how they could arise; maybe there was just the open file descriptor which annoyed me. Thomas From jeanmichel at sequans.com Tue Nov 13 14:00:42 2012 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Tue, 13 Nov 2012 20:00:42 +0100 (CET) Subject: Passing functions as parameter (multiprocessing) In-Reply-To: Message-ID: <2001728939.1281970.1352833242895.JavaMail.root@sequans.com> ----- Original Message ----- > Oscar Benjamin wrote: > > > I don't know if this is to do with the way that the code was > > simplified before posting but this subproc function wrapper does > > nothing (even after Peter fixed it below). This code is needlessly > > complicated for what it does. > > Jean-Michel's Post had the following comment preceding his subproc() > definition > > >>> # here it should be decorated, but for this example to be kept > >>> simple, the function is only wrapped, doing nothing special > > which I unhelpfully removed... > > -- > http://mail.python.org/mailman/listinfo/python-list > Anyway thank you all, I solved the issue with Peter's code. func was rebound after each iteration, though I though it wasn't. I would not have figured it out by myself, my mind was lost in the mysteries of subprocesses, decorated and wrapped methods. Cheers, JM -- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you. From livewebcamsexs at gmail.com Tue Nov 13 14:36:59 2012 From: livewebcamsexs at gmail.com (MoneyMaker) Date: Tue, 13 Nov 2012 11:36:59 -0800 (PST) Subject: Welcome to Horror !!!!!! Message-ID: <54a518cb-2f44-4aa5-a6a3-d6fc56890df7@ez26g2000vbb.googlegroups.com> This site is intended for all horror fans. The site have been collected and collected links to horror fans and joining a membership you can add the best moments of terror. http://horrorhorrorhorror.webs.com/ From cjw at ncf.ca Tue Nov 13 15:24:53 2012 From: cjw at ncf.ca (Colin J. Williams) Date: Tue, 13 Nov 2012 15:24:53 -0500 Subject: Error messages from format() In-Reply-To: <50a293a0$0$29999$c3e8da3$5496439d@news.astraweb.com> References: <50a293a0$0$29999$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 13/11/2012 1:38 PM, Steven D'Aprano wrote: > On Tue, 13 Nov 2012 10:08:59 -0500, Colin J. Williams wrote: > >> Is there some way to get more informative error messages from the >> builtin format? > > Yes -- post a feature request on the Python bug tracker, then wait until > Python 3.4 comes out in about 16 months. > > :( > Many thanks :) I am working on the assumption that the first argument of the format builtin function and be a sequence of values, which can be selected with {1:}, {2:}, {0:} etc. The docs don't make this clear. I would appreciate advice. Colin W. > [snip] From rosuav at gmail.com Tue Nov 13 15:53:21 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 14 Nov 2012 07:53:21 +1100 Subject: How to only get a list of the names of the non-directory files in current directory ('.')? In-Reply-To: References: <31a0765f-c366-4f19-8d6c-bc95e0e51265@googlegroups.com> Message-ID: On Wed, Nov 14, 2012 at 5:16 AM, emile wrote: > BTW, googling for "python how to get a list of names of everything in the > current directory" yields some good information as well. Google is your > friend for this level of question. Not sure anymore beyond that... It yields it? You mean Google is an iterator? From gordon at panix.com Tue Nov 13 16:05:48 2012 From: gordon at panix.com (John Gordon) Date: Tue, 13 Nov 2012 21:05:48 +0000 (UTC) Subject: How to only get a list of the names of the non-directory files in current directory ('.')? References: <31a0765f-c366-4f19-8d6c-bc95e0e51265@googlegroups.com> Message-ID: In Chris Angelico writes: > It yields it? You mean Google is an iterator? ITYM generator. -- John Gordon A is for Amy, who fell down the stairs gordon at panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" From d at davea.name Tue Nov 13 16:18:07 2012 From: d at davea.name (Dave Angel) Date: Tue, 13 Nov 2012 16:18:07 -0500 Subject: Error messages from format() In-Reply-To: References: <50a293a0$0$29999$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50A2B90F.1020303@davea.name> On 11/13/2012 03:24 PM, Colin J. Williams wrote: > > > I am working on the assumption that the first argument of the format > builtin function and be a sequence of values, which can be selected > with {1:}, {2:}, {0:} etc. > > The docs don't make this clear. I would appreciate advice. > The built-in function format(): http://docs.python.org/3.3/library/functions.html?highlight=format%20builtin#format The first parameter is a single object, NOT a sequence. One object, one format. If you want more generality, use the str.format() method: http://docs.python.org/3.3/library/stdtypes.html?highlight=format#str.format where you can supply a list or a dictionary of multiple items to be formatted into a single string. That's the one where you supply the curly braces. -- DaveA From rosuav at gmail.com Tue Nov 13 16:19:13 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 14 Nov 2012 08:19:13 +1100 Subject: How to only get a list of the names of the non-directory files in current directory ('.')? In-Reply-To: References: <31a0765f-c366-4f19-8d6c-bc95e0e51265@googlegroups.com> Message-ID: On Wed, Nov 14, 2012 at 8:05 AM, John Gordon wrote: > In Chris Angelico writes: > >> It yields it? You mean Google is an iterator? > > ITYM generator. Yeah, that thing. Troll fail. *whoops* ChrisA From emile at fenx.com Tue Nov 13 16:24:51 2012 From: emile at fenx.com (emile) Date: Tue, 13 Nov 2012 13:24:51 -0800 Subject: How to only get a list of the names of the non-directory files in current directory ('.')? In-Reply-To: References: <31a0765f-c366-4f19-8d6c-bc95e0e51265@googlegroups.com> Message-ID: On 11/13/2012 01:19 PM, Chris Angelico wrote: > Troll fail. > > *whoops* *sigh* mine too. Emile From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Tue Nov 13 16:36:47 2012 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Tue, 13 Nov 2012 22:36:47 +0100 Subject: how to simulate tar filename substitution across piped subprocess.Popen() calls? In-Reply-To: <50a1402d$0$6947$e4fe514c@news2.news.xs4all.nl> References: <509c588a$0$6904$e4fe514c@news2.news.xs4all.nl> <5b39febc-fbb9-4d8e-b331-0401e78d2b7f@m13g2000vbd.googlegroups.com> <50a12560$0$6972$e4fe514c@news2.news.xs4all.nl> <50a1402d$0$6947$e4fe514c@news2.news.xs4all.nl> Message-ID: Am 12.11.2012 19:30 schrieb Hans Mulder: > This will break if there are spaces in the file name, or other > characters meaningful to the shell. If you change if to > > xargsproc.append("test -f '%s/{}'&& md5sum '%s/{}'" > % (mydir, mydir)) > > , then it will only break if there are single quotes in the file name. And if you do mydir_q = mydir.replace("'", "'\\''") and use mydir_q, you should be safe... Thomas From fn681 at ncf.ca Tue Nov 13 16:42:53 2012 From: fn681 at ncf.ca (Colin J. Williams) Date: Tue, 13 Nov 2012 16:42:53 -0500 Subject: Error messages from format() In-Reply-To: <50A2B90F.1020303@davea.name> References: <50a293a0$0$29999$c3e8da3$5496439d@news.astraweb.com> <50A2B90F.1020303@davea.name> Message-ID: <50A2BEDD.3010201@ncf.ca> An HTML attachment was scrubbed... URL: From michael.weylandt at gmail.com Tue Nov 13 17:14:32 2012 From: michael.weylandt at gmail.com (R. Michael Weylandt) Date: Tue, 13 Nov 2012 22:14:32 +0000 Subject: Division matrix In-Reply-To: <98b451e1-5cd5-46e9-8be4-59dcc835700b@googlegroups.com> References: <98b451e1-5cd5-46e9-8be4-59dcc835700b@googlegroups.com> Message-ID: On Tue, Nov 13, 2012 at 1:00 AM, Cleuson Alves wrote: > Hello, I need to solve an exercise follows, first calculate the inverse matrix and then multiply the first matrix. I would just point out that in most numerical applications, you rarely need to calculate the intermediate of the matrix inverse directly. See, e.g., http://www.johndcook.com/blog/2010/01/19/dont-invert-that-matrix/ Of course, if this hasn't been said yet: NumPy. Michael From steve+comp.lang.python at pearwood.info Tue Nov 13 17:54:46 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Nov 2012 22:54:46 GMT Subject: Error messages from format() References: <50a293a0$0$29999$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50a2cfb6$0$29999$c3e8da3$5496439d@news.astraweb.com> On Tue, 13 Nov 2012 15:24:53 -0500, Colin J. Williams wrote: > On 13/11/2012 1:38 PM, Steven D'Aprano wrote: >> On Tue, 13 Nov 2012 10:08:59 -0500, Colin J. Williams wrote: >> >>> Is there some way to get more informative error messages from the >>> builtin format? >> >> Yes -- post a feature request on the Python bug tracker, then wait >> until Python 3.4 comes out in about 16 months. >> >> :( >> > Many thanks :) > > I am working on the assumption that the first argument of the format > builtin function and be a sequence of values, which can be selected with > {1:}, {2:}, {0:} etc. Um, did you read the rest of my post? I already told you that this is incorrect. -- Steven From richardbp at gmail.com Tue Nov 13 18:20:53 2012 From: richardbp at gmail.com (Richard) Date: Tue, 13 Nov 2012 15:20:53 -0800 (PST) Subject: Generate unique ID for URL Message-ID: <0692e6a2-343c-4eb0-be57-fe5c815efb99@googlegroups.com> Hello, I want to create a URL-safe unique ID for URL's. Currently I use: url_id = base64.urlsafe_b64encode(url) >>> base64.urlsafe_b64encode('docs.python.org/library/uuid.html') 'ZG9jcy5weXRob24ub3JnL2xpYnJhcnkvdXVpZC5odG1s' I would prefer more concise ID's. What do you recommend? - Compression? Richard From gordon at panix.com Tue Nov 13 18:34:02 2012 From: gordon at panix.com (John Gordon) Date: Tue, 13 Nov 2012 23:34:02 +0000 (UTC) Subject: Generate unique ID for URL References: <0692e6a2-343c-4eb0-be57-fe5c815efb99@googlegroups.com> Message-ID: In <0692e6a2-343c-4eb0-be57-fe5c815efb99 at googlegroups.com> Richard writes: > I want to create a URL-safe unique ID for URL's. > Currently I use: > url_id = base64.urlsafe_b64encode(url) > >>> base64.urlsafe_b64encode('docs.python.org/library/uuid.html') > 'ZG9jcy5weXRob24ub3JnL2xpYnJhcnkvdXVpZC5odG1s' > I would prefer more concise ID's. > What do you recommend? - Compression? Does the ID need to contain all the information necessary to recreate the original URL? -- John Gordon A is for Amy, who fell down the stairs gordon at panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" From smaran.harihar at gmail.com Tue Nov 13 18:52:30 2012 From: smaran.harihar at gmail.com (Smaran Harihar) Date: Tue, 13 Nov 2012 16:52:30 -0700 Subject: Zipping files Message-ID: Hi Guys, I am trying to create a script which will zip a bunch of files that I have obtained using the 'glob' So I have a bunch of files, glob.glob("shp/file1*) And I want to zip all the files which are returned by the above command. How can I do that? Is there a zip library in python? -- Thanks & Regards Smaran Harihar -------------- next part -------------- An HTML attachment was scrubbed... URL: From richardbp at gmail.com Tue Nov 13 18:56:39 2012 From: richardbp at gmail.com (Richard) Date: Tue, 13 Nov 2012 15:56:39 -0800 (PST) Subject: Generate unique ID for URL In-Reply-To: References: <0692e6a2-343c-4eb0-be57-fe5c815efb99@googlegroups.com> Message-ID: <133e0be5-63af-4f72-9d0a-c59b04aa4ce4@googlegroups.com> Good point - one way encoding would be fine. Also this is performed millions of times so ideally efficient. On Wednesday, November 14, 2012 10:34:03 AM UTC+11, John Gordon wrote: > In <0692e6a2-343c-4eb0-be57-fe5c815efb99 at googlegroups.com> Richard writes: > > > > > I want to create a URL-safe unique ID for URL's. > > > Currently I use: > > > url_id = base64.urlsafe_b64encode(url) > > > > > >>> base64.urlsafe_b64encode('docs.python.org/library/uuid.html') > > > 'ZG9jcy5weXRob24ub3JnL2xpYnJhcnkvdXVpZC5odG1s' > > > > > I would prefer more concise ID's. > > > What do you recommend? - Compression? > > > > Does the ID need to contain all the information necessary to recreate the > > original URL? > > > > -- > > John Gordon A is for Amy, who fell down the stairs > > gordon at panix.com B is for Basil, assaulted by bears > > -- Edward Gorey, "The Gashlycrumb Tinies" From miki.tebeka at gmail.com Tue Nov 13 19:13:58 2012 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Tue, 13 Nov 2012 16:13:58 -0800 (PST) Subject: Generate unique ID for URL In-Reply-To: <0692e6a2-343c-4eb0-be57-fe5c815efb99@googlegroups.com> References: <0692e6a2-343c-4eb0-be57-fe5c815efb99@googlegroups.com> Message-ID: <3ee369e5-aeea-426a-bf83-da3daeac6c4b@googlegroups.com> > I want to create a URL-safe unique ID for URL's. > What do you recommend? - Compression? You can use base62 with a running counter, but then you'll need a (semi) centralized entity to come up with the next id. You can see one implementation at http://bit.ly/PSJkHS (AppEngine environment). From ckaynor at zindagigames.com Tue Nov 13 19:26:19 2012 From: ckaynor at zindagigames.com (Chris Kaynor) Date: Tue, 13 Nov 2012 16:26:19 -0800 Subject: Generate unique ID for URL In-Reply-To: <133e0be5-63af-4f72-9d0a-c59b04aa4ce4@googlegroups.com> References: <0692e6a2-343c-4eb0-be57-fe5c815efb99@googlegroups.com> <133e0be5-63af-4f72-9d0a-c59b04aa4ce4@googlegroups.com> Message-ID: One option would be using a hash. Python's built-in hash, a 32-bit CRC, 128-bit MD5, 256-bit SHA or one of the many others that exist, depending on the needs. Higher bit counts will reduce the odds of accidental collisions; cryptographically secure ones if outside attacks matter. In such a case, you'd have to roll your own means of converting the hash back into the string if you ever need it for debugging, and there is always the possibility of collisions. A similar solution would be using a pseudo-random GUID using the url as the seed. You could use a counter if all IDs are generated by a single process (and even in other cases with some work). If you want to be able to go both ways, using base64 encoding is probably your best bet, though you might get benefits by using compression. Chris On Tue, Nov 13, 2012 at 3:56 PM, Richard wrote: > Good point - one way encoding would be fine. > > Also this is performed millions of times so ideally efficient. > > > On Wednesday, November 14, 2012 10:34:03 AM UTC+11, John Gordon wrote: >> In <0692e6a2-343c-4eb0-be57-fe5c815efb99 at googlegroups.com> Richard writes: >> >> >> >> > I want to create a URL-safe unique ID for URL's. >> >> > Currently I use: >> >> > url_id = base64.urlsafe_b64encode(url) >> >> >> >> > >>> base64.urlsafe_b64encode('docs.python.org/library/uuid.html') >> >> > 'ZG9jcy5weXRob24ub3JnL2xpYnJhcnkvdXVpZC5odG1s' >> >> >> >> > I would prefer more concise ID's. >> >> > What do you recommend? - Compression? >> >> >> >> Does the ID need to contain all the information necessary to recreate the >> >> original URL? >> >> >> >> -- >> >> John Gordon A is for Amy, who fell down the stairs >> >> gordon at panix.com B is for Basil, assaulted by bears >> >> -- Edward Gorey, "The Gashlycrumb Tinies" > > -- > http://mail.python.org/mailman/listinfo/python-list From a.akhavan.b at gmail.com Tue Nov 13 19:33:17 2012 From: a.akhavan.b at gmail.com (Ali Akhavan) Date: Tue, 13 Nov 2012 16:33:17 -0800 (PST) Subject: Detect file is locked - windows Message-ID: <2af436e3-1336-42ae-aa2f-9e33135a3f2c@googlegroups.com> I am trying to open a file in 'w' mode open('file', 'wb'). open() will throw with IOError with errno 13 if the file is locked by another application or if user does not have permission to open/write to the file. How can I distinguish these two cases ? Namely, if some application has the file open or not. Thanks, nomadali From breamoreboy at yahoo.co.uk Tue Nov 13 19:38:45 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 14 Nov 2012 00:38:45 +0000 Subject: Zipping files In-Reply-To: References: Message-ID: On 13/11/2012 23:52, Smaran Harihar wrote: > Hi Guys, > > I am trying to create a script which will zip a bunch of files that I have > obtained using the 'glob' > > So I have a bunch of files, > > glob.glob("shp/file1*) > > And I want to zip all the files which are returned by the above command. > How can I do that? Is there a zip library in python? > http://docs.python.org/2/library/zipfile.html Any reason that you couldn't have found this out for yourself, given that there are several search engines available? -- Cheers. Mark Lawrence. From richardbp at gmail.com Tue Nov 13 19:41:21 2012 From: richardbp at gmail.com (Richard Baron Penman) Date: Wed, 14 Nov 2012 11:41:21 +1100 Subject: Generate unique ID for URL In-Reply-To: References: <0692e6a2-343c-4eb0-be57-fe5c815efb99@googlegroups.com> <133e0be5-63af-4f72-9d0a-c59b04aa4ce4@googlegroups.com> Message-ID: I found the MD5 and SHA hashes slow to calculate. The builtin hash is fast but I was concerned about collisions. What rate of collisions could I expect? Outside attacks not an issue and multiple processes would be used. On Wed, Nov 14, 2012 at 11:26 AM, Chris Kaynor wrote: > One option would be using a hash. Python's built-in hash, a 32-bit > CRC, 128-bit MD5, 256-bit SHA or one of the many others that exist, > depending on the needs. Higher bit counts will reduce the odds of > accidental collisions; cryptographically secure ones if outside > attacks matter. In such a case, you'd have to roll your own means of > converting the hash back into the string if you ever need it for > debugging, and there is always the possibility of collisions. A > similar solution would be using a pseudo-random GUID using the url as > the seed. > > You could use a counter if all IDs are generated by a single process > (and even in other cases with some work). > > If you want to be able to go both ways, using base64 encoding is > probably your best bet, though you might get benefits by using > compression. > Chris > > > On Tue, Nov 13, 2012 at 3:56 PM, Richard wrote: >> Good point - one way encoding would be fine. >> >> Also this is performed millions of times so ideally efficient. >> >> >> On Wednesday, November 14, 2012 10:34:03 AM UTC+11, John Gordon wrote: >>> In <0692e6a2-343c-4eb0-be57-fe5c815efb99 at googlegroups.com> Richard writes: >>> >>> >>> >>> > I want to create a URL-safe unique ID for URL's. >>> >>> > Currently I use: >>> >>> > url_id = base64.urlsafe_b64encode(url) >>> >>> >>> >>> > >>> base64.urlsafe_b64encode('docs.python.org/library/uuid.html') >>> >>> > 'ZG9jcy5weXRob24ub3JnL2xpYnJhcnkvdXVpZC5odG1s' >>> >>> >>> >>> > I would prefer more concise ID's. >>> >>> > What do you recommend? - Compression? >>> >>> >>> >>> Does the ID need to contain all the information necessary to recreate the >>> >>> original URL? >>> >>> >>> >>> -- >>> >>> John Gordon A is for Amy, who fell down the stairs >>> >>> gordon at panix.com B is for Basil, assaulted by bears >>> >>> -- Edward Gorey, "The Gashlycrumb Tinies" >> >> -- >> http://mail.python.org/mailman/listinfo/python-list From christian at python.org Tue Nov 13 19:43:00 2012 From: christian at python.org (Christian Heimes) Date: Wed, 14 Nov 2012 01:43:00 +0100 Subject: Generate unique ID for URL In-Reply-To: References: <0692e6a2-343c-4eb0-be57-fe5c815efb99@googlegroups.com> <133e0be5-63af-4f72-9d0a-c59b04aa4ce4@googlegroups.com> Message-ID: <50A2E914.5060106@python.org> Am 14.11.2012 01:26, schrieb Chris Kaynor: > One option would be using a hash. Python's built-in hash, a 32-bit > CRC, 128-bit MD5, 256-bit SHA or one of the many others that exist, > depending on the needs. Higher bit counts will reduce the odds of > accidental collisions; cryptographically secure ones if outside > attacks matter. In such a case, you'd have to roll your own means of > converting the hash back into the string if you ever need it for > debugging, and there is always the possibility of collisions. A > similar solution would be using a pseudo-random GUID using the url as > the seed. A hash is the wrong answer to the issue as a hash is open to all sorts of attack vectors like length extension attack. If Robert needs to ensure any kind of collision resistance than he needs a MAC, for example a HMAC with a secret key. If he needs some kind of persistent identifier than some like a URN or DOI may be a better answer. Christian From richardbp at gmail.com Tue Nov 13 19:50:28 2012 From: richardbp at gmail.com (Richard) Date: Tue, 13 Nov 2012 16:50:28 -0800 (PST) Subject: Generate unique ID for URL In-Reply-To: References: <0692e6a2-343c-4eb0-be57-fe5c815efb99@googlegroups.com> <133e0be5-63af-4f72-9d0a-c59b04aa4ce4@googlegroups.com> Message-ID: <9feb1237-b495-4367-8108-4d6291a5a05a@googlegroups.com> These URL ID's would just be used internally for quick lookups, not exposed publicly in a web application. Ideally I would want to avoid collisions altogether. But if that means significant extra CPU time then 1 collision in 10 million hashes would be tolerable. From christian at python.org Tue Nov 13 19:59:53 2012 From: christian at python.org (Christian Heimes) Date: Wed, 14 Nov 2012 01:59:53 +0100 Subject: Generate unique ID for URL In-Reply-To: References: <0692e6a2-343c-4eb0-be57-fe5c815efb99@googlegroups.com> <133e0be5-63af-4f72-9d0a-c59b04aa4ce4@googlegroups.com> Message-ID: <50A2ED09.1000300@python.org> Am 14.11.2012 01:41, schrieb Richard Baron Penman: > I found the MD5 and SHA hashes slow to calculate. > The builtin hash is fast but I was concerned about collisions. What > rate of collisions could I expect? Seriously? It takes about 1-5msec to sha1() one MB of data on a modern CPU, 1.5 on my box. The openssl variants of Python's hash code release the GIL so you use the power of all cores. From christian at python.org Tue Nov 13 20:05:57 2012 From: christian at python.org (Christian Heimes) Date: Wed, 14 Nov 2012 02:05:57 +0100 Subject: Generate unique ID for URL In-Reply-To: <9feb1237-b495-4367-8108-4d6291a5a05a@googlegroups.com> References: <0692e6a2-343c-4eb0-be57-fe5c815efb99@googlegroups.com> <133e0be5-63af-4f72-9d0a-c59b04aa4ce4@googlegroups.com> <9feb1237-b495-4367-8108-4d6291a5a05a@googlegroups.com> Message-ID: Am 14.11.2012 01:50, schrieb Richard: > These URL ID's would just be used internally for quick lookups, not exposed publicly in a web application. > > Ideally I would want to avoid collisions altogether. But if that means significant extra CPU time then 1 collision in 10 million hashes would be tolerable. Are you storing the URLs in any kind of database like a SQL database? A proper index on the data column will avoid full table scans. It will give you almost O(1) complexity on lookups and O(n) worst case complexity for collisions. From breamoreboy at yahoo.co.uk Tue Nov 13 20:14:59 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 14 Nov 2012 01:14:59 +0000 Subject: Detect file is locked - windows In-Reply-To: <2af436e3-1336-42ae-aa2f-9e33135a3f2c@googlegroups.com> References: <2af436e3-1336-42ae-aa2f-9e33135a3f2c@googlegroups.com> Message-ID: On 14/11/2012 00:33, Ali Akhavan wrote: > I am trying to open a file in 'w' mode open('file', 'wb'). open() will throw with IOError with errno 13 if the file is locked by another application or if user does not have permission to open/write to the file. > > How can I distinguish these two cases ? Namely, if some application has the file open or not. > > Thanks, > nomadali > Anything here help http://www.python.org/dev/peps/pep-3151/ ? -- Cheers. Mark Lawrence. From richardbp at gmail.com Tue Nov 13 20:18:00 2012 From: richardbp at gmail.com (Richard) Date: Tue, 13 Nov 2012 17:18:00 -0800 (PST) Subject: Generate unique ID for URL In-Reply-To: References: <0692e6a2-343c-4eb0-be57-fe5c815efb99@googlegroups.com> <133e0be5-63af-4f72-9d0a-c59b04aa4ce4@googlegroups.com> Message-ID: <90679521-52f9-409c-b6ad-5970863c0cff@googlegroups.com> I found md5 / sha 4-5 times slower than hash. And base64 a lot slower. No database or else I would just use their ID. On Wednesday, November 14, 2012 11:59:55 AM UTC+11, Christian Heimes wrote: > Am 14.11.2012 01:41, schrieb Richard Baron Penman: > > > I found the MD5 and SHA hashes slow to calculate. > > > The builtin hash is fast but I was concerned about collisions. What > > > rate of collisions could I expect? > > > > Seriously? It takes about 1-5msec to sha1() one MB of data on a modern > > CPU, 1.5 on my box. The openssl variants of Python's hash code release > > the GIL so you use the power of all cores. From richardbp at gmail.com Tue Nov 13 20:18:00 2012 From: richardbp at gmail.com (Richard) Date: Tue, 13 Nov 2012 17:18:00 -0800 (PST) Subject: Generate unique ID for URL In-Reply-To: References: <0692e6a2-343c-4eb0-be57-fe5c815efb99@googlegroups.com> <133e0be5-63af-4f72-9d0a-c59b04aa4ce4@googlegroups.com> Message-ID: <90679521-52f9-409c-b6ad-5970863c0cff@googlegroups.com> I found md5 / sha 4-5 times slower than hash. And base64 a lot slower. No database or else I would just use their ID. On Wednesday, November 14, 2012 11:59:55 AM UTC+11, Christian Heimes wrote: > Am 14.11.2012 01:41, schrieb Richard Baron Penman: > > > I found the MD5 and SHA hashes slow to calculate. > > > The builtin hash is fast but I was concerned about collisions. What > > > rate of collisions could I expect? > > > > Seriously? It takes about 1-5msec to sha1() one MB of data on a modern > > CPU, 1.5 on my box. The openssl variants of Python's hash code release > > the GIL so you use the power of all cores. From roy at panix.com Tue Nov 13 20:39:04 2012 From: roy at panix.com (Roy Smith) Date: Tue, 13 Nov 2012 20:39:04 -0500 Subject: Generate unique ID for URL References: <0692e6a2-343c-4eb0-be57-fe5c815efb99@googlegroups.com> Message-ID: In article <0692e6a2-343c-4eb0-be57-fe5c815efb99 at googlegroups.com>, Richard wrote: > Hello, > > I want to create a URL-safe unique ID for URL's. > Currently I use: > url_id = base64.urlsafe_b64encode(url) > > >>> base64.urlsafe_b64encode('docs.python.org/library/uuid.html') > 'ZG9jcy5weXRob24ub3JnL2xpYnJhcnkvdXVpZC5odG1s' > > I would prefer more concise ID's. > What do you recommend? - Compression? If you're generating random id strings, there's only two ways to make them shorter. Either encode fewer bits of information, or encode them more compactly. Let's start with the second one. You're already using base64, so you're getting 6 bits per character. You can do a little better than that, but not much. The set of URL-safe characters is the 96-ish printable ascii set, minus a few pieces of punctuation. Maybe you could get it up to 6.3 or 6.4 bits per character, but that's about it. For the complexity this would add it's probably not worth it. The next step is to reduce the number of bits you are encoding. You said in another post that "1 collision in 10 million hashes would be tolerable". So you need: >>> math.log(10*1000*1000, 2) 23.25349666421154 24 bits worth of key. Base64 encoded, that's only 4 characters. Actually, I probably just proved that I don't really understand how probabilities work, so maybe what you really need is 32 or 48 or 64 bits. Certainly not the 264 bits you're encoding with your example above. So, something like: hash = md5.md5('docs.python.org/library/uuid.html').digest() hash64 = base64.urlsafe_b64encode(hash) id = hash64[:8] # or 12, or whatever But, I still don't really understand your use case. You've already mentioned the following requirements: "just be used internally for quick lookups, not exposed publicly" "URL-safe" "unique" "1 collision in 10 million hashes would be tolerable" "one way encoding would be fine" "performed millions of times so ideally efficient" but haven't really explained what it is that you're trying to do. If they're not going to be exposed publicly, why do you care if they're URL-safe? What's wrong with just using the URLs directly as dictionary keys and not worrying about it until you've got some hard data showing that this is not sufficient? From steve+comp.lang.python at pearwood.info Tue Nov 13 21:04:26 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 14 Nov 2012 02:04:26 GMT Subject: Generate unique ID for URL References: <0692e6a2-343c-4eb0-be57-fe5c815efb99@googlegroups.com> <3ee369e5-aeea-426a-bf83-da3daeac6c4b@googlegroups.com> Message-ID: <50a2fc2a$0$21742$c3e8da3$76491128@news.astraweb.com> On Tue, 13 Nov 2012 16:13:58 -0800, Miki Tebeka wrote: >> I want to create a URL-safe unique ID for URL's. What do you recommend? >> - Compression? > You can use base62 with a running counter, but then you'll need a (semi) > centralized entity to come up with the next id. > > You can see one implementation at http://bit.ly/PSJkHS (AppEngine > environment). Perhaps this is a silly question, but if you're using a running counter, why bother with base64? Decimal or hex digits are URL safe. If there are no concerns about predictability, why not just use the counter directly? You can encode a billion IDs in 8 hex digits compared to 16 base64 characters: py> base64.urlsafe_b64encode('1000000000') 'MTAwMDAwMDAwMA==' py> "%x" % 1000000000 '3b9aca00' Short and sweet and easy: no base64 calculation, no hash function, no database lookup, just a trivial int to string conversion. -- Steven From showell30 at yahoo.com Tue Nov 13 21:32:40 2012 From: showell30 at yahoo.com (Steve Howell) Date: Tue, 13 Nov 2012 18:32:40 -0800 (PST) Subject: Generate unique ID for URL References: <0692e6a2-343c-4eb0-be57-fe5c815efb99@googlegroups.com> <3ee369e5-aeea-426a-bf83-da3daeac6c4b@googlegroups.com> <50a2fc2a$0$21742$c3e8da3$76491128@news.astraweb.com> Message-ID: On Nov 13, 6:04?pm, Steven D'Aprano wrote: > On Tue, 13 Nov 2012 16:13:58 -0800, Miki Tebeka wrote: > >> I want to create a URL-safe unique ID for URL's. What do you recommend? > >> - Compression? > > You can use base62 with a running counter, but then you'll need a (semi) > > centralized entity to come up with the next id. > > > You can see one implementation athttp://bit.ly/PSJkHS(AppEngine > > environment). > > Perhaps this is a silly question, but if you're using a running counter, > why bother with base64? Decimal or hex digits are URL safe. If there are > no concerns about predictability, why not just use the counter directly? > > You can encode a billion IDs in 8 hex digits compared to 16 base64 > characters: > > py> base64.urlsafe_b64encode('1000000000') > 'MTAwMDAwMDAwMA==' > py> "%x" % 1000000000 > '3b9aca00' > > Short and sweet and easy: no base64 calculation, no hash function, no > database lookup, just a trivial int to string conversion. > > -- > Steven If you're dealing entirely with integers, then this works too: import base64 def encode(n): s = '' while n > 0: s += chr(n % 256) n //= 256 return base64.urlsafe_b64encode(s) def test(): seen = set() for i in range(999900000, 1000000000): s = encode(i) if s in seen: raise Exception('non-unique encoding') seen.add(s) print encode(1000000000) test() It prints this for 1000000000: AMqaOw== From richardbp at gmail.com Tue Nov 13 22:12:56 2012 From: richardbp at gmail.com (Richard) Date: Tue, 13 Nov 2012 19:12:56 -0800 (PST) Subject: Generate unique ID for URL In-Reply-To: References: <0692e6a2-343c-4eb0-be57-fe5c815efb99@googlegroups.com> <3ee369e5-aeea-426a-bf83-da3daeac6c4b@googlegroups.com> <50a2fc2a$0$21742$c3e8da3$76491128@news.astraweb.com> Message-ID: <016c6b8a-6da4-4439-92af-8e223867ec52@googlegroups.com> I am dealing with URL's rather than integers From richardbp at gmail.com Tue Nov 13 22:25:32 2012 From: richardbp at gmail.com (Richard) Date: Tue, 13 Nov 2012 19:25:32 -0800 (PST) Subject: Generate unique ID for URL In-Reply-To: References: <0692e6a2-343c-4eb0-be57-fe5c815efb99@googlegroups.com> Message-ID: <1ce88f36-bfc7-4a55-89f8-70d1645d27ad@googlegroups.com> So the use case - I'm storing webpages on disk and want a quick retrieval system based on URL. I can't store the files in a single directory because of OS limitations so have been using a sub folder structure. For example to store data at URL "abc": a/b/c/index.html This data is also viewed locally through a web app. If you can suggest a better approach I would welcome it. From richardbp at gmail.com Tue Nov 13 22:27:17 2012 From: richardbp at gmail.com (Richard) Date: Tue, 13 Nov 2012 19:27:17 -0800 (PST) Subject: Generate unique ID for URL In-Reply-To: References: <0692e6a2-343c-4eb0-be57-fe5c815efb99@googlegroups.com> Message-ID: <61a09732-e7e4-466b-9316-96636a9241d7@googlegroups.com> > The next step is to reduce the number of bits you are encoding. You > > said in another post that "1 collision in 10 million hashes would be > > tolerable". So you need: > > > > >>> math.log(10*1000*1000, 2) > > 23.25349666421154 I think a difficulty would be finding a hash algorithm that maps evenly across those bits. From joyhou2010 at gmail.com Tue Nov 13 22:31:31 2012 From: joyhou2010 at gmail.com (Caroline Hou) Date: Tue, 13 Nov 2012 19:31:31 -0800 (PST) Subject: Simple Question regarding running .py program In-Reply-To: <8085ebf2-9f08-4889-84c9-f786d6776001@googlegroups.com> References: <22ae6963-6700-4ea7-b396-344d89ead7f2@googlegroups.com> <8085ebf2-9f08-4889-84c9-f786d6776001@googlegroups.com> Message-ID: On Tuesday, November 13, 2012 7:35:32 AM UTC-5, Ramchandra Apte wrote: > On Tuesday, 13 November 2012 08:15:45 UTC+5:30, Caroline Hou wrote: > > > On Monday, 12 November 2012 21:25:08 UTC-5, Dave Angel wrote: > > > > > > > On 11/12/2012 09:02 PM, Caroline Hou wrote: > > > > > > > > > > > > > > > Hi all! > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > I just started learning Python by myself and I have an extremely simple question now! > > > > > > > > > > > > > > > I am in my Python interpreter now and I want to open/edit a program called nobel.py. But when I typed >>> python nobel.py, it gave me a "SyntaxError:invalid syntax?( I've changed to the correct directory)what should I do? > > > > > > > > > > > > > > > I also want to run the program, but as I double-clicked the program, a command window pops up and closes immediately. How can I see the result of the program run? > > > > > > > > > > > > > > > Could anyone help me please? I am pretty confused here...Thank you! > > > > > > > > > > > > > > > > > > > > > > > > > > > > It'd be nice to specify that you're running Windows, and also what > > > > > > > > > > > > > > version of the interpreter, although in this case the latter doesn't matter. > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > Go to a shell (cmd.exe), change to the directory containing that script, > > > > > > > > > > > > > > and type the command as you did. > > > > > > > > > > > > > > > > > > > > > > > > > > > > On linux: davea at think:~$ python nobel.py > > > > > > > > > > > > > > On Windows: c:\mydir\myscript > python nobel.py > > > > > > > > > > > > > > > > > > > > > > > > > > > > If you're already in the python interpreter, then running python is > > > > > > > > > > > > > > useless -- it's already running. In that case, you might want to use > > > > > > > > > > > > > > import. However, I recommend against it at first, as it opens up some > > > > > > > > > > > > > > other problems you haven't experience with yet. > > > > > > > > > > > > > > > > > > > > > > > > > > > > When you say you "double clicked the program', we have to guess you > > > > > > > > > > > > > > might have meant in MS Explorer. If you do that, it launches a cmd, it > > > > > > > > > > > > > > runs the python system, and it closes the cmd. Blame Windows for not > > > > > > > > > > > > > > reading your mind. If you want the cmd window to stick around, you > > > > > > > > > > > > > > COULD end your program with an raw_input function call, but frequently > > > > > > > > > > > > > > that won't work. The right answer is the first one above... Open a > > > > > > > > > > > > > > shell (perhaps with a menu like DOS BOX), change... > > > > > > > > > > > > > > > > > > > > > > > > > > > > That way, when the program finishes, you can see what happened, or > > > > > > > > > > > > > > didn't happen, and you can run it again using the uparrow key. > > > > > > > > > > > > > > > > > > > > > > > > > > > > BTW, you don't need to send email to both the python-list and to the > > > > > > > > > > > > > > newsgroup. The newsgroup is automatically fed from the list. But since > > > > > > > > > > > > > > you're posting from google groups, that's just one of the bugs. Many > > > > > > > > > > > > > > folks here simply filter out everything from google groups, so your post > > > > > > > > > > > > > > is invisible to them. > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > -- > > > > > > > > > > > > > > > > > > > > > > > > > > > > DaveA > > > > > > > > > > > > Hi Dave! > > > > > > > > > > > > thank you very much for your quick reply! I did manage to get the program run from cmd.exe. > > > > > > So does it mean that if I want to use python interactively,I should use the interpreter,while if I just want to run a python program, I should use DOS shell instead? > > > > > > Also, how could I edit my script? I have sth called "IDLE" installed along with python. Is it the right place to write/edit my script? > > > > > > Sorry about these semi-idiot questions but it is really hard to find an article or book that covers such basic stuffs! > > > > > > Thank you! > > > > > > > > > > > > Caroline Hou > > > > IDLE is recommended for newbies like you because an IDE requires too much configuration. > > When you start writing a big project, you can use an IDE. Thank you Dave and everybody here for your helpful comments!This place is awesome! I found this group when I googled python-list. Seems like this is not the usual way you guys access the list? From wrw at mac.com Tue Nov 13 22:34:55 2012 From: wrw at mac.com (wrw at mac.com) Date: Tue, 13 Nov 2012 22:34:55 -0500 Subject: Subprocess puzzle and two questions Message-ID: <09CE8A6B-19D5-46D6-BF36-F8BC7926A0DD@mac.com> I need to time the operation of a command-line utility (specifically nslookup) from within a python program I'm writing. I don't want to use python's timeit function because I'd like to avoid python's subprocess creation overhead. That leads me to the standard UNIX time function. So for example, in my bash shell, if I enter: $ time nslookup www.es.net 8.8.4.4 I get: Server: 8.8.4.4 Address: 8.8.4.4#53 Non-authoritative answer: www.es.net canonical name = www3.es.net. Name: www3.es.net Address: 128.55.22.201 real 0m0.069s user 0m0.006s sys 0m0.004s The first lines are the result of an nslookup of the IP address of "www.es.net" using the server at 8.8.4.4 (Google's public DNS server b). The last three lines are what I'm after: the real elapsed wall-clock time, the time spent in user space and the time spent in kernel space. However, if I try the same operation in the python interpreter using subprocess.Popen like so: >>> import subprocess >>> result = subprocess.Popen(['time', 'nslookup', 'www.es.net', '8.8.4.4'], shell = False, stdout = subprocess.PIPE, stderr = subprocess.PIPE).communicate() >>> print result ('Server:\t\t8.8.4.4\nAddress:\t8.8.4.4#53\n\nNon-authoritative answer:\nwww.es.net\tcanonical name = www3.es.net.\nName:\twww3.es.net\nAddress: 128.55.22.201\n\n', ' 0.06 real 0.00 user 0.00 sys\n') And the timing information I'm after has been truncated to two digits after the decimal. It appears that Popen is applying a default format. If I do explicit formatting: >>> time = result[1].lstrip().split(' ')[0] >>> formatted_time = '{: >7.3f}'.format(float(time)) >>> print formatted_time 0.060 I get three digits, BUT that third digit isn't real, the format operation has simply appended a zero. So: 1) how can I recover that third digit from the subprocess? 2) is there a more pythonic way to do what I'm trying to do? python 2.7, OS-X 10.8.2 Thanks in advance - Bill Wing From roy at panix.com Tue Nov 13 22:38:50 2012 From: roy at panix.com (Roy Smith) Date: Tue, 13 Nov 2012 22:38:50 -0500 Subject: Generate unique ID for URL References: <0692e6a2-343c-4eb0-be57-fe5c815efb99@googlegroups.com> <1ce88f36-bfc7-4a55-89f8-70d1645d27ad@googlegroups.com> Message-ID: In article <1ce88f36-bfc7-4a55-89f8-70d1645d27ad at googlegroups.com>, Richard wrote: > So the use case - I'm storing webpages on disk and want a quick retrieval > system based on URL. > I can't store the files in a single directory because of OS limitations so > have been using a sub folder structure. > For example to store data at URL "abc": a/b/c/index.html > This data is also viewed locally through a web app. > > If you can suggest a better approach I would welcome it. Ah, so basically, you're reinventing Varnish? Maybe do what Varnish (and MongoDB, and a few other things) do? Bypass the file system entirely. Juar mmap() a chunk of memory large enough to hold everything and let the OS figure out how to page things to disk. From richardbp at gmail.com Tue Nov 13 22:56:19 2012 From: richardbp at gmail.com (Richard) Date: Tue, 13 Nov 2012 19:56:19 -0800 (PST) Subject: Generate unique ID for URL In-Reply-To: References: <0692e6a2-343c-4eb0-be57-fe5c815efb99@googlegroups.com> <1ce88f36-bfc7-4a55-89f8-70d1645d27ad@googlegroups.com> Message-ID: <2d7deb3f-0517-438f-8b98-fb9dc2979b1e@googlegroups.com> thanks for pointer to Varnish. I found MongoDB had a lot of size overhead so that it ended up using 4x the data stored. From rosuav at gmail.com Tue Nov 13 23:06:10 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 14 Nov 2012 15:06:10 +1100 Subject: Generate unique ID for URL In-Reply-To: <1ce88f36-bfc7-4a55-89f8-70d1645d27ad@googlegroups.com> References: <0692e6a2-343c-4eb0-be57-fe5c815efb99@googlegroups.com> <1ce88f36-bfc7-4a55-89f8-70d1645d27ad@googlegroups.com> Message-ID: On Wed, Nov 14, 2012 at 2:25 PM, Richard wrote: > So the use case - I'm storing webpages on disk and want a quick retrieval system based on URL. > I can't store the files in a single directory because of OS limitations so have been using a sub folder structure. > For example to store data at URL "abc": a/b/c/index.html > This data is also viewed locally through a web app. > > If you can suggest a better approach I would welcome it. The cost of a crypto hash on the URL will be completely dwarfed by the cost of storing/retrieving on disk. You could probably do some arithmetic and figure out exactly how many URLs (at an average length of, say, 100 bytes) you can hash in the time of one disk seek. ChrisA From rosuav at gmail.com Tue Nov 13 23:10:54 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 14 Nov 2012 15:10:54 +1100 Subject: Simple Question regarding running .py program In-Reply-To: References: <22ae6963-6700-4ea7-b396-344d89ead7f2@googlegroups.com> <8085ebf2-9f08-4889-84c9-f786d6776001@googlegroups.com> Message-ID: On Wed, Nov 14, 2012 at 2:31 PM, Caroline Hou wrote: > Thank you Dave and everybody here for your helpful comments!This place is awesome! I found this group when I googled python-list. Seems like this is not the usual way you guys access the list? There are several ways to communicate with this list. * The comp.lang.python newsgroup - get a newsreader (there are plenty around), and either connect to your ISP's news server (if they have one that carries c.l.p) or to a public server, some of which cost money. * Use a news-to-web gateway such as Google Groups. That specific one is deprecated on this list, as there's more noise than signal from Google Groups. * The mailing list python-list, delivered directly to your inbox many times a day. This is what I personally use. Try here: http://mail.python.org/mailman/listinfo/python-list ChrisA From richardbp at gmail.com Tue Nov 13 23:14:02 2012 From: richardbp at gmail.com (Richard) Date: Tue, 13 Nov 2012 20:14:02 -0800 (PST) Subject: Generate unique ID for URL In-Reply-To: References: <0692e6a2-343c-4eb0-be57-fe5c815efb99@googlegroups.com> <1ce88f36-bfc7-4a55-89f8-70d1645d27ad@googlegroups.com> Message-ID: yeah good point - I have gone with md5 for now. On Wednesday, November 14, 2012 3:06:18 PM UTC+11, Chris Angelico wrote: > On Wed, Nov 14, 2012 at 2:25 PM, Richard wrote: > > > So the use case - I'm storing webpages on disk and want a quick retrieval system based on URL. > > > I can't store the files in a single directory because of OS limitations so have been using a sub folder structure. > > > For example to store data at URL "abc": a/b/c/index.html > > > This data is also viewed locally through a web app. > > > > > > If you can suggest a better approach I would welcome it. > > > > The cost of a crypto hash on the URL will be completely dwarfed by the > > cost of storing/retrieving on disk. You could probably do some > > arithmetic and figure out exactly how many URLs (at an average length > > of, say, 100 bytes) you can hash in the time of one disk seek. > > > > ChrisA From richardbp at gmail.com Tue Nov 13 23:14:02 2012 From: richardbp at gmail.com (Richard) Date: Tue, 13 Nov 2012 20:14:02 -0800 (PST) Subject: Generate unique ID for URL In-Reply-To: References: <0692e6a2-343c-4eb0-be57-fe5c815efb99@googlegroups.com> <1ce88f36-bfc7-4a55-89f8-70d1645d27ad@googlegroups.com> Message-ID: yeah good point - I have gone with md5 for now. On Wednesday, November 14, 2012 3:06:18 PM UTC+11, Chris Angelico wrote: > On Wed, Nov 14, 2012 at 2:25 PM, Richard wrote: > > > So the use case - I'm storing webpages on disk and want a quick retrieval system based on URL. > > > I can't store the files in a single directory because of OS limitations so have been using a sub folder structure. > > > For example to store data at URL "abc": a/b/c/index.html > > > This data is also viewed locally through a web app. > > > > > > If you can suggest a better approach I would welcome it. > > > > The cost of a crypto hash on the URL will be completely dwarfed by the > > cost of storing/retrieving on disk. You could probably do some > > arithmetic and figure out exactly how many URLs (at an average length > > of, say, 100 bytes) you can hash in the time of one disk seek. > > > > ChrisA From roy at panix.com Tue Nov 13 23:41:24 2012 From: roy at panix.com (Roy Smith) Date: Tue, 13 Nov 2012 23:41:24 -0500 Subject: Subprocess puzzle and two questions References: Message-ID: In article , wrw at mac.com wrote: > I need to time the operation of a command-line utility (specifically > nslookup) from within a python program I'm writing. Ugh. Why are you doing this? Shelling out to nslookup is an incredibly slow and clumsy way of doing name translation. What you really want to be doing is calling getaddrinfo() directly. See http://docs.python.org/2/library/socket.html#socket.getaddrinfo for details. From wrw at mac.com Wed Nov 14 00:03:51 2012 From: wrw at mac.com (William Ray Wing) Date: Wed, 14 Nov 2012 00:03:51 -0500 Subject: Subprocess puzzle and two questions In-Reply-To: References: Message-ID: <56334BE7-3ED0-492A-8FC7-78CE013BDD5E@mac.com> On Nov 13, 2012, at 11:41 PM, Roy Smith wrote: > In article , > wrw at mac.com wrote: > >> I need to time the operation of a command-line utility (specifically >> nslookup) from within a python program I'm writing. > > Ugh. Why are you doing this? Shelling out to nslookup is an incredibly > slow and clumsy way of doing name translation. What you really want to > be doing is calling getaddrinfo() directly. > > See http://docs.python.org/2/library/socket.html#socket.getaddrinfo for > details. > -- Because, unless I'm badly mistaken (very possible), getaddrinfo doesn't let me specify the server from which the name is returned. I'm really not after the name, what I'm REALLY after is the fact that a path exists to the name server I specify (and how long it takes to respond). In the "good old days" I would just have ping'd it, but these days more and more DNS boxes (and servers of all sorts) are shutting off their ping response. Thanks, Bill From rurpy at yahoo.com Wed Nov 14 00:08:45 2012 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Tue, 13 Nov 2012 21:08:45 -0800 (PST) Subject: Simple Question regarding running .py program In-Reply-To: References: <22ae6963-6700-4ea7-b396-344d89ead7f2@googlegroups.com> <8085ebf2-9f08-4889-84c9-f786d6776001@googlegroups.com> Message-ID: <5f1bf680-ca10-446e-abbc-d3ba55327f2d@googlegroups.com> On 11/13/2012 09:10 PM, Chris Angelico wrote: > On Wed, Nov 14, 2012 at 2:31 PM, Caroline Hou wrote: >> Thank you Dave and everybody here for your helpful comments!This >> place is awesome! I found this group when I googled python-list. >> Seems like this is not the usual way you guys access the list? > > There are several ways to communicate with this list. > [...] > * Use a news-to-web gateway such as Google Groups. That > specific one is deprecated on this list, as there's more > noise than signal from Google Groups. Caroline, Chris is mistaken about this, if for no other reason than there is no authority here empowered to decide to deprecate anything. What Chris should have said is that there are some people on this list who don't like Google Groups for whatever reason and encourage others to ignore posts from Google Groups. How successful this boycott effort is is not clear. I use Google Groups as it suits my needs better than any of the alternatives, and so do many others. Both of the other alternatives Chris mentioned involve too much setup or overhead for those who read/post here only occasionally. GG fills this niche adequately if used with care. If you do use Google Groups to post, there are a couple of things you should be careful of: * You'll sometimes see a checkbox above the GG send window that is a CC to the python mailing list () which is checked by default. Uncheck that before sending, or the list will get two copies of your message. * GG doesn't do a very good job in quoting the post you are replying to. If you look at your recent post here: http://mail.python.org/pipermail/python-list/2012-November/635070.html (or on GG) you will see lots and lots of lines empty save for the ">" quote markers. This makes a post hard to read. A way to avoid this is to remove the blank extra blank lines in the GG send window by hand before posting. Alternatively, many email programs have a "paste as quotation" option when writing mail. What I do is to open a blank new email message, copy the original post I'm replying to from GG, paste-as-quotation into the new mail window, then copy and paste back into the GG send window. Pretty easy to do once you get used to it. Hope this helps and provides a little more accurate info about posting from GG than has been provided so far. From rosuav at gmail.com Wed Nov 14 01:02:48 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 14 Nov 2012 17:02:48 +1100 Subject: Simple Question regarding running .py program In-Reply-To: <5f1bf680-ca10-446e-abbc-d3ba55327f2d@googlegroups.com> References: <22ae6963-6700-4ea7-b396-344d89ead7f2@googlegroups.com> <8085ebf2-9f08-4889-84c9-f786d6776001@googlegroups.com> <5f1bf680-ca10-446e-abbc-d3ba55327f2d@googlegroups.com> Message-ID: On Wed, Nov 14, 2012 at 4:08 PM, wrote: > On 11/13/2012 09:10 PM, Chris Angelico wrote: >> * Use a news-to-web gateway such as Google Groups. That >> specific one is deprecated on this list, as there's more >> noise than signal from Google Groups. > > Caroline, Chris is mistaken about this, if for no other > reason than there is no authority here empowered to decide > to deprecate anything. What Chris should have said is > that there are some people on this list who don't like > Google Groups for whatever reason and encourage others > to ignore posts from Google Groups. > > How successful this boycott effort is is not clear. To be more accurate: This is deprecated *by members of* this list. As there is no commanding/controlling entity here, it's up to each individual to make a decision - for instance, abusive users get killfiled rather than banned. The use of Google Groups to post is deprecated in the original sense of the word: strongly disapproved of. My own opinion on the matter is that if it takes as much effort as you describe to use GG properly, it's wasting your time on a massive scale. Surely it's easier to read and post email? ChrisA From kushal.kumaran+python at gmail.com Wed Nov 14 01:05:04 2012 From: kushal.kumaran+python at gmail.com (Kushal Kumaran) Date: Wed, 14 Nov 2012 11:35:04 +0530 Subject: creating size-limited tar files In-Reply-To: References: <509ab0fa$0$6636$9b4e6d93@newsspool2.arcor-online.net> <509AD812.2060605@gmail.com> Message-ID: <87lie4lnxr.fsf@nitrogen.i-did-not-set--mail-host-address--so-tickle-me> Ian Kelly writes: > On Tue, Nov 13, 2012 at 3:31 AM, andrea crotti > wrote: >> but it's a bit ugly. I wonder if I can use the subprocess PIPEs to do >> the same thing, is it going to be as fast and work in the same way?? > > It'll look something like this: > >>>> p1 = subprocess.Popen(cmd1, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) >>>> p2 = subprocess.Popen(cmd2, shell=True, stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE) >>>> p1.communicate() > ('', '') >>>> p2.communicate() > ('', '') >>>> p1.wait() > 0 >>>> p2.wait() > 0 > > Note that there's a subtle potential for deadlock here. During the > p1.communicate() call, if the p2 output buffer fills up, then it will > stop accepting input from p1 until p2.communicate() can be called, and > then if that buffer also fills up, p1 will hang. Additionally, if p2 > needs to wait on the parent process for some reason, then you end up > effectively serializing the two processes. > > Solution would be to poll all the open-ended pipes in a select() loop > instead of using communicate(), or perhaps make the two communicate > calls simultaneously in separate threads. Or, you could just change the p1's stderr to an io.BytesIO instance. Then call p2.communicate *first*. -- regards, kushal From amit.amitagrawal.agrawal at gmail.com Wed Nov 14 01:35:45 2012 From: amit.amitagrawal.agrawal at gmail.com (Amit Agrawal) Date: Wed, 14 Nov 2012 12:05:45 +0530 Subject: access spreadsheet data Message-ID: my problem is, i want to access data in spreadsheet to python code manualy My data is 1/1982 8:00:00 0 1/2/1982 8:00:00 0 1/3/1982 8:00:00 0 1/4/1982 8:00:00 0 1/5/1982 8:00:00 0.7885 1/6/1982 8:00:00 0 1/7/1982 8:00:00 0 1/8/1982 8:00:00 1.6127 1/9/1982 8:00:00 0 1/10/1982 8:00:00 0 1/11/1982 8:00:00 0 1/12/1982 8:00:00 1.6744 1/13/1982 8:00:00 0 1/14/1982 8:00:00 1.5583 1/15/1982 8:00:00 1.7249 1/16/1982 8:00:00 0 1/17/1982 8:00:00 0 1/18/1982 8:00:00 0 1/19/1982 8:00:00 0 1/20/1982 8:00:00 0lly -------------- next part -------------- An HTML attachment was scrubbed... URL: From kushal.kumaran+python at gmail.com Wed Nov 14 01:55:19 2012 From: kushal.kumaran+python at gmail.com (Kushal Kumaran) Date: Wed, 14 Nov 2012 12:25:19 +0530 Subject: Subprocess puzzle and two questions In-Reply-To: <09CE8A6B-19D5-46D6-BF36-F8BC7926A0DD@mac.com> References: <09CE8A6B-19D5-46D6-BF36-F8BC7926A0DD@mac.com> Message-ID: <87ip98llm0.fsf@nitrogen.i-did-not-set--mail-host-address--so-tickle-me> wrw at mac.com writes: > I need to time the operation of a command-line utility (specifically nslookup) from within a python program I'm writing. I don't want to use python's timeit function because I'd like to avoid python's subprocess creation overhead. That leads me to the standard UNIX time function. So for example, in my bash shell, if I enter: > It is unclear to me what overhead you are avoiding. > $ time nslookup www.es.net 8.8.4.4 > > I get: > > Server: 8.8.4.4 > Address: 8.8.4.4#53 > > Non-authoritative answer: > www.es.net canonical name = www3.es.net. > Name: www3.es.net > Address: 128.55.22.201 > > real 0m0.069s > user 0m0.006s > sys 0m0.004s > > The first lines are the result of an nslookup of the IP address of "www.es.net" using the server at 8.8.4.4 (Google's public DNS server b). > The last three lines are what I'm after: the real elapsed wall-clock time, the time spent in user space and the time spent in kernel space. > > However, if I try the same operation in the python interpreter using subprocess.Popen like so: > >>>> import subprocess >>>> result = subprocess.Popen(['time', 'nslookup', 'www.es.net', '8.8.4.4'], shell = False, stdout = subprocess.PIPE, stderr = subprocess.PIPE).communicate() >>>> print result > ('Server:\t\t8.8.4.4\nAddress:\t8.8.4.4#53\n\nNon-authoritative answer:\nwww.es.net\tcanonical name = www3.es.net.\nName:\twww3.es.net\nAddress: 128.55.22.201\n\n', ' 0.06 real 0.00 user 0.00 sys\n') > > And the timing information I'm after has been truncated to two digits after the decimal. It appears that Popen is applying a default format. If I do explicit formatting: > >>>> time = result[1].lstrip().split(' ')[0] >>>> formatted_time = '{: >7.3f}'.format(float(time)) >>>> print formatted_time > 0.060 > > I get three digits, BUT that third digit isn't real, the format operation has simply appended a zero. So: > > 1) how can I recover that third digit from the subprocess? > 2) is there a more pythonic way to do what I'm trying to do? > > python 2.7, OS-X 10.8.2 > It is possible that the "time" invocation from the shell is invoking your shell's builtin time implementation, and your python code is running /usr/bin/time or /bin/time. You should see the same behaviour from the shell if you run /bin/time or /usr/bin/time (whatever you have) instead of just "time". subprocess.Popen should never modify the output of programs it runs. -- regards, kushal From breamoreboy at yahoo.co.uk Wed Nov 14 01:58:39 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 14 Nov 2012 06:58:39 +0000 Subject: access spreadsheet data In-Reply-To: References: Message-ID: On 14/11/2012 06:35, Amit Agrawal wrote: > my problem is, i want to access data in spreadsheet to python code manualy > My data is > > 1/1982 8:00:00 0 > Please tell us your spreadsheet (CSV file, M$ Excel, OS X Numbers, what?), your OS and Python version, and what exactly you're trying to achieve, then you're far more likely to get some useful answers. -- Cheers. Mark Lawrence. From rurpy at yahoo.com Wed Nov 14 02:02:36 2012 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Tue, 13 Nov 2012 23:02:36 -0800 (PST) Subject: Simple Question regarding running .py program In-Reply-To: References: <22ae6963-6700-4ea7-b396-344d89ead7f2@googlegroups.com> <8085ebf2-9f08-4889-84c9-f786d6776001@googlegroups.com> <5f1bf680-ca10-446e-abbc-d3ba55327f2d@googlegroups.com> Message-ID: <6c3c5341-1fb4-4664-80b8-d3e08687830f@googlegroups.com> On 11/13/2012 11:02 PM, Chris Angelico wrote: > On Wed, Nov 14, 2012 at 4:08 PM, rurpy wrote: >> On 11/13/2012 09:10 PM, Chris Angelico wrote: >>> * Use a news-to-web gateway such as Google Groups. That >>> specific one is deprecated on this list, as there's more >>> noise than signal from Google Groups. >> >> Caroline, Chris is mistaken about this, if for no other >> reason than there is no authority here empowered to decide >> to deprecate anything. What Chris should have said is >> that there are some people on this list who don't like >> Google Groups for whatever reason and encourage others >> to ignore posts from Google Groups. >> >> How successful this boycott effort is is not clear. > > To be more accurate: This is deprecated *by members of* this list. As > there is no commanding/controlling entity here, it's up to each > individual to make a decision - for instance, abusive users get > killfiled rather than banned. The use of Google Groups to post is > deprecated in the original sense of the word: strongly disapproved of. s/deprecated *by members of*/deprecated *by some members of*/ (and accuracy could probably be increased further by replacing "some" with "a few".) And again, s/strongly disapproved of/strongly disapproved of by some/ I was objecting to your attempts to make it sound like a fact that GG posts were nearly universally condemned here, an error you repeat again above. > My own opinion on the matter is that if it takes as much effort as you > describe to use GG properly, it's wasting your time on a massive > scale. Surely it's easier to read and post email? "that much effort"? Do you consider a couple of copy- pastes to be "that much effort"? "wasting your time on a massive scale"? Sheesh, get a grip man, let's not get silly. On the other hand finding and configuring a newsreader for someone whose never done it before, as you recommend, is a major time consumer. And signing up for python-list email, posting, dealing with several days' volume, and then signing off because one does not want to read it full time[*], is no picnic either compared to using GG. Sorry, but there are *good* reasons for some people to use GG whether you want to admit it or not. == [*] Actually, now that I think about it, IIRC one can sign up for python-list email, and go into the mailman settings and disable mail delivery, allowing one to post to the list via email yet read the list via GG, Gmane or whatever. However, this is not going to be obvious to many occasional posters, and is still a PITA compared to just posting from GG as our hypothetical user does for all the other groups he/she participates in. From timr at probo.com Wed Nov 14 02:17:14 2012 From: timr at probo.com (Tim Roberts) Date: Tue, 13 Nov 2012 23:17:14 -0800 Subject: Subprocess puzzle and two questions References: Message-ID: wrw at mac.com wrote: >... >However, if I try the same operation in the python interpreter using subprocess.Popen like so: > >>>> import subprocess >>>> result = subprocess.Popen(['time', 'nslookup', 'www.es.net', '8.8.4.4'], shell = False, stdout = subprocess.PIPE, stderr = subprocess.PIPE).communicate() >>>> print result >('Server:\t\t8.8.4.4\nAddress:\t8.8.4.4#53\n\nNon-authoritative answer:\nwww.es.net\tcanonical name = www3.es.net.\nName:\twww3.es.net\nAddress: 128.55.22.201\n\n', ' 0.06 real 0.00 user 0.00 sys\n') > >And the timing information I'm after has been truncated to two digits after the decimal. It appears that Popen is applying a default format. No, that's silly. A few minutes thought should have told you that. In your standalone test, you are getting the "time" command that is built in to bash. In the subprocess example, you've specified "shell = False", so you are using the external "time" command (/usr/bin/time in my system), and that command has a different output format. The csh "time" command is different yet again. >1) how can I recover that third digit from the subprocess? Do you actually believe that the third decimal place has any meaning at all? It doesn't. -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From ian.g.kelly at gmail.com Wed Nov 14 02:22:48 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 14 Nov 2012 00:22:48 -0700 Subject: creating size-limited tar files In-Reply-To: <87lie4lnxr.fsf@nitrogen.i-did-not-set--mail-host-address--so-tickle-me> References: <509ab0fa$0$6636$9b4e6d93@newsspool2.arcor-online.net> <509AD812.2060605@gmail.com> <87lie4lnxr.fsf@nitrogen.i-did-not-set--mail-host-address--so-tickle-me> Message-ID: On Tue, Nov 13, 2012 at 11:05 PM, Kushal Kumaran wrote: > Or, you could just change the p1's stderr to an io.BytesIO instance. > Then call p2.communicate *first*. This doesn't seem to work. >>> b = io.BytesIO() >>> p = subprocess.Popen(["ls", "-l"], stdout=b) Traceback (most recent call last): File "", line 1, in File "/usr/lib64/python3.2/subprocess.py", line 711, in __init__ errread, errwrite) = self._get_handles(stdin, stdout, stderr) File "/usr/lib64/python3.2/subprocess.py", line 1112, in _get_handles c2pwrite = stdout.fileno() io.UnsupportedOperation: fileno I think stdout and stderr need to be actual file objects, not just file-like objects. From rustompmody at gmail.com Wed Nov 14 02:52:29 2012 From: rustompmody at gmail.com (rusi) Date: Tue, 13 Nov 2012 23:52:29 -0800 (PST) Subject: Simple Question regarding running .py program References: <22ae6963-6700-4ea7-b396-344d89ead7f2@googlegroups.com> <8085ebf2-9f08-4889-84c9-f786d6776001@googlegroups.com> <5f1bf680-ca10-446e-abbc-d3ba55327f2d@googlegroups.com> <6c3c5341-1fb4-4664-80b8-d3e08687830f@googlegroups.com> Message-ID: On Nov 14, 12:02?pm, ru... at yahoo.com wrote: > > == > [*] Actually, now that I think about it, IIRC one can sign > up for python-list email, and go into the mailman settings > and disable mail delivery, allowing one to post to the list > via email yet read the list via GG, Gmane or whatever. > However, this is not going to be obvious to many occasional > posters, and is still a PITA compared to just posting from > GG as our hypothetical user does for all the other groups > he/she participates in. Yes this would be (for me) my most preferred option if I could figure out a way of threading into a preexisting thread. From hansmu at xs4all.nl Wed Nov 14 03:22:52 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Wed, 14 Nov 2012 09:22:52 +0100 Subject: how to simulate tar filename substitution across piped subprocess.Popen() calls? In-Reply-To: References: <509c588a$0$6904$e4fe514c@news2.news.xs4all.nl> <5b39febc-fbb9-4d8e-b331-0401e78d2b7f@m13g2000vbd.googlegroups.com> <50a12560$0$6972$e4fe514c@news2.news.xs4all.nl> <50a1402d$0$6947$e4fe514c@news2.news.xs4all.nl> Message-ID: <50a354dd$0$6982$e4fe514c@news2.news.xs4all.nl> On 13/11/12 22:36:47, Thomas Rachel wrote: > Am 12.11.2012 19:30 schrieb Hans Mulder: > >> This will break if there are spaces in the file name, or other >> characters meaningful to the shell. If you change if to >> >> xargsproc.append("test -f '%s/{}'&& md5sum '%s/{}'" >> % (mydir, mydir)) >> >> , then it will only break if there are single quotes in the file name. > > And if you do mydir_q = mydir.replace("'", "'\\''") and use mydir_q, you > should be safe... The problem isn't single quotes in mydir, but single quotes in the files names that 'tar' generates and 'xargs' consumes. In the shell script, these names go directly from tar to xargs via a pipe. If the OP wants to do your replace, his script would have to read the output of tar and do the replace before passing the filenames down a second pipe to xargs. However, once he does that, it's simpler to cut out xargs and invoke "sh" directly. Or even cut out "sh" and "test" and instead use os.path.isfile and then call md5sum directly. And once he does that, he no longer needs to worry about single quotes. The OP has said, he's going to d all that. One step at a time. That sounds like a sensible plan to me. Hope this helps, -- HansM From kushal.kumaran+python at gmail.com Wed Nov 14 03:51:32 2012 From: kushal.kumaran+python at gmail.com (Kushal Kumaran) Date: Wed, 14 Nov 2012 14:21:32 +0530 Subject: creating size-limited tar files In-Reply-To: References: <509ab0fa$0$6636$9b4e6d93@newsspool2.arcor-online.net> <509AD812.2060605@gmail.com> <87lie4lnxr.fsf@nitrogen.i-did-not-set--mail-host-address--so-tickle-me> Message-ID: <87fw4clg8b.fsf@nitrogen.i-did-not-set--mail-host-address--so-tickle-me> Ian Kelly writes: > On Tue, Nov 13, 2012 at 11:05 PM, Kushal Kumaran > wrote: >> Or, you could just change the p1's stderr to an io.BytesIO instance. >> Then call p2.communicate *first*. > > This doesn't seem to work. > >>>> b = io.BytesIO() >>>> p = subprocess.Popen(["ls", "-l"], stdout=b) > Traceback (most recent call last): > File "", line 1, in > File "/usr/lib64/python3.2/subprocess.py", line 711, in __init__ > errread, errwrite) = self._get_handles(stdin, stdout, stderr) > File "/usr/lib64/python3.2/subprocess.py", line 1112, in _get_handles > c2pwrite = stdout.fileno() > io.UnsupportedOperation: fileno > > I think stdout and stderr need to be actual file objects, not just > file-like objects. Well, well, I was wrong, clearly. I wonder if this is fixable. -- regards, kushal From hansmu at xs4all.nl Wed Nov 14 03:55:15 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Wed, 14 Nov 2012 09:55:15 +0100 Subject: Detect file is locked - windows In-Reply-To: References: <2af436e3-1336-42ae-aa2f-9e33135a3f2c@googlegroups.com> Message-ID: <50a35c73$0$6970$e4fe514c@news2.news.xs4all.nl> On 14/11/12 02:14:59, Mark Lawrence wrote: > On 14/11/2012 00:33, Ali Akhavan wrote: >> I am trying to open a file in 'w' mode open('file', 'wb'). open() will >> throw with IOError with errno 13 if the file is locked by another >> application or if user does not have permission to open/write to the >> file. >> >> How can I distinguish these two cases ? Namely, if some application >> has the file open or not. I don't have a Windows machine at hand to try, but this might work: if exc.errno == 13: if os.access('file', os.W_OK): print "Locked by another process" else: print "No permission to write" > Anything here help http://www.python.org/dev/peps/pep-3151/ ? That won't help: in Python 3.3, IOError with errno==13 has been replaced by PermissionError. It still doesn't tell you *why* you got a PermissionError. Hope this helps, -- HansM From wxjmfauth at gmail.com Wed Nov 14 03:56:09 2012 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Wed, 14 Nov 2012 00:56:09 -0800 (PST) Subject: stackoverflow quote on Python In-Reply-To: References: <50a1ddba$0$21742$c3e8da3$76491128@news.astraweb.com> Message-ID: <212572f8-95c0-4e87-b7bd-810206fe5006@googlegroups.com> Le mardi 13 novembre 2012 16:53:30 UTC+1, Mark Lawrence a ?crit?: > On 13/11/2012 13:21, wxjmfauth at gmail.com wrote: > > > Le mardi 13 novembre 2012 06:42:19 UTC+1, Steven D'Aprano a ?crit : > > >> On Tue, 13 Nov 2012 03:08:54 +0000, Mark Lawrence wrote: > > >> > > >> * strings are now proper text strings (Unicode), not byte strings; > > >> > > > > > > ---- > > > > > > Let me laugh. > > > > > > jmf > > > > > > > Presumably because you're looking at yourself in a mirror, and have > > finally realised that you've completely misunderstood the work done with > > unicode in Python 3, specifically Python 3.3? > > > > -- > > Cheers. > > > > Mark Lawrence. -------- I'am still fascinated by the mathematically absurd "negative logic" used in and by the flexible string representation (algorithm). jmf From wxjmfauth at gmail.com Wed Nov 14 04:04:09 2012 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Wed, 14 Nov 2012 01:04:09 -0800 (PST) Subject: Division matrix In-Reply-To: <98b451e1-5cd5-46e9-8be4-59dcc835700b@googlegroups.com> References: <98b451e1-5cd5-46e9-8be4-59dcc835700b@googlegroups.com> Message-ID: <8a13e8d2-1dec-441a-85fe-19ba7a61d1a0@googlegroups.com> Le mardi 13 novembre 2012 02:00:28 UTC+1, Cleuson Alves a ?crit?: > Hello, I need to solve an exercise follows, first calculate the inverse matrix and then multiply the first matrix. > > I await help. > > Thank you. > > follows the code below incomplete. > > > > m = [[1,2,3],[4,5,6],[7,8,9]] > > x = [] > > for i in [0,1,2]: > > y = [] > > for linha in m: > > y.append(linha[i]) > > x.append(y) > > > > print x > > [[1, 4, 7], [2, 5, 8], [3, 6, 9]] > > > > def ProdMatrix(x,b): > > tamL = len(x) > > tamC = len(x[0]) > > c = nullMatrix(tamL,tamC) > > for i in range(tamL): > > for j in range(tamC): > > val = 0 > > for k in range(len(b)): > > val = val + x[i][l]*b[k][j] > > c[i][j] > > return c ------ Pedagogical hint: Before blindly calculating the inverse matrix, it may be a good idea to know if the inverse matrix exists. jmf From mail at timgolden.me.uk Wed Nov 14 04:06:11 2012 From: mail at timgolden.me.uk (Tim Golden) Date: Wed, 14 Nov 2012 09:06:11 +0000 Subject: Detect file is locked - windows In-Reply-To: <50a35c73$0$6970$e4fe514c@news2.news.xs4all.nl> References: <2af436e3-1336-42ae-aa2f-9e33135a3f2c@googlegroups.com> <50a35c73$0$6970$e4fe514c@news2.news.xs4all.nl> Message-ID: <50A35F03.6050904@timgolden.me.uk> On 14/11/2012 08:55, Hans Mulder wrote: > On 14/11/12 02:14:59, Mark Lawrence wrote: >> On 14/11/2012 00:33, Ali Akhavan wrote: >>> I am trying to open a file in 'w' mode open('file', 'wb'). open() will >>> throw with IOError with errno 13 if the file is locked by another >>> application or if user does not have permission to open/write to the >>> file. >>> >>> How can I distinguish these two cases ? Namely, if some application >>> has the file open or not. > > I don't have a Windows machine at hand to try, but this might work: > > if exc.errno == 13: > if os.access('file', os.W_OK): > print "Locked by another process" > else: > print "No permission to write" No luck, I'm afraid. os.access on Windows is basically non-functional (and would have been deprecated if I'd actually got around to doing it). It basically checks the old-style readonly flag and that's it. IOW, you'd return True for a file whose attributes you could read regardless of whether you could read/write the file contents. TJG From rosuav at gmail.com Wed Nov 14 04:07:52 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 14 Nov 2012 20:07:52 +1100 Subject: stackoverflow quote on Python In-Reply-To: <212572f8-95c0-4e87-b7bd-810206fe5006@googlegroups.com> References: <50a1ddba$0$21742$c3e8da3$76491128@news.astraweb.com> <212572f8-95c0-4e87-b7bd-810206fe5006@googlegroups.com> Message-ID: On Wed, Nov 14, 2012 at 7:56 PM, wrote: > I'am still fascinated by the mathematically absurd "negative > logic" used in and by the flexible string representation > (algorithm). I am still fascinated that you persist in comparing a buggy old Python against a bug-free new Python and haven't noticed the difference. ChrisA From dfnsonfsduifb at gmx.de Wed Nov 14 04:44:54 2012 From: dfnsonfsduifb at gmx.de (Johannes Bauer) Date: Wed, 14 Nov 2012 10:44:54 +0100 Subject: Generate unique ID for URL In-Reply-To: References: <0692e6a2-343c-4eb0-be57-fe5c815efb99@googlegroups.com> <133e0be5-63af-4f72-9d0a-c59b04aa4ce4@googlegroups.com> Message-ID: On 14.11.2012 01:41, Richard Baron Penman wrote: > I found the MD5 and SHA hashes slow to calculate. Slow? For URLs? Are you kidding? How many URLs per second do you want to calculate? > The builtin hash is fast but I was concerned about collisions. What > rate of collisions could I expect? MD5 has 16 bytes (128 bit), SHA1 has 20 bytes (160 bit). Utilizing the birthday paradox and some approximations, I can tell you that when using the full MD5 you'd need around 2.609e16 hashes in the same namespace to get a one in a million chance of a collision. That is, 26090000000000000 filenames. For SHA1 This number rises even further and you'd need around 1.71e21 or 1710000000000000000000 hashes in one namespace for the one-in-a-million. I really have no clue about how many URLs you want to hash, and it seems to be LOTS since the speed of MD5 seems to be an issue for you. Let me estimate that you'd want to calculate a million hashes per second then when you use MD5, you'd have about 827 years to fill the namespace up enough to get a one-in-a-million. If you need even more hashes (say a million million per second), I'd suggest you go with SHA-1, giving you 54 years to get the one-in-a-million. Then again, if you went for a million million hashes per second, Python would probably not be the language of your choice. Best regards, Johannes -- >> Wo hattest Du das Beben nochmal GENAU vorhergesagt? > Zumindest nicht ?ffentlich! Ah, der neueste und bis heute genialste Streich unsere gro?en Kosmologen: Die Geheim-Vorhersage. - Karl Kaos ?ber R?diger Thomas in dsa From kiran.m.n at in.ibm.com Wed Nov 14 04:51:34 2012 From: kiran.m.n at in.ibm.com (Kiran N Mallekoppa) Date: Wed, 14 Nov 2012 15:21:34 +0530 Subject: Supported Platforms for Python Message-ID: Hi there! Our team at IBM are exploring the possibility of implementing one of our products using Python. I had a query in this regard. As per IBM's policy, we list details of platforms that our product works on - including the flavors of OS, the versions supported (and sometimes, even the service packs, if it matters) so that it is un-ambiguous to our customers. As an example, you can have a look at this page. Suppose we are riding on Python (i.e., implementing using Python), we need to tell our customers in similar detail as to what platforms we support our products on. I tried to find information about the platforms on which Python is supported from your page. But, it does not detail the versions of OS supported. In this regard, I have the following questions. Thanks for taking your time to respond. 1. Is this information available somewhere? 2. I was pointed to PEP-11, which documents the platforms that are not supported. So, can we take that all active versions of Python (2.7.3 and 3.3, i believe) are supported on all the OS flavors that Python claims to run on -- unless mentioned otherwise in the PEP-11? 3. Also, regarding the following entries listed in the PEP-11. So, any idea which OSes implement these? Name: Linux 1 (Am guessing its the Linux kernel version 1.0?) Unsupported in: Python 2.3 Code removed in: Python 2.4 Name: Systems defining __d6_pthread_create (configure.in) Unsupported in: Python 2.3 Code removed in: Python 2.4 Name: Systems defining PY_PTHREAD_D4, PY_PTHREAD_D6, or PY_PTHREAD_D7 in thread_pthread.h Unsupported in: Python 2.3 Code removed in: Python 2.4 Name: Systems using --with-dl-dld Unsupported in: Python 2.3 Code removed in: Python 2.4 Name: Systems using --without-universal-newlines, Unsupported in: Python 2.3 Code removed in: Python 2.4 Name: Systems using --with-wctype-functions Unsupported in: Python 2.6 Code removed in: Python 2.6 Name: Systems using Mach C Threads Unsupported in: Python 3.2 Code removed in: Python 3.3 Name: Systems using --with-pth (GNU pth threads) Unsupported in: Python 3.2 Code removed in: Python 3.3 Name: Systems using Irix threads Unsupported in: Python 3.2 Code removed in: Python 3.3 Warm Regards, Kiran M N | Software Development (Rational Team Concert for Visual Studio.NET) | IBM Rational | India Software Labs | Email: kiran.m.n at in.ibm.com From: Michael Foord To: webmaster at python.org Cc: Kiran N Mallekoppa/India/IBM at IBMIN Date: 08-11-12 08:10 PM Subject: Re: Supported Platforms for Python On 8 Nov 2012, at 14:36, webmaster at python.org wrote: > On Thu, Nov 08, 2012, Kiran N Mallekoppa wrote: >> >> Suppose we are riding on Python (i.e., implementing using Python), we need >> to tell our customers in similar detail as to what platforms we support our >> products on. I tried to find information about the platforms on which >> Python is supported from your page. But, it does not detail the versions of >> OS supported. >> >> Is this information available somewhere? If not, can this be published on >> your site? > > Not really. ;-) You'll find some on > http://www.python.org/download/other/ > > However, Python is (mostly) plain C and Open Source, which essentially > means that support is available for any platform where people are willing > to invest resources. AIX in particular has always been one of the > problem platforms. > > What this means for you is that if IBM wants to allocate resources to get > Python running on any particular platform, it almost certainly can be > done, and we certainly would appreciate getting any such work contributed > back to the community. > > If you want more information, you're probably best off using one of the > discussion forums listed in your auto-reply. As an addendum note that there is a list of explicitly unsupported platforms (platforms that used to be supported and in which versions of Python support was removed): http://www.python.org/dev/peps/pep-0011/ You can see which platforms we test Python, and the test systems are considered stable, from our buildbots. The Python 2.7 ones are here: http://buildbot.python.org/all/waterfall?category=2.7.stable Another tangible way to support a platform is to provide and maintain a buildbot for running the Python tests on. All the best, Michael Foord > -- > Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ > > "....Normal is what cuts off your sixth finger and your tail..." --Siobhan > -- http://www.voidspace.org.uk/ May you do good and not evil May you find forgiveness for yourself and forgive others May you share freely, never taking more than you give. -- the sqlite blessing http://www.sqlite.org/different.html From mail at timgolden.me.uk Wed Nov 14 05:02:45 2012 From: mail at timgolden.me.uk (Tim Golden) Date: Wed, 14 Nov 2012 10:02:45 +0000 Subject: Detect file is locked - windows In-Reply-To: <2af436e3-1336-42ae-aa2f-9e33135a3f2c@googlegroups.com> References: <2af436e3-1336-42ae-aa2f-9e33135a3f2c@googlegroups.com> Message-ID: <50A36C45.6000407@timgolden.me.uk> On 14/11/2012 00:33, Ali Akhavan wrote: > I am trying to open a file in 'w' mode open('file', 'wb'). open() > will throw with IOError with errno 13 if the file is locked by > another application or if user does not have permission to open/write > to the file. What version of Python are you using? > > How can I distinguish these two cases ? Namely, if some application > has the file open or not. Can I ask what you expect to do differently in each of those cases? In other words, if you can't access the file, you can't access it. (Not to dismiss your question; I just wonder how you're going to handle the different cases) TJG From ulrich.eckhardt at dominolaser.com Wed Nov 14 06:01:59 2012 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Wed, 14 Nov 2012 12:01:59 +0100 Subject: Supported Platforms for Python In-Reply-To: References: Message-ID: <7aacn9-nqq.ln1@satorlaser.homedns.org> Am 14.11.2012 10:51, schrieb Kiran N Mallekoppa: > 1. Is this information available somewhere? > 2. I was pointed to PEP-11, which documents the platforms that are not > supported. So, can we take that all active versions of Python (2.7.3 and > 3.3, i believe) are supported on all the OS flavors that Python claims to > run on -- unless mentioned otherwise in the PEP-11? There is intent to support these platforms, but as with every software that relies on volunteers, the actual extent varies. If you want to be sure that a platform is actively supported, check that the platform has an available and active build bot, because only this detects bitrot to a certain extent. If you want to be sure, create build and test systems for the systems you target yourself, you will then see if it works. > 3. Also, regarding the following entries listed in the PEP-11. So, any idea > which OSes implement these? > Name: Linux 1 (Am guessing its the Linux kernel version > 1.0?) > Unsupported in: Python 2.3 > Code removed in: Python 2.4 Yes, Linux 1 is obsolete and has been for > 10 years. > Name: Systems defining __d6_pthread_create (configure.in) > Unsupported in: Python 2.3 > Code removed in: Python 2.4 > Name: Systems defining PY_PTHREAD_D4, PY_PTHREAD_D6, or PY_PTHREAD_D7 > in thread_pthread.h > Unsupported in: Python 2.3 > Code removed in: Python 2.4 > Name: Systems using --with-dl-dld > Unsupported in: Python 2.3 > Code removed in: Python 2.4 > Name: Systems using --without-universal-newlines, > Unsupported in: Python 2.3 > Code removed in: Python 2.4 > Name: Systems using --with-wctype-functions > Unsupported in: Python 2.6 > Code removed in: Python 2.6 I'm not sure where these are used. > Name: Systems using Mach C Threads > Unsupported in: Python 3.2 > Code removed in: Python 3.3 Mach is a microkernel. I'm not sure if the Mach C Threads interface is obsolete on Mach or if Mach overall isn't supported. Probably irrelevant for the desktop. > Name: Systems using --with-pth (GNU pth threads) > Unsupported in: Python 3.2 > Code removed in: Python 3.3 I think this is targetted at early Linux threads that used fork() while sharing most of the memory space. Obsolete. > Name: Systems using Irix threads > Unsupported in: Python 3.2 > Code removed in: Python 3.3 Irix was a Unix variant shipped with SGI workstations. I don't kknow to what extent this is relevant for you. I think that the main use cases for these machines is 3D rendering/modelling, unless they have been superseeded by common desktop machines. > Kiran M N | Software Development (Rational Team Concert for Visual Studio.NET) Just out of curiosity by one of your RTC users: What nice gimmics are you planning? Cheers! Uli From richardbp at gmail.com Wed Nov 14 06:14:08 2012 From: richardbp at gmail.com (Richard) Date: Wed, 14 Nov 2012 03:14:08 -0800 (PST) Subject: Generate unique ID for URL In-Reply-To: References: <0692e6a2-343c-4eb0-be57-fe5c815efb99@googlegroups.com> <133e0be5-63af-4f72-9d0a-c59b04aa4ce4@googlegroups.com> Message-ID: <0c74dc68-ea47-45d2-a701-a334eea4c22e@googlegroups.com> thanks for perspective! From dfnsonfsduifb at gmx.de Wed Nov 14 06:29:05 2012 From: dfnsonfsduifb at gmx.de (Johannes Bauer) Date: Wed, 14 Nov 2012 12:29:05 +0100 Subject: Generate unique ID for URL In-Reply-To: References: <0692e6a2-343c-4eb0-be57-fe5c815efb99@googlegroups.com> Message-ID: On 14.11.2012 02:39, Roy Smith wrote: > The next step is to reduce the number of bits you are encoding. You > said in another post that "1 collision in 10 million hashes would be > tolerable". So you need: > >>>> math.log(10*1000*1000, 2) > 23.25349666421154 > > 24 bits worth of key. Nope :-) > Base64 encoded, that's only 4 characters. > Actually, I probably just proved that I don't really understand how > probabilities work, so maybe what you really need is 32 or 48 or 64 > bits. :-)) When doing these calculations, it's important to keep the birthday paradox in mind (this is kind of counter-intuitive): The chance of a collission raises tremendously when we're looking for *any* arbitrary two hashes colliding within a certain namespace. The probability you've calculated is the pre-image probability (which you also again need to multiply with a factor of two, because when trying to collide one given hash, in the mean case you'll only have to search *half* the namespace before finding a collision). There are three things you need to know before you can give an estimate: 1. The permissible probability of a collision (1e-7 in this case) 2. The hash size 3. The worst-case number of elements in the namespace You neglected 3 completely -- but knowing this is really important. This becomes obvious when looking at the extreme cases: Let's say you have a hash of arbitrary size, but only hash one element. The chance of a collision is *always* zero. Or look at a hash of size 2^n. Then put 2^n + 1 elements in the namespace. The chance of a collision is *always* one. Doing the calculations (formulas can be found on wikipedia on the site of the birthday phaenomenon), you can come up with these following bitlenghts of the hash with a 1e-7 probability of collision in respect to the worst-case number of elements 10k elements: 49 bit 100k elements: 56 bit 1e6 elements: 63 bit 100e6 elements: 76 bit 1e9 elements: 83 bit 1e12 elements: 102 bit Best regards, Johannes -- >> Wo hattest Du das Beben nochmal GENAU vorhergesagt? > Zumindest nicht ?ffentlich! Ah, der neueste und bis heute genialste Streich unsere gro?en Kosmologen: Die Geheim-Vorhersage. - Karl Kaos ?ber R?diger Thomas in dsa From mail at timgolden.me.uk Wed Nov 14 06:50:49 2012 From: mail at timgolden.me.uk (Tim Golden) Date: Wed, 14 Nov 2012 11:50:49 +0000 Subject: Detect file is locked - windows In-Reply-To: <2af436e3-1336-42ae-aa2f-9e33135a3f2c@googlegroups.com> References: <2af436e3-1336-42ae-aa2f-9e33135a3f2c@googlegroups.com> Message-ID: <50A38599.2010309@timgolden.me.uk> On 14/11/2012 00:33, Ali Akhavan wrote: > I am trying to open a file in 'w' mode open('file', 'wb'). open() > will throw with IOError with errno 13 if the file is locked by > another application or if user does not have permission to open/write > to the file. > > How can I distinguish these two cases ? Namely, if some application > has the file open or not. The Python io module calls into the MS CRT, which maps both errors (ERROR_ACCESS_DENIED & ERROR_SHARING_VIOLATION) to posix errno EACCESS, which is 13. If you really need to distinguish the two situations, you'll need to call CreateFile directly (via ctypes or the pywin32 modules or an extension module) and then call GetLastError() to get the specific condition. You're far better off using this EAFP approach as, even if it were simple to determine beforehand whether a file can be locked or read -- and it's not -- that situation could have changed by the time you actually come to open it. Once you've successfully got a handle to the file, that handle is valid regardless of any later changes to the file's security. TJG From hansmu at xs4all.nl Wed Nov 14 06:51:01 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Wed, 14 Nov 2012 12:51:01 +0100 Subject: Detect file is locked - windows In-Reply-To: References: <2af436e3-1336-42ae-aa2f-9e33135a3f2c@googlegroups.com> Message-ID: <50a385a5$0$6916$e4fe514c@news2.news.xs4all.nl> On 14/11/12 11:02:45, Tim Golden wrote: > On 14/11/2012 00:33, Ali Akhavan wrote: >> I am trying to open a file in 'w' mode open('file', 'wb'). open() >> will throw with IOError with errno 13 if the file is locked by >> another application or if user does not have permission to open/write >> to the file. > > What version of Python are you using? > >> >> How can I distinguish these two cases ? Namely, if some application >> has the file open or not. > > Can I ask what you expect to do differently in each of those cases? In > other words, if you can't access the file, you can't access it. (Not to > dismiss your question; I just wonder how you're going to handle the > different cases) It would be nice if he could give specific error messages, e.g. "Can't write %s because it is locked by %s." vs. "Can't write %s because you don't have write access." I can't speak for Ali, but I'm always annoyed by error messages listing several possible cuases, such as "Can't delete file, because the source or destination is in use". -- HansM From andrea.crotti.0 at gmail.com Wed Nov 14 06:52:59 2012 From: andrea.crotti.0 at gmail.com (andrea crotti) Date: Wed, 14 Nov 2012 11:52:59 +0000 Subject: creating size-limited tar files In-Reply-To: <87fw4clg8b.fsf@nitrogen.i-did-not-set--mail-host-address--so-tickle-me> References: <509ab0fa$0$6636$9b4e6d93@newsspool2.arcor-online.net> <509AD812.2060605@gmail.com> <87lie4lnxr.fsf@nitrogen.i-did-not-set--mail-host-address--so-tickle-me> <87fw4clg8b.fsf@nitrogen.i-did-not-set--mail-host-address--so-tickle-me> Message-ID: 2012/11/14 Kushal Kumaran : > > Well, well, I was wrong, clearly. I wonder if this is fixable. > > -- > regards, > kushal > -- > http://mail.python.org/mailman/listinfo/python-list But would it not be possible to use the pipe in memory in theory? That would be way faster and since I have in theory enough RAM it might be a great improvement.. From mail at timgolden.me.uk Wed Nov 14 07:09:07 2012 From: mail at timgolden.me.uk (Tim Golden) Date: Wed, 14 Nov 2012 12:09:07 +0000 Subject: Detect file is locked - windows In-Reply-To: <50a385a5$0$6916$e4fe514c@news2.news.xs4all.nl> References: <2af436e3-1336-42ae-aa2f-9e33135a3f2c@googlegroups.com> <50a385a5$0$6916$e4fe514c@news2.news.xs4all.nl> Message-ID: <50A389E3.70808@timgolden.me.uk> On 14/11/2012 11:51, Hans Mulder wrote: > It would be nice if he could give specific error messages, e.g. > > "Can't write %s because it is locked by %s." > > vs. > > "Can't write %s because you don't have write access." > > I can't speak for Ali, but I'm always annoyed by error messages > listing several possible cuases, such as "Can't delete file, > because the source or destination is in use". (I realise you're not demanding this particular behaviour from Python but just to expand on what the obstacles are to this at present): Speaking merely from the point of view of the current Python implementation on Windows, there are two obstacles to this: * Python calls into the CRT which simply returns 13 (EACCESS) for both of these situations. Obviously, Python could do its own thing on Windows, partly reimplementing what the CRT does anyway and giving more precise feedback. Equally obviously, this wouldn't be a trivial exercise. * The added information -- who's locked the file, what permissions are in place which prevent you gaining the requested access -- is surprisingly fiddly to get hold of and would be something of an overhead for the majority of the time when it's not wanted. Of course, in this hypothetical Python one could add some sort of flag to the open() function which requested or not the additional information. The first obstacle is more significant than the second but neither is negligible. TJG From d at davea.name Wed Nov 14 07:33:47 2012 From: d at davea.name (Dave Angel) Date: Wed, 14 Nov 2012 07:33:47 -0500 Subject: Generate unique ID for URL In-Reply-To: References: <0692e6a2-343c-4eb0-be57-fe5c815efb99@googlegroups.com> Message-ID: <50A38FAB.5000602@davea.name> On 11/14/2012 06:29 AM, Johannes Bauer wrote: > > > When doing these calculations, it's important to keep the birthday > paradox in mind (this is kind of counter-intuitive): The chance of a > collission raises tremendously when we're looking for *any* arbitrary > two hashes colliding within a certain namespace. The probability you've > calculated is the pre-image probability (which you also again need to > multiply with a factor of two, because when trying to collide one given > hash, in the mean case you'll only have to search *half* the namespace > before finding a collision). > Te birthday paradox could have been important had the OP stated his goal differently. What he said was: """Ideally I would want to avoid collisions altogether. But if that means significant extra CPU time then 1 collision in 10 million hashes would be tolerable.""" That means that he's willing to do the necessary overhead of collision resolution, once in every 10 million lookups. That's not the same as saying that he wants only one chance in 10 million of having ANY collisions among his data items. -- DaveA From shanxing102 at gmail.com Wed Nov 14 07:55:33 2012 From: shanxing102 at gmail.com (xing102 shan) Date: Wed, 14 Nov 2012 04:55:33 -0800 (PST) Subject: cheap wholesale air jordan shoes in http://www.nikeblack.com Message-ID: cheap wholesale air jordan shoes(www.nikeblack.com) cheap air jordan shoes free shipping(www.nikeblack.com) cheap air jordan shoes for sale(www.nikeblack.com) cheap air jordan shoes for men(www.nikeblack.com) cheap air jordan basketball shoes(www.nikeblack.com) cheap air jordan shoes paypal free shipping(www.nikeblack.com) cheap air jordan shoes usa (www.nikeblack.com) wholesale air jordans (www.nikeblack.com) air jordan shoes suppliers (www.nikeblack.com) wholesale nike shoes (www.nikeblack.com) wholesale air jordan shoes china (www.nikeblack.com) wholesale air jordan shoes paypal (www.nikeblack.com) wholesale air jordan shoes free shipping (www.nikeblack.com) wholesale basketball shoes (www.nikeblack.com) wholesale lebron james shoes (www.nikeblack.com) discount michael jordan shoes(www.nikeblack.com) discount nike shoes (www.nikeblack.com) discount basketball shoes(www.nikeblack.com) discount lebron james shoes(www.nikeblack.com) discount footlocker(www.nikeblack.com) discount nike(www.nikeblack.com) discount finish line(www.nikeblack.com) discount air force ones(www.nikeblack.com) buy authentic air jordan shoes(www.nikeblack.com) buy michael jordan shoes(www.nikeblack.com) buy nike jordan shoes(www.nikeblack.com) buy air jordan sneakers(www.nikeblack.com) buy air jordan retro(www.nikeblack.com) buy nike shoes(www.nikeblack.com) buy air force ones shoes(www.nikeblack.com) buy basketball shoes(www.nikeblack.com) authentic air jordan shoes for sale(www.nikeblack.com) air jordan shoes for cheap(www.nikeblack.com) air jordan shoes for sale cheap(www.nikeblack.com) jordan shoes for sale(www.nikeblack.com) air jordan shoes history(www.nikeblack.com) air jordans (www.nikeblack.com) air jordan release dates (www.nikeblack.com) air jordans for sale(www.nikeblack.com) air jordan shoes for sale(www.nikeblack.com) authentic air jordan shoes(www.nikeblack.com) air jordan shoes list(www.nikeblack.com) air jordan shoes history(www.nikeblack.com) air jordan nike shoes(www.nikeblack.com) air jordan skate shoes(www.nikeblack.com) air jordan basketball shoes(www.nikeblack.com) air jordan running shoes (www.nikeblack.com) authentic jordan shoes (www.nikeblack.com) jordan shoes for sale(www.nikeblack.com) retro jordan shoes(www.nikeblack.com) custom jordan shoes(www.nikeblack.com) jordan shoes release dates(www.nikeblack.com) air jordan(www.nikeblack.com) jordan shoes wiki(www.nikeblack.com) jordan nike shoes (www.nikeblack.com) wholesale jordan shoes(www.nikeblack.com) cheap authentic jordan shoes(www.nikeblack.com) cheap jordan shoes for sale(www.nikeblack.com) cheap jordan shoes free shipping(www.nikeblack.com) cheap jordan shoes online(www.nikeblack.com) cheap jordan shoes for kids(www.nikeblack.com) cheap jordan nike shoes(www.nikeblack.com) cheap jordan basketball shoes(www.nikeblack.com) wholesale jordan shoes(www.nikeblack.com) discount jordan shoes wholesale(www.nikeblack.com) cheap jordan shoes(www.nikeblack.com) discount michael jordan shoes(www.nikeblack.com) discount nike shoes(www.nikeblack.com) discount jordan basketball shoes(www.nikeblack.com) discount jordan shoes online(www.nikeblack.com) discount jordans(www.nikeblack.com) website : http://www.nikeblack.com From shanxing102 at gmail.com Wed Nov 14 07:56:39 2012 From: shanxing102 at gmail.com (xing102 shan) Date: Wed, 14 Nov 2012 04:56:39 -0800 (PST) Subject: cheap air jordan shoes free shipping in http://www.nikeblack.com Message-ID: <1ecf1b32-8e4c-4cbd-93a1-30f5ac3de370@vy11g2000pbb.googlegroups.com> cheap wholesale air jordan shoes(www.nikeblack.com) cheap air jordan shoes free shipping(www.nikeblack.com) cheap air jordan shoes for sale(www.nikeblack.com) cheap air jordan shoes for men(www.nikeblack.com) cheap air jordan basketball shoes(www.nikeblack.com) cheap air jordan shoes paypal free shipping(www.nikeblack.com) cheap air jordan shoes usa (www.nikeblack.com) wholesale air jordans (www.nikeblack.com) air jordan shoes suppliers (www.nikeblack.com) wholesale nike shoes (www.nikeblack.com) wholesale air jordan shoes china (www.nikeblack.com) wholesale air jordan shoes paypal (www.nikeblack.com) wholesale air jordan shoes free shipping (www.nikeblack.com) wholesale basketball shoes (www.nikeblack.com) wholesale lebron james shoes (www.nikeblack.com) discount michael jordan shoes(www.nikeblack.com) discount nike shoes (www.nikeblack.com) discount basketball shoes(www.nikeblack.com) discount lebron james shoes(www.nikeblack.com) discount footlocker(www.nikeblack.com) discount nike(www.nikeblack.com) discount finish line(www.nikeblack.com) discount air force ones(www.nikeblack.com) buy authentic air jordan shoes(www.nikeblack.com) buy michael jordan shoes(www.nikeblack.com) buy nike jordan shoes(www.nikeblack.com) buy air jordan sneakers(www.nikeblack.com) buy air jordan retro(www.nikeblack.com) buy nike shoes(www.nikeblack.com) buy air force ones shoes(www.nikeblack.com) buy basketball shoes(www.nikeblack.com) authentic air jordan shoes for sale(www.nikeblack.com) air jordan shoes for cheap(www.nikeblack.com) air jordan shoes for sale cheap(www.nikeblack.com) jordan shoes for sale(www.nikeblack.com) air jordan shoes history(www.nikeblack.com) air jordans (www.nikeblack.com) air jordan release dates (www.nikeblack.com) air jordans for sale(www.nikeblack.com) air jordan shoes for sale(www.nikeblack.com) authentic air jordan shoes(www.nikeblack.com) air jordan shoes list(www.nikeblack.com) air jordan shoes history(www.nikeblack.com) air jordan nike shoes(www.nikeblack.com) air jordan skate shoes(www.nikeblack.com) air jordan basketball shoes(www.nikeblack.com) air jordan running shoes (www.nikeblack.com) authentic jordan shoes (www.nikeblack.com) jordan shoes for sale(www.nikeblack.com) retro jordan shoes(www.nikeblack.com) custom jordan shoes(www.nikeblack.com) jordan shoes release dates(www.nikeblack.com) air jordan(www.nikeblack.com) jordan shoes wiki(www.nikeblack.com) jordan nike shoes (www.nikeblack.com) wholesale jordan shoes(www.nikeblack.com) cheap authentic jordan shoes(www.nikeblack.com) cheap jordan shoes for sale(www.nikeblack.com) cheap jordan shoes free shipping(www.nikeblack.com) cheap jordan shoes online(www.nikeblack.com) cheap jordan shoes for kids(www.nikeblack.com) cheap jordan nike shoes(www.nikeblack.com) cheap jordan basketball shoes(www.nikeblack.com) wholesale jordan shoes(www.nikeblack.com) discount jordan shoes wholesale(www.nikeblack.com) cheap jordan shoes(www.nikeblack.com) discount michael jordan shoes(www.nikeblack.com) discount nike shoes(www.nikeblack.com) discount jordan basketball shoes(www.nikeblack.com) discount jordan shoes online(www.nikeblack.com) discount jordans(www.nikeblack.com) website : http://www.nikeblack.com From shanxing102 at gmail.com Wed Nov 14 07:56:41 2012 From: shanxing102 at gmail.com (xing102 shan) Date: Wed, 14 Nov 2012 04:56:41 -0800 (PST) Subject: cheap air jordan shoes free shipping in http://www.nikeblack.com Message-ID: cheap wholesale air jordan shoes(www.nikeblack.com) cheap air jordan shoes free shipping(www.nikeblack.com) cheap air jordan shoes for sale(www.nikeblack.com) cheap air jordan shoes for men(www.nikeblack.com) cheap air jordan basketball shoes(www.nikeblack.com) cheap air jordan shoes paypal free shipping(www.nikeblack.com) cheap air jordan shoes usa (www.nikeblack.com) wholesale air jordans (www.nikeblack.com) air jordan shoes suppliers (www.nikeblack.com) wholesale nike shoes (www.nikeblack.com) wholesale air jordan shoes china (www.nikeblack.com) wholesale air jordan shoes paypal (www.nikeblack.com) wholesale air jordan shoes free shipping (www.nikeblack.com) wholesale basketball shoes (www.nikeblack.com) wholesale lebron james shoes (www.nikeblack.com) discount michael jordan shoes(www.nikeblack.com) discount nike shoes (www.nikeblack.com) discount basketball shoes(www.nikeblack.com) discount lebron james shoes(www.nikeblack.com) discount footlocker(www.nikeblack.com) discount nike(www.nikeblack.com) discount finish line(www.nikeblack.com) discount air force ones(www.nikeblack.com) buy authentic air jordan shoes(www.nikeblack.com) buy michael jordan shoes(www.nikeblack.com) buy nike jordan shoes(www.nikeblack.com) buy air jordan sneakers(www.nikeblack.com) buy air jordan retro(www.nikeblack.com) buy nike shoes(www.nikeblack.com) buy air force ones shoes(www.nikeblack.com) buy basketball shoes(www.nikeblack.com) authentic air jordan shoes for sale(www.nikeblack.com) air jordan shoes for cheap(www.nikeblack.com) air jordan shoes for sale cheap(www.nikeblack.com) jordan shoes for sale(www.nikeblack.com) air jordan shoes history(www.nikeblack.com) air jordans (www.nikeblack.com) air jordan release dates (www.nikeblack.com) air jordans for sale(www.nikeblack.com) air jordan shoes for sale(www.nikeblack.com) authentic air jordan shoes(www.nikeblack.com) air jordan shoes list(www.nikeblack.com) air jordan shoes history(www.nikeblack.com) air jordan nike shoes(www.nikeblack.com) air jordan skate shoes(www.nikeblack.com) air jordan basketball shoes(www.nikeblack.com) air jordan running shoes (www.nikeblack.com) authentic jordan shoes (www.nikeblack.com) jordan shoes for sale(www.nikeblack.com) retro jordan shoes(www.nikeblack.com) custom jordan shoes(www.nikeblack.com) jordan shoes release dates(www.nikeblack.com) air jordan(www.nikeblack.com) jordan shoes wiki(www.nikeblack.com) jordan nike shoes (www.nikeblack.com) wholesale jordan shoes(www.nikeblack.com) cheap authentic jordan shoes(www.nikeblack.com) cheap jordan shoes for sale(www.nikeblack.com) cheap jordan shoes free shipping(www.nikeblack.com) cheap jordan shoes online(www.nikeblack.com) cheap jordan shoes for kids(www.nikeblack.com) cheap jordan nike shoes(www.nikeblack.com) cheap jordan basketball shoes(www.nikeblack.com) wholesale jordan shoes(www.nikeblack.com) discount jordan shoes wholesale(www.nikeblack.com) cheap jordan shoes(www.nikeblack.com) discount michael jordan shoes(www.nikeblack.com) discount nike shoes(www.nikeblack.com) discount jordan basketball shoes(www.nikeblack.com) discount jordan shoes online(www.nikeblack.com) discount jordans(www.nikeblack.com) website : http://www.nikeblack.com From shanxing102 at gmail.com Wed Nov 14 07:57:35 2012 From: shanxing102 at gmail.com (xing102 shan) Date: Wed, 14 Nov 2012 04:57:35 -0800 (PST) Subject: cheap air jordan shoes for sale in http://www.nikeblack.com Message-ID: <0e485ae8-5050-43de-9029-4b2e492af9b4@uc4g2000pbc.googlegroups.com> cheap wholesale air jordan shoes(www.nikeblack.com) cheap air jordan shoes free shipping(www.nikeblack.com) cheap air jordan shoes for sale(www.nikeblack.com) cheap air jordan shoes for men(www.nikeblack.com) cheap air jordan basketball shoes(www.nikeblack.com) cheap air jordan shoes paypal free shipping(www.nikeblack.com) cheap air jordan shoes usa (www.nikeblack.com) wholesale air jordans (www.nikeblack.com) air jordan shoes suppliers (www.nikeblack.com) wholesale nike shoes (www.nikeblack.com) wholesale air jordan shoes china (www.nikeblack.com) wholesale air jordan shoes paypal (www.nikeblack.com) wholesale air jordan shoes free shipping (www.nikeblack.com) wholesale basketball shoes (www.nikeblack.com) wholesale lebron james shoes (www.nikeblack.com) discount michael jordan shoes(www.nikeblack.com) discount nike shoes (www.nikeblack.com) discount basketball shoes(www.nikeblack.com) discount lebron james shoes(www.nikeblack.com) discount footlocker(www.nikeblack.com) discount nike(www.nikeblack.com) discount finish line(www.nikeblack.com) discount air force ones(www.nikeblack.com) buy authentic air jordan shoes(www.nikeblack.com) buy michael jordan shoes(www.nikeblack.com) buy nike jordan shoes(www.nikeblack.com) buy air jordan sneakers(www.nikeblack.com) buy air jordan retro(www.nikeblack.com) buy nike shoes(www.nikeblack.com) buy air force ones shoes(www.nikeblack.com) buy basketball shoes(www.nikeblack.com) authentic air jordan shoes for sale(www.nikeblack.com) air jordan shoes for cheap(www.nikeblack.com) air jordan shoes for sale cheap(www.nikeblack.com) jordan shoes for sale(www.nikeblack.com) air jordan shoes history(www.nikeblack.com) air jordans (www.nikeblack.com) air jordan release dates (www.nikeblack.com) air jordans for sale(www.nikeblack.com) air jordan shoes for sale(www.nikeblack.com) authentic air jordan shoes(www.nikeblack.com) air jordan shoes list(www.nikeblack.com) air jordan shoes history(www.nikeblack.com) air jordan nike shoes(www.nikeblack.com) air jordan skate shoes(www.nikeblack.com) air jordan basketball shoes(www.nikeblack.com) air jordan running shoes (www.nikeblack.com) authentic jordan shoes (www.nikeblack.com) jordan shoes for sale(www.nikeblack.com) retro jordan shoes(www.nikeblack.com) custom jordan shoes(www.nikeblack.com) jordan shoes release dates(www.nikeblack.com) air jordan(www.nikeblack.com) jordan shoes wiki(www.nikeblack.com) jordan nike shoes (www.nikeblack.com) wholesale jordan shoes(www.nikeblack.com) cheap authentic jordan shoes(www.nikeblack.com) cheap jordan shoes for sale(www.nikeblack.com) cheap jordan shoes free shipping(www.nikeblack.com) cheap jordan shoes online(www.nikeblack.com) cheap jordan shoes for kids(www.nikeblack.com) cheap jordan nike shoes(www.nikeblack.com) cheap jordan basketball shoes(www.nikeblack.com) wholesale jordan shoes(www.nikeblack.com) discount jordan shoes wholesale(www.nikeblack.com) cheap jordan shoes(www.nikeblack.com) discount michael jordan shoes(www.nikeblack.com) discount nike shoes(www.nikeblack.com) discount jordan basketball shoes(www.nikeblack.com) discount jordan shoes online(www.nikeblack.com) discount jordans(www.nikeblack.com) website : http://www.nikeblack.com From dfnsonfsduifb at gmx.de Wed Nov 14 08:00:21 2012 From: dfnsonfsduifb at gmx.de (Johannes Bauer) Date: Wed, 14 Nov 2012 14:00:21 +0100 Subject: Generate unique ID for URL In-Reply-To: References: <0692e6a2-343c-4eb0-be57-fe5c815efb99@googlegroups.com> Message-ID: On 14.11.2012 13:33, Dave Angel wrote: > Te birthday paradox could have been important had the OP stated his goal > differently. What he said was: > > """Ideally I would want to avoid collisions altogether. But if that means significant extra CPU time then 1 collision in 10 million hashes would be tolerable.""" > > That means that he's willing to do the necessary overhead of collision > resolution, once in every 10 million lookups. That's not the same as > saying that he wants only one chance in 10 million of having ANY > collisions among his data items. Since he stated in a later post that he actually went with MD5, the calculations are indeed relevant. They give the number of bits a perfect hash needs to have in order to get the desired low probablility of collision resolutions. And for that the birthday paradox probability must be considered instead of the (much lower) pre-image probability. In any case, it appeared to me as if the OP was rather looking for ideas and wasn't sure himself what approach to take -- so I find it quite appropriate to give suggestions one way or another (even if they might not fit the exact phrasing of one of his postings). Best regards, Johannes -- >> Wo hattest Du das Beben nochmal GENAU vorhergesagt? > Zumindest nicht ?ffentlich! Ah, der neueste und bis heute genialste Streich unsere gro?en Kosmologen: Die Geheim-Vorhersage. - Karl Kaos ?ber R?diger Thomas in dsa From rosuav at gmail.com Wed Nov 14 08:35:24 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 15 Nov 2012 00:35:24 +1100 Subject: Simple Question regarding running .py program In-Reply-To: <6c3c5341-1fb4-4664-80b8-d3e08687830f@googlegroups.com> References: <22ae6963-6700-4ea7-b396-344d89ead7f2@googlegroups.com> <8085ebf2-9f08-4889-84c9-f786d6776001@googlegroups.com> <5f1bf680-ca10-446e-abbc-d3ba55327f2d@googlegroups.com> <6c3c5341-1fb4-4664-80b8-d3e08687830f@googlegroups.com> Message-ID: On Wed, Nov 14, 2012 at 6:02 PM, wrote: > On 11/13/2012 11:02 PM, Chris Angelico wrote: >> To be more accurate: This is deprecated *by members of* this list. As >> there is no commanding/controlling entity here, it's up to each >> individual to make a decision - for instance, abusive users get >> killfiled rather than banned. The use of Google Groups to post is >> deprecated in the original sense of the word: strongly disapproved of. > > s/deprecated *by members of*/deprecated *by some members of*/ > > (and accuracy could probably be increased further by replacing > "some" with "a few".) I stand by what I said. Members, plural, of this list. I didn't say "all members of", ergo the word "some" is superfluous, yet not needful, as Princess Ida put it. In any case, the fact remains that a number of this list's best responders have killfiled Google Groups posters as a whole. Consequently, GG forces you to go to quite a bit of extra work AND prevents your message from getting through to everyone. Why go to extra work to get a worse result? I am therefore not going to recommend Google Groups to anyone as a means of posting to python-list/c.l.p, any more than I would recommend writing it on a Post-It note and feeding it into your floppy drive. ChrisA From aahz at pythoncraft.com Wed Nov 14 09:19:05 2012 From: aahz at pythoncraft.com (Aahz) Date: 14 Nov 2012 06:19:05 -0800 Subject: Python garbage collector/memory manager behaving strangely References: <59f8c664-8f11-439e-8002-ca76ee24a632@g7g2000pbh.googlegroups.com> <50570de3$0$29981$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <50570de3$0$29981$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: >On Mon, 17 Sep 2012 06:46:55 -0400, Dave Angel wrote: >> On 09/16/2012 11:25 PM, alex23 wrote: >>> >>> def readlines(f): >>> lines = [] >>> while "f is not empty": >>> line = f.readline() >>> if not line: break >>> if len(line) > 2 and line[-2:] == '|\n': >>> lines.append(line) >>> yield ''.join(lines) >>> lines = [] >>> else: >>> lines.append(line) >> >> There's a few changes I'd make: >> I'd change the name to something else, so as not to shadow the built-in, > >Which built-in are you referring to? There is no readlines built-in. > >py> readlines >Traceback (most recent call last): > File "", line 1, in >NameError: name 'readlines' is not defined > >There is a file.readlines method, but that lives in a different namespace >to the function readlines so there should be no confusion. At least not >for a moderately experienced programmer, beginners can be confused by the >littlest things sometimes. Actually, as an experienced programmer, I *do* think it is confusing as evidenced by the mistake Dave made! Segregated namespaces are wonderful (per Zen), but let's not pollute multiple namespaces with same name, either. It may not be literally shadowing the built-in, but it definitely mentally shadows the built-in. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "....Normal is what cuts off your sixth finger and your tail..." --Siobhan From roy at panix.com Wed Nov 14 09:22:57 2012 From: roy at panix.com (Roy Smith) Date: Wed, 14 Nov 2012 09:22:57 -0500 Subject: Subprocess puzzle and two questions References: Message-ID: In article , William Ray Wing wrote: > On Nov 13, 2012, at 11:41 PM, Roy Smith wrote: > > > In article , > > wrw at mac.com wrote: > > > >> I need to time the operation of a command-line utility (specifically > >> nslookup) from within a python program I'm writing. > > > > Ugh. Why are you doing this? Shelling out to nslookup is an incredibly > > slow and clumsy way of doing name translation. What you really want to > > be doing is calling getaddrinfo() directly. > > > > See http://docs.python.org/2/library/socket.html#socket.getaddrinfo for > > details. > > -- > Because, unless I'm badly mistaken (very possible), getaddrinfo doesn't let > me specify the server from which the name is returned. I'm really not after > the name, what I'm REALLY after is the fact that a path exists to the name > server I specify (and how long it takes to respond). In the "good old days" I > would just have ping'd it, but these days more and more DNS boxes (and > servers of all sorts) are shutting off their ping response. > > Thanks, Bill Oh, my. You're using DNS as a replacement for ping? Fair enough. In that case, all you really care about is that you can connect to port 53 on the server... import socket import time s = socket.socket() t0 = time.time() s.connect(('8.8.8.8', 53)) t1 = time.time() print "it took %f seconds to connect" % (t1 - t0) From wrw at mac.com Wed Nov 14 09:37:12 2012 From: wrw at mac.com (wrw at mac.com) Date: Wed, 14 Nov 2012 09:37:12 -0500 Subject: Subprocess puzzle and two questions In-Reply-To: References: Message-ID: <04361B15-071E-41C9-A098-1DF36D1DCE12@mac.com> On Nov 14, 2012, at 9:22 AM, Roy Smith wrote: > In article , > William Ray Wing wrote: > >> On Nov 13, 2012, at 11:41 PM, Roy Smith wrote: >> >>> In article , >>> wrw at mac.com wrote: >>> >>>> I need to time the operation of a command-line utility (specifically >>>> nslookup) from within a python program I'm writing. >>> >>> Ugh. Why are you doing this? Shelling out to nslookup is an incredibly >>> slow and clumsy way of doing name translation. What you really want to >>> be doing is calling getaddrinfo() directly. >>> >>> See http://docs.python.org/2/library/socket.html#socket.getaddrinfo for >>> details. >>> -- >> Because, unless I'm badly mistaken (very possible), getaddrinfo doesn't let >> me specify the server from which the name is returned. I'm really not after >> the name, what I'm REALLY after is the fact that a path exists to the name >> server I specify (and how long it takes to respond). In the "good old days" I >> would just have ping'd it, but these days more and more DNS boxes (and >> servers of all sorts) are shutting off their ping response. >> >> Thanks, Bill > > Oh, my. You're using DNS as a replacement for ping? Fair enough. In > that case, all you really care about is that you can connect to port 53 > on the server... > > import socket > import time > s = socket.socket() > t0 = time.time() > s.connect(('8.8.8.8', 53)) > t1 = time.time() > print "it took %f seconds to connect" % (t1 - t0) > -- > http://mail.python.org/mailman/listinfo/python-list Now THAT looks better. Simpler, cleaner, (longer, taller, stronger, faster, cheaper? :-) Thanks, Bill From rosuav at gmail.com Wed Nov 14 09:40:05 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 15 Nov 2012 01:40:05 +1100 Subject: Subprocess puzzle and two questions In-Reply-To: References: Message-ID: On Thu, Nov 15, 2012 at 1:22 AM, Roy Smith wrote: > Oh, my. You're using DNS as a replacement for ping? Fair enough. In > that case, all you really care about is that you can connect to port 53 > on the server... > > import socket > import time > s = socket.socket() > t0 = time.time() > s.connect(('8.8.8.8', 53)) > t1 = time.time() > print "it took %f seconds to connect" % (t1 - t0) That assumes that (a) the remote server supports TCP for DNS (since UDP is by far the more often used, some name servers don't bother supporting TCP), and (b) that connection time for TCP is comparable to ping or an actual DNS lookup. But in terms of approximating your connection times, that's gotta be way better than shelling out to several other processes. ChrisA From insideshoes at gmail.com Wed Nov 14 10:18:38 2012 From: insideshoes at gmail.com (inshu chauhan) Date: Wed, 14 Nov 2012 16:18:38 +0100 Subject: Error Message-ID: for this code m getting this error : CODE : def ComputeClasses(data): radius = .5 points = [] for cy in xrange(0, data.height): for cx in xrange(0, data.width): if data[cy,cx] != (0.0,0.0,0.0): centre = data[cy, cx] points.append(centre) change = True while change: for ring_number in xrange(1, 1000): change = False new_indices = GenerateRing(cx, cy, ring_number) for idx in new_indices: point = data[idx[0], idx[1]] if point == (0.0, 0.0, 0.0 ): continue else: dist = distance(centre, point) if dist < radius : print point points.append(point) change = True print change break ERROR : Traceback (most recent call last): File "Z:\modules\classification2.py", line 74, in ComputeClasses(data) File "Z:\modules\classification2.py", line 56, in ComputeClasses dist = distance(centre, point) UnboundLocalError: local variable 'centre' referenced before assignment And i am unable to understand .. WHY ? -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Wed Nov 14 10:28:12 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 15 Nov 2012 02:28:12 +1100 Subject: Error In-Reply-To: References: Message-ID: On Thu, Nov 15, 2012 at 2:18 AM, inshu chauhan wrote: > > for this code m getting this error : > > CODE : > def ComputeClasses(data): > if data[cy,cx] != (0.0,0.0,0.0): > centre = data[cy, cx] > ... > dist = distance(centre, point) > > ERROR : > UnboundLocalError: local variable 'centre' referenced before assignment > > And i am unable to understand .. WHY ? In brief, here's what causes that error: 1) Somewhere in the function, you assign to that name, which implicitly sets it to be a local variable. That happens there where you go "centre = data[cy, cx]". 2) Somewhere else in the function, you reference that name. That happens where you try to calculate the distance from your previously-defined centre to the current point. 3) At run-time, you haven't executed #1, but you do execute #2. Your problem here I can't diagnose, but it looks like your first point is (0.0,0.0,0.0), so centre never gets set. There are a couple of possible fixes for this, and you'll need to figure out what to do based on knowing your own code. Possibly you just need to initialize centre above the loop, so that it always has a valid value; or possibly the code below needs to not execute if the current centre hasn't been set. Go through your function's logic by hand and figure out what happens when, and whether that's what it ought to do. Then decide what should happen when a data value is (0.0,0.0,0.0) - currently it's retaining the value of centre from the previous iteration of the loop, which smells wrong to me. Beyond that, I don't think I can really help, it's up to you. ChrisA From andrea.crotti.0 at gmail.com Wed Nov 14 10:56:28 2012 From: andrea.crotti.0 at gmail.com (andrea crotti) Date: Wed, 14 Nov 2012 15:56:28 +0000 Subject: creating size-limited tar files In-Reply-To: References: <509ab0fa$0$6636$9b4e6d93@newsspool2.arcor-online.net> <509AD812.2060605@gmail.com> <87lie4lnxr.fsf@nitrogen.i-did-not-set--mail-host-address--so-tickle-me> <87fw4clg8b.fsf@nitrogen.i-did-not-set--mail-host-address--so-tickle-me> Message-ID: Ok this is all very nice, but: [andrea at andreacrotti tar_baller]$ time python2 test_pipe.py > /dev/null real 0m21.215s user 0m0.750s sys 0m1.703s [andrea at andreacrotti tar_baller]$ time ls -lR /home/andrea | cat > /dev/null real 0m0.986s user 0m0.413s sys 0m0.600s where test_pipe.py is: from subprocess import PIPE, Popen # check if doing the pipe with subprocess and with the | is the same or not pipe_file = open('pipefile', 'w') p1 = Popen('ls -lR /home/andrea', shell=True, stdout=PIPE, stderr=PIPE) p2 = Popen('cat', shell=True, stdin=p1.stdout, stdout=PIPE, stderr=PIPE) p1.stdout.close() print(p2.stdout.read()) So apparently it's way slower than using this system, is this normal? From joel.goldstick at gmail.com Wed Nov 14 11:02:34 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Wed, 14 Nov 2012 11:02:34 -0500 Subject: Error In-Reply-To: References: Message-ID: On Wed, Nov 14, 2012 at 10:18 AM, inshu chauhan wrote: > > for this code m getting this error : > > CODE : > def ComputeClasses(data): > radius = .5 > points = [] > for cy in xrange(0, data.height): > for cx in xrange(0, data.width): > if data[cy,cx] != (0.0,0.0,0.0): > This code is only run if the test above is true > centre = data[cy, cx] > points.append(centre) > > > change = True > > while change: > > for ring_number in xrange(1, 1000): > change = False > new_indices = GenerateRing(cx, cy, ring_number) > > > for idx in new_indices: > point = data[idx[0], idx[1]] > > if point == (0.0, 0.0, 0.0 ): > continue > else: > dist = distance(centre, point) > centre is only set if the test above is true. In your run, it apparently wasn't > if dist < radius : > print point > points.append(point) > change = True > print change > > > break > > > ERROR : > Traceback (most recent call last): > File "Z:\modules\classification2.py", line 74, in > ComputeClasses(data) > File "Z:\modules\classification2.py", line 56, in ComputeClasses > dist = distance(centre, point) > UnboundLocalError: local variable 'centre' referenced before assignment > > And i am unable to understand .. WHY ? > > > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- Joel Goldstick -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Wed Nov 14 11:10:47 2012 From: d at davea.name (Dave Angel) Date: Wed, 14 Nov 2012 11:10:47 -0500 Subject: creating size-limited tar files In-Reply-To: References: <509ab0fa$0$6636$9b4e6d93@newsspool2.arcor-online.net> <509AD812.2060605@gmail.com> <87lie4lnxr.fsf@nitrogen.i-did-not-set--mail-host-address--so-tickle-me> <87fw4clg8b.fsf@nitrogen.i-did-not-set--mail-host-address--so-tickle-me> Message-ID: <50A3C287.80707@davea.name> On 11/14/2012 10:56 AM, andrea crotti wrote: > Ok this is all very nice, but: > > [andrea at andreacrotti tar_baller]$ time python2 test_pipe.py > /dev/null > > real 0m21.215s > user 0m0.750s > sys 0m1.703s > > [andrea at andreacrotti tar_baller]$ time ls -lR /home/andrea | cat > /dev/null > > real 0m0.986s > user 0m0.413s > sys 0m0.600s > > > > > So apparently it's way slower than using this system, is this normal? I'm not sure how this timing relates to the thread, but what it mainly shows is that starting up the Python interpreter takes quite a while, compared to not starting it up. -- DaveA From andrea.crotti.0 at gmail.com Wed Nov 14 11:16:16 2012 From: andrea.crotti.0 at gmail.com (andrea crotti) Date: Wed, 14 Nov 2012 16:16:16 +0000 Subject: creating size-limited tar files In-Reply-To: <50A3C287.80707@davea.name> References: <509ab0fa$0$6636$9b4e6d93@newsspool2.arcor-online.net> <509AD812.2060605@gmail.com> <87lie4lnxr.fsf@nitrogen.i-did-not-set--mail-host-address--so-tickle-me> <87fw4clg8b.fsf@nitrogen.i-did-not-set--mail-host-address--so-tickle-me> <50A3C287.80707@davea.name> Message-ID: 2012/11/14 Dave Angel : > On 11/14/2012 10:56 AM, andrea crotti wrote: >> Ok this is all very nice, but: >> >> [andrea at andreacrotti tar_baller]$ time python2 test_pipe.py > /dev/null >> >> real 0m21.215s >> user 0m0.750s >> sys 0m1.703s >> >> [andrea at andreacrotti tar_baller]$ time ls -lR /home/andrea | cat > /dev/null >> >> real 0m0.986s >> user 0m0.413s >> sys 0m0.600s >> >> >> >> >> So apparently it's way slower than using this system, is this normal? > > I'm not sure how this timing relates to the thread, but what it mainly > shows is that starting up the Python interpreter takes quite a while, > compared to not starting it up. > > > -- > > DaveA > Well it's related because my program has to be as fast as possible, so in theory I thought that using Python pipes would be better because I can get easily the PID of the first process. But if it's so slow than it's not worth, and I don't think is the Python interpreter because it's more or less constantly many times slower even changing the size of the input.. From tnajun at gmail.com Wed Nov 14 11:17:43 2012 From: tnajun at gmail.com (Jun Tanaka) Date: Thu, 15 Nov 2012 01:17:43 +0900 Subject: Python-list Digest, Vol 110, Issue 106 In-Reply-To: References: Message-ID: Hi, I have a question about Django. I easy_installed Django1.4 and psycopg2, and python manage.py syncdb. And gave me a error; No module named psycopg2.extensions. posgre9.1 is installed. It works fine on my MAC but not my Windows. Does anyone know about this issue Hope to resolve this issue soon. Jun -------------- next part -------------- An HTML attachment was scrubbed... URL: From roy at panix.com Wed Nov 14 11:20:54 2012 From: roy at panix.com (Roy Smith) Date: 14 Nov 2012 11:20:54 -0500 Subject: Subprocess puzzle and two questions References: Message-ID: I wrote: >> Oh, my. You're using DNS as a replacement for ping? Fair enough. In >> that case, all you really care about is that you can connect to port 53 >> on the server... >> >> s = socket.socket() >> s.connect(('8.8.8.8', 53)) In article , Chris Angelico wrote: >That assumes that (a) the remote server supports TCP for DNS This is true. I honestly don't know what percentage of DNS servers out there only support UDP. The two I tried (Google's 8.8.8.8, and my Apple TimeCapsule) both supported TCP, but that's hardly a representitive sample. > and (b) that connection time for TCP is comparable to > ping or an actual DNS lookup. My first thought to solve both of these is that it shouldn't be too hard to hand-craft a minimal DNS query and send it over UDP. Then, I hunted around a bit and found that somebody had already done that, in spades. Take a look at http://www.dnspython.org; it might be exactly what's needed here. From d at davea.name Wed Nov 14 11:33:27 2012 From: d at davea.name (Dave Angel) Date: Wed, 14 Nov 2012 11:33:27 -0500 Subject: creating size-limited tar files In-Reply-To: References: <509AD812.2060605@gmail.com> <87lie4lnxr.fsf@nitrogen.i-did-not-set--mail-host-address--so-tickle-me> <87fw4clg8b.fsf@nitrogen.i-did-not-set--mail-host-address--so-tickle-me> <50A3C287.80707@davea.name> Message-ID: <50A3C7D7.7050103@davea.name> On 11/14/2012 11:16 AM, andrea crotti wrote: > 2012/11/14 Dave Angel : >> On 11/14/2012 10:56 AM, andrea crotti wrote: >>> Ok this is all very nice, but: >>> >>> [andrea at andreacrotti tar_baller]$ time python2 test_pipe.py > /dev/null >>> >>> real 0m21.215s >>> user 0m0.750s >>> sys 0m1.703s >>> >>> [andrea at andreacrotti tar_baller]$ time ls -lR /home/andrea | cat > /dev/null >>> >>> real 0m0.986s >>> user 0m0.413s >>> sys 0m0.600s >>> >>> >>> >>> >>> So apparently it's way slower than using this system, is this normal? >> >> I'm not sure how this timing relates to the thread, but what it mainly >> shows is that starting up the Python interpreter takes quite a while, >> compared to not starting it up. >> >> >> -- >> >> DaveA >> > > > Well it's related because my program has to be as fast as possible, so > in theory I thought that using Python pipes would be better because I > can get easily the PID of the first process. > > But if it's so slow than it's not worth, and I don't think is the > Python interpreter because it's more or less constantly many times > slower even changing the size of the input.. > > Well, as I said, I don't see how the particular timing has anything to do with the rest of the thread. If you want to do an ls within a Python program, go ahead. But if all you need can be done with ls itself, then it'll be slower to launch python just to run it. Your first timing runs python, which runs two new shells, ls, and cat. Your second timing runs ls and cat. So the difference is starting up python, plus starting the shell two extra times. I'd also be curious if you flushed the system buffers before each timing, as the second test could be running entirely in system memory. And no, I don't know offhand how to flush them in Linux, just that without it, your timings are not at all repeatable. Note the two identical runs here. davea at think:~/temppython$ time ls -lR ~ | cat > /dev/null real 0m0.164s user 0m0.020s sys 0m0.000s davea at think:~/temppython$ time ls -lR ~ | cat > /dev/null real 0m0.018s user 0m0.000s sys 0m0.010s real time goes down by 90%, while user time drops to zero. And on a 3rd and subsequent run, sys time goes to zero as well. -- DaveA From toby at tobiah.org Wed Nov 14 12:58:30 2012 From: toby at tobiah.org (Tobiah) Date: Wed, 14 Nov 2012 09:58:30 -0800 Subject: Getting "empty" attachment with smtplib In-Reply-To: References: Message-ID: I just found out that the attachment works fine when I read the mail from the gmail website. Thunderbird complains that the attachment is empty. Thanks, Toby On 11/14/2012 09:51 AM, Tobiah wrote: > I've been sending an email blast out with smtplib and > it's been working fine. I'm attaching an html doc with > > msg.attach(MIMEText(email, 'html')) > > and it displays fine. Now I need to attach a .pdf > doc, but Thunderbird complains that the attachment > is empty. When I view the source of the email, the > headers look ok to me, and a large base64 looking > mess follows: > > --===============0152408622== > Content-Type: application/pdf > MIME-Version: 1.0 > Content-Transfer-Encoding: base64 > Content-Disposition: attachment; filename="tics.pdf" > > JVBERi0xLjYNJeLjz9MNCjE0IDAgb2JqDTw8L0xpbmVhcml6ZWQgMS9MIDExNzk1My9PIDE2L0Ug > MTEyNjE3L04gMS9UIDExNzY0My9IIFsgNjA2IDI1M10+Pg1lbmRvYmoNICAgICAgICAgICAgICAg > DQo2MSAwIG9iag08PC9EZWNvZGVQYXJtczw8L0NvbHVtbnMgNS9QcmVkaWN0b3IgMTI+Pi9GaWx0 > ZXIvRmxhdGVEZWNvZGUvSURbPDg4RkMxMTM2QjQ3RDhEQzRFMjkxQkEzRDJGNEIyODBBPjxGRTNC > RkM3MjNFMDg3QzRCQUEyNTUzMkM5NEI5QjNCOT5dL0luZGV4WzE0IDc4XS9JbmZvIDEzIDAgUi9M > > and so on. I've tried a few recipes, and this is the one I'm trying now: > > pdf = MIMEApplication(pdf_data, 'pdf') > pdf.add_header('Content-Disposition','attachment', filename = 'tics.pdf') > msg.attach(pdf) > > Any help is appreciated. Also, if anyone has a working recipe, I'd like to > see it. > > Thanks! > > Tobiah From santosh.ssit at gmail.com Wed Nov 14 13:07:38 2012 From: santosh.ssit at gmail.com (san) Date: Wed, 14 Nov 2012 10:07:38 -0800 (PST) Subject: How Run Py.test from PyScripter Message-ID: <15fcd3e9-42de-43df-8ca2-38f76ddf0ec1@googlegroups.com> I am a newbie to py.test , Please let me know how to run the py.test in PyScripter Editor. I have tried in the belwo way but it doesn't work. import pytest def func(x): return x + 1 def test_answer(): assert func(3) == 5 pytest.main() below is the Exception that i get Traceback (most recent call last): File "", line 10, in File "C:\Python27\lib\site-packages\pytest-2.3.2-py2.7.egg\_pytest\core.py", line 474, in main exitstatus = config.hook.pytest_cmdline_main(config=config) File "C:\Python27\lib\site-packages\pytest-2.3.2-py2.7.egg\_pytest\core.py", line 422, in __call__ return self._docall(methods, kwargs) File "C:\Python27\lib\site-packages\pytest-2.3.2-py2.7.egg\_pytest\core.py", line 433, in _docall res = mc.execute() File "C:\Python27\lib\site-packages\pytest-2.3.2-py2.7.egg\_pytest\core.py", line 351, in execute res = method(**kwargs) File "C:\Python27\lib\site-packages\pytest-2.3.2-py2.7.egg\_pytest\main.py", line 107, in pytest_cmdline_main return wrap_session(config, _main) File "C:\Python27\lib\site-packages\pytest-2.3.2-py2.7.egg\_pytest\main.py", line 92, in wrap_session config.pluginmanager.notify_exception(excinfo, config.option) File "C:\Python27\lib\site-packages\pytest-2.3.2-py2.7.egg\_pytest\core.py", line 285, in notify_exception res = self.hook.pytest_internalerror(excrepr=excrepr) File "C:\Python27\lib\site-packages\pytest-2.3.2-py2.7.egg\_pytest\core.py", line 422, in __call__ return self._docall(methods, kwargs) File "C:\Python27\lib\site-packages\pytest-2.3.2-py2.7.egg\_pytest\core.py", line 433, in _docall res = mc.execute() File "C:\Python27\lib\site-packages\pytest-2.3.2-py2.7.egg\_pytest\core.py", line 351, in execute res = method(**kwargs) File "C:\Python27\lib\site-packages\pytest-2.3.2-py2.7.egg\_pytest\terminal.py", line 152, in pytest_internalerror self.write_line("INTERNALERROR> " + line) File "C:\Python27\lib\site-packages\pytest-2.3.2-py2.7.egg\_pytest\terminal.py", line 140, in write_line self._tw.line(line, **markup) File "C:\Python27\lib\site-packages\py-1.4.11-py2.7.egg\py\_io\terminalwriter.py", line 181, in line self.write(s, **kw) File "C:\Python27\lib\site-packages\py-1.4.11-py2.7.egg\py\_io\terminalwriter.py", line 225, in write self._file.write(msg) File "C:\Python27\lib\site-packages\py-1.4.11-py2.7.egg\py\_io\terminalwriter.py", line 241, in write self._writemethod(data) TypeError: 'AsyncStream' object is not callable From smaran.harihar at gmail.com Wed Nov 14 13:09:31 2012 From: smaran.harihar at gmail.com (Smaran Harihar) Date: Wed, 14 Nov 2012 11:09:31 -0700 Subject: Running a curl command within py script Message-ID: Hi Guys, i found pycurl to execute python curl command but not sure how I can execute the curl command using the pycurl. curl -u admin:geoserver -v -XPUT -H 'Content-type: text/plain' -d 'file:/var/www/geo/shapefile/csvQshp/Quercus_iltisii.shp' http://localhost:8080/geoserver/rest/workspaces/acme/datastores/Quercus_iltisii/external.shp ? This is the curl command. -- Thanks & Regards Smaran Harihar -------------- next part -------------- An HTML attachment was scrubbed... URL: From rurpy at yahoo.com Wed Nov 14 13:20:13 2012 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Wed, 14 Nov 2012 10:20:13 -0800 (PST) Subject: Simple Question regarding running .py program In-Reply-To: References: <22ae6963-6700-4ea7-b396-344d89ead7f2@googlegroups.com> <8085ebf2-9f08-4889-84c9-f786d6776001@googlegroups.com> <5f1bf680-ca10-446e-abbc-d3ba55327f2d@googlegroups.com> <6c3c5341-1fb4-4664-80b8-d3e08687830f@googlegroups.com> Message-ID: On 11/14/2012 06:35 AM, Chris Angelico wrote: > On Wed, Nov 14, 2012 at 6:02 PM, rurpy wrote: >> On 11/13/2012 11:02 PM, Chris Angelico wrote: >>> To be more accurate: This is deprecated *by members of* this list. As >>> there is no commanding/controlling entity here, it's up to each >>> individual to make a decision - for instance, abusive users get >>> killfiled rather than banned. The use of Google Groups to post is >>> deprecated in the original sense of the word: strongly disapproved of. >> >> s/deprecated *by members of*/deprecated *by some members of*/ >> >> (and accuracy could probably be increased further by replacing >> "some" with "a few".) > > I stand by what I said. Members, plural, of this list. I didn't say > "all members of", ergo the word "some" is superfluous, yet not > needful, as Princess Ida put it. Then you would have no problem I suppose with "Australians are racists" because some Australians are racist and I didn't say "all"? I stand by what I said. Using the passive voice to give a false sense of authority, leaving out quantifiers when there are likely thousands of readers of this group perhaps a half dozen who've been vocal against GG, is not an accurate description. > In any case, the fact remains that a number of this list's best > responders have killfiled Google Groups posters as a whole. > Consequently, GG forces you to go to quite a bit of extra work AND > prevents your message from getting through to everyone. Why go to > extra work to get a worse result? As a user of GG, Usenet and email lists I claim you are wrong. GG does NOT require "quite a bit of extra work". If it did, I wouldn't use it. For occasional posters, GG is EASIER. (It would be even easier if Google would fix their execrable quoting behaviour but as I showed, it is easy to work around that.) I think you are ignoring setup time and a number of other secondary factors, things that are very significant to occasional posters, in your evaluation of "easy". As for "best", that is clearly a matter of opinion. The very fact that someone would killfile an entire class of poster based on a some others' posts reeks of intolerance and group-think. And since some of the anti-GG proponents are also among the most opinionated and argumentative participants here, their not reading GG posts could be seen as an advantage. As an aside, I've noticed that some those most vocal against GG have also been very vocal about this group being inclusive. If one observes that women post here (as a group) a lot less frequently then men, and if GG is easier for occasional posters, then the anti-GG attitude expressed here by a few would have the effect of disproportionately discriminating against women. > I am therefore not going to > recommend Google Groups to anyone as a means of posting to > python-list/c.l.p, That's fine. But when doing so please leave out the false metaphors... > any more than I would recommend writing it on a > Post-It note and feeding it into your floppy drive. ...such as posting here via GG is similar to feeding post-its into a floppy drive. From python at mrabarnett.plus.com Wed Nov 14 13:48:52 2012 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 14 Nov 2012 18:48:52 +0000 Subject: Error In-Reply-To: References: Message-ID: <50A3E794.7080900@mrabarnett.plus.com> On 2012-11-14 15:18, inshu chauhan wrote: > > for this code m getting this error : > > CODE : > def ComputeClasses(data): > radius = .5 > points = [] > for cy in xrange(0, data.height): > for cx in xrange(0, data.width): > if data[cy,cx] != (0.0,0.0,0.0): > centre = data[cy, cx] > points.append(centre) > > Look at this line: > change = True > It's indented the same as the preceding 'if' statement, which means that it's executed even if the body of the 'if' statement wasn't executed and it hasn't assigned to 'centre'. So 'change' has been set to True, the 'while' loop is entered, and subsequently an attempt is made to get 'centre', which hasn't been set. > while change: > > for ring_number in xrange(1, 1000): > change = False > new_indices = GenerateRing(cx, cy, ring_number) > > > for idx in new_indices: > point = data[idx[0], idx[1]] > > if point == (0.0, 0.0, 0.0 ): > continue > else: > dist = distance(centre, point) > if dist < radius : > print point > points.append(point) > change = True > print change > > The indentation of this line looks wrong to me: > break > It'll affect the 'for cx' loop at the end of its first iteration, every time. > > ERROR : > Traceback (most recent call last): > File "Z:\modules\classification2.py", line 74, in > ComputeClasses(data) > File "Z:\modules\classification2.py", line 56, in ComputeClasses > dist = distance(centre, point) > UnboundLocalError: local variable 'centre' referenced before assignment > > And i am unable to understand .. WHY ? > From gvanem at broadpark.no Wed Nov 14 14:02:19 2012 From: gvanem at broadpark.no (Gisle Vanem) Date: Wed, 14 Nov 2012 20:02:19 +0100 Subject: Running a curl command within py script References: Message-ID: <189FCC221EF74937AEB126FDA8020F70@dev.null> "Smaran Harihar" wrote: > i found pycurl to execute python curl command but not sure how I can > execute the curl command using the pycurl. > > curl -u admin:geoserver -v -XPUT -H 'Content-type: text/plain' -d > 'file:/var/www/geo/shapefile/csvQshp/Quercus_iltisii.shp' > http://localhost:8080/geoserver/rest/workspaces/acme/datastores/Quercus_iltisii/external.shp > ? Off-topic, but here's an idea. Use the 'curl --libcurl foo' option to see what setopt() calls to use in PyCurl. Like: import sys, pycurl c = pycurl.Curl() c.setopt (c.URL, 'http://localhost:8080/geoserver/rest/workspaces/acme/datastores/Quercus_iltisii/external.shp') c.setopt (c.USERPWD, 'admin:geoserver') c.setopt (c.POSTFIELDS, 'file:/var/www/geo/shapefile/csvQshp/Quercus_iltisii.shp'); etc. --gv From andrea.crotti.0 at gmail.com Wed Nov 14 15:43:59 2012 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Wed, 14 Nov 2012 20:43:59 +0000 Subject: creating size-limited tar files In-Reply-To: <50A3C7D7.7050103@davea.name> References: <509AD812.2060605@gmail.com> <87lie4lnxr.fsf@nitrogen.i-did-not-set--mail-host-address--so-tickle-me> <87fw4clg8b.fsf@nitrogen.i-did-not-set--mail-host-address--so-tickle-me> <50A3C287.80707@davea.name> <50A3C7D7.7050103@davea.name> Message-ID: <50A4028F.2020805@gmail.com> On 11/14/2012 04:33 PM, Dave Angel wrote: > Well, as I said, I don't see how the particular timing has anything to > do with the rest of the thread. If you want to do an ls within a Python > program, go ahead. But if all you need can be done with ls itself, then > it'll be slower to launch python just to run it. > > Your first timing runs python, which runs two new shells, ls, and cat. > Your second timing runs ls and cat. > > So the difference is starting up python, plus starting the shell two > extra times. > > I'd also be curious if you flushed the system buffers before each > timing, as the second test could be running entirely in system memory. > And no, I don't know offhand how to flush them in Linux, just that > without it, your timings are not at all repeatable. Note the two > identical runs here. > > davea at think:~/temppython$ time ls -lR ~ | cat > /dev/null > > real 0m0.164s > user 0m0.020s > sys 0m0.000s > davea at think:~/temppython$ time ls -lR ~ | cat > /dev/null > > real 0m0.018s > user 0m0.000s > sys 0m0.010s > > real time goes down by 90%, while user time drops to zero. > And on a 3rd and subsequent run, sys time goes to zero as well. > Right I didn't think about that.. Anyway the only thing I wanted to understand is if using the pipes in subprocess is exactly the same as doing the Linux pipe, or not. And any idea on how to run it in ram? Maybe if I create a pipe in tmpfs it might already work, what do you think? From d at davea.name Wed Nov 14 15:57:20 2012 From: d at davea.name (Dave Angel) Date: Wed, 14 Nov 2012 15:57:20 -0500 Subject: creating size-limited tar files In-Reply-To: <50A4028F.2020805@gmail.com> References: <87lie4lnxr.fsf@nitrogen.i-did-not-set--mail-host-address--so-tickle-me> <87fw4clg8b.fsf@nitrogen.i-did-not-set--mail-host-address--so-tickle-me> <50A3C287.80707@davea.name> <50A3C7D7.7050103@davea.name> <50A4028F.2020805@gmail.com> Message-ID: <50A405B0.3020004@davea.name> On 11/14/2012 03:43 PM, Andrea Crotti wrote: > > Anyway the only thing I wanted to understand is if using the pipes in > subprocess is exactly the same as doing > the Linux pipe, or not. It's not the same thing, but you can usually assume it's close. Other effects will probably dominate any differences. > > And any idea on how to run it in ram? > Maybe if I create a pipe in tmpfs it might already work, what do you think? > > In a good virtual OS, such as Linux, there's very little predictable difference between running in RAM (which is to say reading and writing to the swap file) or reading and writing to a file you specify. In fact, writing to a file can frequently be quicker, if it's sequential. Why? Linux is using any given piece of physical RAM to map a file, or an allocated buffer, or shared memory, or nearly anything. About the only special cases are the kind of RAM that has to be locked into RAM for hardware reasons. Linux decides which pieces to keep in memory, whether it calls it caching, swapping, memory mapping, or whatever. And frequently, attempts to "beat the system" result in counterintuitive results. If in doubt, measure. But choose your measures carefully, because lots more things will change the measurement than you might expect. -- DaveA From rosuav at gmail.com Wed Nov 14 16:54:29 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 15 Nov 2012 08:54:29 +1100 Subject: Subprocess puzzle and two questions In-Reply-To: References: Message-ID: On Thu, Nov 15, 2012 at 3:20 AM, Roy Smith wrote: > I wrote: >>> Oh, my. You're using DNS as a replacement for ping? Fair enough. In >>> that case, all you really care about is that you can connect to port 53 >>> on the server... >>> >>> s = socket.socket() >>> s.connect(('8.8.8.8', 53)) > > In article , > Chris Angelico wrote: >>That assumes that (a) the remote server supports TCP for DNS > > This is true. I honestly don't know what percentage of DNS servers > out there only support UDP. The two I tried (Google's 8.8.8.8, and my > Apple TimeCapsule) both supported TCP, but that's hardly a > representitive sample. I don't know either, all I know is that DNSReport recommends supporting TCP, and none of my DNS servers ever fail that check. >> and (b) that connection time for TCP is comparable to >> ping or an actual DNS lookup. > > My first thought to solve both of these is that it shouldn't be too > hard to hand-craft a minimal DNS query and send it over UDP. Then, I > hunted around a bit and found that somebody had already done that, in > spades. Take a look at http://www.dnspython.org; it might be exactly > what's needed here. Yeah, that sounds like a good option. I'm slightly surprised that there's no way with the Python stdlib to point a DNS query at a specific server, but dnspython might be the solution. On the flip side, dnspython is dauntingly large; it looks like a full implementation of DNS, but I don't see a simple entrypoint that wraps it all up into a simple function that can be bracketed with time.time() calls (granted, I only skimmed the docs VERY quickly). So it may be simpler to hand-craft an outgoing UDP packet once, save it as a string literal, send that, and just wait for any response. That eliminates all DNS protocolling and just times the round trip. ChrisA From jg07024 at gmail.com Wed Nov 14 17:48:58 2012 From: jg07024 at gmail.com (John Graves) Date: Thu, 15 Nov 2012 11:48:58 +1300 Subject: Describing code with slides Message-ID: I'm trying to work out the best way to provide a description of some code in a set of presentation slides which can be played backward and forward through the bits that someone is trying to understand (rather than using a screencast -- where you can never seem to rewind just the right amount ...). This example http://slidespeech.com/s/dLItUXnOen/ required taking multiple screenshots and then importing them into PowerPoint with Insert > Photo Album. I added a voice over script to the speaker notes. The result was then run through SlideSpeech. If the code is available on a website, the slides can link to that site: http://slidespeech.com/s/dLItUXnOen?link=http://codepad.org/X1GPad5c Any suggestions on 1/ how to speed up the authoring process for this or 2/ improve the display of the code (such as avoiding showing all the IDE)? -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Wed Nov 14 18:07:53 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 14 Nov 2012 23:07:53 GMT Subject: Simple Question regarding running .py program References: <22ae6963-6700-4ea7-b396-344d89ead7f2@googlegroups.com> <8085ebf2-9f08-4889-84c9-f786d6776001@googlegroups.com> <5f1bf680-ca10-446e-abbc-d3ba55327f2d@googlegroups.com> <6c3c5341-1fb4-4664-80b8-d3e08687830f@googlegroups.com> Message-ID: <50a42449$0$29978$c3e8da3$5496439d@news.astraweb.com> On Wed, 14 Nov 2012 10:20:13 -0800, rurpy wrote: > On 11/14/2012 06:35 AM, Chris Angelico wrote: [...] >> I stand by what I said. Members, plural, of this list. I didn't say >> "all members of", ergo the word "some" is superfluous, yet not needful, >> as Princess Ida put it. > > Then you would have no problem I suppose with "Australians are racists" > because some Australians are racist and I didn't say "all"? Speaking as an Australian, I wouldn't have a problem with that, because Australians *are* racist. To the degree that we can talk about a "national character", the national character of Australia is racist, even if many Aussies aren't, and many more try not to be. In any case, your example is provocative. Here's a less provocative version: [paraphrase] Then you would have no problem I suppose with "People have two legs" because some people have two legs and I didn't say "all"? [end paraphrase] > As a user of GG, Usenet and email lists I claim you are wrong. GG does > NOT require "quite a bit of extra work". If it did, I wouldn't use it. > For occasional posters, GG is EASIER. (It would be even easier if > Google would fix their execrable quoting behaviour but as I showed, it > is easy to work around that.) I think you are ignoring setup time and a > number of other secondary factors, things that are very significant to > occasional posters, in your evaluation of "easy". I don't understand why you suggest counting setup time for the alternatives to Google Groups, but *don't* consider setup time for Google Groups. You had to create a Google Account didn't you? You've either put in your mobile phone number -- and screw those who don't have one -- or you get badgered every time you sign in. You do sign in don't you? For *really* occasional posters, they might not even remember their Google account details from one post to the next. So they have to either create a new account, or go through the process of recreating it. Why do you ignore these factors in *your* evaluation of "easy"? We all do it -- when we talk about "easy" or "difficult", we have an idealised generalised user in mind. Your idealised user is different from Chris' idealised user. You are both generalising. And that's *my* generalisation. Even if you are right that Google Groups is easier for some users, in my opinion it is easy in the same way as the Dark Side of the Force. Quicker, faster, more seductive, but ultimately destructive. > As for "best", that is clearly a matter of opinion. The very fact that > someone would killfile an entire class of poster based on a some others' > posts reeks of intolerance and group-think. Intolerance? Yes. But group-think? You believe that people are merely copying the group's prejudice against Google Groups. I don't think they are. I think that the dislike against GG is group consensus based on the evidence of our own eyes, not a mere prejudice. The use of Google Groups is, as far as I can tell, the single most effective predictor of badly written, badly thought out, badly formatted posts, and a common source of spam. As for intolerance, you say that like it is that a bad thing. Why should people have to tolerate bad behaviour? Google Groups *encourages* bad behaviour. Should we tolerate spam because any spam filter might occasionally throw away a legitimate mail? Should we tolerate acid attacks on women because occasionally there might be some woman who actually deserves such a horrible fate? I don't think so. For many things, intolerance is a *good* thing, and many people here believe that intolerance for Google Groups is one of those cases. You of course are free to make whatever arrangements to filter spam and use Google Groups as you like, but you equally must respect other people's right to control their own inbox by filtering away GG posters. [...] > As an aside, I've noticed that some those most vocal against GG have > also been very vocal about this group being inclusive. I call bullshit. If you are going to accuse people of being "very vocal" against minorities, you damn well better have some evidence to back up your claim. And if you don't, I would expect a public apology for that slur. -- Steven From rurpy at yahoo.com Wed Nov 14 18:57:05 2012 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Wed, 14 Nov 2012 15:57:05 -0800 (PST) Subject: Simple Question regarding running .py program In-Reply-To: <50a42449$0$29978$c3e8da3$5496439d@news.astraweb.com> References: <22ae6963-6700-4ea7-b396-344d89ead7f2@googlegroups.com> <8085ebf2-9f08-4889-84c9-f786d6776001@googlegroups.com> <5f1bf680-ca10-446e-abbc-d3ba55327f2d@googlegroups.com> <6c3c5341-1fb4-4664-80b8-d3e08687830f@googlegroups.com> <50a42449$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Wednesday, November 14, 2012 4:07:53 PM UTC-7, Steven D'Aprano wrote: > On Wed, 14 Nov 2012 10:20:13 -0800, rurpy wrote: > [...] > > As an aside, I've noticed that some those most vocal against GG have > > also been very vocal about this group being inclusive. > > I call bullshit. If you are going to accuse people of being "very vocal" > against minorities, you damn well better have some evidence to back up > your claim. > > And if you don't, I would expect a public apology for that slur. I wasn't very clear. I should have written "...those most vocal against GG have also been very vocal *in favor* of this group being inclusive." In the next paragraph which you clipped I pointed out the irony of that attitude versus one possible effect of advocating the blacklisting of GG posters: > > If one observes that women post here (as a group) > > a lot less frequently then men, and if GG is easier > > for occasional posters, then the anti-GG attitude > > expressed here by a few would have the effect of > > disproportionately discriminating against women. Response to your other points will need to wait until I have more time. From joshua.landau.ws at gmail.com Wed Nov 14 19:02:28 2012 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Thu, 15 Nov 2012 00:02:28 +0000 Subject: Simple Question regarding running .py program In-Reply-To: <50a42449$0$29978$c3e8da3$5496439d@news.astraweb.com> References: <22ae6963-6700-4ea7-b396-344d89ead7f2@googlegroups.com> <8085ebf2-9f08-4889-84c9-f786d6776001@googlegroups.com> <5f1bf680-ca10-446e-abbc-d3ba55327f2d@googlegroups.com> <6c3c5341-1fb4-4664-80b8-d3e08687830f@googlegroups.com> <50a42449$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steven, whilst I hold you in high regard, this post seems spurned by bias. I would urge you to reconsider your *argument*, although your *position* has merit. On 14 November 2012 23:07, Steven D'Aprano < steve+comp.lang.python at pearwood.info> wrote: > On Wed, 14 Nov 2012 10:20:13 -0800, rurpy wrote: > > > On 11/14/2012 06:35 AM, Chris Angelico wrote: > >> I stand by what I said. Members, plural, of this list. I didn't say > >> "all members of", ergo the word "some" is superfluous, yet not needful, > >> as Princess Ida put it. > > > > Then you would have no problem I suppose with "Australians are racists" > > because some Australians are racist and I didn't say "all"? > > Speaking as an Australian, I wouldn't have a problem with that, because > Australians *are* racist. To the degree that we can talk about a > "national character", the national character of Australia is racist, even > if many Aussies aren't, and many more try not to be. > > In any case, your example is provocative. Here's a less provocative > version: > > [paraphrase] > Then you would have no problem I suppose with "People have two legs" > because some people have two legs and I didn't say "all"? > [end paraphrase] Ahem? Seriously? With rounding, all people *do* have two legs. That's not fair. In fact, the idea that most users of this list ban Google Groups is probably false. Additionally, being provocative isn't actually weakness of his argument, although it is a distraction. He asked if you had a "problem" with it on the basis that if it was a fair claim you would not, in order to show that it was not a fair claim. That would *imply* his correctness. How about this(?): "People have brown hair." > As a user of GG, Usenet and email lists I claim you are wrong. GG does > > NOT require "quite a bit of extra work". If it did, I wouldn't use it. > > For occasional posters, GG is EASIER. (It would be even easier if > > Google would fix their execrable quoting behaviour but as I showed, it > > is easy to work around that.) I think you are ignoring setup time and a > > number of other secondary factors, things that are very significant to > > occasional posters, in your evaluation of "easy". > > I don't understand why you suggest counting setup time for the > alternatives to Google Groups, but *don't* consider setup time for Google > Groups. You had to create a Google Account didn't you? You've either put > in your mobile phone number -- and screw those who don't have one -- or > you get badgered every time you sign in. You do sign in don't you? > That's not fair, either, on the basis that almost everyone has a Google account. Additionally, who signs in manually any more [*wink*]? > For *really* occasional posters, they might not even remember their > Google account details from one post to the next. So they have to either > create a new account, or go through the process of recreating it. Why do > you ignore these factors in *your* evaluation of "easy"? > They might not remember their Email account either. This seems to be a really contrived point. > We all do it -- when we talk about "easy" or "difficult", we have an > idealised generalised user in mind. Your idealised user is different from > Chris' idealised user. You are both generalising. And that's *my* > generalisation. > All of this is really beside the point, anyway. He claimed the he used it because *he* found it easier. And there was claim that there were good reasons to use Google Groups. If you claim that his point is invalid because it only talks about *his* idealised user, you've only invalidated your own point. > Even if you are right that Google Groups is easier for some users, in my > opinion it is easy in the same way as the Dark Side of the Force. > Quicker, faster, more seductive, but ultimately destructive. How so? > > As for "best", that is clearly a matter of opinion. The very fact that > > someone would killfile an entire class of poster based on a some others' > > posts reeks of intolerance and group-think. > > Intolerance? Yes. But group-think? You believe that people are merely > copying the group's prejudice against Google Groups. I don't think they > are. I think that the dislike against GG is group consensus based on the > evidence of our own eyes, not a mere prejudice. Consensus? Hrm...A synonym of "consensus" is "unanimity". This argument's existence basically disproves that. > The use of Google Groups > is, as far as I can tell, the single most effective predictor of badly > written, badly thought out, badly formatted posts, and a common source of > spam. > > As for intolerance, you say that like it is that a bad thing. Why should > people have to tolerate bad behaviour? Google Groups *encourages* bad > behaviour. I think this is a valid thing to say. I agree largely because it's the user's choice to read and reply to this list. Calling someone helpful in a community "intolerant" because you think they could be nicer would be a bit intolerant yourself. > Should we tolerate spam because any spam filter might > occasionally throw away a legitimate mail? This is an optional list. Some who want to be more helpful will, others will not. So is the spam filter. If you filter, you live with the consequences. If you oft get *very* important mail, then the answer to your question is likely "Yes" if you take "tolerate" to mean "not filter". > Should we tolerate acid > attacks on women because occasionally there might be some woman who > actually deserves such a horrible fate? I don't think so. This is not a thoughtful thing to say. He said nothing of the sort. > For many > things, intolerance is a *good* thing, and many people here believe that > intolerance for Google Groups is one of those cases. You of course are free to make whatever arrangements to filter spam and > use Google Groups as you like, but you equally must respect other > people's right to control their own inbox by filtering away GG posters. > Surely then you must respect his right to use and endorse Google Groups, even if you do not agree with his reasons. You have shown as much respect to his opinions and choices as he has to yours. What really matters is that you respect the person, regardless. > > As an aside, I've noticed that some those most vocal against GG have > > also been very vocal about this group being inclusive. > > I call bullshit. If you are going to accuse people of being "very vocal" > against minorities, you damn well better have some evidence to back up > your claim. > > And if you don't, I would expect a public apology for that slur. > Have you misread his sentence(s), or have I? -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjreedy at udel.edu Wed Nov 14 19:11:13 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 14 Nov 2012 19:11:13 -0500 Subject: access spreadsheet data In-Reply-To: References: Message-ID: On 11/14/2012 1:35 AM, Amit Agrawal wrote: > my problem is, i want to access data in spreadsheet to python code manualy > My data is > > 1/1982 8:00:000 > 1/2/1982 8:00:000 > 1/3/1982 8:00:000 > 1/4/1982 8:00:000 > 1/5/1982 8:00:000.7885 > 1/6/1982 8:00:000 > 1/7/1982 8:00:000 > 1/8/1982 8:00:001.6127 You used tabs, which get deleted by some mail/news readers. Anyway, here is a start: data='''\ 1/1982 8:00:00 0 1/5/1982 8:00:00 0.7885 1/19/1982 8:00:00 0 1/20/1982 8:00:00 0''' lines = data.split('\n') # up to here, only for example for line in lines: fields = line.split() print('date {}: time {}: value {}'.format(fields[0], fields[1], fields[2])) >>> date 1/1982: time 8:00:00: value 0 date 1/5/1982: time 8:00:00: value 0.7885 date 1/19/1982: time 8:00:00: value 0 date 1/20/1982: time 8:00:00: value 0 For real usage assume data are in data.txt in current directory. Then start with with open('data.txt') as lines: for line in lines.... -- Terry Jan Reedy From steve+comp.lang.python at pearwood.info Wed Nov 14 19:30:45 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 15 Nov 2012 00:30:45 GMT Subject: Simple Question regarding running .py program References: <22ae6963-6700-4ea7-b396-344d89ead7f2@googlegroups.com> <8085ebf2-9f08-4889-84c9-f786d6776001@googlegroups.com> <5f1bf680-ca10-446e-abbc-d3ba55327f2d@googlegroups.com> <6c3c5341-1fb4-4664-80b8-d3e08687830f@googlegroups.com> <50a42449$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50a437b5$0$29978$c3e8da3$5496439d@news.astraweb.com> On Wed, 14 Nov 2012 23:07:53 +0000, Steven D'Aprano wrote: > On Wed, 14 Nov 2012 10:20:13 -0800, rurpy wrote: [...] > [...] >> As an aside, I've noticed that some those most vocal against GG have >> also been very vocal about this group being inclusive. > > I call bullshit. If you are going to accuse people of being "very vocal" > against minorities, you damn well better have some evidence to back up > your claim. > > And if you don't, I would expect a public apology for that slur. Ah, apparently I misread Rurpy's comment. I'm sorry, I was completely wrong to accuse Rurpy of accusing others of being opposed to including minorites in this group. My apologies Rurpy, I don't know how I made that misreading. -- Steven From tjreedy at udel.edu Wed Nov 14 20:08:03 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 14 Nov 2012 20:08:03 -0500 Subject: Simple Question regarding running .py program In-Reply-To: References: <22ae6963-6700-4ea7-b396-344d89ead7f2@googlegroups.com> <8085ebf2-9f08-4889-84c9-f786d6776001@googlegroups.com> Message-ID: On 11/13/2012 11:10 PM, Chris Angelico wrote: > On Wed, Nov 14, 2012 at 2:31 PM, Caroline Hou wrote: >> Thank you Dave and everybody here for your helpful comments!This place is awesome! I found this group when I googled python-list. Seems like this is not the usual way you guys access the list? > > There are several ways to communicate with this list. > > * The comp.lang.python newsgroup - get a newsreader (there are plenty > around), and either connect to your ISP's news server (if they have > one that carries c.l.p) or to a public server, some of which cost > money. > * Use a news-to-web gateway such as Google Groups. That specific one > is deprecated on this list, as there's more noise than signal from > Google Groups. > * The mailing list python-list, delivered directly to your inbox many > times a day. This is what I personally use. news.gmane.org group gmane.comp.python.general many 'mail' programs such as Outlook Express or Thunderbird also handle news -- Terry Jan Reedy From tjreedy at udel.edu Wed Nov 14 20:18:45 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 14 Nov 2012 20:18:45 -0500 Subject: Simple Question regarding running .py program In-Reply-To: <6c3c5341-1fb4-4664-80b8-d3e08687830f@googlegroups.com> References: <22ae6963-6700-4ea7-b396-344d89ead7f2@googlegroups.com> <8085ebf2-9f08-4889-84c9-f786d6776001@googlegroups.com> <5f1bf680-ca10-446e-abbc-d3ba55327f2d@googlegroups.com> <6c3c5341-1fb4-4664-80b8-d3e08687830f@googlegroups.com> Message-ID: On 11/14/2012 2:02 AM, rurpy at yahoo.com wrote: > On the other hand finding and configuring a newsreader > for someone whose never done it before, as you recommend, > is a major time consumer. Use a mail/news program such as Thunderbird and the newsreader comes for free. Setting up a gmane account with Thunderbird was, as I remember rather easy, easier than setting up a mail account. -- Terry Jan Reedy From 129km09 at gmail.com Wed Nov 14 20:47:33 2012 From: 129km09 at gmail.com (su29090) Date: Wed, 14 Nov 2012 17:47:33 -0800 (PST) Subject: Python questions help Message-ID: I brought a python book and i'm a beginner and I read and tried to do the questions and I still get it wrong. How to create a program that reads an uspecified number of integers, that determines how many positive and negative values have been read, and computes the total and average of the input values(not counting zeroes). My program have to end with the input 0 and have to display the average as a floating-point number. Use nested loops that display the following patterns in separate programs: 1 12 123 1234 12345 123456 123456 12345 1234 123 12 1 1 21 321 4321 54321 654321 Write a program that computes the following summation: 1/ 1+square root of 2 + 1/ 1+square root of 2 + square root of 3 + 1/ 1+square root of 3 + square root of 4...+ 1/ 1+square root of 624 + square root of 625 How to a program to draw a chessboard using range? Thanks From roy at panix.com Wed Nov 14 20:49:19 2012 From: roy at panix.com (Roy Smith) Date: Wed, 14 Nov 2012 20:49:19 -0500 Subject: Subprocess puzzle and two questions References: Message-ID: In article , Chris Angelico wrote: > I'm slightly surprised that there's no way with the Python stdlib to > point a DNS query at a specific server Me too, including the "only slightly" part. The normal high-level C resolver routines (getaddrinfo/getnameinfo, or even the old gethostbyname series), don't expose any way to do that. You have to dig quite far down in the resolver library stack to get to the point where you can do that. The concept of not knowing or caring which specific server has the data you need is quite deeply baked into the basic DNS architecture. From rosuav at gmail.com Wed Nov 14 21:01:16 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 15 Nov 2012 13:01:16 +1100 Subject: Python questions help In-Reply-To: References: Message-ID: On Thu, Nov 15, 2012 at 12:47 PM, su29090 <129km09 at gmail.com> wrote: > I brought a python book and i'm a beginner and I read and tried to do the questions and I still get it wrong. Pick one of the questions, write as much of the code as you can, and then post the specific difficulties you're having. If we write the code for you, it won't help you to learn, will it? Once you have some code that isn't working, we can help you to figure out what it is that isn't working. But do your best to write the code yourself first. Chris Angelico From rosuav at gmail.com Wed Nov 14 21:04:21 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 15 Nov 2012 13:04:21 +1100 Subject: Subprocess puzzle and two questions In-Reply-To: References: Message-ID: On Thu, Nov 15, 2012 at 12:49 PM, Roy Smith wrote: > In article , > Chris Angelico wrote: > >> I'm slightly surprised that there's no way with the Python stdlib to >> point a DNS query at a specific server > > Me too, including the "only slightly" part. The normal high-level C > resolver routines (getaddrinfo/getnameinfo, or even the old > gethostbyname series), don't expose any way to do that. You have to dig > quite far down in the resolver library stack to get to the point where > you can do that. The concept of not knowing or caring which specific > server has the data you need is quite deeply baked into the basic DNS > architecture. Indeed. But Python boasts that the batteries are included, and given the wealth of other networking facilities that are available, it is a bit of a hole that you can't run DNS queries in this way. Mind you, if Python's managed to get this far without it being a major stumbling-block, that probably means that it's not a serious lack. And I don't think many people write DNS *servers* in Python. (Most people don't write DNS servers at all, since BIND exists. But I did exactly that this week, since it would be easier than most other options.) ChrisA From roy at panix.com Wed Nov 14 21:10:29 2012 From: roy at panix.com (Roy Smith) Date: Wed, 14 Nov 2012 21:10:29 -0500 Subject: Subprocess puzzle and two questions References: Message-ID: In article , Chris Angelico wrote: > Indeed. But Python boasts that the batteries are included, and given > the wealth of other networking facilities that are available, it is a > bit of a hole that you can't run DNS queries in this way. Think of the socket and struct modules as a pile of carbon rods and gobs of zinc paste, from which you can assemble your own batteries, and make them in exactly the shape and size you need. From rosuav at gmail.com Wed Nov 14 21:21:07 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 15 Nov 2012 13:21:07 +1100 Subject: Subprocess puzzle and two questions In-Reply-To: References: Message-ID: On Thu, Nov 15, 2012 at 1:10 PM, Roy Smith wrote: > In article , > Chris Angelico wrote: > >> Indeed. But Python boasts that the batteries are included, and given >> the wealth of other networking facilities that are available, it is a >> bit of a hole that you can't run DNS queries in this way. > > Think of the socket and struct modules as a pile of carbon rods and gobs > of zinc paste, from which you can assemble your own batteries, and make > them in exactly the shape and size you need. Then assembly language is a pile of protons, neutrons, and electrons... :) ChrisA From d at davea.name Wed Nov 14 21:55:20 2012 From: d at davea.name (Dave Angel) Date: Wed, 14 Nov 2012 21:55:20 -0500 Subject: Subprocess puzzle and two questions In-Reply-To: References: Message-ID: <50A45998.9080200@davea.name> On 11/14/2012 09:21 PM, Chris Angelico wrote: > On Thu, Nov 15, 2012 at 1:10 PM, Roy Smith wrote: >> In article , >> Chris Angelico wrote: >> >>> Indeed. But Python boasts that the batteries are included, and given >>> the wealth of other networking facilities that are available, it is a >>> bit of a hole that you can't run DNS queries in this way. >> Think of the socket and struct modules as a pile of carbon rods and gobs >> of zinc paste, from which you can assemble your own batteries, and make >> them in exactly the shape and size you need. > Then assembly language is a pile of protons, neutrons, and electrons... And real machine language (microcode) is a pile of quarks; fermions versus bosons. But in recent years, you pretty much have to work at Intel to see that part of the processor. -- DaveA From kushal.kumaran+python at gmail.com Wed Nov 14 23:53:08 2012 From: kushal.kumaran+python at gmail.com (Kushal Kumaran) Date: Thu, 15 Nov 2012 10:23:08 +0530 Subject: Subprocess puzzle and two questions In-Reply-To: References: Message-ID: <87d2zflb63.fsf@nitrogen.i-did-not-set--mail-host-address--so-tickle-me> Chris Angelico writes: > On Thu, Nov 15, 2012 at 12:49 PM, Roy Smith wrote: >> In article , >> Chris Angelico wrote: >> >>> I'm slightly surprised that there's no way with the Python stdlib to >>> point a DNS query at a specific server >> >> Me too, including the "only slightly" part. The normal high-level C >> resolver routines (getaddrinfo/getnameinfo, or even the old >> gethostbyname series), don't expose any way to do that. You have to dig >> quite far down in the resolver library stack to get to the point where >> you can do that. The concept of not knowing or caring which specific >> server has the data you need is quite deeply baked into the basic DNS >> architecture. > > Indeed. But Python boasts that the batteries are included, and given > the wealth of other networking facilities that are available, it is a > bit of a hole that you can't run DNS queries in this way. > > Mind you, if Python's managed to get this far without it being a major > stumbling-block, that probably means that it's not a serious lack. And > I don't think many people write DNS *servers* in Python. (Most people > don't write DNS servers at all, since BIND exists. But I did exactly > that this week, since it would be easier than most other options.) > Indeed. Most people would prefer if random applications didn't make their own decisions about using specific DNS servers. That way, the users can make their own configuration choices (gai.conf, nsswitch.conf) according to their site preferences. If your application needs that level of control (if you're writing a nslookup replacement for some reason, perhaps), dnspython (www.dnspython.org) seems to have it. -- regards, kushal From aahz at pythoncraft.com Thu Nov 15 00:42:58 2012 From: aahz at pythoncraft.com (Aahz) Date: 14 Nov 2012 21:42:58 -0800 Subject: DNS from Python (was Re: Subprocess puzzle and two questions) References: Message-ID: In article , Chris Angelico wrote: >On Thu, Nov 15, 2012 at 3:20 AM, Roy Smith wrote: >> >> My first thought to solve both of these is that it shouldn't be too >> hard to hand-craft a minimal DNS query and send it over UDP. Then, I >> hunted around a bit and found that somebody had already done that, in >> spades. Take a look at http://www.dnspython.org; it might be exactly >> what's needed here. > >Yeah, that sounds like a good option. I'm slightly surprised that >there's no way with the Python stdlib to point a DNS query at a >specific server, but dnspython might be the solution. On the flip >side, dnspython is dauntingly large; it looks like a full >implementation of DNS, but I don't see a simple entrypoint that wraps >it all up into a simple function that can be bracketed with >time.time() calls (granted, I only skimmed the docs VERY quickly). So >it may be simpler to hand-craft an outgoing UDP packet once, save it >as a string literal, send that, and just wait for any response. That >eliminates all DNS protocolling and just times the round trip. >From one of my scripts lying around: domain = MAILTO.split('@',1)[1] server = str(dns.resolver.query(domain, 'MX')[0].exchange) You'll need to play around a bit to find out what that does, but it should point you in the right direction. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "LL YR VWL R BLNG T S" -- www.nancybuttons.com From dieter at handshake.de Thu Nov 15 02:31:15 2012 From: dieter at handshake.de (Dieter Maurer) Date: Thu, 15 Nov 2012 08:31:15 +0100 Subject: Python garbage collector/memory manager behaving strangely References: <59f8c664-8f11-439e-8002-ca76ee24a632@g7g2000pbh.googlegroups.com> <50570de3$0$29981$c3e8da3$5496439d@news.astraweb.com> Message-ID: <87lie3l3uk.fsf@handshake.de> aahz at pythoncraft.com (Aahz) writes: > ... >>>> def readlines(f): >>>> lines = [] >>>> while "f is not empty": >>>> line = f.readline() >>>> if not line: break >>>> if len(line) > 2 and line[-2:] == '|\n': >>>> lines.append(line) >>>> yield ''.join(lines) >>>> lines = [] >>>> else: >>>> lines.append(line) >>> >>> There's a few changes I'd make: >>> I'd change the name to something else, so as not to shadow the built-in, > ... > Actually, as an experienced programmer, I *do* think it is confusing as > evidenced by the mistake Dave made! Segregated namespaces are wonderful > (per Zen), but let's not pollute multiple namespaces with same name, > either. > > It may not be literally shadowing the built-in, but it definitely > mentally shadows the built-in. I disagree with you. namespaces are there that in working with a namespace I do not need to worry much about other namespaces. Therefore, calling a function "readlines" is very much justified (if it reads lines from a file), even though there was a module around with name "readlines". By the way, the module is named "readline" (not "readlines"). From dieter at handshake.de Thu Nov 15 02:37:24 2012 From: dieter at handshake.de (Dieter Maurer) Date: Thu, 15 Nov 2012 08:37:24 +0100 Subject: Getting "empty" attachment with smtplib References: Message-ID: <87haorl3kb.fsf@handshake.de> Tobiah writes: > I just found out that the attachment works fine > when I read the mail from the gmail website. Thunderbird > complains that the attachment is empty. The MIME standard (a set of RFCs) specifies how valid messages with attachments should look like. Fetch the mail (unprocessed if possible) and look at its structure. If it is conformant to the MIME standard, then "Thunderbird" made a mistake; otherwise, something went wrong with the message construction. I can already say that "smtplib" is not to blame. It is (mostly) unconcerned with the internal structure of the message -- and by itself will not empty attachments. From nawijn at gmail.com Thu Nov 15 05:43:50 2012 From: nawijn at gmail.com (Marco Nawijn) Date: Thu, 15 Nov 2012 02:43:50 -0800 (PST) Subject: Printing a text over an image In-Reply-To: <2e801261-9ec6-4660-bbd1-6168c3dce8ed@v3g2000yqb.googlegroups.com> References: <2e801261-9ec6-4660-bbd1-6168c3dce8ed@v3g2000yqb.googlegroups.com> Message-ID: <726c1358-7b71-456e-88e1-51672baa17bf@googlegroups.com> On Wednesday, November 7, 2012 5:52:36 PM UTC+1, Martha Morrigan wrote: > Hi guys, > > > > Using python, wxpython and sqlite in a windows system, Im trying to > > print some certificates/diplomas/cards with a image at background with > > the name of person/text over it. > > > > I know the basic steps to print the text using win32print from Pywin32 > > but...: > > > > 1) I dont know how to add an image and set it to background. > > > > while ..... > > > > ..... > > > > # Query sqlite rows and collumn name and set the self.text for > > each certificate > > > > ..... > > > > # Now send to printer > > > > DC = win32ui.CreateDC() > > DC.CreatePrinterDC(win32print.GetDefaultPrinter()) > > > > DC.SetMapMode(win32con.MM_TWIPS) > > > > DC.StartDoc("Certificates Job") > > > > DC.StartPage() > > > > ux = 1000 > > uy = -1000 > > lx = 5500 > > ly = -55000 > > > > DC.DrawText(self.text, (ux, uy, lx, ly),win32con.DT_LEFT) > > > > DC.EndPage() > > DC.EndDoc() > > > > This printer-code is inside a while loop calling each people name from > > a sqlite database per check condition. > > > > > > 2) All the names of database was printed at same page... how i command > > the printer to spit out 1 page per name from the database? > > > > > > 3) Any more simple approach or module to deals with printers (paper > > and/or pdf) will be welcome. > > > > Thanks in advance, > > > > Martha Hi Martha, Since you are on windows, why don't you use MS/Word directly from Python. It has all the abstractions you need (documents, pages, tables, figures, printing etc.). Working with MS/Word through the win32 bindings is really simple. I have done this a while ago for some automatic report generation. The basic routine is to start recording your actions with the MS/Word macro recorder, do the things you want to do, stop recording, look at the VB code and guess the equivalent Python code. This is not as bad as it sounds. It normally is really straightforward. I am on Linux at the moment, so I cannot present any code examples. Feel free to try and post some example code if you get stuck. Marco From jeanmichel at sequans.com Thu Nov 15 06:09:37 2012 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Thu, 15 Nov 2012 12:09:37 +0100 (CET) Subject: Python-list Digest, Vol 110, Issue 106 In-Reply-To: Message-ID: <183316795.134061.1352977777233.JavaMail.root@sequans.com> ----- Original Message ----- > Hi, > I have a question about Django. I easy_installed Django1.4 and > psycopg2, and python manage.py syncdb. And gave me a error; No > module named psycopg2.extensions. posgre9.1 is installed. > It works fine on my MAC but not my Windows. Does anyone know about > this issue > Hope to resolve this issue soon. > Jun > -- > http://mail.python.org/mailman/listinfo/python-list Hi, Check that the psychopg2 version is the same on your MAC and Windows. Otherwise you better ask this question on the django mailing list. JM -- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you. -------------- next part -------------- An HTML attachment was scrubbed... URL: From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Thu Nov 15 06:20:32 2012 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Thu, 15 Nov 2012 12:20:32 +0100 Subject: Python garbage collector/memory manager behaving strangely In-Reply-To: References: <5056871E.7050206@davea.name> Message-ID: Am 17.09.2012 04:28 schrieb Jadhav, Alok: > Thanks Dave for clean explanation. I clearly understand what is going on > now. I still need some suggestions from you on this. > > There are 2 reasons why I was using self.rawfile.read().split('|\n') > instead of self.rawfile.readlines() > > - As you have seen, the line separator is not '\n' but its '|\n'. > Sometimes the data itself has '\n' characters in the middle of the line > and only way to find true end of the line is that previous character > should be a bar '|'. I was not able specify end of line using > readlines() function, but I could do it using split() function. > (One hack would be to readlines and combine them until I find '|\n'. is > there a cleaner way to do this?) > - Reading whole file at once and processing line by line was must > faster. Though speed is not of very important issue here but I think the > tie it took to parse complete file was reduced to one third of original > time. With def itersep(f, sep='\0', buffering=1024, keepsep=True): if keepsep: keepsep=sep else: keepsep='' data = f.read(buffering) next_line = data # empty? -> end. while next_line: # -> data is empty as well. lines = data.split(sep) for line in lines[:-1]: yield line+keepsep next_line = f.read(buffering) data = lines[-1] + next_line # keepsep: only if we have something. if (not keepsep) or data: yield data you can iterate over everything you want without needing too much memory. Using a larger "buffering" might improve speed a little bit. Thomas From chip9munk at gmail.com Thu Nov 15 07:29:03 2012 From: chip9munk at gmail.com (chip9munk at gmail.com) Date: Thu, 15 Nov 2012 04:29:03 -0800 (PST) Subject: debugging in eclipse Message-ID: Hi all! I have a stupid problem, for which I cannot find a solution... I have a python module, lets call it debugTest.py. and it contains: def test(): a=1 b=2 c=a+b c so as simple as possible. Now I would like to debug it in eclipse.. (I have pydev and all) so the question is how do I debug the test function? (by debug I mean go into it in the debugging mode and execute step by step, inspect the variables and so on.. you know, like it is so easily done in Matlab for example). I place a break point in the function, run the debugger and it stars and is terminated immediately. (of course it does not know where to go..) So how can I do this? please help! Thank you all in advance! From roy at panix.com Thu Nov 15 07:49:21 2012 From: roy at panix.com (Roy Smith) Date: Thu, 15 Nov 2012 07:49:21 -0500 Subject: debugging in eclipse References: Message-ID: In article , chip9munk at gmail.com wrote: > Now I would like to debug it in eclipse.. Heh. It took me a while to realize that the subject line was not referring to http://en.wikipedia.org/wiki/Solar_eclipse_of_November_13,_2012 From d at davea.name Thu Nov 15 08:41:40 2012 From: d at davea.name (Dave Angel) Date: Thu, 15 Nov 2012 08:41:40 -0500 Subject: debugging in eclipse In-Reply-To: References: Message-ID: <50A4F114.7080906@davea.name> On 11/15/2012 07:29 AM, chip9munk at gmail.com wrote: > Hi all! > > I have a stupid problem, for which I cannot find a solution... > > I have a python module, lets call it debugTest.py. > > and it contains: > def test(): > a=1 > b=2 > c=a+b > c > > so as simple as possible. > > Now I would like to debug it in eclipse.. (I have pydev and all) > so the question is how do I debug the test function? (by debug I mean go into it in the debugging mode and execute step by step, inspect the variables and so on.. you know, like it is so easily done in Matlab for example). > I don't know Eclipse, but you probably have to have some top-level code in your program. As it stands, it's just an importable module, doing nothing useful when you run it. Add a call to test() to the end of the file, and it might do better. I'd also add a print statement, just to assure yourself that it's running. -- DaveA From ulrich.eckhardt at dominolaser.com Thu Nov 15 08:43:26 2012 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Thu, 15 Nov 2012 14:43:26 +0100 Subject: debugging in eclipse In-Reply-To: References: Message-ID: Am 15.11.2012 13:29, schrieb chip9munk at gmail.com: > I have a python module, lets call it debugTest.py. > > and it contains: > def test(): > a=1 > b=2 > c=a+b > c > > so as simple as possible. Should that be "return c" instead of "c" on a line? > Now I would like to debug it in eclipse.. (I have pydev and all) so > the question is how do I debug the test function? [...] > I place a break point in the function, run the debugger and it stars > and is terminated immediately. For a start, I would try to actually call the function. Just add "print(test())" after the function definition. Uli From chip9munk at gmail.com Thu Nov 15 08:43:53 2012 From: chip9munk at gmail.com (chip9munk at gmail.com) Date: Thu, 15 Nov 2012 05:43:53 -0800 (PST) Subject: debugging in eclipse In-Reply-To: References: Message-ID: On Thursday, November 15, 2012 1:49:22 PM UTC+1, Roy Smith wrote: > Heh. It took me a while to realize that the subject line was not > referring to > http://en.wikipedia.org/wiki/Solar_eclipse_of_November_13,_2012 :)) From martin.hellwig at gmail.com Thu Nov 15 08:44:21 2012 From: martin.hellwig at gmail.com (Martin P. Hellwig) Date: Thu, 15 Nov 2012 05:44:21 -0800 (PST) Subject: debugging in eclipse In-Reply-To: References: Message-ID: On Thursday, 15 November 2012 12:29:04 UTC, chip... at gmail.com wrote: > Hi all! > > > > I have a stupid problem, for which I cannot find a solution... > > > > I have a python module, lets call it debugTest.py. > > > > and it contains: > > def test(): > > a=1 > > b=2 > > c=a+b > > c > > > > so as simple as possible. > > > > Now I would like to debug it in eclipse.. (I have pydev and all) > > so the question is how do I debug the test function? (by debug I mean go into it in the debugging mode and execute step by step, inspect the variables and so on.. you know, like it is so easily done in Matlab for example). > > > > I place a break point in the function, run the debugger and it stars and is terminated immediately. (of course it does not know where to go..) > > > > So how can I do this? please help! > > > > Thank you all in advance! I assume you have at the end of the debugTest.py file something like this: if __name__ == '__main__': test() From chip9munk at gmail.com Thu Nov 15 08:45:43 2012 From: chip9munk at gmail.com (chip9munk at gmail.com) Date: Thu, 15 Nov 2012 05:45:43 -0800 (PST) Subject: debugging in eclipse In-Reply-To: References: Message-ID: On Thursday, November 15, 2012 2:42:09 PM UTC+1, Dave Angel wrote: > Add a call to test() to the end of the file, and it might do better. > > I'd also add a print statement, just to assure yourself that it's running. > > thanks, that is it, (stupid me) now if I have many functions in the model I will simply ad a call to the specific function at the end and that is it... working on the project too long, all the simple and smart ideas are long gone ;) thanks! From chip9munk at gmail.com Thu Nov 15 08:45:43 2012 From: chip9munk at gmail.com (chip9munk at gmail.com) Date: Thu, 15 Nov 2012 05:45:43 -0800 (PST) Subject: debugging in eclipse In-Reply-To: References: Message-ID: On Thursday, November 15, 2012 2:42:09 PM UTC+1, Dave Angel wrote: > Add a call to test() to the end of the file, and it might do better. > > I'd also add a print statement, just to assure yourself that it's running. > > thanks, that is it, (stupid me) now if I have many functions in the model I will simply ad a call to the specific function at the end and that is it... working on the project too long, all the simple and smart ideas are long gone ;) thanks! From chip9munk at gmail.com Thu Nov 15 08:46:49 2012 From: chip9munk at gmail.com (chip9munk at gmail.com) Date: Thu, 15 Nov 2012 05:46:49 -0800 (PST) Subject: debugging in eclipse In-Reply-To: References: Message-ID: <67c7866f-3c35-4cbd-ae42-75ac5c34ce74@googlegroups.com> On Thursday, November 15, 2012 2:44:22 PM UTC+1, Martin P. Hellwig wrote: > I assume you have at the end of the debugTest.py file something like this: > if __name__ == '__main__': > test() no i did not have it... is main really necessary? From roy at panix.com Thu Nov 15 08:54:08 2012 From: roy at panix.com (Roy Smith) Date: Thu, 15 Nov 2012 08:54:08 -0500 Subject: debugging in eclipse References: Message-ID: In article , Dave Angel wrote: > I'd also add a print statement, just to assure yourself that it's running. My trick to make sure something is running is to add "assert 0". To be fair, I usually start by adding a print statement, as Dave suggests. If I see the output, I know it ran. But if I don't see the output, there's two possibilities. Either it didn't run, or it ran but something snarfed the output and hid it from my eyes. That's common in test frameworks. It's also common in background processes where stdout goes who-knows-where, and it's anybody's guess how the logging config might be borked. On the other hand, an "assert 0" is pretty much guaranteed to produce some visible evidence that it ran. About the only thing that would stop it is if somebody had wrapped the code in a try block which caught AssertionError (or Exception). From alister.ware at ntlworld.com Thu Nov 15 09:21:51 2012 From: alister.ware at ntlworld.com (Alister) Date: Thu, 15 Nov 2012 14:21:51 GMT Subject: debugging in eclipse References: <67c7866f-3c35-4cbd-ae42-75ac5c34ce74@googlegroups.com> Message-ID: <3U6ps.201510$nB6.78865@fx21.am4> On Thu, 15 Nov 2012 05:46:49 -0800, chip9munk wrote: > On Thursday, November 15, 2012 2:44:22 PM UTC+1, Martin P. Hellwig > wrote: >> I assume you have at the end of the debugTest.py file something like >> this: >> if __name__ == '__main__': >> test() > > no i did not have it... > > is main really necessary? doing it that way means that it will only call test when executed directly & not when imported as a module -- I used to be an agnostic, but now I'm not so sure. From chip9munk at gmail.com Thu Nov 15 09:27:16 2012 From: chip9munk at gmail.com (chip9munk at gmail.com) Date: Thu, 15 Nov 2012 06:27:16 -0800 (PST) Subject: debugging in eclipse In-Reply-To: References: Message-ID: <93b24860-dde9-4dc6-9e75-f961b702ac1f@googlegroups.com> On Thursday, November 15, 2012 2:43:26 PM UTC+1, Ulrich Eckhardt wrote: > Should that be "return c" instead of "c" on a line? oh it is just a silly example function, the functionality is not important. It does not have to return anything... > For a start, I would try to actually call the function. Just add > "print(test())" after the function definition. yes I call the function now, that was the thing... From chip9munk at gmail.com Thu Nov 15 09:28:00 2012 From: chip9munk at gmail.com (chip9munk at gmail.com) Date: Thu, 15 Nov 2012 06:28:00 -0800 (PST) Subject: debugging in eclipse In-Reply-To: <3U6ps.201510$nB6.78865@fx21.am4> References: <67c7866f-3c35-4cbd-ae42-75ac5c34ce74@googlegroups.com> <3U6ps.201510$nB6.78865@fx21.am4> Message-ID: <973c36bb-c554-480e-ad4a-72e3286bf539@googlegroups.com> On Thursday, November 15, 2012 3:21:52 PM UTC+1, Alister wrote: > doing it that way means that it will only call test when executed > directly & not when imported as a module I see, thanks! From eric.frederich at gmail.com Thu Nov 15 09:38:02 2012 From: eric.frederich at gmail.com (Eric Frederich) Date: Thu, 15 Nov 2012 09:38:02 -0500 Subject: error importing smtplib Message-ID: Hello, I created some bindings to a 3rd party library. I have found that when I run Python and import smtplib it works fine. If I first log into the 3rd party application using my bindings however I get a bunch of errors. What do you think this 3rd party login could be doing that would affect the ability to import smtp lib. Any suggestions for debugging this further. I am lost. This works... import smtplib FOO_login() This doesn't... FOO_login() import smtplib Errors..... >>> import smtplib ERROR:root:code for hash sha224 was not found. Traceback (most recent call last): File "/opt/foo/python27/lib/python2.7/hashlib.py", line 139, in globals()[__func_name] = __get_hash(__func_name) File "/opt/foo/python27/lib/python2.7/hashlib.py", line 103, in __get_openssl_constructor return __get_builtin_constructor(name) File "/opt/foo/python27/lib/python2.7/hashlib.py", line 91, in __get_builtin_constructor raise ValueError('unsupported hash type %s' % name) ValueError: unsupported hash type sha224 ERROR:root:code for hash sha256 was not found. Traceback (most recent call last): File "/opt/foo/python27/lib/python2.7/hashlib.py", line 139, in globals()[__func_name] = __get_hash(__func_name) File "/opt/foo/python27/lib/python2.7/hashlib.py", line 103, in __get_openssl_constructor return __get_builtin_constructor(name) File "/opt/foo/python27/lib/python2.7/hashlib.py", line 91, in __get_builtin_constructor raise ValueError('unsupported hash type %s' % name) ValueError: unsupported hash type sha256 ERROR:root:code for hash sha384 was not found. Traceback (most recent call last): File "/opt/foo/python27/lib/python2.7/hashlib.py", line 139, in globals()[__func_name] = __get_hash(__func_name) File "/opt/foo/python27/lib/python2.7/hashlib.py", line 103, in __get_openssl_constructor return __get_builtin_constructor(name) File "/opt/foo/python27/lib/python2.7/hashlib.py", line 91, in __get_builtin_constructor raise ValueError('unsupported hash type %s' % name) ValueError: unsupported hash type sha384 ERROR:root:code for hash sha512 was not found. Traceback (most recent call last): File "/opt/foo/python27/lib/python2.7/hashlib.py", line 139, in globals()[__func_name] = __get_hash(__func_name) File "/opt/foo/python27/lib/python2.7/hashlib.py", line 103, in __get_openssl_constructor return __get_builtin_constructor(name) File "/opt/foo/python27/lib/python2.7/hashlib.py", line 91, in __get_builtin_constructor raise ValueError('unsupported hash type %s' % name) ValueError: unsupported hash type sha512 -------------- next part -------------- An HTML attachment was scrubbed... URL: From aahz at pythoncraft.com Thu Nov 15 10:56:17 2012 From: aahz at pythoncraft.com (Aahz) Date: 15 Nov 2012 07:56:17 -0800 Subject: debugging in eclipse References: Message-ID: In article , Roy Smith wrote: >In article , > Dave Angel wrote: >> >> I'd also add a print statement, just to assure yourself that it's running. > >My trick to make sure something is running is to add "assert 0". ``1/0`` is shorter. ;-) -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "LL YR VWL R BLNG T S" -- www.nancybuttons.com From aahz at pythoncraft.com Thu Nov 15 11:01:45 2012 From: aahz at pythoncraft.com (Aahz) Date: 15 Nov 2012 08:01:45 -0800 Subject: Detect file is locked - windows References: <2af436e3-1336-42ae-aa2f-9e33135a3f2c@googlegroups.com> Message-ID: In article , Tim Golden wrote: >On 14/11/2012 00:33, Ali Akhavan wrote: >> >> I am trying to open a file in 'w' mode open('file', 'wb'). open() >> will throw with IOError with errno 13 if the file is locked by >> another application or if user does not have permission to open/write >> to the file. >> >> How can I distinguish these two cases ? Namely, if some application >> has the file open or not. > >Can I ask what you expect to do differently in each of those cases? In >other words, if you can't access the file, you can't access it. (Not to >dismiss your question; I just wonder how you're going to handle the >different cases) Real-life use case for user-requested operation: if no permission, skip the file "permanently" (until it changes, at least); if locked, place in retry loop. And in response to your other post, you absolutely want to use CreateFileW()... ;-) -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "LL YR VWL R BLNG T S" -- www.nancybuttons.com From kevin.gullikson at gmail.com Thu Nov 15 11:04:01 2012 From: kevin.gullikson at gmail.com (Kevin Gullikson) Date: Thu, 15 Nov 2012 10:04:01 -0600 Subject: Dictionary of Functions Message-ID: Hi all, I am trying to make a dictionary of functions, where each entry in the dictionary is the same function with a few of the parameters set to specific parameters. My actual use is pretty complicated, but I managed to boil down the issue I am having to the following example: In [1]: def test_fcn(a, x): ...: return a*x ...: In [2]: fcn_dict = {} In [3]: for i in [1,2,3]: ...: fcn_dict[i] = lambda x: test_fcn(i, x) ...: In [4]: fcn_dict Out[4]: {1: at 0x102b42c08>, 2: at 0x102b42b18>, 3: at 0x102b42c80>} In [5]: fcn_dict[1](5) Out[5]: 15 In [6]: fcn_dict[2](5) Out[6]: 15 In [7]: fcn_dict[3](5) Out[7]: 15 As you can see, all of the functions are returning the value that I want for fcn_dict[3]. If I make separate functions for each case instead of a dictionary it works, but I would really prefer to use dictionaries if possible. Is there a way to make this work? Thanks, Kevin Gullikson -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at mrabarnett.plus.com Thu Nov 15 11:27:28 2012 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 15 Nov 2012 16:27:28 +0000 Subject: Dictionary of Functions In-Reply-To: References: Message-ID: <50A517F0.1020102@mrabarnett.plus.com> On 2012-11-15 16:04, Kevin Gullikson wrote: > Hi all, > > I am trying to make a dictionary of functions, where each entry in the > dictionary is the same function with a few of the parameters set to > specific parameters. My actual use is pretty complicated, but I managed > to boil down the issue I am having to the following example: > > In [1]: def test_fcn(a, x): > ...: return a*x > ...: > > In [2]: fcn_dict = {} > > In [3]: for i in [1,2,3]: > ...: fcn_dict[i] = lambda x: test_fcn(i, x) > ...: > > In [4]: fcn_dict > Out[4]: > {1: at 0x102b42c08>, > 2: at 0x102b42b18>, > 3: at 0x102b42c80>} > > In [5]: fcn_dict[1](5) > Out[5]: 15 > > In [6]: fcn_dict[2](5) > Out[6]: 15 > > In [7]: fcn_dict[3](5) > Out[7]: 15 > > > As you can see, all of the functions are returning the value that I want > for fcn_dict[3]. If I make separate functions for each case instead of a > dictionary it works, but I would really prefer to use dictionaries if > possible. Is there a way to make this work? > It's looking up 'i' at the time that the function is called, which is after the 'for' loop has finished and 'i' has been left as 3. What you need to do is capture the current value of 'i'. The usual way is with a default argument: for i in [1,2,3]: fcn_dict[i] = lambda x, i=i: test_fcn(i, x) From tjreedy at udel.edu Thu Nov 15 11:57:23 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 15 Nov 2012 11:57:23 -0500 Subject: error importing smtplib In-Reply-To: References: Message-ID: On 11/15/2012 9:38 AM, Eric Frederich wrote: > Hello, > > I created some bindings to a 3rd party library. > I have found that when I run Python and import smtplib it works fine. > If I first log into the 3rd party application using my bindings however > I get a bunch of errors. > > What do you think this 3rd party login could be doing that would affect > the ability to import smtp lib. I don't know what 'login' actually means,... > This works... > > import smtplib > FOO_login() > > This doesn't... > > FOO_login() > import smtplib but my first guess is that FOO_login alters the module search path so that at least one of smtplib, hashlib, or the _xxx modules imported by hashlib is being imported from a different place. To check that import sys before = sys.path FOO_login() print sys.path==before Similar code can check anything else accessible through sys. > Errors..... > > >>> import smtplib > ERROR:root:code for hash sha224 was not found. I am puzzled by this line before the traceback. I cannot find 'ERROR' in either smtplib or hashlib. > Traceback (most recent call last): > File "/opt/foo/python27/lib/python2.7/hashlib.py", line 139, in > globals()[__func_name] = __get_hash(__func_name) > File "/opt/foo/python27/lib/python2.7/hashlib.py", line 103, in > __get_openssl_constructor > return __get_builtin_constructor(name) > File "/opt/foo/python27/lib/python2.7/hashlib.py", line 91, in > __get_builtin_constructor > raise ValueError('unsupported hash type %s' % name) > ValueError: unsupported hash type sha224 [snip similar messages] It is also unusual to get multiple tracebacks. *Exactly* how are you running python and is 2.7 what you intend to run? -- Terry Jan Reedy From steve+comp.lang.python at pearwood.info Thu Nov 15 12:05:35 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 15 Nov 2012 17:05:35 GMT Subject: debugging in eclipse References: Message-ID: <50a520df$0$29978$c3e8da3$5496439d@news.astraweb.com> On Thu, 15 Nov 2012 07:56:17 -0800, Aahz wrote: > In article , Roy Smith > wrote: >>In article , >> Dave Angel wrote: >>> >>> I'd also add a print statement, just to assure yourself that it's >>> running. >> >>My trick to make sure something is running is to add "assert 0". > > ``1/0`` is shorter. ;-) It is also guaranteed to run, unlike assert. -- Steven From ckaynor at zindagigames.com Thu Nov 15 12:13:20 2012 From: ckaynor at zindagigames.com (Chris Kaynor) Date: Thu, 15 Nov 2012 09:13:20 -0800 Subject: Dictionary of Functions In-Reply-To: References: Message-ID: On Thu, Nov 15, 2012 at 8:04 AM, Kevin Gullikson wrote: > Hi all, > > I am trying to make a dictionary of functions, where each entry in the > dictionary is the same function with a few of the parameters set to specific > parameters. My actual use is pretty complicated, but I managed to boil down > the issue I am having to the following example: > > In [1]: def test_fcn(a, x): > ...: return a*x > ...: > > In [2]: fcn_dict = {} > > In [3]: for i in [1,2,3]: > ...: fcn_dict[i] = lambda x: test_fcn(i, x) > ...: In this case, I would recommend using functools.partial instead of a lambda. It will solve this problem (although MRAB's solution will as well), is trivially faster (likely insignificant in any real application), and, IMO, clearer: for i in [1,2,3]: fcn_dict[i] = functools.partial(test_fcn, i) Note that this only works if you are either only specifying the first arguments by position, or specifying arguments by keyword. There is no way to specify the second argument by position; you'd have to pass it as a keyword argument. From toby at tobiah.org Thu Nov 15 12:26:21 2012 From: toby at tobiah.org (Tobiah) Date: Thu, 15 Nov 2012 09:26:21 -0800 Subject: Getting "empty" attachment with smtplib In-Reply-To: References: Message-ID: <1B9ps.45628$c16.31159@newsfe10.iad> > I can already say that "smtplib" is not to blame. It is (mostly) unconcerned > with the internal structure of the message -- and by itself > will not empty attachments. On the advice of a co-worker, I tried using web2py's gluon.tools.Mail. It was easier to accomplish the attachment, and Thunderbird opened the .pdf just fine. Thanks for the suggestions. Tobiah From eric.frederich at gmail.com Thu Nov 15 13:48:08 2012 From: eric.frederich at gmail.com (Eric Frederich) Date: Thu, 15 Nov 2012 13:48:08 -0500 Subject: error importing smtplib In-Reply-To: References: Message-ID: Thanks for the idea. sys.path was the same before and after the login What else should I be checking? On Thu, Nov 15, 2012 at 11:57 AM, Terry Reedy wrote: > On 11/15/2012 9:38 AM, Eric Frederich wrote: > >> Hello, >> >> I created some bindings to a 3rd party library. >> I have found that when I run Python and import smtplib it works fine. >> If I first log into the 3rd party application using my bindings however >> I get a bunch of errors. >> >> What do you think this 3rd party login could be doing that would affect >> the ability to import smtp lib. >> > > I don't know what 'login' actually means,... > > > This works... >> >> import smtplib >> FOO_login() >> >> This doesn't... >> >> FOO_login() >> import smtplib >> > > but my first guess is that FOO_login alters the module search path so that > at least one of smtplib, hashlib, or the _xxx modules imported by hashlib > is being imported from a different place. To check that > > import sys > before = sys.path > FOO_login() > print sys.path==before > > Similar code can check anything else accessible through sys. > > > Errors..... >> >> >>> import smtplib >> ERROR:root:code for hash sha224 was not found. >> > > I am puzzled by this line before the traceback. I cannot find 'ERROR' in > either smtplib or hashlib. > > > Traceback (most recent call last): >> File "/opt/foo/python27/lib/**python2.7/hashlib.py", line 139, in >> >> globals()[__func_name] = __get_hash(__func_name) >> File "/opt/foo/python27/lib/**python2.7/hashlib.py", line 103, in >> __get_openssl_constructor >> return __get_builtin_constructor(**name) >> File "/opt/foo/python27/lib/**python2.7/hashlib.py", line 91, in >> __get_builtin_constructor >> raise ValueError('unsupported hash type %s' % name) >> ValueError: unsupported hash type sha224 >> > [snip similar messages] > > It is also unusual to get multiple tracebacks. *Exactly* how are you > running python and is 2.7 what you intend to run? > > -- > Terry Jan Reedy > > -- > http://mail.python.org/**mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From eric.frederich at gmail.com Thu Nov 15 14:01:23 2012 From: eric.frederich at gmail.com (Eric Frederich) Date: Thu, 15 Nov 2012 14:01:23 -0500 Subject: error importing smtplib In-Reply-To: References: Message-ID: Sorry, only saw your first response, didn't see the others. I compiled Python 2.7.2 myself with --enable-shared To create standalone applications that interact with this 3rd party program your main C file instead of having a "main" function has a FOO_user_main function. When you link your program you link against a provided foo_main.o file. My python executable (called FOO_user_main) was created from python.c but with main replaced with FOO_user_main.... no other differences. I have been using this type of installation for a couple of years and everything works fine. I only get these errors/warnings on one environment out of 10 or so development and qa machines. Any help trying to figure out what is different before and after the login would be appreciated. Is there some place in /proc I could look to see what happened? Thanks, ~Eric On Thu, Nov 15, 2012 at 11:57 AM, Terry Reedy wrote: > On 11/15/2012 9:38 AM, Eric Frederich wrote: > >> Hello, >> >> I created some bindings to a 3rd party library. >> I have found that when I run Python and import smtplib it works fine. >> If I first log into the 3rd party application using my bindings however >> I get a bunch of errors. >> >> What do you think this 3rd party login could be doing that would affect >> the ability to import smtp lib. >> > > I don't know what 'login' actually means,... > > > This works... >> >> import smtplib >> FOO_login() >> >> This doesn't... >> >> FOO_login() >> import smtplib >> > > but my first guess is that FOO_login alters the module search path so that > at least one of smtplib, hashlib, or the _xxx modules imported by hashlib > is being imported from a different place. To check that > > import sys > before = sys.path > FOO_login() > print sys.path==before > > Similar code can check anything else accessible through sys. > > > Errors..... >> >> >>> import smtplib >> ERROR:root:code for hash sha224 was not found. >> > > I am puzzled by this line before the traceback. I cannot find 'ERROR' in > either smtplib or hashlib. > > > Traceback (most recent call last): >> File "/opt/foo/python27/lib/**python2.7/hashlib.py", line 139, in >> >> globals()[__func_name] = __get_hash(__func_name) >> File "/opt/foo/python27/lib/**python2.7/hashlib.py", line 103, in >> __get_openssl_constructor >> return __get_builtin_constructor(**name) >> File "/opt/foo/python27/lib/**python2.7/hashlib.py", line 91, in >> __get_builtin_constructor >> raise ValueError('unsupported hash type %s' % name) >> ValueError: unsupported hash type sha224 >> > [snip similar messages] > > It is also unusual to get multiple tracebacks. *Exactly* how are you > running python and is 2.7 what you intend to run? > > -- > Terry Jan Reedy > > -- > http://mail.python.org/**mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From andriy.kornatskyy at live.com Thu Nov 15 14:33:24 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Thu, 15 Nov 2012 22:33:24 +0300 Subject: Lazy Attribute Message-ID: A lazy attribute is an attribute that is calculated on demand and only once. The post below shows how you can use lazy attribute in your Python class: http://mindref.blogspot.com/2012/11/python-lazy-attribute.html Comments or suggestions are welcome. Thanks. Andriy Kornatskyy From joshua.landau.ws at gmail.com Thu Nov 15 14:41:03 2012 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Thu, 15 Nov 2012 19:41:03 +0000 Subject: Dictionary of Functions In-Reply-To: References: Message-ID: On 15 November 2012 17:13, Chris Kaynor wrote: > On Thu, Nov 15, 2012 at 8:04 AM, Kevin Gullikson > wrote: > > Hi all, > > > > I am trying to make a dictionary of functions, where each entry in the > > dictionary is the same function with a few of the parameters set to > specific > > parameters. My actual use is pretty complicated, but I managed to boil > down > > the issue I am having to the following example: > > > > In [1]: def test_fcn(a, x): > > ...: return a*x > > ...: > > > > In [2]: fcn_dict = {} > > > > In [3]: for i in [1,2,3]: > > ...: fcn_dict[i] = lambda x: test_fcn(i, x) > > ...: > > In this case, I would recommend using functools.partial instead of a > lambda. > It will solve this problem (although MRAB's solution will as well), is > trivially faster (likely insignificant in any real application), and, > IMO, clearer: > > for i in [1,2,3]: > fcn_dict[i] = functools.partial(test_fcn, i) > > Note that this only works if you are either only specifying the first > arguments by position, or specifying arguments by keyword. There is no > way to specify the second argument by position; you'd have to pass it > as a keyword argument. > Another way to do this is by making a factory function: >>> def factor_multiplier(factor): ... def factor_multiply(target): ... return factor * target ... return factor_multiply ... Testing it: >>> trippler = factor_multiplier(3) >>> trippler .factor_multiply at 0x7ffeb5d6db90> >>> trippler(10) 30 >>> doubler = factor_multiplier(2) >>> doubler(15) 30 >>> doubler(trippler(1)) 6 >>> Solving your problem: >>> function_dict = {} >>> for i in range(100): ... function_dict[i] = factor_multiplier(i) ... >>> >>> function_dict[42](2) 84 >>> function_dict[20](3) 60 >>> This is definitely longer that Chris' approach, but it's more powerful overall. It's worth learning and using both. In a sense, you were close, but you were just not catching the variable: >>> function_dict.clear() >>> for i in range(100): ... function_dict[i] = (lambda i: lambda x: x*i)(i) ... >>> function_dict[19](2) 38 >>> -------------- next part -------------- An HTML attachment was scrubbed... URL: From joshua.landau.ws at gmail.com Thu Nov 15 15:12:56 2012 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Thu, 15 Nov 2012 20:12:56 +0000 Subject: Python questions help In-Reply-To: References: Message-ID: On 15 November 2012 01:47, su29090 <129km09 at gmail.com> wrote: > I brought a python book and i'm a beginner and I read and tried to do the > questions and I still get it wrong. > > How to create a program that reads an uspecified number of integers, that > determines how many positive and negative values have been read, and > computes the total and average of the input values(not counting zeroes). My > program have to end with the input 0 and have to display the average as a > floating-point number. > Think about the requirements. For example, you need here: * Some sort of loop to take in an unlimited number of integers * Some way to take in integers * Some way of stopping when a 0 is entered * Some way of finding the total and averages * Some way of putting it all together If you can solve some of these sub-tasks and just need help with others, tell us what you've done and we'll help you work out the rest. Use nested loops that display the following patterns in separate programs: > > 1 > 12 > 123 > 1234 > 12345 > 123456 > What are the patterns here? 1st: 1 -> 2 -> 3 -> 4 -> ... 2nd: 1 -> 12 -> 123 -> 1234 -> ... How would you do these? How would you combine them? > 123456 > 12345 > 1234 > 123 > 12 > 1 > How would you change the above to do this instead? > 1 > 21 > 321 > 4321 > 54321 > 654321 > How would you change it to do this? > Write a program that computes the following summation: > > 1/ 1+square root of 2 + 1/ 1+square root of 2 + square root of 3 + 1/ > 1+square root of 3 + square root of 4...+ 1/ 1+square root of 624 + square > root of 625 > You've probably written this wrong. You've got: (1/ 1) + (square root of 2) + (1/ 1) + (square root of 2) + (square root of 3) + (1/ 1) + (square root of 3) + (square root of 4)... + (1/ 1) + (square root of 624) + (square root of 625) Which you can write as: 1 + root(2) + 1 + root(2) + root(3) + 1 + root(3) + root(4) + ... + 1 + root(624) + root(625) As (1/1) is 1. You probably need brackets somewhere. I've never seen any equation like this, so I can't guess at what you really wanted. Do you know how to find the square root? Just search "square root python" if you do not. Then put it in a loop. > How to a program to draw a chessboard using range? > I imagine you want to use "loops", where a range is what you loop over. O X O X O X O X X O X O X O X O O X O X O X O X X O X O X O X O O X O X O X O X X O X O X O X O O X O X O X O X X O X O X O X O It's 8 by 8, so you want to loop 8 times for one dimension and 8 times for the other. How would you use range to do this? -------------- next part -------------- An HTML attachment was scrubbed... URL: From rurpy at yahoo.com Thu Nov 15 16:29:31 2012 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Thu, 15 Nov 2012 13:29:31 -0800 (PST) Subject: Simple Question regarding running .py program In-Reply-To: <50a42449$0$29978$c3e8da3$5496439d@news.astraweb.com> References: <22ae6963-6700-4ea7-b396-344d89ead7f2@googlegroups.com> <8085ebf2-9f08-4889-84c9-f786d6776001@googlegroups.com> <5f1bf680-ca10-446e-abbc-d3ba55327f2d@googlegroups.com> <6c3c5341-1fb4-4664-80b8-d3e08687830f@googlegroups.com> <50a42449$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 11/14/2012 04:07 PM, Steven D'Aprano wrote: > On Wed, 14 Nov 2012 10:20:13 -0800, rurpy wrote: I'll skip the issues already addressed by Joshua Landau. >[...] > I don't understand why you suggest counting setup time for the > alternatives to Google Groups, but *don't* consider setup time for Google > Groups. You had to create a Google Account didn't you? You've either put > in your mobile phone number -- and screw those who don't have one -- or > you get badgered every time you sign in. You do sign in don't you? Yes I sign in. And I've never entered my mobile phone number and no I don't get badgered every time (I've not been asked when I logged in several times today and I just tried again to confirm.) I have been asked in the past and just ignore it -- click Save (or whatever the button is) with a blank text box. As was pointed out, a large number of people already have Google accounts. And creating an account at Google is not comparable to researching news readers, downloading and installing software, setting up an account, etc for someone who's never even heard of usenet before. Subscribing to email is easier but it has its own problems (all those email you don't care about, the time delay (I've had to wait over 24 hours for a response for some email lists), what to do when you're traveling, reading some groups via email but others by GG. I've also had problems trying to post through Gmane and then there were Gmane's accessibly problems a few months ago, fixed now but for how long? The OP had already found her way to GG and managed to post. So the incremental cost for her to *continue* using GG is very low. That's in comparision to *changing* to a new posting method. I'm not saying the Google is always easier than an alternative but for a significant number of people it is. But most importantly it is *their* place to say what is easier for them, not yours or mine. [...] > Even if you are right that Google Groups is easier for some users, in my > opinion it is easy in the same way as the Dark Side of the Force. > Quicker, faster, more seductive, but ultimately destructive. Well, that's the best example of FUD I've seen in this thread so far. Congratulations. ;-) >> As for "best", that is clearly a matter of opinion. The very fact that >> someone would killfile an entire class of poster based on a some others' >> posts reeks of intolerance and group-think. > > Intolerance? Yes. But group-think? You believe that people are merely > copying the group's prejudice against Google Groups. Please don't tell me what I believe, especially when you get it wrong. > I don't think they > are. I think that the dislike against GG is group consensus based on the > evidence of our own eyes, not a mere prejudice. The use of Google Groups > is, as far as I can tell, the single most effective predictor of badly > written, badly thought out, badly formatted posts, and a common source of > spam. Again you repeat Chris Angelo's mistake (if it's a mistake). "group's prejudice"? You've presented no evidence that "the group" as a whole or in large part (including many people who seldom if ever post) share your view. Same with "consensus". A consensus of whom? Are you saying there is a consensus among those who dislike GG posts that they dislike GG posts? You say the dislike is "not a mere prejudice" and yet I can't help but wonder where the hard evidence is. I've not seen it posted though I could have easily missed it. All the news/email tools I use make it a little work to see where a post came from -- usually they'll be a button somewhere or a menu item to show the headers and one will scan those for the source. While easy enough it is still (at least for me) much easier to simply skip a post based on the subject/poster or a quick peak at the contents. So I've never had any inclination to look and have no idea how many crap posts come from GG. Yet you claim that a large percentage of this group has made the effort to do that. (Or maybe there is an easier way to check?) However I can easily imagine how some could think they are checking... "Oh man, what a crap post! Let's check the headers. Yup, just as I thought, Google Groups." But of course, our genius doesn't keep any records and the cases where he is wrong don't make as much impression on his memory. Further, he doesn't bother to check the headers on the non-crap posts. Even a junior-high science student could see the problems with this methodology. And how many people actually do even that? Some may find it an offensive suggestion but there is such a thing as group psychology and there are people who follow leaders. (I suspect those people are all of "us" at least some of the time.) Further people tend to be convinced even more easily when they think "everybody knows it". So when a few of the more prolific and respected posters here start talking about "the consensus is...", "deprecated on this list" and make statements like "GG is irredeemably broken" there are people who will accept that info at the posters' word. And when someone challenges the anti-GG claim, the issue get polarized and choosing one side or the other (still without much reliable evidence) becomes an action of support. Finally there is a significant amount of anti-Google sentiment in the world and it can difficult to tell if someone's motivation is purely against obnoxious posts or is also motivated in part by a desire to oppose Google. You yourself I think have publicly criticized Google and even advocated using an alternate search engine, yes? That kind of political decision is something each person should decide for themself and should not be subject to external pressure, at least not here. So your claim that everyone rejecting GG posts is doing so based solely on their own personal experience is not convincing to me. Now none of that proves that GG posts *aren't* largely crap. But I do object to hyping up the claim. And I still question the need to killfile GG posts based on: * My experience that it is not hard to ignore or quickly skip over crap posts and neither are they very numerous so killfiling does not provide much incremental benefit. * Killfiling is detrimental in that it loses the non- crap posts as well. (The OP of this thread is one example and I recall another example a week or two ago as well.) * My belief that is wrong on some deep level to reject people based on statistics for a group they belong to, especially on a list that makes a big point of being inclusive. >[...] > You of course are free to make whatever arrangements to filter spam and > use Google Groups as you like, but you equally must respect other > people's right to control their own inbox by filtering away GG posters. Right. And I've never said anything contrary. I am not promoting GG and am happy to see helpful suggestions on how to access this group by other means. My responses in this and other threads have pretty much been limited to correcting bad or very biased information and while I'm at it, expressing my opinion that killfiling based on a source (and one as widely used as GG) is not a good way to address the problem as expanded on above. From tjreedy at udel.edu Thu Nov 15 16:37:27 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 15 Nov 2012 16:37:27 -0500 Subject: error importing smtplib In-Reply-To: References: Message-ID: On 11/15/2012 1:48 PM, Eric Frederich wrote: > Thanks for the idea. > sys.path was the same before and after the login Too bad. That seems to be a typical cause of import failure. > What else should I be checking? No idea. You are working beyond my knowledge. But I might either look at the foo-login code carefully, or disable (comment out) parts of it to see what makes the import fail. -- Terry Jan Reedy From breamoreboy at yahoo.co.uk Thu Nov 15 17:15:05 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 15 Nov 2012 22:15:05 +0000 Subject: Simple Question regarding running .py program In-Reply-To: References: <22ae6963-6700-4ea7-b396-344d89ead7f2@googlegroups.com> <8085ebf2-9f08-4889-84c9-f786d6776001@googlegroups.com> <5f1bf680-ca10-446e-abbc-d3ba55327f2d@googlegroups.com> <6c3c5341-1fb4-4664-80b8-d3e08687830f@googlegroups.com> <50a42449$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 15/11/2012 21:29, rurpy at yahoo.com wrote: All I'll say is that when I read something on gmane via Thunderbird on Windows Vista on any of the 25 Python mailing lists that I subscribe to, I don't want to read the double spaced crap that comes from G$. I hence perceive a problem. 1) G$ are too interested in making huge profits and so have no interest in people who have to read the garbage that originates from them. 2) People who are too bone idle to get hold of any other mechanism that doesn't put the double spaced garbage in. Any and all answers to this dilemma are welcome. -- Cheers. Mark Lawrence. From ian.g.kelly at gmail.com Thu Nov 15 17:24:40 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 15 Nov 2012 15:24:40 -0700 Subject: Lazy Attribute In-Reply-To: References: Message-ID: On Thu, Nov 15, 2012 at 12:33 PM, Andriy Kornatskyy wrote: > > A lazy attribute is an attribute that is calculated on demand and only once. > > The post below shows how you can use lazy attribute in your Python class: > > http://mindref.blogspot.com/2012/11/python-lazy-attribute.html > > Comments or suggestions are welcome. The name "attribute" is not very descriptive. Why not "lazy_attribute" instead? I note that trying to access the descriptor on the class object results in an error: >>> from descriptors import attribute >>> class Foo: ... @attribute ... def forty_two(self): ... return 6 * 9 ... >>> Foo().forty_two 54 >>> Foo.forty_two Traceback (most recent call last): File "", line 1, in File ".\descriptors.py", line 33, in __get__ setattr(obj, f.__name__, val) AttributeError: 'NoneType' object has no attribute 'forty_two' If accessing the descriptor on the class object has no special meaning, then the custom is to return the descriptor object itself, as properties do. >>> class Foo: ... @property ... def forty_two(self): ... return 6 * 9 ... >>> Foo().forty_two 54 >>> Foo.forty_two From ian.g.kelly at gmail.com Thu Nov 15 17:46:19 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 15 Nov 2012 15:46:19 -0700 Subject: Lazy Attribute In-Reply-To: References: Message-ID: On Thu, Nov 15, 2012 at 12:33 PM, Andriy Kornatskyy wrote: > > A lazy attribute is an attribute that is calculated on demand and only once. > > The post below shows how you can use lazy attribute in your Python class: > > http://mindref.blogspot.com/2012/11/python-lazy-attribute.html > > Comments or suggestions are welcome. I should add that I like the approach you're taking here. Usually when I want a lazy property I just make an ordinary property of a memoized function call: def memoize(func): cache = {} @functools.wraps(func) def wrapper(*args, **kwargs): kwset = frozenset(kwargs.items()) try: return cache[args, kwset] except KeyError: result = cache[args, kwset] = func(*args, **kwargs) return result return wrapper class Foo: def __init__(self): self.times_called = 0 @property @memoize # Alternatively, use functools.lru_cache def forty_two(self): self.times_called += 1 return 6 * 9 >>> foo = Foo() >>> foo.times_called 0 >>> foo.forty_two 54 >>> foo.times_called 1 >>> foo.forty_two 54 >>> foo.times_called 1 Although you don't go into it in the blog entry, what I like about your approach of replacing the descriptor with an attribute is that, in addition to being faster, it makes it easy to force the object to lazily reevaluate the attribute, just by deleting it. Using the Person example from your blog post: >>> p = Person('John', 'Smith') >>> p.display_name 'John Smith' >>> p.display_name 'John Smith' >>> p.calls_count 1 >>> p.first_name = 'Eliza' >>> del p.display_name >>> p.display_name 'Eliza Smith' >>> p.calls_count 2 Although in general it's probably better to use some form of reactive programming for that. From nobody at nowhere.com Thu Nov 15 17:54:10 2012 From: nobody at nowhere.com (Nobody) Date: Thu, 15 Nov 2012 22:54:10 +0000 Subject: Subprocess puzzle and two questions References: Message-ID: On Wed, 14 Nov 2012 20:49:19 -0500, Roy Smith wrote: >> I'm slightly surprised that there's no way with the Python stdlib to >> point a DNS query at a specific server > > Me too, including the "only slightly" part. The normal high-level C > resolver routines (getaddrinfo/getnameinfo, or even the old > gethostbyname series), don't expose any way to do that. That's because the high-level routines aren't tied to DNS. gethostbyname() and getaddrinfo() use the NSS (name-service switch) mechanism, which is configured via /etc/nsswitch.conf. Depending upon configuration, hostnames can be looked up via a plain text file (/etc/hosts), Berkeley DB files, DNS, NIS, NIS+, LDAP, WINS, etc. DNS is just one particular back-end, which may or may not be used on any given system. If you specifically want to perform DNS queries, you have to use a DNS-specific interface (e.g. the res_* functions described in the resolver(3) manpage), or raw sockets, rather than a high-level interface such as gethostbyname() or getaddrinfo(). From brucegoodstein at gmail.com Thu Nov 15 18:20:10 2012 From: brucegoodstein at gmail.com (brucegoodstein at gmail.com) Date: Thu, 15 Nov 2012 15:20:10 -0800 (PST) Subject: Is there a simpler way to modify all arguments in a function before using the arguments? In-Reply-To: References: <18134e77-9b02-4aec-afb0-794ed900d194@googlegroups.com> Message-ID: On Saturday, November 10, 2012 10:35:12 AM UTC-5, Aahz wrote: > In article , > > Peter Otten <__peter__ at web.de> wrote: > > >Miki Tebeka wrote: > > > > > >>> Is there a simpler way to modify all arguments in a function before using > > >>> the arguments? > > >> > > >> You can use a decorator: > > >> > > >> from functools import wraps > > >> > > >> def fix_args(fn): > > >> @wraps(fn) > > >> def wrapper(*args): > > >> args = (arg.replace('_', '') for arg in args) > > >> return fn(*args) > > >> > > >> return wrapper > > >> > > >> @fix_args > > >> def foo(x, y): > > >> print(x) > > >> print(y) > > > > > >I was tempted to post that myself, but he said /simpler/ ;) > > > > From my POV, that *is* simpler. When you change the parameters for foo, > > you don't need to change the arg pre-processing. Also allows code reuse, > > probably any program needing this kind of processing once will need it > > again. > > -- > > Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ > > > > "....Normal is what cuts off your sixth finger and your tail..." --Siobhan Using a decorator works when named arguments are not used. When named arguments are used, unexpected keyword error is reported. Is there a simple fix? Thanks to all, Bruce Code: ----- from functools import wraps def fix_args(fn): @wraps(fn) def wrapper(*args): args = (arg.replace('_', '') for arg in args) return fn(*args) return wrapper @fix_args def foo(a1="", a2="", b1="", b2=""): print(a1) print(a2) print(b1) print(b2) foo ('a1a1_x', 'a2a2_x', 'b1b1_x', 'b2b2_____x') foo (a1='a1a1_x', a2='a2a2_x', b1='b1b1_x', b2='b2b2_____x') Results: -------- a1a1x a2a2x b1b1x b2b2x Traceback (most recent call last): File "C:\WORK\masterDB_Update\argtest.py", line 19, in foo (a1='a1a1_x', a2='a2a2_x', b1='b1b1_x', b2='b2b2_____x') TypeError: wrapper() got an unexpected keyword argument 'a1' From emile at fenx.com Thu Nov 15 19:03:27 2012 From: emile at fenx.com (Emile van Sebille) Date: Thu, 15 Nov 2012 16:03:27 -0800 Subject: Is there a simpler way to modify all arguments in a function before using the arguments? In-Reply-To: References: <18134e77-9b02-4aec-afb0-794ed900d194@googlegroups.com> Message-ID: brucegoodstein at gmail.com wrote: > Using a decorator works when named arguments are not used. When named arguments are used, unexpected keyword error is reported. Is there a simple fix? Extend def wrapper(*args) to handle *kwargs as well Emile > Code: > ----- > > from functools import wraps > > def fix_args(fn): > @wraps(fn) > def wrapper(*args): > args = (arg.replace('_', '') for arg in args) > return fn(*args) > return wrapper > > @fix_args > def foo(a1="", a2="", b1="", b2=""): > print(a1) > print(a2) > print(b1) > print(b2) > > foo ('a1a1_x', 'a2a2_x', 'b1b1_x', 'b2b2_____x') > foo (a1='a1a1_x', a2='a2a2_x', b1='b1b1_x', b2='b2b2_____x') > > Results: > -------- > a1a1x > a2a2x > b1b1x > b2b2x > Traceback (most recent call last): > File "C:\WORK\masterDB_Update\argtest.py", line 19, in > foo (a1='a1a1_x', a2='a2a2_x', b1='b1b1_x', b2='b2b2_____x') > TypeError: wrapper() got an unexpected keyword argument 'a1' From roy at panix.com Thu Nov 15 20:07:38 2012 From: roy at panix.com (Roy Smith) Date: Thu, 15 Nov 2012 20:07:38 -0500 Subject: Subprocess puzzle and two questions References: Message-ID: In article , Nobody wrote: > That's because the high-level routines aren't tied to DNS. This is true. >> gethostbyname() and getaddrinfo() use the NSS (name-service switch) > mechanism, which is configured via /etc/nsswitch.conf. Depending upon > configuration, hostnames can be looked up via a plain text file > (/etc/hosts), Berkeley DB files, DNS, NIS, NIS+, LDAP, WINS, etc. Gethostbyname() long predates NSS. For that matter, I think it even predates DNS (i.e. back to the days when /etc/hosts was the *only* way to look up a hostname). But, that's a nit. From wuwei23 at gmail.com Thu Nov 15 20:10:27 2012 From: wuwei23 at gmail.com (alex23) Date: Thu, 15 Nov 2012 17:10:27 -0800 (PST) Subject: debugging in eclipse References: <50a520df$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: <0507780b-671b-4a7d-98f7-acd5a5503a55@lg12g2000pbb.googlegroups.com> On Nov 16, 3:05?am, Steven D'Aprano wrote: > > ``1/0`` is shorter. ?;-) > > It is also guaranteed to run, unlike assert. Only if they actively pass the command line switch to turn it off, which I'd assume someone intentionally using an assertion wouldn't do. From invalid at invalid.invalid Thu Nov 15 21:55:56 2012 From: invalid at invalid.invalid (Grant Edwards) Date: Fri, 16 Nov 2012 02:55:56 +0000 (UTC) Subject: Simple Question regarding running .py program References: <22ae6963-6700-4ea7-b396-344d89ead7f2@googlegroups.com> <8085ebf2-9f08-4889-84c9-f786d6776001@googlegroups.com> <5f1bf680-ca10-446e-abbc-d3ba55327f2d@googlegroups.com> <6c3c5341-1fb4-4664-80b8-d3e08687830f@googlegroups.com> <50a42449$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2012-11-15, Mark Lawrence wrote: > On 15/11/2012 21:29, rurpy at yahoo.com wrote: > > All I'll say is that when I read something on gmane via Thunderbird on > Windows Vista on any of the 25 Python mailing lists that I subscribe to, > I don't want to read the double spaced crap that comes from G$. Doesn't Thunderbird have a scoring, blocking, or blacklisting facility to allow you to hide/ignore those posts? I have slrn hide all postings with headers that contain a line that matches this (case-insensitve) RE: Message-ID: .*googlegroups.com > I hence perceive a problem. Indeed. > 1) G$ are too interested in making huge profits and so have no interest > in people who have to read the garbage that originates from them. While I generally find Google to be mostly non-evil[1] (at least when compared to most other behmouth companies), their attitude regarding Google Groups is notably awful. > 2) People who are too bone idle to get hold of any other mechanism that > doesn't put the double spaced garbage in. Or it could be they're too ignorant to know there's a problem. Trying to explain the problem and the available options is, IME, pointless. Even if you can drag them along to the point where they understand there's a problem and they can do something about it, as long as people read and respond to their posts, they've got no motivation to do so. > Any and all answers to this dilemma are welcome. I just gave up and now ignore posts from Google Groups. I've decided there's no point it trying to change either Google Groups itself or the people who use it. I occasionally see most/all of posts from GG when they get quoted in followups, and never have I had occasion to wish I hadn't missed a GG posting. [1] OK, so I'm am annoyed with them after my Google phone updated to Android 4.2 this afternoon and the lock-screen clock is now _physically_painful_ to look at. However, I'm convinced that's not evil -- just a complete and utter lack of visual design ability. -- Grant From ethan at stoneleaf.us Thu Nov 15 23:00:25 2012 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 15 Nov 2012 20:00:25 -0800 Subject: Is there a simpler way to modify all arguments in a function before using the arguments? In-Reply-To: References: <18134e77-9b02-4aec-afb0-794ed900d194@googlegroups.com> Message-ID: <50A5BA59.1030806@stoneleaf.us> Emile van Sebille wrote: > brucegoodstein at gmail.com wrote: > >> Using a decorator works when named arguments are not used. When named >> arguments are used, unexpected keyword error is reported. Is there a >> simple fix? > > Extend def wrapper(*args) to handle *kwargs as well > > Emile > >> Code: >> ----- >> >> from functools import wraps >> >> def fix_args(fn): >> @wraps(fn) >> def wrapper(*args): so this line ^ becomes def wrapper(*args, **kwargs): >> args = (arg.replace('_', '') for arg in args) and add a line for k, v in kwargs: kwargs[k] = v.replace('_', '') >> return fn(*args) and this line ^ becomes return fn(*args, **kwargs) >> return wrapper ~Ethan~ From rustompmody at gmail.com Thu Nov 15 23:10:39 2012 From: rustompmody at gmail.com (rusi) Date: Thu, 15 Nov 2012 20:10:39 -0800 (PST) Subject: Simple Question regarding running .py program References: <22ae6963-6700-4ea7-b396-344d89ead7f2@googlegroups.com> <8085ebf2-9f08-4889-84c9-f786d6776001@googlegroups.com> <5f1bf680-ca10-446e-abbc-d3ba55327f2d@googlegroups.com> <6c3c5341-1fb4-4664-80b8-d3e08687830f@googlegroups.com> <50a42449$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Nov 16, 2:29?am, ru... at yahoo.com wrote: > But of course, our genius doesn't keep any records > and the cases where he is wrong don't make as much > impression on his memory. ?Further, he doesn't bother > to check the headers on the non-crap posts. ?Even a > junior-high science student could see the problems > with this methodology. Reminds of the difference between pop and educated statistics: http://www.johndcook.com/blog/2008/10/20/nearly-everyone-is-above-average/ On the whole I agree with rurpy. One small addition: GG allows spam posts to be marked as spam. This feature costs a few seconds and can help everyone (if a few more GG users would use it) From rosuav at gmail.com Fri Nov 16 01:37:08 2012 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 16 Nov 2012 17:37:08 +1100 Subject: Simple Question regarding running .py program In-Reply-To: References: <22ae6963-6700-4ea7-b396-344d89ead7f2@googlegroups.com> <8085ebf2-9f08-4889-84c9-f786d6776001@googlegroups.com> <5f1bf680-ca10-446e-abbc-d3ba55327f2d@googlegroups.com> <6c3c5341-1fb4-4664-80b8-d3e08687830f@googlegroups.com> <50a42449$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Nov 16, 2012 at 3:10 PM, rusi wrote: > One small addition: GG allows spam posts to be marked as spam. > > This feature costs a few seconds and can help everyone (if a few more > GG users would use it) And Gmail lets you do the exact same thing, but I almost never need to do it, because the filter is already pretty good. Though every once in a while I go check the spambox and there's usually something that technically shouldn't have been called spam (but even then, I've never regretted missing it). And I'm sure you can get it with a newsreader too, though I've not looked. Spam filtering is nothing unique. ChrisA From rosuav at gmail.com Fri Nov 16 01:38:39 2012 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 16 Nov 2012 17:38:39 +1100 Subject: Simple Question regarding running .py program In-Reply-To: References: <22ae6963-6700-4ea7-b396-344d89ead7f2@googlegroups.com> <8085ebf2-9f08-4889-84c9-f786d6776001@googlegroups.com> <5f1bf680-ca10-446e-abbc-d3ba55327f2d@googlegroups.com> <6c3c5341-1fb4-4664-80b8-d3e08687830f@googlegroups.com> <50a42449$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Nov 16, 2012 at 5:37 PM, Chris Angelico wrote: > Spam filtering is nothing unique. > (Don't get me wrong though, that doesn't stop it from being a good thing. It's just not a reason to use GG above all else.) ChrisA From krishna.k.kishor3 at gmail.com Fri Nov 16 02:28:21 2012 From: krishna.k.kishor3 at gmail.com (krishna.k.kishor3 at gmail.com) Date: Thu, 15 Nov 2012 23:28:21 -0800 (PST) Subject: Understanding '?' in regular expressions Message-ID: <07510777-b5c9-4639-811f-ee2f6825e2c3@googlegroups.com> Can someone explain the below behavior please? >>> re1 = re.compile(r'(?:((?:1000|1010|1020))[ ]*?[\,]?[ ]*?){1,3}') >>> re.findall(re_obj,'1000,1020,1000') ['1000'] >>> re.findall(re_obj,'1000,1020, 1000') ['1020', '1000'] However when I use "[\,]??" instead of "[\,]?" as below, I see a different result >>> re2 = re.compile(r'(?:((?:1000|1010|1020))[ ]*?[\,]??[ ]*?){1,3}') >>> re.findall(re_obj,'1000,1020,1000') ['1000', '1020', '1000'] I am not able to understand what's causing the difference of behavior here, I am assuming it's not 'greediness' if "?" Thank you, Kishor From andriy.kornatskyy at live.com Fri Nov 16 02:49:07 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Fri, 16 Nov 2012 10:49:07 +0300 Subject: Lazy Attribute In-Reply-To: References: , Message-ID: Ian, Thank you for the comments. > The name "attribute" is not very descriptive. Why not "lazy_attribute" instead? It just shorter and still descriptive. > If accessing the descriptor on the class object has no special > meaning, then the custom is to return the descriptor object itself, as > properties do. The lazy attribute, as a pattern, is designed to calculate something on demand, that being said means some `dynamic` nature must present, thus a class instance - object is a good candidate, while class itself is considered relatively `immutable`... of cause there might be extreme cases. > If accessing the descriptor on the class object has no special > meaning, then the custom is to return the descriptor object itself, as > properties do. If I would satisfy this, I will be forced to check for None 99.9% of the use cases (it is not None, being applied to an object). Thus it behaves as designed. Thanks. Andriy Kornatskyy ---------------------------------------- > From: ian.g.kelly at gmail.com > Date: Thu, 15 Nov 2012 15:24:40 -0700 > Subject: Re: Lazy Attribute > To: python-list at python.org > > On Thu, Nov 15, 2012 at 12:33 PM, Andriy Kornatskyy > wrote: > > > > A lazy attribute is an attribute that is calculated on demand and only once. > > > > The post below shows how you can use lazy attribute in your Python class: > > > > http://mindref.blogspot.com/2012/11/python-lazy-attribute.html > > > > Comments or suggestions are welcome. > > The name "attribute" is not very descriptive. Why not "lazy_attribute" instead? > > I note that trying to access the descriptor on the class object > results in an error: > > >>> from descriptors import attribute > >>> class Foo: > ... @attribute > ... def forty_two(self): > ... return 6 * 9 > ... > >>> Foo().forty_two > 54 > >>> Foo.forty_two > Traceback (most recent call last): > File "", line 1, in > File ".\descriptors.py", line 33, in __get__ > setattr(obj, f.__name__, val) > AttributeError: 'NoneType' object has no attribute 'forty_two' > > If accessing the descriptor on the class object has no special > meaning, then the custom is to return the descriptor object itself, as > properties do. > > >>> class Foo: > ... @property > ... def forty_two(self): > ... return 6 * 9 > ... > >>> Foo().forty_two > 54 > >>> Foo.forty_two > > -- > http://mail.python.org/mailman/listinfo/python-list From andriy.kornatskyy at live.com Fri Nov 16 02:50:56 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Fri, 16 Nov 2012 10:50:56 +0300 Subject: Lazy Attribute In-Reply-To: References: , Message-ID: Ian, Thank you for mentioning about this research, really appreciate that. Thanks. Andriy Kornatskyy ---------------------------------------- > From: ian.g.kelly at gmail.com > Date: Thu, 15 Nov 2012 15:46:19 -0700 > Subject: Re: Lazy Attribute > To: python-list at python.org > > On Thu, Nov 15, 2012 at 12:33 PM, Andriy Kornatskyy > wrote: > > > > A lazy attribute is an attribute that is calculated on demand and only once. > > > > The post below shows how you can use lazy attribute in your Python class: > > > > http://mindref.blogspot.com/2012/11/python-lazy-attribute.html > > > > Comments or suggestions are welcome. > > I should add that I like the approach you're taking here. Usually > when I want a lazy property I just make an ordinary property of a > memoized function call: > > def memoize(func): > cache = {} > @functools.wraps(func) > def wrapper(*args, **kwargs): > kwset = frozenset(kwargs.items()) > try: > return cache[args, kwset] > except KeyError: > result = cache[args, kwset] = func(*args, **kwargs) > return result > return wrapper > > class Foo: > def __init__(self): > self.times_called = 0 > > @property > @memoize # Alternatively, use functools.lru_cache > def forty_two(self): > self.times_called += 1 > return 6 * 9 > > > >>> foo = Foo() > >>> foo.times_called > 0 > >>> foo.forty_two > 54 > >>> foo.times_called > 1 > >>> foo.forty_two > 54 > >>> foo.times_called > 1 > > > Although you don't go into it in the blog entry, what I like about > your approach of replacing the descriptor with an attribute is that, > in addition to being faster, it makes it easy to force the object to > lazily reevaluate the attribute, just by deleting it. Using the > Person example from your blog post: > > >>> p = Person('John', 'Smith') > >>> p.display_name > 'John Smith' > >>> p.display_name > 'John Smith' > >>> p.calls_count > 1 > >>> p.first_name = 'Eliza' > >>> del p.display_name > >>> p.display_name > 'Eliza Smith' > >>> p.calls_count > 2 > > Although in general it's probably better to use some form of reactive > programming for that. > -- > http://mail.python.org/mailman/listinfo/python-list From sscc at mweb.co.za Fri Nov 16 02:56:41 2012 From: sscc at mweb.co.za (Alex Strickland) Date: Fri, 16 Nov 2012 09:56:41 +0200 Subject: Lazy Attribute In-Reply-To: References: , Message-ID: <50A5F1B9.2070302@mweb.co.za> On 2012/11/16 09:49 AM, Andriy Kornatskyy wrote: >> The name "attribute" is not very descriptive. Why not "lazy_attribute" instead? > > It just shorter and still descriptive. Shorter, but not descriptive. -- Regards Alex From jpiitula at ling.helsinki.fi Fri Nov 16 03:05:32 2012 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 16 Nov 2012 10:05:32 +0200 Subject: Understanding '?' in regular expressions References: <07510777-b5c9-4639-811f-ee2f6825e2c3@googlegroups.com> Message-ID: krishna.k.kishor3 at gmail.com writes: > Can someone explain the below behavior please? > > >>> re1 = re.compile(r'(?:((?:1000|1010|1020))[ ]*?[\,]?[ ]*?){1,3}') > >>> re.findall(re_obj,'1000,1020,1000') > ['1000'] > >>> re.findall(re_obj,'1000,1020, 1000') > ['1020', '1000'] > > However when I use "[\,]??" instead of "[\,]?" as below, I see a > different result > >>> re2 = re.compile(r'(?:((?:1000|1010|1020))[ ]*?[\,]??[ ]*?){1,3}') > >>> re.findall(re_obj,'1000,1020,1000') > ['1000', '1020', '1000'] Those re_obj should be re1 and re2, respectively. With that correction, the behaviour appears to be as you say. > I am not able to understand what's causing the difference of > behavior here, I am assuming it's not 'greediness' if "?" But the greed seems to be the only the difference. I can't wrap my mind around this (at the moment at least) and I need to rush away, but may I suggest the removal of all that is not relevant to the problem at hand. Study these instead: >>> re.findall(r'(10.0,?){1,3}', '1000,1020,1000') ['1000'] >>> re.findall(r'(10.0,??){1,3}', '1000,1020,1000') ['1000', '1020', '1000'] From ian.g.kelly at gmail.com Fri Nov 16 03:15:24 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 16 Nov 2012 01:15:24 -0700 Subject: Understanding '?' in regular expressions In-Reply-To: <07510777-b5c9-4639-811f-ee2f6825e2c3@googlegroups.com> References: <07510777-b5c9-4639-811f-ee2f6825e2c3@googlegroups.com> Message-ID: On Fri, Nov 16, 2012 at 12:28 AM, wrote: > Can someone explain the below behavior please? > >>>> re1 = re.compile(r'(?:((?:1000|1010|1020))[ ]*?[\,]?[ ]*?){1,3}') >>>> re.findall(re_obj,'1000,1020,1000') > ['1000'] >>>> re.findall(re_obj,'1000,1020, 1000') > ['1020', '1000'] Try removing the grouping parentheses to see the full strings being matched: >>> re1 = re.compile(r'(?:(?:1000|1010|1020)[ ]*?[\,]?[ ]*?){1,3}') >>> re.findall(re1,'1000,1020,1000') ['1000,1020,1000'] >>> re.findall(re1,'1000,1020, 1000') ['1000,1020,', '1000'] In the first case, the regular expression is matching the full string. It could also match shorter expressions, but as only the space quantifiers are non-greedy and there are no spaces to match anyway, it does not. With the grouping parentheses in place, only the *last* value of the group is returned, which is why you only see the last '1000' instead of all three strings in the group, even though the group is actually matching three different substrings. In the second case, the regular expression finds first the '1000,1020' and then the '1000' as two separate matches. The reason for this is the space. Since the quantifier on the space is non-greedy, it first tries *not* matching the space, finds that it has a valid match, and so does not backtrack. The '1000' is then identified as a separate match. As before, with the grouping parentheses in place you see only the '1020' and the last '1000' because the group only reports the last substring it matched for that particular match. > However when I use "[\,]??" instead of "[\,]?" as below, I see a different result >>>> re2 = re.compile(r'(?:((?:1000|1010|1020))[ ]*?[\,]??[ ]*?){1,3}') >>>> re.findall(re_obj,'1000,1020,1000') > ['1000', '1020', '1000'] > > I am not able to understand what's causing the difference of behavior here, I am assuming it's not 'greediness' if "?" The difference is the non-greediness of the comma quantifier. When it comes time for it to match the comma, because the quantifier is non-greedy, it first tries *not* matching the comma, whereas before it first tried to match it. As with the space above, when the comma is not matched, it finds that it has a valid match anyway if it just stops matching immediately. So it does not need to backtrack, and in this case it ends up terminating each match early upon the comma and returning all three numbers as separate matches. What exactly is it that you're trying to do with this regular expression? I suspect that it the solution actually a lot simpler than you're making it. From steve+comp.lang.python at pearwood.info Fri Nov 16 04:04:39 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 16 Nov 2012 09:04:39 GMT Subject: Lazy Attribute References: Message-ID: <50a601a7$0$29978$c3e8da3$5496439d@news.astraweb.com> On Fri, 16 Nov 2012 10:49:07 +0300, Andriy Kornatskyy wrote: > Ian, > > Thank you for the comments. > >> The name "attribute" is not very descriptive. Why not "lazy_attribute" >> instead? > > It just shorter and still descriptive. It is not descriptive. EVERYTHING accessed used dot notation obj.thing is an attribute. What makes your "attribute" different from ordinary attributes, properties, dynamic attributes calculated with __getattr__, methods, etc? -- Steven From rouslank at msn.com Fri Nov 16 04:32:08 2012 From: rouslank at msn.com (Rouslan Korneychuk) Date: Fri, 16 Nov 2012 04:32:08 -0500 Subject: Lazy Attribute In-Reply-To: References: , Message-ID: On 11/16/2012 02:49 AM, Andriy Kornatskyy wrote: >> If accessing the descriptor on the class object has no special >> meaning, then the custom is to return the descriptor object itself, as >> properties do. > > If I would satisfy this, I will be forced to check for None 99.9% of the use cases (it is not None, being applied to an object). Thus it behaves as designed. That's not true. You can use a try-except block to return the descriptor object when an AttributeError is raised. From rouslank at msn.com Fri Nov 16 05:12:11 2012 From: rouslank at msn.com (Rouslan Korneychuk) Date: Fri, 16 Nov 2012 05:12:11 -0500 Subject: Lazy Attribute In-Reply-To: References: , Message-ID: <%jops.44$TT7.13@newsfe04.iad> On 11/16/2012 04:32 AM, Rouslan Korneychuk wrote: > On 11/16/2012 02:49 AM, Andriy Kornatskyy wrote: >>> If accessing the descriptor on the class object has no special >>> meaning, then the custom is to return the descriptor object itself, as >>> properties do. >> >> If I would satisfy this, I will be forced to check for None 99.9% of >> the use cases (it is not None, being applied to an object). Thus it >> behaves as designed. > > That's not true. You can use a try-except block to return the descriptor > object when an AttributeError is raised. Actually, never mind. I just realized the function has to be called before the attribute can be set, which can not-only raise any exception, but could potentially have undesired side-effects given a parameter it doesn't expect. From andriy.kornatskyy at live.com Fri Nov 16 05:26:01 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Fri, 16 Nov 2012 13:26:01 +0300 Subject: Lazy Attribute In-Reply-To: <50A5F1B9.2070302@mweb.co.za> References: , , , , <50A5F1B9.2070302@mweb.co.za> Message-ID: from wheezy.core.descriptors import attribute as lazy @lazy def display_name... Thanks. Andriy Kornatskyy ---------------------------------------- > Date: Fri, 16 Nov 2012 09:56:41 +0200 > From: sscc at mweb.co.za > To: python-list at python.org > Subject: Re: Lazy Attribute > > On 2012/11/16 09:49 AM, Andriy Kornatskyy wrote: > > >> The name "attribute" is not very descriptive. Why not "lazy_attribute" instead? > > > > It just shorter and still descriptive. > > Shorter, but not descriptive. > > -- > Regards > Alex > -- > http://mail.python.org/mailman/listinfo/python-list From andriy.kornatskyy at live.com Fri Nov 16 05:27:06 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Fri, 16 Nov 2012 13:27:06 +0300 Subject: Lazy Attribute In-Reply-To: <50a601a7$0$29978$c3e8da3$5496439d@news.astraweb.com> References: , , , <50a601a7$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: Same applies to properties... they are seen as an object attributes. Thanks. Andriy ---------------------------------------- > From: steve+comp.lang.python at pearwood.info > Subject: Re: Lazy Attribute > Date: Fri, 16 Nov 2012 09:04:39 +0000 > To: python-list at python.org > > On Fri, 16 Nov 2012 10:49:07 +0300, Andriy Kornatskyy wrote: > > > Ian, > > > > Thank you for the comments. > > > >> The name "attribute" is not very descriptive. Why not "lazy_attribute" > >> instead? > > > > It just shorter and still descriptive. > > It is not descriptive. EVERYTHING accessed used dot notation obj.thing is > an attribute. What makes your "attribute" different from ordinary > attributes, properties, dynamic attributes calculated with __getattr__, > methods, etc? > > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list From steve+comp.lang.python at pearwood.info Fri Nov 16 05:29:03 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 16 Nov 2012 10:29:03 GMT Subject: Lazy Attribute References: Message-ID: <50a6156e$0$29978$c3e8da3$5496439d@news.astraweb.com> On Thu, 15 Nov 2012 15:46:19 -0700, Ian Kelly wrote: > Although you don't go into it in the blog entry, what I like about your > approach of replacing the descriptor with an attribute is that, in > addition to being faster, it makes it easy to force the object to lazily > reevaluate the attribute, just by deleting it. You just lost me right there. That's a poor UI design -- it violates the principle of least surprise. If I delete something, it should be deleted. Consider your example: >>>> del p.display_name >>>> p.display_name > 'Eliza Smith' That's very surprising. I am not aware of any other name in Python where deleting it does not remove the name from the namespace. (It is possible with properties, but I haven't ever come across someone who does that.) I don't have a good solution for invaliding such lazy attributes. Ideally we could have a new statement: refresh obj.attr # or some other name like "invalidate" but that won't happen. Other alternatives like: obj.attr.refresh() refresh(obj.attr) can't work because the function will see the result of the attribute lookup, not the lazy attribute itself. This won't do: obj.__class__.attr.refresh() because it won't know which instance to invalidate, although this could work: obj.__class__.attr.refresh(obj) # but it's ugly I'm very vaguely leaning towards this as the least-worst solution to invalidating the cached value: refresh(obj, 'attr') # pass the instance and the name -- Steven From steve+comp.lang.python at pearwood.info Fri Nov 16 05:30:24 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 16 Nov 2012 10:30:24 GMT Subject: debugging in eclipse References: <50a520df$0$29978$c3e8da3$5496439d@news.astraweb.com> <0507780b-671b-4a7d-98f7-acd5a5503a55@lg12g2000pbb.googlegroups.com> Message-ID: <50a615c0$0$29978$c3e8da3$5496439d@news.astraweb.com> On Thu, 15 Nov 2012 17:10:27 -0800, alex23 wrote: > On Nov 16, 3:05?am, Steven D'Aprano +comp.lang.pyt... at pearwood.info> wrote: >> > ``1/0`` is shorter. ?;-) >> >> It is also guaranteed to run, unlike assert. > > Only if they actively pass the command line switch to turn it off, Not necessarily "actively". On Linux you can set up command aliases, e.g. `alias python=python -O` and I dare say there is some equivalent under Windows. Once you have done so (which could be many months in the past, and forgotten about) you no longer need to actively specify -O to run with optimization on. > which > I'd assume someone intentionally using an assertion wouldn't do. Provided that they know the side-effect of -O, and that the code contains an assertion. Not all code is executed by the same person who wrote it, and not all people remember every fine detail about every script they wrote. I couldn't possibly tell you what all my scripts do without checking the source code. -- Steven From andriy.kornatskyy at live.com Fri Nov 16 05:31:58 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Fri, 16 Nov 2012 13:31:58 +0300 Subject: Lazy Attribute In-Reply-To: <%jops.44$TT7.13@newsfe04.iad> References: , , , , , <%jops.44$TT7.13@newsfe04.iad> Message-ID: This is very minor use case. Unlikely useful to add any checks for None, or translate one exception to the other... with pretty much the same outcome: it makes sense in objects only. Thanks. Andriy ---------------------------------------- > From: rouslank at msn.com > Subject: Re: Lazy Attribute > Date: Fri, 16 Nov 2012 05:12:11 -0500 > To: python-list at python.org > > On 11/16/2012 04:32 AM, Rouslan Korneychuk wrote: > > On 11/16/2012 02:49 AM, Andriy Kornatskyy wrote: > >>> If accessing the descriptor on the class object has no special > >>> meaning, then the custom is to return the descriptor object itself, as > >>> properties do. > >> > >> If I would satisfy this, I will be forced to check for None 99.9% of > >> the use cases (it is not None, being applied to an object). Thus it > >> behaves as designed. > > > > That's not true. You can use a try-except block to return the descriptor > > object when an AttributeError is raised. > > Actually, never mind. I just realized the function has to be called > before the attribute can be set, which can not-only raise any exception, > but could potentially have undesired side-effects given a parameter it > doesn't expect. > -- > http://mail.python.org/mailman/listinfo/python-list From stefan at epy.co.at Fri Nov 16 05:41:50 2012 From: stefan at epy.co.at (Stefan H. Holek) Date: Fri, 16 Nov 2012 11:41:50 +0100 Subject: Lazy Attribute In-Reply-To: References: Message-ID: <9CA84FCB-5292-4191-B4DE-6926DD0FFCB4@epy.co.at> On 15.11.2012, at 20:33, Andriy Kornatskyy wrote: > A lazy attribute is an attribute that is calculated on demand and only once. > > The post below shows how you can use lazy attribute in your Python class: > > http://mindref.blogspot.com/2012/11/python-lazy-attribute.html > > Comments or suggestions are welcome. There is a ready made and well tested lazy decorator at http://pypi.python.org/pypi/lazy. I even has a better name. ;-) Since people seem to come up with their own implementations on a bi-weekly basis, I am seriously wondering how to improve its visibility. Putting it on PyPI alone does not cut it, apparently. Stefan -- Stefan H. Holek stefan at epy.co.at From stefan at epy.co.at Fri Nov 16 05:45:32 2012 From: stefan at epy.co.at (Stefan H. Holek) Date: Fri, 16 Nov 2012 11:45:32 +0100 Subject: Lazy Attribute In-Reply-To: <50a6156e$0$29978$c3e8da3$5496439d@news.astraweb.com> References: <50a6156e$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 16.11.2012, at 11:29, Steven D'Aprano wrote: > I'm very vaguely leaning towards this as the least-worst solution to > invalidating the cached value: > > refresh(obj, 'attr') # pass the instance and the name This it exactly how lazy handles invalidation. http://lazy.readthedocs.org/en/latest/ Stefan -- Stefan H. Holek stefan at epy.co.at From andriy.kornatskyy at live.com Fri Nov 16 05:46:43 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Fri, 16 Nov 2012 13:46:43 +0300 Subject: Lazy Attribute In-Reply-To: <50a6156e$0$29978$c3e8da3$5496439d@news.astraweb.com> References: , , <50a6156e$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: I believe it is not valid relate a lazy attribute as something `cached` since it cause confusion (e.g. delete of attribute cause cached item to be re-evaluated...), `cached` and `lazy` have completely different semantic meaning... however might overlap, as we see. Andriy ---------------------------------------- > From: steve+comp.lang.python at pearwood.info > Subject: Re: Lazy Attribute > Date: Fri, 16 Nov 2012 10:29:03 +0000 > To: python-list at python.org > > On Thu, 15 Nov 2012 15:46:19 -0700, Ian Kelly wrote: > > > Although you don't go into it in the blog entry, what I like about your > > approach of replacing the descriptor with an attribute is that, in > > addition to being faster, it makes it easy to force the object to lazily > > reevaluate the attribute, just by deleting it. > > You just lost me right there. That's a poor UI design -- it violates the > principle of least surprise. If I delete something, it should be deleted. > Consider your example: > > >>>> del p.display_name > >>>> p.display_name > > 'Eliza Smith' > > That's very surprising. I am not aware of any other name in Python where > deleting it does not remove the name from the namespace. (It is possible > with properties, but I haven't ever come across someone who does that.) > > I don't have a good solution for invaliding such lazy attributes. Ideally > we could have a new statement: > > refresh obj.attr # or some other name like "invalidate" > > but that won't happen. Other alternatives like: > > obj.attr.refresh() > refresh(obj.attr) > > can't work because the function will see the result of the attribute > lookup, not the lazy attribute itself. This won't do: > > obj.__class__.attr.refresh() > > because it won't know which instance to invalidate, although this could > work: > > obj.__class__.attr.refresh(obj) # but it's ugly > > I'm very vaguely leaning towards this as the least-worst solution to > invalidating the cached value: > > refresh(obj, 'attr') # pass the instance and the name > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list From "chip9munk[SSSpAm" at gmail.com Fri Nov 16 07:06:07 2012 From: "chip9munk[SSSpAm" at gmail.com (chip9munk) Date: Fri, 16 Nov 2012 13:06:07 +0100 Subject: editing conf file Message-ID: Hi all! I would like to use conf file to get all the variables in my code. And it works great. I use the following (simple example): execfile("example.conf", config) print config["value1"] and it works like a charm. Now the problem is I do not know how to edit the conf file... let us say that I calculate something and would like to save it in the conf file so that it is set for the next run of some program. How do I edit a specific value in the conf file? (I would like to avoid editing txt if possible)... I should also mention that I use Python 3.. so some of the solutions I came across are not compatible... Thanks!! From "chip9munk[SSSpAm" at gmail.com Fri Nov 16 07:15:07 2012 From: "chip9munk[SSSpAm" at gmail.com (chip9munk) Date: Fri, 16 Nov 2012 13:15:07 +0100 Subject: editing conf file In-Reply-To: References: Message-ID: ok, I've got it: http://docs.python.org/3.1/library/configparser.html works like a charm! Sorry for the unnecessary question. :/ From rustompmody at gmail.com Fri Nov 16 07:35:33 2012 From: rustompmody at gmail.com (rusi) Date: Fri, 16 Nov 2012 04:35:33 -0800 (PST) Subject: editing conf file References: Message-ID: <996a6d6c-fdb2-40a0-bd5c-ada2ba35af78@i2g2000pbi.googlegroups.com> On Nov 16, 5:15?pm, chip9munk <"chip9munk[SSSpAm"@gmail.com> wrote: > ok, I've got it:http://docs.python.org/3.1/library/configparser.html > > works like a charm! > > Sorry for the unnecessary question. :/ Not an issue. And there may be better options (allows nested sections) http://www.voidspace.org.uk/python/configobj.html A more broadbased comparison http://wiki.python.org/moin/ConfigParserShootout From stefan at epy.co.at Fri Nov 16 07:46:01 2012 From: stefan at epy.co.at (Stefan H. Holek) Date: Fri, 16 Nov 2012 13:46:01 +0100 Subject: Lazy Attribute In-Reply-To: References: , , <50a6156e$0$29978$c3e8da3$5496439d@news.astraweb.com>, Message-ID: <8FF664DC-AB7E-4A53-855D-9A7477F36FD6@epy.co.at> On 16.11.2012, at 11:54, Andriy Kornatskyy wrote: >> Subject: Re: Lazy Attribute >> From: stefan at epy.co.at >> Date: Fri, 16 Nov 2012 11:45:32 +0100 >> To: python-list at python.org >> >> On 16.11.2012, at 11:29, Steven D'Aprano wrote: >> >>> I'm very vaguely leaning towards this as the least-worst solution to >>> invalidating the cached value: >>> >>> refresh(obj, 'attr') # pass the instance and the name >> >> This it exactly how lazy handles invalidation. http://lazy.readthedocs.org/en/latest/ > > @property is a solution to evaluate something that is dynamic. @attribute is good for immutable objects. Even if we assume refresh is a good idea... how I would know when it is valid to `refresh`? What is criteria? > > Andriy I had to implement invalidation anyway in order to write tests. I decided to expose the mechanism to keep users from having to invent their own SHOULD the need arise. I was not advocating invalidation in any way with my reply. All I wanted was to confirm the "least bad" solution. ;-) Stefan -- Stefan H. Holek stefan at epy.co.at From "chip9munk[SSSpAm" at gmail.com Fri Nov 16 07:48:49 2012 From: "chip9munk[SSSpAm" at gmail.com (chip9munk) Date: Fri, 16 Nov 2012 13:48:49 +0100 Subject: editing conf file In-Reply-To: <996a6d6c-fdb2-40a0-bd5c-ada2ba35af78@i2g2000pbi.googlegroups.com> References: <996a6d6c-fdb2-40a0-bd5c-ada2ba35af78@i2g2000pbi.googlegroups.com> Message-ID: On 11/16/2012 1:35 PM, rusi wrote: > And there may be better options (allows nested sections) > http://www.voidspace.org.uk/python/configobj.html > but it does not seem to work with python 3 I have an issue... configparser has four functions: get, getboolean, getfloat and getint. how do I get list from cfg file?! any ideas? From ulrich.eckhardt at dominolaser.com Fri Nov 16 08:02:29 2012 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Fri, 16 Nov 2012 14:02:29 +0100 Subject: editing conf file In-Reply-To: References: Message-ID: <54qhn9-u85.ln1@satorlaser.homedns.org> Am 16.11.2012 13:06, schrieb chip9munk: > I would like to use conf file to get all the variables in my code. And > it works great. I use the following (simple example): > > execfile("example.conf", config) > print config["value1"] > > and it works like a charm. This works, but in general importing configuration data by loading and executing code is a questionable approach. The problem is in particular that the code parser is always more strict with the syntax than a configuration file should be. Also, it presents the danger of code injection, especially when exec'ing or importing untrusted code. That said, if you really want full programmability inside that configuration and are aware of the implications, you can do that. In that case, I would rather call this a Python module though and instead "from settings.py import *" to import any setting from this module (this is similar to exec(), but less hack-ish). I use something similar to import settings for automated tests, but still wouldn't recommend it for general use. If you don't want that, use a configuration file parser instead. Python comes with one, see the section "13.2 Configuration file parser" at http://docs.python.org/2/library/, which can both read and write simple configuration files. > I should also mention that I use Python 3.. so some of the solutions I > came across are not compatible... No you don't, Above code clearly uses a print statement instead of a print function. :P Anyhow, concerning the link above, replace the 2 with a 3 and skip to section 14.2. Uli From thbach at students.uni-mainz.de Fri Nov 16 08:04:12 2012 From: thbach at students.uni-mainz.de (Thomas Bach) Date: Fri, 16 Nov 2012 14:04:12 +0100 Subject: editing conf file In-Reply-To: References: <996a6d6c-fdb2-40a0-bd5c-ada2ba35af78@i2g2000pbi.googlegroups.com> Message-ID: <20121116130411.GA4246@taris.box> On Fri, Nov 16, 2012 at 01:48:49PM +0100, chip9munk wrote: > configparser has four functions: get, getboolean, getfloat and getint. > > how do I get list from cfg file?! AFAIK you have to parse the list yourself. Something like my_list = [ s.strip() for s in cp.get('section', 'option').split(',') ] if you use a comma as a separator. Have a look at YAML if this is not enough for you, as I think lists are supported there. Haven't had a look myself though, yet. Regards, Thomas Bach. From "chip9munk[SSSpAm" at gmail.com Fri Nov 16 08:27:27 2012 From: "chip9munk[SSSpAm" at gmail.com (chip9munk) Date: Fri, 16 Nov 2012 14:27:27 +0100 Subject: editing conf file In-Reply-To: <54qhn9-u85.ln1@satorlaser.homedns.org> References: <54qhn9-u85.ln1@satorlaser.homedns.org> Message-ID: On 11/16/2012 2:02 PM, Ulrich Eckhardt wrote: > Am 16.11.2012 13:06, schrieb chip9munk: >> I would like to use conf file to get all the variables in my code. And >> it works great. I use the following (simple example): >> >> execfile("example.conf", config) >> print config["value1"] >> >> and it works like a charm. > > This works, but in general importing configuration data by loading and > executing code is a questionable approach. The problem is in particular > that the code parser is always more strict with the syntax than a > configuration file should be. Also, it presents the danger of code > injection, especially when exec'ing or importing untrusted code. huh... ok, the thing is that there will actually be no code in the config file, just some variables and values.. it will be more like a "setting file"... so no execution of the config file is necessary, just getting and setting variables... > That said, if you really want full programmability inside that > configuration and are aware of the implications, you can do that. In > that case, I would rather call this a Python module though and instead > "from settings.py import *" to import any setting from this module (this > is similar to exec(), but less hack-ish). I use something similar to > import settings for automated tests, but still wouldn't recommend it for > general use. thank you for the tip! > If you don't want that, use a configuration file parser instead. Python > comes with one, see the section "13.2 Configuration file parser" at > http://docs.python.org/2/library/, which can both read and write simple > configuration files. yes I will use it >> I should also mention that I use Python 3.. so some of the solutions I >> came across are not compatible... > > No you don't, Above code clearly uses a print statement instead of a > print function. :P Yes i do :) i just did not c/p code from my script but from some webpage :)) i have print (config["value1"]) > Anyhow, concerning the link above, replace the 2 with > a 3 and skip to section 14.2. thank you i am using configparser... the only problem as i mention in another post is that it reads lists as string... so some additional parsing is necessary.. thanks! From "chip9munk[SSSpAm" at gmail.com Fri Nov 16 08:28:33 2012 From: "chip9munk[SSSpAm" at gmail.com (chip9munk) Date: Fri, 16 Nov 2012 14:28:33 +0100 Subject: editing conf file In-Reply-To: References: <996a6d6c-fdb2-40a0-bd5c-ada2ba35af78@i2g2000pbi.googlegroups.com> Message-ID: On 11/16/2012 2:04 PM, Thomas Bach wrote: > On Fri, Nov 16, 2012 at 01:48:49PM +0100, chip9munk wrote: >> configparser has four functions: get, getboolean, getfloat and getint. >> >> how do I get list from cfg file?! > > AFAIK you have to parse the list yourself. Something like > > my_list = [ s.strip() for s in cp.get('section', 'option').split(',') ] > > if you use a comma as a separator. yes i guess this is needed, thanks! > Have a look at YAML if this is not enough for you, as I think lists > are supported there. Haven't had a look myself though, yet. > I will check it out! Thank you! From python.list at tim.thechases.com Fri Nov 16 08:43:11 2012 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 16 Nov 2012 07:43:11 -0600 Subject: editing conf file In-Reply-To: <20121116130411.GA4246@taris.box> References: <996a6d6c-fdb2-40a0-bd5c-ada2ba35af78@i2g2000pbi.googlegroups.com> <20121116130411.GA4246@taris.box> Message-ID: <50A642EF.3040201@tim.thechases.com> On 11/16/12 07:04, Thomas Bach wrote: > On Fri, Nov 16, 2012 at 01:48:49PM +0100, chip9munk wrote: >> configparser has four functions: get, getboolean, getfloat and getint. >> >> how do I get list from cfg file?! > > AFAIK you have to parse the list yourself. Something like > > my_list = [ s.strip() for s in cp.get('section', 'option').split(',') ] > > if you use a comma as a separator. For slightly more complex option text, you can use the CSV module to do the heavy lifting, so if you have something like [section] option="one, one, one",two,3 then you can have Python give you my_list = next(csv.reader([cp.get("section", "option")])) or alternatively use the shlex module and separate them like shell options: [section] option="one, one, one" two 3 then do my_list = list(shlex.shlex(cp.get("section", "option"))) Or yet one more way using Python list syntax for literals: [section] option=["one, one, one", "two", 3] and get them with my_list = ast.literal_eval(cp.get("section", "option)) Lots of fun (and batteries-included) ways depending on how you want to represent the list in the config file and what sorts of data it can contain. -tkc From roy at panix.com Fri Nov 16 09:08:11 2012 From: roy at panix.com (Roy Smith) Date: Fri, 16 Nov 2012 09:08:11 -0500 Subject: editing conf file References: <54qhn9-u85.ln1@satorlaser.homedns.org> Message-ID: Ulrich Eckhardt wrote: > in general importing configuration data by loading and > executing code is a questionable approach. The problem is in particular > that the code parser is always more strict with the syntax than a > configuration file should be. Also, it presents the danger of code > injection, especially when exec'ing or importing untrusted code. chip9munk <"chip9munk[SSSpAm"@gmail.com> wrote: > huh... ok, the thing is that there will actually be no code in the > config file, just some variables and values.. it will be more like a > "setting file"... so no execution of the config file is necessary, just > getting and setting variables... I've been using django for the past couple of years, and I have to say I'm really addicted to their style of executable config files. The ability to put conditional logic in your settings.py file is extremely powerful. Even simple stuff like: DEBUG = songza.config['build_type'] != 'production' adds value. But, yes, Ulrich is 100% correct that it can lead to code injection attacks if you allow reading configs from untrusted sources. Like all powerful tools, it needs to be used with care. These days, if I was writing something that needed a config file and I didn't want to do "import settings" for whatever reason, I would go with YAML. It seems to give an attractive mix of: * supporting complex data structures * easy to for humans to hand-edit * easy for humans to read * safe from code injection attacks From bruceg113355 at gmail.com Fri Nov 16 10:00:37 2012 From: bruceg113355 at gmail.com (bruceg113355 at gmail.com) Date: Fri, 16 Nov 2012 07:00:37 -0800 (PST) Subject: Is there a simpler way to modify all arguments in a function before using the arguments? In-Reply-To: References: <18134e77-9b02-4aec-afb0-794ed900d194@googlegroups.com> Message-ID: <211b323b-1fd4-4018-a7e5-34c6fbda01a5@googlegroups.com> On Thursday, November 15, 2012 11:16:08 PM UTC-5, Ethan Furman wrote: > Emile van Sebille wrote: > > > > > > >> Using a decorator works when named arguments are not used. When named > > >> arguments are used, unexpected keyword error is reported. Is there a > > >> simple fix? > > > > > > Extend def wrapper(*args) to handle *kwargs as well > > > > > > Emile > > > > > >> Code: > > >> ----- > > >> > > >> from functools import wraps > > >> > > >> def fix_args(fn): > > >> @wraps(fn) > > >> def wrapper(*args): > > so this line ^ becomes > > def wrapper(*args, **kwargs): > > >> args = (arg.replace('_', '') for arg in args) > > and add a line > > for k, v in kwargs: > > kwargs[k] = v.replace('_', '') > > >> return fn(*args) > > and this line ^ becomes > > return fn(*args, **kwargs) > > >> return wrapper > > > > ~Ethan~ Ethan, I tried you code suggestions but got errors. However, this works: from functools import wraps def fix_args(fn): @wraps(fn) def wrapper(*args, **kwargs): args = (arg.replace('_', '') for arg in args) for kv in kwargs: kwargs[kv] = kwargs[kv].replace('_', '') return fn(*args, **kwargs) return wrapper @fix_args def foo(a1="", a2="", b1="", b2=""): print(a1) print(a2) print(b1) print(b2) print "" foo ('a1a1_x', 'a2a2_x', 'b1b1_x', 'b2b2_____x') foo (a1='a1a1_x', a2='a2a2_x', b1='b1b1_x', b2='b2b2_____x') foo ('a1a1_x', 'a2a2_x', b1='b1b1_x', b2='b2b2_____x') Bruce From bruceg113355 at gmail.com Fri Nov 16 10:00:37 2012 From: bruceg113355 at gmail.com (bruceg113355 at gmail.com) Date: Fri, 16 Nov 2012 07:00:37 -0800 (PST) Subject: Is there a simpler way to modify all arguments in a function before using the arguments? In-Reply-To: References: <18134e77-9b02-4aec-afb0-794ed900d194@googlegroups.com> Message-ID: <211b323b-1fd4-4018-a7e5-34c6fbda01a5@googlegroups.com> On Thursday, November 15, 2012 11:16:08 PM UTC-5, Ethan Furman wrote: > Emile van Sebille wrote: > > > > > > >> Using a decorator works when named arguments are not used. When named > > >> arguments are used, unexpected keyword error is reported. Is there a > > >> simple fix? > > > > > > Extend def wrapper(*args) to handle *kwargs as well > > > > > > Emile > > > > > >> Code: > > >> ----- > > >> > > >> from functools import wraps > > >> > > >> def fix_args(fn): > > >> @wraps(fn) > > >> def wrapper(*args): > > so this line ^ becomes > > def wrapper(*args, **kwargs): > > >> args = (arg.replace('_', '') for arg in args) > > and add a line > > for k, v in kwargs: > > kwargs[k] = v.replace('_', '') > > >> return fn(*args) > > and this line ^ becomes > > return fn(*args, **kwargs) > > >> return wrapper > > > > ~Ethan~ Ethan, I tried you code suggestions but got errors. However, this works: from functools import wraps def fix_args(fn): @wraps(fn) def wrapper(*args, **kwargs): args = (arg.replace('_', '') for arg in args) for kv in kwargs: kwargs[kv] = kwargs[kv].replace('_', '') return fn(*args, **kwargs) return wrapper @fix_args def foo(a1="", a2="", b1="", b2=""): print(a1) print(a2) print(b1) print(b2) print "" foo ('a1a1_x', 'a2a2_x', 'b1b1_x', 'b2b2_____x') foo (a1='a1a1_x', a2='a2a2_x', b1='b1b1_x', b2='b2b2_____x') foo ('a1a1_x', 'a2a2_x', b1='b1b1_x', b2='b2b2_____x') Bruce From demianbrecht at gmail.com Fri Nov 16 10:22:54 2012 From: demianbrecht at gmail.com (Demian Brecht) Date: Fri, 16 Nov 2012 07:22:54 -0800 Subject: Lazy Attribute In-Reply-To: <9CA84FCB-5292-4191-B4DE-6926DD0FFCB4@epy.co.at> References: <9CA84FCB-5292-4191-B4DE-6926DD0FFCB4@epy.co.at> Message-ID: > There is a ready made and well tested lazy decorator at http://pypi.python.org/pypi/lazy. I even has a better name. ;-) I was ignorantly unaware of this module. You've saved me a few lines of code every time I want to achieve lazy loading - thanks :) > Since people seem to come up with their own implementations on a bi-weekly basis, I am seriously wondering how to improve its visibility. Putting it on PyPI alone does not cut it, apparently. This was a good start ;) Demian Brecht @demianbrecht http://demianbrecht.github.com From rustompmody at gmail.com Fri Nov 16 11:41:38 2012 From: rustompmody at gmail.com (rusi) Date: Fri, 16 Nov 2012 08:41:38 -0800 (PST) Subject: editing conf file References: <54qhn9-u85.ln1@satorlaser.homedns.org> Message-ID: On Nov 16, 7:08?pm, Roy Smith wrote: > These days, if I was writing something that needed a config file and I > didn't want to do "import settings" for whatever reason, I would go with > YAML. ?It seems to give an attractive mix of: > > * supporting complex data structures > * easy to for humans to hand-edit > * easy for humans to read > * safe from code injection attacks +1 except for a caveat on the last: Use safe_load and safe_dump. dump and load are vulnerable to code injection attacks From rosuav at gmail.com Fri Nov 16 13:08:30 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 17 Nov 2012 05:08:30 +1100 Subject: Python questions help In-Reply-To: <20121116100054.54257de92fb4ed6e65a28126@lavabit.com> References: <20121116100054.54257de92fb4ed6e65a28126@lavabit.com> Message-ID: On Sat, Nov 17, 2012 at 5:00 AM, rh wrote: > "How many people think programming skills are inherent?" > i.e. that some people are just born with the gift to be good programmers > Result: very few hands raised maybe a couple (possibly non-progammers??) Maybe, but there's definitely something that happens close to birth. If your parents give you the name Chris, you're more likely to become a geek and a programmer. ChrisA From eric.frederich at gmail.com Fri Nov 16 14:37:26 2012 From: eric.frederich at gmail.com (Eric Frederich) Date: Fri, 16 Nov 2012 14:37:26 -0500 Subject: error importing smtplib In-Reply-To: References: Message-ID: So I inspected the process through /proc//maps That seemed to show what libraries had been loaded (though there is probably an easier way to do this). In any case, I found that if I import smtplib before logging in I see these get loaded... /opt/foo/python27/lib/python2.7/lib-dynload/_ssl.so /lib64/libssl.so.0.9.8e Then after logging in, I see this other .so get loaded... /opt/bar/lib64/libssl.so.0.9.7 So that is what happens when when things are well and I don't get any error messages. However, when I do the log in first I see the /opt/bar .so file loaded first /opt/bar/lib64/libssl.so.0.9.7 Then after importing smtplib I see the other two show up... /opt/foo/python27/lib/python2.7/lib-dynload/_ssl.so /lib64/libssl.so.0.9.8e So.... I'm guessing the problem is that after I log in, the process has a conflicting libssl.so file loaded. Then when I try to import smtplib it tries getting things from there and that is where the errors are coming from. The question now is how do I fix this? On Thu, Nov 15, 2012 at 4:37 PM, Terry Reedy wrote: > On 11/15/2012 1:48 PM, Eric Frederich wrote: > >> Thanks for the idea. >> sys.path was the same before and after the login >> > > Too bad. That seems to be a typical cause of import failure. > > > What else should I be checking? >> > > No idea. You are working beyond my knowledge. But I might either look at > the foo-login code carefully, or disable (comment out) parts of it to see > what makes the import fail. > > > -- > Terry Jan Reedy > > -- > http://mail.python.org/**mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From dieter at handshake.de Fri Nov 16 14:38:13 2012 From: dieter at handshake.de (Dieter Maurer) Date: Fri, 16 Nov 2012 20:38:13 +0100 Subject: error importing smtplib References: Message-ID: <87ip9572ze.fsf@handshake.de> Eric Frederich writes: > I created some bindings to a 3rd party library. > I have found that when I run Python and import smtplib it works fine. > If I first log into the 3rd party application using my bindings however I > get a bunch of errors. > > What do you think this 3rd party login could be doing that would affect the > ability to import smtp lib. > > Any suggestions for debugging this further. I am lost. > > This works... > > import smtplib > FOO_login() > > This doesn't... > > FOO_login() > import smtplib > > Errors..... > >>>> import smtplib > ERROR:root:code for hash sha224 was not found. > Traceback (most recent call last): > File "/opt/foo/python27/lib/python2.7/hashlib.py", line 139, in > globals()[__func_name] = __get_hash(__func_name) > File "/opt/foo/python27/lib/python2.7/hashlib.py", line 103, in > __get_openssl_constructor > return __get_builtin_constructor(name) > File "/opt/foo/python27/lib/python2.7/hashlib.py", line 91, in > __get_builtin_constructor > raise ValueError('unsupported hash type %s' % name) > ValueError: unsupported hash type sha224 >From the error, I suppose it does something bad for hash registries. When I have analysed problems with "hashlib" (some time ago, my memory may not be completely trustworthy), I got the impression that "hashlib" essentially delegates to the "openssl" libraries for the real work and especially the supported hash types. Thus, I suspect that your "FOO_login()" does something which confuses "openssl". One potential reason could be that it loads a bad version of an "openssl" shared library. I would use the "trace" (shell) command to find out what operating system calls are executed during "FOO_login()", hoping that one of them give me a clue. From subhabangalore at gmail.com Fri Nov 16 15:39:31 2012 From: subhabangalore at gmail.com (subhabangalore at gmail.com) Date: Fri, 16 Nov 2012 12:39:31 -0800 (PST) Subject: Understanding Code In-Reply-To: References: <45923b5c-f0bd-4367-84f9-ba08a260da69@googlegroups.com> Message-ID: <2e9710fe-8dcc-4c42-b535-bfadd374b938@googlegroups.com> On Tuesday, November 13, 2012 4:12:52 PM UTC+5:30, Peter Otten wrote: > subhabangalore at gmail.com wrote: > > > > > Dear Group, > > > To improve my code writing I am trying to read good codes. Now, I have > > > received a code,as given below,(apology for slight indentation errors) the > > > code is running well. Now to comprehend the code, I am looking to > > > understand it completely. > > > > > > class Calculate: > > > def __init__(self): > > > self.prior = {} > > > self.total = {} > > > self.count = 0 > > > def add(self, cls, obs): > > > self.prior[cls] = self.prior.get(cls, 0) + 1 > > > for idx, val in enumerate(obs): > > > key = cls, idx, val > > > self.total[key] = self.total.get(key, 0) + 1 > > > self.count += 1 > > > def discr(self, cls, obs): > > > result = self.prior[cls]/self.count > > > for idx, val in enumerate(obs): > > > freq = self.total.get((cls, idx, val), 0) > > > result *= freq/self.prior[cls] > > > return result > > > def classify(self, obs): > > > candidates = [(self.discr(c, obs), c) for c in self.prior] > > > return max(candidates)[1] > > > > > > I am not understanding many parts of it, I am understanding many parts of > > > it also. > > > > > > So I am looking for an exercise what are the things I should know to > > > understand it, (please do not give answers I would get back with the > > > answers in a week and would discuss even how to write better than this). > > > > Start with running the code for the simplest piece of the class: > > >>> c = Calculate() > > >>> c.add("x", [1,2,3]) > > > > Then inspect the attributes: > > > > >>> c.prior > > {'x': 1} > > >>> c.total > > {('x', 2, 3): 1, ('x', 1, 2): 1, ('x', 0, 1): 1} > > >>> c.count > > > > Now read the code for Calculate.add(). Do you understand what > > > > > self.prior[cls] = self.prior.get(cls, 0) + 1 > > > > does? Experiment with a dict and its get() method in the interactive > > interpreter. Next to the loop. > > > > > for idx, val in enumerate(obs): > > > key = cls, idx, val > > > self.total[key] = self.total.get(key, 0) + 1 > > > self.count += 1 > > > > Do you understand what enumerate() does? If not read its documentation with > > > > >>> help(enumerate) > > > > Do you understand what key looks like? If you don't add a print statement > > > > > for idx, val in enumerate(obs): > > > key = cls, idx, val > > print key > > > self.total[key] = self.total.get(key, 0) + 1 > > > self.count += 1 > > > > What does > > > > > self.total[key] = self.total.get(key, 0) + 1 > > > > do? Note that this line is very similar to > > > > > self.prior[cls] = self.prior.get(cls, 0) + 1 > > > > which you have studied before. > > > > > self.count += 1 > > > > This like the rest of your class is left as an exercise. The routine is > > always the same: > > > > - break parts that you don't understand into smaller parts > > - consult the documentation on unknown classes, functions, methods, > > preferrably with help(some_obj) or dir(some_obj) > > - run portions of the code or similar code in the interactive interpreter or > > with a little throw-away script. > > - add print statements to inspect variables at interesting points in your > > script. Dear Sir, Thank you for your kind guidance. I tried to do the following exercises, (i) On dict.get(): >>> tel = {'jack': 4098, 'sape': 4139, 'obama':3059,'blair':3301} >>> dict.get('obama') >>> tel.get('obama') 3059 >>> for i in tel: x1=tel.get(i) print x1 4139 3301 4098 3059 >>> >>> tel.get('blair',0) 3301 >>> >>> tel.get('blair',0)+1 3302 >>> (ii) On enumerate: >>> list1=["Man","Woman","Gentleman","Lady","Sir","Madam"] >>> for i,j in enumerate(list1): print i,j 0 Man 1 Woman 2 Gentleman 3 Lady 4 Sir 5 Madam >>> (iii) Trying to check the values individually: >>> class Calculate: def __init__(self): self.prior = {} self.total = {} self.count = 0 def add(self, cls, obs): self.prior[cls] = self.prior.get(cls, 0) + 1 for idx, val in enumerate(obs): key = cls, idx, val print key self.total[key] = self.total.get(key, 0) + 1 self.count += 1 >>> x1=Calculate() >>> x1.add("x", [1,2,3]) ('x', 0, 1) ('x', 1, 2) ('x', 2, 3) >>> class Calculate: def __init__(self): self.prior = {} self.total = {} self.count = 0 def add(self, cls, obs): self.prior[cls] = self.prior.get(cls, 0) + 1 for idx, val in enumerate(obs): key = cls, idx, val self.total[key] = self.total.get(key, 0) + 1 self.count += 1 def discr(self, cls, obs): result = self.prior[cls]/self.count for idx, val in enumerate(obs): freq = self.total.get((cls, idx, val), 0) print freq result *= freq/self.prior[cls] print result >>> x2=Calculate() >>> x2.add("x", [7,8,9]) >>> x2.discr("x", [7,8,9]) 1 1.0 1 1.0 1 1.0 >>> Based on these exercises,I tried to we can say, self.total is dictionary format,and key is cls,idx,val, where cls is assigned in the self.prior. The next function is more or less same but here cls is called from earlier function. Please let me know how I am addressing it and if I have to do any extra bit? Regards, Subhabrata. From subhabangalore at gmail.com Fri Nov 16 15:39:31 2012 From: subhabangalore at gmail.com (subhabangalore at gmail.com) Date: Fri, 16 Nov 2012 12:39:31 -0800 (PST) Subject: Understanding Code In-Reply-To: References: <45923b5c-f0bd-4367-84f9-ba08a260da69@googlegroups.com> Message-ID: <2e9710fe-8dcc-4c42-b535-bfadd374b938@googlegroups.com> On Tuesday, November 13, 2012 4:12:52 PM UTC+5:30, Peter Otten wrote: > subhabangalore at gmail.com wrote: > > > > > Dear Group, > > > To improve my code writing I am trying to read good codes. Now, I have > > > received a code,as given below,(apology for slight indentation errors) the > > > code is running well. Now to comprehend the code, I am looking to > > > understand it completely. > > > > > > class Calculate: > > > def __init__(self): > > > self.prior = {} > > > self.total = {} > > > self.count = 0 > > > def add(self, cls, obs): > > > self.prior[cls] = self.prior.get(cls, 0) + 1 > > > for idx, val in enumerate(obs): > > > key = cls, idx, val > > > self.total[key] = self.total.get(key, 0) + 1 > > > self.count += 1 > > > def discr(self, cls, obs): > > > result = self.prior[cls]/self.count > > > for idx, val in enumerate(obs): > > > freq = self.total.get((cls, idx, val), 0) > > > result *= freq/self.prior[cls] > > > return result > > > def classify(self, obs): > > > candidates = [(self.discr(c, obs), c) for c in self.prior] > > > return max(candidates)[1] > > > > > > I am not understanding many parts of it, I am understanding many parts of > > > it also. > > > > > > So I am looking for an exercise what are the things I should know to > > > understand it, (please do not give answers I would get back with the > > > answers in a week and would discuss even how to write better than this). > > > > Start with running the code for the simplest piece of the class: > > >>> c = Calculate() > > >>> c.add("x", [1,2,3]) > > > > Then inspect the attributes: > > > > >>> c.prior > > {'x': 1} > > >>> c.total > > {('x', 2, 3): 1, ('x', 1, 2): 1, ('x', 0, 1): 1} > > >>> c.count > > > > Now read the code for Calculate.add(). Do you understand what > > > > > self.prior[cls] = self.prior.get(cls, 0) + 1 > > > > does? Experiment with a dict and its get() method in the interactive > > interpreter. Next to the loop. > > > > > for idx, val in enumerate(obs): > > > key = cls, idx, val > > > self.total[key] = self.total.get(key, 0) + 1 > > > self.count += 1 > > > > Do you understand what enumerate() does? If not read its documentation with > > > > >>> help(enumerate) > > > > Do you understand what key looks like? If you don't add a print statement > > > > > for idx, val in enumerate(obs): > > > key = cls, idx, val > > print key > > > self.total[key] = self.total.get(key, 0) + 1 > > > self.count += 1 > > > > What does > > > > > self.total[key] = self.total.get(key, 0) + 1 > > > > do? Note that this line is very similar to > > > > > self.prior[cls] = self.prior.get(cls, 0) + 1 > > > > which you have studied before. > > > > > self.count += 1 > > > > This like the rest of your class is left as an exercise. The routine is > > always the same: > > > > - break parts that you don't understand into smaller parts > > - consult the documentation on unknown classes, functions, methods, > > preferrably with help(some_obj) or dir(some_obj) > > - run portions of the code or similar code in the interactive interpreter or > > with a little throw-away script. > > - add print statements to inspect variables at interesting points in your > > script. Dear Sir, Thank you for your kind guidance. I tried to do the following exercises, (i) On dict.get(): >>> tel = {'jack': 4098, 'sape': 4139, 'obama':3059,'blair':3301} >>> dict.get('obama') >>> tel.get('obama') 3059 >>> for i in tel: x1=tel.get(i) print x1 4139 3301 4098 3059 >>> >>> tel.get('blair',0) 3301 >>> >>> tel.get('blair',0)+1 3302 >>> (ii) On enumerate: >>> list1=["Man","Woman","Gentleman","Lady","Sir","Madam"] >>> for i,j in enumerate(list1): print i,j 0 Man 1 Woman 2 Gentleman 3 Lady 4 Sir 5 Madam >>> (iii) Trying to check the values individually: >>> class Calculate: def __init__(self): self.prior = {} self.total = {} self.count = 0 def add(self, cls, obs): self.prior[cls] = self.prior.get(cls, 0) + 1 for idx, val in enumerate(obs): key = cls, idx, val print key self.total[key] = self.total.get(key, 0) + 1 self.count += 1 >>> x1=Calculate() >>> x1.add("x", [1,2,3]) ('x', 0, 1) ('x', 1, 2) ('x', 2, 3) >>> class Calculate: def __init__(self): self.prior = {} self.total = {} self.count = 0 def add(self, cls, obs): self.prior[cls] = self.prior.get(cls, 0) + 1 for idx, val in enumerate(obs): key = cls, idx, val self.total[key] = self.total.get(key, 0) + 1 self.count += 1 def discr(self, cls, obs): result = self.prior[cls]/self.count for idx, val in enumerate(obs): freq = self.total.get((cls, idx, val), 0) print freq result *= freq/self.prior[cls] print result >>> x2=Calculate() >>> x2.add("x", [7,8,9]) >>> x2.discr("x", [7,8,9]) 1 1.0 1 1.0 1 1.0 >>> Based on these exercises,I tried to we can say, self.total is dictionary format,and key is cls,idx,val, where cls is assigned in the self.prior. The next function is more or less same but here cls is called from earlier function. Please let me know how I am addressing it and if I have to do any extra bit? Regards, Subhabrata. From ethan at stoneleaf.us Fri Nov 16 16:30:34 2012 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 16 Nov 2012 13:30:34 -0800 Subject: Is there a simpler way to modify all arguments in a function before using the arguments? In-Reply-To: <211b323b-1fd4-4018-a7e5-34c6fbda01a5@googlegroups.com> References: <18134e77-9b02-4aec-afb0-794ed900d194@googlegroups.com> <211b323b-1fd4-4018-a7e5-34c6fbda01a5@googlegroups.com> Message-ID: <50A6B07A.7040409@stoneleaf.us> bruceg113355 at gmail.com wrote: > On Thursday, November 15, 2012 11:16:08 PM UTC-5, Ethan Furman wrote: >> Emile van Sebille wrote: >> >> >>>> Using a decorator works when named arguments are not used. When named >>>> arguments are used, unexpected keyword error is reported. Is there a >>>> simple fix? >>> Extend def wrapper(*args) to handle *kwargs as well >>> Emile >>>> Code: >>>> ----- >>>> from functools import wraps >>>> def fix_args(fn): >>>> @wraps(fn) >>>> def wrapper(*args): >> so this line ^ becomes >> >> def wrapper(*args, **kwargs): >> >>>> args = (arg.replace('_', '') for arg in args) >> and add a line >> >> for k, v in kwargs: >> >> kwargs[k] = v.replace('_', '') >> >>>> return fn(*args) >> and this line ^ becomes >> >> return fn(*args, **kwargs) >> >>>> return wrapper >> >> >> ~Ethan~ > > > Ethan, > > I tried you code suggestions but got errors. Right, my 'for k, v in kwargs' should have been 'for k, v in kwargs.items()' Glad you were able to make it work! ~Ethan~ From buck at yelp.com Fri Nov 16 16:44:03 2012 From: buck at yelp.com (buck at yelp.com) Date: Fri, 16 Nov 2012 13:44:03 -0800 (PST) Subject: latin1 and cp1252 inconsistent? Message-ID: Latin1 has a block of 32 undefined characters. Windows-1252 (aka cp1252) fills in 27 of these characters but leaves five undefined: 0x81, 0x8D, 0x8F, 0x90, 0x9D The byte 0x81 decoded with latin gives the unicode 0x81. Decoding the same byte with windows-1252 yields a stack trace with `UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 0: character maps to ` This seems inconsistent to me, given that this byte is equally undefined in the two standards. Also, the html5 standard says: When a user agent [browser] would otherwise use a character encoding given in the first column [ISO-8859-1, aka latin1] of the following table to either convert content to Unicode characters or convert Unicode characters to bytes, it must instead use the encoding given in the cell in the second column of the same row [windows-1252, aka cp1252]. http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#character-encodings-0 The current implementation of windows-1252 isn't usable for this purpose (a replacement of latin1), since it will throw an error in cases that latin1 would succeed. From tjreedy at udel.edu Fri Nov 16 17:00:22 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 16 Nov 2012 17:00:22 -0500 Subject: error importing smtplib In-Reply-To: References: Message-ID: On 11/16/2012 2:37 PM, Eric Frederich wrote: > So I inspected the process through /proc//maps > That seemed to show what libraries had been loaded (though there is > probably an easier way to do this). > In any case, I found that if I import smtplib before logging in I see > these get loaded... > > /opt/foo/python27/lib/python2.7/lib-dynload/_ssl.so > /lib64/libssl.so.0.9.8e > > Then after logging in, I see this other .so get loaded... > > /opt/bar/lib64/libssl.so.0.9.7 That version appears to be about a decade old. Why is bar using it? > So that is what happens when when things are well and I don't get any > error messages. > However, when I do the log in first I see the /opt/bar .so file loaded first > > /opt/bar/lib64/libssl.so.0.9.7 > > Then after importing smtplib I see the other two show up... > > /opt/foo/python27/lib/python2.7/lib-dynload/_ssl.so > /lib64/libssl.so.0.9.8e What I know is that hashlib.py imports _hashlib (compilied .c) and that the latter wraps libssl, or calls _ssl.so which wraps libssl. In *nix this is expected to already be on the system, so in not distributed with python. Furthermore, hashlib requires a version recent enough to have the latest (best) hash functions. I suspect decade-old 9.9.7 does not qualify. What I don't know is how .so loading and linking works. It seems that two version get loaded but linking gets messed up. This reminds me of 'dll hell' on Windows ;-). I don't know either if modifying the loading of ...9.7 in for or bar code could do anything. > So.... I'm guessing the problem is that after I log in, the process has > a conflicting libssl.so file loaded. > Then when I try to import smtplib it tries getting things from there and > that is where the errors are coming from. > > The question now is how do I fix this? [easy] Do the import before the function call, which is the proper order and the one that works. Remove ...9.7 from bar/lib64/ and have bar use the up-to-date system version, like python does. An alternative is to replace ...9.7 with a duplicate ...9.8e (and probably better, only load it if there is no system version). > What else should I be checking? Thinking more, you can look at sys.modules, but this does not have any info about non-module libraries wrapped by modules, even if the latter are C-coded. -- Terry Jan Reedy From ian.g.kelly at gmail.com Fri Nov 16 17:33:59 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 16 Nov 2012 15:33:59 -0700 Subject: latin1 and cp1252 inconsistent? In-Reply-To: References: Message-ID: On Fri, Nov 16, 2012 at 2:44 PM, wrote: > Latin1 has a block of 32 undefined characters. These characters are not undefined. 0x80-0x9f are the C1 control codes in Latin-1, much as 0x00-0x1f are the C0 control codes, and their Unicode mappings are well defined. http://tools.ietf.org/html/rfc1345 > Windows-1252 (aka cp1252) fills in 27 of these characters but leaves five undefined: 0x81, 0x8D, 0x8F, 0x90, 0x9D In CP 1252, these codes are actually undefined. http://msdn.microsoft.com/en-us/goglobal/cc305145.aspx > Also, the html5 standard says: > > When a user agent [browser] would otherwise use a character encoding given in the first column [ISO-8859-1, aka latin1] of the following table to either convert content to Unicode characters or convert Unicode characters to bytes, it must instead use the encoding given in the cell in the second column of the same row [windows-1252, aka cp1252]. > > http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#character-encodings-0 > > > The current implementation of windows-1252 isn't usable for this purpose (a replacement of latin1), since it will throw an error in cases that latin1 would succeed. You can use a non-strict error handling scheme to prevent the error. >>> b'hello \x81 world'.decode('cp1252') Traceback (most recent call last): File "", line 1, in File "c:\python33\lib\encodings\cp1252.py", line 15, in decode return codecs.charmap_decode(input,errors,decoding_table) UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 6: character maps to >>> b'hello \x81 world'.decode('cp1252', 'replace') 'hello \ufffd world' >>> b'hello \x81 world'.decode('cp1252', 'ignore') 'hello world' From memilanuk at gmail.com Fri Nov 16 18:25:33 2012 From: memilanuk at gmail.com (Monte Milanuk) Date: Fri, 16 Nov 2012 15:25:33 -0800 Subject: 'Experimental Design' aka DoE Message-ID: Hello, I'm interested in refining some tests I do for a hobby of mine beyond the traditional 'one factor at a time' (OFAT) method. I have been looking at 'design of experiment' (DoE) methods and they look promising. The problem is that most of the software packages that aid in the setup and analysis of this sort of thing are rather expensive (the cheapest is an Excel plugin that runs ~$250, and they go up from there rapidly). I've found some limited DoE support in R and SciLab, but I'd like to stick with Python if possible. So far though, I haven't been able to find hardly any mention of the topic, even searching through lists for numpy, scipy, pydata, statsmodel, etc. Given the hype that I've always seen about Python being used in science & engineering fields, it seems like there should be *something* already out there... If anyone has any handy links or references I'd greatly appreciate it. TIA, Monte -------------- next part -------------- An HTML attachment was scrubbed... URL: From buck at yelp.com Fri Nov 16 18:27:54 2012 From: buck at yelp.com (buck at yelp.com) Date: Fri, 16 Nov 2012 15:27:54 -0800 (PST) Subject: latin1 and cp1252 inconsistent? In-Reply-To: References: Message-ID: <4c683e33-d6cb-480c-a6da-20e3523c2103@googlegroups.com> On Friday, November 16, 2012 2:34:32 PM UTC-8, Ian wrote: > On Fri, Nov 16, 2012 at 2:44 PM, wrote: > > > Latin1 has a block of 32 undefined characters. > > > These characters are not undefined. 0x80-0x9f are the C1 control > codes in Latin-1, much as 0x00-0x1f are the C0 control codes, and > their Unicode mappings are well defined. They are indeed undefined: ftp://std.dkuug.dk/JTC1/sc2/wg3/docs/n411.pdf """ The shaded positions in the code table correspond to bit combinations that do not represent graphic characters. Their use is outside the scope of ISO/IEC 8859; it is specified in other International Standards, for example ISO/IEC 6429. However it's reasonable for 0x81 to decode to U+81 because the unicode standard says: http://www.unicode.org/versions/Unicode6.2.0/ch16.pdf """ The semantics of the control codes are generally determined by the application with which they are used. However, in the absence of specific application uses, they may be interpreted according to the control function semantics specified in ISO/IEC 6429:1992. > You can use a non-strict error handling scheme to prevent the error. > >>> b'hello \x81 world'.decode('cp1252', 'replace') > 'hello \ufffd world' This creates a non-reversible encoding, and loss of data, which isn't acceptable for my application. From buck at yelp.com Fri Nov 16 18:27:54 2012 From: buck at yelp.com (buck at yelp.com) Date: Fri, 16 Nov 2012 15:27:54 -0800 (PST) Subject: latin1 and cp1252 inconsistent? In-Reply-To: References: Message-ID: <4c683e33-d6cb-480c-a6da-20e3523c2103@googlegroups.com> On Friday, November 16, 2012 2:34:32 PM UTC-8, Ian wrote: > On Fri, Nov 16, 2012 at 2:44 PM, wrote: > > > Latin1 has a block of 32 undefined characters. > > > These characters are not undefined. 0x80-0x9f are the C1 control > codes in Latin-1, much as 0x00-0x1f are the C0 control codes, and > their Unicode mappings are well defined. They are indeed undefined: ftp://std.dkuug.dk/JTC1/sc2/wg3/docs/n411.pdf """ The shaded positions in the code table correspond to bit combinations that do not represent graphic characters. Their use is outside the scope of ISO/IEC 8859; it is specified in other International Standards, for example ISO/IEC 6429. However it's reasonable for 0x81 to decode to U+81 because the unicode standard says: http://www.unicode.org/versions/Unicode6.2.0/ch16.pdf """ The semantics of the control codes are generally determined by the application with which they are used. However, in the absence of specific application uses, they may be interpreted according to the control function semantics specified in ISO/IEC 6429:1992. > You can use a non-strict error handling scheme to prevent the error. > >>> b'hello \x81 world'.decode('cp1252', 'replace') > 'hello \ufffd world' This creates a non-reversible encoding, and loss of data, which isn't acceptable for my application. From d at davea.name Fri Nov 16 19:05:32 2012 From: d at davea.name (Dave Angel) Date: Fri, 16 Nov 2012 19:05:32 -0500 Subject: latin1 and cp1252 inconsistent? In-Reply-To: <4c683e33-d6cb-480c-a6da-20e3523c2103@googlegroups.com> References: <4c683e33-d6cb-480c-a6da-20e3523c2103@googlegroups.com> Message-ID: <50A6D4CC.2020601@davea.name> On 11/16/2012 06:27 PM, buck at yelp.com wrote: > (doublespaced nonsense deleted. GoogleGropups strikes again.) > This creates a non-reversible encoding, and loss of data, which isn't > acceptable for my application. So tell us more about your application. If you have data which is invalid, and you encode it to some other form, you have to expect that it won't be reversible. But maybe your data isn't really characters at all, and you're just trying to manipulate bytes? Without a use case, we really can't guess. The fact that you are waffling between latin1 and 1252 indicates this isn't really character data. Also, while you're at it, please specify the Python version and OS you're on. You haven't given us any code to guess it from. -- DaveA From nobody at nowhere.com Fri Nov 16 19:17:26 2012 From: nobody at nowhere.com (Nobody) Date: Sat, 17 Nov 2012 00:17:26 +0000 Subject: Subprocess puzzle and two questions References: Message-ID: On Thu, 15 Nov 2012 20:07:38 -0500, Roy Smith wrote: >>> gethostbyname() and getaddrinfo() use the NSS (name-service switch) >> mechanism, which is configured via /etc/nsswitch.conf. Depending upon >> configuration, hostnames can be looked up via a plain text file >> (/etc/hosts), Berkeley DB files, DNS, NIS, NIS+, LDAP, WINS, etc. > > Gethostbyname() long predates NSS. Before NSS there was host.conf, which provided similar functionality except that the set of mechanisms was fixed (they were built into libc rather than being dynamically-loaded libraries) and it only applied to hostnames (NSS is also used for getpwent(), getprotoent(), etc). > For that matter, I think it even predates DNS (i.e. back to the days > when /etc/hosts was the *only* way to look up a hostname). > > But, that's a nit. Indeed; the main point is that gethostbyname() has never been specific to DNS. From ian.g.kelly at gmail.com Fri Nov 16 19:20:24 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 16 Nov 2012 17:20:24 -0700 Subject: latin1 and cp1252 inconsistent? In-Reply-To: <4c683e33-d6cb-480c-a6da-20e3523c2103@googlegroups.com> References: <4c683e33-d6cb-480c-a6da-20e3523c2103@googlegroups.com> Message-ID: On Fri, Nov 16, 2012 at 4:27 PM, wrote: > They are indeed undefined: ftp://std.dkuug.dk/JTC1/sc2/wg3/docs/n411.pdf > > """ The shaded positions in the code table correspond > to bit combinations that do not represent graphic > characters. Their use is outside the scope of > ISO/IEC 8859; it is specified in other International > Standards, for example ISO/IEC 6429. It gets murkier than that. I don't want to spend time hunting down the relevant documents, so I'll just quote from Wikipedia: """ In 1992, the IANA registered the character map ISO_8859-1:1987, more commonly known by its preferred MIME name of ISO-8859-1 (note the extra hyphen over ISO 8859-1), a superset of ISO 8859-1, for use on the Internet. This map assigns the C0 and C1 control characters to the unassigned code values thus provides for 256 characters via every possible 8-bit value. """ http://en.wikipedia.org/wiki/ISO/IEC_8859-1#History >> You can use a non-strict error handling scheme to prevent the error. >> >>> b'hello \x81 world'.decode('cp1252', 'replace') >> 'hello \ufffd world' > > This creates a non-reversible encoding, and loss of data, which isn't acceptable for my application. Well, what characters would you have these bytes decode to, considering that they're undefined? If the string is really CP-1252, then the presence of undefined characters in the document does not signify "data". They're just junk bytes, possibly indicative of data corruption. If on the other hand the string is really Latin-1, and you *know* that it is Latin-1, then you should probably forget the aliasing recommendation and just decode it as Latin-1. Apparently this Latin-1 -> CP-1252 encoding aliasing is already commonly performed by modern user agents. What do IE and Firefox do when presented with a Latin-1 encoding and undefined CP-1252 codings? From nobody at nowhere.com Fri Nov 16 19:33:14 2012 From: nobody at nowhere.com (Nobody) Date: Sat, 17 Nov 2012 00:33:14 +0000 Subject: latin1 and cp1252 inconsistent? References: Message-ID: On Fri, 16 Nov 2012 13:44:03 -0800, buck wrote: > When a user agent [browser] would otherwise use a character encoding given > in the first column [ISO-8859-1, aka latin1] of the following table to > either convert content to Unicode characters or convert Unicode characters > to bytes, it must instead use the encoding given in the cell in the second > column of the same row [windows-1252, aka cp1252]. It goes on to say: The requirement to treat certain encodings as other encodings according to the table above is a willful violation of the W3C Character Model specification, motivated by a desire for compatibility with legacy content. [CHARMOD] IOW: Microsoft's "embrace, extend, extinguish" strategy has been too successful and now we have to deal with it. If HTML content is tagged as using ISO-8859-1, it's more likely that it's actually Windows-1252 content generated by someone who doesn't know the difference. Given that the only differences between the two are for code points which are in the C1 range (0x80-0x9F), which should never occur in HTML, parsing ISO-8859-1 as Windows-1252 should be harmless. If you need to support either, you can parse it as ISO-8859-1 then explicitly convert C1 codes to their Windows-1252 equivalents as a post-processing step, e.g. using the .translate() method. From ian.g.kelly at gmail.com Fri Nov 16 20:08:36 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 16 Nov 2012 18:08:36 -0700 Subject: latin1 and cp1252 inconsistent? In-Reply-To: References: Message-ID: On Fri, Nov 16, 2012 at 5:33 PM, Nobody wrote: > If you need to support either, you can parse it as ISO-8859-1 then > explicitly convert C1 codes to their Windows-1252 equivalents as a > post-processing step, e.g. using the .translate() method. Or just create a custom codec by taking the one in Lib/encodings/cp1252.py and modifying it slightly. >>> import codecs >>> import cp1252a >>> codecs.register(lambda n: cp1252a.getregentry() if n == "cp1252a" else None) >>> b'\x81\x8d\x8f\x90\x9d'.decode('cp1252a') '?????' From steve+comp.lang.python at pearwood.info Fri Nov 16 20:30:57 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 17 Nov 2012 01:30:57 GMT Subject: StandardError in Python 2 -> 3 Message-ID: <50a6e8d1$0$29978$c3e8da3$5496439d@news.astraweb.com> The exception hierarchy in Python 3 is shallower than in Python 2. Here is a partial list of exceptions in Python 2: BaseException +-- SystemExit +-- KeyboardInterrupt +-- GeneratorExit +-- Exception +-- StandardError | +-- AttributeError | +-- ImportError | +-- NameError | +-- TypeError | +-- ValueError +-- Warning and the same again in Python 3: BaseException +-- SystemExit +-- KeyboardInterrupt +-- GeneratorExit +-- Exception +-- AttributeError +-- ImportError +-- NameError +-- SystemError +-- TypeError +-- ValueError +-- Warning Note that StandardError is gone. Does anyone use StandardError in their own code? In Python 2, I normally inherit from StandardError rather than Exception. Should I stop and just inherit from Exception in both 2 and 3? -- Steven From ian.g.kelly at gmail.com Fri Nov 16 20:51:53 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 16 Nov 2012 18:51:53 -0700 Subject: StandardError in Python 2 -> 3 In-Reply-To: <50a6e8d1$0$29978$c3e8da3$5496439d@news.astraweb.com> References: <50a6e8d1$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Nov 16, 2012 at 6:30 PM, Steven D'Aprano wrote: > Does anyone use StandardError in their own code? In Python 2, I normally > inherit from StandardError rather than Exception. Should I stop and just > inherit from Exception in both 2 and 3? According to the docs, StandardError is for built-in exceptions, and user-defined exceptions are meant to inherit from Exception. http://docs.python.org/2/library/exceptions.html#exceptions.Exception From steve+comp.lang.python at pearwood.info Fri Nov 16 22:12:30 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 17 Nov 2012 03:12:30 GMT Subject: Catching exceptions from Python 2.4 to 3.x Message-ID: <50a7009e$0$29978$c3e8da3$5496439d@news.astraweb.com> Oh for the day I can drop support for Python 2.4 and 2.5... I have some code that needs to run in any version of Python from 2.4 onwards. Yes, it must be a single code base. I wish to catch an exception and bind the exception to a name. In Python 2.6 onwards, I can do: try: something() except Exception as err: process(err) But in 2.4 and 2.5 "as err" gives me a SyntaxError, and I have to write: try: something() except Exception, err: process(err) Is there some other trick to grab the current exception from inside an except block? -- Steven From cs at zip.com.au Fri Nov 16 22:26:43 2012 From: cs at zip.com.au (Cameron Simpson) Date: Sat, 17 Nov 2012 14:26:43 +1100 Subject: Catching exceptions from Python 2.4 to 3.x In-Reply-To: <50a7009e$0$29978$c3e8da3$5496439d@news.astraweb.com> References: <50a7009e$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: <20121117032643.GA5417@cskk.homeip.net> On 17Nov2012 03:12, Steven D'Aprano wrote: | Oh for the day I can drop support for Python 2.4 and 2.5... | | | I have some code that needs to run in any version of Python from 2.4 | onwards. Yes, it must be a single code base. | | I wish to catch an exception and bind the exception to a name. | | In Python 2.6 onwards, I can do: | | try: | something() | except Exception as err: | process(err) | | But in 2.4 and 2.5 "as err" gives me a SyntaxError, and I have to write: | | try: | something() | except Exception, err: | process(err) | | | Is there some other trick to grab the current exception from inside an | except block? sys.exc_info ? -- Cameron Simpson Peeve: Going to our favorite breakfast place, only to find that they were hit by a car...AND WE MISSED IT. - Don Baldwin, From jordan.bylsma1 at gmail.com Sat Nov 17 10:04:07 2012 From: jordan.bylsma1 at gmail.com (Jordan Bylsma) Date: Sat, 17 Nov 2012 07:04:07 -0800 (PST) Subject: Python Script for Colorizing Traceroute Output Message-ID: <6ea528f6-e4c2-4da9-a011-d81f13b83d28@googlegroups.com> I'm looking into writing a python script that colorizes particular hops when using traceroute. Anyone run across something like this? I don't think it would be extremely difficult to write but some example code would help. Basically particular hops in traceroute output would match a table as either a router, firewall or layer 3 switch and be colorized accordingly. From garabik-news-2005-05 at kassiopeia.juls.savba.sk Sat Nov 17 10:18:56 2012 From: garabik-news-2005-05 at kassiopeia.juls.savba.sk (garabik-news-2005-05 at kassiopeia.juls.savba.sk) Date: Sat, 17 Nov 2012 15:18:56 +0000 (UTC) Subject: Python Script for Colorizing Traceroute Output References: <6ea528f6-e4c2-4da9-a011-d81f13b83d28@googlegroups.com> Message-ID: Jordan Bylsma wrote: > I'm looking into writing a python script that colorizes particular > hops when using traceroute. Anyone run across something like this? I > don't think it would be extremely difficult to write but some example > code would help. > Generic Colouriser http://kassiopeia.juls.savba.sk/~garabik/software/grc.html It includes configuration file for traceroute. -- ----------------------------------------------------------- | Radovan Garab?k http://kassiopeia.juls.savba.sk/~garabik/ | | __..--^^^--..__ garabik @ kassiopeia.juls.savba.sk | ----------------------------------------------------------- Antivirus alert: file .signature infected by signature virus. Hi! I'm a signature virus! Copy me into your signature file to help me spread! From steve+comp.lang.python at pearwood.info Sat Nov 17 10:53:45 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 17 Nov 2012 15:53:45 GMT Subject: Catching exceptions from Python 2.4 to 3.x References: <50a7009e$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50a7b309$0$29978$c3e8da3$5496439d@news.astraweb.com> On Sat, 17 Nov 2012 14:26:43 +1100, Cameron Simpson wrote: > On 17Nov2012 03:12, Steven D'Aprano > wrote: > | Is there some other trick to grab the current exception from inside an > | except block? > > sys.exc_info ? Thanks, that is just what I was looking for. -- Steven From buck at yelp.com Sat Nov 17 11:56:46 2012 From: buck at yelp.com (buck at yelp.com) Date: Sat, 17 Nov 2012 08:56:46 -0800 (PST) Subject: latin1 and cp1252 inconsistent? In-Reply-To: References: Message-ID: <32ab6e2e-e1b1-41ea-8ef4-6e4f763065bf@googlegroups.com> On Friday, November 16, 2012 4:33:14 PM UTC-8, Nobody wrote: > On Fri, 16 Nov 2012 13:44:03 -0800, buck wrote: > IOW: Microsoft's "embrace, extend, extinguish" strategy has been too > successful and now we have to deal with it. If HTML content is tagged as > using ISO-8859-1, it's more likely that it's actually Windows-1252 content > generated by someone who doesn't know the difference. Yes that's exactly what it says. > Given that the only differences between the two are for code points which > are in the C1 range (0x80-0x9F), which should never occur in HTML, parsing > ISO-8859-1 as Windows-1252 should be harmless. "should" is a wish. The reality is that documents (and especially URLs) exist that can be decoded with latin1, but will backtrace with cp1252. I see this as a sign that a small refactorization of cp1252 is in order. The proposal is to change those "UNDEFINED" entries to "" entries, as is done here: http://dvcs.w3.org/hg/encoding/raw-file/tip/index-windows-1252.txt and here: ftp://ftp.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WindowsBestFit/bestfit1252.txt This is in line with the unicode standard, which says: http://www.unicode.org/versions/Unicode6.2.0/ch16.pdf > There are 65 code points set aside in the Unicode Standard for compatibility with the C0 > and C1 control codes defined in the ISO/IEC 2022 framework. The ranges of these code > points are U+0000..U+001F, U+007F, and U+0080..U+009F, which correspond to the 8-bit > controls 0x00 to 0x1F (C0 controls), 0x7F (delete), and 0x80 to 0x9F (C1 controls), > respectively ... There is a simple, one-to-one mapping between 7-bit (and 8-bit) control > codes and the Unicode control codes: every 7-bit (or 8-bit) control code is numerically > equal to its corresponding Unicode code point. IOW: Bytes with undefined semantics in the C0/C1 range are "control codes", which decode to the unicode-point of equal value. This is exactly the section which allows latin1 to decode 0x81 to U+81, even though ISO-8859-1 explicitly does not define semantics for that byte (6.2 ftp://std.dkuug.dk/JTC1/sc2/wg3/docs/n411.pdf) From wrw at mac.com Sat Nov 17 12:06:14 2012 From: wrw at mac.com (wrw at mac.com) Date: Sat, 17 Nov 2012 12:06:14 -0500 Subject: Python Script for Colorizing Traceroute Output In-Reply-To: <6ea528f6-e4c2-4da9-a011-d81f13b83d28@googlegroups.com> References: <6ea528f6-e4c2-4da9-a011-d81f13b83d28@googlegroups.com> Message-ID: Don't forget that most firewalls don't decrement) the time-to-live number, and unless you REALLY know what to look for, are invisible. -Bill On Nov 17, 2012, at 10:04 AM, Jordan Bylsma wrote: > I'm looking into writing a python script that colorizes particular hops when using traceroute. Anyone run across something like this? I don't think it would be extremely difficult to write but some example code would help. > > Basically particular hops in traceroute output would match a table as either a router, firewall or layer 3 switch and be colorized accordingly. > -- > http://mail.python.org/mailman/listinfo/python-list From wrw at mac.com Sat Nov 17 12:07:27 2012 From: wrw at mac.com (wrw at mac.com) Date: Sat, 17 Nov 2012 12:07:27 -0500 Subject: Python Script for Colorizing Traceroute Output (apologies for top-post) In-Reply-To: <6ea528f6-e4c2-4da9-a011-d81f13b83d28@googlegroups.com> References: <6ea528f6-e4c2-4da9-a011-d81f13b83d28@googlegroups.com> Message-ID: <9B5D5553-33D0-4FCD-BF5A-797ED25950CD@mac.com> On Nov 17, 2012, at 10:04 AM, Jordan Bylsma wrote: > I'm looking into writing a python script that colorizes particular hops when using traceroute. Anyone run across something like this? I don't think it would be extremely difficult to write but some example code would help. > > Basically particular hops in traceroute output would match a table as either a router, firewall or layer 3 switch and be colorized accordingly. > -- > http://mail.python.org/mailman/listinfo/python-list Don't forget that most firewalls don't decrement) the time-to-live number, and unless you REALLY know what to look for, are invisible. -Bill From steve+comp.lang.python at pearwood.info Sat Nov 17 12:47:58 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 17 Nov 2012 17:47:58 GMT Subject: module trace and counts References: Message-ID: <50a7cdce$0$29978$c3e8da3$5496439d@news.astraweb.com> On Sat, 17 Nov 2012 09:25:02 -0800, rh wrote: > Is it for or range that is executed 8000 times when I for i in > range(3,8000,2): I'm sorry, I don't understand that question. Is it for what? What "it" are you talking about? If you run the code: for i in range(3, 8000, 2): pass the `for` statement executes once, giving 3999 iterations in total. `range` gets executed once. Nothing is executed 8000 times. > Maybe the for and the range contributes to that total. What total? I'm sure the context of your question is obvious to you, but we're not mind-readers and have no idea what you have done and what total you have got. Please explain your question. -- Steven From chinjannisha at gmail.com Sat Nov 17 13:01:01 2012 From: chinjannisha at gmail.com (chinjannisha at gmail.com) Date: Sat, 17 Nov 2012 10:01:01 -0800 (PST) Subject: Python Interview Questions In-Reply-To: <1193768041.349129.26350@v3g2000hsg.googlegroups.com> References: <1193768041.349129.26350@v3g2000hsg.googlegroups.com> Message-ID: <55443eb7-847c-4f4c-8d04-1e6b507aac00@googlegroups.com> Hi I had one doubt.. I know very little bit of python .I wanted to know when to use list,tuple,dictionary and set? Please reply me asap thanks From ian.g.kelly at gmail.com Sat Nov 17 13:08:49 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sat, 17 Nov 2012 11:08:49 -0700 Subject: latin1 and cp1252 inconsistent? In-Reply-To: <32ab6e2e-e1b1-41ea-8ef4-6e4f763065bf@googlegroups.com> References: <32ab6e2e-e1b1-41ea-8ef4-6e4f763065bf@googlegroups.com> Message-ID: On Sat, Nov 17, 2012 at 9:56 AM, wrote: > "should" is a wish. The reality is that documents (and especially URLs) exist that can be decoded with latin1, but will backtrace with cp1252. I see this as a sign that a small refactorization of cp1252 is in order. The proposal is to change those "UNDEFINED" entries to "" entries, as is done here: > > http://dvcs.w3.org/hg/encoding/raw-file/tip/index-windows-1252.txt > > and here: > > ftp://ftp.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WindowsBestFit/bestfit1252.txt The README for the "BestFit" document states: """ These tables include "best fit" behavior which is not present in the other files. Examples of best fit are converting fullwidth letters to their counterparts when converting to single byte code pages, and mapping the Infinity character to the number 8. """ This does not sound like appropriate behavior for a generalized conversion scheme. It is also noted that the "BestFit" document is not authoritative at: http://www.iana.org/assignments/charset-reg/windows-1252 > This is in line with the unicode standard, which says: http://www.unicode.org/versions/Unicode6.2.0/ch16.pdf > >> There are 65 code points set aside in the Unicode Standard for compatibility with the C0 >> and C1 control codes defined in the ISO/IEC 2022 framework. The ranges of these code >> points are U+0000..U+001F, U+007F, and U+0080..U+009F, which correspond to the 8-bit >> controls 0x00 to 0x1F (C0 controls), 0x7F (delete), and 0x80 to 0x9F (C1 controls), >> respectively ... There is a simple, one-to-one mapping between 7-bit (and 8-bit) control >> codes and the Unicode control codes: every 7-bit (or 8-bit) control code is numerically >> equal to its corresponding Unicode code point. > > IOW: Bytes with undefined semantics in the C0/C1 range are "control codes", which decode to the unicode-point of equal value. > > This is exactly the section which allows latin1 to decode 0x81 to U+81, even though ISO-8859-1 explicitly does not define semantics for that byte (6.2 ftp://std.dkuug.dk/JTC1/sc2/wg3/docs/n411.pdf) But Latin-1 explicitly defers to to the control codes for those characters. CP-1252 does not; the reason those characters are left undefined is to allow for future expansion, such as when Microsoft added the Euro sign at 0x80. Since we're talking about conversion from bytes to Unicode, I think the most authoritative source we could possibly reference would be the official ISO 10646 conversion tables for the character sets in question. I understand those are to be found here: http://www.unicode.org/Public/MAPPINGS/ISO8859/8859-1.TXT and here: http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1252.TXT Note that the ISO-8859-1 mapping defines the C0 and C1 codes, whereas the cp1252 mapping leaves those five codes undefined. This would seem to indicate that Python is correctly decoding CP-1252 according to the Unicode standard. From ian.g.kelly at gmail.com Sat Nov 17 13:13:51 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sat, 17 Nov 2012 11:13:51 -0700 Subject: latin1 and cp1252 inconsistent? In-Reply-To: References: <32ab6e2e-e1b1-41ea-8ef4-6e4f763065bf@googlegroups.com> Message-ID: On Sat, Nov 17, 2012 at 11:08 AM, Ian Kelly wrote: > On Sat, Nov 17, 2012 at 9:56 AM, wrote: >> "should" is a wish. The reality is that documents (and especially URLs) exist that can be decoded with latin1, but will backtrace with cp1252. I see this as a sign that a small refactorization of cp1252 is in order. The proposal is to change those "UNDEFINED" entries to "" entries, as is done here: >> >> http://dvcs.w3.org/hg/encoding/raw-file/tip/index-windows-1252.txt >> >> and here: >> >> ftp://ftp.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WindowsBestFit/bestfit1252.txt > > The README for the "BestFit" document states: > > """ > These tables include "best fit" behavior which is not present in the > other files. Examples of best fit > are converting fullwidth letters to their counterparts when converting > to single byte code pages, and > mapping the Infinity character to the number 8. > """ > > This does not sound like appropriate behavior for a generalized > conversion scheme. It is also noted that the "BestFit" document is > not authoritative at: > > http://www.iana.org/assignments/charset-reg/windows-1252 I meant to also comment on the first link, but forgot. As that document is published by the W3C, I understand it to be specific to the Web, which Python is not. Hence I think the more general Unicode specification is more appropriate for Python. From d at davea.name Sat Nov 17 13:37:23 2012 From: d at davea.name (Dave Angel) Date: Sat, 17 Nov 2012 13:37:23 -0500 Subject: module trace and counts In-Reply-To: <20121117092502.12c53a8f48072cd249108b47@lavabit.com> References: <20121117092502.12c53a8f48072cd249108b47@lavabit.com> Message-ID: <50A7D963.5060801@davea.name> On 11/17/2012 12:25 PM, rh wrote: > Is it for or range that is executed 8000 times when I > for i in range(3,8000,2): Nothing is executed 8000 times. I figure it at 3998 times. Anyway, neither the for nor the range is executed multiple times. Deciphering this depends on whether this is Python 2.x or Python 3.x. If Python 2.x, range returns a list of 3998 items, and that is iterated over. If Python 3.x, range returns a range object, which has an __iter__() special method. That method gets called 3999 times, and the last time it throws an exception to end the loop. > > Maybe the for and the range contributes to that total. > > Can anyone recommend their favorite trace or profile type tool that > spits out an html page? I like trace because I don't have to insert my > program into it but would like a html page too. > -- DaveA From roy at panix.com Sat Nov 17 13:38:53 2012 From: roy at panix.com (Roy Smith) Date: Sat, 17 Nov 2012 13:38:53 -0500 Subject: Python Script for Colorizing Traceroute Output (apologies for top-post) References: <6ea528f6-e4c2-4da9-a011-d81f13b83d28@googlegroups.com> Message-ID: In article , wrw at mac.com wrote: > On Nov 17, 2012, at 10:04 AM, Jordan Bylsma wrote: > > > I'm looking into writing a python script that colorizes particular hops > > when using traceroute. Anyone run across something like this? I don't think > > it would be extremely difficult to write but some example code would help. > > > > Basically particular hops in traceroute output would match a table as > > either a router, firewall or layer 3 switch and be colorized accordingly. > > -- > > http://mail.python.org/mailman/listinfo/python-list > > Don't forget that most firewalls don't decrement) the time-to-live number, > and unless you REALLY know what to look for, are invisible. Interesting. If the firewall doesn't decrement TTL (a particular nasty thing to do, and thus completely expected of a firewall) what REALLY knowledge would enable you to detect its presence in a traceroute printout at all? From rosuav at gmail.com Sat Nov 17 13:45:57 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 18 Nov 2012 05:45:57 +1100 Subject: Python Script for Colorizing Traceroute Output (apologies for top-post) In-Reply-To: References: <6ea528f6-e4c2-4da9-a011-d81f13b83d28@googlegroups.com> Message-ID: On Sun, Nov 18, 2012 at 5:38 AM, Roy Smith wrote: > In article , > wrw at mac.com wrote: >> Don't forget that most firewalls don't decrement) the time-to-live number, >> and unless you REALLY know what to look for, are invisible. > > Interesting. If the firewall doesn't decrement TTL (a particular nasty > thing to do, and thus completely expected of a firewall) what REALLY > knowledge would enable you to detect its presence in a traceroute > printout at all? If it's a router, then it definitely should be decrementing TTLs, though if the firewall is simply a part of another hop, you won't notice it specifically. What's more common, though, is devices that decrement TTL and drop packets but don't send back Time Exceeded messages, giving the classic "* * *" line. I'm not sure if that's what was being referred to, though. (And they aren't necessarily firewalls.) ChrisA From nobody at nowhere.com Sat Nov 17 14:15:15 2012 From: nobody at nowhere.com (Nobody) Date: Sat, 17 Nov 2012 19:15:15 +0000 Subject: latin1 and cp1252 inconsistent? References: <32ab6e2e-e1b1-41ea-8ef4-6e4f763065bf@googlegroups.com> Message-ID: On Sat, 17 Nov 2012 08:56:46 -0800, buck wrote: >> Given that the only differences between the two are for code points >> which are in the C1 range (0x80-0x9F), which should never occur in HTML, >> parsing ISO-8859-1 as Windows-1252 should be harmless. > > "should" is a wish. The reality is that documents (and especially URLs) > exist that can be decoded with latin1, but will backtrace with cp1252. In which case, they're probably neither ISO-8859-1 nor Windows-1252, but some other (unknown) encoding which has acquired the ISO-8859-1 label "by default". In that situation, if you still need to know the encoding, you need to resort to heuristics such as those employed by the chardet library. From jordan.bylsma1 at gmail.com Sat Nov 17 14:59:17 2012 From: jordan.bylsma1 at gmail.com (Jordan Bylsma) Date: Sat, 17 Nov 2012 11:59:17 -0800 (PST) Subject: Python Script for Colorizing Traceroute Output (apologies for top-post) In-Reply-To: References: <6ea528f6-e4c2-4da9-a011-d81f13b83d28@googlegroups.com> Message-ID: For this case the firewalls DO respond to TTL(in most cases) and will show in a traceroute. The objective here is to colorize particular devices to easily see what type of devices traffic would traverse across the network. I would be using a database of device hostnames that when they match in traceroute they would be printed a particular color. Something like router=green, layer 3 switch yellow, and firewall red. I'm just looking for a basic script to accomplish this. Nothing too fancy. From jordan.bylsma1 at gmail.com Sat Nov 17 14:59:17 2012 From: jordan.bylsma1 at gmail.com (Jordan Bylsma) Date: Sat, 17 Nov 2012 11:59:17 -0800 (PST) Subject: Python Script for Colorizing Traceroute Output (apologies for top-post) In-Reply-To: References: <6ea528f6-e4c2-4da9-a011-d81f13b83d28@googlegroups.com> Message-ID: For this case the firewalls DO respond to TTL(in most cases) and will show in a traceroute. The objective here is to colorize particular devices to easily see what type of devices traffic would traverse across the network. I would be using a database of device hostnames that when they match in traceroute they would be printed a particular color. Something like router=green, layer 3 switch yellow, and firewall red. I'm just looking for a basic script to accomplish this. Nothing too fancy. From rosuav at gmail.com Sat Nov 17 17:00:10 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 18 Nov 2012 09:00:10 +1100 Subject: Python Script for Colorizing Traceroute Output (apologies for top-post) In-Reply-To: References: <6ea528f6-e4c2-4da9-a011-d81f13b83d28@googlegroups.com> Message-ID: On Sun, Nov 18, 2012 at 6:59 AM, Jordan Bylsma wrote: > For this case the firewalls DO respond to TTL(in most cases) and will show in a traceroute. The objective here is to colorize particular devices to easily see what type of devices traffic would traverse across the network. I would be using a database of device hostnames that when they match in traceroute they would be printed a particular color. Something like router=green, layer 3 switch yellow, and firewall red. I'm just looking for a basic script to accomplish this. Nothing too fancy. Sounds like the previously-mentioned Generic Colo(u)?ri(s|z)er (and yes, its README says exactly that) could do that for you. You'd need to craft a regexp to identify each hostname set, rather than using a database, but it shouldn't be difficult to write a script to create grc's config file. I've never used the program, though, so I have no idea how good it is. All I've done is download the tar.gz and glance over a few bits (the licence, mainly - which is mostly-GPL). By the way, you may wish to consider identifying your devices by IP address rather than hostname. If your IPs are stable and readable, you could save yourself the delays of reverse DNS lookups by simply colorizing the IPs. ChrisA From steve+comp.lang.python at pearwood.info Sat Nov 17 20:18:39 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 18 Nov 2012 01:18:39 GMT Subject: Python Script for Colorizing Traceroute Output (apologies for top-post) References: <6ea528f6-e4c2-4da9-a011-d81f13b83d28@googlegroups.com> Message-ID: <50a8376f$0$29978$c3e8da3$5496439d@news.astraweb.com> On Sun, 18 Nov 2012 09:00:10 +1100, Chris Angelico wrote: [...] > I've never used the program, though, so I have no idea how good it is. > All I've done is download the tar.gz and glance over a few bits (the > licence, mainly - which is mostly-GPL). "Mostly" GPL? You mean "not GPL". I really wish people wouldn't invent their own licences. It adds complexity and confusion to the licencing space. Unless you're a lawyer, and sometimes even if you are a lawyer, any modification you make to an established licence could be invalid, or possibly even invalidate the entire licence. Licence proliferation hurts us all. Just say No. http://en.wikipedia.org/wiki/License_proliferation -- Steven From rosuav at gmail.com Sat Nov 17 20:27:02 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 18 Nov 2012 12:27:02 +1100 Subject: Python Script for Colorizing Traceroute Output (apologies for top-post) In-Reply-To: <50a8376f$0$29978$c3e8da3$5496439d@news.astraweb.com> References: <6ea528f6-e4c2-4da9-a011-d81f13b83d28@googlegroups.com> <50a8376f$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, Nov 18, 2012 at 12:18 PM, Steven D'Aprano wrote: > On Sun, 18 Nov 2012 09:00:10 +1100, Chris Angelico wrote: > > [...] >> I've never used the program, though, so I have no idea how good it is. >> All I've done is download the tar.gz and glance over a few bits (the >> licence, mainly - which is mostly-GPL). > > "Mostly" GPL? You mean "not GPL". > > I really wish people wouldn't invent their own licences. It adds > complexity and confusion to the licencing space. Unless you're a lawyer, > and sometimes even if you are a lawyer, any modification you make to an > established licence could be invalid, or possibly even invalidate the > entire licence. > > Licence proliferation hurts us all. Just say No. > > http://en.wikipedia.org/wiki/License_proliferation The wording in the file does permit you to use it under the exact terms of the GPL (though no version is specified). But I agree, we should stick to a handful of well-known licenses and be done with it. I like the BSD two-clause, myself, less wordy than the GPL. ChrisA From steve+comp.lang.python at pearwood.info Sat Nov 17 20:30:41 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 18 Nov 2012 01:30:41 GMT Subject: module trace and counts References: <20121117092502.12c53a8f48072cd249108b47@lavabit.com> Message-ID: <50a83a40$0$29978$c3e8da3$5496439d@news.astraweb.com> On Sat, 17 Nov 2012 13:37:23 -0500, Dave Angel wrote: > On 11/17/2012 12:25 PM, rh wrote: >> Is it for or range that is executed 8000 times when I for i in >> range(3,8000,2): > Nothing is executed 8000 times. I figure it at 3998 times. Off by one. py> len(range(3, 8000, 2)) 3999 -- Steven From d at davea.name Sat Nov 17 21:54:31 2012 From: d at davea.name (Dave Angel) Date: Sat, 17 Nov 2012 21:54:31 -0500 Subject: module trace and counts In-Reply-To: <50a83a40$0$29978$c3e8da3$5496439d@news.astraweb.com> References: <20121117092502.12c53a8f48072cd249108b47@lavabit.com> <50a83a40$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50A84DE7.3090009@davea.name> On 11/17/2012 08:30 PM, Steven D'Aprano wrote: > On Sat, 17 Nov 2012 13:37:23 -0500, Dave Angel wrote: > >> On 11/17/2012 12:25 PM, rh wrote: >>> Is it for or range that is executed 8000 times when I for i in >>> range(3,8000,2): >> Nothing is executed 8000 times. I figure it at 3998 times. > Off by one. > > py> len(range(3, 8000, 2)) > 3999 > > I should have actually let Python figure it. Instead I knocked off one because the interval is half-open, and another because it starts at 3. Then I checked it by subtracting the first from the last and dividing by 2 ( (7999-3)/2 ), but then made the stupid mistake of not adding one because my interval WAS closed. -- DaveA From leonardo.s.comm at gmail.com Sun Nov 18 00:00:38 2012 From: leonardo.s.comm at gmail.com (leonardo) Date: Sat, 17 Nov 2012 21:00:38 -0800 (PST) Subject: [OFF] Convert GMap Encoded Polyline string to a set of coordinates Message-ID: Hi, How to convert GMap Encoded Polyline string to a set of coordinates ? I need it to calculate the distance between a coordinate and a line in a Django app. Thanks, From dieter at handshake.de Sun Nov 18 02:06:12 2012 From: dieter at handshake.de (Dieter Maurer) Date: Sun, 18 Nov 2012 08:06:12 +0100 Subject: error importing smtplib References: Message-ID: <87ehjr8k63.fsf@handshake.de> Eric Frederich writes: > ... > So.... I'm guessing the problem is that after I log in, the process has a > conflicting libssl.so file loaded. > Then when I try to import smtplib it tries getting things from there and > that is where the errors are coming from. > > The question now is how do I fix this? Likely, you must relink the shared object containing your "FOO_login". When its current version was linked, the (really) old "libssl" has been current and the version was linked against it. As the binary objects for your shared object might depend on the old version, it is best, to not only relink but to recompile it as well. From abhi.forall at gmail.com Sun Nov 18 03:27:22 2012 From: abhi.forall at gmail.com (Abhijeet Mahagaonkar) Date: Sun, 18 Nov 2012 03:27:22 -0500 Subject: Question on Socket Timeouts Message-ID: Hi all, I'm new to network programming. I have a question. Can we set a timeout to limit how long a particular socket can read or write? I have used a settimeout() function. The settimeout() works fine as long as the client doesnt send any data for x seconds. The data that I receive in the server after accept()ing a connect() from a client I check if the client is sending any invalid data. I'm trying to ensure that a client sending invalid data constantly cannot hold the server. So is there a way of saying I want the client to use this socket for x seconds before I close it, no matter what data I receive? Thanks in advance, -Abhijeet -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Sun Nov 18 04:39:41 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 18 Nov 2012 09:39:41 GMT Subject: Python Interview Questions References: <1193768041.349129.26350@v3g2000hsg.googlegroups.com> <55443eb7-847c-4f4c-8d04-1e6b507aac00@googlegroups.com> Message-ID: <50a8acdc$0$29978$c3e8da3$5496439d@news.astraweb.com> On Sat, 17 Nov 2012 10:01:01 -0800, chinjannisha wrote: > Hi I had one doubt.. I know very little bit of python .I wanted to know > when to use list,tuple,dictionary and set? Please reply me asap Use a list when you want a list of items that should all be treated the same way: list_of_numbers = [1, 3, 5.1, 2, 6.5] total = sum(list_of_numbers) or when you need a collection of items where the order they are in is important: winners = ['george', 'susan', 'henry'] # 1st, 2nd, 3rd place print('The winner is:', winners[0]) Use a tuple when you want a collection of items that mean different things, a bit like a C struct or Pascal record: a = (23, "henry", True, 'engineer') b = (42, "natalie", False, 'manager') c = (17, "george", True, 'student') Use a dict when you need a collection of key:value mappings: config = {'name': 'fred', 'pagesize': 'A4', 'verbose': True, 'size': 18} address = {'fred': 'fred at example.com', 'sally': 'sally_smith at example.com'} if config['verbose']: print('sending email...') send_email_to(address['fred'], "Subject: Hello") Use a set when you want to represent a collection of items and the order is not important: failed = {'fred', 'tom', 'sally'} # set literal syntax in Python 3 only # use set(['fred', 'tom', 'sally']) in Python 2 if 'george' in failed: print('George, you have failed!') -- Steven From rosuav at gmail.com Sun Nov 18 06:45:43 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 18 Nov 2012 22:45:43 +1100 Subject: Point of idle curiosity Message-ID: (if you'll forgive the pun) Is IDLE named after Eric of that name, or is it pure coincidence? ChrisA From livewebcamsexs at gmail.com Sun Nov 18 06:52:47 2012 From: livewebcamsexs at gmail.com (MoneyMaker) Date: Sun, 18 Nov 2012 03:52:47 -0800 (PST) Subject: Horror Horror Horror!!!!! Message-ID: <2d2a0b98-c587-4459-9489-680b1ddc47c8@googlegroups.com> Welcome to the site of horror: http://horrorhorrorhorror.webs.com/ Join now free !!! Free Horror Movies!!!!!! Free Horror Games!!!! From artie.ziff at gmail.com Sun Nov 18 08:32:26 2012 From: artie.ziff at gmail.com (Artie Ziff) Date: Sun, 18 Nov 2012 05:32:26 -0800 Subject: xml data or other? In-Reply-To: <96b24715-cb4b-4588-844e-fc2e2f51a170@m4g2000pbd.googlegroups.com> References: <96b24715-cb4b-4588-844e-fc2e2f51a170@m4g2000pbd.googlegroups.com> Message-ID: <50A8E36A.5010606@gmail.com> On 11/9/12 5:50 AM, rusi wrote: > On Nov 9, 5:54 pm, Artie Ziff wrote: > # submit correctedinput to etree I was very grateful to get the "leg up" on getting started down that right path with my coding. Many thanks to you, rusi. I took your excellent advices and have this working. class Converter(): PREFIX = """ """ POSTFIX = "" def __init__(self, data): self.data = data self.writeXML() def writeXML(self): pattern = re.compile('') replaceStr = r'' xmlData = re.sub(pattern, replaceStr, self.data) self.dataXML = self.PREFIX + xmlData.replace("\\", "/") + self.POSTFIX ### main # input to script is directory: # sanitize trailing slash testPkgDir = sys.argv[1].rstrip('/') # Within each test package directory is doc/testcase tcDocDir = "doc/testcases" # set input dir, containing broken files tcTxtDir = os.path.join(testPkgDir, tcDocDir) # set output dir, to write proper XML files tcXmlDir = os.path.join(testPkgDir, tcDocDir + "_XML") if not os.path.exists(tcXmlDir): os.makedirs(tcXmlDir) # iterate through files in input dir for filename in os.listdir(tcTxtDir): # set filepaths filepathTXT = os.path.join(tcTxtDir, filename) base = os.path.splitext(filename)[0] fileXML = base + ".xml" filepathXML = os.path.join(tcXmlDir, fileXML) # read broken file, convert to proper XML with open(filepathTXT) as f: c = Converter(f.read()) xmlFO = open(filepathXML, 'w') # xmlFileObject xmlFO.write(c.dataXML) xmlFO.close() ### Writing XML files so to see whats happening. My plan is to keep xml data in memory and parse with xml.etree.ElementTree. Unfortunately, xml parsing fails due to angle brackets inside description tags. In particular, xml.etree.ElementTree.parse() aborts on '<' inside xml data such as the following: This testcase tests if crontab installs the cronjob and cron schedules the job correctly. <\description> ## What is right way to handle the extra angle brackets? Substitute on line-by-line basis, if that works? Or learn to write a simple stack-style parser, or recursive descent, it may be called? I am open to comments to improve my code more to be more readable, pythonic, or better. Many thanks AZ From borkintom at gmail.com Sun Nov 18 08:48:16 2012 From: borkintom at gmail.com (Tom Borkin) Date: Sun, 18 Nov 2012 09:48:16 -0400 Subject: Problem with subprocess.call and windows schtasks Message-ID: Hi, I have this code: #!\Python27\python import subprocess #subprocess.call(['SchTasks /Create /SC ONCE /TN "My Tasks" /TR "C:/Program Files/Apache Group/Apache2/htdocs/ccc/run_alert.py" /ST 07:50'], shell=True) subprocess.call(['SchTasks /Create /SC ONCE /TN "test" /TR "run_alert.py" /ST 07:50'], shell=True) With either call, I get this error: C:\Program Files\Apache Group\Apache2\htdocs\ccc>cron_alert_activity.py The system cannot find the path specified. If I remove the ", shell=True" I get this: C:\Program Files\Apache Group\Apache2\htdocs\ccc>cron_alert_activity.py C:\Program Files\Apache Group\Apache2\htdocs\ccc\cron_alert_activity.py, line 4, in subprocess.call(['SchTasks /Create /SC ONCE /TN "test" /TR "run_alert.py" /ST 07:50']) File "C:\Python27\lib\subprocess.py", line 493, in call return Popen(*popenargs, **kwargs).wait() File "C:\Python27\lib\subprocess.py", line 679, in __init__ errread, errwrite) File "C:\Python27\lib\subprocess.py", line 896, in _execute_child startupinfo) WindowsError: [Error 2] The system cannot find the file specified The file exists in said directory. I can execute it from the cmd prompt. So I tried this: pgm = "SchTasks" args = ['/Create /SC ONCE /TN "test" /TR "run_alert.py" /ST 07:50'] #args = ['/Create', '/SC ONCE', '/TN "test"', '/TR "run_alert.py"', '/ST 07:50'] cmd = [pgm] cmd.extend(args) subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] but got this error: ERROR: Invalid argument/option - <> If I use the other args list I get this error: ERROR: Invalid argument/option - '/SC ONCE' so apparently it liked the first argument. Please advise. TIA, Tom -------------- next part -------------- An HTML attachment was scrubbed... URL: From roy at panix.com Sun Nov 18 08:53:25 2012 From: roy at panix.com (Roy Smith) Date: Sun, 18 Nov 2012 08:53:25 -0500 Subject: Python Interview Questions References: <1193768041.349129.26350@v3g2000hsg.googlegroups.com> <55443eb7-847c-4f4c-8d04-1e6b507aac00@googlegroups.com> <50a8acdc$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <50a8acdc$0$29978$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > Use a list when you want a list of items that should all be treated the > same way [...] or when you need a collection of items where the order they are in is > important: > > Use a tuple when you want a collection of items that mean different > things, a bit like a C struct or Pascal record: That is certainly the right answer according to the One True Church Of Pythonic Orthodoxy And Theoretical Correctness. But, let me give an alternative answer which works for The Unwashed Masses Who Live In The Trenches And Write Python Code For A Living: Use a list when you need an ordered collection which is mutable (i.e. can be altered after being created). Use a tuple when you need an immutable list (such as for a dictionary key). From rustompmody at gmail.com Sun Nov 18 10:54:05 2012 From: rustompmody at gmail.com (rusi) Date: Sun, 18 Nov 2012 07:54:05 -0800 (PST) Subject: xml data or other? References: <96b24715-cb4b-4588-844e-fc2e2f51a170@m4g2000pbd.googlegroups.com> Message-ID: <633d181e-1337-456e-8e97-d187654c1f2a@uk1g2000pbb.googlegroups.com> On Nov 18, 6:32?pm, Artie Ziff wrote: > Unfortunately, xml parsing fails due to angle brackets inside > description tags. In particular, xml.etree.ElementTree.parse() > aborts on '<' inside xml data such as the following: > > > ? ? ? > ? ? ? ? ?This testcase tests if crontab installs the cronjob > ? ? ? ? ?and cron schedules the job correctly. > ? ? ?<\description> > > ## > > What is right way to handle the extra angle brackets? > Substitute on line-by-line basis, if that works? > Or learn to write a simple stack-style parser, or > recursive descent, it may be called? > > I am open to comments to improve my code more to be more readable, > pythonic, or better. > > Many thanks > AZ Start with cgi.escape perhaps? http://docs.python.org/2/library/cgi.html From rustompmody at gmail.com Sun Nov 18 10:58:12 2012 From: rustompmody at gmail.com (rusi) Date: Sun, 18 Nov 2012 07:58:12 -0800 (PST) Subject: xml data or other? References: <96b24715-cb4b-4588-844e-fc2e2f51a170@m4g2000pbd.googlegroups.com> <633d181e-1337-456e-8e97-d187654c1f2a@uk1g2000pbb.googlegroups.com> Message-ID: <0b611cab-bd5c-495f-885c-0372e85c1770@kt16g2000pbb.googlegroups.com> On Nov 18, 8:54?pm, rusi wrote: > Start with cgi.escape perhaps?http://docs.python.org/2/library/cgi.html This may be a better link for starters http://wiki.python.org/moin/EscapingHtml (Note the escaping xml at the bottom) From steve+comp.lang.python at pearwood.info Sun Nov 18 10:59:51 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 18 Nov 2012 15:59:51 GMT Subject: Point of idle curiosity References: Message-ID: <50a905f7$0$29978$c3e8da3$5496439d@news.astraweb.com> On Sun, 18 Nov 2012 22:45:43 +1100, Chris Angelico wrote: > (if you'll forgive the pun) > > Is IDLE named after Eric of that name, or is it pure coincidence? Well, IDLE is an IDE. The L doesn't seem to mean anything, so it's plausible that it is named after Eric Idle. -- Steven From breamoreboy at yahoo.co.uk Sun Nov 18 11:08:34 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 18 Nov 2012 16:08:34 +0000 Subject: Point of idle curiosity In-Reply-To: <50a905f7$0$29978$c3e8da3$5496439d@news.astraweb.com> References: <50a905f7$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 18/11/2012 15:59, Steven D'Aprano wrote: > On Sun, 18 Nov 2012 22:45:43 +1100, Chris Angelico wrote: > >> (if you'll forgive the pun) >> >> Is IDLE named after Eric of that name, or is it pure coincidence? > > Well, IDLE is an IDE. The L doesn't seem to mean anything, so it's > plausible that it is named after Eric Idle. > > https://en.wikipedia.org/wiki/IDLE_%28Python%29 -- Cheers. Mark Lawrence. From rosuav at gmail.com Sun Nov 18 11:13:10 2012 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 19 Nov 2012 03:13:10 +1100 Subject: Point of idle curiosity In-Reply-To: References: <50a905f7$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Nov 19, 2012 at 3:08 AM, Mark Lawrence wrote: > On 18/11/2012 15:59, Steven D'Aprano wrote: >> >> On Sun, 18 Nov 2012 22:45:43 +1100, Chris Angelico wrote: >> >>> (if you'll forgive the pun) >>> >>> Is IDLE named after Eric of that name, or is it pure coincidence? >> >> >> Well, IDLE is an IDE. The L doesn't seem to mean anything, so it's >> plausible that it is named after Eric Idle. > > https://en.wikipedia.org/wiki/IDLE_%28Python%29 > Thanks both. I went looking at the docs, but didn't find anything conclusive either way. It's sometimes hard to know just how much Monty Python there is in the language... which justified me getting a bit naughty a couple of weeks ago and almost citing the Python language in-character in my Dungeons and Dragons campaign: "There is still a cheeseshop owned by his family. It's called PyPi, though whether that's an abbreviation for something or not is not reported here." And then when they went to the place, I described it thusly: "It's all done up with a pythonic decor. Everything's nicely done up with snakes; the walls have indented blocks with no braces on them." Now I just need to find a signwriter and graphic designer named Eric Haffabee, laying around idle all the time... or maybe that's too obscure a set of references. ChrisA From mail at timgolden.me.uk Sun Nov 18 11:19:19 2012 From: mail at timgolden.me.uk (Tim Golden) Date: Sun, 18 Nov 2012 16:19:19 +0000 Subject: Problem with subprocess.call and windows schtasks In-Reply-To: References: Message-ID: <50A90A87.2000906@timgolden.me.uk> On 18/11/2012 13:48, Tom Borkin wrote: > import subprocess > #subprocess.call(['SchTasks /Create /SC ONCE /TN "My Tasks" /TR "C:/Program > Files/Apache Group/Apache2/htdocs/ccc/run_alert.py" /ST 07:50'], shell=True) > subprocess.call(['SchTasks /Create /SC ONCE /TN "test" /TR "run_alert.py" > /ST 07:50'], shell=True) > With either call, I get this error: > C:\Program Files\Apache Group\Apache2\htdocs\ccc>cron_alert_activity.py > The system cannot find the path specified. Generally, with subprocess call: * Pass each element in the command line as a separate element in the list: ['SchTasks', '/Create', '/SC', 'ONCE', ... &c.] * Don't use shell=True unless you're running a command which is internal to cmd.exe (such as dir, copy, etc.). If you're running anything which exists as its own .exe, you shouldn't be using shell=True. TJG From clp2 at rebertia.com Sun Nov 18 11:39:27 2012 From: clp2 at rebertia.com (Chris Rebert) Date: Sun, 18 Nov 2012 08:39:27 -0800 Subject: Problem with subprocess.call and windows schtasks In-Reply-To: References: Message-ID: On Sun, Nov 18, 2012 at 5:48 AM, Tom Borkin wrote: > Hi, > I have this code: > > #!\Python27\python > > import subprocess > #subprocess.call(['SchTasks /Create /SC ONCE /TN "My Tasks" /TR "C:/Program > Files/Apache Group/Apache2/htdocs/ccc/run_alert.py" /ST 07:50'], shell=True) > subprocess.call(['SchTasks /Create /SC ONCE /TN "test" /TR "run_alert.py" > /ST 07:50'], shell=True) > With either call, I get this error: > C:\Program Files\Apache Group\Apache2\htdocs\ccc>cron_alert_activity.py > The system cannot find the path specified. > > If I remove the ", shell=True" I get this: > C:\Program Files\Apache Group\Apache2\htdocs\ccc>cron_alert_activity.py > C:\Program Files\Apache Group\Apache2\htdocs\ccc\cron_alert_activity.py, > line 4, in > subprocess.call(['SchTasks /Create /SC ONCE /TN "test" /TR "run_alert.py" > /ST 07:50']) > File "C:\Python27\lib\subprocess.py", line 493, in call > return Popen(*popenargs, **kwargs).wait() > File "C:\Python27\lib\subprocess.py", line 679, in __init__ errread, > errwrite) > File "C:\Python27\lib\subprocess.py", line 896, in _execute_child > startupinfo) > WindowsError: [Error 2] The system cannot find the file specified > The file exists in said directory. I can execute it from the cmd prompt. Per the docs (http://docs.python.org/2/library/subprocess.html#frequently-used-arguments ): "If passing a single string [as the `args` argument], either `shell` must be True (see below) or else the string must simply name the program to be executed **without specifying any arguments.**" (emphasis mine) > So I tried this: > pgm = "SchTasks" > args = ['/Create /SC ONCE /TN "test" /TR "run_alert.py" /ST 07:50'] > #args = ['/Create', '/SC ONCE', '/TN "test"', '/TR "run_alert.py"', '/ST > 07:50'] > cmd = [pgm] > cmd.extend(args) > subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] > but got this error: > ERROR: Invalid argument/option - <> > > If I use the other args list I get this error: > ERROR: Invalid argument/option - '/SC ONCE' > so apparently it liked the first argument. > > Please advise. Your tokenization of your command is incorrect. Consult the Note box in the docs regarding `args` tokenization, and apply it to your command: http://docs.python.org/2/library/subprocess.html#subprocess.Popen The-docs-are-your-friend-ly Yours, Chris From steve+comp.lang.python at pearwood.info Sun Nov 18 11:50:52 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 18 Nov 2012 16:50:52 GMT Subject: Python Interview Questions References: <1193768041.349129.26350@v3g2000hsg.googlegroups.com> <55443eb7-847c-4f4c-8d04-1e6b507aac00@googlegroups.com> <50a8acdc$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50a911ec$0$29978$c3e8da3$5496439d@news.astraweb.com> On Sun, 18 Nov 2012 08:53:25 -0500, Roy Smith wrote: > In article <50a8acdc$0$29978$c3e8da3$5496439d at news.astraweb.com>, > Steven D'Aprano wrote: > >> Use a list when you want a list of items that should all be treated the >> same way [...] or when you need a collection of items where the order >> they are in is important: >> >> Use a tuple when you want a collection of items that mean different >> things, a bit like a C struct or Pascal record: > > That is certainly the right answer according to the One True Church Of > Pythonic Orthodoxy And Theoretical Correctness. Oh I'm sorry, did something I say suggest that the couple of examples I gave are the *only* acceptable uses? My apologies for not giving an exhaustive list of every possible use of lists and tuples, I'll be sure to punish myself severely for the lapse. > But, let me give an > alternative answer which works for The Unwashed Masses Who Live In The > Trenches And Write Python Code For A Living: > > Use a list when you need an ordered collection which is mutable (i.e. > can be altered after being created). Use a tuple when you need an > immutable list (such as for a dictionary key). I keep hearing about this last one, but I wonder... who *actually* does this? I've created many, many lists over the years -- lists of names, lists of phone numbers, lists of directory search paths, all sorts of things. I've never needed to use one as a dictionary key. Under what sort of circumstances would somebody want to take a mutable list of data, say a list of email addresses, freeze it into a known state, and use that frozen state as a key in a dict? What would be the point? Even if there was some meaningful reason to look up "this list of 12000 email addresses" as a single key, it is going to get out of sync with the actual mutable list. Sure, I have built a collection of items as a list, because lists are mutable, then frozen it into a tuple, and *thrown the list away*, then used the tuple as a key. But that's not the same thing, the intent is different. In my case, the data was never intended to be a list, it was always intended to be a fixed record-like collection, the use of list was as a temporary data structure used for construction. A bit like the idiom of ''.join(some_list). But I can't think of any meaningful, non-contrived example where I might want an actual mutable list of values as a dict key. -- Steven From darcy at druid.net Sun Nov 18 12:16:14 2012 From: darcy at druid.net (D'Arcy J.M. Cain) Date: Sun, 18 Nov 2012 12:16:14 -0500 Subject: Python Interview Questions In-Reply-To: <50a911ec$0$29978$c3e8da3$5496439d@news.astraweb.com> References: <1193768041.349129.26350@v3g2000hsg.googlegroups.com> <55443eb7-847c-4f4c-8d04-1e6b507aac00@googlegroups.com> <50a8acdc$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a911ec$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: <20121118121614.24d2416d@dilbert> On 18 Nov 2012 16:50:52 GMT Steven D'Aprano wrote: > On Sun, 18 Nov 2012 08:53:25 -0500, Roy Smith wrote: >> > Use a list when you need an ordered collection which is mutable > > (i.e. can be altered after being created). Use a tuple when you > > need an immutable list (such as for a dictionary key). > > I keep hearing about this last one, but I wonder... who *actually* > does this? I've created many, many lists over the years -- lists of > names, lists of phone numbers, lists of directory search paths, all > sorts of things. I've never needed to use one as a dictionary key. Well, as long as *you* never needed it then... CellBlock = 9 # There's a riot going on... Cell = 17 Bunk = "top" Prisoner = {(CellBlock, Cell, Bunk): "Bernie Madoff"} -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner. IM: darcy at Vex.Net From roy at panix.com Sun Nov 18 12:53:50 2012 From: roy at panix.com (Roy Smith) Date: Sun, 18 Nov 2012 12:53:50 -0500 Subject: Python Interview Questions References: <1193768041.349129.26350@v3g2000hsg.googlegroups.com> <55443eb7-847c-4f4c-8d04-1e6b507aac00@googlegroups.com> <50a8acdc$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a911ec$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <50a911ec$0$29978$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > Oh I'm sorry, did something I say suggest that the couple of examples I > gave are the *only* acceptable uses? My apologies for not giving an > exhaustive list of every possible use of lists and tuples, I'll be sure > to punish myself severely for the lapse. Hmmm. I didn't mean any offense. I was just pointing out that what's true in theory and what's true in practice aren't always the same. > Under what sort of circumstances would somebody want to take a mutable > list of data, say a list of email addresses, freeze it into a known > state, and use that frozen state as a key in a dict? I've got a script which trolls our log files looking for python stack dumps. For each dump it finds, it computes a signature (basically, a call sequence which led to the exception) and uses this signature as a dictionary key. Here's the relevant code (abstracted slightly for readability): def main(args): crashes = {} [...] for line in open(log_file): if does_not_look_like_a_stack_dump(line): continue lines = traceback_helper.unfold(line) header, stack = traceback_helper.extract_stack(lines) signature = tuple(stack) if signature in crashes: count, header = crashes[signature] crashes[signature] = (count + 1, header) else: crashes[signature] = (1, header) You can find traceback_helper at https://bitbucket.org/roysmith/python-tools/src/4f8118d175ed/logs/traceba ck_helper.py The stack that's returned is a list. It's inherently a list, per the classic definition: * It's variable length. Different stacks have different depths. * It's homogeneous. There's nothing particularly significant about each entry other than it's the next one in the stack. * It's mutable. I can build it up one item at a time as I discover them. * It's ordered. f1(f2()) is not the same as f2(f1()). But, to use it as a dictionary key, I need to make it into a tuple, since keys have to be immutable. From tjreedy at udel.edu Sun Nov 18 14:27:44 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 18 Nov 2012 14:27:44 -0500 Subject: Point of idle curiosity In-Reply-To: References: Message-ID: On 11/18/2012 6:45 AM, Chris Angelico wrote: > (if you'll forgive the pun) > > Is IDLE named after Eric of that name, or is it pure coincidence? Officially, I believe it is I(ntegrated) D(eve)L(opement) E(nvironment). However, now that you mention it, I would not be surprised if Eric was in the back of however's mind. -- Terry Jan Reedy From tjreedy at udel.edu Sun Nov 18 14:31:57 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 18 Nov 2012 14:31:57 -0500 Subject: Spam source (Re: Horror Horror Horror!!!!!) In-Reply-To: <2d2a0b98-c587-4459-9489-680b1ddc47c8@googlegroups.com> References: <2d2a0b98-c587-4459-9489-680b1ddc47c8@googlegroups.com> Message-ID: The question was raised as to how much spam comes from googlegroups. Not all, but more that half, I believe. This one does. From: MoneyMaker ... Message-ID: <2d2a0b98-c587-4459-9489-680b1ddc47c8 at googlegroups.com> -- Terry Jan Reedy From rosuav at gmail.com Sun Nov 18 15:02:49 2012 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 19 Nov 2012 07:02:49 +1100 Subject: Python Interview Questions In-Reply-To: <20121118121614.24d2416d@dilbert> References: <1193768041.349129.26350@v3g2000hsg.googlegroups.com> <55443eb7-847c-4f4c-8d04-1e6b507aac00@googlegroups.com> <50a8acdc$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a911ec$0$29978$c3e8da3$5496439d@news.astraweb.com> <20121118121614.24d2416d@dilbert> Message-ID: On Mon, Nov 19, 2012 at 4:16 AM, D'Arcy J.M. Cain wrote: > On 18 Nov 2012 16:50:52 GMT > Steven D'Aprano wrote: >> On Sun, 18 Nov 2012 08:53:25 -0500, Roy Smith wrote: >>> > Use a list when you need an ordered collection which is mutable >> > (i.e. can be altered after being created). Use a tuple when you >> > need an immutable list (such as for a dictionary key). >> >> I keep hearing about this last one, but I wonder... who *actually* >> does this? I've created many, many lists over the years -- lists of >> names, lists of phone numbers, lists of directory search paths, all >> sorts of things. I've never needed to use one as a dictionary key. > > Well, as long as *you* never needed it then... > > CellBlock = 9 # There's a riot going on... > Cell = 17 > Bunk = "top" > > Prisoner = {(CellBlock, Cell, Bunk): "Bernie Madoff"} That's a structure, not a list. Every key will consist of precisely three values: two integers and one keyword string. Already covered by a previous example. ChrisA From jkn_gg at nicorp.f9.co.uk Sun Nov 18 18:17:48 2012 From: jkn_gg at nicorp.f9.co.uk (jkn) Date: Sun, 18 Nov 2012 15:17:48 -0800 (PST) Subject: how to simulate tar filename substitution across piped subprocess.Popen() calls? In-Reply-To: <50a354dd$0$6982$e4fe514c@news2.news.xs4all.nl> References: <509c588a$0$6904$e4fe514c@news2.news.xs4all.nl> <5b39febc-fbb9-4d8e-b331-0401e78d2b7f@m13g2000vbd.googlegroups.com> <50a12560$0$6972$e4fe514c@news2.news.xs4all.nl> <50a1402d$0$6947$e4fe514c@news2.news.xs4all.nl> <50a354dd$0$6982$e4fe514c@news2.news.xs4all.nl> Message-ID: Hi Hans [...] > > > However, once he does that, it's simpler to cut out xargs and invoke > > "sh" directly. Or even cut out "sh" and "test" and instead use > > os.path.isfile and then call md5sum directly. And once he does that, > > he no longer needs to worry about single quotes. > Yes indeed, using os.path.isfile() and them md5sum directly is my plan ... for reasons of maintainability (by myself) more than anything else. > > > The OP has said, he's going to d all that. One step at a time. > > That sounds like a sensible plan to me. > Thanks a lot. J^n From martien.friedeman at gmail.com Sun Nov 18 18:44:59 2012 From: martien.friedeman at gmail.com (martien.friedeman at gmail.com) Date: Sun, 18 Nov 2012 15:44:59 -0800 (PST) Subject: Testers please for CodeInvestigator Message-ID: <75d58c10-bb60-49ee-9e7f-91838377a959@googlegroups.com> I have released version 3 of CodeInvestigator. It is a web-application that allows you to visualize the flow of data through your program. It is intended as a way to easily learn about how your program does its thing. Softoxi has done a review of the previous version: http://www.softoxi.com/codeinvestigator.html Version 3.0.0 can be downloaded from sourceforge: http://sourceforge.net/project/showfiles.php?group_id=183942 I would be interested in bugs and things that you think should be included. The 'wouldn't it be nice if ...' features. Thank you! Martien Friedeman From steve+comp.lang.python at pearwood.info Sun Nov 18 19:31:28 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 19 Nov 2012 00:31:28 GMT Subject: Python Interview Questions References: <1193768041.349129.26350@v3g2000hsg.googlegroups.com> <55443eb7-847c-4f4c-8d04-1e6b507aac00@googlegroups.com> <50a8acdc$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a911ec$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50a97de0$0$29983$c3e8da3$5496439d@news.astraweb.com> On Sun, 18 Nov 2012 12:53:50 -0500, Roy Smith wrote: > I've got a script which trolls our log files looking for python stack > dumps. For each dump it finds, it computes a signature (basically, a > call sequence which led to the exception) and uses this signature as a > dictionary key. Here's the relevant code (abstracted slightly for > readability): > > def main(args): > crashes = {} > [...] > for line in open(log_file): > if does_not_look_like_a_stack_dump(line): > continue > lines = traceback_helper.unfold(line) > header, stack = traceback_helper.extract_stack(lines) > signature = tuple(stack) > if signature in crashes: > count, header = crashes[signature] > crashes[signature] = (count + 1, header) > else: > crashes[signature] = (1, header) > > You can find traceback_helper at > https://bitbucket.org/roysmith/python-tools/src/4f8118d175ed/logs/ > traceback_helper.py > > The stack that's returned is a list. It's inherently a list, per the > classic definition: Er, no, it's inherently a blob of multiple text lines. Sure, you've built it a line at a time by using a list, but I've already covered that case. Once you've identified a stack, you never append to it, sort it, delete lines in the middle of it... none of these list operations are meaningful for a Python stack trace. The stack becomes a fixed string, and not just because you use it as a dict key, but because inherently it counts as a single, immutable blob of lines. A tuple of individual lines is one reasonable data structure for a blob of lines. Another would be a single string: signature = '\n'.join(stack) Depending on what you plan to do with the signatures, one or the other implementation might be better. I'm sure that there are other data structures as well. > * It's variable length. Different stacks have different depths. Once complete, the stack trace is fixed length, but that fixed length is different from one stack to the next. Deleting a line would make it incomplete, and adding a line would make it invalid. > * It's homogeneous. There's nothing particularly significant about each > entry other than it's the next one in the stack. > > * It's mutable. I can build it up one item at a time as I discover > them. The complete stack trace is inhomogeneous and immutable. I've already covered immutability above: removing, adding or moving lines will invalidate the stack trace. Inhomogeneity comes from the structure of a stack trace. The mere fact that each line is a string does not mean that any two lines are equivalent. Different lines represent different things. Traceback (most recent call last): File "./prattle.py", line 873, in select selection = self.do_callback(cb, response) File "./prattle.py", line 787, in do_callback raise callback ValueError: what do you mean? is a valid stack. But: Traceback (most recent call last): raise callback selection = self.do_callback(cb, response) File "./prattle.py", line 787, in do_callback ValueError: what do you mean? File "./prattle.py", line 873, in select is not. A stack trace has structure. The equivalent here is the difference between: ages = [23, 42, 19, 67, # age, age, age, age 17, 94, 32, 51, # ... ] values = [23, 1972, 1, 34500, # age, year, number of children, income 35, 1985, 0, 67900, # age, year, number of children, income ] A stack trace is closer to the second example than the first: each item may be the same type, but the items don't represent the same *kind of thing*. You could make a stack trace homogeneous with a little work: - drop the Traceback line and the final exception line; - parse the File lines to extract the useful fields; - combine them with the source code. Now you have a blob of homogeneous records, here shown as lines of text with ! as field separator: ./prattle.py ! 873 ! select ! selection = self.do_callback(cb, response) ./prattle.py ! 787 ! do_callback ! raise callback But there's really nothing you can do about the immutability. There isn't any meaningful reason why you might want to take a complete stack trace and add or delete lines from it. -- Steven From robertmiles at teranews.com Sun Nov 18 20:14:08 2012 From: robertmiles at teranews.com (Robert Miles) Date: Sun, 18 Nov 2012 19:14:08 -0600 Subject: Unpaking Tuple In-Reply-To: References: <801f0e2c-7d1d-4e91-bec5-78c5e53a70ec@googlegroups.com> Message-ID: <1Kfqs.6517$fF5.3545@newsfe01.iad> On 10/9/2012 1:07 AM, Bob Martin wrote: > in 682592 20121008 232126 "Prasad, Ramit" wrote: >> Thomas Bach wrote:=0D=0A> Hi there,=0D=0A> =0D=0A> On Sat, Oct 06, 2012 at = >> 03:08:38PM +0000, Steven D'Aprano wrote:=0D=0A> >=0D=0A> > my_tuple =3D my_= >> tuple[:4]=0D=0A> > a,b,c,d =3D my_tuple if len(my_tuple) =3D=3D 4 else (my_= >> tuple + (None,)*4)[:4]=0D=0A> >=0D=0A> =0D=0A> Are you sure this works as y= >> ou expect? I just stumbled over the following:=0D=0A> =0D=0A> $ python=0D= >> =0A> Python 3=2E2=2E3 (default, Jun 25 2012, 23:10:56)=0D=0A> [GCC 4=2E7=2E= >> 1] on linux2=0D=0A> Type "help", "copyright", "credits" or "license" for mo= >> re information=2E=0D=0A> >>> split =3D ['foo', 'bar']=0D=0A> >>> head, tail= >> =3D split if len(split) =3D=3D 2 else split[0], None=0D=0A> >>> head=0D=0A= >>> ['foo', 'bar']=0D=0A> >>> tail=0D=0A> >>>=0D=0A> =0D=0A> I don't get it! = >> Could someone help me, please? Why is head not 'foo'=0D=0A> and tail not 'b= >> ar'?=0D=0A> =0D=0A> Regards,=0D=0A> Thomas=0D=0A> --=0D=0A=0D=0AI think yo= >> u just need to wrap the else in parenthesis so the=0D=0Aelse clause is trea= >> ted as a tuple=2E Without the parenthesis =0D=0AI believe it is grouping th= >> e code like this=2E=0D=0A=0D=0Ahead, tail =3D (split if len(split) =3D=3D 2= >> else split[0] ), None=0D=0A=0D=0AYou want:=0D=0Ahead, tail =3D split if le= >> n(split) =3D=3D 2 else (split[0], None )=0D=0A=0D=0A=0D=0ARamit=0D=0AThis e= >> mail is confidential and subject to important disclaimers and=0D=0Aconditio= >> ns including on offers for the purchase or sale of=0D=0Asecurities, accurac= >> y and completeness of information, viruses,=0D=0Aconfidentiality, legal pri= >> vilege, and legal entity disclaimers,=0D=0Aavailable at http://www=2Ejpmorg= >> an=2Ecom/pages/disclosures/email=2E > > How does one unpack this post? ;-) There are a number of programs for converting ends of lines between Linux format, Windows format, and Mac formats. You could try running all of those programs your operating system provides on that text, then checking which one of them gives the most readable results. From robertmilesxyz at gmail.com Sun Nov 18 20:36:37 2012 From: robertmilesxyz at gmail.com (Robert Miles) Date: Sun, 18 Nov 2012 17:36:37 -0800 (PST) Subject: Spam source (Re: Horror Horror Horror!!!!!) In-Reply-To: References: <2d2a0b98-c587-4459-9489-680b1ddc47c8@googlegroups.com> Message-ID: <8ecb92a8-9f91-4ec7-9127-64af1315a766@googlegroups.com> On Sunday, November 18, 2012 1:35:00 PM UTC-6, Terry Reedy wrote: > The question was raised as to how much spam comes from googlegroups. > > Not all, but more that half, I believe. This one does. > > > > From: MoneyMaker > > ... > > Message-ID: <2d2a0b98-c587-4459-9489-680b1ddc47c8 at googlegroups.com> > > > > -- > > Terry Jan Reedy That depends on your definition of spam. This one does not appear to be trying to sell anything, and therefore does not meet some of the stricter definitions. Definitely off-topic, though. From robertmilesxyz at gmail.com Sun Nov 18 20:36:37 2012 From: robertmilesxyz at gmail.com (Robert Miles) Date: Sun, 18 Nov 2012 17:36:37 -0800 (PST) Subject: Spam source (Re: Horror Horror Horror!!!!!) In-Reply-To: References: <2d2a0b98-c587-4459-9489-680b1ddc47c8@googlegroups.com> Message-ID: <8ecb92a8-9f91-4ec7-9127-64af1315a766@googlegroups.com> On Sunday, November 18, 2012 1:35:00 PM UTC-6, Terry Reedy wrote: > The question was raised as to how much spam comes from googlegroups. > > Not all, but more that half, I believe. This one does. > > > > From: MoneyMaker > > ... > > Message-ID: <2d2a0b98-c587-4459-9489-680b1ddc47c8 at googlegroups.com> > > > > -- > > Terry Jan Reedy That depends on your definition of spam. This one does not appear to be trying to sell anything, and therefore does not meet some of the stricter definitions. Definitely off-topic, though. From 129km09 at gmail.com Sun Nov 18 20:52:35 2012 From: 129km09 at gmail.com (su29090) Date: Sun, 18 Nov 2012 17:52:35 -0800 (PST) Subject: Problems on these two questions Message-ID: <400c615f-4d93-4b8b-85ec-1bb4c47e30fe@googlegroups.com> I all of the other problems but I have issues with these: 1.Given a positive integer n , assign True to is_prime if n has no factors other than 1 and itself. (Remember, m is a factor of n if m divides n evenly.) 2.An arithmetic progression is a sequence of numbers in which the distance (or difference) between any two successive numbers if the same. This in the sequence 1, 3, 5, 7, ... , the distance is 2 while in the sequence 6, 12, 18, 24, ... , the distance is 6. Given the positive integer distance and the positive integer n , associate the variable sum with the sum of the elements of the arithmetic progression from 1 to n with distance distance . For example, if distance is 2 and n is 10 , then sum would be associated with 26 because 1+3+5+7+9 = 25 . Thanks in advance. From hansmu at xs4all.nl Sun Nov 18 20:56:29 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Mon, 19 Nov 2012 02:56:29 +0100 Subject: Unpaking Tuple In-Reply-To: References: <801f0e2c-7d1d-4e91-bec5-78c5e53a70ec@googlegroups.com> Message-ID: <50a991cd$0$6861$e4fe514c@news2.news.xs4all.nl> On 9/10/12 08:07:32, Bob Martin wrote: > in 682592 20121008 232126 "Prasad, Ramit" wrote: >> Thomas Bach wrote:=0D=0A> Hi there,=0D=0A> =0D=0A> On Sat, Oct 06, 2012 at = >> 03:08:38PM +0000, Steven D'Aprano wrote:=0D=0A> >=0D=0A> > my_tuple =3D my_= >> tuple[:4]=0D=0A> > a,b,c,d =3D my_tuple if len(my_tuple) =3D=3D 4 else (my_= >> tuple + (None,)*4)[:4]=0D=0A> >=0D=0A> =0D=0A> Are you sure this works as y= >> ou expect? I just stumbled over the following:=0D=0A> =0D=0A> $ python=0D= >> =0A> Python 3=2E2=2E3 (default, Jun 25 2012, 23:10:56)=0D=0A> [GCC 4=2E7=2E= >> 1] on linux2=0D=0A> Type "help", "copyright", "credits" or "license" for mo= >> re information=2E=0D=0A> >>> split =3D ['foo', 'bar']=0D=0A> >>> head, tail= >> =3D split if len(split) =3D=3D 2 else split[0], None=0D=0A> >>> head=0D=0A= >>> ['foo', 'bar']=0D=0A> >>> tail=0D=0A> >>>=0D=0A> =0D=0A> I don't get it! = >> Could someone help me, please? Why is head not 'foo'=0D=0A> and tail not 'b= >> ar'?=0D=0A> =0D=0A> Regards,=0D=0A> Thomas=0D=0A> --=0D=0A=0D=0AI think yo= >> u just need to wrap the else in parenthesis so the=0D=0Aelse clause is trea= >> ted as a tuple=2E Without the parenthesis =0D=0AI believe it is grouping th= >> e code like this=2E=0D=0A=0D=0Ahead, tail =3D (split if len(split) =3D=3D 2= >> else split[0] ), None=0D=0A=0D=0AYou want:=0D=0Ahead, tail =3D split if le= >> n(split) =3D=3D 2 else (split[0], None )=0D=0A=0D=0A=0D=0ARamit=0D=0AThis e= >> mail is confidential and subject to important disclaimers and=0D=0Aconditio= >> ns including on offers for the purchase or sale of=0D=0Asecurities, accurac= >> y and completeness of information, viruses,=0D=0Aconfidentiality, legal pri= >> vilege, and legal entity disclaimers,=0D=0Aavailable at http://www=2Ejpmorg= >> an=2Ecom/pages/disclosures/email=2E > > How does one unpack this post? ;-) How about: print re.sub('^>* ', '', this_post, flags=re.M).decode('quopri') Hope this helps, -- HansM From roy at panix.com Sun Nov 18 21:09:36 2012 From: roy at panix.com (Roy Smith) Date: Sun, 18 Nov 2012 21:09:36 -0500 Subject: Python Interview Questions References: <1193768041.349129.26350@v3g2000hsg.googlegroups.com> <55443eb7-847c-4f4c-8d04-1e6b507aac00@googlegroups.com> <50a8acdc$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a911ec$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a97de0$0$29983$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <50a97de0$0$29983$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > > The stack that's returned is a list. It's inherently a list, per the > > classic definition: > > Er, no, it's inherently a blob of multiple text lines. No, it's a list that looks like (taken from the doc string of the code I referenced): [('/usr/lib/.../base.py', 'get_response'), ('/home/songza/.../views.py', 'song_info'), ('/home/songza.../api.py', 'get_song'), ('/home/songza/.../api.py', 'api')] [it doesn't really have ...'s in the paths; I just elided some text to make it easier to read] > > * It's homogeneous. There's nothing particularly significant about each > > entry other than it's the next one in the stack. > > The complete stack trace is inhomogeneous and immutable. I've already > covered immutability above: removing, adding or moving lines will > invalidate the stack trace. Inhomogeneity comes from the structure of a > stack trace. The mere fact that each line is a string does not mean that > any two lines are equivalent. Different lines represent different things. No. Each entry in the list represents a source file and a function name. They're all the same "shape". You could remove one or add another one, or shuffle the order, and you would have something which was syntactically correct and semantically meaningful (even if it didn't represent an actual code path. > - drop the Traceback line and the final exception line; > - parse the File lines to extract the useful fields; > - combine them with the source code. You mean, kind of like the code I cited does? :-) I think we're going to have to let this be. You obviously have your concept of what a tuple is and what a list is. I disagree. I don't think either of us is right or wrong, we just have different ways of thinking about things. You come at it from a theoretical point of view. You think of each type as an embodiment of certain concepts ("it represents a fixed-length heterogenous sequence"). Your thinking is driven by what each type was intended to be used for. I come at it from a practical point of view. To me, each type is a collection of methods. I have certain operations I need to perform. I pick the type which offers those operations. If the set of operations I need to perform (in this case, {append, hash}) don't exist in a single type, I'm forced to use both types and convert from one to the other as needed. The theorist understands that a chisel and a screwdriver were intended for different purposes, but the pragmatist gets the paint can open. From breamoreboy at yahoo.co.uk Sun Nov 18 21:09:48 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 19 Nov 2012 02:09:48 +0000 Subject: Problems on these two questions In-Reply-To: <400c615f-4d93-4b8b-85ec-1bb4c47e30fe@googlegroups.com> References: <400c615f-4d93-4b8b-85ec-1bb4c47e30fe@googlegroups.com> Message-ID: On 19/11/2012 01:52, su29090 wrote: > > I all of the other problems but I have issues with these: > > 1.Given a positive integer n , assign True to is_prime if n has no factors other than 1 and itself. (Remember, m is a factor of n if m divides n evenly.) > > 2.An arithmetic progression is a sequence of numbers in which the distance (or difference) between any two successive numbers if the same. This in the sequence 1, 3, 5, 7, ... , the distance is 2 while in the sequence 6, 12, 18, 24, ... , the distance is 6. > > Given the positive integer distance and the positive integer n , associate the variable sum with the sum of the elements of the arithmetic progression from 1 to n with distance distance . For example, if distance is 2 and n is 10 , then sum would be associated with 26 because 1+3+5+7+9 = 25 . > > Thanks in advance. > Please specify what programming language and OS you're using, what code you've tried so far, and what problems if any you're having. If you're using Python please give us the version, if not please try another mailing list. -- Cheers. Mark Lawrence. From 129km09 at gmail.com Sun Nov 18 21:15:40 2012 From: 129km09 at gmail.com (su29090) Date: Sun, 18 Nov 2012 18:15:40 -0800 (PST) Subject: Problems on these two questions In-Reply-To: <400c615f-4d93-4b8b-85ec-1bb4c47e30fe@googlegroups.com> References: <400c615f-4d93-4b8b-85ec-1bb4c47e30fe@googlegroups.com> Message-ID: On Sunday, November 18, 2012 8:52:35 PM UTC-5, su29090 wrote: > I did all of the other problems but I have issues with these: > > > > 1.Given a positive integer n , assign True to is_prime if n has no factors other than 1 and itself. (Remember, m is a factor of n if m divides n evenly.) > > > > 2.An arithmetic progression is a sequence of numbers in which the distance (or difference) between any two successive numbers if the same. This in the sequence 1, 3, 5, 7, ... , the distance is 2 while in the sequence 6, 12, 18, 24, ... , the distance is 6. > > > > Given the positive integer distance and the positive integer n , associate the variable sum with the sum of the elements of the arithmetic progression from 1 to n with distance distance . For example, if distance is 2 and n is 10 , then sum would be associated with 26 because 1+3+5+7+9 = 25 . > > > > Thanks in advance. I'm using Python 3.2 From rosuav at gmail.com Sun Nov 18 21:18:18 2012 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 19 Nov 2012 13:18:18 +1100 Subject: Python Interview Questions In-Reply-To: References: <1193768041.349129.26350@v3g2000hsg.googlegroups.com> <55443eb7-847c-4f4c-8d04-1e6b507aac00@googlegroups.com> <50a8acdc$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a911ec$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a97de0$0$29983$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Nov 19, 2012 at 1:09 PM, Roy Smith wrote: > The theorist understands that a chisel and a screwdriver were intended > for different purposes, but the pragmatist gets the paint can open. A good tool can always be used in ways its inventor never intended - and it will function as its user expects. $ some_program | egrep --color=always '(ERROR|^)' will highlight the word ERROR in red anywhere it appears in the program's output, while maintaining all other lines without color. Not normal use of grep, to be sure, but quite functional. A tuple may have been intended to be a record, a struct, whatever, but it is what it is, and I'll use one any time it's the best tool for the job. Maybe its immutability is critical; or maybe it's just the most convenient syntax and all I care about is that it be iterable. But when I'm explaining grep to someone, I'll describe it as a filter that keeps only some lines from the original, and when I describe a tuple, I'll point out that it's immutable and (potentially) hashable. The obvious first, the unobvious later. ChrisA From d at davea.name Sun Nov 18 21:18:19 2012 From: d at davea.name (Dave Angel) Date: Sun, 18 Nov 2012 21:18:19 -0500 Subject: Problems on these two questions In-Reply-To: <400c615f-4d93-4b8b-85ec-1bb4c47e30fe@googlegroups.com> References: <400c615f-4d93-4b8b-85ec-1bb4c47e30fe@googlegroups.com> Message-ID: <50A996EB.2000600@davea.name> On 11/18/2012 08:52 PM, su29090 wrote: > I all of the other problems but I have issues with these: > > 1.Given a positive integer n , assign True to is_prime if n has no factors other than 1 and itself. (Remember, m is a factor of n if m divides n evenly.) if is_a_prime(n): is_prime = True Now all you have to do is write is_a_prime(). if you get stuck, please show us what you've got, and what the problem is with it. And as usual, tell us what version of Python you're writing in, if any. > 2.An arithmetic progression is a sequence of numbers in which the distance (or difference) between any two successive numbers if the same. This in the sequence 1, 3, 5, 7, ... , the distance is 2 while in the sequence 6, 12, 18, 24, ... , the distance is 6. > > Given the positive integer distance and the positive integer n , associate the variable sum with the sum of the elements of the arithmetic progression from 1 to n with distance distance . For example, if distance is 2 and n is 10 , then sum would be associated with 26 because 1+3+5+7+9 = 25 . Don't call it 'sum' since that's a built-in function. Coincidentally, it's a function that takes an iterable, and calculates the sum of its elements. Sounds useful, no? The other thing you might want is xrange, which takes a start value, and end value, and a distance value. > > Thanks in advance. You never responded to any of the messages in the other thread. But Chris's advice was good, and better worded than mine. Pick a problem, make an attempt, then ask for help. -- DaveA From gheskett at wdtv.com Sun Nov 18 21:18:55 2012 From: gheskett at wdtv.com (Gene Heskett) Date: Sun, 18 Nov 2012 21:18:55 -0500 Subject: Spam source (Re: Horror Horror Horror!!!!!) In-Reply-To: <8ecb92a8-9f91-4ec7-9127-64af1315a766@googlegroups.com> References: <2d2a0b98-c587-4459-9489-680b1ddc47c8@googlegroups.com> <8ecb92a8-9f91-4ec7-9127-64af1315a766@googlegroups.com> Message-ID: <201211182118.55926.gheskett@wdtv.com> On Sunday 18 November 2012 21:18:16 Robert Miles did opine: > On Sunday, November 18, 2012 1:35:00 PM UTC-6, Terry Reedy wrote: > > The question was raised as to how much spam comes from googlegroups. > > > > Not all, but more that half, I believe. This one does. > > > > > > > > From: MoneyMaker > > > > ... > > > > Message-ID: <2d2a0b98-c587-4459-9489-680b1ddc47c8 at googlegroups.com> > > That depends on your definition of spam. This one does not appear to be > trying to sell anything, and therefore does not meet some of the > stricter definitions. Definitely off-topic, though. Not withstanding, it was fed to sa-learn -spam here. Cheers, Gene -- "There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order." -Ed Howdershelt (Author) My web page: is up! Only God can make random selections. From breamoreboy at yahoo.co.uk Sun Nov 18 21:21:19 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 19 Nov 2012 02:21:19 +0000 Subject: Spam source (Re: Horror Horror Horror!!!!!) In-Reply-To: References: <2d2a0b98-c587-4459-9489-680b1ddc47c8@googlegroups.com> Message-ID: On 18/11/2012 19:31, Terry Reedy wrote: > The question was raised as to how much spam comes from googlegroups. > I don't know the answer but I take the greatest pleasure in hurtling onto the dread googlegroups and gmane to report spam. Thankfully it's easy as the amount I receive via gmane is effectively zero. YMMV? -- Cheers. Mark Lawrence. From rosuav at gmail.com Sun Nov 18 21:23:37 2012 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 19 Nov 2012 13:23:37 +1100 Subject: Problems on these two questions In-Reply-To: <400c615f-4d93-4b8b-85ec-1bb4c47e30fe@googlegroups.com> References: <400c615f-4d93-4b8b-85ec-1bb4c47e30fe@googlegroups.com> Message-ID: On Mon, Nov 19, 2012 at 12:52 PM, su29090 <129km09 at gmail.com> wrote: > 1.Given a positive integer n , assign True to is_prime if n has no factors other than 1 and itself. (Remember, m is a factor of n if m divides n evenly.) > > 2.An arithmetic progression is a sequence of numbers in which the distance (or difference) between any two successive numbers if the same. This in the sequence 1, 3, 5, 7, ... , the distance is 2 while in the sequence 6, 12, 18, 24, ... , the distance is 6. Each of these problems is in two halves: a) Understanding the mathematics behind the question b) Writing the code. Which half are you halfing (oops sorry, *having*) trouble with? If (a), this isn't a programming question at all - search the web for information on the problem, as these are well-known challenges. If (b), you'll need to post your code to get any sort of useful help - we aren't mindreaders, though we do try to look that way sometimes! Either way, check this out: http://www.catb.org/esr/faqs/smart-questions.html#homework ChrisA From d at davea.name Sun Nov 18 21:33:43 2012 From: d at davea.name (Dave Angel) Date: Sun, 18 Nov 2012 21:33:43 -0500 Subject: Problems on these two questions In-Reply-To: References: <400c615f-4d93-4b8b-85ec-1bb4c47e30fe@googlegroups.com> Message-ID: <50A99A87.1050708@davea.name> On 11/18/2012 09:15 PM, su29090 wrote: > > I'm using Python 3.2 In Python 3.2, xrange has been renamed to range. So have you tried to solve problem #2 yet? -- DaveA From breamoreboy at yahoo.co.uk Sun Nov 18 21:42:48 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 19 Nov 2012 02:42:48 +0000 Subject: Python Interview Questions In-Reply-To: References: <1193768041.349129.26350@v3g2000hsg.googlegroups.com> <55443eb7-847c-4f4c-8d04-1e6b507aac00@googlegroups.com> <50a8acdc$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a911ec$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a97de0$0$29983$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 19/11/2012 02:09, Roy Smith wrote: > > The theorist understands that a chisel and a screwdriver were intended > for different purposes, but the pragmatist gets the paint can open. > To throw a chiseldriver into the works, IIRC a tuple is way faster to create but accessing a list is much faster. The obvious snag is that may have been Python 2.7 whereas 3.3 is completely different. Sorry but I'm currently wearing my XXXL size Lazy Bone Idle Hat so have no figures to back my probably incorrect memory up, anyone know anything about this? -- Cheers. Mark Lawrence. From frednotbob at hotmail.ca Sun Nov 18 22:00:57 2012 From: frednotbob at hotmail.ca (Graham Fielding) Date: Sun, 18 Nov 2012 19:00:57 -0800 Subject: Getting a seeded value from a list Message-ID: Hello! Clueless noob again! :) This time around, I'm trying to figure out the random.seed() function -- specifically, I'd like to retrieve the seeded values from a list (which I've called levelSeed), and use them in a random-dungeon generator. The numbers are generating and storing properly, but I can't find any examples online that aren't specific to a particular purpose (such as mathematics, arrays, and the like). Would I use 'getstate' when the dungeon is generated, or is there another method that would be better suited to the task? -------------- next part -------------- An HTML attachment was scrubbed... URL: From cs at zip.com.au Sun Nov 18 22:40:54 2012 From: cs at zip.com.au (Cameron Simpson) Date: Mon, 19 Nov 2012 14:40:54 +1100 Subject: Question on Socket Timeouts In-Reply-To: References: Message-ID: <20121119034053.GA5537@cskk.homeip.net> On 18Nov2012 03:27, Abhijeet Mahagaonkar wrote: | I'm new to network programming. | I have a question. | | Can we set a timeout to limit how long a particular socket can read or | write? On the socket itself? Probably not. But... | I have used a settimeout() function. | The settimeout() works fine as long as the client doesnt send any data for | x seconds. | The data that I receive in the server after accept()ing a connect() from a | client I check if the client is sending any invalid data. | I'm trying to ensure that a client sending invalid data constantly cannot | hold the server. So is there a way of saying I want the client to use this | socket for x seconds before I close it, no matter what data I receive? Not the time you set up the socket, or when you accept the client's connection. Thereafter, ever time you get some data, look at the clock. If enough time has elapsed, close the socket yourself. So, not via an interface to the socket but as logic in your own code. Cheers, -- Cameron Simpson Their are thre mistakes in this sentence. - Rob Ray DoD#33333 From abhi.forall at gmail.com Mon Nov 19 00:12:43 2012 From: abhi.forall at gmail.com (Abhijeet Mahagaonkar) Date: Mon, 19 Nov 2012 02:12:43 -0300 Subject: Question on Socket Timeouts In-Reply-To: <20121119034053.GA5537@cskk.homeip.net> References: <20121119034053.GA5537@cskk.homeip.net> Message-ID: I also tried looking at SO_RCVTIMEO option. Turns out that also resets if data is received. And yeah I implemented that as a separate logic in my code. I was wondering if sockets natively provided this functionality. Thanks again for clarifying. Cheers, Abhijeet On Mon, Nov 19, 2012 at 12:40 AM, Cameron Simpson wrote: > On 18Nov2012 03:27, Abhijeet Mahagaonkar wrote: > | I'm new to network programming. > | I have a question. > | > | Can we set a timeout to limit how long a particular socket can read or > | write? > > On the socket itself? Probably not. But... > > | I have used a settimeout() function. > | The settimeout() works fine as long as the client doesnt send any data > for > | x seconds. > | The data that I receive in the server after accept()ing a connect() from > a > | client I check if the client is sending any invalid data. > | I'm trying to ensure that a client sending invalid data constantly cannot > | hold the server. So is there a way of saying I want the client to use > this > | socket for x seconds before I close it, no matter what data I receive? > > Not the time you set up the socket, or when you accept the client's > connection. Thereafter, ever time you get some data, look at the clock. > If enough time has elapsed, close the socket yourself. > > So, not via an interface to the socket but as logic in your own code. > > Cheers, > -- > Cameron Simpson > > Their are thre mistakes in this sentence. > - Rob Ray DoD#33333 > -------------- next part -------------- An HTML attachment was scrubbed... URL: From solin.pavel at gmail.com Mon Nov 19 00:30:34 2012 From: solin.pavel at gmail.com (Pavel Solin) Date: Sun, 18 Nov 2012 21:30:34 -0800 Subject: Yet another Python textbook Message-ID: I would like to introduce a new Python textbook aimed at high school students: http://femhub.com/textbook-python/. The textbook is open source and its public Git repository is located at Github: git at github.com:femhub/nclab-textbook-python.git Feedback and contributions are very much welcome, every contributor becomes automatically a co-author. Best regards, Pavel -- Pavel Solin Associate Professor Applied and Computational Mathematics University of Nevada, Reno http://hpfem.org/~pavel -------------- next part -------------- An HTML attachment was scrubbed... URL: From cs at zip.com.au Mon Nov 19 00:37:00 2012 From: cs at zip.com.au (Cameron Simpson) Date: Mon, 19 Nov 2012 16:37:00 +1100 Subject: Question on Socket Timeouts In-Reply-To: <20121119034053.GA5537@cskk.homeip.net> References: <20121119034053.GA5537@cskk.homeip.net> Message-ID: <20121119053700.GA14804@cskk.homeip.net> On 19Nov2012 14:40, I wrote: | Not the time you set up the socket, or when you accept the client's | connection. Thereafter, ever time you get some data, look at the clock. | If enough time has elapsed, close the socket yourself. That would be "Note", not "Not". Sorry. -- Cameron Simpson No keyboard present Hit F1 to continue Zen engineering? - Haiku Error Messages http://www.salonmagazine.com/21st/chal/1998/02/10chal2.html From ian.g.kelly at gmail.com Mon Nov 19 01:01:48 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sun, 18 Nov 2012 23:01:48 -0700 Subject: Python Interview Questions In-Reply-To: References: <1193768041.349129.26350@v3g2000hsg.googlegroups.com> <55443eb7-847c-4f4c-8d04-1e6b507aac00@googlegroups.com> <50a8acdc$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a911ec$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a97de0$0$29983$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, Nov 18, 2012 at 7:42 PM, Mark Lawrence wrote: > To throw a chiseldriver into the works, IIRC a tuple is way faster to create > but accessing a list is much faster. The obvious snag is that may have been > Python 2.7 whereas 3.3 is completely different. Sorry but I'm currently > wearing my XXXL size Lazy Bone Idle Hat so have no figures to back my > probably incorrect memory up, anyone know anything about this? It's not been my experience with Python 2.7 that list access is faster than tuple access. Tuples are as fast as or faster than lists, pretty much universally. They seem to have closed the gap a bit in Python 3.3, though, as the following timings show. For one-shot construction, tuples seem to be more efficient for short sequences, but then lists win for longer sequences, although not by much. Of course, lists are always going to be much slower if you build them up with appends and extends. C:\>python -m timeit -s "x = range(10)" "tuple(x)" 1000000 loops, best of 3: 0.773 usec per loop C:\>python -m timeit -s "x = range(10)" "list(x)" 1000000 loops, best of 3: 0.879 usec per loop C:\>python -m timeit -s "x = range(100)" "tuple(x)" 100000 loops, best of 3: 2.88 usec per loop C:\>python -m timeit -s "x = range(100)" "list(x)" 100000 loops, best of 3: 2.63 usec per loop C:\>python -m timeit -s "x = range(1000)" "tuple(x)" 10000 loops, best of 3: 37.4 usec per loop C:\>python -m timeit -s "x = range(1000)" "list(x)" 10000 loops, best of 3: 36.2 usec per loop C:\>python -m timeit -s "x = range(10000)" "tuple(x)" 1000 loops, best of 3: 418 usec per loop C:\>python -m timeit -s "x = range(10000)" "list(x)" 1000 loops, best of 3: 410 usec per loop For iteration, tuples are consistently 7-8% faster. C:\>python -m timeit -s "x = tuple(range(10))" "for i in x: pass" 1000000 loops, best of 3: 0.467 usec per loop C:\>python -m timeit -s "x = list(range(10))" "for i in x: pass" 1000000 loops, best of 3: 0.498 usec per loop C:\>python -m timeit -s "x = tuple(range(100))" "for i in x: pass" 100000 loops, best of 3: 3.31 usec per loop C:\>python -m timeit -s "x = list(range(100))" "for i in x: pass" 100000 loops, best of 3: 3.56 usec per loop C:\>python -m timeit -s "x = tuple(range(1000))" "for i in x: pass" 10000 loops, best of 3: 31.6 usec per loop C:\>python -m timeit -s "x = list(range(1000))" "for i in x: pass" 10000 loops, best of 3: 34.3 usec per loop C:\>python -m timeit -s "x = tuple(range(10000))" "for i in x: pass" 1000 loops, best of 3: 318 usec per loop C:\>python -m timeit -s "x = list(range(10000))" "for i in x: pass" 1000 loops, best of 3: 341 usec per loop For direct item access, tuples seem to be about 2-3% faster. C:\>python -m timeit -s "import operator as o; x = tuple(range(10)); g = o.itemgetter(*range(len(x)))" "g(x)" 1000000 loops, best of 3: 0.67 usec per loop C:\>python -m timeit -s "import operator as o; x = list(range(10)); g = o.itemgetter(*range(len(x)))" "g(x)" 1000000 loops, best of 3: 0.674 usec per loop C:\>python -m timeit -s "import operator as o; x = tuple(range(100)); g = o.itemgetter(*range(len(x)))" "g(x)" 100000 loops, best of 3: 4.52 usec per loop C:\>python -m timeit -s "import operator as o; x = list(range(100)); g = o.itemgetter(*range(len(x)))" "g(x)" 100000 loops, best of 3: 4.65 usec per loop C:\>python -m timeit -s "import operator as o; x = tuple(range(1000)); g = o.itemgetter(*range(len(x)))" "g(x)" 10000 loops, best of 3: 43.2 usec per loop C:\>python -m timeit -s "import operator as o; x = list(range(1000)); g = o.itemgetter(*range(len(x)))" "g(x)" 10000 loops, best of 3: 43.7 usec per loop C:\>python -m timeit -s "import operator as o; x = tuple(range(10000)); g = o.itemgetter(*range(len(x)))" "g(x)" 1000 loops, best of 3: 422 usec per loop C:\>python -m timeit -s "import operator as o; x = list(range(10000)); g = o.itemgetter(*range(len(x)))" "g(x)" 1000 loops, best of 3: 447 usec per loop From steve+comp.lang.python at pearwood.info Mon Nov 19 02:54:56 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 19 Nov 2012 07:54:56 GMT Subject: Python Interview Questions References: <1193768041.349129.26350@v3g2000hsg.googlegroups.com> <55443eb7-847c-4f4c-8d04-1e6b507aac00@googlegroups.com> <50a8acdc$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a911ec$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a97de0$0$29983$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50a9e5cf$0$21863$c3e8da3$76491128@news.astraweb.com> On Sun, 18 Nov 2012 21:09:36 -0500, Roy Smith wrote: > In article <50a97de0$0$29983$c3e8da3$5496439d at news.astraweb.com>, > Steven D'Aprano wrote: > > >> > The stack that's returned is a list. It's inherently a list, per the >> > classic definition: >> >> Er, no, it's inherently a blob of multiple text lines. > > No, it's a list that looks like (taken from the doc string of the code I > referenced): > > [('/usr/lib/.../base.py', 'get_response'), > ('/home/songza/.../views.py', 'song_info'), > ('/home/songza.../api.py', 'get_song'), > ('/home/songza/.../api.py', 'api')] > > [it doesn't really have ...'s in the paths; I just elided some text to > make it easier to read] I see. It wasn't clear from your earlier description that the items had been post-processed from collections of raw log lines to fixed records. But it doesn't actually change my analysis any. See below. By the way, based on the sample data you show, your script is possibly broken. You don't record either the line number that raises, or the exception raised, so your script doesn't differentiate between different errors that happen to occur with similar stack traces. (I say "possibly" broken because I don't know what your requirements are. Maybe your requirements are sufficiently wide that you don't care that distinct failures are counted together.) E.g. these three stack traces will probably generate the same fixed record, even though the errors are distinct: #1 Traceback (most recent call last): File "./spam.py", line 20, in select selection = func(a, b) File "./spam.py", line 60, in func return 1/x ZeroDivisionError: float division #2 Traceback (most recent call last): File "./spam.py", line 20, in select selection = func(a, b) File "./spam.py", line 60, in func return 1/x TypeError: unsupported operand type(s) for /: 'int' and 'NoneType' #3 Traceback (most recent call last): File "./spam.py", line 20, in select selection = func(a, b) File "./spam.py", line 55, in func y = 1/(a + b) ZeroDivisionError: float division Maybe that's okay for your application, but it strikes me as odd that you do distinguish *some* distinct errors in the same function, but not others. >> > * It's homogeneous. There's nothing particularly significant about >> > each entry other than it's the next one in the stack. >> >> The complete stack trace is inhomogeneous and immutable. I've already >> covered immutability above: removing, adding or moving lines will >> invalidate the stack trace. Inhomogeneity comes from the structure of a >> stack trace. The mere fact that each line is a string does not mean >> that any two lines are equivalent. Different lines represent different >> things. > > No. Each entry in the list represents a source file and a function > name. They're all the same "shape". You could remove one or add > another one, or shuffle the order, and you would have something which > was syntactically correct and semantically meaningful (even if it didn't > represent an actual code path. If you remove/add/shuffle lines in the stack, you no longer have the same stack. Take the example you gave before: stack1 = [('/usr/lib/.../base.py', 'get_response'), ('/home/songza/.../views.py', 'song_info'), ('/home/songza.../api.py', 'get_song'), ('/home/songza/.../api.py', 'api') ] Here's a different stack trace, representing a different code path, which as you say is syntactically correct and semantically meaningful: stack2 = [('/home/songza/.../api.py', 'api'), ('/home/songza.../api.py', 'get_song'), ('/home/songza/.../views.py', 'song_info'), ('/usr/lib/.../base.py', 'get_response') ] Since they are different stacks, they are treated as different keys: data = {stack1: 11, stack2: 22} Do you agree that this is what your application expects? Different stack traces are different keys, associated with different values. I claim this only makes sense if you treat the stacks as inherently immutable. Never mind Python's limitation. Let's pretend we were running this code under some other language, NeoPython, which allowed mutable keys. You claim that stacks are *inherently mutable*. So I should be able to do this: stack1.sort() # it's the *same stack*, all I've done is mutate it print data[stack1] and expect to see "11" printed. I am looking at the same key, right? So I certainly don't expect to see the value associated with a completely different key. But wait a minute... after sorting, stack1 and stack2 now are equal. I could just as easily expect to see "22" printed. I thought we had just agreed that stack1 and stack2 are *different* keys. Of course they are different. They represent different code paths. But after sorting stack1, it looks exactly like stack2. It looks like a different code path. It *lies* -- it no longer represents the code path that it actually represents, instead it looks like a *different* code path. I then generate another stack: stack3 = [('/home/songza/.../api.py', 'api'), ('/home/songza.../api.py', 'get_song'), ('/home/songza/.../views.py', 'song_info'), ('/usr/lib/.../base.py', 'get_response') ] should data[stack3] return 11 (it has the same value as stack1) or 22 (it has the same value as stack2)? Or possibly 33? Or raise KeyError? Treating stacks in this context as mutable is *incoherent*. It is nice and convenient to be able to build up a stack trace using a mutable list, you won't get an argument from me about that, but that can only be considered a temporary data structure used to build the data structure you actually care about, which is fixed. That brings it back to my question: your application is not a counter- example to my question about using lists as keys, because your data is not inherently list-like. It is inherently tuple-like, you just build it using a temporary list. That's perfectly fine, by the way, I do the same thing. As you say, the order of the lines in the stack trace is significant. You cannot expect to mutate the stack and move lines around and treat it as the same stack. If you move the lines about, it represents a different stack. That is fundamentally different from the normal use of a list, where you do expect to be able to move lines about and still have it count as "the same list". > I think we're going to have to let this be. You obviously have your > concept of what a tuple is and what a list is. I disagree. I think a tuple is an immutable sequence of items, and a list is a mutable sequence of items. > I don't > think either of us is right or wrong, we just have different ways of > thinking about things. > > You come at it from a theoretical point of view. I certainly do not. My position here is imminently practical. The alternative, the mutability of keys, is simply incoherent. > You think of each type > as an embodiment of certain concepts ("it represents a fixed-length > heterogenous sequence"). Your thinking is driven by what each type was > intended to be used for. Not even close. My thinking is driven by the things each data structure needs to do. See below. > I come at it from a practical point of view. To me, each type is a > collection of methods. I have certain operations I need to perform. I > pick the type which offers those operations. If the set of operations I > need to perform (in this case, {append, hash}) don't exist in a single > type, I'm forced to use both types and convert from one to the other as > needed. I don't see that as a problem. Converting from one type to another is exactly the sort of thing I described in my earlier question. In your application, you build up a collection of code lines that represent a stack trace. Here's that example from your own documentation again: [('/usr/lib/.../base.py', 'get_response'), ('/home/songza/.../views.py', 'song_info'), ('/home/songza.../api.py', 'get_song'), ('/home/songza/.../api.py', 'api')] What are the sorts of things I might meaningfully want to do with this *complete* stack trace? Add extra lines to it? No. If I needed to add extra lines, it wouldn't be complete. Delete lines? Certainly not, that would change the code path it claims to represent to a code path it doesn't represent. Sort the list? Reverse it? Heavens no. If you look at the available list methods, *not one* of the mutating methods is appropriate to a completed stack trace object. *None* of the mutator list methods are appropriate once the stack trace object is complete, and using them would be counter-productive. If you believe different, then please tell me what mutations your code actually performs after the stack trace object is completed. In the code you showed, you throw the list away after turning it into a tuple. If the object represents a "list of code lines", in the sense of a mutable Python list rather than a mere sequence, then why don't you use any list methods on it? The append method is useful during construction, but that is all. After the stack is complete, use of any mutator method would be a bug. In other words, it ought to be immutable, and the use of a list ought to be buried in the appropriate function as an internal implementation detail. The public interface ought to be that of an immutable tuple of immutable strings, because once you have finished building the object, it should not be possible to mutate it. This is hardly a theoretical viewpoint. The idea of treating data that ought not be changed as immutable is borne out of bitter experience of millions of man-hours tracking down hundreds of thousands of bugs. (Admittedly not all of those bugs were *my* bugs. I'm talking the collective experience of programmers over fifty years of coding.) -- Steven From tjreedy at udel.edu Mon Nov 19 03:27:46 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 19 Nov 2012 03:27:46 -0500 Subject: Python Interview Questions In-Reply-To: References: <1193768041.349129.26350@v3g2000hsg.googlegroups.com> <55443eb7-847c-4f4c-8d04-1e6b507aac00@googlegroups.com> <50a8acdc$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a911ec$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a97de0$0$29983$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 11/19/2012 1:01 AM, Ian Kelly wrote: > than tuple access. Tuples are as fast as or faster than lists, pretty > much universally. They seem to have closed the gap a bit in > Python 3.3, though, as the following timings show. For one-shot > construction, tuples seem to be more efficient for short sequences, > but then lists win for longer sequences, although not by much. Of > course, lists are always going to be much slower if you build them up > with appends and extends. Interesting results. But what system (hardware, os). These sorts of times tend to vary with the system. > > C:\>python -m timeit -s "x = range(10)" "tuple(x)" > 1000000 loops, best of 3: 0.773 usec per loop > > C:\>python -m timeit -s "x = range(10)" "list(x)" > 1000000 loops, best of 3: 0.879 usec per loop > > C:\>python -m timeit -s "x = range(100)" "tuple(x)" > 100000 loops, best of 3: 2.88 usec per loop > > C:\>python -m timeit -s "x = range(100)" "list(x)" > 100000 loops, best of 3: 2.63 usec per loop > > C:\>python -m timeit -s "x = range(1000)" "tuple(x)" > 10000 loops, best of 3: 37.4 usec per loop > > C:\>python -m timeit -s "x = range(1000)" "list(x)" > 10000 loops, best of 3: 36.2 usec per loop > > C:\>python -m timeit -s "x = range(10000)" "tuple(x)" > 1000 loops, best of 3: 418 usec per loop > > C:\>python -m timeit -s "x = range(10000)" "list(x)" > 1000 loops, best of 3: 410 usec per loop > > > For iteration, tuples are consistently 7-8% faster. > > > C:\>python -m timeit -s "x = tuple(range(10))" "for i in x: pass" > 1000000 loops, best of 3: 0.467 usec per loop > > C:\>python -m timeit -s "x = list(range(10))" "for i in x: pass" > 1000000 loops, best of 3: 0.498 usec per loop > > C:\>python -m timeit -s "x = tuple(range(100))" "for i in x: pass" > 100000 loops, best of 3: 3.31 usec per loop > > C:\>python -m timeit -s "x = list(range(100))" "for i in x: pass" > 100000 loops, best of 3: 3.56 usec per loop > > C:\>python -m timeit -s "x = tuple(range(1000))" "for i in x: pass" > 10000 loops, best of 3: 31.6 usec per loop > > C:\>python -m timeit -s "x = list(range(1000))" "for i in x: pass" > 10000 loops, best of 3: 34.3 usec per loop > > C:\>python -m timeit -s "x = tuple(range(10000))" "for i in x: pass" > 1000 loops, best of 3: 318 usec per loop > > C:\>python -m timeit -s "x = list(range(10000))" "for i in x: pass" > 1000 loops, best of 3: 341 usec per loop > > > For direct item access, tuples seem to be about 2-3% faster. > > > C:\>python -m timeit -s "import operator as o; x = tuple(range(10)); g > = o.itemgetter(*range(len(x)))" "g(x)" > 1000000 loops, best of 3: 0.67 usec per loop > > C:\>python -m timeit -s "import operator as o; x = list(range(10)); g > = o.itemgetter(*range(len(x)))" "g(x)" > 1000000 loops, best of 3: 0.674 usec per loop > > C:\>python -m timeit -s "import operator as o; x = tuple(range(100)); > g = o.itemgetter(*range(len(x)))" "g(x)" > 100000 loops, best of 3: 4.52 usec per loop > > C:\>python -m timeit -s "import operator as o; x = list(range(100)); g > = o.itemgetter(*range(len(x)))" "g(x)" > 100000 loops, best of 3: 4.65 usec per loop > > C:\>python -m timeit -s "import operator as o; x = tuple(range(1000)); > g = o.itemgetter(*range(len(x)))" "g(x)" > 10000 loops, best of 3: 43.2 usec per loop > > C:\>python -m timeit -s "import operator as o; x = list(range(1000)); > g = o.itemgetter(*range(len(x)))" "g(x)" > 10000 loops, best of 3: 43.7 usec per loop > > C:\>python -m timeit -s "import operator as o; x = > tuple(range(10000)); g = o.itemgetter(*range(len(x)))" "g(x)" > 1000 loops, best of 3: 422 usec per loop > > C:\>python -m timeit -s "import operator as o; x = list(range(10000)); > g = o.itemgetter(*range(len(x)))" "g(x)" > 1000 loops, best of 3: 447 usec per loop > -- Terry Jan Reedy From andrea.crotti.0 at gmail.com Mon Nov 19 05:39:29 2012 From: andrea.crotti.0 at gmail.com (andrea crotti) Date: Mon, 19 Nov 2012 10:39:29 +0000 Subject: changing process name Message-ID: I have very long processes to spawn which I want to lauch as separate processes (and communicate with ZeroMQ), but now the problem is that the forked process appears in "ps" with the same name as the launcher process. This is a simplified version of what I'm trying to do: import sys from os import fork, _exit def on_forked_process(func): """Decorator that forks the process, runs the function and gives back control to the main process """ def _on_forked_process(*args, **kwargs): pid = fork() if pid == 0: func(*args, **kwargs) _exit(0) else: return pid return _on_forked_process @on_forked_process def start_long_proc(): sys.argv[:] = [sys.argv[0]] + ['daemon', 'arguments'] from daemon import long_sleep long_sleep() if __name__ == '__main__': start_long_proc() # if the main process is still running but it's not told the child # when it dies then it becomes a zombie?? no apparently it doesn't while True: pass Where daemon.py: import sys import time def long_sleep(): sys.argv[:] = [sys.argv[0]] + ['daemon', 'arguments'] time.sleep(20) so both the sys.argv reassignment don't work so far, any other way to fix this? On the real machine I also get zombie processes, but on my machine I can't reproduce this, and would also be nice to fix that.. Thanks From ulrich.eckhardt at dominolaser.com Mon Nov 19 07:38:23 2012 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Mon, 19 Nov 2012 13:38:23 +0100 Subject: Point of idle curiosity In-Reply-To: References: Message-ID: Am 18.11.2012 12:45, schrieb Chris Angelico: > (if you'll forgive the pun) Nevarr! > Is IDLE named after Eric of that name, or is it pure coincidence? Maybe. Interestingly, there is also http://eric-ide.python-projects.org/, just to add some more unfounded conspiracy theories to this discussion. :P Uli From yasirsaleem105 at gmail.com Mon Nov 19 08:29:13 2012 From: yasirsaleem105 at gmail.com (Yasir Saleem) Date: Mon, 19 Nov 2012 05:29:13 -0800 (PST) Subject: "Premature end of script headers: wsgihandler.py" on usage of BytesIO() Message-ID: Hi all, I am generating graphs using "cairo plot" in web2py project. Here I am using BytesIO() stream for generating graphs. Everything runs fine when I run on localhost but when I deploy it on apache server and then run from different machines OR from different browsers in same machine then the server becomes halt and in apache error log, I found this error message: Premature end of script headers: wsgihandler.py, Furthermore, it occurs only when I use BytesIO() stream for generating graphs. In all other cases, it run smoothly. Please guide me that how I should resolve this issue. Furthermore, please also guide, if I can use any stream other BytesIO() From eric.frederich at gmail.com Mon Nov 19 08:37:32 2012 From: eric.frederich at gmail.com (Eric Frederich) Date: Mon, 19 Nov 2012 08:37:32 -0500 Subject: error importing smtplib In-Reply-To: References: Message-ID: I can do this in stand alone programs because my code does the import and calls the login function so I can control the order of things. Unfortunately stand alone programs are not the only ways in which I am using these Python bindings. You can customize and extend this 3rd party application at various extension points all of which are invoked after login. We have a lot of extensions written in Python. I guess I will have to back to the BAR vendor and ask if it is okay to remove their old .so file. Perhaps their code will just work with the newer 0.9.8e or perhaps they'll have to relink or recompile. On Fri, Nov 16, 2012 at 5:00 PM, Terry Reedy wrote: > [easy] Do the import before the function call, which is the proper order > and the one that works. > > -- > Terry Jan Reedy > > -- > http://mail.python.org/**mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From roy at panix.com Mon Nov 19 09:30:54 2012 From: roy at panix.com (Roy Smith) Date: Mon, 19 Nov 2012 09:30:54 -0500 Subject: Python Interview Questions References: <1193768041.349129.26350@v3g2000hsg.googlegroups.com> <55443eb7-847c-4f4c-8d04-1e6b507aac00@googlegroups.com> <50a8acdc$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a911ec$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a97de0$0$29983$c3e8da3$5496439d@news.astraweb.com> <50a9e5cf$0$21863$c3e8da3$76491128@news.astraweb.com> Message-ID: In article <50a9e5cf$0$21863$c3e8da3$76491128 at news.astraweb.com>, Steven D'Aprano wrote: > I see. It wasn't clear from your earlier description that the items had > been post-processed from collections of raw log lines to fixed records. Well, I did provide the code that does this. > But it doesn't actually change my analysis any. See below. > > By the way, based on the sample data you show, your script is possibly > broken. You don't record either the line number that raises, or the > exception raised, so your script doesn't differentiate between different > errors that happen to occur with similar stack traces. You really might want to read the code I provided. Here's the reference again: https://bitbucket.org/roysmith/python-tools/src/4f8118d175ed/logs/traceba ck_helper.py The "header" referred to does indeed contain the exception raised. And the line numbers are included. Here's a typical output stanza: 2012-11-19T00:00:15+00:00 web5 ????2012-11-19 00:00:15,831 [2712]: songza-api IGPhwNU2SJ691cx8 4C0ABFA9-50A974E7-384995 W6D-HSO 173.145.137.54 songza.django.middleware ERROR process_exception() Path = u'/api/1/station/1459775/next', Exception = ValueError(u">: no song ids for mp3",) /home/songza/env/python/local/lib/python2.7/site-packages/django/core/han dlers/base.py:111:get_response() /home/songza/deploy/current/pyza/djapi/decorators.py:11:_wrapped_view_fun c() /home/songza/env/python/local/lib/python2.7/site-packages/django/views/de corators/http.py:45:inner() /home/songza/deploy/current/pyza/djapi/views.py:1659:station_next() /home/songza/deploy/current/pyza/models/station.py:660:next_song() /home/songza/deploy/current/pyza/lib/song_picker.py:327:pick() > I say "possibly" broken because I don't know what your requirements are. Our requirements are to scan the logs of a production site and filter down the gobs and gobs of output (we produced 70 GB of log files yesterday) into something small enough that a human can see what the most common failures were. The tool I wrote does that. The rest of this conversation is just silly. It's turning into getting hit on the head lessons. From neilc at norwich.edu Mon Nov 19 09:57:49 2012 From: neilc at norwich.edu (Neil Cerutti) Date: 19 Nov 2012 14:57:49 GMT Subject: Python questions help References: <20121116100054.54257de92fb4ed6e65a28126@lavabit.com> Message-ID: On 2012-11-16, Chris Angelico wrote: > On Sat, Nov 17, 2012 at 5:00 AM, rh > wrote: >> "How many people think programming skills are inherent?" i.e. >> that some people are just born with the gift to be good >> programmers Result: very few hands raised maybe a couple >> (possibly non-progammers??) > > Maybe, but there's definitely something that happens close to > birth. If your parents give you the name Chris, you're more > likely to become a geek and a programmer. There are people with rare talent who can program in a way that most others can't, .e.g, Chris Sawyer. But, as Louis Moyse, a great musician remarked: "Without hard work, talent means nothing." -- Neil Cerutti From roy at panix.com Mon Nov 19 09:59:19 2012 From: roy at panix.com (Roy Smith) Date: Mon, 19 Nov 2012 09:59:19 -0500 Subject: Python Interview Questions References: <1193768041.349129.26350@v3g2000hsg.googlegroups.com> <55443eb7-847c-4f4c-8d04-1e6b507aac00@googlegroups.com> <50a8acdc$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a911ec$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a97de0$0$29983$c3e8da3$5496439d@news.astraweb.com> <50a9e5cf$0$21863$c3e8da3$76491128@news.astraweb.com> Message-ID: OK, I've just read back over the whole thread. I'm really struggling to understand what point you're trying to make. I started out by saying: > Use a list when you need an ordered collection which is mutable (i.e. > can be altered after being created). Use a tuple when you need an > immutable list (such as for a dictionary key). To which you obviously objected. So now you write: > I think a tuple is an immutable sequence of items, and a list is a > mutable sequence of items. So how is that different from what I said? Is this whole argument boiling down to your use of "immutable sequence" vs. my use of "immutable list"? From hansmu at xs4all.nl Mon Nov 19 10:00:42 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Mon, 19 Nov 2012 16:00:42 +0100 Subject: "Premature end of script headers: wsgihandler.py" on usage of BytesIO() In-Reply-To: References: Message-ID: <50aa499b$0$6885$e4fe514c@news2.news.xs4all.nl> On 19/11/12 14:29:13, Yasir Saleem wrote: > Hi all, > > I am generating graphs using "cairo plot" in web2py project. > Here I am using BytesIO() stream for generating graphs. > Everything runs fine when I run on localhost but when I deploy > it on apache server and then run from different machines OR > from different browsers in same machine then the server > becomes halt and in apache error log, I found this error > message: > > Premature end of script headers: wsgihandler.py, > > Furthermore, it occurs only when I use BytesIO() stream for > generating graphs. In all other cases, it run smoothly. > > Please guide me that how I should resolve this issue. One technique is to put at the very top of the script, even above the import statements: print "Content-Type: text/plain\n\n" This will cause all text output by your script to be displayed in your browser as plain text [1]. If you inspect it, you'll probably find some kind of warning displayed above the HTML headers. You'll need to find a way to not receive that warning. It's usually best if you can actually solve the issue Python is warning about. it that's not possible, suppressing the warning may be your only alternative. If you can't figure out what the message means, and Google doesn't know either, you can post it in this forum and ask for further guidance. [1] Except if you use Internet Explorer, which will ask you whether you want to save the document. You can either do that and view the content with another application, or use another browser, or change the content-type to text/html. If you do the latter, IE will notice that the content is really plain text, and that it is actually quite capable of displaying that. Hope this helps, -- HansM From blockeduser at gmail.com Mon Nov 19 11:14:51 2012 From: blockeduser at gmail.com (blockeduser at gmail.com) Date: Mon, 19 Nov 2012 08:14:51 -0800 (PST) Subject: Paid Python work for 30mins - 1 hour Message-ID: I have three scripts that I would like written, they are designed to do the following: Backup.py ? Zip a folder and store it on amazon S3 using BOTO with the date and time as the folder name. Restore.py ? Grab a file from S3 and download it and then unzip it in the right location with two commandline parameters (1 = Get most recent, 2 = Get specific file) Import.py ? Check that I have done this correctly and add command line parameter for changing the command This code is probably 50% completed already and if someone knows what they are doing, could be completed in a very short time. It is really basic Python code, I just dont know python myself. If you are interested get in touch! From blockeduser at gmail.com Mon Nov 19 11:17:36 2012 From: blockeduser at gmail.com (blockeduser at gmail.com) Date: Mon, 19 Nov 2012 08:17:36 -0800 (PST) Subject: Paid Python work for 30mins - 1 hour In-Reply-To: References: Message-ID: <63cfcfba-b0de-4690-a870-ef8f5102c275@googlegroups.com> Forgot to say, my email is blockeduser at gmail.com From ian.g.kelly at gmail.com Mon Nov 19 11:44:27 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 19 Nov 2012 09:44:27 -0700 Subject: Python Interview Questions In-Reply-To: References: <1193768041.349129.26350@v3g2000hsg.googlegroups.com> <55443eb7-847c-4f4c-8d04-1e6b507aac00@googlegroups.com> <50a8acdc$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a911ec$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a97de0$0$29983$c3e8da3$5496439d@news.astraweb.com> <50a9e5cf$0$21863$c3e8da3$76491128@news.astraweb.com> Message-ID: On Mon, Nov 19, 2012 at 7:30 AM, Roy Smith wrote: > In article <50a9e5cf$0$21863$c3e8da3$76491128 at news.astraweb.com>, > Steven D'Aprano wrote: >> >> By the way, based on the sample data you show, your script is possibly >> broken. You don't record either the line number that raises, or the >> exception raised, so your script doesn't differentiate between different >> errors that happen to occur with similar stack traces. > > You really might want to read the code I provided. Here's the reference > again: > > https://bitbucket.org/roysmith/python-tools/src/4f8118d175ed/logs/traceba > ck_helper.py > > The "header" referred to does indeed contain the exception raised. And > the line numbers are included. Here's a typical output stanza: Yes, but the dict is still keyed on the traceback alone, and only the first header for a particular traceback is stored. If two different exceptions occur at the same line of code and sharing the same traceback, the second exception would be counted as a second occurrence of the first, effectively squashing any reporting of the second exception. From nicolettivma at gmail.com Mon Nov 19 11:44:37 2012 From: nicolettivma at gmail.com (EDI Support) Date: Mon, 19 Nov 2012 08:44:37 -0800 (PST) Subject: Linux compatibility Message-ID: <5ab8efb4-d66b-42f7-9301-7048bf38fcef@googlegroups.com> Hi All, I would like know if Python 2.4.3 will be compatible with Linux RHEL 5.5 or 6.1? Thanks Tony From kwpolska at gmail.com Mon Nov 19 11:58:55 2012 From: kwpolska at gmail.com (Kwpolska) Date: Mon, 19 Nov 2012 17:58:55 +0100 Subject: Yet another Python textbook In-Reply-To: References: Message-ID: On Mon, Nov 19, 2012 at 6:30 AM, Pavel Solin wrote: > I would like to introduce a new Python textbook > aimed at high school students: > > http://femhub.com/textbook-python/. > > The textbook is open source and its public Git > repository is located at Github: > > git at github.com:femhub/nclab-textbook-python.git URL for humans: https://github.com/femhub/nclab-textbook-python > > Feedback and contributions are very much > welcome, every contributor becomes automatically > a co-author. > > Best regards, > > Pavel > You are writing it for something called ?NCLab?, not for the general public, and that sucks. 1. please use math-compatible fonts in LaTeX, like Computer Modern, Latin Modern, Lucida Bright or MathTime (I suggest one of the first two). This way, stuff in the math environment won?t look differently than the text. Currently, your text is in a fancy font and maths are in Computer Modern. 2. IMO, you should be doing a bit more general usage programming, not science-specific. 3. Code highlighting for inline code and other languages, please. This are just my basic thoughts from looking through it. -- Kwpolska stop html mail | always bottom-post www.asciiribbon.org | www.netmeister.org/news/learn2quote.html GPG KEY: 5EAAEA16 From jeanmichel at sequans.com Mon Nov 19 13:02:16 2012 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Mon, 19 Nov 2012 19:02:16 +0100 (CET) Subject: Python Interview Questions In-Reply-To: <50a8acdc$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: <681008532.1982266.1353348136262.JavaMail.root@sequans.com> ----- Original Message ----- > Use a set when you want to represent a collection of items and the > order > is not important: An important feature of sets is that their items are unique. set(list(...)) is a good shortcut to remove duplicate in a list. JM -- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you. From no.email at nospam.invalid Mon Nov 19 13:33:10 2012 From: no.email at nospam.invalid (Paul Rubin) Date: Mon, 19 Nov 2012 10:33:10 -0800 Subject: Paid Python work for 30mins - 1 hour References: Message-ID: <7xhaolzbmh.fsf@ruckus.brouhaha.com> blockeduser at gmail.com writes: > I have three scripts that I would like written, they are designed to > do the following: > Backup.py ? Zip a folder and store it on amazon S3 ... > If you are interested get in touch! You could just type "python s3 upload" into web search and see if you can use the stuff that turns up. More realistically I'm not sure it makes sense to set up a 30 minute / 1 hour development gig, especially without a completely precise specification up front. There will have to be some back-and-forth about the user interface, the script will have to be tested, the developer will need access to an S3 account if s/he doesn't have it already, etc. So it's likely to end up taking longer than 1 hour even though the task is pretty simple. But, maybe someone will take you up on it, and if it turns out to be more trouble than it was worth, at least they learned something. If you already have 50% of the code written, what's preventing you from doing the other 50% yourself? If you're hitting snags, maybe you could post what you have so far, and ask for advice. From dreamingforward at gmail.com Mon Nov 19 14:08:35 2012 From: dreamingforward at gmail.com (Mark Adam) Date: Mon, 19 Nov 2012 13:08:35 -0600 Subject: Paid Python work for 30mins - 1 hour In-Reply-To: References: Message-ID: On Mon, Nov 19, 2012 at 10:14 AM, wrote: > I have three scripts that I would like written, they are designed to do the following: > > Backup.py ? Zip a folder and store it on amazon S3 using BOTO with the date and time as the folder name. > > Restore.py ? Grab a file from S3 and download it and then unzip it in the right location with two commandline parameters (1 = Get most recent, 2 = Get specific file) > > Import.py ? Check that I have done this correctly and add command line parameter for changing the command > > This code is probably 50% completed already and if someone knows what they are doing, could be completed in a very short time. It is really basic Python code, I just dont know python myself. > > If you are interested get in touch! You might consider putting your request on elance.com or guru.com where you can hire programmers for small projects like this. Good luck, mark From no.email at nospam.invalid Mon Nov 19 14:52:00 2012 From: no.email at nospam.invalid (Paul Rubin) Date: Mon, 19 Nov 2012 11:52:00 -0800 Subject: Paid Python work for 30mins - 1 hour References: Message-ID: <7xd2z9s74v.fsf@ruckus.brouhaha.com> Mark Adam writes: > You might consider putting your request on elance.com or guru.com > where you can hire programmers for small projects like this. Even though the coding task is very small, I think it's unrealistic to scope it at less than 2-4 hours, given communication overhead etc. It would be quicker if it were done in person. From jcasale at activenetwerx.com Mon Nov 19 15:32:29 2012 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Mon, 19 Nov 2012 20:32:29 +0000 Subject: Robust regex Message-ID: Trying to robustly parse a string that will have key/value pairs separated by three pipes, where each additional key/value (if more than one exists) will be delineated by four more pipes. string = 'key_1|||value_1||||key_2|||value_2' regex = '((?:(?!\|\|\|).)+)(?:\|\|\|)((?:(?!\|\|\|).)+)(?:\|\|\|\|)?' I am not convinced this is the most effective or safest, any opinions would be greatly appreciated! jlc From benjamin.kaplan at case.edu Mon Nov 19 15:41:31 2012 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Mon, 19 Nov 2012 12:41:31 -0800 Subject: Robust regex In-Reply-To: References: Message-ID: On Nov 19, 2012 12:37 PM, "Joseph L. Casale" wrote: > > Trying to robustly parse a string that will have key/value pairs separated > by three pipes, where each additional key/value (if more than one exists) > will be delineated by four more pipes. > > string = 'key_1|||value_1||||key_2|||value_2' > regex = '((?:(?!\|\|\|).)+)(?:\|\|\|)((?:(?!\|\|\|).)+)(?:\|\|\|\|)?' > > I am not convinced this is the most effective or safest, any opinions would > be greatly appreciated! > > jlc > -- > http://mail.python.org/mailman/listinfo/python-list Do you even need a regular expression for this? Just split on |||| and then split those on |||. -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjreedy at udel.edu Mon Nov 19 15:41:36 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 19 Nov 2012 15:41:36 -0500 Subject: Python Interview Questions In-Reply-To: References: <1193768041.349129.26350@v3g2000hsg.googlegroups.com> <55443eb7-847c-4f4c-8d04-1e6b507aac00@googlegroups.com> <50a8acdc$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a911ec$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a97de0$0$29983$c3e8da3$5496439d@news.astraweb.com> <50a9e5cf$0$21863$c3e8da3$76491128@news.astraweb.com> Message-ID: On 11/19/2012 9:30 AM, Roy Smith wrote: > Our requirements are to scan the logs of a production site and filter > down the gobs and gobs of output (we produced 70 GB of log files > yesterday) into something small enough that a human can see what the > most common failures were. The tool I wrote does that. > > The rest of this conversation is just silly. It's turning into getting > hit on the head lessons. I agree. In early Python, tuples were more different from lists than they are today. They did not have any (public) methods. Today, they have .index and .count methods, which make little sense from the 'tuple is a record' viewpoint. The addition of those methods redefined tuples as read-only (and therefore hashable) sequences. From the collections.abc doc ''' Sequence | Sized, Iterable, Container | __getitem__ __contains__, __iter__, __reversed__, index, and count ... class collections.abc.Sequence class collections.abc.MutableSequence ABCs for read-only and mutable sequences. ''' >>> from collections.abc import Sequence >>> issubclass(tuple, Sequence) True -- Terry Jan Reedy From rosuav at gmail.com Mon Nov 19 15:42:19 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 20 Nov 2012 07:42:19 +1100 Subject: Robust regex In-Reply-To: References: Message-ID: On Tue, Nov 20, 2012 at 7:32 AM, Joseph L. Casale wrote: > Trying to robustly parse a string that will have key/value pairs separated > by three pipes, where each additional key/value (if more than one exists) > will be delineated by four more pipes. > > string = 'key_1|||value_1||||key_2|||value_2' > regex = '((?:(?!\|\|\|).)+)(?:\|\|\|)((?:(?!\|\|\|).)+)(?:\|\|\|\|)?' > > I am not convinced this is the most effective or safest, any opinions would > be greatly appreciated! Is regex a requirement? Since you posted this on python-list, I'm going to assume you're working in Python. string = 'key_1|||value_1||||key_2|||value_2' content = dict(map(lambda x: x.split("|||"),string.split("||||"))) --> {'key_1': 'value_1', 'key_2': 'value_2'} ChrisA From alister.ware at ntlworld.com Mon Nov 19 15:44:31 2012 From: alister.ware at ntlworld.com (Alister) Date: Mon, 19 Nov 2012 20:44:31 GMT Subject: Point of idle curiosity References: <50a905f7$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, 18 Nov 2012 16:08:34 +0000, Mark Lawrence wrote: > On 18/11/2012 15:59, Steven D'Aprano wrote: >> On Sun, 18 Nov 2012 22:45:43 +1100, Chris Angelico wrote: >> >>> (if you'll forgive the pun) >>> >>> Is IDLE named after Eric of that name, or is it pure coincidence? >> >> Well, IDLE is an IDE. The L doesn't seem to mean anything, so it's >> plausible that it is named after Eric Idle. >> >> >> > https://en.wikipedia.org/wiki/IDLE_%28Python%29 i think it is fairly safe to assume that for most (if not all) good sounding acronyms the acronym was chosen before the meaning or at least chosen from short list of possible meanings because it looked good. -- I THINK MAN INVENTED THE CAR by instinct. -- Jack Handley, The New Mexican, 1988. From gordon at panix.com Mon Nov 19 15:50:10 2012 From: gordon at panix.com (John Gordon) Date: Mon, 19 Nov 2012 20:50:10 +0000 (UTC) Subject: Robust regex References: Message-ID: In "Joseph L. Casale" writes: > Trying to robustly parse a string that will have key/value pairs separated > by three pipes, where each additional key/value (if more than one exists) > will be delineated by four more pipes. > string = 'key_1|||value_1||||key_2|||value_2' > regex = '((?:(?!\|\|\|).)+)(?:\|\|\|)((?:(?!\|\|\|).)+)(?:\|\|\|\|)?' > I am not convinced this is the most effective or safest, any opinions would > be greatly appreciated! Regexes may be overkill here. A simple string split might be better: string = 'key_1|||value_1||||key_2|||value_2' pairs = string.split('||||') for pair in pairs: keyval = pair.split('|||') print '%s=%s' % (keyval[0], keyval[1]) -- John Gordon A is for Amy, who fell down the stairs gordon at panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" From python at mrabarnett.plus.com Mon Nov 19 15:50:46 2012 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 19 Nov 2012 20:50:46 +0000 Subject: Robust regex In-Reply-To: References: Message-ID: <50AA9BA6.5060508@mrabarnett.plus.com> On 2012-11-19 20:32, Joseph L. Casale wrote: > Trying to robustly parse a string that will have key/value pairs separated > by three pipes, where each additional key/value (if more than one exists) > will be delineated by four more pipes. > > string = 'key_1|||value_1||||key_2|||value_2' > regex = '((?:(?!\|\|\|).)+)(?:\|\|\|)((?:(?!\|\|\|).)+)(?:\|\|\|\|)?' > > I am not convinced this is the most effective or safest, any opinions would > be greatly appreciated! > Do you need to use regex? It would be simpler to use the .split method: for pair in string.split("||||"): key, value = pair.split("|||") print("key is {!r}, value is {!r}".format(key, value)) From rosuav at gmail.com Mon Nov 19 15:54:52 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 20 Nov 2012 07:54:52 +1100 Subject: Point of idle curiosity In-Reply-To: References: Message-ID: On Mon, Nov 19, 2012 at 11:38 PM, Ulrich Eckhardt wrote: > Am 18.11.2012 12:45, schrieb Chris Angelico: >> Is IDLE named after Eric of that name, or is it pure coincidence? > > > Maybe. Interestingly, there is also http://eric-ide.python-projects.org/, > just to add some more unfounded conspiracy theories to this discussion. :P Wait, that's completely separate. I don't think Eric Idle was ever a member of the Eric Conspiracy. http://www.catb.org/~esr/ecsl/ ChrisA From rosuav at gmail.com Mon Nov 19 16:35:01 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 20 Nov 2012 08:35:01 +1100 Subject: Python questions help In-Reply-To: References: <20121116100054.54257de92fb4ed6e65a28126@lavabit.com> Message-ID: On Tue, Nov 20, 2012 at 1:57 AM, Neil Cerutti wrote: > On 2012-11-16, Chris Angelico wrote: >> On Sat, Nov 17, 2012 at 5:00 AM, rh >> wrote: >>> "How many people think programming skills are inherent?" i.e. >>> that some people are just born with the gift to be good >>> programmers Result: very few hands raised maybe a couple >>> (possibly non-progammers??) >> >> Maybe, but there's definitely something that happens close to >> birth. If your parents give you the name Chris, you're more >> likely to become a geek and a programmer. > > There are people with rare talent who can program in a way that > most others can't, .e.g, Chris Sawyer. But, as Louis Moyse, a > great musician remarked: "Without hard work, talent means > nothing." Sure, it definitely takes work. You still have to put in your ten thousand hours. I don't know what the connection is, but there do seem to be a LOT of geeky Chrises; in fact, in any mid-length thread here on python-list, you could probably conclude with a "Thanks for the tip, Chris, it works now!" without even bothering to read it. ChrisA From ramit.prasad at jpmorgan.com Mon Nov 19 16:42:00 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Mon, 19 Nov 2012 21:42:00 +0000 Subject: xml data or other? In-Reply-To: <50A8E36A.5010606@gmail.com> References: <96b24715-cb4b-4588-844e-fc2e2f51a170@m4g2000pbd.googlegroups.com> <50A8E36A.5010606@gmail.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF474167B8C1C@SCACMX008.exchad.jpmchase.net> Artie Ziff wrote: > > On 11/9/12 5:50 AM, rusi wrote: > > On Nov 9, 5:54 pm, Artie Ziff wrote: > > # submit correctedinput to etree > I was very grateful to get the "leg up" on getting started down that > right path with my coding. Many thanks to you, rusi. I took your > excellent advices and have this working. > > class Converter(): > PREFIX = """ > > """ > POSTFIX = "" > def __init__(self, data): > self.data = data > self.writeXML() > def writeXML(self): > pattern = re.compile('') > replaceStr = r'' > xmlData = re.sub(pattern, replaceStr, self.data) > self.dataXML = self.PREFIX + xmlData.replace("\\", "/") + > self.POSTFIX > > ### main > # input to script is directory: > # sanitize trailing slash > testPkgDir = sys.argv[1].rstrip('/') > # Within each test package directory is doc/testcase > tcDocDir = "doc/testcases" > # set input dir, containing broken files > tcTxtDir = os.path.join(testPkgDir, tcDocDir) > # set output dir, to write proper XML files > tcXmlDir = os.path.join(testPkgDir, tcDocDir + "_XML") > if not os.path.exists(tcXmlDir): > os.makedirs(tcXmlDir) > # iterate through files in input dir > for filename in os.listdir(tcTxtDir): > # set filepaths > filepathTXT = os.path.join(tcTxtDir, filename) > base = os.path.splitext(filename)[0] > fileXML = base + ".xml" > filepathXML = os.path.join(tcXmlDir, fileXML) > # read broken file, convert to proper XML > with open(filepathTXT) as f: > c = Converter(f.read()) > xmlFO = open(filepathXML, 'w') # xmlFileObject > xmlFO.write(c.dataXML) > xmlFO.close() > > ### > > Writing XML files so to see whats happening. My plan is to > keep xml data in memory and parse with xml.etree.ElementTree. > > Unfortunately, xml parsing fails due to angle brackets inside > description tags. In particular, xml.etree.ElementTree.parse() > aborts on '<' inside xml data such as the following: > > > > This testcase tests if crontab installs the cronjob > and cron schedules the job correctly. > <\description> > > ## > > What is right way to handle the extra angle brackets? > Substitute on line-by-line basis, if that works? > Or learn to write a simple stack-style parser, or > recursive descent, it may be called? I think your description text should be in a CDATA section. http://en.wikipedia.org/wiki/CDATA#CDATA_sections_in_XML ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From jcasale at activenetwerx.com Mon Nov 19 18:37:05 2012 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Mon, 19 Nov 2012 23:37:05 +0000 Subject: Robust regex In-Reply-To: References: , Message-ID: > Regexes may be overkill here. A simple string split might be better: Yup, and much more robust as I was looking for. Thanks everyone! jlc From steve+comp.lang.python at pearwood.info Mon Nov 19 18:42:17 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 19 Nov 2012 23:42:17 GMT Subject: Python Interview Questions References: <1193768041.349129.26350@v3g2000hsg.googlegroups.com> <55443eb7-847c-4f4c-8d04-1e6b507aac00@googlegroups.com> <50a8acdc$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a911ec$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a97de0$0$29983$c3e8da3$5496439d@news.astraweb.com> <50a9e5cf$0$21863$c3e8da3$76491128@news.astraweb.com> Message-ID: <50aac3d8$0$29983$c3e8da3$5496439d@news.astraweb.com> On Mon, 19 Nov 2012 09:30:54 -0500, Roy Smith wrote: > In article <50a9e5cf$0$21863$c3e8da3$76491128 at news.astraweb.com>, > Steven D'Aprano wrote: > >> I see. It wasn't clear from your earlier description that the items had >> been post-processed from collections of raw log lines to fixed records. > > Well, I did provide the code that does this. You did? When? [goes back and looks] Oh, so you did. Oops. By the way, your news client seems to be mangling long URLs, by splitting them when they exceed the maximum line length. I didn't follow the link you gave because it was mangled, and then forgot it even existed. Sorry about that. [...] > You really might want to read the code I provided. Here's the reference > again: > > https://bitbucket.org/roysmith/python-tools/src/4f8118d175ed/logs/ traceba > ck_helper.py And mangled again :) > The "header" referred to does indeed contain the exception raised. And > the line numbers are included. Here's a typical output stanza: [snip] Ian Kelly has picked up on what I'm trying to say. You might collect the traceback in the "header", but it doesn't get used in the key, and each time you find a repeated stack trace, you toss away whatever header you just saw and keep the header you saw the first time. [quote] header, stack = traceback_helper.extract_stack(lines) signature = tuple(stack) if signature in crashes: count, header = crashes[signature] crashes[signature] = (count + 1, header) else: crashes[signature] = (1, header) [end quote] In general, it is an unsafe assumption that the actual exception raised will be the same just because the stack trace is the same. So as I said, if you have two *distinct* failures occurring in the same function (not even necessarily on the same line), your code appears to treat them as the same error. That seems odd to me, but if you have a good reason for doing it that way, so be it. -- Steven From kevinintx at gmail.com Mon Nov 19 18:43:10 2012 From: kevinintx at gmail.com (Kevin T) Date: Mon, 19 Nov 2012 15:43:10 -0800 (PST) Subject: re.search when used within an if/else fails Message-ID: python version 2.4.3, yes i know that it is old. getting the sysadmin to update the OS requires a first born. with the following code.. for signal in register['signals'] : 351 sigName = signal['functionName'] 352 if re.search( "rsrvd", sigName ) == None : 353 print sigName 354 newVal = "%s%s" % ( '1'*signal['bits'] , newVal ) #prepend 0's 355 if re.search( "rsrvd", sigName ) != None : 356 print sigName 357 newVal = "%s%s" % ( '0'*signal['bits'], newVal ) regardless of how i code line 352, i can not EVER use an else clause with it. if i use an else clause, the else will NEVER get executed... has any one experienced anything like this behavior? any suggestions? the above code works but... why should i have to code it like this? kevin From stephane at wirtel.be Mon Nov 19 18:43:52 2012 From: stephane at wirtel.be (=?ISO-8859-1?Q?St=E9phane_Wirtel?=) Date: Tue, 20 Nov 2012 00:43:52 +0100 Subject: Call of Papers - FOSDEM 2013 - Python Devroom Message-ID: <50AAC438.1010204@wirtel.be> Hi all, This is the official call for sessions for the FOSDEM 2013 [1] python devroom. This year, Python will be represented by its Community. If you want to discuss with a lot of Python Users, it's the place to be in February ! Like every year, FOSDEM [1] will take place the first week-end of February in Brussels (Belgium). We will have a room in the K building (80 seats). This dev room will be open all day Sunday, February 3rd. If you want to hold a session in the Python devroom, please fill this survey [2] Call for Papers is open until December 21st This year, the submissions will be reviewed by a committee: * Tarek Ziade - https://twitter.com/tarek_ziade (Mozilla) * Ludovic Gasc - https://twitter.com/GMLudo (Eyepea) * Christophe Simonis - https://twitter.com/KangOl (OpenERP) * Stephane Wirtel - https://twitter.com/matrixise (OpenERP) Thank you for submitting your sessions and see you soon in Brussels to talk Python and/or have some nice Belgian Beers. [1] http://fosdem.org/2013/ [2] https://docs.google.com/spreadsheet/viewform?formkey=dDJUWjhEYW1CRzVrSERVSmVlYkJFbnc6MQ Please, could you share this information, thank you Best regards, Stephane Wirtel (@matrixise) From stephane at wirtel.be Mon Nov 19 18:44:59 2012 From: stephane at wirtel.be (=?ISO-8859-1?Q?St=E9phane_Wirtel?=) Date: Tue, 20 Nov 2012 00:44:59 +0100 Subject: Call of Papers - FOSDEM 2013 - Python Devroom Message-ID: <50AAC47B.3070100@wirtel.be> Hi all, This is the official call for sessions for the FOSDEM 2013 [1] python devroom. This year, Python will be represented by its Community. If you want to discuss with a lot of Python Users, it's the place to be in February ! Like every year, FOSDEM [1] will take place the first week-end of February in Brussels (Belgium). We will have a room in the K building (80 seats). This dev room will be open all day Sunday, February 3rd. If you want to hold a session in the Python devroom, please fill this survey [2] Call for Papers is open until December 21st This year, the submissions will be reviewed by a committee: * Tarek Ziade - https://twitter.com/tarek_ziade (Mozilla) * Ludovic Gasc - https://twitter.com/GMLudo (Eyepea) * Christophe Simonis - https://twitter.com/KangOl (OpenERP) * Stephane Wirtel - https://twitter.com/matrixise (OpenERP) Thank you for submitting your sessions and see you soon in Brussels to talk Python and/or have some nice Belgian Beers. [1] http://fosdem.org/2013/ [2] https://docs.google.com/spreadsheet/viewform?formkey=dDJUWjhEYW1CRzVrSERVSmVlYkJFbnc6MQ Here is the link of this announce: http://wirtel.be/posts/en/2012/11/20/call_of_papers_fosdem_2013/ Please, could you share this information, thank you Best regards, Stephane Wirtel (@matrixise) From steve+comp.lang.python at pearwood.info Mon Nov 19 18:53:16 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 19 Nov 2012 23:53:16 GMT Subject: Python Interview Questions References: <1193768041.349129.26350@v3g2000hsg.googlegroups.com> <55443eb7-847c-4f4c-8d04-1e6b507aac00@googlegroups.com> <50a8acdc$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a911ec$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a97de0$0$29983$c3e8da3$5496439d@news.astraweb.com> <50a9e5cf$0$21863$c3e8da3$76491128@news.astraweb.com> Message-ID: <50aac66c$0$29983$c3e8da3$5496439d@news.astraweb.com> On Mon, 19 Nov 2012 09:59:19 -0500, Roy Smith wrote: > OK, I've just read back over the whole thread. I'm really struggling to > understand what point you're trying to make. I started out by saying: > >> Use a list when you need an ordered collection which is mutable (i.e. >> can be altered after being created). Use a tuple when you need an >> immutable list (such as for a dictionary key). > > To which you obviously objected. So now you write: > >> I think a tuple is an immutable sequence of items, and a list is a >> mutable sequence of items. > > So how is that different from what I said? Is this whole argument > boiling down to your use of "immutable sequence" vs. my use of > "immutable list"? Sheesh, of course not. Give me some credit. I gave some examples of when somebody might use lists, tuples, sets and dicts. Apparently I forgot a couple, and you responded with a sarcastic comment about the "One True Church Of Pythonic Orthodoxy And Theoretical Correctness" and gave a couple of additional examples. Although I didn't come out and *explicitly* say "I agree" to your examples, I actually did, with one proviso: your example of using an "immutable list" as dict key. So I asked a question about that *specific* use-case: [quote] Under what sort of circumstances would somebody want to take a mutable list of data, say a list of email addresses, freeze it into a known state, and use that frozen state as a key in a dict? What would be the point? Even if there was some meaningful reason to look up "this list of 12000 email addresses" as a single key, it is going to get out of sync with the actual mutable list. [end quote] Your reply was to give your stack trace script as an example. That's a fine example as a use-case for a temporary list, and I've done similar things dozens, hundreds of times myself. As I said: [quote] Sure, I have built a collection of items as a list, because lists are mutable, then frozen it into a tuple, and *thrown the list away*, then used the tuple as a key. But that's not the same thing, the intent is different. In my case, the data was never intended to be a list, it was always intended to be a fixed record-like collection, the use of list was as a temporary data structure used for construction. A bit like the idiom of ''.join(some_list). [end quote] To me, this sounds *exactly* like your use-case: your data, stack traces, represent a little chunk of immutable data that you build up a line at a time using a temporary list first, just like I wrote. And I said so. There's no sign in either your code or your description that the stack traces get treated as mutable objects in any way once you have finished building them a line at a time. So your real world, practical, "in the trenches" example matches my experience: you build a *fixed data record* using a *temporary list*, throw the list away, and then never mutate that data record again. So why are we disagreeing? Like many such discussions on the Internet, this one has rambled a bit, and I've misunderstood some of your code (sorry), and you seem to have misunderstood the question I am asking. Maybe my explanation was not clear enough, in which case, sorry again. I'm asking about the case where one might want the key to remain mutable even after it is used as a key, but can't because Python won't let you. There's no sign that your stack trace example is such an example. As I earlier said: [quote] But I can't think of any meaningful, non-contrived example where I might want an actual mutable list of values as a dict key. [end quote] and I still can't. -- Steven From ramit.prasad at jpmorgan.com Mon Nov 19 18:57:43 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Mon, 19 Nov 2012 23:57:43 +0000 Subject: Python Interview Questions In-Reply-To: References: <1193768041.349129.26350@v3g2000hsg.googlegroups.com> <55443eb7-847c-4f4c-8d04-1e6b507aac00@googlegroups.com> <50a8acdc$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a911ec$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a97de0$0$29983$c3e8da3$5496439d@news.astraweb.com> <50a9e5cf$0$21863$c3e8da3$76491128@news.astraweb.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF474167B949E@SCACMX008.exchad.jpmchase.net> Roy Smith wrote: > > OK, I've just read back over the whole thread. I'm really struggling to > understand what point you're trying to make. I started out by saying: > > > Use a list when you need an ordered collection which is mutable (i.e. > > can be altered after being created). Use a tuple when you need an > > immutable list (such as for a dictionary key). > > To which you obviously objected. So now you write: > > > I think a tuple is an immutable sequence of items, and a list is a > > mutable sequence of items. > > So how is that different from what I said? Is this whole argument > boiling down to your use of "immutable sequence" vs. my use of > "immutable list"? ''' Roy: > Use a list when you need an ordered collection which is mutable (i.e. > can be altered after being created). Use a tuple when you need an > immutable list (such as for a dictionary key). Steven: I keep hearing about this last one, but I wonder... who *actually* does this? I've created many, many lists over the years -- lists of names, lists of phone numbers, lists of directory search paths, all sorts of things. I've never needed to use one as a dictionary key. ''' To me this is more of a question than an argument. Now moving on to your specific example. ''' def extract_stack(lines): "in traceback_helper module " header = lines[0] stack = [] for line in lines: m = frame_pattern.match(line) if not m: continue frame = (m.group('path'), m.group('function')) stack.append(frame) # [Convert to tuple and return after finished building stack.] return (header, stack) [...] def main(args): crashes = {} [...] for line in open(log_file): if does_not_look_like_a_stack_dump(line): continue lines = traceback_helper.unfold(line) header, stack = traceback_helper.extract_stack(lines) signature = tuple(stack) if signature in crashes: count, header = crashes[signature] crashes[signature] = (count + 1, header) else: crashes[signature] = (1, header) ''' Seems to me that Steven is suggesting that stack (after being built) should converted to a tuple before being returned, because a "stack" for any unique exception should be unique and immutable. You do this anyway; you just do it before putting it into a dictionary rather than before returning it. Same net effect (as long as you do not modify `stack` later), so no real argument. ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From josh at merchantconcepts.com Mon Nov 19 19:05:30 2012 From: josh at merchantconcepts.com (josh at merchantconcepts.com) Date: Mon, 19 Nov 2012 16:05:30 -0800 (PST) Subject: Splitting a line while keeping quoted items together Message-ID: <80a8cdf4-92b2-4f6b-84b2-cfff30ec0a8f@googlegroups.com> I am working on a cmd.Cmd-based program, and normally could just split the string and get the right parts. Now I have a case where I could have two or three words in the string that need to be grouped into the same thing. Then I realized that I'm not the only person who has had to deal with this, and I'm wondering if my solution is the best one out there or if this is as ugly at it feels? Code below ....... #x('Seattle 456') -> ('Seattle', '456') #x('"Portland Alpha" 123') -> ('Portland Alpha', '123') #x("'Portland Beta' 789') -> ('Portland Beta', '789') def x(line): res = [] append = False appended = None quote = None for item in line.split(): if append: if item.endswith(quote): appended.append(item[:-1]) res.append(' '.join(appended)) quote = None appended = None append = False else: appended.append(item) else: if item[0] in ["'",'"']: append = True appended = [item[1:]] quote = item[0] else: res.append(item) return res ...... This seem really ugly. Is there a cleaner way to do this? Is there a keyword I could search by to find something nicer? Thanks, Josh From clp2 at rebertia.com Mon Nov 19 19:21:09 2012 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 19 Nov 2012 16:21:09 -0800 Subject: Splitting a line while keeping quoted items together In-Reply-To: <80a8cdf4-92b2-4f6b-84b2-cfff30ec0a8f@googlegroups.com> References: <80a8cdf4-92b2-4f6b-84b2-cfff30ec0a8f@googlegroups.com> Message-ID: On Monday, November 19, 2012, wrote: > I am working on a cmd.Cmd-based program, and normally could just split the > string and get the right parts. > > Now I have a case where I could have two or three words in the string that > need to be grouped into the same thing. > > Then I realized that I'm not the only person who has had to deal with > this, and I'm wondering if my solution is the best one out there or if this > is as ugly at it feels? > > Code below > ....... > > #x('Seattle 456') -> ('Seattle', '456') > #x('"Portland Alpha" 123') -> ('Portland Alpha', '123') > #x("'Portland Beta' 789') -> ('Portland Beta', '789') > > This seem really ugly. Is there a cleaner way to do this? Is there a > keyword I could search by to find something nicer? > Use the "shlex" module in the std lib? Cheers, Chris -- Cheers, Chris -- http://rebertia.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From josh at merchantconcepts.com Mon Nov 19 19:37:41 2012 From: josh at merchantconcepts.com (Joshua R English) Date: Mon, 19 Nov 2012 16:37:41 -0800 Subject: Splitting a line while keeping quoted items together In-Reply-To: Message-ID: An HTML attachment was scrubbed... URL: From cs at zip.com.au Mon Nov 19 19:41:32 2012 From: cs at zip.com.au (Cameron Simpson) Date: Tue, 20 Nov 2012 11:41:32 +1100 Subject: Linux compatibility In-Reply-To: References: Message-ID: <20121120004132.GA28382@cskk.homeip.net> On Mon, 19 Nov 2012 08:44:37 -0800 (PST), EDI Support | declaimed the following in | gmane.comp.python.general: | > I would like know if Python 2.4.3 will be compatible with Linux RHEL 5.5 or 6.1? It would help if you could qualify what you imagine "compatible with" to mean... On 19Nov2012 18:38, Dennis Lee Bieber wrote: | Uhm... | 1) Python 2.4.x is practically an antique. (I just updated my Windows | machines to 2.7 from 2.5 -- and 2.7 will be the last 2.x version) RHEL 5.x ships with python 2.4; a up to date RHEL 5.x box here has 2.4.3. I would not surprise me if that was what shipped with 5.0. One feature if the RHEL distro is that it is _stable_. (See Dennis' point (3)). They backport bugfixes and security fixes, but otherwise the base OS doesn't change API. This produces reliable, predictable behaviour for stuff you have deployed to such a platform. The price for that is that pretty soon the versions of things supplied are quite dated. Anyway, qualify what "compatible" is supposed to mean for you. -- Cameron Simpson in rec.moto, jsh wrote: > Dan Nitschke wrote: > > Ged Martin wrote: > > > On Sat, 17 May 1997 16:53:33 +0000, Dan Nitschke scribbled: > > > >(And you stay *out* of my dreams, you deviant little > > > >weirdo.) > > > Yeah, yeah, that's what you're saying in _public_.... > > Feh. You know nothing of my dreams. I dream entirely in text (New Century > > Schoolbook bold oblique 14 point), and never in color. I once dreamed I > > was walking down a flowchart of my own code, and a waterfall of semicolons > > was chasing me. (I hid behind a global variable until they went by.) > You write code in a proportional serif? No wonder you got extra > semicolons falling all over the place. No, I *dream* about writing code in a proportional serif font. It's much more exciting than my real life. /* dan: THE Anti-Ged -- Ignorant Yank (tm) #1, none-%er #7 */ Dan Nitschke peDANtic at best.com nitschke at redbrick.com From aung.thetnaing at gmail.com Mon Nov 19 19:49:13 2012 From: aung.thetnaing at gmail.com (Aung Thet Naing) Date: Mon, 19 Nov 2012 16:49:13 -0800 (PST) Subject: Stack_overflow error Message-ID: <1fd7672d-000b-4cbc-bd87-a9f37320c9e8@googlegroups.com> I'm having Stack_overflow exception in _ctypes_callproc (callproc.c). The error actually come from the: cleanup: for (i = 0; i < argcount; ++i) Py_XDECREF(args[i].keep); when args[i].keep->ob_refCnt == 1 Aung. From steve+comp.lang.python at pearwood.info Mon Nov 19 20:09:54 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 20 Nov 2012 01:09:54 GMT Subject: Splitting a line while keeping quoted items together References: <80a8cdf4-92b2-4f6b-84b2-cfff30ec0a8f@googlegroups.com> Message-ID: <50aad861$0$29983$c3e8da3$5496439d@news.astraweb.com> On Mon, 19 Nov 2012 16:05:30 -0800, josh wrote: > I am working on a cmd.Cmd-based program, and normally could just split > the string and get the right parts. > > Now I have a case where I could have two or three words in the string > that need to be grouped into the same thing. Try shlex.split. -- Steven From rosuav at gmail.com Mon Nov 19 20:15:55 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 20 Nov 2012 12:15:55 +1100 Subject: Stack_overflow error In-Reply-To: <1fd7672d-000b-4cbc-bd87-a9f37320c9e8@googlegroups.com> References: <1fd7672d-000b-4cbc-bd87-a9f37320c9e8@googlegroups.com> Message-ID: On Tue, Nov 20, 2012 at 11:49 AM, Aung Thet Naing wrote: > I'm having Stack_overflow exception in _ctypes_callproc (callproc.c). The error actually come from the: > > cleanup: > for (i = 0; i < argcount; ++i) > Py_XDECREF(args[i].keep); > > when args[i].keep->ob_refCnt == 1 Can you offer more details? I'm guessing you're using ctypes from a Python script; can you share the script with us? What Python version are you using? What procedure are you calling? ChrisA From python.list at tim.thechases.com Mon Nov 19 20:20:16 2012 From: python.list at tim.thechases.com (Tim Chase) Date: Mon, 19 Nov 2012 19:20:16 -0600 Subject: Splitting a line while keeping quoted items together In-Reply-To: References: Message-ID: <50AADAD0.5030909@tim.thechases.com> >> Use the "shlex" module in the std lib? > > Well color me ignorant. > > Works cleanly. I shouldn't have reinvented the wheel. I've experienced this enough: the csv module, option parsing, config-file parsing, logging, timeit, and pwd all come to mind as code I've written before realizing the stdlib already has it. Now, if my task sounds remotely like something that somebody else might have implemented already, my first stop is always to browse through the stdlib docs. Then try http://pypi.python.org/pypi to see if somebody else has already solved the problem without the solution getting into the stdlib. Only then do I proceed to trying to code up something of my own. -tkc From python at mrabarnett.plus.com Mon Nov 19 20:21:54 2012 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 20 Nov 2012 01:21:54 +0000 Subject: re.search when used within an if/else fails In-Reply-To: References: Message-ID: <50AADB32.6060509@mrabarnett.plus.com> On 2012-11-19 23:43, Kevin T wrote: > python version 2.4.3, yes i know that it is old. getting the sysadmin to update the OS requires a first born. > > with the following code.. > for signal in register['signals'] : > > 351 sigName = signal['functionName'] > 352 if re.search( "rsrvd", sigName ) == None : > 353 print sigName > 354 newVal = "%s%s" % ( '1'*signal['bits'] , newVal ) #prepend 0's > 355 if re.search( "rsrvd", sigName ) != None : > 356 print sigName > 357 newVal = "%s%s" % ( '0'*signal['bits'], newVal ) > > regardless of how i code line 352, i can not EVER use an else clause with it. if i use an else clause, the else will NEVER get executed... > > has any one experienced anything like this behavior? any suggestions? the above code works but... why should i have to code it like this? > Have you checked the indentation? There may be a mixture of tabs and spaces. A couple of points: 1. You should be using "is None" and "is not None" instead of "== None" and "!= None". 2. You don't need to use regex. Use "rsrvd" in sigName and "rsrvd" not in sigName. From steve+comp.lang.python at pearwood.info Mon Nov 19 20:24:54 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 20 Nov 2012 01:24:54 GMT Subject: re.search when used within an if/else fails References: Message-ID: <50aadbe6$0$29983$c3e8da3$5496439d@news.astraweb.com> On Mon, 19 Nov 2012 15:43:10 -0800, Kevin T wrote: > python version 2.4.3, yes i know that it is old. getting the sysadmin > to update the OS requires a first born. > > with the following code.. > for signal in register['signals'] : > > 351 sigName = signal['functionName'] 352 > if re.search( "rsrvd", sigName ) == None : 353 > print sigName 354 newVal = "%s%s" % ( > '1'*signal['bits'] , newVal ) #prepend 0's 355 if > re.search( "rsrvd", sigName ) != None : 356 print > sigName 357 newVal = "%s%s" % ( '0'*signal['bits'], > newVal ) Your code is mangled to the point of unreadability. Please resend, and make sure you send it as PLAIN TEXT and not as HTML ("rich text"), since many mail programs feel that they are allowed to arbitrarily rewrap HTML text however they like. Preferably simplify your example to the simplest example that we can run: http://sscce.org/ Being able to run it means you shouldn't put line numbers on the left. If you must draw our attention to a specific line, use a comment. This isn't 1975 and we're not programming in BASIC. # BAD don't do this: 350 do_this(x) 351 do_that(y, z) 352 if something: 353 do_something_else(x, y, z) # GOOD do this: do_this(x) do_that(y, z) if something: # LINE 352 do_something_else(x, y, z) > regardless of how i code line 352, i can not EVER use an else clause > with it. if i use an else clause, the else will NEVER get executed... The code you show doesn't actually have an `else` clause, which might explain why it doesn't get executed. By the way, you should not write "if something == None", always use "if something is None" or "is not None". Technically, there are some exceptions but if you have to ask what they are, you don't need to know *wink* -- Steven From steve+comp.lang.python at pearwood.info Mon Nov 19 20:29:20 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 20 Nov 2012 01:29:20 GMT Subject: re.search when used within an if/else fails References: <50aadbe6$0$29983$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50aadcf0$0$29983$c3e8da3$5496439d@news.astraweb.com> On Tue, 20 Nov 2012 01:24:54 +0000, Steven D'Aprano wrote: > Your code is mangled to the point of unreadability. Ah, never mind, that's *my* fault, not yours. Or rather, my news reader software. Sorry about the noise. The rest of my post still stands: - simplify your example to the simplest example that we can run http://sscce.org/ - don't put line numbers at the start of lines - your code doesn't have an else clause - use "if something is None", not == None. -- Steven From steve+comp.lang.python at pearwood.info Mon Nov 19 20:33:11 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 20 Nov 2012 01:33:11 GMT Subject: Linux compatibility References: <5ab8efb4-d66b-42f7-9301-7048bf38fcef@googlegroups.com> Message-ID: <50aaddd7$0$29983$c3e8da3$5496439d@news.astraweb.com> On Mon, 19 Nov 2012 08:44:37 -0800, EDI Support wrote: > Hi All, > > I would like know if Python 2.4.3 will be compatible with Linux RHEL > 5.5 or 6.1? I don't see any reason why it wouldn't be, but why would you want to use Python 2.4 in production if you don't have to? RHEL will come with Python already installed, I believe it is Python 2.6. It is *much* better to use Python 2.6 if you can: it is faster, has fewer bugs, and more modern features. But if you must use Python 2.4, make sure that you do NOT replace the system Python already installed. If installing from source, use "make altinstall" instead of "make install" to ensure that it doesn't overwrite the system Python. -- Steven From d at davea.name Mon Nov 19 20:52:13 2012 From: d at davea.name (Dave Angel) Date: Mon, 19 Nov 2012 20:52:13 -0500 Subject: Problems on these two questions In-Reply-To: References: <400c615f-4d93-4b8b-85ec-1bb4c47e30fe@googlegroups.com> <50A996EB.2000600@davea.name> Message-ID: <50AAE24D.5000602@davea.name> On 11/19/2012 06:16 PM, Dennis Lee Bieber wrote: > On Sun, 18 Nov 2012 21:18:19 -0500, Dave Angel declaimed > the following in gmane.comp.python.general: > > >> if is_a_prime(n): >> is_prime = True >> >> Now all you have to do is write is_a_prime(). if you get stuck, please >> show us what you've got, and what the problem is with it. And as usual, >> tell us what version of Python you're writing in, if any. >> > Since "is_a_prime" is returning a Boolean, this condenses to: > > is_prime = is_a_prime(n) Nope, the original problem statement didn't say what should should happen if n does have such factors. Clearly, it's only describing a program fragment. """1.Given a positive integer n , assign True to is_prime if n has no factors other than 1 and itself. (Remember, m is a factor of n if m divides n evenly.) """ -- DaveA From breamoreboy at yahoo.co.uk Mon Nov 19 21:20:13 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 20 Nov 2012 02:20:13 +0000 Subject: [OT]Two-tonne Witch computer gets a reboot Message-ID: You may find this interesting http://www.bbc.co.uk/news/technology-20395212 -- Cheers. Mark Lawrence. From jaorozco at estudiantes.uci.cu Mon Nov 19 21:30:12 2012 From: jaorozco at estudiantes.uci.cu (jorge2) Date: Mon, 19 Nov 2012 21:30:12 -0500 Subject: Using cntlm as proxy Message-ID: <50AAEB34.7040905@estudiantes.uci.cu> Hi everyone. I'm using cntlm locally and I want my Python scripts to connect to some web, parse some data and come back with results all across my local proxy. I've tried a lot of example code but I still don't find what I need. I think I don't understand how urllib2 manages proxy. Help will be really appreciated. 10mo. ANIVERSARIO DE LA CREACION DE LA UNIVERSIDAD DE LAS CIENCIAS INFORMATICAS... CONECTADOS AL FUTURO, CONECTADOS A LA REVOLUCION http://www.uci.cu http://www.facebook.com/universidad.uci http://www.flickr.com/photos/universidad_uci From roy at panix.com Mon Nov 19 21:33:42 2012 From: roy at panix.com (Roy Smith) Date: Mon, 19 Nov 2012 21:33:42 -0500 Subject: Python Interview Questions References: <1193768041.349129.26350@v3g2000hsg.googlegroups.com> <55443eb7-847c-4f4c-8d04-1e6b507aac00@googlegroups.com> <50a8acdc$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a911ec$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a97de0$0$29983$c3e8da3$5496439d@news.astraweb.com> <50a9e5cf$0$21863$c3e8da3$76491128@news.astraweb.com> <50aac3d8$0$29983$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <50aac3d8$0$29983$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > By the way, your news client seems to be mangling long URLs, by splitting > them when they exceed the maximum line length. Hmmm. So it did. My bad. From wuwei23 at gmail.com Mon Nov 19 21:49:25 2012 From: wuwei23 at gmail.com (alex23) Date: Mon, 19 Nov 2012 18:49:25 -0800 (PST) Subject: Yet another Python textbook References: Message-ID: On Nov 20, 2:58?am, Kwpolska wrote: > You are writing it for something called ?NCLab?, not for the general > public, and that sucks. And making it available to the general public to consume. What's wrong with writing for one audience and providing for a broader? If you're that concerned with the "NCLab"-ness of it, fork it. > 2. IMO, you should be doing a bit more general usage programming, > not science-specific. It's produced by the Network Computing Laboratory for Science, Technology, Engineering and Mathematics, of course their focus will be on science. It's not like there's a lack of more generalised programming guides for Python. Some nice bikeshedding there, btw. From roy at panix.com Mon Nov 19 22:14:39 2012 From: roy at panix.com (Roy Smith) Date: Mon, 19 Nov 2012 22:14:39 -0500 Subject: Python Interview Questions References: <1193768041.349129.26350@v3g2000hsg.googlegroups.com> <55443eb7-847c-4f4c-8d04-1e6b507aac00@googlegroups.com> <50a8acdc$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a911ec$0$29978$c3e8da3$5496439d@news.astraweb.com> <50a97de0$0$29983$c3e8da3$5496439d@news.astraweb.com> <50a9e5cf$0$21863$c3e8da3$76491128@news.astraweb.com> <50aac66c$0$29983$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <50aac66c$0$29983$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > I'm asking about the case where one might want the key to remain mutable > even after it is used as a key, but can't because Python won't let you. Ah. Now I see what you're getting at. Thank you. Well, I will admit that it probably doesn't make sense to mutate an object after it's put into a dict (or at least mutate it in a way which changes it's hash value and/or whether it compares equal to the original object). If you did (assuming lists were allowed as keys): l = [1, 2, 3] d = {l: "spam"} l.append(4) print d[l] I'm not sure what I would expect to print. It's not too hard to experiment, though. All you need do is: class HashableList(list): def __hash__(self): return hash(tuple(self)) and python is then happy to let you use a list as a key. I just played around with this a bit off-line. I think I got the results I was expecting, but since I'm not sure what I was expecting, that's hard to say. However, you didn't ask if it made sense to mutate an object after using it as a key. You asked if it made sense to let the object remain mutable after using it as a key. That's a harder question. Let's say I had lots of of lists I wanted to use a dictionary keys. As it stands now, I have to convert them to tuples, which means copying all the data. For a lot of data, that's inefficient. Wouldn't it be nice (or at least, more efficient) if I could just use the original lists as keys directly, without the extra copy? I would have to understand that even though they are mutable, interesting (and perhaps, unwanted) things if I actually mutated them. But, we're all consenting adults here. If I'm willing to accept responsibility for the consequences of my actions in return for the efficiency gain, why shouldn't I be allowed to? I guess the answer is, that I am allowed to. I just need to do the HashableList deal, shown above (no broken URL required to read the code). From timr at probo.com Tue Nov 20 00:30:42 2012 From: timr at probo.com (Tim Roberts) Date: Mon, 19 Nov 2012 21:30:42 -0800 Subject: changing process name References: Message-ID: andrea crotti wrote: >I have very long processes to spawn which I want to lauch as separate >processes (and communicate with ZeroMQ), but now the problem is that the >forked process appears in "ps" with the same name as the launcher >process. http://code.google.com/p/py-setproctitle/ -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From frednotbob at hotmail.ca Tue Nov 20 00:45:55 2012 From: frednotbob at hotmail.ca (frednotbob at hotmail.ca) Date: Mon, 19 Nov 2012 21:45:55 -0800 (PST) Subject: Getting a seeded value from a list In-Reply-To: References: Message-ID: > > Are you generating the entire level on entry, or as each room is > > "opened" (if, "as opened", you have the complication that going down a > > level and back up will result in different random numbers vs going > > straight to the room). > > > > Generating on entry only needs the seed for the initial generation > > point (as mentioned, first time you could use the system clock and save > > the value). I'm generating the room on entry -- the 'stairs' object calls 'make_map()' which creates the floor and lays out monsters and treasure. What I'm trying to do is set a persistent state for the levels generated by make_map(), so the player can move between floors without generating a totally new randomized floor each time. > > But for a dungeon -- you may be best served by generating the entire > > level (all rooms, doors, static encounters [traps, treasure]) when > > entering the level, and saving the entire dungeon (after all, after a > > treasure is collected, it shouldn't re-appear the next time you start > > that same level -- if you only save the starting seed, then all > > encounters will also be regenerated). In this scenario, where you save > > the entire dungeon, you don't even need to worry about the seed -- > > you'll never really want to recreate the same dungeon for another party > > [unless running a competition in which case you seed every computer the > > same so every participant is running the identical dungeon]. That actually sounds close to what I'd like to ?do. How would I go about saving the dungeon? I'm guessing I'd need to define how many levels to generate, first of all.... From frednotbob at hotmail.ca Tue Nov 20 00:45:55 2012 From: frednotbob at hotmail.ca (frednotbob at hotmail.ca) Date: Mon, 19 Nov 2012 21:45:55 -0800 (PST) Subject: Getting a seeded value from a list In-Reply-To: References: Message-ID: > > Are you generating the entire level on entry, or as each room is > > "opened" (if, "as opened", you have the complication that going down a > > level and back up will result in different random numbers vs going > > straight to the room). > > > > Generating on entry only needs the seed for the initial generation > > point (as mentioned, first time you could use the system clock and save > > the value). I'm generating the room on entry -- the 'stairs' object calls 'make_map()' which creates the floor and lays out monsters and treasure. What I'm trying to do is set a persistent state for the levels generated by make_map(), so the player can move between floors without generating a totally new randomized floor each time. > > But for a dungeon -- you may be best served by generating the entire > > level (all rooms, doors, static encounters [traps, treasure]) when > > entering the level, and saving the entire dungeon (after all, after a > > treasure is collected, it shouldn't re-appear the next time you start > > that same level -- if you only save the starting seed, then all > > encounters will also be regenerated). In this scenario, where you save > > the entire dungeon, you don't even need to worry about the seed -- > > you'll never really want to recreate the same dungeon for another party > > [unless running a competition in which case you seed every computer the > > same so every participant is running the identical dungeon]. That actually sounds close to what I'd like to ?do. How would I go about saving the dungeon? I'm guessing I'd need to define how many levels to generate, first of all.... From stefan_ml at behnel.de Tue Nov 20 00:48:20 2012 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 20 Nov 2012 06:48:20 +0100 Subject: xml data or other? In-Reply-To: <5B80DD153D7D744689F57F4FB69AF474167B8C1C@SCACMX008.exchad.jpmchase.net> References: <96b24715-cb4b-4588-844e-fc2e2f51a170@m4g2000pbd.googlegroups.com> <50A8E36A.5010606@gmail.com> <5B80DD153D7D744689F57F4FB69AF474167B8C1C@SCACMX008.exchad.jpmchase.net> Message-ID: Prasad, Ramit, 19.11.2012 22:42: > Artie Ziff wrote: >> Writing XML files so to see whats happening. My plan is to >> keep xml data in memory and parse with xml.etree.ElementTree. >> >> Unfortunately, xml parsing fails due to angle brackets inside >> description tags. In particular, xml.etree.ElementTree.parse() >> aborts on '<' inside xml data such as the following: >> >> >> >> This testcase tests if crontab installs the cronjob >> and cron schedules the job correctly. >> <\description> >> >> ## >> >> What is right way to handle the extra angle brackets? >> Substitute on line-by-line basis, if that works? >> Or learn to write a simple stack-style parser, or >> recursive descent, it may be called? > > I think your description text should be in a CDATA section. > http://en.wikipedia.org/wiki/CDATA#CDATA_sections_in_XML Ah, don't bother with CDATA. Just make sure the data gets properly escaped, any XML serialiser will do that for you. Just generate the XML using ElementTree and you'll be fine. Generating XML as literal text is not a good idea. Stefan From ian.g.kelly at gmail.com Tue Nov 20 01:32:37 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 19 Nov 2012 23:32:37 -0700 Subject: Problems on these two questions In-Reply-To: References: <400c615f-4d93-4b8b-85ec-1bb4c47e30fe@googlegroups.com> Message-ID: On Mon, Nov 19, 2012 at 4:15 PM, Dennis Lee Bieber wrote: > On Sun, 18 Nov 2012 17:52:35 -0800 (PST), su29090 <129km09 at gmail.com> > declaimed the following in gmane.comp.python.general: > >> >> I all of the other problems but I have issues with these: >> >> 1.Given a positive integer n , assign True to is_prime if n has no factors other than 1 and itself. (Remember, m is a factor of n if m divides n evenly.) >> > Google: Sieve of Eratosthenes (might be mis-spelled) No, the Sieve is nifty, but it's meant for generating sequences of primes, not for testing individual primality. It's also more complex than is necessary. A better starting place for a programming novice is with trial division, which is a somewhat simpler algorithm and all that is needed here. From ian.g.kelly at gmail.com Tue Nov 20 02:46:37 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 20 Nov 2012 00:46:37 -0700 Subject: Yet another Python textbook In-Reply-To: References: Message-ID: On Sun, Nov 18, 2012 at 10:30 PM, Pavel Solin wrote: > I would like to introduce a new Python textbook > aimed at high school students: > > http://femhub.com/textbook-python/. > > The textbook is open source and its public Git > repository is located at Github: > > git at github.com:femhub/nclab-textbook-python.git > > Feedback and contributions are very much > welcome, every contributor becomes automatically > a co-author. First impression: I'm opening up the book and reading the introduction, and I get to section 1.6, and the very first code example given is: >>> print "Hello, World!" A fine tradition to be sure, but I have to say that I'm a little disappointed that a new textbook on Python being written in 2012 is focused squarely on Python 2, especially when I just read on the previous page that Python 3 was released in 2008. Is there any work underway get Python 3 into NCLab? The issue comes up again four pages later in section 2.4, when division is being demoed, and the text takes a page-and-a-half detour to caution about the use of floor division for expressions like: >>> 33 / 6 If the book were teaching Python 3, then this warning would be unnecessary, since division in Python 3 is *automatically* true division, unless you go out of your way to invoke floor division by using the special // operator. I think that the earliness and frequency that these differences arise underscore the point that it would be best if the book could simply be teaching Python 3 to start with. Getting off that soapbox and moving along, I notice that on pages 20-22 there are some examples by way of comparison that are written in C, which makes me wonder what audience this textbook is really intended for. The previous pages and references to Karel have given me the impression that this is geared toward beginning programmers, who most likely are not familiar with C. The most troublesome is the last of these examples, which is led up to with this text: The asterisks in the code below are pointers, an additional programming concept that one needs to learn and utilize here: This seems to suggest that the reader should stop reading here and do a Google search on pointers, in order to understand the example. Since this is not a textbook on C, and Python has no concept of pointers at all, doing this would be a complete waste of the reader's time. Skimming through a few more chapters, I don't see anything else that sets my spidey sense tingling. I hope that what I've written above gives you some things to consider, though. Cheers, Ian From solin.pavel at gmail.com Tue Nov 20 03:02:15 2012 From: solin.pavel at gmail.com (Pavel Solin) Date: Tue, 20 Nov 2012 00:02:15 -0800 Subject: Yet another Python textbook In-Reply-To: References: Message-ID: Hi Ian, thank you for your comments. On Mon, Nov 19, 2012 at 11:46 PM, Ian Kelly wrote: > On Sun, Nov 18, 2012 at 10:30 PM, Pavel Solin > wrote: > > I would like to introduce a new Python textbook > > aimed at high school students: > > > > http://femhub.com/textbook-python/. > > > > The textbook is open source and its public Git > > repository is located at Github: > > > > git at github.com:femhub/nclab-textbook-python.git > > > > Feedback and contributions are very much > > welcome, every contributor becomes automatically > > a co-author. > > First impression: I'm opening up the book and reading the > introduction, and I get to section 1.6, and the very first code > example given is: > > >>> print "Hello, World!" > :) > > A fine tradition to be sure, but I have to say that I'm a little > disappointed that a new textbook on Python being written in 2012 is > focused squarely on Python 2, especially when I just read on the > previous page that Python 3 was released in 2008. Is there any work > underway get Python 3 into NCLab? > There is an ongoing discussion but we are not sure. Are there any reasons except for the print () command and division of integers? > > The issue comes up again four pages later in section 2.4, when > division is being demoed, and the text takes a page-and-a-half detour > to caution about the use of floor division for expressions like: > > >>> 33 / 6 > > If the book were teaching Python 3, then this warning would be > unnecessary, since division in Python 3 is *automatically* true > division, unless you go out of your way to invoke floor division by > using the special // operator. I think that the earliness and > frequency that these differences arise underscore the point that it > would be best if the book could simply be teaching Python 3 to start > with. > Perhaps you are right. Is there any statistics of how many Python programmers are using 2.7 vs. 3? Most of people I know use 2.7. > > Getting off that soapbox and moving along, I notice that on pages > 20-22 there are some examples by way of comparison that are written in > C, which makes me wonder what audience this textbook is really > intended for. The previous pages and references to Karel have given > me the impression that this is geared toward beginning programmers, > who most likely are not familiar with C. That's exactly right. Unfortunately, many high school teachers are using C++ to teach programming to complete beginners. This comment is for them. It can be removed, you can do it if you like. The code is on Github. > The most troublesome is the > last of these examples, which is led up to with this text: > > The asterisks in the code below are pointers, an additional > programming concept that one needs to learn and utilize here: > > This seems to suggest that the reader should stop reading here and do > a Google search on pointers, in order to understand the example. > Since this is not a textbook on C, and Python has no concept of > pointers at all, doing this would be a complete waste of the reader's > time. > > Skimming through a few more chapters, I don't see anything else that > sets my spidey sense tingling. I hope that what I've written above > gives you some things to consider, though. > Thank you once more for the comments. Pavel > > Cheers, > Ian > -- Pavel Solin Associate Professor Applied and Computational Mathematics University of Nevada, Reno http://hpfem.org/~pavel -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Tue Nov 20 03:09:46 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 20 Nov 2012 19:09:46 +1100 Subject: Yet another Python textbook In-Reply-To: References: Message-ID: On Tue, Nov 20, 2012 at 7:02 PM, Pavel Solin wrote: > Perhaps you are right. Is there any statistics of how many Python > programmers are using 2.7 vs. 3? Most of people I know use 2.7. If you're teaching Python, the stats are probably about zero for zero. Start them off on Py3 and help move the world forward. ChrisA From steve+comp.lang.python at pearwood.info Tue Nov 20 03:16:08 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 20 Nov 2012 08:16:08 GMT Subject: Yet another Python textbook References: Message-ID: <50ab3c48$0$21863$c3e8da3$76491128@news.astraweb.com> On Mon, 19 Nov 2012 17:58:55 +0100, Kwpolska wrote: > On Mon, Nov 19, 2012 at 6:30 AM, Pavel Solin > wrote: >> I would like to introduce a new Python textbook aimed at high school >> students: >> >> http://femhub.com/textbook-python/. >> >> The textbook is open source and its public Git repository is located at >> Github: >> >> git at github.com:femhub/nclab-textbook-python.git > > URL for humans: https://github.com/femhub/nclab-textbook-python > > >> Feedback and contributions are very much welcome, every contributor >> becomes automatically a co-author. >> >> Best regards, >> >> Pavel >> >> > You are writing it for something called ?NCLab?, not for the general > public, and that sucks. I don't see why you say that it sucks. Why do you care if a free, useful Python text book drives some eyeballs to a website? -- Steven From nobody at nowhere.com Tue Nov 20 03:26:58 2012 From: nobody at nowhere.com (Nobody) Date: Tue, 20 Nov 2012 08:26:58 +0000 Subject: Getting a seeded value from a list References: Message-ID: On Mon, 19 Nov 2012 21:45:55 -0800, frednotbob wrote: > What I'm trying to do is set a persistent state for the levels generated > by make_map(), so the player can move between floors without generating a > totally new randomized floor each time. You need to distinguish between immutable data (e.g. walls) and mutable data (monsters, treasure). The former can be generated from the seed each time you enter the level; the latter must be generated the first time then stored. If the number of levels is reasonable, you can use a PRNG to generate a list of random numbers at game start, which are the seeds used to generate each level. Alternatively, you can generate a single random "game id" at game start then generate the seed for each level from a hash of the level number and the game id. When it comes to random level generation, a single sequential PRNG isn't always the best option, as it requires that you always use all of the generated numbers in the same order, even if you only want a subset of the level's data. This is particularly significant for large levels. It's sometimes better to use a hierarchy of PRNGs, where the values generated by higher-level PRNGs are used as seeds for the lower-level PRNGs. This way, if you need to generate a subset of the data, you only need to run the PRNGs for the specific subset, and their ancestors. E.g. if you want a 256x256 grid of random numbers, you could just generate 65536 random numbers and use them in top-to-bottom, left-to-right order. But if you only want the bottom-right tile, you still need to generate all of the preceding numbers to ensure that you get the correct value. A hierarchical approach would generate 4 numbers which are used as the seeds for the 4 128x128 quarters. Each quarter uses its seed to generate 4 more numbers for the 4 64x64 quarters. And so on until you get down to a 2x2 region, at which point the 4 numbers generated are the actual values used. This way, calculating a single cell only requires 4*log(n) (e.g. 4*8=32) values, while generating the entire grid only requires 33% more numbers for the higher levels (1 + 1/4 + 1/16 + 1/64 + ... = 1+1/3). From stephane.wirtel at gmail.com Tue Nov 20 03:37:26 2012 From: stephane.wirtel at gmail.com (=?ISO-8859-1?Q?St=E9phane_Wirtel?=) Date: Tue, 20 Nov 2012 09:37:26 +0100 Subject: Call of Papers - FOSDEM 2013 - Python Devroom Message-ID: <50AB4146.6060104@gmail.com> Hi all, This is the official call for sessions for the FOSDEM 2013 [1] python devroom. This year, Python will be represented by its Community. If you want to discuss with a lot of Python Users, it's the place to be in February ! Like every year, FOSDEM [1] will take place the first week-end of February in Brussels (Belgium). We will have a room in the K building (80 seats). This dev room will be open all day Sunday, February 3rd. If you want to hold a session in the Python devroom, please fill this survey [2] Call for Papers is open until December 21st This year, the submissions will be reviewed by a committee: * Tarek Ziade - https://twitter.com/tarek_ziade (Mozilla) * Ludovic Gasc - https://twitter.com/GMLudo (Eyepea) * Christophe Simonis - https://twitter.com/KangOl (OpenERP) * Stephane Wirtel - https://twitter.com/matrixise (OpenERP) Thank you for submitting your sessions and see you soon in Brussels to talk Python and/or have some nice Belgian Beers. [1] http://fosdem.org/2013/ [2] https://docs.google.com/spreadsheet/viewform?formkey=dDJUWjhEYW1CRzVrSERVSmVlYkJFbnc6MQ Here is the link of this announce: http://wirtel.be/posts/en/2012/11/20/call_of_papers_fosdem_2013/ Please, could you share this information, thank you Best regards, Stephane Wirtel (@matrixise) From insideshoes at gmail.com Tue Nov 20 04:06:02 2012 From: insideshoes at gmail.com (inshu chauhan) Date: Tue, 20 Nov 2012 10:06:02 +0100 Subject: Error In-Reply-To: <50A3E794.7080900@mrabarnett.plus.com> References: <50A3E794.7080900@mrabarnett.plus.com> Message-ID: thanx ..I understand the problem now.. On Wed, Nov 14, 2012 at 7:48 PM, MRAB wrote: > On 2012-11-14 15:18, inshu chauhan wrote: > >> >> for this code m getting this error : >> >> CODE : >> def ComputeClasses(data): >> radius = .5 >> points = [] >> for cy in xrange(0, data.height): >> for cx in xrange(0, data.width): >> if data[cy,cx] != (0.0,0.0,0.0): >> centre = data[cy, cx] >> points.append(centre) >> >> >> Look at this line: > > change = True >> >> It's indented the same as the preceding 'if' statement, which means > that it's executed even if the body of the 'if' statement wasn't > executed and it hasn't assigned to 'centre'. > > So 'change' has been set to True, the 'while' loop is entered, and > subsequently an attempt is made to get 'centre', which hasn't been set. > > > while change: >> >> for ring_number in xrange(1, 1000): >> change = False >> new_indices = GenerateRing(cx, cy, ring_number) >> >> >> for idx in new_indices: >> point = data[idx[0], idx[1]] >> >> if point == (0.0, 0.0, 0.0 ): >> continue >> else: >> dist = distance(centre, point) >> if dist < radius : >> print point >> points.append(point) >> change = True >> print change >> >> >> The indentation of this line looks wrong to me: > > break >> > But If I change the indentation of break towards inside, its going into infinite loop.. ??? > >> It'll affect the 'for cx' loop at the end of its first iteration, every > time. > > >> ERROR : >> Traceback (most recent call last): >> File "Z:\modules\classification2.**py", line 74, in >> ComputeClasses(data) >> File "Z:\modules\classification2.**py", line 56, in ComputeClasses >> dist = distance(centre, point) >> UnboundLocalError: local variable 'centre' referenced before assignment >> >> And i am unable to understand .. WHY ? >> >> > -- > http://mail.python.org/**mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From dfnsonfsduifb at gmx.de Tue Nov 20 05:02:14 2012 From: dfnsonfsduifb at gmx.de (Johannes Bauer) Date: Tue, 20 Nov 2012 11:02:14 +0100 Subject: Greedy parsing of argparse/positional arguments Message-ID: Hi list, I have a problem with Python3.2's argparse module. The following sample: parser = argparse.ArgumentParser(prog = sys.argv[0]) parser.add_argument("-enc", metavar = "enc", nargs = "+", type = str, default = [ "utf-8" ]) parser.add_argument("pattern", metavar = "pattern", type = str, nargs = 1) parser.add_argument("filename", metavar = "filename", type = str, nargs = 1) args = parser.parse_args(sys.argv[1:]) illustrates the problem: I want to be able to specify an encoding one or more times (multiple encodings possible), have a pattern and a filename as the last two arguments. This works as long as I don't specify '-enc' on the command line. If I do, for example ./foo -enc myencoding mypattern myfile The "-enc" greedy parser seems to capture ["myencoding", "mypattern", "myfile"], leaving nothing for "pattern" and "filename", yielding an error: ./foo: error: too few arguments How can I force positional arguments to take precedence over optional arguments? I could exclude them from the parsing altogether, but that would make them not appear in the help page (which I'd like to avoid). Best regards, Johannes -- >> Wo hattest Du das Beben nochmal GENAU vorhergesagt? > Zumindest nicht ?ffentlich! Ah, der neueste und bis heute genialste Streich unsere gro?en Kosmologen: Die Geheim-Vorhersage. - Karl Kaos ?ber R?diger Thomas in dsa From andriy.kornatskyy at live.com Tue Nov 20 07:00:58 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Tue, 20 Nov 2012 15:00:58 +0300 Subject: Python Web Routing Benchmark In-Reply-To: References: Message-ID: Web Routing Benchmark has been updated with latest version of various web frameworks.? http://mindref.blogspot.com/2012/10/python-web-routing-benchmark.html Note, wheezy.web seo routing benchmark has been improved by approximately 40%. Thanks. Andriy Kornatskyy ---------------------------------------- > From: andriy.kornatskyy at live.com > To: python-list at python.org > Subject: Python Web Routing Benchmark > Date: Wed, 10 Oct 2012 17:05:08 +0300 > > > How fast web frameworks process routing (URL dispatch)? > > Here is a benchmark for various web frameworks (bottle, django, flask, pyramid, tornado and wheezy.web) running the following routing: static, dynamic, SEO and missing... with a trivial 'hello world' application (all routes are pointing to the same handler). > > http://mindref.blogspot.com/2012/10/python-web-routing-benchmark.html > > Benchmark is executed in isolated environment using CPython 2.7. Source is here: > > https://bitbucket.org/akorn/helloworld/src/tip/02-routing > > Comments or suggestions are welcome. > > Thanks. > > Andriy Kornatskyy > > -- > http://mail.python.org/mailman/listinfo/python-list From michael.herrmann at getautoma.com Tue Nov 20 07:18:38 2012 From: michael.herrmann at getautoma.com (Michael Herrmann) Date: Tue, 20 Nov 2012 04:18:38 -0800 (PST) Subject: 10 sec poll - please reply! Message-ID: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> Hi, I'm developing a GUI Automation library (http://www.getautoma.com) and am having difficulty picking a name for the function that simulates key strokes. I currently have it as 'type' but that clashes with the built-in function. Example uses of 'type': type(ENTER) type("Hello World!") type(CTRL + 'a') What, in your view, would be the most intuitive alternative name? Here are my thoughts so far: I could call it 'press' but then our GUI automation tool also allows you to click things and then "press" might be mistaken for "pressing a button". A less ambiguous alternative is "type_keys" but that is rather long and doesn't read as well, for instance in type_keys(ENTER). Thank you very much! From rosuav at gmail.com Tue Nov 20 07:23:35 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 20 Nov 2012 23:23:35 +1100 Subject: 10 sec poll - please reply! In-Reply-To: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> Message-ID: On Tue, Nov 20, 2012 at 11:18 PM, Michael Herrmann wrote: > Hi, > > I'm developing a GUI Automation library (http://www.getautoma.com) and am having difficulty picking a name for the function that simulates key strokes. I currently have it as 'type' but that clashes with the built-in function. Example uses of 'type': > > type(ENTER) > > type("Hello World!") > > type(CTRL + 'a') > > What, in your view, would be the most intuitive alternative name? I've done similar things under names like "send_keys". ChrisA From insideshoes at gmail.com Tue Nov 20 07:31:42 2012 From: insideshoes at gmail.com (inshu chauhan) Date: Tue, 20 Nov 2012 13:31:42 +0100 Subject: Error In-Reply-To: References: <50A3E794.7080900@mrabarnett.plus.com> Message-ID: I did the following changes in this part of my programme.. now the refereence error is removed but its showing me another error : def ComputeClasses(data): radius = .5 points = [] for cy in xrange(0, data.height): for cx in xrange(0, data.width): if data[cy,cx] == (0.0,0.0,0.0): continue else : centre = data[cy, cx] print centre points.append(centre) change = True while change: for ring_number in xrange(1, 1000): change = False new_indices = GenerateRing(cx, cy, ring_number) for idx in new_indices: point = data[idx[0], idx[1]] if point == (0.0, 0.0, 0.0 ): continue else: dist = distance(centre, point) if dist < radius : print point points.append(point) change = True print change break print points ERROR : Traceback (most recent call last): File "Z:/modules/classification1.py", line 71, in ComputeClasses(data) File "Z:/modules/classification1.py", line 47, in ComputeClasses point = data[idx[0], idx[1]] error: index is out of range What is meant by this statement ' Index out of range ' ? Does it mean that my range 1, 1000 is exceeded ?? -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Tue Nov 20 08:01:52 2012 From: d at davea.name (Dave Angel) Date: Tue, 20 Nov 2012 08:01:52 -0500 Subject: Error In-Reply-To: References: <50A3E794.7080900@mrabarnett.plus.com> Message-ID: <50AB7F40.3070609@davea.name> On 11/20/2012 07:31 AM, inshu chauhan wrote: > I did the following changes in this part of my programme.. now the > refereence error is removed but its showing me another error : > > def ComputeClasses(data): > radius = .5 > points = [] > for cy in xrange(0, data.height): > for cx in xrange(0, data.width): > > if data[cy,cx] == (0.0,0.0,0.0): > continue > else : > centre = data[cy, cx] > print centre > points.append(centre) > > > change = True > > while change: > > for ring_number in xrange(1, 1000): > change = False > new_indices = GenerateRing(cx, cy, ring_number) > > for idx in new_indices: > point = data[idx[0], idx[1]] > > if point == (0.0, 0.0, 0.0 ): > continue > else: > > dist = distance(centre, point) > if dist < radius : > print point > points.append(point) > change = True > print change > > > break > > > print points > > > ERROR : > > Traceback (most recent call last): > File "Z:/modules/classification1.py", line 71, in > ComputeClasses(data) > File "Z:/modules/classification1.py", line 47, in ComputeClasses > point = data[idx[0], idx[1]] > error: index is out of range > > What is meant by this statement ' Index out of range ' ? Does it mean that > my range 1, 1000 is exceeded ?? > > When you're using custom classes that mimic the standard ones, the error can mean most anything. But assuming the design was to keep as close as possible, it simply means that you're subscripting a list with an index that's too large or too small. So if idx is a list that has only one element, element number zero, then idx[1] would be out of range. On the same line, if data is acting kind of like a two-dimensional list, then it has limits on each dimension, and either idx[0] is too big/small for the first dimension, or idx[1] is too big or small for the second. First thing is to figure out which part of this expression is causing the exception. So do a separate pair of assignments, dummy0 = idx[0] dummy1 = idx[1] and then point = data[dummy0, dummy1] Incidentally, if idx is a tuple or a list, of exactly two items, then you could just say point = data[*idx] Anyway, if that still doesn't make things clear, then print dummy0 and dummy1 before the point= line. That way you can see the last value, the one it dies on, just before the stack trace. Naturally, you could also print the size attributes of the data item as well. -- DaveA From steve+comp.lang.python at pearwood.info Tue Nov 20 08:09:04 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 20 Nov 2012 13:09:04 GMT Subject: 10 sec poll - please reply! References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> Message-ID: <50ab80f0$0$29987$c3e8da3$5496439d@news.astraweb.com> You know, you would probably get more responses if you picked a descriptive subject line that didn't look like spam. I only read your post because I accidentally clicked on it. On Tue, 20 Nov 2012 04:18:38 -0800, Michael Herrmann wrote: > I'm developing a GUI Automation library (http://www.getautoma.com) and By the way, your website is down. > am having difficulty picking a name for the function that simulates key > strokes. I currently have it as 'type' but that clashes with the > built-in function. Example uses of 'type': > > type(ENTER) > type("Hello World!") > type(CTRL + 'a') > > What, in your view, would be the most intuitive alternative name? I'd keep the name as "type". It exists in a different namespace to the builtin, so people have a choice: they can refer to the fully-qualified name, or not, as they prefer. I don't know the name of your module, since your website is down, but let's say it's called "automata": # Option 1 import automata automata.type("Hello World") # Option 2 from automata import type type("Hello World" import builtins # in Python 2 use __builtin__ instead builtins.type([]) So the name clash doesn't matter. -- Steven From michael.herrmann at getautoma.com Tue Nov 20 08:17:37 2012 From: michael.herrmann at getautoma.com (Michael Herrmann) Date: Tue, 20 Nov 2012 05:17:37 -0800 (PST) Subject: 10 sec poll - please reply! In-Reply-To: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> Message-ID: <64db1a8f-4606-4943-b0d0-0f97c1b986fe@googlegroups.com> Hi, thank you for your replies. So far two people said 'send_keys' and one person said 'type'. Steven, thanks for your reply. Sorry if the message title disturbed you. My personal feelings aren't too strongly against 'type' either, but then I'm afraid it might bother more experienced Python programmers who are used to a very different meaning of 'type'. Do you think that could be a problem? Thanks again to all who have replied, Michael On Tuesday, November 20, 2012 1:18:38 PM UTC+1, Michael Herrmann wrote: > Hi, > > > > I'm developing a GUI Automation library (http://www.getautoma.com) and am having difficulty picking a name for the function that simulates key strokes. I currently have it as 'type' but that clashes with the built-in function. Example uses of 'type': > > > > type(ENTER) > > > > type("Hello World!") > > > > type(CTRL + 'a') > > > > What, in your view, would be the most intuitive alternative name? > > > > Here are my thoughts so far: I could call it 'press' but then our GUI automation tool also allows you to click things and then "press" might be mistaken for "pressing a button". A less ambiguous alternative is "type_keys" but that is rather long and doesn't read as well, for instance in type_keys(ENTER). > > > > Thank you very much! From insideshoes at gmail.com Tue Nov 20 08:19:37 2012 From: insideshoes at gmail.com (inshu chauhan) Date: Tue, 20 Nov 2012 14:19:37 +0100 Subject: Error In-Reply-To: <50AB7F40.3070609@davea.name> References: <50A3E794.7080900@mrabarnett.plus.com> <50AB7F40.3070609@davea.name> Message-ID: Yes you are rightI figured that out after posting to python list.. actually my index is reaching the last point.. and my prog is not doing what I want.. I am wondering why it is reaching the last point in my list .. its never stopping in between ??? On Tue, Nov 20, 2012 at 2:01 PM, Dave Angel wrote: > On 11/20/2012 07:31 AM, inshu chauhan wrote: > > I did the following changes in this part of my programme.. now the > > refereence error is removed but its showing me another error : > > > > def ComputeClasses(data): > > radius = .5 > > points = [] > > for cy in xrange(0, data.height): > > for cx in xrange(0, data.width): > > > > if data[cy,cx] == (0.0,0.0,0.0): > > continue > > else : > > centre = data[cy, cx] > > print centre > > points.append(centre) > > > > > > change = True > > > > while change: > > > > for ring_number in xrange(1, 1000): > > change = False > > new_indices = GenerateRing(cx, cy, ring_number) > > > > for idx in new_indices: > > point = data[idx[0], idx[1]] > > > > if point == (0.0, 0.0, 0.0 ): > > continue > > else: > > > > dist = distance(centre, point) > > if dist < radius : > > print point > > points.append(point) > > change = True > > print change > > > > > > break > > > > > > print points > > > > > > ERROR : > > > > Traceback (most recent call last): > > File "Z:/modules/classification1.py", line 71, in > > ComputeClasses(data) > > File "Z:/modules/classification1.py", line 47, in ComputeClasses > > point = data[idx[0], idx[1]] > > error: index is out of range > > > > What is meant by this statement ' Index out of range ' ? Does it mean > that > > my range 1, 1000 is exceeded ?? > > > > > > When you're using custom classes that mimic the standard ones, the error > can mean most anything. But assuming the design was to keep as close as > possible, it simply means that you're subscripting a list with an index > that's too large or too small. So if idx is a list that has only one > element, element number zero, then idx[1] would be out of range. On the > same line, if data is acting kind of like a two-dimensional list, then > it has limits on each dimension, and either idx[0] is too big/small for > the first dimension, or idx[1] is too big or small for the second. > > First thing is to figure out which part of this expression is causing > the exception. So do a separate pair of assignments, > dummy0 = idx[0] > dummy1 = idx[1] > > and then point = data[dummy0, dummy1] > > Incidentally, if idx is a tuple or a list, of exactly two items, then > you could just say > point = data[*idx] > > Anyway, if that still doesn't make things clear, then print dummy0 and > dummy1 before the point= line. That way you can see the last value, the > one it dies on, just before the stack trace. Naturally, you could also > print the size attributes of the data item as well. > > > -- > > DaveA > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From michael.herrmann at getautoma.com Tue Nov 20 08:20:33 2012 From: michael.herrmann at getautoma.com (Michael Herrmann) Date: Tue, 20 Nov 2012 05:20:33 -0800 (PST) Subject: 10 sec poll - please reply! In-Reply-To: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> Message-ID: <79d530d3-e6ce-49da-857c-1b31a9a9f44c@googlegroups.com> P.S.: The website is back online; our hosting provider was having technical problems... On Tuesday, November 20, 2012 1:18:38 PM UTC+1, Michael Herrmann wrote: > Hi, > > > > I'm developing a GUI Automation library (http://www.getautoma.com) and am having difficulty picking a name for the function that simulates key strokes. I currently have it as 'type' but that clashes with the built-in function. Example uses of 'type': > > > > type(ENTER) > > > > type("Hello World!") > > > > type(CTRL + 'a') > > > > What, in your view, would be the most intuitive alternative name? > > > > Here are my thoughts so far: I could call it 'press' but then our GUI automation tool also allows you to click things and then "press" might be mistaken for "pressing a button". A less ambiguous alternative is "type_keys" but that is rather long and doesn't read as well, for instance in type_keys(ENTER). > > > > Thank you very much! From breamoreboy at yahoo.co.uk Tue Nov 20 08:21:56 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 20 Nov 2012 13:21:56 +0000 Subject: Error In-Reply-To: References: <50A3E794.7080900@mrabarnett.plus.com> Message-ID: On 20/11/2012 12:31, inshu chauhan wrote: > I did the following changes in this part of my programme.. now the > refereence error is removed but its showing me another error : > > def ComputeClasses(data): > radius = .5 > points = [] > for cy in xrange(0, data.height): > for cx in xrange(0, data.width): > > if data[cy,cx] == (0.0,0.0,0.0): > continue > else : > centre = data[cy, cx] > print centre > points.append(centre) > > > change = True > > while change: > > for ring_number in xrange(1, 1000): > change = False > new_indices = GenerateRing(cx, cy, ring_number) > > for idx in new_indices: > point = data[idx[0], idx[1]] > > if point == (0.0, 0.0, 0.0 ): > continue > else: > > dist = distance(centre, point) > if dist < radius : > print point > points.append(point) > change = True > print change > > > break > > > print points > > > ERROR : > > Traceback (most recent call last): > File "Z:/modules/classification1.py", line 71, in > ComputeClasses(data) > File "Z:/modules/classification1.py", line 47, in ComputeClasses > point = data[idx[0], idx[1]] > error: index is out of range > > What is meant by this statement ' Index out of range ' ? Does it mean that > my range 1, 1000 is exceeded ?? > > What makes you think the error applies to the value from the xrange call? The traceback tells you that the error has occurred at line 47. Looking at that and the lines above, I'd guess your problem lies in the return values from the GenerateRing function. -- Cheers. Mark Lawrence. From d at davea.name Tue Nov 20 08:23:34 2012 From: d at davea.name (Dave Angel) Date: Tue, 20 Nov 2012 08:23:34 -0500 Subject: Error In-Reply-To: References: <50A3E794.7080900@mrabarnett.plus.com> <50AB7F40.3070609@davea.name> Message-ID: <50AB8456.5050103@davea.name> On 11/20/2012 08:19 AM, inshu chauhan wrote: > Yes you are rightI figured that out after posting to python list.. actually > my index is reaching the last point.. and my prog is not doing what I > want.. I am wondering why it is reaching the last point in my list .. its > never stopping in between ??? > (Please don't top-post) If this is intended as a non-rhetorical question, a bit more specific wording is needed. Which index is reaching what last point? And in what loop is it skipping in-between values? -- DaveA From jeandubois314 at gmail.com Tue Nov 20 08:28:57 2012 From: jeandubois314 at gmail.com (Jean Dubois) Date: Tue, 20 Nov 2012 05:28:57 -0800 (PST) Subject: problem with module PyVisa References: <2f065a8d-5cf6-455f-b559-d1b78d225fca@10g2000vbu.googlegroups.com> <2325633a-640c-42f6-b8e7-930e475b7217@c20g2000vbz.googlegroups.com> Message-ID: <9ee686ea-18f6-4782-9058-7cc2405b3be0@f17g2000vbz.googlegroups.com> On 11 nov, 20:30, Jean Dubois wrote: > On 9 nov, 22:14, w... at mac.com wrote: > > > > > On Nov 9, 2012, at 3:43 PM, Jean Dubois wrote: > > > > The error may be obvious but finding this file and how to install it > > > is not unfortunately. > > > It seems I have to install it from the National Instruments site but > > > Debian Linux doesn't seem to be supported... > > > and I doubt whether just copying this file will be sufficient to make > > > PyVisa work. > > > I wonder whether there might be another way to communicate via USB > > > with a Keithley programmable power supply using Python. > > > > best regards, > > > Jean > > > > -- > > >http://mail.python.org/mailman/listinfo/python-list > > > I've been using pyserial quite successfully to control a USB-to-serial converter. > > > That is, controlling a couple of RS232 serial devices via the USB port through a KeySpan USB-to-Serial converter. > > > Pyserial seems to make communication through the USB port quite transparent, at least on my OS-X system. > > > -Bill > > Well, in fact I do have some working scripts using pyserial to control > an older (and more expensive) Keithley sourcemeter in combination with > a USB-to-serial converter. > But the trouble started when buying a cheaper and newer Keithley model > which does not have rs232 but only USB. I noticed they have put an > extra layer above USB called USBTMC > which complicates things further. I followed the instructions athttp://www.home.agilent.com/upload/cmc_upload/All/usbtmc.htm?&cc=BE&l...outline > and compiled and loaded the usbtmc-driver but I still can't > communicate with the Keithley, hence I started looking for an > alternative using PyVisa...and now I'm stuck > > jean I finally got it working without PyVisa as follows: #!/usr/bin/python #sample program for Keithley 2200 USB #first compile and load module usbtmc import os usbkeith = open('/dev/usbtmc1','r+') #next commando is very important #without it you can fetch data but not SET data usbkeith.write("SYST:REM" + "\n") usbkeith.write("*IDN?\n") identification=usbkeith.readline() print 'Found: ',identification usbkeith.write("SOUR:CURR 0.2A\n") usbkeith.write("SOUR:OUTP:STAT ON\n") usbkeith.write("MEAS:VOLT?\n") measurement=usbkeith.readline() print 'Measured voltage: ',measurement regards, jean From davea at dejaviewphoto.com Tue Nov 20 08:39:43 2012 From: davea at dejaviewphoto.com (Dave Angel) Date: Tue, 20 Nov 2012 08:39:43 -0500 Subject: 10 sec poll - please reply! In-Reply-To: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> Message-ID: <50AB881F.3070702@dejaviewphoto.com> On 11/20/2012 07:18 AM, Michael Herrmann wrote: > Hi, > > I'm developing a GUI Automation library (http://www.getautoma.com) and am having difficulty picking a name for the function that simulates key strokes. I currently have it as 'type' but that clashes with the built-in function. Example uses of 'type': > > type(ENTER) > > type("Hello World!") > > type(CTRL + 'a') > > What, in your view, would be the most intuitive alternative name? > > Here are my thoughts so far: I could call it 'press' but then our GUI automation tool also allows you to click things and then "press" might be mistaken for "pressing a button". A less ambiguous alternative is "type_keys" but that is rather long and doesn't read as well, for instance in type_keys(ENTER). > > Thank you very much! I also vote for send_keys(), even before I saw Chris' reply. 'type' is too overloaded a word. For example, in Windows, it's the command to display the contents of a file - it types it to the screen. From insideshoes at gmail.com Tue Nov 20 08:43:48 2012 From: insideshoes at gmail.com (inshu chauhan) Date: Tue, 20 Nov 2012 14:43:48 +0100 Subject: Index Error Message-ID: def distance(c, p): dist = sqrt( ((c[0]-p[0])**2) + ((c[1]-p[1])**2) + ((c[2]-p[2])**2) ) return dist def GenerateRing(x,y, N): Generates square rings around a point in data which has 300 columns(x) and 3000 rows(y) indices = [] for i in xrange(-N, N): indices.append((x+i, y-N)) indices.append((x+N, y+i)) indices.append((x-i, y+N)) indices.append((x-N, y-i)) return indices def ComputeClasses(data): radius = .5 points = [] for cy in xrange(0, data.height): for cx in xrange(0, data.width): if data[cy,cx] == (0.0,0.0,0.0): continue else : centre = data[cy, cx] points.append(centre) change = True while change: for ring_number in xrange(1, 100): change = False new_indices = GenerateRing(cx, cy, ring_number) print new_indices for idx in new_indices: I need help in this part as I am unable to device a method in which if the points are out of index,it should stop and if idx[0] >= 300 and idx[1] >= 3000: go to next centre and start generating rings from there.. and again if the index is out of range .. this should repeat continue else : point = data[idx[0], idx[1]] if point == (0.0, 0.0, 0.0 ): print point continue else: dist = distance(centre, point) print dist if dist < radius : and rings should be added only when this condition is satisfied print point points.append(point) change = True print change break print points ERROR now : data loaded [(296, 403), (298, 403), (298, 405), (296, 405), (297, 403), (298, 404), (297, 405), (296, 404)] ... I am printing Indices to know what index it dies out.. Traceback (most recent call last): File "Z:/modules/Classify.py", line 73, in ComputeClasses(data) File "Z:/modules/Classify.py", line 49, in ComputeClasses point = data[idx[0], idx[1]] error: index is out of range -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Tue Nov 20 08:48:34 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 21 Nov 2012 00:48:34 +1100 Subject: Index Error In-Reply-To: References: Message-ID: On Wed, Nov 21, 2012 at 12:43 AM, inshu chauhan wrote: > I need help in this part as I am unable to device a method in which if the > points are out of index,it should stop..... > > Traceback (most recent call last): > File "Z:/modules/Classify.py", line 73, in > ComputeClasses(data) > File "Z:/modules/Classify.py", line 49, in ComputeClasses > point = data[idx[0], idx[1]] > error: index is out of range > When Python throws an exception, you can catch it and handle it as you please. In this instance, it seems to me you want to break out of one loop (if I've read your comments correctly), so just bracket that loop with try... except. Start here: http://docs.python.org/3.3/tutorial/errors.html ChrisA From alvaro.combo at gmail.com Tue Nov 20 08:56:27 2012 From: alvaro.combo at gmail.com (Alvaro Combo) Date: Tue, 20 Nov 2012 05:56:27 -0800 (PST) Subject: Problem with list.remove() method Message-ID: Hi All, I'm relatively new to Python... but I have found something I cannot explain... and I'm sure you can help me. I have the following function that serves for removing the duplicates from a list... It's a simple and (almost) trivial task. I'm using WingIDE as editor/debugger and have Python 2.7.3. When running this I have an error when trying to remove cpy_lst[4]... and ONLY THAT!!! Even in interactive mode!!! Any suggestion is MOST welcome. Best Regards ACombo ...And the code: def remove_dup_5_10(): """ Remove the duplicates of a given list. The original list MUST be kept. """ # Set the original list lst = ['a', 1, 10.0, 2, 'd', 'b', 'b', 'b', 1, 2, 'b' ] # NEED to create a copy... See dicussion on Problem 5.6 and issue #2 cpy_lst = list(lst) # Perform an infinite loop... explained later i=0 # initialize the index while i != len(cpy_lst): if cpy_lst.count(cpy_lst[i]) != 1: cpy_lst.remove(i) else: i += 1 print "The original List: ", lst print "List with NO duplicates: ", cpy_lst return True From insideshoes at gmail.com Tue Nov 20 08:57:29 2012 From: insideshoes at gmail.com (inshu chauhan) Date: Tue, 20 Nov 2012 14:57:29 +0100 Subject: Index Error In-Reply-To: References: Message-ID: I am using python 2.7.3 , so can it be done in that ? On Tue, Nov 20, 2012 at 2:48 PM, Chris Angelico wrote: > On Wed, Nov 21, 2012 at 12:43 AM, inshu chauhan > wrote: > > I need help in this part as I am unable to device a method in which if > the > > points are out of index,it should stop..... > > > > Traceback (most recent call last): > > File "Z:/modules/Classify.py", line 73, in > > ComputeClasses(data) > > File "Z:/modules/Classify.py", line 49, in ComputeClasses > > point = data[idx[0], idx[1]] > > error: index is out of range > > > > When Python throws an exception, you can catch it and handle it as you > please. In this instance, it seems to me you want to break out of one > loop (if I've read your comments correctly), so just bracket that loop > with try... except. Start here: > > http://docs.python.org/3.3/tutorial/errors.html > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From nicolettivma at gmail.com Tue Nov 20 08:58:16 2012 From: nicolettivma at gmail.com (EDI Support) Date: Tue, 20 Nov 2012 05:58:16 -0800 (PST) Subject: Linux compatibility In-Reply-To: <5ab8efb4-d66b-42f7-9301-7048bf38fcef@googlegroups.com> References: <5ab8efb4-d66b-42f7-9301-7048bf38fcef@googlegroups.com> Message-ID: <3abfd778-ed75-4017-acc7-2ea0addff984@googlegroups.com> On Monday, November 19, 2012 11:44:37 AM UTC-5, EDI Support wrote: > Hi All, I would like know if Python 2.4.3 will be compatible with Linux RHEL 5.5 or 6.1? Thanks Tony Thanks everyone for your replies. To clarify, We would like to run a Proof of concept - that our current version 2.4.3 will run without any application or OS issues on RHEL 6.1. From rosuav at gmail.com Tue Nov 20 09:00:01 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 21 Nov 2012 01:00:01 +1100 Subject: Index Error In-Reply-To: References: Message-ID: On Wed, Nov 21, 2012 at 12:57 AM, inshu chauhan wrote: > I am using python 2.7.3 , so can it be done in that ? (Please don't top-post; just delete the couple of blank lines that gmail oh so kindly provides, and type your response at the bottom. You may also want to consider trimming the quoted text to just what you actually need.) Yes, it certainly can. Go to the docs page I linked to, then up the top left you'll see this: Python ? 3.3.0 Documentation with the "3.3.0" part a drop-down list. Drop it down and select 2.7, and you'll see the equivalent page for version 2.7.3. Have fun! ChrisA From insideshoes at gmail.com Tue Nov 20 09:11:08 2012 From: insideshoes at gmail.com (inshu chauhan) Date: Tue, 20 Nov 2012 15:11:08 +0100 Subject: Index Error In-Reply-To: References: Message-ID: On Tue, Nov 20, 2012 at 3:00 PM, Chris Angelico wrote: > On Wed, Nov 21, 2012 at 12:57 AM, inshu chauhan > wrote: > > I am using python 2.7.3 , so can it be done in that ? > > (Please don't top-post; just delete the couple of blank lines that > gmail oh so kindly provides, and type your response at the bottom. You > may also want to consider trimming the quoted text to just what you > actually need.) > > Yes, it certainly can. Go to the docs page I linked to, then up the > top left you'll see this: > > Python ? 3.3.0 Documentation > > with the "3.3.0" part a drop-down list. Drop it down and select 2.7, > and you'll see the equivalent page for version 2.7.3. > > Have fun! > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list > Ok.. Thanks a ton ... -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Tue Nov 20 09:12:01 2012 From: d at davea.name (Dave Angel) Date: Tue, 20 Nov 2012 09:12:01 -0500 Subject: Index Error In-Reply-To: References: Message-ID: <50AB8FB1.2030208@davea.name> On 11/20/2012 08:43 AM, inshu chauhan wrote: > def distance(c, p): > dist = sqrt( > ((c[0]-p[0])**2) + > ((c[1]-p[1])**2) + > ((c[2]-p[2])**2) > ) > return dist > > > def GenerateRing(x,y, N): Generates square rings around a point in data > which has 300 columns(x) and 3000 rows(y) > indices = [] > for i in xrange(-N, N): > indices.append((x+i, y-N)) > indices.append((x+N, y+i)) > indices.append((x-i, y+N)) > indices.append((x-N, y-i)) > return indices > Is it possible that N is greater than either x or y ? Are negative subscripts permissible? You should consider doing the clipping logic in this function, perhaps by passing xlimit and ylimit in as arguments. > > def ComputeClasses(data): > radius = .5 > points = [] > for cy in xrange(0, data.height): > for cx in xrange(0, data.width): > > if data[cy,cx] == (0.0,0.0,0.0): > continue > else : > centre = data[cy, cx] > points.append(centre) > > > change = True > > while change: > > for ring_number in xrange(1, 100): > change = False > new_indices = GenerateRing(cx, cy, ring_number) > print new_indices > for idx in new_indices: > I need help in this part as I am unable to device a method in > which if the > points are out of index,it should stop and > if idx[0] >= 300 and idx[1] >= 3000: go > to next centre and start generating rings from there.. and again if the > index is out of range .. this should repeat This is where you're trying to clip the values that may be outside of the total matrix. You do not want "and" in that expression. The way you've coded it, it'll only skip items in which both indices are out of range. Change it to if idx[0] >= data.width or idx[1] >= data.height: and depending on your answer to my earlier query, you may want to also check if either subscript is negative. > continue > else : > point = data[idx[0], idx[1]] > if point == (0.0, 0.0, 0.0 ): > print point > continue > else: > dist = distance(centre, point) > print dist > if dist < radius : and rings > should be added only when this condition is satisfied > print point > points.append(point) > change = True > print change > > > break Why do you want to terminate the loop after only iteration? > > > print points > > > ERROR now : > > data loaded > [(296, 403), (298, 403), (298, 405), (296, 405), (297, 403), (298, 404), > (297, 405), (296, 404)] ... I am printing Indices to know what index it > dies out.. > > Traceback (most recent call last): > File "Z:/modules/Classify.py", line 73, in > ComputeClasses(data) > File "Z:/modules/Classify.py", line 49, in ComputeClasses > point = data[idx[0], idx[1]] > error: index is out of range > -- DaveA From rosuav at gmail.com Tue Nov 20 09:14:38 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 21 Nov 2012 01:14:38 +1100 Subject: Problem with list.remove() method In-Reply-To: References: Message-ID: On Wed, Nov 21, 2012 at 12:56 AM, Alvaro Combo wrote: > Hi All, > > I'm relatively new to Python... but I have found something I cannot explain... and I'm sure you can help me. > > I have the following function that serves for removing the duplicates from a list... It's a simple and (almost) trivial task. > > I'm using WingIDE as editor/debugger and have Python 2.7.3. > > When running this I have an error when trying to remove cpy_lst[4]... and ONLY THAT!!! Even in interactive mode!!! Several points here. You've written a beautiful O(N^2) duplicates remover... Python has a really fast way of doing it, if you don't mind losing order: cpy_lst = list(set(lst)) But let's assume you're doing this for the exercise. Your technique is fine, if inefficient on large lists, but the remove() method looks for the first occurrence of an element by its value - what you want is: del cpy_lst[i] which will remove one element by index. With that change, you'll have a slightly odd duplicate remover that keeps the *last* of any given element. That's rather unusual. Instead, you may want to consider maintaining a set of "items I've already seen", and keeping all elements that aren't in that set. I won't give you all the code, but here's the basic set operations: sighted = set() sighted.add(some_element) if some_element in sighted: # condition is True if you've already seen this element Hope that helps! ChrisA From insideshoes at gmail.com Tue Nov 20 09:26:12 2012 From: insideshoes at gmail.com (inshu chauhan) Date: Tue, 20 Nov 2012 15:26:12 +0100 Subject: Index Error In-Reply-To: <50AB8FB1.2030208@davea.name> References: <50AB8FB1.2030208@davea.name> Message-ID: > def GenerateRing(x,y, N): Generates square rings around a point in data > > which has 300 columns(x) and 3000 rows(y) > > indices = [] > > for i in xrange(-N, N): > > indices.append((x+i, y-N)) > > indices.append((x+N, y+i)) > > indices.append((x-i, y+N)) > > indices.append((x-N, y-i)) > > return indices > > > Is it possible that N is greater than either x or y ? Are negative > subscripts permissible? > > You should consider doing the clipping logic in this function, perhaps > by passing xlimit and ylimit in as arguments.> > Yes N cannot be greater than x and y and it cant be negative too... > I need help in this part as I am unable to device a method in > which if the > points are out of index,it should stop and > if idx[0] >= 300 and idx[1] >= 3000: go > to next centre and start generating rings from there.. and again if the > index is out of range .. this should repeat This is where you're trying to clip the values that may be outside of > the total matrix. > Yes I am trying to clip the values here but its not working actually , I mean not at all working > > You do not want "and" in that expression. The way you've coded it, > it'll only skip items in which both indices are out of range. Change it to > if idx[0] >= data.width or idx[1] >= data.height: > > and depending on your answer to my earlier query, you may want to also > check if either subscript is negative. > > > continue > > else : > > point = data[idx[0], idx[1]] > > if point == (0.0, 0.0, 0.0 ): > > print point > > continue > > else: > > dist = distance(centre, point) > > print dist > > if dist < radius : and > rings > > should be added only when this condition is satisfied > > print point > > points.append(point) > > change = True > > print change > > > > > > break > > Why do you want to terminate the loop after only iteration? > I dint get your question ?? -------------- next part -------------- An HTML attachment was scrubbed... URL: From alvaro.combo at gmail.com Tue Nov 20 09:37:20 2012 From: alvaro.combo at gmail.com (Alvaro Combo) Date: Tue, 20 Nov 2012 06:37:20 -0800 (PST) Subject: Problem with list.remove() method In-Reply-To: References: Message-ID: <9caaea9c-8b7c-4ed5-bc3b-2519a58ceb26@googlegroups.com> Dear Chris, Thank you very much for you reply... For a newcomer to Python the Hell is in the details... :-). You are absolutely right... and I just used the index instead of the value. Regarding you other (most relevant) comments, I absolutely agree... BUT in those cases... I was aware :-). But since it is in fact an exercise... and having N^2 operations... is not important. Still your comments are quite pertinent. Once again, Thank you and best regards ACombo From alvaro.combo at gmail.com Tue Nov 20 09:37:20 2012 From: alvaro.combo at gmail.com (Alvaro Combo) Date: Tue, 20 Nov 2012 06:37:20 -0800 (PST) Subject: Problem with list.remove() method In-Reply-To: References: Message-ID: <9caaea9c-8b7c-4ed5-bc3b-2519a58ceb26@googlegroups.com> Dear Chris, Thank you very much for you reply... For a newcomer to Python the Hell is in the details... :-). You are absolutely right... and I just used the index instead of the value. Regarding you other (most relevant) comments, I absolutely agree... BUT in those cases... I was aware :-). But since it is in fact an exercise... and having N^2 operations... is not important. Still your comments are quite pertinent. Once again, Thank you and best regards ACombo From neilc at norwich.edu Tue Nov 20 09:39:17 2012 From: neilc at norwich.edu (Neil Cerutti) Date: 20 Nov 2012 14:39:17 GMT Subject: 10 sec poll - please reply! References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> Message-ID: On 2012-11-20, Dave Angel wrote: > I also vote for send_keys(), even before I saw Chris' reply. > > 'type' is too overloaded a word. For example, in Windows, it's > the command to display the contents of a file - it types it to > the screen. type is a nice verb, but since it's also a well-used noun it's perhaps not quite as good as send. -- Neil Cerutti From d at davea.name Tue Nov 20 09:39:31 2012 From: d at davea.name (Dave Angel) Date: Tue, 20 Nov 2012 09:39:31 -0500 Subject: Index Error In-Reply-To: References: Message-ID: <50AB9623.9030902@davea.name> On 11/20/2012 09:11 AM, inshu chauhan wrote: > On Tue, Nov 20, 2012 at 3:00 PM, Chris Angelico wrote: > >> On Wed, Nov 21, 2012 at 12:57 AM, inshu chauhan >> wrote: >>> I am using python 2.7.3 , so can it be done in that ? >> (Please don't top-post; just delete the couple of blank lines that >> gmail oh so kindly provides, and type your response at the bottom. You >> may also want to consider trimming the quoted text to just what you >> actually need.) >> >> Yes, it certainly can. Go to the docs page I linked to, then up the >> top left you'll see this: >> >> Python ? 3.3.0 Documentation >> >> with the "3.3.0" part a drop-down list. Drop it down and select 2.7, >> and you'll see the equivalent page for version 2.7.3. >> >> Have fun! >> >> ChrisA >> -- >> http://mail.python.org/mailman/listinfo/python-list >> > > Ok.. Thanks a ton ... > > > The catch to the approach suggested by Chris is that you DON'T want to break out of the whole loop when one value is out of range. You only want to skip that one point. -- DaveA From d at davea.name Tue Nov 20 09:46:28 2012 From: d at davea.name (Dave Angel) Date: Tue, 20 Nov 2012 09:46:28 -0500 Subject: Index Error In-Reply-To: References: <50AB8FB1.2030208@davea.name> Message-ID: <50AB97C4.7050505@davea.name> On 11/20/2012 09:26 AM, inshu chauhan wrote: >> def GenerateRing(x,y, N): Generates square rings around a point in data >>> which has 300 columns(x) and 3000 rows(y) >>> indices = [] >>> for i in xrange(-N, N): >>> indices.append((x+i, y-N)) >>> indices.append((x+N, y+i)) >>> indices.append((x-i, y+N)) >>> indices.append((x-N, y-i)) >>> return indices >>> >> Is it possible that N is greater than either x or y ? Are negative >> subscripts permissible? >> >> You should consider doing the clipping logic in this function, perhaps >> by passing xlimit and ylimit in as arguments.> >> > > > Yes N cannot be greater than x and y and it cant be negative too... > >> I need help in this part as I am unable to device a method in >> which if the >> points are out of index,it should stop and >> if idx[0] >= 300 and idx[1] >= 3000: go >> to next centre and start generating rings from there.. and again if the >> index is out of range .. this should repeat > > This is where you're trying to clip the values that may be outside of >> the total matrix. >> > > Yes I am trying to clip the values here but its not working actually , I > mean not at all working > So did you read the following paragraphs? You should not be using "and" in that expression. >> >> You do not want "and" in that expression. The way you've coded it, >> it'll only skip items in which both indices are out of range. Change it to >> if idx[0] >= data.width or idx[1] >= data.height: >> >> and depending on your answer to my earlier query, you may want to also >> check if either subscript is negative. >> >>> continue >>> else : >>> point = data[idx[0], idx[1]] >>> if point == (0.0, 0.0, 0.0 ): >>> print point >>> continue >>> else: >>> dist = distance(centre, point) >>> print dist >>> if dist < radius : and >> rings >>> should be added only when this condition is satisfied >>> print point >>> points.append(point) >>> change = True >>> print change >>> >>> >>> break >> >> Why do you want to terminate the loop after only iteration? >> > > I dint get your question ?? > You have a break there. What's it for? It'll make sure you only process one of the idx values from new_indices. i doubt that's what you intended. -- DaveA From jaorozco at estudiantes.uci.cu Tue Nov 20 09:48:19 2012 From: jaorozco at estudiantes.uci.cu (Jorge Alberto Diaz Orozco) Date: Tue, 20 Nov 2012 09:48:19 -0500 Subject: proxy?? Message-ID: <50AB9833.6020409@estudiantes.uci.cu> Hi there. Does anyone knows how to manage headers using a simple proxy??? I'm doing this but It gives me problems In some pages. import SocketServer import SimpleHTTPServer import urllib2 PORT = 8888 class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler): def do_GET(self): try: print self.path self.copyfile(urllib2.urlopen(self.path), self.wfile) except: print 'error',self.path httpd = SocketServer.ForkingTCPServer(('127.0.0.1', PORT), Proxy) print "serving at port", PORT httpd.serve_forever() 10mo. ANIVERSARIO DE LA CREACION DE LA UNIVERSIDAD DE LAS CIENCIAS INFORMATICAS... CONECTADOS AL FUTURO, CONECTADOS A LA REVOLUCION http://www.uci.cu http://www.facebook.com/universidad.uci http://www.flickr.com/photos/universidad_uci From wxjmfauth at gmail.com Tue Nov 20 09:57:06 2012 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Tue, 20 Nov 2012 06:57:06 -0800 (PST) Subject: Yet another Python textbook In-Reply-To: References: Message-ID: <31a82817-8c9b-4dd2-a468-89d8d081fd1b@googlegroups.com> Le mardi 20 novembre 2012 09:09:50 UTC+1, Chris Angelico a ?crit?: > On Tue, Nov 20, 2012 at 7:02 PM, Pavel Solin wrote: > > > Perhaps you are right. Is there any statistics of how many Python > > > programmers are using 2.7 vs. 3? Most of people I know use 2.7. > > > > If you're teaching Python, the stats are probably about zero for zero. > > Start them off on Py3 and help move the world forward. > > > > ChrisA -------- Do not count with me. The absurd flexible string representation has practically borrowed the idea to propose once Python has a teaching tool. jmf From dan at klein.ws Tue Nov 20 09:58:35 2012 From: dan at klein.ws (Daniel Klein) Date: Tue, 20 Nov 2012 09:58:35 -0500 Subject: [Python 3.3/Windows] Path Browser seems to be broken Message-ID: <18F56CB3AB1841D4BF7D12D334EA8751@AKEPA> If you try to expand any of the paths in the Path Browser (by clicking the + sign) then it not only closes the Path Browser but it also closes all other windows that were opened in IDLE, including the IDLE interpreter itself. I did a Google search and it doesn't look like this been reported. If this is truly a bug then can you point me in the right direction for issuing a bug report? I'm running... Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:57:17) [MSC v.1600 64 bit (AMD64)] on win32 Dan -------------- next part -------------- An HTML attachment was scrubbed... URL: From insideshoes at gmail.com Tue Nov 20 10:01:44 2012 From: insideshoes at gmail.com (inshu chauhan) Date: Tue, 20 Nov 2012 16:01:44 +0100 Subject: Index Error In-Reply-To: <50AB97C4.7050505@davea.name> References: <50AB8FB1.2030208@davea.name> <50AB97C4.7050505@davea.name> Message-ID: > > > So did you read the following paragraphs? You should not be using "and" > in that expression. > Yes i tried "or" also but no use ..... > > >> > >> You do not want "and" in that expression. The way you've coded it, > >> it'll only skip items in which both indices are out of range. Change > it to > >> if idx[0] >= data.width or idx[1] >= data.height: > >> > >> and depending on your answer to my earlier query, you may want to also > >> check if either subscript is negative. > >> > >>> continue > >>> else : > >>> point = data[idx[0], idx[1]] > >>> if point == (0.0, 0.0, 0.0 ): > >>> print point > >>> continue > >>> else: > >>> dist = distance(centre, point) > >>> print dist > >>> if dist < radius : and > >> rings > >>> should be added only when this condition is satisfied > >>> print point > >>> points.append(point) > >>> change = True > >>> print change > >>> > >>> > >>> break > >> > >> Why do you want to terminate the loop after only iteration? > >> > > > > I dint get your question ?? > > > > You have a break there. What's it for? It'll make sure you only > process one of the idx values from new_indices. i doubt that's what you > intended. > The idea here is if no more point is added to the list of points i.e. all points surrounding the centre is zero.. I want to break the loop and go to the next point.. -------------- next part -------------- An HTML attachment was scrubbed... URL: From neilc at norwich.edu Tue Nov 20 10:02:08 2012 From: neilc at norwich.edu (Neil Cerutti) Date: 20 Nov 2012 15:02:08 GMT Subject: Problems on these two questions References: <400c615f-4d93-4b8b-85ec-1bb4c47e30fe@googlegroups.com> Message-ID: On 2012-11-19, Dennis Lee Bieber wrote: > On Sun, 18 Nov 2012 17:52:35 -0800 (PST), su29090 > <129km09 at gmail.com> declaimed the following in > gmane.comp.python.general: > >> >> I all of the other problems but I have issues with these: >> >> 1.Given a positive integer n , assign True to is_prime if n >> has no factors other than 1 and itself. (Remember, m is a >> factor of n if m divides n evenly.) >> > Google: Sieve of Eratosthenes (might be mis-spelled) The sieve is a nice simple and fast algorithm, provided there's a bound on the highest n you need to check. It's much less simple and less fast if n is unbounded or the bound is unknown. Python's standard library isn't equipped with the an obvious collection to use to implement it either. >> 2.An arithmetic progression is a sequence of numbers in which >> the distance (or difference) between any two successive >> numbers if the same. This in the sequence 1, 3, 5, 7, ... , >> the distance is 2 while in the sequence 6, 12, 18, 24, ... , >> the distance is 6. >> >> Given the positive integer distance and the positive integer >> n , associate the variable sum with the sum of the elements >> of the arithmetic progression from 1 to n with distance >> distance . For example, if distance is 2 and n is 10 , >> then sum would be associated with 26 because 1+3+5+7+9 = >> 25 . > > So, what have you tried? > > Consider: you have a "sum", you have a sequence of "elements" > (based upon a spacing "distance"), and you have an upper bound > "n" > > You need to generate a sequence of "elements" starting at "1", > using "distance" as the spacing, until you exceed "n", and you > want to produce a "sum" of all those elements... This one's sort of a trick question, depending on your definition of "trick". The most obvious implementation is pretty good. In both cases a web search and a little high-density reading provides insights and examples for the OP. -- Neil Cerutti From insideshoes at gmail.com Tue Nov 20 10:07:15 2012 From: insideshoes at gmail.com (inshu chauhan) Date: Tue, 20 Nov 2012 16:07:15 +0100 Subject: Index Error In-Reply-To: <50AB9623.9030902@davea.name> References: <50AB9623.9030902@davea.name> Message-ID: The catch to the approach suggested by Chris is that you DON'T want to > break out of the whole loop when one value is out of range. You only > want to skip that one point. > > > Yes I want to skip only that one point (centre) but try and expect seems to be the last option.. I would like to try other ways first.. -------------- next part -------------- An HTML attachment was scrubbed... URL: From michael.herrmann at getautoma.com Tue Nov 20 10:18:42 2012 From: michael.herrmann at getautoma.com (Michael Herrmann) Date: Tue, 20 Nov 2012 07:18:42 -0800 (PST) Subject: 10 sec poll - please reply! In-Reply-To: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> Message-ID: <7290a127-97e1-4032-98ae-6e602b87b74b@googlegroups.com> Thanks again for your further replies. So far, it's 4 votes for 'send_keys' and 1 vote for 'type'. Regarding 'send_keys': To me personally it makes sense to send keys _to_ something. However, in our API, send_keys would not be called on an object or with a parameter indicating the target. It would just be send_keys(ENTER) send_keys("Hello World!") send_keys(CTRL + 'a') Does that change your preference for 'send_keys'? Thanks a lot!!! On Tuesday, November 20, 2012 1:18:38 PM UTC+1, Michael Herrmann wrote: > Hi, > > > > I'm developing a GUI Automation library (http://www.getautoma.com) and am having difficulty picking a name for the function that simulates key strokes. I currently have it as 'type' but that clashes with the built-in function. Example uses of 'type': > > > > type(ENTER) > > > > type("Hello World!") > > > > type(CTRL + 'a') > > > > What, in your view, would be the most intuitive alternative name? > > > > Here are my thoughts so far: I could call it 'press' but then our GUI automation tool also allows you to click things and then "press" might be mistaken for "pressing a button". A less ambiguous alternative is "type_keys" but that is rather long and doesn't read as well, for instance in type_keys(ENTER). > > > > Thank you very much! From insideshoes at gmail.com Tue Nov 20 10:20:15 2012 From: insideshoes at gmail.com (inshu chauhan) Date: Tue, 20 Nov 2012 16:20:15 +0100 Subject: Index Error In-Reply-To: References: <50AB9623.9030902@davea.name> Message-ID: I dont want error actually to come. With try nd except , the error will come but without affecting my prog... -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at mrabarnett.plus.com Tue Nov 20 10:59:44 2012 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 20 Nov 2012 15:59:44 +0000 Subject: 10 sec poll - please reply! In-Reply-To: <7290a127-97e1-4032-98ae-6e602b87b74b@googlegroups.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <7290a127-97e1-4032-98ae-6e602b87b74b@googlegroups.com> Message-ID: <50ABA8F0.5030905@mrabarnett.plus.com> On 2012-11-20 15:18, Michael Herrmann wrote: > Thanks again for your further replies. So far, it's 4 votes for 'send_keys' and 1 vote for 'type'. > > Regarding 'send_keys': To me personally it makes sense to send keys _to_ something. However, in our API, send_keys would not be called on an object or with a parameter indicating the target. It would just be > > send_keys(ENTER) > send_keys("Hello World!") > send_keys(CTRL + 'a') > > Does that change your preference for 'send_keys'? > Calling it "send_keys" does have precedent. There's a module called SendKeys which uses the SendKeys system call in Windows. Unfortunately, the last version was for Python 2.6. When I wanted the functionality for Python 3.3 I had to write my own. From danielkleinad at gmail.com Tue Nov 20 11:00:34 2012 From: danielkleinad at gmail.com (Daniel Klein) Date: Tue, 20 Nov 2012 11:00:34 -0500 Subject: Path Browser seems to be broken Message-ID: If you try to expand any of the paths in the Path Browser (by clicking the + sign) then it not only closes the Path Browser but it also closes all other windows that were opened in IDLE, including the IDLE interpreter itself. A Google search doesn't look like this been reported. If this is truly a bug then can you point me in the right direction for issuing a bug report? I'm running... Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:57:17) [MSC v.1600 64 bit (AMD64)] on win32 Dan -------------- next part -------------- An HTML attachment was scrubbed... URL: From gordon at panix.com Tue Nov 20 11:09:18 2012 From: gordon at panix.com (John Gordon) Date: Tue, 20 Nov 2012 16:09:18 +0000 (UTC) Subject: 10 sec poll - please reply! References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> Message-ID: In <3d71f175-164e-494c-a521-2eaa5679b524 at googlegroups.com> Michael Herrmann writes: > What, in your view, would be the most intuitive alternative name? keyboard_input(). -- John Gordon A is for Amy, who fell down the stairs gordon at panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" From d at davea.name Tue Nov 20 11:17:47 2012 From: d at davea.name (Dave Angel) Date: Tue, 20 Nov 2012 11:17:47 -0500 Subject: Index Error In-Reply-To: References: <50AB8FB1.2030208@davea.name> <50AB97C4.7050505@davea.name> Message-ID: <50ABAD2B.4060000@davea.name> On 11/20/2012 10:01 AM, inshu chauhan wrote: >> >> >> So did you read the following paragraphs? You should not be using "and" >> in that expression. > > > >> Yes i tried "or" also but no use ..... Explain "no use". If you mean you still fail, then what else did you try? For example, did you try interchanging the two subscripts? I've suspected all along that the meanings of row and column, x and y, [0] and [1], height and width are possibly confused. Perhaps it'd be better if you used a similar terminology everywhere. Or perhaps you should simply make a class called Point, with attributes x and y. And use instances of that class rather than tuples. And start the function by defining xmax and ymax, from data.height and data.width (in whatever order matches the docs of that library you're using) >> >>>> >>>> You do not want "and" in that expression. The way you've coded it, >>>> it'll only skip items in which both indices are out of range. Change >> it to >>>> if idx[0] >= data.width or idx[1] >= data.height: >>>> >>>> and depending on your answer to my earlier query, you may want to also >>>> check if either subscript is negative. >>>> >>>>> continue >>>>> else : >>>>> point = data[idx[0], idx[1]] >>>>> if point == (0.0, 0.0, 0.0 ): >>>>> print point >>>>> continue >>>>> else: >>>>> dist = distance(centre, point) >>>>> print dist >>>>> if dist < radius : and >>>> rings >>>>> should be added only when this condition is satisfied >>>>> print point >>>>> points.append(point) >>>>> change = True >>>>> print change >>>>> >>>>> >>>>> break >>>> >>>> Why do you want to terminate the loop after only iteration? >>>> >>> > The idea here is if no more point is added to the list of points i.e. all > points surrounding the centre is zero.. I want to break the loop and go to > the next point.. > That's not at all what the break does. But it's probably not what you meant to say anyway. > I think what you're saying is that you want to append at most one of the points from the ring. In that case, the break is appropriate, but it'd be much clearer if it were inside the clause that triggers it, the place where you say points.append(point). (naturally, it'd be at the end of that clause, following print change.) In other words indent it to line up with print change. Back to an earlier comment. I asked if N was ever bigger than x or bigger than y, and you said never. But your ComputeClasses will have such a case the very first time around, when cx==0, cy==0, and ring_number == 1. Have you actually tested a trivial nested loop: for cy in xrange(0, data.height): for cx in xrange(0, data.width): point = data[cy, cx] to see whether it blows up. And if it does, whether reversing cy and cx will change it? Your comment in the line: if dist < radius : and rings should be added only when this condition is satisfied is confusing to me. How can you make the call to GenerateRing() after this test, when this test is measuring something about one of the values returned by GenerateRing ? I must confess I have no idea what data represents. When you're doing rings, you use deltas on the cx and cy values. But when you're computing radius, you use the 3d coordinates returned by data[cx, cy]. So is data some kind of transformation, like a projection from a 3d object into a plane ? -- DaveA From roy at panix.com Tue Nov 20 11:18:33 2012 From: roy at panix.com (Roy Smith) Date: 20 Nov 2012 11:18:33 -0500 Subject: Printing time in "at" format? Message-ID: I need to talk to an API which requires (for reasons totally beyond my comprehension), time specified in "at" format, i.e. the format accepted by the unix "at" command. This allows you to do such bizarre things as use "teatime" to indicate 4:00 PM. The best I can find for a specification is http://tinyurl.com/d5vddoa. Before I dive into this too far, has anybody already written some code which formats times like that? From d at davea.name Tue Nov 20 11:19:30 2012 From: d at davea.name (Dave Angel) Date: Tue, 20 Nov 2012 11:19:30 -0500 Subject: 10 sec poll - please reply! In-Reply-To: References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> Message-ID: <50ABAD92.7070503@davea.name> On 11/20/2012 11:09 AM, John Gordon wrote: > In <3d71f175-164e-494c-a521-2eaa5679b524 at googlegroups.com> Michael Herrmann writes: > >> What, in your view, would be the most intuitive alternative name? > keyboard_input(). > Well, since Python already has input() and raw_input(), it would then be clear that keyboard_input() would take some kind of data from the keyboard, not send it. -- DaveA From steve+comp.lang.python at pearwood.info Tue Nov 20 11:21:07 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 20 Nov 2012 16:21:07 GMT Subject: 10 sec poll - please reply! References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <7290a127-97e1-4032-98ae-6e602b87b74b@googlegroups.com> Message-ID: <50abadf3$0$29987$c3e8da3$5496439d@news.astraweb.com> On Tue, 20 Nov 2012 07:18:42 -0800, Michael Herrmann wrote: > Thanks again for your further replies. So far, it's 4 votes for > 'send_keys' and 1 vote for 'type'. > > Regarding 'send_keys': To me personally it makes sense to send keys _to_ > something. However, in our API, send_keys would not be called on an > object or with a parameter indicating the target. It would just be > > send_keys(ENTER) > send_keys("Hello World!") > send_keys(CTRL + 'a') "send_keys" is wrong, because you aren't sending keys. You're sending strings, except you aren't actually sending strings either, because "send" does not make sense without a target. You're automating the typing of strings, including control characters. I believe that your initial instinct for the name of this function was correct. It automates typing, so you should call it "type" or (for those paranoid about shadowing the built-in, "type_str". -- Steven From emile at fenx.com Tue Nov 20 11:21:20 2012 From: emile at fenx.com (emile) Date: Tue, 20 Nov 2012 08:21:20 -0800 Subject: 10 sec poll - please reply! In-Reply-To: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> Message-ID: On 11/20/2012 04:18 AM, Michael Herrmann wrote: > Hi, > > I'm developing a GUI Automation library (http://www.getautoma.com) and am having difficulty picking a name for the function that simulates key strokes. I currently have it as 'type' but that clashes with the built-in function. Example uses of 'type': > > type(ENTER) > > type("Hello World!") > > type(CTRL + 'a') > > What, in your view, would be the most intuitive alternative name? > > Here are my thoughts so far: I could call it 'press' I have several tools that distinguish between press and release for this. You may want to consider having both. Emile From mherrmann.at at gmail.com Tue Nov 20 11:29:57 2012 From: mherrmann.at at gmail.com (mherrmann.at at gmail.com) Date: Tue, 20 Nov 2012 08:29:57 -0800 (PST) Subject: 10 sec poll - please reply! In-Reply-To: References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> Message-ID: <528c4303-4067-404c-a831-5d362b4eadf6@googlegroups.com> That's a very good suggestion Emile!! So I might eventually need both 'press' and 'release' (or press_key/release_key). Thanks for this! To everyone else who has been so kind to reply thus far: What do you think of generate_keystrokes? It's a bit long but describes exactly what the function would be doing. All of you are a great help and I really appreciate it. Thank you! Michael On Tuesday, 20 November 2012 17:21:38 UTC+1, emile wrote: > On 11/20/2012 04:18 AM, Michael Herrmann wrote: > > > Hi, > > > > > > I'm developing a GUI Automation library (http://www.getautoma.com) and am having difficulty picking a name for the function that simulates key strokes. I currently have it as 'type' but that clashes with the built-in function. Example uses of 'type': > > > > > > type(ENTER) > > > > > > type("Hello World!") > > > > > > type(CTRL + 'a') > > > > > > What, in your view, would be the most intuitive alternative name? > > > > > > Here are my thoughts so far: I could call it 'press' > > > > I have several tools that distinguish between press and release for > > this. You may want to consider having both. > > > > Emile From mherrmann.at at gmail.com Tue Nov 20 11:29:57 2012 From: mherrmann.at at gmail.com (mherrmann.at at gmail.com) Date: Tue, 20 Nov 2012 08:29:57 -0800 (PST) Subject: 10 sec poll - please reply! In-Reply-To: References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> Message-ID: <528c4303-4067-404c-a831-5d362b4eadf6@googlegroups.com> That's a very good suggestion Emile!! So I might eventually need both 'press' and 'release' (or press_key/release_key). Thanks for this! To everyone else who has been so kind to reply thus far: What do you think of generate_keystrokes? It's a bit long but describes exactly what the function would be doing. All of you are a great help and I really appreciate it. Thank you! Michael On Tuesday, 20 November 2012 17:21:38 UTC+1, emile wrote: > On 11/20/2012 04:18 AM, Michael Herrmann wrote: > > > Hi, > > > > > > I'm developing a GUI Automation library (http://www.getautoma.com) and am having difficulty picking a name for the function that simulates key strokes. I currently have it as 'type' but that clashes with the built-in function. Example uses of 'type': > > > > > > type(ENTER) > > > > > > type("Hello World!") > > > > > > type(CTRL + 'a') > > > > > > What, in your view, would be the most intuitive alternative name? > > > > > > Here are my thoughts so far: I could call it 'press' > > > > I have several tools that distinguish between press and release for > > this. You may want to consider having both. > > > > Emile From breamoreboy at yahoo.co.uk Tue Nov 20 11:31:49 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 20 Nov 2012 16:31:49 +0000 Subject: 10 sec poll - please reply! In-Reply-To: <7290a127-97e1-4032-98ae-6e602b87b74b@googlegroups.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <7290a127-97e1-4032-98ae-6e602b87b74b@googlegroups.com> Message-ID: On 20/11/2012 15:18, Michael Herrmann wrote: > Thanks again for your further replies. So far, it's 4 votes for 'send_keys' and 1 vote for 'type'. > > Regarding 'send_keys': To me personally it makes sense to send keys _to_ something. However, in our API, send_keys would not be called on an object or with a parameter indicating the target. It would just be > > send_keys(ENTER) > send_keys("Hello World!") > send_keys(CTRL + 'a') In that case I'd just call it keys, that shouldn't cause too much confusion :) Though keys_pressed comes to mind as an alternative. > > Does that change your preference for 'send_keys'? > > Thanks a lot!!! > -- Cheers. Mark Lawrence. From ian.g.kelly at gmail.com Tue Nov 20 12:11:58 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 20 Nov 2012 10:11:58 -0700 Subject: Yet another Python textbook In-Reply-To: References: Message-ID: On Tue, Nov 20, 2012 at 1:02 AM, Pavel Solin wrote: > There is an ongoing discussion but we are not sure. > Are there any reasons except for the print () command > and division of integers? The big one is that Python 3 holds the future of Python development. There are no more feature releases planned for the 2.x series, only maintenance releases. Eventually, Python 2.7 is going to start looking pretty old. You might look through the whatsnew documents at: http://docs.python.org/3/whatsnew/index.html to get an idea of what features have been added in Python 3. Many of these have been backported to Python 2.6 or 2.7, possibly requiring a __future__ import, but most have not. One change that comes to my mind that might be of interest for NCLab is that in Python 3.3, the decimal module has been rewritten in C for a major speed-up. From name.surname at gmail.com Tue Nov 20 12:29:05 2012 From: name.surname at gmail.com (Marco) Date: Tue, 20 Nov 2012 18:29:05 +0100 Subject: The type.__call__() method manages the calls to __new__ and __init__? Message-ID: Looking at the documentation of Py3.3: http://docs.python.org/3.3/reference/datamodel.html#object.__new__ I saw the method `__new__()` is called automatically when I create an istance. After the call to __new__(), if it returns an instance `self` then `self.__init__()` is called. Because when I call an instance the __call__ method is called, and because the classes are instances of type, I thought when I call a Foo class this imply the call type.__call__(Foo), and so this one manages the Foo.__new__ and Foo.__init__ calls: >>> class Foo: ... def __new__(cls): ... print('Foo.__new__()') ... return super().__new__(cls) ... def __init__(self): ... print('Foo.__init__(self)') ... >>> f = type.__call__(Foo) Foo.__new__() Foo.__init__(self) Is that right? Thanks in advance -- Marco From yujian4newsgroup at gmail.com Tue Nov 20 12:40:58 2012 From: yujian4newsgroup at gmail.com (yujian4newsgroup at gmail.com) Date: Wed, 21 Nov 2012 01:40:58 +0800 Subject: How to use the python to do the unit test References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> , <528c4303-4067-404c-a831-5d362b4eadf6@googlegroups.com> Message-ID: <201211210140534223470@gmail.com> I write a mfc application, then I want to use the python to test this application. just as user click that button. Please tell me how to write the python application? From andriy.kornatskyy at live.com Tue Nov 20 12:41:42 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Tue, 20 Nov 2012 20:41:42 +0300 Subject: Web Frameworks Excessive Complexity Message-ID: Cyclomatic (or conditional) complexity is a metric used to indicate the complexity of a source code. Excessive complexity is something that is beyond recommended level of 10 (threshold that points to the fact the source code is too complex and refactoring is suggested). Here is a list of web frameworks examined: bottle, cherrypy, circuits, django, flask, pyramid, pysi, tornado, turbogears, web.py, web2py and wheezy.web. You can read more here: http://mindref.blogspot.com/2012/11/python-web-excessive-complexity.html Thanks. Comments or suggestions are welcome. Andriy Kornatskyy From emile at fenx.com Tue Nov 20 12:51:09 2012 From: emile at fenx.com (emile) Date: Tue, 20 Nov 2012 09:51:09 -0800 Subject: How to use the python to do the unit test In-Reply-To: <201211210140534223470@gmail.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> , <528c4303-4067-404c-a831-5d362b4eadf6@googlegroups.com> <201211210140534223470@gmail.com> Message-ID: On 11/20/2012 09:40 AM, yujian4newsgroup at gmail.com wrote: > I write a mfc application, then I want to use the python to test this application. just as user click that button. Please tell me how to write the python application? I currently use MacroScheduler (http://www.mjtnet.com/) then write macros from within python and run them using the commands module to test. Emile From python.list at tim.thechases.com Tue Nov 20 13:16:05 2012 From: python.list at tim.thechases.com (Tim Chase) Date: Tue, 20 Nov 2012 12:16:05 -0600 Subject: Printing time in "at" format? In-Reply-To: References: Message-ID: <50ABC8E5.9010401@tim.thechases.com> On 11/20/12 10:18, Roy Smith wrote: > I need to talk to an API which requires (for reasons totally beyond my > comprehension), time specified in "at" format, i.e. the format > accepted by the unix "at" command. This allows you to do such bizarre > things as use "teatime" to indicate 4:00 PM. The best I can find for > a specification is http://tinyurl.com/d5vddoa. > > Before I dive into this too far, has anybody already written some code > which formats times like that? Your statement can be ambiguously parsed as "I need to merely *generate* time specifications that 'at' can parse", or it can be parsed as "I need to generate *and consume* time specifications the same way as 'at' does" If it's the former, it's pretty easy--'at' accepts a wide variety of formats. If it's the latter, I've got a doc in /usr/share/doc/at/timespec on my Debian machine, which seems to be googleable, but here's one copy of it http://fts.ifac.cnr.it/cgi-bin/dwww/usr/share/doc/at/timespec I don't have a parser already, but that should get you a jump start on what (at least Debian's) 'at' parses, and might tie nicely with pyparsing. -tkc From __peter__ at web.de Tue Nov 20 13:23:57 2012 From: __peter__ at web.de (Peter Otten) Date: Tue, 20 Nov 2012 19:23:57 +0100 Subject: Path Browser seems to be broken References: Message-ID: Daniel Klein wrote: > If you try to expand any of the paths in the Path Browser (by clicking the > + sign) then it not only closes the Path Browser but it also closes all > other windows that were opened in IDLE, including the IDLE interpreter > itself. > > A Google search doesn't look like this been reported. If this is truly a > bug It is. > then can you point me in the right direction for issuing a bug report? is the place to go... > I'm running... > > Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:57:17) [MSC v.1600 64 > bit (AMD64)] on win32 but this bug has already been reported: From roy at panix.com Tue Nov 20 13:24:21 2012 From: roy at panix.com (Roy Smith) Date: Tue, 20 Nov 2012 13:24:21 -0500 Subject: Printing time in "at" format? In-Reply-To: <50ABC8E5.9010401@tim.thechases.com> References: <50ABC8E5.9010401@tim.thechases.com> Message-ID: <9E895B89-09EA-4513-A1C4-432CD40C0EC0@panix.com> On Nov 20, 2012, at 1:16 PM, Tim Chase wrote: > Your statement can be ambiguously parsed as "I need to merely > *generate* time specifications that 'at' can parse", or it can be > parsed as "I need to generate *and consume* time specifications the > same way as 'at' does" > > If it's the former, it's pretty easy--'at' accepts a wide variety of > formats. Fortunately, it's the former. Since posting my original query, I have discovered by experimentation that the API also accepts a string of digits as a unix timestamp, so this turns out to be trivial. --- Roy Smith roy at panix.com From dieter at handshake.de Tue Nov 20 13:40:09 2012 From: dieter at handshake.de (Dieter Maurer) Date: Tue, 20 Nov 2012 19:40:09 +0100 Subject: Stack_overflow error References: <1fd7672d-000b-4cbc-bd87-a9f37320c9e8@googlegroups.com> Message-ID: <87ip9086eu.fsf@handshake.de> Aung Thet Naing writes: > I'm having Stack_overflow exception in _ctypes_callproc (callproc.c). The error actually come from the: > > cleanup: > for (i = 0; i < argcount; ++i) > Py_XDECREF(args[i].keep); > > when args[i].keep->ob_refCnt == 1 Really a stack overflow or a general segmentation violation? Under *nix, both are not easy to distinguish -- but maybe, you are working with Windows? From kevinintx at gmail.com Tue Nov 20 14:09:12 2012 From: kevinintx at gmail.com (Kevin T) Date: Tue, 20 Nov 2012 11:09:12 -0800 (PST) Subject: re.search when used within an if/else fails In-Reply-To: <50aadcf0$0$29983$c3e8da3$5496439d@news.astraweb.com> References: <50aadbe6$0$29983$c3e8da3$5496439d@news.astraweb.com> <50aadcf0$0$29983$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Monday, November 19, 2012 7:29:20 PM UTC-6, Steven D'Aprano wrote: > On Tue, 20 Nov 2012 01:24:54 +0000, Steven D'Aprano wrote: > > > > - use "if something is None", not == None. > > > Steven i will not include line #'s in the future, point taken i will change ==/!= to is/is not as most people pointed out. there is no else because it doesn't work. i used eclipse in debug mode and a command line execution of the code, both behave the same way #if re.search( "rsrvd", sigName ) : #version a #if re.search( "rsrvd", sigName ) == None : #version b if re.search( "rsrvd", sigName ) is None : #version bb print sigName newVal = "%s%s" % ('1'*signal['bits'] , newVal ) #else: #version c if re.search( "rsrvd", sigName ) != None : #version d print sigName newVal = "%s%s" % ( '0'*signal['bits'],> newVal ) i can use either version a/b the else clause (version c) will not execute. fortunately, with version bb, the else clause will execute!! thanks for the input all.. kevin Now if i change From ian.g.kelly at gmail.com Tue Nov 20 14:25:08 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 20 Nov 2012 12:25:08 -0700 Subject: The type.__call__() method manages the calls to __new__ and __init__? In-Reply-To: References: Message-ID: On Tue, Nov 20, 2012 at 10:29 AM, Marco wrote: > Because when I call an instance the __call__ method is called, and because > the classes are instances of type, I thought when I call a Foo class this > imply the call type.__call__(Foo), and so this one manages the Foo.__new__ > and Foo.__init__ calls: Yes, that's right. Observe: >>> class MetaFoo(type): ... def __call__(cls): ... print("before") ... self = super().__call__() ... print("after", self) ... return self ... >>> class Foo(metaclass=MetaFoo): ... def __new__(cls): ... print("__new__") ... return super().__new__(cls) ... def __init__(self): ... print("__init__") ... >>> f = Foo() before __new__ __init__ after <__main__.Foo object at 0x00C55410> From robert.kern at gmail.com Tue Nov 20 14:32:31 2012 From: robert.kern at gmail.com (Robert Kern) Date: Tue, 20 Nov 2012 19:32:31 +0000 Subject: Web Frameworks Excessive Complexity In-Reply-To: References: Message-ID: On 20/11/2012 17:41, Andriy Kornatskyy wrote: > > Cyclomatic (or conditional) complexity is a metric used to indicate the complexity of a source code. Excessive complexity is something that is beyond recommended > level of 10 (threshold that points to the fact the source code is too > complex and refactoring is suggested). Here is a list of web frameworks examined: bottle, cherrypy, circuits, > django, flask, pyramid, pysi, tornado, turbogears, web.py, web2py and > wheezy.web. Cyclomatic complexity tells you nothing that counting lines of code doesn't already. http://www.scirp.org/Journal/PaperInformation.aspx?paperID=779 -- 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 ian.g.kelly at gmail.com Tue Nov 20 14:37:01 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 20 Nov 2012 12:37:01 -0700 Subject: re.search when used within an if/else fails In-Reply-To: References: <50aadbe6$0$29983$c3e8da3$5496439d@news.astraweb.com> <50aadcf0$0$29983$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, Nov 20, 2012 at 12:09 PM, Kevin T wrote: > #if re.search( "rsrvd", sigName ) : #version a > #if re.search( "rsrvd", sigName ) == None : #version b > if re.search( "rsrvd", sigName ) is None : #version bb > print sigName > newVal = "%s%s" % ('1'*signal['bits'] , newVal ) > #else: #version c > if re.search( "rsrvd", sigName ) != None : #version d > print sigName > newVal = "%s%s" % ( '0'*signal['bits'],> newVal ) > > i can use either version a/b the else clause (version c) will not execute. > fortunately, with version bb, the else clause will execute!! There must be some other difference in your testing. I don't have Python 2.4 available, but I tried your version a in both Python 2.3 and 2.5 using made-up values for sigName, and the else clause is executed in both. From ian.g.kelly at gmail.com Tue Nov 20 14:39:10 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 20 Nov 2012 12:39:10 -0700 Subject: re.search when used within an if/else fails In-Reply-To: References: <50aadbe6$0$29983$c3e8da3$5496439d@news.astraweb.com> <50aadcf0$0$29983$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, Nov 20, 2012 at 12:37 PM, Ian Kelly wrote: > On Tue, Nov 20, 2012 at 12:09 PM, Kevin T wrote: >> #if re.search( "rsrvd", sigName ) : #version a >> #if re.search( "rsrvd", sigName ) == None : #version b >> if re.search( "rsrvd", sigName ) is None : #version bb >> print sigName >> newVal = "%s%s" % ('1'*signal['bits'] , newVal ) >> #else: #version c >> if re.search( "rsrvd", sigName ) != None : #version d >> print sigName >> newVal = "%s%s" % ( '0'*signal['bits'],> newVal ) >> >> i can use either version a/b the else clause (version c) will not execute. >> fortunately, with version bb, the else clause will execute!! > > There must be some other difference in your testing. I don't have > Python 2.4 available, but I tried your version a in both Python 2.3 > and 2.5 using made-up values for sigName, and the else clause is > executed in both. It should be noted, however, that version a is the logical *inverse* of both b and bb. With version a, you're testing for the logical truth of the re.search result; it will be true if it is *not* None. With the other two you are testing that the result *is* None. From andriy.kornatskyy at live.com Tue Nov 20 14:46:27 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Tue, 20 Nov 2012 22:46:27 +0300 Subject: Web Frameworks Excessive Complexity In-Reply-To: References: , Message-ID: Robert, Thank you for the comment. I do not try relate CC with LOC. Instead pointing to excessive complexity, something that is beyond recommended threshold, a subject to refactoring in respective web frameworks. Those areas are likely to be potential source of bugs (e.g. due to low code coverage with unit tests) thus have certain degree of interest to both: end users and framework developers. Thanks. Andriy Kornatskyy ---------------------------------------- > To: python-list at python.org > From: robert.kern at gmail.com > Subject: Re: Web Frameworks Excessive Complexity > Date: Tue, 20 Nov 2012 19:32:31 +0000 > > On 20/11/2012 17:41, Andriy Kornatskyy wrote: > > > > Cyclomatic (or conditional) complexity is a metric used to indicate the complexity of a source code. Excessive complexity is something that is beyond recommended > > level of 10 (threshold that points to the fact the source code is too > > complex and refactoring is suggested). Here is a list of web frameworks examined: bottle, cherrypy, circuits, > > django, flask, pyramid, pysi, tornado, turbogears, web.py, web2py and > > wheezy.web. > > Cyclomatic complexity tells you nothing that counting lines of code doesn't already. > > http://www.scirp.org/Journal/PaperInformation.aspx?paperID=779 > > -- > Robert Kern > > "I have come to believe that the whole world is an enigma, a harmless enigma > that is made terrible by our own mad attempt to interpret it as though it had > an underlying truth." > -- Umberto Eco > > -- > http://mail.python.org/mailman/listinfo/python-list From robert.kern at gmail.com Tue Nov 20 15:07:54 2012 From: robert.kern at gmail.com (Robert Kern) Date: Tue, 20 Nov 2012 20:07:54 +0000 Subject: Web Frameworks Excessive Complexity In-Reply-To: References: , Message-ID: On 20/11/2012 19:46, Andriy Kornatskyy wrote: > > Robert, > > Thank you for the comment. I do not try relate CC with LOC. Instead pointing to excessive complexity, something that is beyond recommended threshold, a subject to refactoring in respective web frameworks. Those areas are likely to be potential source of bugs (e.g. due to low code coverage with unit tests) thus have certain degree of interest to both: end users and framework developers. Did you read the paper? I'm not suggesting that you compare CC with LoC; I'm suggesting that you don't use CC as a metric at all. The research is fairly conclusive that CC doesn't measure what you think it measures. The source of bugs is not excessive complexity in a method, just excessive lines of code. LoC is much simpler, easier to understand, and easier to correct than CC. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco From tjreedy at udel.edu Tue Nov 20 15:21:06 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 20 Nov 2012 15:21:06 -0500 Subject: Path Browser seems to be broken In-Reply-To: References: Message-ID: On 11/20/2012 1:23 PM, Peter Otten wrote: > Daniel Klein wrote: > >> If you try to expand any of the paths in the Path Browser (by clicking the >> + sign) then it not only closes the Path Browser but it also closes all >> other windows that were opened in IDLE, including the IDLE interpreter >> itself. >> >> A Google search doesn't look like this been reported. If this is truly a >> bug > > It is. > >> then can you point me in the right direction for issuing a bug report? > > is the place to go... > > >> I'm running... >> >> Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:57:17) [MSC v.1600 64 >> bit (AMD64)] on win32 > > > but this bug has already been reported: > > I would not expect Google to index the dynamic tracker form pages, certainly not all of them, as it would have to know which issue numbers are currently valid in the template 'http:/bugs/python.org/isue#', where # is up to 7 digits. The tracker has a search function. I found this issue by searching the tracker for 'all text: path browser', 'components: IDLE', and 'status: don't care' and then looking at the 8 responses. Path browser works in 2.7 and 3.2. It was broken in 3.3.0 and has been fixed for 3.3.1 and 3.4.0. The issue status is 'closed', so one must change the search status field from the default of 'open' to find it. -- Terry Jan Reedy From andriy.kornatskyy at live.com Tue Nov 20 15:22:49 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Tue, 20 Nov 2012 23:22:49 +0300 Subject: Web Frameworks Excessive Complexity In-Reply-To: References: , , , , Message-ID: Robert, I respect your point of view and it definitely make sense to me. I personally do not have a problem to understand CC but agree, method LoC is easier to understand. Regardless the path your choose in your next refactoring (based on method CC, LoC) it gives your better product. Andriy ---------------------------------------- > To: python-list at python.org > From: robert.kern at gmail.com > Subject: Re: Web Frameworks Excessive Complexity > Date: Tue, 20 Nov 2012 20:07:54 +0000 > > On 20/11/2012 19:46, Andriy Kornatskyy wrote: > > > > Robert, > > > > Thank you for the comment. I do not try relate CC with LOC. Instead pointing to excessive complexity, something that is beyond recommended threshold, a subject to refactoring in respective web frameworks. Those areas are likely to be potential source of bugs (e.g. due to low code coverage with unit tests) thus have certain degree of interest to both: end users and framework developers. > > Did you read the paper? I'm not suggesting that you compare CC with LoC; I'm > suggesting that you don't use CC as a metric at all. The research is fairly > conclusive that CC doesn't measure what you think it measures. The source of > bugs is not excessive complexity in a method, just excessive lines of code. LoC > is much simpler, easier to understand, and easier to correct than CC. > > -- > Robert Kern > > "I have come to believe that the whole world is an enigma, a harmless enigma > that is made terrible by our own mad attempt to interpret it as though it had > an underlying truth." > -- Umberto Eco > > -- > http://mail.python.org/mailman/listinfo/python-list From joshua.landau.ws at gmail.com Tue Nov 20 15:23:48 2012 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Tue, 20 Nov 2012 20:23:48 +0000 Subject: 10 sec poll - please reply! In-Reply-To: <50ABAD92.7070503@davea.name> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <50ABAD92.7070503@davea.name> Message-ID: On 20 November 2012 16:19, Dave Angel wrote: > On 11/20/2012 11:09 AM, John Gordon wrote: > > In <3d71f175-164e-494c-a521-2eaa5679b524 at googlegroups.com> Michael > Herrmann writes: > > > >> What, in your view, would be the most intuitive alternative name? > > keyboard_input(). > > > > Well, since Python already has input() and raw_input(), it would then be > clear that keyboard_input() would take some kind of data from the > keyboard, not send it. keyboard_output() output_keys() ? -------------- next part -------------- An HTML attachment was scrubbed... URL: From joshua.landau.ws at gmail.com Tue Nov 20 15:29:35 2012 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Tue, 20 Nov 2012 20:29:35 +0000 Subject: proxy?? In-Reply-To: <50AB9833.6020409@estudiantes.uci.cu> References: <50AB9833.6020409@estudiantes.uci.cu> Message-ID: On 20 November 2012 14:48, Jorge Alberto Diaz Orozco < jaorozco at estudiantes.uci.cu> wrote: > Hi there. > Does anyone knows how to manage headers using a simple proxy??? > I'm doing this but It gives me problems In some pages. > I don't know the answer, but I do know you'd get more favour if you explained what"problems" and "In some pages" refer to. > import SocketServer > import SimpleHTTPServer > import urllib2 > > PORT = 8888 > > class Proxy(SimpleHTTPServer.**SimpleHTTPRequestHandler): > def do_GET(self): > try: > print self.path > self.copyfile(urllib2.urlopen(**self.path), self.wfile) > except: > print 'error',self.path > > httpd = SocketServer.ForkingTCPServer(**('127.0.0.1', PORT), Proxy) > print "serving at port", PORT > httpd.serve_forever() > -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjreedy at udel.edu Tue Nov 20 15:32:21 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 20 Nov 2012 15:32:21 -0500 Subject: Problem with list.remove() method In-Reply-To: References: Message-ID: On 11/20/2012 9:14 AM, Chris Angelico wrote: > On Wed, Nov 21, 2012 at 12:56 AM, Alvaro Combo wrote: >> Hi All, >> >> I'm relatively new to Python... but I have found something I cannot explain... and I'm sure you can help me. >> >> I have the following function that serves for removing the duplicates from a list... It's a simple and (almost) trivial task. >> >> I'm using WingIDE as editor/debugger and have Python 2.7.3. >> >> When running this I have an error when trying to remove cpy_lst[4]... and ONLY THAT!!! Even in interactive mode!!! > > Several points here. You've written a beautiful O(N^2) duplicates > remover... Python has a really fast way of doing it, if you don't mind > losing order: > > cpy_lst = list(set(lst)) > > But let's assume you're doing this for the exercise. Your technique is > fine, if inefficient on large lists, but the remove() method looks for > the first occurrence of an element by its value - what you want is: > > del cpy_lst[i] > > which will remove one element by index. > > With that change, you'll have a slightly odd duplicate remover that > keeps the *last* of any given element. That's rather unusual. Instead, > you may want to consider maintaining a set of "items I've already > seen", and keeping all elements that aren't in that set. I won't give > you all the code, but here's the basic set operations: > > sighted = set() > sighted.add(some_element) > if some_element in sighted: # condition is True if you've already > seen this element The itertools doc, in the last section, has a recipe for this problem that uses the above approach. -- Terry Jan Reedy From robert.kern at gmail.com Tue Nov 20 15:33:46 2012 From: robert.kern at gmail.com (Robert Kern) Date: Tue, 20 Nov 2012 20:33:46 +0000 Subject: Web Frameworks Excessive Complexity In-Reply-To: References: , , , , Message-ID: On 20/11/2012 20:22, Andriy Kornatskyy wrote: > > Robert, > > I respect your point of view and it definitely make sense to me. I personally do not have a problem to understand CC but agree, method LoC is easier to understand. Regardless the path your choose in your next refactoring (based on method CC, LoC) it gives your better product. No, refactoring based on CC does not give you a better product, except by accident. -- 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 ramit.prasad at jpmorgan.com Tue Nov 20 15:39:51 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Tue, 20 Nov 2012 20:39:51 +0000 Subject: Index Error In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF474167BB43E@SCACMX008.exchad.jpmchase.net> Can you please post in plain text and stop top-posting? Thanks. inshu chauhan wrote: > > def distance(c, p): > ??? dist = sqrt( > ??????????? ((c[0]-p[0])**2) + > ??????????? ((c[1]-p[1])**2) + > ??????????? ((c[2]-p[2])**2) > ??????????? ) > ??? return dist > > > def GenerateRing(x,y, N): Generates square rings around a point in data which has 300 columns(x) and 3000 > rows(y) > ??? indices = [] > ??? for i in xrange(-N, N): > ??????? indices.append((x+i, y-N)) > ??????? indices.append((x+N, y+i)) > ??????? indices.append((x-i, y+N)) > ??????? indices.append((x-N, y-i)) > ??? return indices No, this creates a one dimensional list with 2N elements of where each element is a two item tuple. > > > def ComputeClasses(data): > ??? radius = .5 > ??? points = [] > ??? for cy in xrange(0, data.height): > ??????? for cx in xrange(0, data.width): > > ??????????? if data[cy,cx] == (0.0,0.0,0.0): > ??????????????? continue > ??????????? else : > ??????????????? centre = data[cy, cx] > ??????????????? points.append(centre) > > > ??????????? change = True > > ??????????? while change: > > ??????????????? for ring_number in xrange(1, 100): > ??????????????????? change = False > ??????????????????? new_indices = GenerateRing(cx, cy, ring_number) > ??????????????????? print new_indices > ??????????????????? for idx in new_indices:????????????????????????????????? I need help in this part as I am > unable to device a method in which if the points are out of index,it should stop and > ??????????????????????? if idx[0] >= 300 and idx[1] >= 3000:?????????? go to next centre and start generating > rings from there.. and again if the index is out of range .. this should repeat > ??????????????????????????? continue > ??????????????????????? else : > ??????????????????????????? point = data[idx[0], idx[1]] You can use a few different methods. This is just one example. for idx, temp_point in enumerate(new_indices): try: temp_point[0] temp_point[1] except Exception: #Should be IndexError I think. print 'idx: {0}\ntemp_point:{1}'.format(idx, temp_point) # Possibly add a break or exit so you do not have to # keep going once you hit a failure. point = data[temp_point[0], temp_point[1]] What is `data`? I have not seen any built-in structure that takes a tuple in this manner...unless it is a dictionary. Or from numpy. Given my lack of knowledge of what `data`, it could be the problem is there. That is one reason I accessed `temp_point[0]` and `temp_point[1]` separately. > ??????????????????????????? if point == (0.0, 0.0, 0.0 ): > ??????????????????????????????? print point > ??????????????????????????????? continue > ??????????????????????????? else: > ??????????????????????????????? dist = distance(centre, point) > ??????????????????????????????? print dist > ??????????????????????????????? if? dist < radius :?????????????? and rings should be added only when this > condition is satisfied > ??????????????????????????????????? print point > ??????????????????????????????????? points.append(point) > ??????????????????????????????????? change = True > ??????????????????????????????????? print change > > > ??????????????????????? break > > > ??????????? print points > > > ERROR now : > > data loaded > [(296, 403), (298, 403), (298, 405), (296, 405), (297, 403), (298, 404), (297, 405), (296, 404)] ... I am > printing Indices to know what index it dies out.. > > Traceback (most recent call last): > ? File "Z:/modules/Classify.py", line 73, in > ??? ComputeClasses(data) > ? File "Z:/modules/Classify.py", line 49, in ComputeClasses > ??? point = data[idx[0], idx[1]] > error: index is out of range > Is that the actual error? If so, then the problem is not `idx` or `temp_point` but instead `data`. If it is not the exact error, please copy and paste the error message *exactly* as given. >>> [1][2] IndexError: list index out of range >>> (1,2)[3] IndexError: tuple index out of range ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From ramit.prasad at jpmorgan.com Tue Nov 20 15:47:47 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Tue, 20 Nov 2012 20:47:47 +0000 Subject: Problem with list.remove() method In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF474167BB469@SCACMX008.exchad.jpmchase.net> Alvaro Combo wrote: > > Hi All, > > I'm relatively new to Python... but I have found something I cannot explain... and I'm sure you can help me. > > I have the following function that serves for removing the duplicates from a list... It's a simple and (almost) > trivial task. > > I'm using WingIDE as editor/debugger and have Python 2.7.3. > > When running this I have an error when trying to remove cpy_lst[4]... and ONLY THAT!!! Even in interactive > mode!!! > > Any suggestion is MOST welcome. > > Best Regards > > ACombo > > ...And the code: > > def remove_dup_5_10(): > """ > Remove the duplicates of a given list. The original list MUST be kept. > """ > > # Set the original list > lst = ['a', 1, 10.0, 2, 'd', 'b', 'b', 'b', 1, 2, 'b' ] > > # NEED to create a copy... See dicussion on Problem 5.6 and issue #2 > cpy_lst = list(lst) > > # Perform an infinite loop... explained later > i=0 # initialize the index > while i != len(cpy_lst): > if cpy_lst.count(cpy_lst[i]) != 1: > cpy_lst.remove(i) > else: > i += 1 > > print "The original List: ", lst > print "List with NO duplicates: ", cpy_lst > > return True > -- Remove looks for the *value* not the *index* of the value. >>> help([].remove) Help on built-in function remove: remove(...) L.remove(value) -- remove first occurrence of value. Raises ValueError if the value is not present. Change ` cpy_lst.remove(i)` to `cpy_lst.remove(cpy_lst[i])`. ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From rosuav at gmail.com Tue Nov 20 15:48:08 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 21 Nov 2012 07:48:08 +1100 Subject: Problem with list.remove() method In-Reply-To: <9caaea9c-8b7c-4ed5-bc3b-2519a58ceb26@googlegroups.com> References: <9caaea9c-8b7c-4ed5-bc3b-2519a58ceb26@googlegroups.com> Message-ID: On Wed, Nov 21, 2012 at 1:37 AM, Alvaro Combo wrote: > Dear Chris, > > Thank you very much for you reply... > For a newcomer to Python the Hell is in the details... :-). You're most welcome! As Adam Savage of Mythbusters is fond of saying (with an exaggerated accent), "It's all a learning experience". > Regarding you other (most relevant) comments, I absolutely agree... BUT in those cases... I was aware :-). But since it is in fact an exercise... and having N^2 operations... is not important. Still your comments are quite pertinent. Yep. O(N^2) isn't inherently a problem, it just means that the technique will scale poorly to large numbers of list elements. With small lists, it'll be "fast enough" - for all intents and purposes, the bulk of Python code executes in zero time, despite being in an oh so slow interpreted language and using ridiculously inefficient (but beautifully readable) code. That's the beauty of modern hardware :) However, I do think that people should be aware when they're writing non-scaleable code. That's not just algorithmic complexity; if you're making a system that won't handle more than one request a second (because, for instance, it uses seconds-since-1970 as a request identifier), that's something worth being aware of, even if it's unlikely ever to be a problem. Just know, so that if a problem ever _does_ occur, you know where it is! ChrisA From ian.g.kelly at gmail.com Tue Nov 20 15:53:12 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 20 Nov 2012 13:53:12 -0700 Subject: [Python 3.3/Windows] Path Browser seems to be broken In-Reply-To: <18F56CB3AB1841D4BF7D12D334EA8751@AKEPA> References: <18F56CB3AB1841D4BF7D12D334EA8751@AKEPA> Message-ID: On Tue, Nov 20, 2012 at 7:58 AM, Daniel Klein wrote: > If you try to expand any of the paths in the Path Browser (by clicking the + > sign) then it not only closes the Path Browser but it also closes all other > windows that were opened in IDLE, including the IDLE interpreter itself. > > I did a Google search and it doesn't look like this been reported. If this > is truly a bug then can you point me in the right direction for issuing a > bug report? Already reported and fixed here: http://bugs.python.org/issue16226 From rosuav at gmail.com Tue Nov 20 15:57:26 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 21 Nov 2012 07:57:26 +1100 Subject: 10 sec poll - please reply! In-Reply-To: <50abadf3$0$29987$c3e8da3$5496439d@news.astraweb.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <7290a127-97e1-4032-98ae-6e602b87b74b@googlegroups.com> <50abadf3$0$29987$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Wed, Nov 21, 2012 at 3:21 AM, Steven D'Aprano wrote: > On Tue, 20 Nov 2012 07:18:42 -0800, Michael Herrmann wrote: > >> Thanks again for your further replies. So far, it's 4 votes for >> 'send_keys' and 1 vote for 'type'. >> >> Regarding 'send_keys': To me personally it makes sense to send keys _to_ >> something. However, in our API, send_keys would not be called on an >> object or with a parameter indicating the target. It would just be >> >> send_keys(ENTER) >> send_keys("Hello World!") >> send_keys(CTRL + 'a') > > > "send_keys" is wrong, because you aren't sending keys. You're sending > strings, except you aren't actually sending strings either, because > "send" does not make sense without a target. You're automating the typing > of strings, including control characters. That depends on what the function actually does. If it sends a single command to blat a string, including control characters, to the target, then yes, it's sending a string. But if, as my reading of the OP tells me, the last one is "send press-Ctrl, send press-a, send release-a, send release-Ctrl", then it's sending keys, and the name should say so. And it's this method that the key-sender in the Yosemite project uses (though, for hysterical raisins, its function is called "dokey" - which I am NOT recommending). ChrisA From rosuav at gmail.com Tue Nov 20 16:00:43 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 21 Nov 2012 08:00:43 +1100 Subject: Yet another Python textbook In-Reply-To: <31a82817-8c9b-4dd2-a468-89d8d081fd1b@googlegroups.com> References: <31a82817-8c9b-4dd2-a468-89d8d081fd1b@googlegroups.com> Message-ID: On Wed, Nov 21, 2012 at 1:57 AM, wrote: > Le mardi 20 novembre 2012 09:09:50 UTC+1, Chris Angelico a ?crit : >> On Tue, Nov 20, 2012 at 7:02 PM, Pavel Solin wrote: >> >> > Perhaps you are right. Is there any statistics of how many Python >> >> > programmers are using 2.7 vs. 3? Most of people I know use 2.7. >> >> >> >> If you're teaching Python, the stats are probably about zero for zero. >> >> Start them off on Py3 and help move the world forward. >> >> >> >> ChrisA > > -------- > > Do not count with me. > > The absurd flexible string representation has practically > borrowed the idea to propose once Python has a teaching tool. To the OP: jmf has an unnatural hatred of Python 3.3 and PEP 393 strings. Take no notice; the rest of the world sees this as a huge advantage. Python is now in a VERY small group of languages (I'm aware of just one other) that have absolutely proper Unicode handling *and* efficient string handling. ChrisA From ramit.prasad at jpmorgan.com Tue Nov 20 16:08:24 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Tue, 20 Nov 2012 21:08:24 +0000 Subject: 10 sec poll - please reply! In-Reply-To: <50abadf3$0$29987$c3e8da3$5496439d@news.astraweb.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <7290a127-97e1-4032-98ae-6e602b87b74b@googlegroups.com> <50abadf3$0$29987$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF474167BB4FF@SCACMX008.exchad.jpmchase.net> Steven D'Aprano wrote: > > On Tue, 20 Nov 2012 07:18:42 -0800, Michael Herrmann wrote: > > > Thanks again for your further replies. So far, it's 4 votes for > > 'send_keys' and 1 vote for 'type'. > > > > Regarding 'send_keys': To me personally it makes sense to send keys _to_ > > something. However, in our API, send_keys would not be called on an > > object or with a parameter indicating the target. It would just be > > > > send_keys(ENTER) > > send_keys("Hello World!") > > send_keys(CTRL + 'a') > > > "send_keys" is wrong, because you aren't sending keys. You're sending > strings, except you aren't actually sending strings either, because > "send" does not make sense without a target. You're automating the typing > of strings, including control characters. simulate_keypress simulate_key(s)_down send_kb_press fake_typing send_char(s) > > I believe that your initial instinct for the name of this function was > correct. It automates typing, so you should call it "type" or (for those > paranoid about shadowing the built-in, "type_str". > I can too easily see somebody doing from module import * OR from module import type. ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From brintoul at controlledthinking.com Tue Nov 20 16:14:53 2012 From: brintoul at controlledthinking.com (brintoul at controlledthinking.com) Date: Tue, 20 Nov 2012 13:14:53 -0800 (PST) Subject: Choosing Source Address to Bind Socket to in IMAP Client Message-ID: <72731eb3-1521-46f0-8601-8a2df58849d8@googlegroups.com> Hello: I have a multihomed machine that I would like to run the Python imaplib's IMAP4 client on. I would like to be able to specify which interface the underlying socket will bind to as its source address. How could I best do this? Thanks for any help... From edriscoll at wisc.edu Tue Nov 20 16:24:59 2012 From: edriscoll at wisc.edu (Evan Driscoll) Date: Tue, 20 Nov 2012 15:24:59 -0600 Subject: Managing multiple packages Message-ID: <50ABF52B.1080101@wisc.edu> I have perhaps a bit of a silly question, but I'm interested in what people do for workflow when actively developing multiple Python modules at once (for a single project). Suppose I have packages A-C. In addition to being modules in the Python sense, they are logically distinct, probably sit in different repositories, etc., so there's a directory layout like my_project/ +-- moduleA/ | +-- setup.py | +-- src/ | | +-- A/ | | +-- __init__.py | +-- doc/ | +-- how_to_be_awesome.md +-- moduleB/ | +-- setup.py | +-- src/ | +-- B/ | +-- __init__.py +-- moduleC/ +-- setup.py +-- src/ +-- C/ +-- __init__.py Finally, suppose that you're changing between editing all three modules. How do you deal with this? Do you set PYTHONPATH=my_project/moduleA/src:my_project/moduleB/src:my_project/moduleC/src, adding a new entry to PYTHONPATH for each module you need to use? Install all of the modules to some central location (e.g. my_project/install) and just point PYTHONPATH to there? Put symlinks in some central location and point them at the module directories? Mess with sys.path at the beginning of any scripts you want to run? Some other trick I don't know of? I've sort of tended to do the my_project/install thing, but it's pretty easy to forget to re-install a module after changing something, and possible to accidentally edit the copy of the file in the install directory (which will just be overwritten). Evan -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 554 bytes Desc: OpenPGP digital signature URL: From thudfoo at gmail.com Tue Nov 20 16:33:04 2012 From: thudfoo at gmail.com (xDog Walker) Date: Tue, 20 Nov 2012 13:33:04 -0800 Subject: 10 sec poll - please reply! In-Reply-To: <528c4303-4067-404c-a831-5d362b4eadf6@googlegroups.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <528c4303-4067-404c-a831-5d362b4eadf6@googlegroups.com> Message-ID: <201211201333.04480.thudfoo@gmail.com> On Tuesday 2012 November 20 08:29, mherrmann.at at gmail.com wrote: > To everyone else who has been so kind to reply thus far: What do you think > of generate_keystrokes? It's a bit long but describes exactly what the > function would be doing. If not already offered and rejected, how about enter() ? -- Yonder nor sorghum stenches shut ladle gulls stopper torque wet strainers. From tjreedy at udel.edu Tue Nov 20 16:34:14 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 20 Nov 2012 16:34:14 -0500 Subject: Yet another Python textbook In-Reply-To: References: Message-ID: On 11/20/2012 3:02 AM, Pavel Solin wrote: > previous page that Python 3 was released in 2008. Is there any work > underway get Python 3 into NCLab? > > > There is an ongoing discussion but we are not sure. > Are there any reasons except for the print () command > and division of integers? (In addition to Ian's answer, which points to an already long list of new features...) There are two separate questions. First, which versions of Python should NCLab support? I would say both 2.7 and 3.x+. Since Py3 support does not exist now, starting with 3.3+ might work best. Second, if NCLab supported both, which to teach in the book? I would say 3.3+. Python 3 has many improvements from a teaching standpoint. For instance, old-style classes are gone, so class statements produce modern-style classes by default. You can just say that the headers class C: class C(object): have the same effect and never mention that there was once a separate user-class system. Py 3 used unicode for text, and 3.3 now has a correct and portable unicode implementation. While non-ascii and even non-latin1 characters are not needed for interfacing with ascii-only instruments, scientific text uses lots of them. ... > Perhaps you are right. Is there any statistics of how many Python > programmers are using 2.7 vs. 3? Most of people I know use 2.7. Experienced Python programmers are not the target of your book. Many school/university classes have moved to Py3, and more will in the future. Many people who want to move to Py3 cannot because they *have to use* a Py2-only library. -- Terry Jan Reedy From rosuav at gmail.com Tue Nov 20 16:47:55 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 21 Nov 2012 08:47:55 +1100 Subject: Choosing Source Address to Bind Socket to in IMAP Client In-Reply-To: <72731eb3-1521-46f0-8601-8a2df58849d8@googlegroups.com> References: <72731eb3-1521-46f0-8601-8a2df58849d8@googlegroups.com> Message-ID: On Wed, Nov 21, 2012 at 8:14 AM, wrote: > Hello: > > I have a multihomed machine that I would like to run the Python imaplib's IMAP4 client on. I would like to be able to specify which interface the underlying socket will bind to as its source address. How could I best do this? You're referring to this function? http://docs.python.org/3.3/library/imaplib.html#imaplib.IMAP4 The docs suggest that you can simply pass it a parameter to specify the address to bind to. (Note that you bind to addresses, not interfaces. Figuring out which interface has which address is a separate issue.) ChrisA From danielkleinad at gmail.com Tue Nov 20 16:49:48 2012 From: danielkleinad at gmail.com (Daniel Klein) Date: Tue, 20 Nov 2012 16:49:48 -0500 Subject: Encoding conundrum Message-ID: With the assistance of this group I am understanding unicode encoding issues much better; especially when handling special characters that are outside of the ASCII range. I've got my application working perfectly now :-) However, I am still confused as to why I can only use one specific encoding. I've done some research and it appears that I should be able to use any of the following codecs with codepoints '\xfc' (chr(252)) '\xfd' (chr(253)) and '\xfe' (chr(254)) : ISO-8859-1 [ note that I'm using this codec on my Linux box ] cp1252 cp437 latin1 utf-8 If I'm not mistaken, all of these codecs can handle the complete 8bit character set. However, on Windows 7, I am only able to use 'cp437' to display (print) data with those characters in Python. If I use any other encoding, Windows laughs at me with this error message: File "C:\Python33\lib\encodings\cp437.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_map)[0] UnicodeEncodeError: 'charmap' codec can't encode character '\xfd' in position 3: character maps to Furthermore I get this from IDLE: >>> import locale >>> locale.getdefaultlocale() ('en_US', 'cp1252') I also get 'cp1252' when running the same script from a Windows command prompt. So there is a contradiction between the error message and the default encoding. Why am I restricted from using just that one codec? Is this a Windows or Python restriction? Please enlighten me. Dan -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Tue Nov 20 16:55:54 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 20 Nov 2012 21:55:54 +0000 Subject: Yet another Python textbook In-Reply-To: References: <31a82817-8c9b-4dd2-a468-89d8d081fd1b@googlegroups.com> Message-ID: On 20/11/2012 21:00, Chris Angelico wrote: > > To the OP: jmf has an unnatural hatred of Python 3.3 and PEP 393 > strings. Take no notice; the rest of the world sees this as a huge > advantage. Python is now in a VERY small group of languages (I'm aware > of just one other) that have absolutely proper Unicode handling *and* > efficient string handling. > > ChrisA > Rather more polite than the response I've had sitting in my drafts folder for several hours. I'm so pleased I didn't send it, I can now happily delete it and move on :) -- Cheers. Mark Lawrence. From ramit.prasad at jpmorgan.com Tue Nov 20 16:58:55 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Tue, 20 Nov 2012 21:58:55 +0000 Subject: Choosing Source Address to Bind Socket to in IMAP Client In-Reply-To: <72731eb3-1521-46f0-8601-8a2df58849d8@googlegroups.com> References: <72731eb3-1521-46f0-8601-8a2df58849d8@googlegroups.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF474167BB609@SCACMX008.exchad.jpmchase.net> brintoul at controlledthinking.com wrote: > > Hello: > > I have a multihomed machine that I would like to run the Python imaplib's IMAP4 client on. I would like to be > able to specify which interface the underlying socket will bind to as its source address. How could I best do > this? One assumes by programming. > > Thanks for any help... You are quite welcome. :) On a less flippant note, maybe some links would help you get started. http://www.doughellmann.com/PyMOTW/imaplib/ http://yuji.wordpress.com/2011/06/22/python-imaplib-imap-example-with-gmail/ http://docs.python.org/2/library/imaplib.html ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From brintoul at controlledthinking.com Tue Nov 20 17:00:42 2012 From: brintoul at controlledthinking.com (brintoul at controlledthinking.com) Date: Tue, 20 Nov 2012 14:00:42 -0800 (PST) Subject: Choosing Source Address to Bind Socket to in IMAP Client In-Reply-To: References: <72731eb3-1521-46f0-8601-8a2df58849d8@googlegroups.com> Message-ID: <2d0c91ed-b3b0-4c1d-bb10-5ec1357ef9ce@googlegroups.com> On Tuesday, November 20, 2012 1:48:46 PM UTC-8, Chris Angelico wrote: > On Wed, Nov 21, 2012 at 8:14 AM, wrote: > > > Hello: > > > > > > I have a multihomed machine that I would like to run the Python imaplib's IMAP4 client on. I would like to be able to specify which interface the underlying socket will bind to as its source address. How could I best do this? > > > > You're referring to this function? > > > > http://docs.python.org/3.3/library/imaplib.html#imaplib.IMAP4 > > > > The docs suggest that you can simply pass it a parameter to specify > > the address to bind to. (Note that you bind to addresses, not > > interfaces. Figuring out which interface has which address is a > > separate issue.) > > > > ChrisA Unless I'm reading that wrong, that's specifying the address/host to connect to (the destination address) not the source address... From brintoul at controlledthinking.com Tue Nov 20 17:00:42 2012 From: brintoul at controlledthinking.com (brintoul at controlledthinking.com) Date: Tue, 20 Nov 2012 14:00:42 -0800 (PST) Subject: Choosing Source Address to Bind Socket to in IMAP Client In-Reply-To: References: <72731eb3-1521-46f0-8601-8a2df58849d8@googlegroups.com> Message-ID: <2d0c91ed-b3b0-4c1d-bb10-5ec1357ef9ce@googlegroups.com> On Tuesday, November 20, 2012 1:48:46 PM UTC-8, Chris Angelico wrote: > On Wed, Nov 21, 2012 at 8:14 AM, wrote: > > > Hello: > > > > > > I have a multihomed machine that I would like to run the Python imaplib's IMAP4 client on. I would like to be able to specify which interface the underlying socket will bind to as its source address. How could I best do this? > > > > You're referring to this function? > > > > http://docs.python.org/3.3/library/imaplib.html#imaplib.IMAP4 > > > > The docs suggest that you can simply pass it a parameter to specify > > the address to bind to. (Note that you bind to addresses, not > > interfaces. Figuring out which interface has which address is a > > separate issue.) > > > > ChrisA Unless I'm reading that wrong, that's specifying the address/host to connect to (the destination address) not the source address... From breamoreboy at yahoo.co.uk Tue Nov 20 17:02:45 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 20 Nov 2012 22:02:45 +0000 Subject: How to use the python to do the unit test In-Reply-To: <201211210140534223470@gmail.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> , <528c4303-4067-404c-a831-5d362b4eadf6@googlegroups.com> <201211210140534223470@gmail.com> Message-ID: On 20/11/2012 17:40, yujian4newsgroup at gmail.com wrote: > I write a mfc application, then I want to use the python to test this application. just as user click that button. Please tell me how to write the python application? > Easy, open your favourite editor and start typing, what's the problem? -- Cheers. Mark Lawrence. From brintoul at controlledthinking.com Tue Nov 20 17:12:30 2012 From: brintoul at controlledthinking.com (brintoul at controlledthinking.com) Date: Tue, 20 Nov 2012 14:12:30 -0800 (PST) Subject: Choosing Source Address to Bind Socket to in IMAP Client In-Reply-To: References: <72731eb3-1521-46f0-8601-8a2df58849d8@googlegroups.com> Message-ID: On Tuesday, November 20, 2012 1:59:34 PM UTC-8, Prasad, Ramit wrote: > brintoul at controlledthinking.com wrote: > > > > > > Hello: > > > > > > I have a multihomed machine that I would like to run the Python imaplib's IMAP4 client on. I would like to be > > > able to specify which interface the underlying socket will bind to as its source address. How could I best do > > > this? > > > > One assumes by programming. > > > > > > > > Thanks for any help... > > > > You are quite welcome. :) > > > > On a less flippant note, maybe some links would help you > > get started. > > > > http://www.doughellmann.com/PyMOTW/imaplib/ > > http://yuji.wordpress.com/2011/06/22/python-imaplib-imap-example-with-gmail/ > > http://docs.python.org/2/library/imaplib.html > > > > > > ~Ramit > > > > > > This email is confidential and subject to important disclaimers and > > conditions including on offers for the purchase or sale of > > securities, accuracy and completeness of information, viruses, > > confidentiality, legal privilege, and legal entity disclaimers, > > available at http://www.jpmorgan.com/pages/disclosures/email. While I appreciate your attempt at humor, this is not information which helps me. Perhaps you are not familiar with the basics of socket programming. This is all fine and dandy, and it is good to "know what you don't know". If you're interested, look at the third argument to "create_connection": http://docs.python.org/2/library/socket.html From brintoul at controlledthinking.com Tue Nov 20 17:12:30 2012 From: brintoul at controlledthinking.com (brintoul at controlledthinking.com) Date: Tue, 20 Nov 2012 14:12:30 -0800 (PST) Subject: Choosing Source Address to Bind Socket to in IMAP Client In-Reply-To: References: <72731eb3-1521-46f0-8601-8a2df58849d8@googlegroups.com> Message-ID: On Tuesday, November 20, 2012 1:59:34 PM UTC-8, Prasad, Ramit wrote: > brintoul at controlledthinking.com wrote: > > > > > > Hello: > > > > > > I have a multihomed machine that I would like to run the Python imaplib's IMAP4 client on. I would like to be > > > able to specify which interface the underlying socket will bind to as its source address. How could I best do > > > this? > > > > One assumes by programming. > > > > > > > > Thanks for any help... > > > > You are quite welcome. :) > > > > On a less flippant note, maybe some links would help you > > get started. > > > > http://www.doughellmann.com/PyMOTW/imaplib/ > > http://yuji.wordpress.com/2011/06/22/python-imaplib-imap-example-with-gmail/ > > http://docs.python.org/2/library/imaplib.html > > > > > > ~Ramit > > > > > > This email is confidential and subject to important disclaimers and > > conditions including on offers for the purchase or sale of > > securities, accuracy and completeness of information, viruses, > > confidentiality, legal privilege, and legal entity disclaimers, > > available at http://www.jpmorgan.com/pages/disclosures/email. While I appreciate your attempt at humor, this is not information which helps me. Perhaps you are not familiar with the basics of socket programming. This is all fine and dandy, and it is good to "know what you don't know". If you're interested, look at the third argument to "create_connection": http://docs.python.org/2/library/socket.html From rosuav at gmail.com Tue Nov 20 17:25:48 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 21 Nov 2012 09:25:48 +1100 Subject: Yet another Python textbook In-Reply-To: References: <31a82817-8c9b-4dd2-a468-89d8d081fd1b@googlegroups.com> Message-ID: On Wed, Nov 21, 2012 at 8:55 AM, Mark Lawrence wrote: > On 20/11/2012 21:00, Chris Angelico wrote: >> >> >> To the OP: jmf has an unnatural hatred of Python 3.3 and PEP 393 >> strings. Take no notice; the rest of the world sees this as a huge >> advantage. Python is now in a VERY small group of languages (I'm aware >> of just one other) that have absolutely proper Unicode handling *and* >> efficient string handling. >> >> ChrisA >> > > Rather more polite than the response I've had sitting in my drafts folder > for several hours. I'm so pleased I didn't send it, I can now happily > delete it and move on :) Polite is good :) Incidentally, if anyone else knows of a language that fits the description above, I'd be most curious. Maybe we're going to see a revolution in language design - with everyone adopting this sort of string handling - or in language usage - with everyone adopting Python or Pike. Hmm. We're having major security issues with Joomla, I wonder how hard it'd be to convince our webmaster to switch to a Python-based web framework... Dreaming-ly yours, ChrisA From rosuav at gmail.com Tue Nov 20 17:35:14 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 21 Nov 2012 09:35:14 +1100 Subject: Choosing Source Address to Bind Socket to in IMAP Client In-Reply-To: <2d0c91ed-b3b0-4c1d-bb10-5ec1357ef9ce@googlegroups.com> References: <72731eb3-1521-46f0-8601-8a2df58849d8@googlegroups.com> <2d0c91ed-b3b0-4c1d-bb10-5ec1357ef9ce@googlegroups.com> Message-ID: On Wed, Nov 21, 2012 at 9:00 AM, wrote: > On Tuesday, November 20, 2012 1:48:46 PM UTC-8, Chris Angelico wrote: >> On Wed, Nov 21, 2012 at 8:14 AM, wrote: >> >> > I have a multihomed machine that I would like to run the Python imaplib's IMAP4 client on. I would like to be able to specify which interface the underlying socket will bind to as its source address. How could I best do this? >> >> You're referring to this function? >> >> http://docs.python.org/3.3/library/imaplib.html#imaplib.IMAP4 >> >> The docs suggest that you can simply pass it a parameter to specify >> the address to bind to. (Note that you bind to addresses, not >> interfaces. Figuring out which interface has which address is a >> separate issue.) >> > > Unless I'm reading that wrong, that's specifying the address/host to connect to (the destination address) not the source address... Ah, whoops! My bad. For some reason I was thinking that was creating a server socket. Sorry! Poking around in the source (imaplib.py) shows that it creates a socket here: class IMAP4: def _create_socket(self): return socket.create_connection((self.host, self.port)) Adding a third parameter to create_connection would do what you want. (Note that this is currently getting one parameter, not two.) http://docs.python.org/3.3/library/socket.html#socket.create_connection My recommendation: Subclass IMAP4 and override this one function. ChrisA From ramit.prasad at jpmorgan.com Tue Nov 20 17:41:13 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Tue, 20 Nov 2012 22:41:13 +0000 Subject: Choosing Source Address to Bind Socket to in IMAP Client In-Reply-To: References: <72731eb3-1521-46f0-8601-8a2df58849d8@googlegroups.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF474167BBC92@SCACMX008.exchad.jpmchase.net> brintoul at controlledthinking.com wrote: > > > I have a multihomed machine that I would like to run the Python imaplib's IMAP4 client on. I would like to be > > > able to specify which interface the underlying socket will bind to as its source address. How could I best do > > > this? [snip] > While I appreciate your attempt at humor, this is not information which helps me. Perhaps you are not familiar > with the basics of socket programming. This is all fine and dandy, and it is good to "know what you don't know". > If you're interested, look at the third argument to "create_connection": > http://docs.python.org/2/library/socket.html Apologies, I misread your question. According to the imaplib docs, you can subclass IMAP4 and override `IMAP4.open` to create the socket and bind it to the desired interface. You could also connect using IMAP4 and then close/reopen the socket inside the IMAP4 connection object. I hope that is more help, Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From d at davea.name Tue Nov 20 17:46:55 2012 From: d at davea.name (Dave Angel) Date: Tue, 20 Nov 2012 17:46:55 -0500 Subject: Encoding conundrum In-Reply-To: References: Message-ID: <50AC085F.1040701@davea.name> On 11/20/2012 04:49 PM, Daniel Klein wrote: > With the assistance of this group I am understanding unicode encoding > issues much better; especially when handling special characters that are > outside of the ASCII range. I've got my application working perfectly now > :-) > > However, I am still confused as to why I can only use one specific encoding. Who says you can only use one? You need to use the right encoding for the device or file you're talking with, and if different devices want different encodings, then you must use multiple ones. Only one can be the default, however, and that's where some problems come about. > > I've done some research and it appears that I should be able to use any of > the following codecs with codepoints '\xfc' (chr(252)) '\xfd' (chr(253)) > and '\xfe' (chr(254)) : > > ISO-8859-1 [ note that I'm using this codec on my Linux box ] > cp1252 > cp437 > latin1 > utf-8 > > If I'm not mistaken, all of these codecs can handle the complete 8bit > character set. What 8 bit character set? This is a nonsense statement. If you mean all of them can convert an 8 bit byte to SOME unicode character, then fine. But they won't convert each such byte to the SAME unicode character, or they'd be the same encoding. > However, on Windows 7, I am only able to use 'cp437' to display (print) > data with those characters in Python. If I use any other encoding, Windows > laughs at me with this error message: > > File "C:\Python33\lib\encodings\cp437.py", line 19, in encode > return codecs.charmap_encode(input,self.errors,encoding_map)[0] > UnicodeEncodeError: 'charmap' codec can't encode character '\xfd' in > position 3: character maps to > > Furthermore I get this from IDLE: > >>>> import locale >>>> locale.getdefaultlocale() > ('en_US', 'cp1252') > > I also get 'cp1252' when running the same script from a Windows command > prompt. > > So there is a contradiction between the error message and the default > encoding. > > Why am I restricted from using just that one codec? Is this a Windows or > Python restriction? Please enlighten me. > > > I don't know much about Windows quirks anymore. I haven't had to use it much for years. -- DaveA From brintoul at controlledthinking.com Tue Nov 20 17:55:44 2012 From: brintoul at controlledthinking.com (brintoul at controlledthinking.com) Date: Tue, 20 Nov 2012 14:55:44 -0800 (PST) Subject: Choosing Source Address to Bind Socket to in IMAP Client In-Reply-To: References: <72731eb3-1521-46f0-8601-8a2df58849d8@googlegroups.com> Message-ID: On Tuesday, November 20, 2012 2:41:58 PM UTC-8, Prasad, Ramit wrote: > brintoul at controlledthinking.com wrote: > > Apologies, I misread your question. > > > > According to the imaplib docs, you can subclass IMAP4 and override > > `IMAP4.open` to create the socket and bind it to the desired interface. > > You could also connect using IMAP4 and then close/reopen the socket > > inside the IMAP4 connection object. > > I hope that is more help, > > Ramit > Thanks, yes, that helps! Just out of curiosity, can you give me a quick link to the docs you are looking at? I'm not seeing anything which gets very specific in the docs for Python 2.7 at docs.python.org ... From brintoul at controlledthinking.com Tue Nov 20 17:55:44 2012 From: brintoul at controlledthinking.com (brintoul at controlledthinking.com) Date: Tue, 20 Nov 2012 14:55:44 -0800 (PST) Subject: Choosing Source Address to Bind Socket to in IMAP Client In-Reply-To: References: <72731eb3-1521-46f0-8601-8a2df58849d8@googlegroups.com> Message-ID: On Tuesday, November 20, 2012 2:41:58 PM UTC-8, Prasad, Ramit wrote: > brintoul at controlledthinking.com wrote: > > Apologies, I misread your question. > > > > According to the imaplib docs, you can subclass IMAP4 and override > > `IMAP4.open` to create the socket and bind it to the desired interface. > > You could also connect using IMAP4 and then close/reopen the socket > > inside the IMAP4 connection object. > > I hope that is more help, > > Ramit > Thanks, yes, that helps! Just out of curiosity, can you give me a quick link to the docs you are looking at? I'm not seeing anything which gets very specific in the docs for Python 2.7 at docs.python.org ... From ian.g.kelly at gmail.com Tue Nov 20 18:03:14 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 20 Nov 2012 16:03:14 -0700 Subject: Encoding conundrum In-Reply-To: References: Message-ID: On Tue, Nov 20, 2012 at 2:49 PM, Daniel Klein wrote: > With the assistance of this group I am understanding unicode encoding issues > much better; especially when handling special characters that are outside of > the ASCII range. I've got my application working perfectly now :-) > > However, I am still confused as to why I can only use one specific encoding. > > I've done some research and it appears that I should be able to use any of > the following codecs with codepoints '\xfc' (chr(252)) '\xfd' (chr(253)) and > '\xfe' (chr(254)) : These refer to the characters with *Unicode* codepoints 252, 253, and 254: >>> unicodedata.name('\xfc') 'LATIN SMALL LETTER U WITH DIAERESIS' >>> unicodedata.name('\xfd') 'LATIN SMALL LETTER Y WITH ACUTE' >>> unicodedata.name('\xfe') 'LATIN SMALL LETTER THORN' > ISO-8859-1 [ note that I'm using this codec on my Linux box ] For ISO 8859-1, these characters happen to exist and even correspond to the same ordinals: 252, 253, and 254 (this is by design); so there is no problem encoding them, and the resulting bytes even happen to match the codepoints of the characters. > cp1252 cp1252 is designed after ISO 8859-1 and also has those same three characters: >>> for char in b'\xfc\xfd\xfe'.decode('cp1252'): ... print(unicodedata.name(char)) ... LATIN SMALL LETTER U WITH DIAERESIS LATIN SMALL LETTER Y WITH ACUTE LATIN SMALL LETTER THORN > latin1 Latin-1 is just another name for ISO 8859-1. > utf-8 UTF-8 is a *multi-byte* encoding. It can encode any Unicode characters, so you can represent those three characters in UTF-8, but with a different (and longer) byte sequence: >>> print('\xfc\xfd\xfd'.encode('utf8')) b'\xc3\xbc\xc3\xbd\xc3\xbd' > cp437 cp437 is another 8-bit encoding, but it maps entirely different characters to those three bytes: >>> for char in b'\xfc\xfd\xfe'.decode('cp437'): ... print(unicodedata.name(char)) ... SUPERSCRIPT LATIN SMALL LETTER N SUPERSCRIPT TWO BLACK SQUARE As it happens, the character at codepoint 252 (that's LATIN SMALL LETTER U WITH DIAERESIS) does exist in cp437. It maps to the byte 0x81: >>> '\xfc'.encode('cp437') b'\x81' The other two Unicode characters, at codepoints 253 and 254, do not exist at all in cp437 and cannot be encoded. > If I'm not mistaken, all of these codecs can handle the complete 8bit > character set. There is no "complete 8bit character set". cp1252, Latin1, and cp437 are all 8-bit character sets, but they're *different* 8-bit character sets with only partial overlap. > However, on Windows 7, I am only able to use 'cp437' to display (print) data > with those characters in Python. If I use any other encoding, Windows laughs > at me with this error message: > > File "C:\Python33\lib\encodings\cp437.py", line 19, in encode > return codecs.charmap_encode(input,self.errors,encoding_map)[0] > UnicodeEncodeError: 'charmap' codec can't encode character '\xfd' in > position 3: character maps to It would be helpful to see the code you're running that causes this error. > Furthermore I get this from IDLE: > >>>> import locale >>>> locale.getdefaultlocale() > ('en_US', 'cp1252') > > I also get 'cp1252' when running the same script from a Windows command > prompt. > > So there is a contradiction between the error message and the default > encoding. If you're printing to stdout, it's going to use the encoding associated with stdout, which does not necessarily have anything to do with the default locale. Use this to determine what character set you need to be working in if you want your data to be printable: >>> import sys >>> sys.stdout.encoding 'cp437' > Why am I restricted from using just that one codec? Is this a Windows or > Python restriction? Please enlighten me. In Linux, your terminal encoding is probably either UTF-8 or Latin-1, and either way it has no problems encoding that data for output. In a Windows cmd terminal, the default terminal encoding is cp437, which can't support two of the three characters you mentioned above. From ramit.prasad at jpmorgan.com Tue Nov 20 18:10:27 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Tue, 20 Nov 2012 23:10:27 +0000 Subject: Choosing Source Address to Bind Socket to in IMAP Client In-Reply-To: References: <72731eb3-1521-46f0-8601-8a2df58849d8@googlegroups.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF474167BBD73@SCACMX008.exchad.jpmchase.net> brintoul at controlledthinking.com wrote: > > On Tuesday, November 20, 2012 2:41:58 PM UTC-8, Prasad, Ramit wrote: > > brintoul at controlledthinking.com wrote: > > > > Apologies, I misread your question. > > > > According to the imaplib docs, you can subclass IMAP4 and override > > `IMAP4.open` to create the socket and bind it to the desired interface. > > You could also connect using IMAP4 and then close/reopen the socket > > inside the IMAP4 connection object. > > > > I hope that is more help, > > Ramit > > > > Thanks, yes, that helps! Just out of curiosity, can you give me a quick link to the docs you are looking at? > I'm not seeing anything which gets very specific in the docs for Python 2.7 at docs.python.org ... It is not very specific or helpful but here is the text in question. ''' IMAP4.open(host, port) Opens socket to port at host. This method is implicitly called by the IMAP4 constructor. The connection objects established by this method will be used in the read, readline, send, and shutdown methods. You may override this method. ''' http://docs.python.org/2.7/library/imaplib.html#imaplib.IMAP4.open ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From borkintom at gmail.com Tue Nov 20 18:41:42 2012 From: borkintom at gmail.com (Tom Borkin) Date: Tue, 20 Nov 2012 19:41:42 -0400 Subject: Problem with subprocess.call and windows schtasks In-Reply-To: References: Message-ID: Using shlex, I now have this: #!\Python27\python import os, subprocess path = os.path.join("C:\\", "Program Files", "Apache Group", "Apache2", "htdocs", "ccc", "run_alert.py") #subprocess.call(['SchTasks', '/Create', '/SC', 'ONCE', '/TN', '"test"', '/TR', path, '/ST', '23:50']) subprocess.call(['SchTasks', '/Create', '/SC', 'ONCE', '/TN', '"test"', '/TR', 'run_alert.py', '/ST', '23:50']) Both of the above commands throw the same error: ERROR: The filename, directory name or volume label syntax is incorrect. Pls advise. TIA, Tom On Sun, Nov 18, 2012 at 12:39 PM, Chris Rebert wrote: > On Sun, Nov 18, 2012 at 5:48 AM, Tom Borkin wrote: > > Hi, > > I have this code: > > > > #!\Python27\python > > > > import subprocess > > #subprocess.call(['SchTasks /Create /SC ONCE /TN "My Tasks" /TR > "C:/Program > > Files/Apache Group/Apache2/htdocs/ccc/run_alert.py" /ST 07:50'], > shell=True) > > subprocess.call(['SchTasks /Create /SC ONCE /TN "test" /TR "run_alert.py" > > /ST 07:50'], shell=True) > > With either call, I get this error: > > C:\Program Files\Apache Group\Apache2\htdocs\ccc>cron_alert_activity.py > > The system cannot find the path specified. > > > > If I remove the ", shell=True" I get this: > > C:\Program Files\Apache Group\Apache2\htdocs\ccc>cron_alert_activity.py > > C:\Program Files\Apache Group\Apache2\htdocs\ccc\cron_alert_activity.py, > > line 4, in > > subprocess.call(['SchTasks /Create /SC ONCE /TN "test" /TR > "run_alert.py" > > /ST 07:50']) > > File "C:\Python27\lib\subprocess.py", line 493, in call > > return Popen(*popenargs, **kwargs).wait() > > File "C:\Python27\lib\subprocess.py", line 679, in __init__ errread, > > errwrite) > > File "C:\Python27\lib\subprocess.py", line 896, in _execute_child > > startupinfo) > > WindowsError: [Error 2] The system cannot find the file specified > > The file exists in said directory. I can execute it from the cmd prompt. > > Per the docs ( > http://docs.python.org/2/library/subprocess.html#frequently-used-arguments > ): > "If passing a single string [as the `args` argument], either `shell` > must be True (see below) or else the string must simply name the > program to be executed **without specifying any arguments.**" > (emphasis mine) > > > So I tried this: > > pgm = "SchTasks" > > args = ['/Create /SC ONCE /TN "test" /TR "run_alert.py" /ST 07:50'] > > #args = ['/Create', '/SC ONCE', '/TN "test"', '/TR "run_alert.py"', '/ST > > 07:50'] > > cmd = [pgm] > > cmd.extend(args) > > subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] > > but got this error: > > ERROR: Invalid argument/option - <> > > > > If I use the other args list I get this error: > > ERROR: Invalid argument/option - '/SC ONCE' > > so apparently it liked the first argument. > > > > Please advise. > > Your tokenization of your command is incorrect. Consult the Note box > in the docs regarding `args` tokenization, and apply it to your > command: > http://docs.python.org/2/library/subprocess.html#subprocess.Popen > > The-docs-are-your-friend-ly Yours, > Chris > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ameyer2 at yahoo.com Tue Nov 20 18:46:07 2012 From: ameyer2 at yahoo.com (Alan Meyer) Date: Tue, 20 Nov 2012 18:46:07 -0500 Subject: 10 sec poll - please reply! In-Reply-To: <7290a127-97e1-4032-98ae-6e602b87b74b@googlegroups.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <7290a127-97e1-4032-98ae-6e602b87b74b@googlegroups.com> Message-ID: <50AC163F.9070608@yahoo.com> On 11/20/2012 11:29 AM, mherrmann.at at gmail.com wrote: > ... generate_keystrokes? ... Not bad. "gen_keystrokes", or even "keystrokes" might also do. I suggest using a name that is unique enough that you can grep through piles of code and find where it's used. "type" fails that test. "generate_keystrokes" passes with flying colors, but may be overkill. Alan From python.list at tim.thechases.com Tue Nov 20 19:00:59 2012 From: python.list at tim.thechases.com (Tim Chase) Date: Tue, 20 Nov 2012 18:00:59 -0600 Subject: 10 sec poll - please reply! In-Reply-To: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> Message-ID: <50AC19BB.9080008@tim.thechases.com> On 11/20/12 06:18, Michael Herrmann wrote: > am having difficulty picking a name for the function that > simulates key strokes. I currently have it as 'type' but that > clashes with the built-in function. Just to add one more to the pot, Vim uses "feedkeys()" for a similar purpose. -tkc From joshua.landau.ws at gmail.com Tue Nov 20 19:09:45 2012 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 21 Nov 2012 00:09:45 +0000 Subject: Greedy parsing of argparse/positional arguments In-Reply-To: References: Message-ID: On 20 November 2012 10:02, Johannes Bauer wrote: > Hi list, > > I have a problem with Python3.2's argparse module. The following sample: > > parser = argparse.ArgumentParser(prog = sys.argv[0]) > parser.add_argument("-enc", metavar = "enc", nargs = "+", type = str, > default = [ "utf-8" ]) > parser.add_argument("pattern", metavar = "pattern", type = str, nargs = 1) > parser.add_argument("filename", metavar = "filename", type = str, nargs = > 1) > args = parser.parse_args(sys.argv[1:]) > > illustrates the problem: I want to be able to specify an encoding one or > more times (multiple encodings possible), have a pattern and a filename > as the last two arguments. > > This works as long as I don't specify '-enc' on the command line. If I > do, for example > > ./foo -enc myencoding mypattern myfile > > The "-enc" greedy parser seems to capture ["myencoding", "mypattern", > "myfile"], leaving nothing for "pattern" and "filename", yielding an error: > > ./foo: error: too few arguments > > How can I force positional arguments to take precedence over optional > arguments? I could exclude them from the parsing altogether, but that > would make them not appear in the help page (which I'd like to avoid). My first suggestion would be to change "-enc" to "--enc", my second to make the input "--enc FIRST --enc SECOND --enc THIRD... pattern filename" (action="append", remove nargs="+"), and my third to use docopt (docopt.org) where the example you have posted is just: ------------- """My Program. Usage: my_prog.py [--enc=...] Options: --enc [default: UTF-8] """ from docopt import docopt arguments = docopt(__doc__) --------------* Note that this will not work if you don't take my first two suggestions. An alternative is like mplayer's, where it accepts comma-delimited lists: my_prog.py --enc UTF-8,ASCII,FOO,BAR "pattern%" filename.txt Where you will parse the comma-delimited list afterwards. This is only worth it if you expect a lot of encodings. Q: How about actually answering the question? A: I don't know how, short of parsing it manually. * Small differences exist -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Tue Nov 20 20:17:26 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 21 Nov 2012 01:17:26 GMT Subject: 10 sec poll - please reply! References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> Message-ID: <50ac2ba6$0$29987$c3e8da3$5496439d@news.astraweb.com> On Tue, 20 Nov 2012 18:00:59 -0600, Tim Chase wrote: > On 11/20/12 06:18, Michael Herrmann wrote: >> am having difficulty picking a name for the function that simulates key >> strokes. I currently have it as 'type' but that clashes with the >> built-in function. > > Just to add one more to the pot, Vim uses "feedkeys()" for a similar > purpose. What does it feed to the keys? Hypercard and other XTalk languages use "type" to simulate typing. -- Steven From steve+comp.lang.python at pearwood.info Tue Nov 20 20:20:15 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 21 Nov 2012 01:20:15 GMT Subject: 10 sec poll - please reply! References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <7290a127-97e1-4032-98ae-6e602b87b74b@googlegroups.com> <50abadf3$0$29987$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50ac2c4f$0$29987$c3e8da3$5496439d@news.astraweb.com> On Tue, 20 Nov 2012 21:08:24 +0000, Prasad, Ramit wrote: >> I believe that your initial instinct for the name of this function was >> correct. It automates typing, so you should call it "type" or (for >> those paranoid about shadowing the built-in, "type_str". >> >> > I can too easily see somebody doing from module import * OR from module > import type. Yes. So what? If they do, then either they intended to do it, or they will soon learn not to. *Accidental* shadowing of names is a bad thing, because you get unexpected bugs. *Deliberate* shadowing is not. We're all consenting adults here, if somebody calls "from module import type", and shadows the builtin type, that's their right to shoot themselves in the foot. Or not, as the case may be. -- Steven From d at davea.name Tue Nov 20 20:28:24 2012 From: d at davea.name (Dave Angel) Date: Tue, 20 Nov 2012 20:28:24 -0500 Subject: Problem with subprocess.call and windows schtasks In-Reply-To: References: Message-ID: <50AC2E38.8050303@davea.name> On 11/20/2012 06:41 PM, Tom Borkin wrote: (Please don't top-post. Now we lose all the context) > Using shlex, I now have this: > #!\Python27\python > import os, subprocess > path = os.path.join("C:\\", "Program Files", "Apache Group", "Apache2", > "htdocs", "ccc", "run_alert.py") > #subprocess.call(['SchTasks', '/Create', '/SC', 'ONCE', '/TN', '"test"', > '/TR', path, '/ST', '23:50']) > subprocess.call(['SchTasks', '/Create', '/SC', 'ONCE', '/TN', '"test"', > '/TR', 'run_alert.py', '/ST', '23:50']) > Both of the above commands throw the same error: > ERROR: The filename, directory name or volume label syntax is incorrect. I don't use Windows, but doesn't a Windows program usually have an .exe extension? So why would you expect it to find SchTasks ? Adding extensions is a shell feature, and you're not using the shell. Also, you should take a look at the value "path". On Linux, it shows up as: C:\\/Program Files/Apache Group/Apache2/htdocs/ccc/run_alert.py It'll be different under Windows, but probably still wrong. -- DaveA From python.list at tim.thechases.com Tue Nov 20 20:30:37 2012 From: python.list at tim.thechases.com (Tim Chase) Date: Tue, 20 Nov 2012 19:30:37 -0600 Subject: 10 sec poll - please reply! In-Reply-To: <50ac2ba6$0$29987$c3e8da3$5496439d@news.astraweb.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <50ac2ba6$0$29987$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50AC2EBD.7060508@tim.thechases.com> On 11/20/12 19:17, Steven D'Aprano wrote: > On Tue, 20 Nov 2012 18:00:59 -0600, Tim Chase wrote: >> Just to add one more to the pot, Vim uses "feedkeys()" for a similar >> purpose. > > What does it feed to the keys? In Vim's case, the signature would be something like def feedkeys(str, mode='m'): ... where the 'mode' parameter specifies whether keystrokes should be passed through the key-remapping functionality, and whether they should be treated as typed or as a mapping-unit (which affects undo behavior, whether each key is undoable, or if it's undone as a chunk) Vim has the advantage that feedkeys() only has to deal with the keys that Vim sees, which doesn't distinguish keydown/keyup events, or the presses of modifier keys. > Hypercard and other XTalk languages use "type" to simulate typing. However, I suspect that they don't _also_ have a type() function to dynamically determine the class of an object (or, if they do, it's called something other than type() :-) -tkc From jaorozco at estudiantes.uci.cu Tue Nov 20 20:32:40 2012 From: jaorozco at estudiantes.uci.cu (Jorge Alberto Diaz Orozco) Date: Tue, 20 Nov 2012 20:32:40 -0500 Subject: proxy?? In-Reply-To: References: <50AB9833.6020409@estudiantes.uci.cu> Message-ID: <50AC2F38.4010401@estudiantes.uci.cu> When I try to open facebook or search something using google it is just not working. gives me forbidden or not found errors. that's why I think the problem is about the headers of the request. On 20/11/12 15:29, Joshua Landau wrote: > On 20 November 2012 14:48, Jorge Alberto Diaz Orozco > > wrote: > > Hi there. > Does anyone knows how to manage headers using a simple proxy??? > I'm doing this but It gives me problems In some pages. > > > I don't know the answer, but I do know you'd get more favour if you > explained what"problems" and "In some pages" refer to. > > import SocketServer > import SimpleHTTPServer > import urllib2 > > PORT = 8888 > > class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler): > def do_GET(self): > try: > print self.path > self.copyfile(urllib2.urlopen(self.path), self.wfile) > except: > print 'error',self.path > > httpd = SocketServer.ForkingTCPServer(('127.0.0.1', PORT), Proxy) > print "serving at port", PORT > httpd.serve_forever() > 10mo. ANIVERSARIO DE LA CREACION DE LA UNIVERSIDAD DE LAS CIENCIAS INFORMATICAS... CONECTADOS AL FUTURO, CONECTADOS A LA REVOLUCION http://www.uci.cu http://www.facebook.com/universidad.uci http://www.flickr.com/photos/universidad_uci -------------- next part -------------- An HTML attachment was scrubbed... URL: From python.list at tim.thechases.com Tue Nov 20 20:39:02 2012 From: python.list at tim.thechases.com (Tim Chase) Date: Tue, 20 Nov 2012 19:39:02 -0600 Subject: 10 sec poll - please reply! In-Reply-To: <50ac2c4f$0$29987$c3e8da3$5496439d@news.astraweb.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <7290a127-97e1-4032-98ae-6e602b87b74b@googlegroups.com> <50abadf3$0$29987$c3e8da3$5496439d@news.astraweb.com> <50ac2c4f$0$29987$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50AC30B6.9030708@tim.thechases.com> On 11/20/12 19:20, Steven D'Aprano wrote: > *Accidental* shadowing of names is a bad thing, because you get > unexpected bugs. *Deliberate* shadowing is not. We're all > consenting adults here, if somebody calls "from module import > type", and shadows the builtin type, that's their right to shoot > themselves in the foot. Or not, as the case may be. Python even allows you to unshoot your foot by doing from module import type as unshadowed_type So others can have your poorly-named cake and eat it too. Or some such confuddling of aphorisms. -tkc From steve+comp.lang.python at pearwood.info Tue Nov 20 20:43:57 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 21 Nov 2012 01:43:57 GMT Subject: Web Frameworks Excessive Complexity References: Message-ID: <50ac31dd$0$29987$c3e8da3$5496439d@news.astraweb.com> On Tue, 20 Nov 2012 20:07:54 +0000, Robert Kern wrote: > The source of bugs is not excessive complexity in a method, just > excessive lines of code. Taken literally, that cannot possibly the case. def method(self, a, b, c): do_this(a) do_that(b) do_something_else(c) def method(self, a, b, c): do_this(a); do_that(b); do_something_else(c) It *simply isn't credible* that version 1 is statistically likely to have twice as many bugs as version 2. Over-reliance on LOC is easily gamed, especially in semicolon languages. Besides, I think you have the cause and effect backwards. I would rather say: The source of bugs is not lines of code in a method, but excessive complexity. It merely happens that counting complexity is hard, counting lines of code is easy, and the two are strongly correlated, so why count complexity when you can just count lines of code? Keep in mind that something like 70-80% of published scientific papers are never replicated, or cannot be replicated. Just because one paper concludes that LOC alone is a better metric than CC doesn't necessary make it so. But even if we assume that the paper is valid, it is important to understand just what it says, and not extrapolate too far. The paper makes various assumptions, takes statistical samples, and uses models. (Which of course *any* such study must.) I'm not able to comment on whether those models and assumptions are valid, but assuming that they are, the conclusion of the paper is no stronger than the models and assumptions. We should not really conclude that "CC has no more predictive power than LOC". The right conclusion is that one specific model of cyclic complexity, McCabe's CC, has no more predictive power than LOC for projects written in C, C++ and Java. How does that apply to Python code? Well, it's certainly suggestive, but it isn't definitive. It's also important to note that the authors point out that in their samples of code, they found very high variance and large numbers of outliers: [quote] Modules where LOC does not predict CC (or vice-versa) may indicate an overly-complex module with a high density of decision points or an overly- simple module that may need to be refactored. [end quote] So *even by the terms of this paper*, it isn't true that CC has no predictive value over LOC -- if the CC is radically high or low for the LOC, that is valuable to know. > LoC is much simpler, easier to understand, and > easier to correct than CC. Well, sure, but do you really think Perl one-liners are the paragon of bug-free code we ought to be aiming for? *wink* -- Steven From driscoll at cs.wisc.edu Tue Nov 20 20:51:08 2012 From: driscoll at cs.wisc.edu (Evan Driscoll) Date: Tue, 20 Nov 2012 19:51:08 -0600 Subject: 10 sec poll - please reply! In-Reply-To: <50AC163F.9070608@yahoo.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <7290a127-97e1-4032-98ae-6e602b87b74b@googlegroups.com> <50AC163F.9070608@yahoo.com> Message-ID: <50AC338C.6020207@cs.wisc.edu> On 11/20/2012 05:46 PM, Alan Meyer wrote: > On 11/20/2012 11:29 AM, mherrmann.at at gmail.com wrote: > > > ... generate_keystrokes? ... > > Not bad. "gen_keystrokes", or even "keystrokes" might also do. I would emphatically vote "no" for "keystrokes". That's a noun, not a verb. What does it do? Tell you if its parameters are keystrokes? (gen_keystrokes is fine, though personally I'd probably stick with generate_keystrokes of the two.) Evan From alan at scooby-doo.csail.mit.edu Tue Nov 20 21:15:08 2012 From: alan at scooby-doo.csail.mit.edu (Alan Bawden) Date: Tue, 20 Nov 2012 21:15:08 -0500 Subject: 10 sec poll - please reply! References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <7290a127-97e1-4032-98ae-6e602b87b74b@googlegroups.com> Message-ID: Since the event being generated is commonly called a "keystroke", and since my dictionary defines the noun "stroke" as being: "the act of striking", a good verb to choose for the action itself would seem to be "strike": strike('a') From frednotbob at hotmail.ca Tue Nov 20 21:18:17 2012 From: frednotbob at hotmail.ca (frednotbob at hotmail.ca) Date: Tue, 20 Nov 2012 18:18:17 -0800 (PST) Subject: Getting a seeded value from a list In-Reply-To: References: Message-ID: <993b9303-dbca-43f1-9ff4-9677278776b1@googlegroups.com> > >The former can be generated from the seed each >time you enter the level; the latter must be generated the first time then >stored. > Random.random() is already populating the levelSeed list; I've set it as part of new_game() so that any time the player begins a new game, the levelSeed list is replenished with a new set of data. The problem, in a nutshell, is this: When the player starts a new game, make_map() randomly generates level 'Foo' as the player's starting floor. Floor 'Bar' is similarly generated as the player descends from 'Foo' to 'Bar. Each time the player goes from 'Foo' to 'Bar' (or vice versa), make_map randomly generates a new layout for either level. What I'd like to do is select values from 'levelSeed' and assign them to the levels that make_map generates so that the player goes back to the same 'Foo' and 'Bar' each time (at least until the player repopulates levelSeed with new values by whatever method I eventually decide to use). I simply can't find any relevant examples online; everything I've found so far is either intended for a different purpose or assumes that the reader is already an experienced programmer. From frednotbob at hotmail.ca Tue Nov 20 21:18:17 2012 From: frednotbob at hotmail.ca (frednotbob at hotmail.ca) Date: Tue, 20 Nov 2012 18:18:17 -0800 (PST) Subject: Getting a seeded value from a list In-Reply-To: References: Message-ID: <993b9303-dbca-43f1-9ff4-9677278776b1@googlegroups.com> > >The former can be generated from the seed each >time you enter the level; the latter must be generated the first time then >stored. > Random.random() is already populating the levelSeed list; I've set it as part of new_game() so that any time the player begins a new game, the levelSeed list is replenished with a new set of data. The problem, in a nutshell, is this: When the player starts a new game, make_map() randomly generates level 'Foo' as the player's starting floor. Floor 'Bar' is similarly generated as the player descends from 'Foo' to 'Bar. Each time the player goes from 'Foo' to 'Bar' (or vice versa), make_map randomly generates a new layout for either level. What I'd like to do is select values from 'levelSeed' and assign them to the levels that make_map generates so that the player goes back to the same 'Foo' and 'Bar' each time (at least until the player repopulates levelSeed with new values by whatever method I eventually decide to use). I simply can't find any relevant examples online; everything I've found so far is either intended for a different purpose or assumes that the reader is already an experienced programmer. From sparx10 at gmail.com Tue Nov 20 22:35:00 2012 From: sparx10 at gmail.com (sparx10 at gmail.com) Date: Tue, 20 Nov 2012 19:35:00 -0800 (PST) Subject: moving object along circle Message-ID: <82ad5977-620a-4dc7-bb16-0e80bd6427bf@googlegroups.com> I'm trying to move an object along a circle (orbit), and I did come up with this: radius = 100 from math import sqrt for x in range(-radius,radius): y = sqrt(radius**2-x**2) print(x, y) however it moves faster at the beginning and end of the range (y value changes faster than x value) because the x value is changing at a constant rate but the y value isn't. I can't think of a way to get something to move smoothly around in a circle though.. From steve+comp.lang.python at pearwood.info Tue Nov 20 22:37:09 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 21 Nov 2012 03:37:09 GMT Subject: Getting a seeded value from a list References: Message-ID: <50ac4c65$0$21790$c3e8da3$76491128@news.astraweb.com> On Tue, 20 Nov 2012 18:18:17 -0800, frednotbob wrote: > The problem, in a nutshell, is this: > > When the player starts a new game, make_map() randomly generates level > 'Foo' as the player's starting floor. Floor 'Bar' is similarly > generated as the player descends from 'Foo' to 'Bar. > > Each time the player goes from 'Foo' to 'Bar' (or vice versa), make_map > randomly generates a new layout for either level. > > What I'd like to do is select values from 'levelSeed' and assign them to > the levels that make_map generates so that the player goes back to the > same 'Foo' and 'Bar' each time (at least until the player repopulates > levelSeed with new values by whatever method I eventually decide to > use). Just store a mapping between levels and seeds: levels = {} # starts out blank Now, each time you enter a level, see if there is a seed recorded, and if so, use that. Here I assume each level has a name, and you look up by name. name = "Lock And Load" if name in levels: seed = levels[name] else: # Never seen this level before. seed = pick_a_new_seed_somehow() levels[name] = seed map = make_map(name, seed) # whatever arguments needed redraw(map) -- Steven From rosuav at gmail.com Tue Nov 20 22:41:24 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 21 Nov 2012 14:41:24 +1100 Subject: Getting a seeded value from a list In-Reply-To: <993b9303-dbca-43f1-9ff4-9677278776b1@googlegroups.com> References: <993b9303-dbca-43f1-9ff4-9677278776b1@googlegroups.com> Message-ID: On Wed, Nov 21, 2012 at 1:18 PM, wrote: > Each time the player goes from 'Foo' to 'Bar' (or vice versa), make_map randomly generates a new layout for either level. > > What I'd like to do is select values from 'levelSeed' and assign them to the levels that make_map generates so that the player goes back to the same 'Foo' and 'Bar' each time (at least until the player repopulates levelSeed with new values by whatever method I eventually decide to use). The easiest way would be to retain a mapping of seeds, eg: seeds = {'Foo': 142857, 'Bar': 271828} And then you reseed the random number generator with seeds[level] each time. (If your levels are identified by consecutive integers starting from 0 or 1, you could use a list instead of a dictionary, but the same applies.) However, this still means that the player will see the exact same level regenerated every time, absolutely fresh. As previously stated in this thread, that's not usually a good thing for encounters, treasure, etc. Once some nasty critter has been killed, he should STAY killed! :) ChrisA From steve+comp.lang.python at pearwood.info Tue Nov 20 22:46:39 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 21 Nov 2012 03:46:39 GMT Subject: moving object along circle References: <82ad5977-620a-4dc7-bb16-0e80bd6427bf@googlegroups.com> Message-ID: <50ac4e9f$0$21790$c3e8da3$76491128@news.astraweb.com> On Tue, 20 Nov 2012 19:35:00 -0800, sparx10 wrote: > I'm trying to move an object along a circle (orbit), and I did come up > with this: > > radius = 100 > from math import sqrt > for x in range(-radius,radius): > y = sqrt(radius**2-x**2) > print(x, y) > > however it moves faster at the beginning and end of the range (y value > changes faster than x value) because the x value is changing at a > constant rate but the y value isn't. I can't think of a way to get > something to move smoothly around in a circle though.. Instead of using rectangular (x, y) coordinates directly, use polar coordinates (r, ?) where r (radius) is the constant radius of your circle, and ? (theta) smoothly varies between 0 and 360?. http://www.teacherschoice.com.au/maths_library/coordinates/polar_-_rectangular_conversion.htm import math radius = 100 for angle in range(0, 361): theta = math.radians(angle) x = radius*math.cos(theta) y = radius*math.sin(theta) print(x, y) -- Steven From steve+comp.lang.python at pearwood.info Tue Nov 20 22:47:52 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 21 Nov 2012 03:47:52 GMT Subject: Getting a seeded value from a list References: <993b9303-dbca-43f1-9ff4-9677278776b1@googlegroups.com> Message-ID: <50ac4ee7$0$21790$c3e8da3$76491128@news.astraweb.com> On Wed, 21 Nov 2012 14:41:24 +1100, Chris Angelico wrote: > However, this still means that the player will see the exact same level > regenerated every time, absolutely fresh. As previously stated in this > thread, that's not usually a good thing for encounters, treasure, etc. > Once some nasty critter has been killed, he should STAY killed! :) Why? That isn't true in real life, why should it be true for games? -- Steven From rosuav at gmail.com Tue Nov 20 23:00:26 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 21 Nov 2012 15:00:26 +1100 Subject: Getting a seeded value from a list In-Reply-To: <50ac4ee7$0$21790$c3e8da3$76491128@news.astraweb.com> References: <993b9303-dbca-43f1-9ff4-9677278776b1@googlegroups.com> <50ac4ee7$0$21790$c3e8da3$76491128@news.astraweb.com> Message-ID: On Wed, Nov 21, 2012 at 2:47 PM, Steven D'Aprano wrote: > On Wed, 21 Nov 2012 14:41:24 +1100, Chris Angelico wrote: > >> However, this still means that the player will see the exact same level >> regenerated every time, absolutely fresh. As previously stated in this >> thread, that's not usually a good thing for encounters, treasure, etc. >> Once some nasty critter has been killed, he should STAY killed! :) > > Why? That isn't true in real life, why should it be true for games? It's either Rule of Fun or Rule of Funny, and I don't really care which... ChrisA [1] http://tvtropes.org/pmwiki/pmwiki.php/Main/RuleOfFun [2] http://tvtropes.org/pmwiki/pmwiki.php/Main/RuleOfFunny From rosuav at gmail.com Wed Nov 21 01:35:27 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 21 Nov 2012 17:35:27 +1100 Subject: 10 sec poll - please reply! In-Reply-To: <20121120212544.8492f9d565883b2d8fb663c1@lavabit.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <20121120212544.8492f9d565883b2d8fb663c1@lavabit.com> Message-ID: On Wed, Nov 21, 2012 at 4:25 PM, rh wrote: > To counter your question. 10 sec. poll for the name for the law that > states that people will discuss/argue the most over those things > having little or no significance. You mean bikeshedding? Parkinson's Law of something-or-other... not the Law of Inverse Relevance, though the two do discuss the same sorts of things. And yet, trivial though it may seem, function naming in a permanent API is pretty important. Threads like this can be the difference between coherent and useful APIs and veritable piles of excrement. ChrisA From steve+comp.lang.python at pearwood.info Wed Nov 21 02:01:25 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 21 Nov 2012 07:01:25 GMT Subject: 10 sec poll - please reply! References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <20121120212544.8492f9d565883b2d8fb663c1@lavabit.com> Message-ID: <50ac7c45$0$21790$c3e8da3$76491128@news.astraweb.com> On Wed, 21 Nov 2012 17:35:27 +1100, Chris Angelico wrote: > And yet, trivial though it may seem, function naming in a permanent API > is pretty important. Threads like this can be the difference between > coherent and useful APIs and veritable piles of excrement. "There are only two hard problems in Computer Science: cache invalidation and naming things." -- Phil Karlton -- Steven From kevin.p.dwyer at gmail.com Wed Nov 21 02:32:25 2012 From: kevin.p.dwyer at gmail.com (Kev Dwyer) Date: Wed, 21 Nov 2012 07:32:25 +0000 Subject: Suitable software stacks for simple python web service Message-ID: Hello List, I have to build a simple web service which will: - receive queries from our other servers - forward the requests to a third party SOAP service - process the response from the third party - send the result back to the original requester >From the point of view of the requester, this will happen within the scope of a single request. The data exchanged with the original requester will likely be encoded as JSON; the SOAP service will be handled by SUDS. The load is likely to be quite light, say a few requests per hour, though this may increase in the future. Given these requirements, what do you think might be a suitable software stack, i.e. webserver and web framework (if a web framework is even necessary)? Candidates should be compatible with Python2.7, though I'd be happy to consider Python 3 if anyone knows of a Python3 SOAP library that has good WSDL support. Cheers, Kev From ashish.makani at gmail.com Wed Nov 21 02:43:29 2012 From: ashish.makani at gmail.com (ashish) Date: Tue, 20 Nov 2012 23:43:29 -0800 (PST) Subject: Creating a torrent file & associated tracker through a django web app Message-ID: <7433e9d7-eb1f-4333-b172-ed4200d60713@googlegroups.com> Hi c.l.p folks Following is a description of what i am trying to achieve : The user should log into a django web app, select a file & the web app should generate a .torrent file & a private tracker(http://IP_ADDRESS:PORT_NUMBER/announce) for that .torrent file. Basically, i want to programmatically create a .torrent file, on the server side. I found two libraries mktorrent[1] & py3createtorrent [2], but i need help with how to create a torrent from a django web app. Any suggestions for django modules or libraries which can do this ? Any suggestions/pointers/ideas/links will be greatly appreciated Thanks a ton, cheers, ashish 1. http://mktorrent.sourceforge.net/ 2. http://www.robertnitsch.de/projects/py3createtorrent From gelonida at gmail.com Wed Nov 21 03:23:00 2012 From: gelonida at gmail.com (Gelonida N) Date: Wed, 21 Nov 2012 09:23:00 +0100 Subject: windows question: default click action points to wrong python version Message-ID: Hi, I installed python 2.6 and python 2.7 on a windows 7 machine. At the moment Python 2.7 is the interpreter being used if I 'start' a python script without explicit interpreter. I always thought, that 'repairing' Python 2.6 (reinstalling it) would set the default settings back to Python 2.6. I also see with assoc / ftypes, that python 2.6. has now been configured as default. However when I click on a script it is still started with 2.7. (even after a full restart of the machine) This is really surprising to me. I thought ftype is the command to change file associations. What am I missing? From ulrich.eckhardt at dominolaser.com Wed Nov 21 03:33:09 2012 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Wed, 21 Nov 2012 09:33:09 +0100 Subject: Web Frameworks Excessive Complexity In-Reply-To: <50ac31dd$0$29987$c3e8da3$5496439d@news.astraweb.com> References: <50ac31dd$0$29987$c3e8da3$5496439d@news.astraweb.com> Message-ID: <67gun9-kk8.ln1@satorlaser.homedns.org> Am 21.11.2012 02:43, schrieb Steven D'Aprano: > On Tue, 20 Nov 2012 20:07:54 +0000, Robert Kern wrote: >> The source of bugs is not excessive complexity in a method, just >> excessive lines of code. > > Taken literally, that cannot possibly the case. > > def method(self, a, b, c): > do_this(a) > do_that(b) > do_something_else(c) > > > def method(self, a, b, c): > do_this(a); do_that(b); do_something_else(c) > > > It *simply isn't credible* that version 1 is statistically likely to have > twice as many bugs as version 2. Over-reliance on LOC is easily gamed, > especially in semicolon languages. "Don't indent deeper than 4 levels!" "OK, not indenting at all, $LANG doesn't need it anyway." Sorry, but if code isn't even structured halfway reasonably it is unmaintainable, regardless of what CC or LOC say. > Besides, I think you have the cause and effect backwards. I would rather > say: > > The source of bugs is not lines of code in a method, but excessive > complexity. It merely happens that counting complexity is hard, counting > lines of code is easy, and the two are strongly correlated, so why count > complexity when you can just count lines of code? I agree here, and I'd go even further: Measuring complexity is not just hard, it requires a metric that you need to agree on first. With LOC you only need to agree on not semicolon-chaining lines and how to treat comments and empty lines. With CC, you effectively agree that an if statement has complexity of one (or 2?) while a switch statement has a complexity according to its number of cases, while it is way easier to read and comprehend than a similar number produced by if statement. Also, CC doesn't even consider new-fashioned stuff like exceptions that introduce yet another control flow path. >> LoC is much simpler, easier to understand, and >> easier to correct than CC. > > Well, sure, but do you really think Perl one-liners are the paragon of > bug-free code we ought to be aiming for? *wink* Hehehe... ;) Uli From markus.moldaschl at gmail.com Wed Nov 21 04:29:39 2012 From: markus.moldaschl at gmail.com (markus.moldaschl at gmail.com) Date: Wed, 21 Nov 2012 01:29:39 -0800 (PST) Subject: 10 sec poll - please reply! In-Reply-To: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> Message-ID: Am Dienstag, 20. November 2012 13:18:38 UTC+1 schrieb Michael Herrmann: > Hi, > > > > I'm developing a GUI Automation library (http://www.getautoma.com) and am having difficulty picking a name for the function that simulates key strokes. I currently have it as 'type' but that clashes with the built-in function. Example uses of 'type': > > > > type(ENTER) > > > > type("Hello World!") > > > > type(CTRL + 'a') > > > > What, in your view, would be the most intuitive alternative name? > > > > Here are my thoughts so far: I could call it 'press' but then our GUI automation tool also allows you to click things and then "press" might be mistaken for "pressing a button". A less ambiguous alternative is "type_keys" but that is rather long and doesn't read as well, for instance in type_keys(ENTER). > > > > Thank you very much! I would expect 'type' or 'press'. 'type' is maybe not the best solution because of overloading. I like 'press'. Good luck, Mr. Michael ;-) From mail at timgolden.me.uk Wed Nov 21 04:34:40 2012 From: mail at timgolden.me.uk (Tim Golden) Date: Wed, 21 Nov 2012 09:34:40 +0000 Subject: windows question: default click action points to wrong python version In-Reply-To: References: Message-ID: <50ACA030.6090509@timgolden.me.uk> On 21/11/2012 08:23, Gelonida N wrote: > Hi, > > I installed python 2.6 and python 2.7 on a windows 7 machine. > > At the moment Python 2.7 is the interpreter being used if I 'start' a > python script without explicit interpreter. > > I always thought, that 'repairing' Python 2.6 (reinstalling it) would > set the default settings back to Python 2.6. > > I also see with assoc / ftypes, that python 2.6. has now been configured > as default. > > However when I click on a script it is still started with 2.7. > (even after a full restart of the machine) > > This is really surprising to me. > I thought ftype is the command to change file associations. This area is a bit messy. There is a difference between: going to the command line and typing "myscript.py"; and double-clicking on a file in Explorer. The former uses the result of merging the assoc/ftype registry keys: HKEY_CURRENT_USER\Software\Classes\Python.File\shell\open\command HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Python.File\shell\open\command while the latter uses the Explorer registry keys at: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.py\UserChoice Initially, I suppose, the two are in sync. But presumably they can get out of sync, especially if you move backwards and forwards between associations. I haven't bothered fishing around in the Shell API but I presume that you can reconcile the two -- or just edit the registry, obviously. TJG From mail at timgolden.me.uk Wed Nov 21 05:01:33 2012 From: mail at timgolden.me.uk (Tim Golden) Date: Wed, 21 Nov 2012 10:01:33 +0000 Subject: Problem with subprocess.call and windows schtasks In-Reply-To: References: Message-ID: <50ACA67D.4080406@timgolden.me.uk> On 20/11/2012 23:41, Tom Borkin wrote: > Using shlex, I now have this: > #!\Python27\python > import os, subprocess > path = os.path.join("C:\\", "Program Files", "Apache Group", "Apache2", > "htdocs", "ccc", "run_alert.py") > #subprocess.call(['SchTasks', '/Create', '/SC', 'ONCE', '/TN', '"test"', > '/TR', path, '/ST', '23:50']) > subprocess.call(['SchTasks', '/Create', '/SC', 'ONCE', '/TN', '"test"', > '/TR', 'run_alert.py', '/ST', '23:50']) > Both of the above commands throw the same error: > ERROR: The filename, directory name or volume label syntax is incorrect. The following works for me: import subprocess path = r"C:\Program Files\Apache Group\Apache2\htdocs\ccc\run_alert.py" subprocess.call([ 'SchTasks', '/Create', '/SC', 'ONCE', '/TN', 'test', '/TR', path, '/ST', '23:50' ]) Things to note: * I haven't added extra quoting to any of the items in the command list which is subprocess.call's first parameter. The point about using the list (as opposed to passing an entire string which you can also do) is that the subprocess module can quote things as needed. If you've already added quotes, you'll get double-quoting which you almost certainly don't want. * (Obviously) I've formatted the subprocess.call as I have for clarity, especially via email. It's just a list. TJG From insideshoes at gmail.com Wed Nov 21 05:11:14 2012 From: insideshoes at gmail.com (inshu chauhan) Date: Wed, 21 Nov 2012 11:11:14 +0100 Subject: Index Error Message-ID: > > > > >> Yes i tried "or" also but no use ..... > > Explain "no use". If you mean you still fail, then what else did you > try? For example, did you try interchanging the two subscripts? I've > suspected all along that the meanings of row and column, x and y, [0] > and [1], height and width are possibly confused. Perhaps it'd be better > if you used a similar terminology everywhere. > I tried using "or" instead of "and" interchanging also, still the index error is coming. x is no of column(300) and y is no of rows(3000), [0] corresponds to x i.e. column and [1] corresponds to y i.e. row and lastly height is y i.e row and width is column i.e. x > > Or perhaps you should simply make a class called Point, with attributes > x and y. And use instances of that class rather than tuples. And start > the function by defining xmax and ymax, from data.height and > data.width (in whatever order matches the docs of that library you're > using) > Ok >>>>> should be added only when this condition is satisfied >>>>> print point >>>>> points.append(point) >>>>> change = True >>>>> print change >>>>> >>>>> >>>>> break >>>> >>>> Why do you want to terminate the loop after only iteration? >>>> > >>> > > The idea here is if no more point is added to the list of points i.e. all > > points surrounding the centre is zero.. I want to break the loop and go > to > > the next point.. > > > That's not at all what the break does. But it's probably not what you > meant to say anyway. > > > I think what you're saying is that you want to append at most one of the > points from the ring. In that case, the break is appropriate, but it'd > be much clearer if it were inside the clause that triggers it, the place > where you say points.append(point). (naturally, it'd be at the end of > that clause, following print change.) In other words indent it to line > up with print change. > Yes done that.. I totally agree.. > > Back to an earlier comment. I asked if N was ever bigger than x or > bigger than y, and you said never. But your ComputeClasses will have > such a case the very first time around, when cx==0, cy==0, and > ring_number == 1. > I doubt this , M confused.. > > Have you actually tested a trivial nested loop: > > for cy in xrange(0, data.height): > for cx in xrange(0, data.width): > point = data[cy, cx] > > to see whether it blows up. And if it does, whether reversing cy and cx > will change it? > Yes , I tested this. Its working fine, reversing cy and cx is not correct > > > Your comment in the line: > if dist < radius : and rings should be added > only when this condition is satisfied > > is confusing to me. How can you make the call to GenerateRing() after > this test, when this test is measuring something about one of the values > returned by GenerateRing ? > Actually , this is one of my condition.. but i think It means that when dist is greater than radius more points will not be added to the list , So the loop should stop here, and next centre should be taken, creating second list and so on.. > > I must confess I have no idea what data represents. When you're doing > rings, you use deltas on the cx and cy values. But when you're > computing radius, you use the 3d coordinates returned by data[cx, cy]. > So is data some kind of transformation, like a projection from a 3d > object into a plane ? > If I say data is a yml image file.. does it makes sense ? Nyways I am treating it as matrix with values. Yes you can say that it is a projection of 3D objects onto a 2D plane.. which I am finding hard to work with !!!!! -------------- next part -------------- An HTML attachment was scrubbed... URL: From insideshoes at gmail.com Wed Nov 21 05:15:56 2012 From: insideshoes at gmail.com (inshu chauhan) Date: Wed, 21 Nov 2012 11:15:56 +0100 Subject: Index Error Message-ID: > > > > def GenerateRing(x,y, N): Generates square rings around a point in data > which has 300 columns(x) and 3000 > > rows(y) > > indices = [] > > for i in xrange(-N, N): > > indices.append((x+i, y-N)) > > indices.append((x+N, y+i)) > > indices.append((x-i, y+N)) > > indices.append((x-N, y-i)) > > return indices > > No, this creates a one dimensional list with 2N elements of where each > element is a two item tuple. > > Yes, in programme it returns a list of tuples but pysically it is creating a ring . > > I need help in this part as I am > > unable to device a method in which if the points are out of index,it > should stop and > > if idx[0] >= 300 and idx[1] >= 3000: > go to next centre and start generating > > rings from there.. and again if the index is out of range .. this should > repeat > > continue > > else : > > point = data[idx[0], idx[1]] > > You can use a few different methods. This is just one example. > > for idx, temp_point in enumerate(new_indices): > try: > temp_point[0] > temp_point[1] > except Exception: #Should be IndexError I think. > print 'idx: {0}\ntemp_point:{1}'.format(idx, temp_point) > # Possibly add a break or exit so you do not have to > # keep going once you hit a failure. > point = data[temp_point[0], temp_point[1]] > > > Thank you for the suggestion. > What is `data`? I have not seen any built-in structure that takes > a tuple in this manner...unless it is a dictionary. Or from numpy. > Given my lack of knowledge of what `data`, it could be the > problem is there. That is one reason I accessed `temp_point[0]` and > `temp_point[1]` separately. > Data is an image. > > > > > > Traceback (most recent call last): > > File "Z:/modules/Classify.py", line 73, in > > ComputeClasses(data) > > File "Z:/modules/Classify.py", line 49, in ComputeClasses > > point = data[idx[0], idx[1]] > > error: index is out of range > > > > Is that the actual error? If so, then the problem is not `idx` or > `temp_point` but instead `data`. If it is not the exact error, please > copy and paste the error message *exactly* as given. > Sorry but this is the actual error . -------------- next part -------------- An HTML attachment was scrubbed... URL: From andriy.kornatskyy at live.com Wed Nov 21 06:02:26 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Wed, 21 Nov 2012 14:02:26 +0300 Subject: Web Frameworks Excessive Complexity In-Reply-To: References: , , , , , , , , , Message-ID: Robert, You would never get a better product by accident. The meaning of better product might differ from team to team but you can not ignore excessive complexity. Earlier or later you get back to that code and refactor it, thus?existence?of such fact was driven by your intention to make it a bit better (easier to understand, to support, to cover with unit tests, etc), with a team of 20 heads you can get even further: the whole team?adherence. So those drops make the overall picture better. This is what you, as a software developer, donate to what the final better product become. Thanks. Andriy ---------------------------------------- > To: python-list at python.org > From: robert.kern at gmail.com > Subject: Re: Web Frameworks Excessive Complexity > Date: Tue, 20 Nov 2012 20:33:46 +0000 > > On 20/11/2012 20:22, Andriy Kornatskyy wrote: > > > > Robert, > > > > I respect your point of view and it definitely make sense to me. I personally do not have a problem to understand CC but agree, method LoC is easier to understand. Regardless the path your choose in your next refactoring (based on method CC, LoC) it gives your better product. > > No, refactoring based on CC does not give you a better product, except by accident. > > -- > Robert Kern > > "I have come to believe that the whole world is an enigma, a harmless enigma > that is made terrible by our own mad attempt to interpret it as though it had > an underlying truth." > -- Umberto Eco > > -- > http://mail.python.org/mailman/listinfo/python-list From andriy.kornatskyy at live.com Wed Nov 21 06:09:38 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Wed, 21 Nov 2012 14:09:38 +0300 Subject: Web Frameworks Excessive Complexity In-Reply-To: <67gun9-kk8.ln1@satorlaser.homedns.org> References: , , , , <50ac31dd$0$29987$c3e8da3$5496439d@news.astraweb.com>, <67gun9-kk8.ln1@satorlaser.homedns.org> Message-ID: We choose Python for its readability. This is essential principal of language and thousands around reading the open source code. Things like PEP8, CC, LoC are all to serve you one purpose: bring your attention, teach you make your code better. Thanks. Andriy ---------------------------------------- > From: ulrich.eckhardt at dominolaser.com > Subject: Re: Web Frameworks Excessive Complexity > Date: Wed, 21 Nov 2012 09:33:09 +0100 > To: python-list at python.org > > Am 21.11.2012 02:43, schrieb Steven D'Aprano: > > On Tue, 20 Nov 2012 20:07:54 +0000, Robert Kern wrote: > >> The source of bugs is not excessive complexity in a method, just > >> excessive lines of code. > > > > Taken literally, that cannot possibly the case. > > > > def method(self, a, b, c): > > do_this(a) > > do_that(b) > > do_something_else(c) > > > > > > def method(self, a, b, c): > > do_this(a); do_that(b); do_something_else(c) > > > > > > It *simply isn't credible* that version 1 is statistically likely to have > > twice as many bugs as version 2. Over-reliance on LOC is easily gamed, > > especially in semicolon languages. > > "Don't indent deeper than 4 levels!" "OK, not indenting at all, $LANG > doesn't need it anyway." Sorry, but if code isn't even structured > halfway reasonably it is unmaintainable, regardless of what CC or LOC say. > > > > Besides, I think you have the cause and effect backwards. I would rather > > say: > > > > The source of bugs is not lines of code in a method, but excessive > > complexity. It merely happens that counting complexity is hard, counting > > lines of code is easy, and the two are strongly correlated, so why count > > complexity when you can just count lines of code? > > I agree here, and I'd go even further: Measuring complexity is not just > hard, it requires a metric that you need to agree on first. With LOC you > only need to agree on not semicolon-chaining lines and how to treat > comments and empty lines. With CC, you effectively agree that an if > statement has complexity of one (or 2?) while a switch statement has a > complexity according to its number of cases, while it is way easier to > read and comprehend than a similar number produced by if statement. > Also, CC doesn't even consider new-fashioned stuff like exceptions that > introduce yet another control flow path. > > > >> LoC is much simpler, easier to understand, and > >> easier to correct than CC. > > > > Well, sure, but do you really think Perl one-liners are the paragon of > > bug-free code we ought to be aiming for? *wink* > > Hehehe... ;) > > Uli > > > -- > http://mail.python.org/mailman/listinfo/python-list From rosuav at gmail.com Wed Nov 21 06:21:23 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 21 Nov 2012 22:21:23 +1100 Subject: Web Frameworks Excessive Complexity In-Reply-To: References: <50ac31dd$0$29987$c3e8da3$5496439d@news.astraweb.com> <67gun9-kk8.ln1@satorlaser.homedns.org> Message-ID: On Wed, Nov 21, 2012 at 10:09 PM, Andriy Kornatskyy wrote: > We choose Python for its readability. This is essential principal of language and thousands around reading the open source code. Things like PEP8, CC, LoC are all to serve you one purpose: bring your attention, teach you make your code better. But too much focus on metrics results in those metrics improving without any material benefit to the code. If there's a number that you can watch going up or down, nobody's going to want to be the one that pushes that number the wrong direction. So what happens when the right thing to do happens to conflict with the given metric? And yes, it WILL happen, guaranteed. No metric is perfect. Counting lines of code teaches you to make dense code. That's not a good thing nor a bad thing; you'll end up with list comprehensions rather than short loops, regardless of which is easier to actually read. Counting complexity by giving a score to every statement encourages code like this: def bletch(x,y): return x + {"foo":y*2,"bar":x*3+y,"quux":math.sin(y)}.get(mode,0) instead of: def bletch(x,y): if mode=="foo": return x+y*2 if mode=="bar": return x*4+y if mode=="quux": return x+math.sin(y) return x Okay, this is a stupid contrived example, but tell me which of those you'd rather work with, and then tell me a plausible metric that would agree with you. ChrisA From danielkleinad at gmail.com Wed Nov 21 06:24:01 2012 From: danielkleinad at gmail.com (danielk) Date: Wed, 21 Nov 2012 03:24:01 -0800 (PST) Subject: Encoding conundrum In-Reply-To: References: Message-ID: <6cce1b50-aa37-4077-89d5-34e57de2193e@googlegroups.com> On Tuesday, November 20, 2012 6:03:47 PM UTC-5, Ian wrote: > On Tue, Nov 20, 2012 at 2:49 PM, Daniel Klein wrote: > > > With the assistance of this group I am understanding unicode encoding issues > > > much better; especially when handling special characters that are outside of > > > the ASCII range. I've got my application working perfectly now :-) > > > > > > However, I am still confused as to why I can only use one specific encoding. > > > > > > I've done some research and it appears that I should be able to use any of > > > the following codecs with codepoints '\xfc' (chr(252)) '\xfd' (chr(253)) and > > > '\xfe' (chr(254)) : > > > > These refer to the characters with *Unicode* codepoints 252, 253, and 254: > > > > >>> unicodedata.name('\xfc') > > 'LATIN SMALL LETTER U WITH DIAERESIS' > > >>> unicodedata.name('\xfd') > > 'LATIN SMALL LETTER Y WITH ACUTE' > > >>> unicodedata.name('\xfe') > > 'LATIN SMALL LETTER THORN' > > > > > ISO-8859-1 [ note that I'm using this codec on my Linux box ] > > > > For ISO 8859-1, these characters happen to exist and even correspond > > to the same ordinals: 252, 253, and 254 (this is by design); so there > > is no problem encoding them, and the resulting bytes even happen to > > match the codepoints of the characters. > > > > > cp1252 > > > > cp1252 is designed after ISO 8859-1 and also has those same three characters: > > > > >>> for char in b'\xfc\xfd\xfe'.decode('cp1252'): > > ... print(unicodedata.name(char)) > > ... > > LATIN SMALL LETTER U WITH DIAERESIS > > LATIN SMALL LETTER Y WITH ACUTE > > LATIN SMALL LETTER THORN > > > > > latin1 > > > > Latin-1 is just another name for ISO 8859-1. > > > > > utf-8 > > > > UTF-8 is a *multi-byte* encoding. It can encode any Unicode > > characters, so you can represent those three characters in UTF-8, but > > with a different (and longer) byte sequence: > > > > >>> print('\xfc\xfd\xfd'.encode('utf8')) > > b'\xc3\xbc\xc3\xbd\xc3\xbd' > > > > > cp437 > > > > cp437 is another 8-bit encoding, but it maps entirely different > > characters to those three bytes: > > > > >>> for char in b'\xfc\xfd\xfe'.decode('cp437'): > > ... print(unicodedata.name(char)) > > ... > > SUPERSCRIPT LATIN SMALL LETTER N > > SUPERSCRIPT TWO > > BLACK SQUARE > > > > As it happens, the character at codepoint 252 (that's LATIN SMALL > > LETTER U WITH DIAERESIS) does exist in cp437. It maps to the byte > > 0x81: > > > > >>> '\xfc'.encode('cp437') > > b'\x81' > > > > The other two Unicode characters, at codepoints 253 and 254, do not > > exist at all in cp437 and cannot be encoded. > > > > > If I'm not mistaken, all of these codecs can handle the complete 8bit > > > character set. > > > > There is no "complete 8bit character set". cp1252, Latin1, and cp437 > > are all 8-bit character sets, but they're *different* 8-bit character > > sets with only partial overlap. > > > > > However, on Windows 7, I am only able to use 'cp437' to display (print) data > > > with those characters in Python. If I use any other encoding, Windows laughs > > > at me with this error message: > > > > > > File "C:\Python33\lib\encodings\cp437.py", line 19, in encode > > > return codecs.charmap_encode(input,self.errors,encoding_map)[0] > > > UnicodeEncodeError: 'charmap' codec can't encode character '\xfd' in > > > position 3: character maps to > > > > It would be helpful to see the code you're running that causes this error. I'm using subprocess.Popen to run a process that sends a list of codepoints to the calling Python program. The list is sent to stdout as a string. Here is a simple example that encodes the string "Dead^Parrot", where (for this example) I'm using '^' to represent chr(254) : encoded_string = '[68,101,97,100,254,80,97,114,114,111,116]' This in turn is handled in __repr__ with: return bytes((eval(encoded_string))).decode('cp437') I get the aforementioned 'error' if I use any other encoding. > > > > > Furthermore I get this from IDLE: > > > > > >>>> import locale > > >>>> locale.getdefaultlocale() > > > ('en_US', 'cp1252') > > > > > > I also get 'cp1252' when running the same script from a Windows command > > > prompt. > > > > > > So there is a contradiction between the error message and the default > > > encoding. > > > > If you're printing to stdout, it's going to use the encoding > > associated with stdout, which does not necessarily have anything to do > > with the default locale. Use this to determine what character set you > > need to be working in if you want your data to be printable: > > > > >>> import sys > > >>> sys.stdout.encoding > > 'cp437' > Hmmm. So THAT'S why I am only able to use 'cp437'. I had (mistakenly) thought that I could just indicate whatever encoding I wanted, as long as the codec supported it. > > > > Why am I restricted from using just that one codec? Is this a Windows or > > > Python restriction? Please enlighten me. > > > > In Linux, your terminal encoding is probably either UTF-8 or Latin-1, > > and either way it has no problems encoding that data for output. In a > > Windows cmd terminal, the default terminal encoding is cp437, which > > can't support two of the three characters you mentioned above. It may not be able to encode those two characters but it is able to decode them. That seems rather inconsistent (and contradictory) to me. From danielkleinad at gmail.com Wed Nov 21 06:24:01 2012 From: danielkleinad at gmail.com (danielk) Date: Wed, 21 Nov 2012 03:24:01 -0800 (PST) Subject: Encoding conundrum In-Reply-To: References: Message-ID: <6cce1b50-aa37-4077-89d5-34e57de2193e@googlegroups.com> On Tuesday, November 20, 2012 6:03:47 PM UTC-5, Ian wrote: > On Tue, Nov 20, 2012 at 2:49 PM, Daniel Klein wrote: > > > With the assistance of this group I am understanding unicode encoding issues > > > much better; especially when handling special characters that are outside of > > > the ASCII range. I've got my application working perfectly now :-) > > > > > > However, I am still confused as to why I can only use one specific encoding. > > > > > > I've done some research and it appears that I should be able to use any of > > > the following codecs with codepoints '\xfc' (chr(252)) '\xfd' (chr(253)) and > > > '\xfe' (chr(254)) : > > > > These refer to the characters with *Unicode* codepoints 252, 253, and 254: > > > > >>> unicodedata.name('\xfc') > > 'LATIN SMALL LETTER U WITH DIAERESIS' > > >>> unicodedata.name('\xfd') > > 'LATIN SMALL LETTER Y WITH ACUTE' > > >>> unicodedata.name('\xfe') > > 'LATIN SMALL LETTER THORN' > > > > > ISO-8859-1 [ note that I'm using this codec on my Linux box ] > > > > For ISO 8859-1, these characters happen to exist and even correspond > > to the same ordinals: 252, 253, and 254 (this is by design); so there > > is no problem encoding them, and the resulting bytes even happen to > > match the codepoints of the characters. > > > > > cp1252 > > > > cp1252 is designed after ISO 8859-1 and also has those same three characters: > > > > >>> for char in b'\xfc\xfd\xfe'.decode('cp1252'): > > ... print(unicodedata.name(char)) > > ... > > LATIN SMALL LETTER U WITH DIAERESIS > > LATIN SMALL LETTER Y WITH ACUTE > > LATIN SMALL LETTER THORN > > > > > latin1 > > > > Latin-1 is just another name for ISO 8859-1. > > > > > utf-8 > > > > UTF-8 is a *multi-byte* encoding. It can encode any Unicode > > characters, so you can represent those three characters in UTF-8, but > > with a different (and longer) byte sequence: > > > > >>> print('\xfc\xfd\xfd'.encode('utf8')) > > b'\xc3\xbc\xc3\xbd\xc3\xbd' > > > > > cp437 > > > > cp437 is another 8-bit encoding, but it maps entirely different > > characters to those three bytes: > > > > >>> for char in b'\xfc\xfd\xfe'.decode('cp437'): > > ... print(unicodedata.name(char)) > > ... > > SUPERSCRIPT LATIN SMALL LETTER N > > SUPERSCRIPT TWO > > BLACK SQUARE > > > > As it happens, the character at codepoint 252 (that's LATIN SMALL > > LETTER U WITH DIAERESIS) does exist in cp437. It maps to the byte > > 0x81: > > > > >>> '\xfc'.encode('cp437') > > b'\x81' > > > > The other two Unicode characters, at codepoints 253 and 254, do not > > exist at all in cp437 and cannot be encoded. > > > > > If I'm not mistaken, all of these codecs can handle the complete 8bit > > > character set. > > > > There is no "complete 8bit character set". cp1252, Latin1, and cp437 > > are all 8-bit character sets, but they're *different* 8-bit character > > sets with only partial overlap. > > > > > However, on Windows 7, I am only able to use 'cp437' to display (print) data > > > with those characters in Python. If I use any other encoding, Windows laughs > > > at me with this error message: > > > > > > File "C:\Python33\lib\encodings\cp437.py", line 19, in encode > > > return codecs.charmap_encode(input,self.errors,encoding_map)[0] > > > UnicodeEncodeError: 'charmap' codec can't encode character '\xfd' in > > > position 3: character maps to > > > > It would be helpful to see the code you're running that causes this error. I'm using subprocess.Popen to run a process that sends a list of codepoints to the calling Python program. The list is sent to stdout as a string. Here is a simple example that encodes the string "Dead^Parrot", where (for this example) I'm using '^' to represent chr(254) : encoded_string = '[68,101,97,100,254,80,97,114,114,111,116]' This in turn is handled in __repr__ with: return bytes((eval(encoded_string))).decode('cp437') I get the aforementioned 'error' if I use any other encoding. > > > > > Furthermore I get this from IDLE: > > > > > >>>> import locale > > >>>> locale.getdefaultlocale() > > > ('en_US', 'cp1252') > > > > > > I also get 'cp1252' when running the same script from a Windows command > > > prompt. > > > > > > So there is a contradiction between the error message and the default > > > encoding. > > > > If you're printing to stdout, it's going to use the encoding > > associated with stdout, which does not necessarily have anything to do > > with the default locale. Use this to determine what character set you > > need to be working in if you want your data to be printable: > > > > >>> import sys > > >>> sys.stdout.encoding > > 'cp437' > Hmmm. So THAT'S why I am only able to use 'cp437'. I had (mistakenly) thought that I could just indicate whatever encoding I wanted, as long as the codec supported it. > > > > Why am I restricted from using just that one codec? Is this a Windows or > > > Python restriction? Please enlighten me. > > > > In Linux, your terminal encoding is probably either UTF-8 or Latin-1, > > and either way it has no problems encoding that data for output. In a > > Windows cmd terminal, the default terminal encoding is cp437, which > > can't support two of the three characters you mentioned above. It may not be able to encode those two characters but it is able to decode them. That seems rather inconsistent (and contradictory) to me. From steve+comp.lang.python at pearwood.info Wed Nov 21 06:43:10 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 21 Nov 2012 11:43:10 GMT Subject: Web Frameworks Excessive Complexity References: <50ac31dd$0$29987$c3e8da3$5496439d@news.astraweb.com> <67gun9-kk8.ln1@satorlaser.homedns.org> Message-ID: <50acbe4e$0$29987$c3e8da3$5496439d@news.astraweb.com> On Wed, 21 Nov 2012 22:21:23 +1100, Chris Angelico wrote: > Counting complexity by giving a score to every statement encourages code > like this: > > def bletch(x,y): > return x + {"foo":y*2,"bar":x*3+y,"quux":math.sin(y)}.get(mode,0) > > instead of: > > def bletch(x,y): > if mode=="foo": return x+y*2 > if mode=="bar": return x*4+y > if mode=="quux": return x+math.sin(y) return x > > Okay, this is a stupid contrived example, but tell me which of those > you'd rather work with Am I being paid by the hour or the line? -- Steven From andriy.kornatskyy at live.com Wed Nov 21 06:45:48 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Wed, 21 Nov 2012 14:45:48 +0300 Subject: Web Frameworks Excessive Complexity In-Reply-To: References: , , , , <50ac31dd$0$29987$c3e8da3$5496439d@news.astraweb.com>, <67gun9-kk8.ln1@satorlaser.homedns.org>, , Message-ID: Chris, The focus of development team is controlled by setting a metric?threshold or just excluding some. So you do not have an overhead for the development team from the point it set forward, assuming them team committed to?adherence?it. Your strategy for perfection may vary.?You can start with 8 for CC in new project, or with a higher level of 15 in an existing project. Where you end up / the team agrees upon, depends on team commitment to the goal you set. There is no gold median, there is just recommendation, how you fluctuate from it and what reason you face with depends on team. Thanks. Andriy ---------------------------------------- > Date: Wed, 21 Nov 2012 22:21:23 +1100 > Subject: Re: Web Frameworks Excessive Complexity > From: rosuav at gmail.com > To: python-list at python.org > > On Wed, Nov 21, 2012 at 10:09 PM, Andriy Kornatskyy > wrote: > > We choose Python for its readability. This is essential principal of language and thousands around reading the open source code. Things like PEP8, CC, LoC are all to serve you one purpose: bring your attention, teach you make your code better. > > But too much focus on metrics results in those metrics improving > without any material benefit to the code. If there's a number that you > can watch going up or down, nobody's going to want to be the one that > pushes that number the wrong direction. So what happens when the right > thing to do happens to conflict with the given metric? And yes, it > WILL happen, guaranteed. No metric is perfect. > > Counting lines of code teaches you to make dense code. That's not a > good thing nor a bad thing; you'll end up with list comprehensions > rather than short loops, regardless of which is easier to actually > read. > > Counting complexity by giving a score to every statement encourages > code like this: > > def bletch(x,y): > return x + {"foo":y*2,"bar":x*3+y,"quux":math.sin(y)}.get(mode,0) > > instead of: > > def bletch(x,y): > if mode=="foo": return x+y*2 > if mode=="bar": return x*4+y > if mode=="quux": return x+math.sin(y) > return x > > Okay, this is a stupid contrived example, but tell me which of those > you'd rather work with, and then tell me a plausible metric that would > agree with you. > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list From andriy.kornatskyy at live.com Wed Nov 21 06:47:00 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Wed, 21 Nov 2012 14:47:00 +0300 Subject: Web Frameworks Excessive Complexity In-Reply-To: <50acbe4e$0$29987$c3e8da3$5496439d@news.astraweb.com> References: , , , , <50ac31dd$0$29987$c3e8da3$5496439d@news.astraweb.com>, <67gun9-kk8.ln1@satorlaser.homedns.org>, , , <50acbe4e$0$29987$c3e8da3$5496439d@news.astraweb.com> Message-ID: I believe for the quality of code you produce. Thanks. Andriy ---------------------------------------- > From: steve+comp.lang.python at pearwood.info > Subject: Re: Web Frameworks Excessive Complexity > Date: Wed, 21 Nov 2012 11:43:10 +0000 > To: python-list at python.org > > On Wed, 21 Nov 2012 22:21:23 +1100, Chris Angelico wrote: > > > Counting complexity by giving a score to every statement encourages code > > like this: > > > > def bletch(x,y): > > return x + {"foo":y*2,"bar":x*3+y,"quux":math.sin(y)}.get(mode,0) > > > > instead of: > > > > def bletch(x,y): > > if mode=="foo": return x+y*2 > > if mode=="bar": return x*4+y > > if mode=="quux": return x+math.sin(y) return x > > > > Okay, this is a stupid contrived example, but tell me which of those > > you'd rather work with > > > Am I being paid by the hour or the line? > > > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list From robert.kern at gmail.com Wed Nov 21 06:47:44 2012 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 21 Nov 2012 11:47:44 +0000 Subject: Web Frameworks Excessive Complexity In-Reply-To: <50ac31dd$0$29987$c3e8da3$5496439d@news.astraweb.com> References: <50ac31dd$0$29987$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 21/11/2012 01:43, Steven D'Aprano wrote: > On Tue, 20 Nov 2012 20:07:54 +0000, Robert Kern wrote: > >> The source of bugs is not excessive complexity in a method, just >> excessive lines of code. > > Taken literally, that cannot possibly the case. > > def method(self, a, b, c): > do_this(a) > do_that(b) > do_something_else(c) > > > def method(self, a, b, c): > do_this(a); do_that(b); do_something_else(c) > > > It *simply isn't credible* that version 1 is statistically likely to have > twice as many bugs as version 2. Over-reliance on LOC is easily gamed, > especially in semicolon languages. Logical LoC (executable LoC, number of statements, etc.) is a better measure than Physical LoC, I agree. That's not the same thing as cyclomatic complexity, though. Also, the relationship between LoC (of either type) and bugs is not linear (at least not in the small-LoC regime), so you are certainly correct that it isn't credible that version 1 is likely to have twice as many bugs as version 2. No one is saying that it is. > Besides, I think you have the cause and effect backwards. I would rather > say: > > The source of bugs is not lines of code in a method, but excessive > complexity. It merely happens that counting complexity is hard, counting > lines of code is easy, and the two are strongly correlated, so why count > complexity when you can just count lines of code? No, that is not the takeaway of the research. More code correlates with more bugs. More cyclomatic complexity also correlates with more bugs. You want to find out what causes bugs. What the research shows is that cyclomatic complexity is so correlated with LoC that it is going to be very difficult, or impossible, to establish a causal relationship between cyclomatic complexity and bugs. The previous research that just correlated cyclomatic complexity to bugs without controlling for LoC does not establish the causal relationship. > Keep in mind that something like 70-80% of published scientific papers > are never replicated, or cannot be replicated. Just because one paper > concludes that LOC alone is a better metric than CC doesn't necessary > make it so. But even if we assume that the paper is valid, it is > important to understand just what it says, and not extrapolate too far. This paper is actually a replication. It is notable for how comprehensive it is. > The paper makes various assumptions, takes statistical samples, and uses > models. (Which of course *any* such study must.) I'm not able to comment > on whether those models and assumptions are valid, but assuming that they > are, the conclusion of the paper is no stronger than the models and > assumptions. We should not really conclude that "CC has no more > predictive power than LOC". The right conclusion is that one specific > model of cyclic complexity, McCabe's CC, has no more predictive power > than LOC for projects written in C, C++ and Java. > > How does that apply to Python code? Well, it's certainly suggestive, but > it isn't definitive. More so than the evidence that CC is a worthwhile measure, for Python or any language. > It's also important to note that the authors point out that in their > samples of code, they found very high variance and large numbers of > outliers: > > [quote] > Modules where LOC does not predict CC (or vice-versa) may indicate an > overly-complex module with a high density of decision points or an overly- > simple module that may need to be refactored. > [end quote] > > So *even by the terms of this paper*, it isn't true that CC has no > predictive value over LOC -- if the CC is radically high or low for the > LOC, that is valuable to know. Is it? What is the evidence that excess, unpredicted-by-LoC CC causes (or even correlates with) bugs? The paper points that out as a target for future research because no one has studied it yet. It may turn out to be a valid metric, but one that has a very specific utility: identifying a particular hotspot. Running CC over whole projects to compare their "quality", as the OP has done, is not a valid use of even that. >> LoC is much simpler, easier to understand, and >> easier to correct than CC. > > Well, sure, but do you really think Perl one-liners are the paragon of > bug-free code we ought to be aiming for? *wink* No, but introducing more statements and method calls to avoid if statements isn't either. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco From robert.kern at gmail.com Wed Nov 21 06:54:06 2012 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 21 Nov 2012 11:54:06 +0000 Subject: Web Frameworks Excessive Complexity In-Reply-To: References: , , , , , , , , , Message-ID: On 21/11/2012 11:02, Andriy Kornatskyy wrote: > > Robert, > > You would never get a better product by accident. > > The meaning of better product might differ from team to team but you can not ignore excessive complexity. Earlier or later you get back to that code and refactor it, thus existence of such fact was driven by your intention to make it a bit better (easier to understand, to support, to cover with unit tests, etc), with a team of 20 heads you can get even further: the whole team adherence. So those drops make the overall picture better. This is what you, as a software developer, donate to what the final better product become. I think you may be misinterpreting the English idiom. I don't mean that your finger slips and randomly types out better code. I mean that by focusing on CC as a metric for improvement, you may very well end up improving the code, but it's not because you reduced the CC of the code. It's because of all of those *other* things that you talk about. Those are the things that should drive your refactoring, not CC, because they actually do cause improved code. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco From hansmu at xs4all.nl Wed Nov 21 07:14:32 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Wed, 21 Nov 2012 13:14:32 +0100 Subject: Printing time in "at" format? In-Reply-To: References: Message-ID: <50acc5a9$0$6940$e4fe514c@news2.news.xs4all.nl> On 20/11/12 17:18:33, Roy Smith wrote: > I need to talk to an API which requires (for reasons totally beyond my > comprehension), time specified in "at" format, i.e. the format > accepted by the unix "at" command. This allows you to do such bizarre > things as use "teatime" to indicate 4:00 PM. The best I can find for > a specification is http://tinyurl.com/d5vddoa. > > Before I dive into this too far, has anybody already written some code > which formats times like that? On my machine x.strftime("%H:%m %d.%m.%Y") formats datetime objects in a format that at(1) understands: $ > at -f my_job '12:11 21.11.2012' at: trying to travel back in time Hope this helps, -- HansM From andriy.kornatskyy at live.com Wed Nov 21 07:17:37 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Wed, 21 Nov 2012 15:17:37 +0300 Subject: Web Frameworks Excessive Complexity In-Reply-To: References: , , ,,, ,,, , , , , , , , , Message-ID: Agreed. I think we have pretty much the same point of view on this. All these metrics advise you... this is again depends how you look at this. If you are a new comer to a project, you usually spend some time on code review, talk to people, read docs if any. The qa tools for static code analysis give you an initial picture, how it fits with your own vision, etc. Convince or accept? Andriy Kornatskyy ---------------------------------------- > To: python-list at python.org > From: robert.kern at gmail.com > Subject: Re: Web Frameworks Excessive Complexity > Date: Wed, 21 Nov 2012 11:54:06 +0000 > > On 21/11/2012 11:02, Andriy Kornatskyy wrote: > > > > Robert, > > > > You would never get a better product by accident. > > > > The meaning of better product might differ from team to team but you can not ignore excessive complexity. Earlier or later you get back to that code and refactor it, thus existence of such fact was driven by your intention to make it a bit better (easier to understand, to support, to cover with unit tests, etc), with a team of 20 heads you can get even further: the whole team adherence. So those drops make the overall picture better. This is what you, as a software developer, donate to what the final better product become. > > I think you may be misinterpreting the English idiom. I don't mean that your > finger slips and randomly types out better code. I mean that by focusing on CC > as a metric for improvement, you may very well end up improving the code, but > it's not because you reduced the CC of the code. It's because of all of those > *other* things that you talk about. Those are the things that should drive your > refactoring, not CC, because they actually do cause improved code. > > -- > Robert Kern > > "I have come to believe that the whole world is an enigma, a harmless enigma > that is made terrible by our own mad attempt to interpret it as though it had > an underlying truth." > -- Umberto Eco > > -- > http://mail.python.org/mailman/listinfo/python-list From nobody at nowhere.com Wed Nov 21 07:18:23 2012 From: nobody at nowhere.com (Nobody) Date: Wed, 21 Nov 2012 12:18:23 +0000 Subject: Encoding conundrum References: <6cce1b50-aa37-4077-89d5-34e57de2193e@googlegroups.com> Message-ID: On Wed, 21 Nov 2012 03:24:01 -0800, danielk wrote: >> >>> import sys >> >>> sys.stdout.encoding >> 'cp437' > > Hmmm. So THAT'S why I am only able to use 'cp437'. I had (mistakenly) > thought that I could just indicate whatever encoding I wanted, as long as > the codec supported it. sys.stdout.encoding determines how Python converts unicode characters written to sys.stdout to bytes. If you want the correct characters to be shown, this has to match the encoding which the console window uses to convert those bytes back to unicode characters. You can tell Python to use whichever encoding you want, but often you only get to control one side of the equation, in which case there's only one "right" answer. From robert.kern at gmail.com Wed Nov 21 07:26:26 2012 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 21 Nov 2012 12:26:26 +0000 Subject: Web Frameworks Excessive Complexity In-Reply-To: References: , , , , , , , , , , , , , , , , Message-ID: On 21/11/2012 12:17, Andriy Kornatskyy wrote: > > Agreed. I think we have pretty much the same point of view on this. > > All these metrics advise you... this is again depends how you look at this. If you are a new comer to a project, you usually spend some time on code review, talk to people, read docs if any. The qa tools for static code analysis give you an initial picture, how it fits with your own vision, etc. Convince or accept? No, we don't have the same point of view on this. I think that using metrics that have no evidence for their utility is a misleading distraction. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco From hansmu at xs4all.nl Wed Nov 21 07:33:43 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Wed, 21 Nov 2012 13:33:43 +0100 Subject: 10 sec poll - please reply! In-Reply-To: <50ac2ba6$0$29987$c3e8da3$5496439d@news.astraweb.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <50ac2ba6$0$29987$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50acca27$0$6940$e4fe514c@news2.news.xs4all.nl> On 21/11/12 02:17:26, Steven D'Aprano wrote: > On Tue, 20 Nov 2012 18:00:59 -0600, Tim Chase wrote: > >> On 11/20/12 06:18, Michael Herrmann wrote: >>> am having difficulty picking a name for the function that simulates key >>> strokes. I currently have it as 'type' but that clashes with the >>> built-in function. >> >> Just to add one more to the pot, Vim uses "feedkeys()" for a similar >> purpose. > > What does it feed to the keys? Spam, spam, spam, eggs, and spam. -- HansM From giacomo.alzetta at gmail.com Wed Nov 21 07:43:57 2012 From: giacomo.alzetta at gmail.com (Giacomo Alzetta) Date: Wed, 21 Nov 2012 04:43:57 -0800 (PST) Subject: Inconsistent behaviour os str.find/str.index when providing optional parameters Message-ID: <9ecd357d-aaaa-4f4d-a987-a478e92b2052@googlegroups.com> I just came across this: >>> 'spam'.find('', 5) -1 Now, reading find's documentation: >>> print(str.find.__doc__) S.find(sub [,start [,end]]) -> int Return the lowest index in S where substring sub is found, such that sub is contained within S[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 on failure. Now, the empty string is a substring of every string so how can find fail? find, from the doc, should be generally be equivalent to S[start:end].find(substring) + start, except if the substring is not found but since the empty string is a substring of the empty string it should never fail. Looking at the source code for find(in stringlib/find.h): Py_LOCAL_INLINE(Py_ssize_t) stringlib_find(const STRINGLIB_CHAR* str, Py_ssize_t str_len, const STRINGLIB_CHAR* sub, Py_ssize_t sub_len, Py_ssize_t offset) { Py_ssize_t pos; if (str_len < 0) return -1; I believe it should be: if (str_len < 0) return (sub_len == 0 ? 0 : -1); Is there any reason of having this unexpected behaviour or was this simply overlooked? From andriy.kornatskyy at live.com Wed Nov 21 07:47:03 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Wed, 21 Nov 2012 15:47:03 +0300 Subject: Web Frameworks Excessive Complexity In-Reply-To: References: , , , ,,, , ,,, , ,,, ,,, , , , , , , , , Message-ID: Hm... what serves an evidence purpose for you? See functions at line 2619 and 2974 as an example for CC 20+: https://github.com/defnull/bottle/blob/master/bottle.py Andriy ---------------------------------------- > To: python-list at python.org > From: robert.kern at gmail.com > Subject: Re: Web Frameworks Excessive Complexity > Date: Wed, 21 Nov 2012 12:26:26 +0000 > > On 21/11/2012 12:17, Andriy Kornatskyy wrote: > > > > Agreed. I think we have pretty much the same point of view on this. > > > > All these metrics advise you... this is again depends how you look at this. If you are a new comer to a project, you usually spend some time on code review, talk to people, read docs if any. The qa tools for static code analysis give you an initial picture, how it fits with your own vision, etc. Convince or accept? > > No, we don't have the same point of view on this. I think that using metrics > that have no evidence for their utility is a misleading distraction. > > -- > Robert Kern > > "I have come to believe that the whole world is an enigma, a harmless enigma > that is made terrible by our own mad attempt to interpret it as though it had > an underlying truth." > -- Umberto Eco > > -- > http://mail.python.org/mailman/listinfo/python-list From d at davea.name Wed Nov 21 08:02:43 2012 From: d at davea.name (Dave Angel) Date: Wed, 21 Nov 2012 08:02:43 -0500 Subject: Encoding conundrum In-Reply-To: <6cce1b50-aa37-4077-89d5-34e57de2193e@googlegroups.com> References: <6cce1b50-aa37-4077-89d5-34e57de2193e@googlegroups.com> Message-ID: <50ACD0F3.1020908@davea.name> On 11/21/2012 06:24 AM, danielk wrote: > On Tuesday, November 20, 2012 6:03:47 PM UTC-5, Ian wrote: >>> >> >> In Linux, your terminal encoding is probably either UTF-8 or Latin-1, >> >> and either way it has no problems encoding that data for output. In a >> >> Windows cmd terminal, the default terminal encoding is cp437, which >> >> can't support two of the three characters you mentioned above. > It may not be able to encode those two characters but it is able to decode them. That seems rather inconsistent (and contradictory) to me. You encode characters (code points), but you never decode them. You decode bytes. In some cases and in some encodings, the number(ord) of the two happens to be the same, eg. for ASCII characters. Or to pick latin1, where the first 256 map exactly. But to pick utf8 for example, which I use almost exclusively on Linux, the character chr(255) is a lowercase y with a diaeresis accent. >>> chr(255) '?' >>> unicodedata.name(chr(255)) 'LATIN SMALL LETTER Y WITH DIAERESIS' >>> chr(255).encode() b'\xc3\xbf' >>> len(chr(255).encode()) 2 It takes 2 bytes to encode that character. (Since there are 1112064 possible characters, most of them take more than one byte to encode in utf-8. I believe the size can range up to 4 bytes.) But naturally, the first byte of those 2 cannot be one that's valid by itself as an encoded character, or it'd be impossible to pick apart (decode) a byte string starting with that one. So, there is no character which can be encoded to a single byte 0xc3. In other words: >>> bytes([253]) b'\xfd' >>> bytes([253]).decode() Traceback (most recent call last): File "", line 1, in UnicodeDecodeError: 'utf8' codec can't decode byte 0xfd in position 0: invalid start byte http://encyclopedia.thefreedictionary.com/UTF-8 has a description of the encoding rules. Note they're really just arithmetic, rather than arbitrary. Ranges of characters encode to various numbers of bytes. The main rules are that characters below 0x80 are unchanged, and no valid character encoding is a prefix to any other valid character encoding. Contrast that with cp437, where the particular 256 valid characters were chosen based only on their usefulness, and many of them are above 255. Consequently, there must be many characters below 255 which cannot be encoded. -- DaveA From robert.kern at gmail.com Wed Nov 21 08:13:36 2012 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 21 Nov 2012 13:13:36 +0000 Subject: Web Frameworks Excessive Complexity In-Reply-To: References: , , , , , , , , , , , , , , , , , , , , , , , , , Message-ID: On 21/11/2012 12:47, Andriy Kornatskyy wrote: > > Hm... what serves an evidence purpose for you? Well-done empirical studies, like the one I gave you. -- 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 d at davea.name Wed Nov 21 08:26:10 2012 From: d at davea.name (Dave Angel) Date: Wed, 21 Nov 2012 08:26:10 -0500 Subject: Index Error In-Reply-To: References: Message-ID: <50ACD672.8040308@davea.name> On 11/21/2012 05:11 AM, inshu chauhan wrote: >> >>> >>>> >> I must confess I have no idea what data represents. When you're doing >> rings, you use deltas on the cx and cy values. But when you're >> computing radius, you use the 3d coordinates returned by data[cx, cy]. >> So is data some kind of transformation, like a projection from a 3d >> object into a plane ? >> > > If I say data is a yml image file.. does it makes sense ? Nyways I am > treating it as matrix with values. > Yes you can say that it is a projection of 3D objects onto a 2D plane.. > which I am finding hard to work with !!!!! > http://www.fileinfo.com/extension/yml indicates that .yml files are human readable files, but doesn't specify much else. from other references, I gather it's used for javascript and for database. So far, I've only found one reference to image, http://stackoverflow.com/questions/10017298/read-yml-files-in-matlab but that doesn't really give me any intuitive meaning for the data. So i cannot guess what the meaning of circle radius is. At 8:43 yesterday, you had the following output and trace: """ data loaded [(296, 403), (298, 403), (298, 405), (296, 405), (297, 403), (298, 404), (297, 405), (296, 404)] ... I am printing Indices to know what index it dies out.. Traceback (most recent call last): File "Z:/modules/Classify.py", line 73, in ComputeClasses(data) File "Z:/modules/Classify.py", line 49, in ComputeClasses point = data[idx[0], idx[1]] error: index is out of range """ If you meant to just show us the last new_indices list printed out, then presumably somewhere in that list is the one that fails. Could it be that the internally linked jpeg file has the size 300 x 3000, and that the yml data only goes to 300 x 400 ? -- DaveA From d at davea.name Wed Nov 21 08:32:26 2012 From: d at davea.name (Dave Angel) Date: Wed, 21 Nov 2012 08:32:26 -0500 Subject: Index Error In-Reply-To: References: Message-ID: <50ACD7EA.4020302@davea.name> > > > >> >> Back to an earlier comment. I asked if N was ever bigger than x or >> bigger than y, and you said never. But your ComputeClasses will have >> such a case the very first time around, when cx==0, cy==0, and >> ring_number == 1. >> > > I doubt this , M confused.. > I'll paste an excerpt of the last source I've seen from you: """ def GenerateRing(x,y, N): Generates square rings around a point in data which has 300 columns(x) and 3000 rows(y) indices = [] for i in xrange(-N, N): indices.append((x+i, y-N)) indices.append((x+N, y+i)) indices.append((x-i, y+N)) indices.append((x-N, y-i)) return indices def ComputeClasses(data): radius = .5 points = [] for cy in xrange(0, data.height): for cx in xrange(0, data.width): if data[cy,cx] == (0.0,0.0,0.0): continue else : centre = data[cy, cx] points.append(centre) change = True while change: for ring_number in xrange(1, 100): change = False new_indices = GenerateRing(cx, cy, ring_number) """ When that GenerateRing() is first called, cy will be zero, cx the same, and ring_number will be 1. So some of the tuples in the returned list will have negative ints. You don't check for that either. I still think that data.height and data.width aren't the right limits to be using. -- DaveA From python at mrabarnett.plus.com Wed Nov 21 08:32:43 2012 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 21 Nov 2012 13:32:43 +0000 Subject: Inconsistent behaviour os str.find/str.index when providing optional parameters In-Reply-To: <9ecd357d-aaaa-4f4d-a987-a478e92b2052@googlegroups.com> References: <9ecd357d-aaaa-4f4d-a987-a478e92b2052@googlegroups.com> Message-ID: <50ACD7FB.3060906@mrabarnett.plus.com> On 2012-11-21 12:43, Giacomo Alzetta wrote: > I just came across this: > >>>> 'spam'.find('', 5) > -1 > > > Now, reading find's documentation: > >>>> print(str.find.__doc__) > S.find(sub [,start [,end]]) -> int > > Return the lowest index in S where substring sub is found, > such that sub is contained within S[start:end]. Optional > arguments start and end are interpreted as in slice notation. > > Return -1 on failure. > > Now, the empty string is a substring of every string so how can find fail? > find, from the doc, should be generally be equivalent to S[start:end].find(substring) + start, except if the substring is not found but since the empty string is a substring of the empty string it should never fail. > [snip] I think that returning -1 is correct (as far as returning -1 instead of raising an exception like .index could be considered correct!) because otherwise it whould be returning a non-existent index. For the string "spam", the range is 0..4. From j.robinson.10 at aberdeen.ac.uk Wed Nov 21 08:59:31 2012 From: j.robinson.10 at aberdeen.ac.uk (moadeep) Date: Wed, 21 Nov 2012 05:59:31 -0800 (PST) Subject: Update slider widget range Message-ID: I am trying to write a small bit of code that interactively deletes selected slices in an image series using matplotlib. I have created a button 'delete' which stores a number of indices to be deleted when the button 'update' is selected. However, I am currently unable to reset the range of my slider widget, i.e. removing the number of deleted slices from valmax. What is the pythonic solution to this problem? Here is my code: import dicom import numpy as np import matplotlib.pyplot as plt from matplotlib.widgets import Slider, Button frame = 0 #store indices of slices to be deleted delete_list = [] def main(): data = np.random.rand(16,256,256) nframes = data.shape[0] raw_dicom_stack = [] for x in range (nframes): raw_dicom_stack.append(data[x,:,:]) #yframe = 0 # Visualize it viewer = VolumeViewer(raw_dicom_stack, nframes) viewer.show() class VolumeViewer(object): def __init__(self, raw_dicom_stack, nframes): global delete_list self.raw_dicom_stack = raw_dicom_stack self.nframes = nframes self.delete_list = delete_list # Setup the axes. self.fig, self.ax = plt.subplots() self.slider_ax = self.fig.add_axes([0.2, 0.03, 0.65, 0.03]) self.delete_ax = self.fig.add_axes([0.85,0.84,0.1,0.04]) self.update_ax = self.fig.add_axes([0.85,0.78,0.1,0.04]) self.register_ax = self.fig.add_axes([0.85,0.72,0.1,0.04]) self.add_ax = self.fig.add_axes([0.85,0.66,0.1,0.04]) # Make the slider self.slider = Slider(self.slider_ax, 'Frame', 1, self.nframes, valinit=1, valfmt='%1d/{}'.format(self.nframes)) self.slider.on_changed(self.update) #Make the buttons self.del_button = Button(self.delete_ax, 'Delete') self.del_button.on_clicked(self.delete) self.upd_button = Button(self.update_ax, 'Update') self.upd_button.on_clicked(self.img_update) self.reg_button = Button(self.register_ax, 'Register') self.add_button = Button(self.add_ax, "Add") # Plot the first slice of the image self.im = self.ax.imshow(np.array(raw_dicom_stack[0])) def update(self, value): global frame frame = int(np.round(value - 1)) # Update the image data dat = np.array(self.raw_dicom_stack[frame]) self.im.set_data(dat) # Reset the image scaling bounds (this may not be necessary for you) self.im.set_clim([dat.min(), dat.max()]) # Redraw the plot self.fig.canvas.draw() def delete(self,event): global frame global delete_list delete_list.append(frame) print 'Frame %s has been added to list of slices to be deleted' %str(frame+1) print 'Please click update to delete these slices and show updated image series \n' #Remove duplicates from delete list def img_update(self,event): #function deletes image stacks and updates viewer global delete_list #Remove duplicates from list and sort into numerical order delete_list = list(set(delete_list)) delete_list.sort() #Make sure delete_list is not empty if not delete_list: print "Delete list is empty, no slices to delete" #Loop through delete list in reverse numerical order and remove slices from series else: for i in reversed(delete_list): self.raw_dicom_stack.pop(i) print 'Slice %i removed from dicom series \n' %(i+1) #Can now remove contents from delete_list del delete_list[:] #Update slider range self.nframes = len(self.raw_dicom_stack) def show(self): plt.show() if __name__ == '__main__': main() From insideshoes at gmail.com Wed Nov 21 09:33:44 2012 From: insideshoes at gmail.com (inshu chauhan) Date: Wed, 21 Nov 2012 15:33:44 +0100 Subject: Index Error In-Reply-To: <50ACD7EA.4020302@davea.name> References: <50ACD7EA.4020302@davea.name> Message-ID: I guess I have to use try and except as Chris suggested, this isn't working. -------------- next part -------------- An HTML attachment was scrubbed... URL: From wxjmfauth at gmail.com Wed Nov 21 09:49:16 2012 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Wed, 21 Nov 2012 06:49:16 -0800 (PST) Subject: Yet another Python textbook In-Reply-To: References: <31a82817-8c9b-4dd2-a468-89d8d081fd1b@googlegroups.com> Message-ID: <755fc776-652c-4e9c-afed-c831dbb95b46@googlegroups.com> Le mardi 20 novembre 2012 22:00:49 UTC+1, Chris Angelico a ?crit?: > On Wed, Nov 21, 2012 at 1:57 AM, wrote: > ----- > To the OP: jmf has an unnatural hatred of Python 3.3 and PEP 393 > > strings. No. Not at all. I'm mainly and deeply disappointed. jmf From wxjmfauth at gmail.com Wed Nov 21 09:49:16 2012 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Wed, 21 Nov 2012 06:49:16 -0800 (PST) Subject: Yet another Python textbook In-Reply-To: References: <31a82817-8c9b-4dd2-a468-89d8d081fd1b@googlegroups.com> Message-ID: <755fc776-652c-4e9c-afed-c831dbb95b46@googlegroups.com> Le mardi 20 novembre 2012 22:00:49 UTC+1, Chris Angelico a ?crit?: > On Wed, Nov 21, 2012 at 1:57 AM, wrote: > ----- > To the OP: jmf has an unnatural hatred of Python 3.3 and PEP 393 > > strings. No. Not at all. I'm mainly and deeply disappointed. jmf From saikari78 at gmail.com Wed Nov 21 09:59:11 2012 From: saikari78 at gmail.com (saikari78) Date: Wed, 21 Nov 2012 06:59:11 -0800 (PST) Subject: Constructing JSON data structures from non-string key python dictionaries Message-ID: <737ef2a9-f2e0-43fe-86a0-199940be2b69@googlegroups.com> Hi, I'm using the json module to create a JSON string, then inserting that string into a html template containing a javascript function (from the highcharts library: http://www.highcharts.com/) The json string I'm trying to create is to initialize a data variable in the javascript function, that has the following example format. data = [{ y: 55.11, color: colors[0], drilldown: { name: 'MSIE versions', categories: ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE 9.0'], data: [10.85, 7.35, 33.06, 2.81], color: colors[0] } }] However, I don't know how to do that because dictionary keys in python need to be strings. If I try to do the following, Python,of course, complains that y,color,drilldown, etc are not defined. import json data = [ { y:55.11, color:colors[0], drilldown:{name: 'MSIE versions',categories: ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE 9.0'],data: [10.85, 7.35, 33.06, 2.81],color: colors[0] }} ] data_string = json.dumps(data) Many thanks for any suggestions on how to do this. From python at mrabarnett.plus.com Wed Nov 21 10:48:00 2012 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 21 Nov 2012 15:48:00 +0000 Subject: Constructing JSON data structures from non-string key python dictionaries In-Reply-To: <737ef2a9-f2e0-43fe-86a0-199940be2b69@googlegroups.com> References: <737ef2a9-f2e0-43fe-86a0-199940be2b69@googlegroups.com> Message-ID: <50ACF7B0.8020901@mrabarnett.plus.com> On 2012-11-21 14:59, saikari78 wrote: > Hi, > > I'm using the json module to create a JSON string, then inserting that string into a html template containing a javascript function (from the highcharts library: http://www.highcharts.com/) > The json string I'm trying to create is to initialize a data variable in the javascript function, that has the following example format. > > > > data = [{ > y: 55.11, > color: colors[0], > drilldown: { > name: 'MSIE versions', > categories: ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE 9.0'], > data: [10.85, 7.35, 33.06, 2.81], > color: colors[0] > } > }] > > However, I don't know how to do that because dictionary keys in python need to be strings. If I try to do the following, Python,of course, complains that y,color,drilldown, etc are not defined. > > > import json > > data = [ { y:55.11, color:colors[0], drilldown:{name: 'MSIE versions',categories: ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE 9.0'],data: [10.85, 7.35, 33.06, 2.81],color: colors[0] }} ] > > data_string = json.dumps(data) > > > Many thanks for any suggestions on how to do this. > Just quote them: data = [ { 'y':55.11, 'color':colors[0], 'drilldown':{'name': 'MSIE versions','categories': ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE 9.0'],'data': [10.85, 7.35, 33.06, 2.81],'color': colors[0] }} ] Incidentally, dictionary keys in Python don't have to be strings, but merely 'hashable', which includes integers, floats and tuples amongst others. From invalid at invalid.invalid Wed Nov 21 11:04:35 2012 From: invalid at invalid.invalid (Grant Edwards) Date: Wed, 21 Nov 2012 16:04:35 +0000 (UTC) Subject: Constructing JSON data structures from non-string key python dictionaries References: <737ef2a9-f2e0-43fe-86a0-199940be2b69@googlegroups.com> Message-ID: On 2012-11-21, MRAB wrote: >> However, I don't know how to do that because dictionary keys in >> python need to be strings. If I try to do the following, Python,of >> course, complains that y,color,drilldown, etc are not defined. > > Just quote them: > > data = [ { 'y':55.11, 'color':colors[0], 'drilldown':{'name': 'MSIE > versions','categories': ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE > 9.0'],'data': [10.85, 7.35, 33.06, 2.81],'color': colors[0] }} ] > > Incidentally, dictionary keys in Python don't have to be strings, but > merely 'hashable', which includes integers, floats and tuples amongst > others. I think he meant that in his use case, the Python dictionary keys must be strings, since that's what JSON requires. -- Grant Edwards grant.b.edwards Yow! I have a TINY BOWL in at my HEAD gmail.com From hfolch at gmail.com Wed Nov 21 11:04:49 2012 From: hfolch at gmail.com (hfolch at gmail.com) Date: Wed, 21 Nov 2012 08:04:49 -0800 (PST) Subject: Constructing JSON data structures from non-string key python dictionaries In-Reply-To: References: <737ef2a9-f2e0-43fe-86a0-199940be2b69@googlegroups.com> Message-ID: <448f6f44-2387-4a2d-9131-084b3585443e@googlegroups.com> Thanks for your reply, but the javascript function expects option names to be unquoted, otherwise it won't work. On Wednesday, November 21, 2012 3:48:07 PM UTC, MRAB wrote: > On 2012-11-21 14:59, saikari78 wrote: > > > Hi, > > > > > > I'm using the json module to create a JSON string, then inserting that string into a html template containing a javascript function (from the highcharts library: http://www.highcharts.com/) > > > The json string I'm trying to create is to initialize a data variable in the javascript function, that has the following example format. > > > > > > > > > > > > data = [{ > > > y: 55.11, > > > color: colors[0], > > > drilldown: { > > > name: 'MSIE versions', > > > categories: ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE 9.0'], > > > data: [10.85, 7.35, 33.06, 2.81], > > > color: colors[0] > > > } > > > }] > > > > > > However, I don't know how to do that because dictionary keys in python need to be strings. If I try to do the following, Python,of course, complains that y,color,drilldown, etc are not defined. > > > > > > > > > import json > > > > > > data = [ { y:55.11, color:colors[0], drilldown:{name: 'MSIE versions',categories: ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE 9.0'],data: [10.85, 7.35, 33.06, 2.81],color: colors[0] }} ] > > > > > > data_string = json.dumps(data) > > > > > > > > > Many thanks for any suggestions on how to do this. > > > > > Just quote them: > > > > data = [ { 'y':55.11, 'color':colors[0], 'drilldown':{'name': 'MSIE > > versions','categories': ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE > > 9.0'],'data': [10.85, 7.35, 33.06, 2.81],'color': colors[0] }} ] > > > > Incidentally, dictionary keys in Python don't have to be strings, but > > merely 'hashable', which includes integers, floats and tuples amongst > > others. From hfolch at gmail.com Wed Nov 21 11:04:49 2012 From: hfolch at gmail.com (hfolch at gmail.com) Date: Wed, 21 Nov 2012 08:04:49 -0800 (PST) Subject: Constructing JSON data structures from non-string key python dictionaries In-Reply-To: References: <737ef2a9-f2e0-43fe-86a0-199940be2b69@googlegroups.com> Message-ID: <448f6f44-2387-4a2d-9131-084b3585443e@googlegroups.com> Thanks for your reply, but the javascript function expects option names to be unquoted, otherwise it won't work. On Wednesday, November 21, 2012 3:48:07 PM UTC, MRAB wrote: > On 2012-11-21 14:59, saikari78 wrote: > > > Hi, > > > > > > I'm using the json module to create a JSON string, then inserting that string into a html template containing a javascript function (from the highcharts library: http://www.highcharts.com/) > > > The json string I'm trying to create is to initialize a data variable in the javascript function, that has the following example format. > > > > > > > > > > > > data = [{ > > > y: 55.11, > > > color: colors[0], > > > drilldown: { > > > name: 'MSIE versions', > > > categories: ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE 9.0'], > > > data: [10.85, 7.35, 33.06, 2.81], > > > color: colors[0] > > > } > > > }] > > > > > > However, I don't know how to do that because dictionary keys in python need to be strings. If I try to do the following, Python,of course, complains that y,color,drilldown, etc are not defined. > > > > > > > > > import json > > > > > > data = [ { y:55.11, color:colors[0], drilldown:{name: 'MSIE versions',categories: ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE 9.0'],data: [10.85, 7.35, 33.06, 2.81],color: colors[0] }} ] > > > > > > data_string = json.dumps(data) > > > > > > > > > Many thanks for any suggestions on how to do this. > > > > > Just quote them: > > > > data = [ { 'y':55.11, 'color':colors[0], 'drilldown':{'name': 'MSIE > > versions','categories': ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE > > 9.0'],'data': [10.85, 7.35, 33.06, 2.81],'color': colors[0] }} ] > > > > Incidentally, dictionary keys in Python don't have to be strings, but > > merely 'hashable', which includes integers, floats and tuples amongst > > others. From spetrie at gmail.com Wed Nov 21 11:17:10 2012 From: spetrie at gmail.com (Steve Petrie) Date: Wed, 21 Nov 2012 08:17:10 -0800 (PST) Subject: Suitable software stacks for simple python web service In-Reply-To: References: Message-ID: On Wednesday, November 21, 2012 2:32:40 AM UTC-5, Kev Dwyer wrote: > Hello List, > > > > I have to build a simple web service which will: > > > > - receive queries from our other servers > > - forward the requests to a third party SOAP service > > - process the response from the third party > > - send the result back to the original requester > > > > >From the point of view of the requester, this will happen within the scope > > of a single request. > > > > The data exchanged with the original requester will likely be encoded as > > JSON; the SOAP service will be handled by SUDS. > > > > The load is likely to be quite light, say a few requests per hour, though > > this may increase in the future. > > > > Given these requirements, what do you think might be a suitable software > > stack, i.e. webserver and web framework (if a web framework is even > > necessary)? > > > > Candidates should be compatible with Python2.7, though I'd be happy to > > consider Python 3 if anyone knows of a Python3 SOAP library that has good > > WSDL support. > > > > Cheers, > > > > Kev I'm using the Bottle web framework (http://bottlepy.org) to integrate requests and replies originating in a Drupal site, a Beanstream (payment processor) account, and a Salesforce instance. Communication with Salesforce is done through the Salesforce Python Toolkit (http://code.google.com/p/salesforce-python-toolkit/), which uses Suds. Communication with the Drupal site uses Python's (and PHP's on the Drupal side) native JSON support. This is under Python 2.6.8 and Apache 2.2.23 running on an AWS EC2 instance. No (major) problems so far, though still in the early stages of this project. Steve I chose Bottle after trying a few other frameworks because, well, I can't remember exactly why, though thinking back it's probably because of the clarity of Bottle's approach and the simplicity of the documentation. From spetrie at gmail.com Wed Nov 21 11:17:10 2012 From: spetrie at gmail.com (Steve Petrie) Date: Wed, 21 Nov 2012 08:17:10 -0800 (PST) Subject: Suitable software stacks for simple python web service In-Reply-To: References: Message-ID: On Wednesday, November 21, 2012 2:32:40 AM UTC-5, Kev Dwyer wrote: > Hello List, > > > > I have to build a simple web service which will: > > > > - receive queries from our other servers > > - forward the requests to a third party SOAP service > > - process the response from the third party > > - send the result back to the original requester > > > > >From the point of view of the requester, this will happen within the scope > > of a single request. > > > > The data exchanged with the original requester will likely be encoded as > > JSON; the SOAP service will be handled by SUDS. > > > > The load is likely to be quite light, say a few requests per hour, though > > this may increase in the future. > > > > Given these requirements, what do you think might be a suitable software > > stack, i.e. webserver and web framework (if a web framework is even > > necessary)? > > > > Candidates should be compatible with Python2.7, though I'd be happy to > > consider Python 3 if anyone knows of a Python3 SOAP library that has good > > WSDL support. > > > > Cheers, > > > > Kev I'm using the Bottle web framework (http://bottlepy.org) to integrate requests and replies originating in a Drupal site, a Beanstream (payment processor) account, and a Salesforce instance. Communication with Salesforce is done through the Salesforce Python Toolkit (http://code.google.com/p/salesforce-python-toolkit/), which uses Suds. Communication with the Drupal site uses Python's (and PHP's on the Drupal side) native JSON support. This is under Python 2.6.8 and Apache 2.2.23 running on an AWS EC2 instance. No (major) problems so far, though still in the early stages of this project. Steve I chose Bottle after trying a few other frameworks because, well, I can't remember exactly why, though thinking back it's probably because of the clarity of Bottle's approach and the simplicity of the documentation. From python at mrabarnett.plus.com Wed Nov 21 11:27:50 2012 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 21 Nov 2012 16:27:50 +0000 Subject: Constructing JSON data structures from non-string key python dictionaries In-Reply-To: <448f6f44-2387-4a2d-9131-084b3585443e@googlegroups.com> References: <737ef2a9-f2e0-43fe-86a0-199940be2b69@googlegroups.com> <448f6f44-2387-4a2d-9131-084b3585443e@googlegroups.com> Message-ID: <50AD0106.6020303@mrabarnett.plus.com> On 2012-11-21 16:04, hfolch at gmail.com wrote: > On Wednesday, November 21, 2012 3:48:07 PM UTC, MRAB wrote: >> On 2012-11-21 14:59, saikari78 wrote: >>> Hi, >>> >>> I'm using the json module to create a JSON string, then >>> inserting that string into a html template containing a javascript >>> function (from the highcharts library: http://www.highcharts.com/) >>> >>> The json string I'm trying to create is to initialize a data >>> variable in the javascript function, that has the following example >>> format. >>> >>> data = [{ y: 55.11, color: colors[0], drilldown: { name: 'MSIE >>> versions', categories: ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE 9.0'], >>> data: [10.85, 7.35, 33.06, 2.81], color: colors[0] } }] >>> >>> However, I don't know how to do that because dictionary keys in >>> python need to be strings. If I try to do the following, Python,of >>> course, complains that y,color,drilldown, etc are not defined. >>> >>> >>> import json >>> >>> data = [ { y:55.11, color:colors[0], drilldown:{name: 'MSIE versions',categories: ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE 9.0'],data: [10.85, 7.35, 33.06, 2.81],color: colors[0] }} ] >>> >>> data_string = json.dumps(data) >>> >>> >>> Many thanks for any suggestions on how to do this. >>> >> Just quote them: data = [ { 'y':55.11, 'color':colors[0], >> 'drilldown':{'name': 'MSIE versions','categories': ['MSIE 6.0', >> 'MSIE 7.0', 'MSIE 8.0', 'MSIE 9.0'],'data': [10.85, 7.35, 33.06, >> 2.81],'color': colors[0] }} ] Incidentally, dictionary keys in >> Python don't have to be strings, but merely 'hashable', which >> includes integers, floats and tuples amongst others. > > Thanks for your reply, but the javascript function expects option > names to be unquoted, otherwise it won't work. Both Python source code and JSON require the dictionary keys to be quoted, so using the json module to generate JavaScript code isn't going to give you what you want. It shouldn't be too difficult to write a simple function to do it, considering the limited range of types. From kevinintx at gmail.com Wed Nov 21 11:41:53 2012 From: kevinintx at gmail.com (Kevin T) Date: Wed, 21 Nov 2012 08:41:53 -0800 (PST) Subject: re.search when used within an if/else fails References: <50aadbe6$0$29983$c3e8da3$5496439d@news.astraweb.com> <50aadcf0$0$29983$c3e8da3$5496439d@news.astraweb.com> Message-ID: <2930dc36-9d8f-411d-aa7a-272fe4885ec5@i5g2000yqh.googlegroups.com> On Nov 20, 1:37?pm, Ian Kelly wrote: > On Tue, Nov 20, 2012 at 12:09 PM, Kevin T wrote: > > #if re.search( "rsrvd", sigName ) : ? #version a > > #if re.search( "rsrvd", sigName ) == None : ? #version b > > if re.search( "rsrvd", sigName ) is None : ? #version bb > > ? ?print sigName > > ? ?newVal = "%s%s" % ('1'*signal['bits'] , newVal ) > > #else: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #version c > > if re.search( "rsrvd", sigName ) != None : ? #version d > > ? ?print sigName > > ? ?newVal = "%s%s" % ( '0'*signal['bits'],> newVal ) > > > i can use either version a/b the else clause (version c) will not execute. > > fortunately, ?with version bb, the else clause will execute!! > > There must be some other difference in your testing. ?I don't have > Python 2.4 available, but I tried your version a in both Python 2.3 > and 2.5 using made-up values for sigName, and the else clause is > executed in both. I went back and tried version a again, blam it is/does work now ?!?!? I am not sure what changed but version a was the original code that wouldn't work. All the examples i had read, showed version a as a working version. I spent significant time trying version a with parens, spacing changes, different regex values to no avail. hence the creation of all the other versions. thanks for your help. From python at mrabarnett.plus.com Wed Nov 21 11:43:15 2012 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 21 Nov 2012 16:43:15 +0000 Subject: Constructing JSON data structures from non-string key python dictionaries In-Reply-To: <50AD0106.6020303@mrabarnett.plus.com> References: <737ef2a9-f2e0-43fe-86a0-199940be2b69@googlegroups.com> <448f6f44-2387-4a2d-9131-084b3585443e@googlegroups.com> <50AD0106.6020303@mrabarnett.plus.com> Message-ID: <50AD04A3.3010706@mrabarnett.plus.com> On 2012-11-21 16:27, MRAB wrote:> On 2012-11-21 16:04, hfolch at gmail.com wrote: >> On Wednesday, November 21, 2012 3:48:07 PM UTC, MRAB wrote: >>> On 2012-11-21 14:59, saikari78 wrote: >>>> Hi, >>>> >>>> I'm using the json module to create a JSON string, then >>>> inserting that string into a html template containing a javascript >>>> function (from the highcharts library: http://www.highcharts.com/) >>>> >>>> The json string I'm trying to create is to initialize a data >>>> variable in the javascript function, that has the following example >>>> format. >>>> >>>> data = [{ y: 55.11, color: colors[0], drilldown: { name: 'MSIE >>>> versions', categories: ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE >>>> 9.0'], data: [10.85, 7.35, 33.06, 2.81], color: colors[0] } }] >>>> >>>> However, I don't know how to do that because dictionary keys in >>>> python need to be strings. If I try to do the following, Python,of >>>> course, complains that y,color,drilldown, etc are not defined. >>>> >>>> >>>> import json >>>> >>>> data = [ { y:55.11, color:colors[0], drilldown:{name: 'MSIE >>>> versions',categories: ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE >>>> 9.0'],data: [10.85, 7.35, 33.06, 2.81],color: colors[0] }} ] >>>> >>>> data_string = json.dumps(data) >>>> >>>> >>>> Many thanks for any suggestions on how to do this. >>>> >>> Just quote them: >>> data = [ { 'y':55.11, 'color':colors[0], 'drilldown':{'name': 'MSIE >>> versions', 'categories': ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE >>> 9.0'],'data': [10.85, 7.35, 33.06, 2.81],'color': colors[0] }} ] >>> >>> Incidentally, dictionary keys in Python don't have to be strings, >>> but merely 'hashable', which includes integers, floats and tuples >>> amongst others. >> >> Thanks for your reply, but the javascript function expects option >> names to be unquoted, otherwise it won't work. > > Both Python source code and JSON require the dictionary keys to be > quoted, so using the json module to generate JavaScript code isn't > going to give you what you want. > > It shouldn't be too difficult to write a simple function to do it, > considering the limited range of types. > Here's such a function: def dump_javascript(data): if isinstance(data, dict): items = [] for key, value in data.items(): items.append(key + ": " + dump_javascript(value)) return "{" + ", ".join(items) + "}" elif isinstance(data, list): items = [] for value in data: items.append(dump_javascript(value)) return "[" + ", ".join(items) + "]" else: return json.dumps(data) From alister.ware at ntlworld.com Wed Nov 21 11:59:05 2012 From: alister.ware at ntlworld.com (Alister) Date: Wed, 21 Nov 2012 16:59:05 GMT Subject: Inconsistent behaviour os str.find/str.index when providing optional parameters References: <9ecd357d-aaaa-4f4d-a987-a478e92b2052@googlegroups.com> Message-ID: On Wed, 21 Nov 2012 04:43:57 -0800, Giacomo Alzetta wrote: > I just came across this: > >>>> 'spam'.find('', 5) > -1 > > > Now, reading find's documentation: > >>>> print(str.find.__doc__) > S.find(sub [,start [,end]]) -> int > > Return the lowest index in S where substring sub is found, > such that sub is contained within S[start:end]. Optional arguments > start and end are interpreted as in slice notation. > > Return -1 on failure. > > Now, the empty string is a substring of every string so how can find > fail? > find, from the doc, should be generally be equivalent to > S[start:end].find(substring) + start, except if the substring is not > found but since the empty string is a substring of the empty string it > should never fail. > > Looking at the source code for find(in stringlib/find.h): > > Py_LOCAL_INLINE(Py_ssize_t) > stringlib_find(const STRINGLIB_CHAR* str, Py_ssize_t str_len, > const STRINGLIB_CHAR* sub, Py_ssize_t sub_len, > Py_ssize_t offset) > { > Py_ssize_t pos; > > if (str_len < 0) > return -1; > > I believe it should be: > > if (str_len < 0) > return (sub_len == 0 ? 0 : -1); > > Is there any reason of having this unexpected behaviour or was this > simply overlooked? why would you be searching for an empty string? what result would you expect to get from such a search? -- Turn your Pentium into an XT -- just add Windows! From cjw at ncf.ca Wed Nov 21 12:03:30 2012 From: cjw at ncf.ca (Colin J. Williams) Date: Wed, 21 Nov 2012 12:03:30 -0500 Subject: Yet another Python textbook In-Reply-To: References: <31a82817-8c9b-4dd2-a468-89d8d081fd1b@googlegroups.com> Message-ID: <50AD0962.5080002@ncf.ca> On 20/11/2012 4:00 PM, Chris Angelico wrote: > On Wed, Nov 21, 2012 at 1:57 AM, wrote: >> Le mardi 20 novembre 2012 09:09:50 UTC+1, Chris Angelico a ?crit : >>> On Tue, Nov 20, 2012 at 7:02 PM, Pavel Solin wrote: >>> >>>> Perhaps you are right. Is there any statistics of how many Python >>> >>>> programmers are using 2.7 vs. 3? Most of people I know use 2.7. >>> >>> >>> >>> If you're teaching Python, the stats are probably about zero for zero. >>> >>> Start them off on Py3 and help move the world forward. >>> >>> >>> >>> ChrisA >> >> -------- >> >> Do not count with me. >> >> The absurd flexible string representation has practically >> borrowed the idea to propose once Python has a teaching tool. > > To the OP: jmf has an unnatural hatred of Python 3.3 and PEP 393 > strings. Take no notice; the rest of the world sees this as a huge > advantage. Python is now in a VERY small group of languages (I'm aware > of just one other) that have absolutely proper Unicode handling *and* > efficient string handling. > > ChrisA > It's interesting to see that someone else finds the format function to be a pain. Perhaps the problem lies with the documentation. Colin W. From mining.facts at gmail.com Wed Nov 21 12:19:15 2012 From: mining.facts at gmail.com (Christian) Date: Wed, 21 Nov 2012 09:19:15 -0800 (PST) Subject: mysql insert with tuple Message-ID: Hi , my purpose is a generic insert via tuple , because the number of fields and can differ. But I'm stucking . ilist=['hello',None,7,None,None] #This version works, but all varchar fields are in extra '' enclosed. con.execute(""" INSERT INTO {} VALUES %r; """.format(table) , (tuple(ilist),)) #This produce (1054, "Unknown column 'None' in 'field list'"), #but without None values it works. con.execute(""" INSERT INTO {} VALUES %r; """.format(table) % (tuple(ilist),)) Many thanks, Christian From michael.herrmann at getautoma.com Wed Nov 21 12:19:23 2012 From: michael.herrmann at getautoma.com (Michael Herrmann) Date: Wed, 21 Nov 2012 09:19:23 -0800 (PST) Subject: 10 sec poll - please reply! In-Reply-To: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> Message-ID: <351c5ef7-261d-41f6-a911-a4dcf8378b24@googlegroups.com> Dear all, thanks so much for your replies. Based on your inputs, we have started to experiment with changes to our API. I hope to be able to present the results to you tomorrow. Thanks again, Michael On Tuesday, November 20, 2012 1:18:38 PM UTC+1, Michael Herrmann wrote: > Hi, > > > > I'm developing a GUI Automation library (http://www.getautoma.com) and am having difficulty picking a name for the function that simulates key strokes. I currently have it as 'type' but that clashes with the built-in function. Example uses of 'type': > > > > type(ENTER) > > > > type("Hello World!") > > > > type(CTRL + 'a') > > > > What, in your view, would be the most intuitive alternative name? > > > > Here are my thoughts so far: I could call it 'press' but then our GUI automation tool also allows you to click things and then "press" might be mistaken for "pressing a button". A less ambiguous alternative is "type_keys" but that is rather long and doesn't read as well, for instance in type_keys(ENTER). > > > > Thank you very much! From clp2 at rebertia.com Wed Nov 21 13:12:26 2012 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 21 Nov 2012 10:12:26 -0800 Subject: Web Frameworks Excessive Complexity In-Reply-To: <20121121094939.2be7ebf0809c1959538f8166@lavabit.com> References: <20121121094939.2be7ebf0809c1959538f8166@lavabit.com> Message-ID: On Wed, Nov 21, 2012 at 9:49 AM, rh wrote: > On Tue, 20 Nov 2012 20:41:42 +0300 > Andriy Kornatskyy wrote: >> Cyclomatic (or conditional) complexity is a metric used to indicate >> the complexity of a source code. Excessive complexity is something >> that is beyond recommended level of 10 (threshold that points to the >> fact the source code is too complex and refactoring is suggested). >> Here is a list of web frameworks examined: bottle, cherrypy, >> circuits, django, flask, pyramid, pysi, tornado, turbogears, web.py, >> web2py and wheezy.web. >> >> You can read more here: >> >> http://mindref.blogspot.com/2012/11/python-web-excessive-complexity.html > > You are the author of wheezy.web right? Can't blame you for trying to > market your product. The conclusions, or lack of, are meaningless to me. > I have to get in and drive the car before I go all in and buy it. > > I'm looking at different technology right now on which to base a site. > I tried pyramid and after install it consumed 92MB of disk. It seemed > large and it turns out that it installed its own version of python. > Seems more complex to me, yet another python on disk. That's how virtualenvs (http://www.virtualenv.org/ ) normally work. Not really Pyramid's fault, it's more a deficiency of the current Python package management tools. Cheers, Chris From andriy.kornatskyy at live.com Wed Nov 21 13:23:32 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Wed, 21 Nov 2012 21:23:32 +0300 Subject: Web Frameworks Excessive Complexity In-Reply-To: <20121121094939.2be7ebf0809c1959538f8166@lavabit.com> References: , <20121121094939.2be7ebf0809c1959538f8166@lavabit.com> Message-ID: Richard, Thank you for the comment. I have examined web frameworks for PEP8 and CC metrics already. Results are here: http://mindref.blogspot.com/2012/10/python-web-pep8-consistency.html http://mindref.blogspot.com/2012/11/python-web-excessive-complexity.html Same applies to performance benchmarks: http://mindref.blogspot.com/2012/09/python-fastest-web-framework.html http://mindref.blogspot.com/2012/10/python-web-routing-benchmark.html http://mindref.blogspot.com/2012/10/python-web-reverse-urls-benchmark.html http://mindref.blogspot.com/2012/10/python-web-caching-benchmark.html and template engine related: http://mindref.blogspot.com/2012/10/python-templates-benchmark.html http://mindref.blogspot.com/2012/07/python-fastest-template.html If there are any other metrics we can gather automatically I will be happy to run them. Thank you for your interest of wheezy.web. Before you buy: Demo application source: https://bitbucket.org/akorn/wheezy.web/src/tip/demos/template Demo application live: http://wheezy.pythonanywhere.com/ Tutorial: http://packages.python.org/wheezy.web/tutorial.html Finished: https://bitbucket.org/akorn/wheezy.web/src/tip/demos/guestbook Try understand what is cache dependency and how you benefit of using it with content caching. Try relate it with web caching benchmark from above and imagine 99% of your web application content is served out from cache, even it is dynamic (you control content cache with cache dependency). Thanks. Andriy Kornatskyy ---------------------------------------- > To: python-list at python.org > From: richard_hubbe11 at lavabit.com > Subject: Re: Web Frameworks Excessive Complexity > Date: Wed, 21 Nov 2012 09:49:39 -0800 > > On Tue, 20 Nov 2012 20:41:42 +0300 > Andriy Kornatskyy wrote: > > > > > Cyclomatic (or conditional) complexity is a metric used to indicate > > the complexity of a source code. Excessive complexity is something > > that is beyond recommended level of 10 (threshold that points to the > > fact the source code is too complex and refactoring is suggested). > > Here is a list of web frameworks examined: bottle, cherrypy, > > circuits, django, flask, pyramid, pysi, tornado, turbogears, web.py, > > web2py and wheezy.web. > > > > You can read more here: > > > > http://mindref.blogspot.com/2012/11/python-web-excessive-complexity.html > > You are the author of wheezy.web right? Can't blame you for trying to > market your product. The conclusions, or lack of, are meaningless to me. > I have to get in and drive the car before I go all in and buy it. > > I'm looking at different technology right now on which to base a site. > I tried pyramid and after install it consumed 92MB of disk. It seemed > large and it turns out that it installed its own version of python. > Seems more complex to me, yet another python on disk. > > Anyway if you're really serious about making a point that complexity > matters you'll have to measure many more things than loc or cc. > > Did you look at the number of commits to the same file over time? > Etc. Number of authors for same file? Etc., etc. Number of security > fixes? There must be a correlation between complexity and insecurity. > Maybe check for randomness of the code. Not sure how, maybe > look for strange, non-idiomatic uses of the language. > > I'm no computer scientist and I'm sure there are volumes on all this. > > Then there's also the social side, how much discussion takes place > about the software? Does more discussion mean more problems? > More project vibrancy? You could check for vocab, etc. > > I'm gonna take a look at wheezy.web. > > > > Thanks. > > > > Comments or suggestions are welcome. > > > > Andriy Kornatskyy > > > > > -- > > > -- > http://mail.python.org/mailman/listinfo/python-list From azzmatsayed84 at gmail.com Wed Nov 21 13:40:56 2012 From: azzmatsayed84 at gmail.com (Azzmat Sayed) Date: Wed, 21 Nov 2012 21:40:56 +0300 Subject: No subject Message-ID: -------------- next part -------------- An HTML attachment was scrubbed... URL: From clp2 at rebertia.com Wed Nov 21 14:04:53 2012 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 21 Nov 2012 11:04:53 -0800 Subject: Constructing JSON data structures from non-string key python dictionaries In-Reply-To: <448f6f44-2387-4a2d-9131-084b3585443e@googlegroups.com> References: <737ef2a9-f2e0-43fe-86a0-199940be2b69@googlegroups.com> <448f6f44-2387-4a2d-9131-084b3585443e@googlegroups.com> Message-ID: On Wed, Nov 21, 2012 at 7:48 AM, MRAB wrote: > On 2012-11-21 14:59, saikari78 wrote: >> >> Hi, >> >> I'm using the json module to create a JSON string, then inserting that >> string into a html template containing a javascript function (from the >> highcharts library: http://www.highcharts.com/) Nontrivial templating of JavaScript is generally a bad/inelegant approach. I would instead suggest generating the JSON separately and loading it from JavaScript via $.getJSON or similar. Or sticking the JSON into a hidden part of the webpage and then using JSON.parse(). >> The json string I'm trying to create is to initialize a data variable in >> the javascript function, that has the following example format. >> >> data = [{ >> y: 55.11, >> color: colors[0], >> drilldown: { >> name: 'MSIE versions', >> categories: ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', >> 'MSIE 9.0'], >> data: [10.85, 7.35, 33.06, 2.81], >> color: colors[0] >> } >> }] >> >> However, I don't know how to do that because dictionary keys in python >> need to be strings. If I try to do the following, Python,of course, >> complains that y,color,drilldown, etc are not defined. >> >> >> import json >> >> data = [ { y:55.11, color:colors[0], drilldown:{name: 'MSIE >> versions',categories: ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE 9.0'],data: >> [10.85, 7.35, 33.06, 2.81],color: colors[0] }} ] >> >> data_string = json.dumps(data) >> >> >> Many thanks for any suggestions on how to do this. >> > Just quote them: > > > data = [ { 'y':55.11, 'color':colors[0], 'drilldown':{'name': 'MSIE > versions','categories': ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE > 9.0'],'data': [10.85, 7.35, 33.06, 2.81],'color': colors[0] }} ] > > Incidentally, dictionary keys in Python don't have to be strings, but > merely 'hashable', which includes integers, floats and tuples amongst > others. On Wed, Nov 21, 2012 at 8:04 AM, wrote: > Thanks for your reply, but the javascript function expects option names to be unquoted, otherwise it won't work. As a user of HighCharts (and thus, unfortunately, JavaScript), I can tell you that that's absolutely incorrect. In JavaScript, {x : y}, {"x" : y}, and {'x' : y} are all equivalent (at least when x is a valid JavaScript identifier; consult some non-w3schools JavaScript docs). Plus, you say you're using JSON; JSON *explicitly mandates that the keys be quoted* (see RFC 4627). You are experiencing Python NameErrors because {"x" : y} and {x : y} aren't equivalent in Python. Python doesn't limit dicts keys to strings, so `x` is a variable in the latter snippet; x's value is used as the key. You cannot expect to take arbitrary, unmodified JavaScript code/literals, copy-paste them into Python, and expect them to work. TL;DR: # foo.py from json import dumps colors = SOME_LIST data = [dict( # use dict() to avoid tedious quoting y=55.11, color=colors[0], drilldown=dict( name='MSIE versions', categories=['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE 9.0'], data=[10.85, 7.35, 33.06, 2.81], color=colors[0], ) )] your_json = dumps(data) # ...serve the JSON somehow... ================ // bar.js // Not industrial-strength. Assumes the use of jQuery. // ... $.getJSON(SOME_URL, function (data) { // use 'data', which will be a JavaScript object by this point }); // ... Regards, Chris From clp2 at rebertia.com Wed Nov 21 14:08:14 2012 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 21 Nov 2012 11:08:14 -0800 Subject: Web Frameworks Excessive Complexity In-Reply-To: <20121121105703.83afe07ede3c397cf7529092@lavabit.com> References: <20121121094939.2be7ebf0809c1959538f8166@lavabit.com> <20121121105703.83afe07ede3c397cf7529092@lavabit.com> Message-ID: On Wed, Nov 21, 2012 at 10:57 AM, rh wrote: > On Wed, 21 Nov 2012 10:12:26 -0800 > Chris Rebert wrote: >> On Wed, Nov 21, 2012 at 9:49 AM, rh >> wrote: >> > On Tue, 20 Nov 2012 20:41:42 +0300 >> > Andriy Kornatskyy wrote: >> > I'm looking at different technology right now on which to base a >> > site. I tried pyramid and after install it consumed 92MB of disk. >> > It seemed large and it turns out that it installed its own version >> > of python. Seems more complex to me, yet another python on disk. >> >> That's how virtualenvs (http://www.virtualenv.org/ ) normally work. >> Not really Pyramid's fault, it's more a deficiency of the current >> Python package management tools. > > There's still 92MB under pyramid, I just installed a new virtualenv and > installed wheezy.web, grand total 3.3MB. > > What deficiency? "install[ing] its own version of python" -- Chris From hansmu at xs4all.nl Wed Nov 21 14:25:09 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Wed, 21 Nov 2012 20:25:09 +0100 Subject: Inconsistent behaviour os str.find/str.index when providing optional parameters In-Reply-To: References: <9ecd357d-aaaa-4f4d-a987-a478e92b2052@googlegroups.com> Message-ID: <50ad2a95$0$6907$e4fe514c@news2.news.xs4all.nl> On 21/11/12 17:59:05, Alister wrote: > On Wed, 21 Nov 2012 04:43:57 -0800, Giacomo Alzetta wrote: > >> I just came across this: >> >>>>> 'spam'.find('', 5) >> -1 >> >> >> Now, reading find's documentation: >> >>>>> print(str.find.__doc__) >> S.find(sub [,start [,end]]) -> int >> >> Return the lowest index in S where substring sub is found, >> such that sub is contained within S[start:end]. Optional arguments >> start and end are interpreted as in slice notation. >> >> Return -1 on failure. >> >> Now, the empty string is a substring of every string so how can find >> fail? >> find, from the doc, should be generally be equivalent to >> S[start:end].find(substring) + start, except if the substring is not >> found but since the empty string is a substring of the empty string it >> should never fail. >> >> Looking at the source code for find(in stringlib/find.h): >> >> Py_LOCAL_INLINE(Py_ssize_t) >> stringlib_find(const STRINGLIB_CHAR* str, Py_ssize_t str_len, >> const STRINGLIB_CHAR* sub, Py_ssize_t sub_len, >> Py_ssize_t offset) >> { >> Py_ssize_t pos; >> >> if (str_len < 0) >> return -1; >> >> I believe it should be: >> >> if (str_len < 0) >> return (sub_len == 0 ? 0 : -1); >> >> Is there any reason of having this unexpected behaviour or was this >> simply overlooked? > > why would you be searching for an empty string? > what result would you expect to get from such a search? In general, if needle in haystack[ start: ] return True, then you' expect haystack.find(needle, start) to return the smallest i >= start such that haystack[i:i+len(needle)] == needle also returns True. >>> "" in "spam"[5:] True >>> "spam"[5:5+len("")] == "" True >>> So, you'd expect that spam.find("", 5) would return 5. The only other consistent position would be that "spam"[5:] should raise an IndexError, because 5 is an invalid index. For that matter, I wouldn;t mind if "spam".find(s, 5) were to raise an IndexError. But if slicing at position 5 proudces an empry string, then .find should be able to find that empty string. -- HansM From ron.eggler at gmail.com Wed Nov 21 14:26:51 2012 From: ron.eggler at gmail.com (cerr) Date: Wed, 21 Nov 2012 11:26:51 -0800 (PST) Subject: logger doesn't roll over Message-ID: <4a09e161-9185-4bac-905c-828222bdc67e@googlegroups.com> Hi, I'm using Python's logger for logging but it doesn't seem to roll over, my file is now 2.2MB and should've rolled over after ever 1MB. My code: logger = logging.getLogger("") logger.setLevel(logging.DEBUG) handler = logging.handlers.RotatingFileHandler( LOGFILE, maxBytes=(1048576*10), backupCount=5 ) What am I doing wrong here, I don't get it. Thank you! Ron From gelonida at gmail.com Wed Nov 21 14:30:01 2012 From: gelonida at gmail.com (Gelonida N) Date: Wed, 21 Nov 2012 20:30:01 +0100 Subject: windows question: default click action points to wrong python version In-Reply-To: <50ACA030.6090509@timgolden.me.uk> References: <50ACA030.6090509@timgolden.me.uk> Message-ID: Hi Tim, Thanks a lot for your answer. On 11/21/2012 10:34 AM, Tim Golden wrote: > On 21/11/2012 08:23, Gelonida N wrote: >> Hi, >> >> I installed python 2.6 and python 2.7 on a windows 7 machine. >> >> At the moment Python 2.7 is the interpreter being used if I 'start' a >> python script without explicit interpreter. >> >> I always thought, that 'repairing' Python 2.6 (reinstalling it) would >> set the default settings back to Python 2.6. >> >> I also see with assoc / ftypes, that python 2.6. has now been configured >> as default. >> >> However when I click on a script it is still started with 2.7. >> (even after a full restart of the machine) >> >> This is really surprising to me. >> I thought ftype is the command to change file associations. > > > This area is a bit messy. There is a difference between: going to the > command line and typing "myscript.py"; and double-clicking on a file in > Explorer. > > The former uses the result of merging the assoc/ftype registry keys: > > HKEY_CURRENT_USER\Software\Classes\Python.File\shell\open\command > > HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Python.File\shell\open\command > > > while the latter uses the Explorer registry keys at: > > > HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.py\UserChoice > > > Initially, I suppose, the two are in sync. But presumably they can get > out of sync, especially if you move backwards and forwards between > associations. I haven't bothered fishing around in the Shell API but I > presume that you can reconcile the two -- or just edit the registry, > obviously. Hmm I don't mind changing the registry, now that I know the culprit. Will try it tomorrow when being back to my Win PC. > From gwchamb at gwcmail.com Wed Nov 21 14:41:03 2012 From: gwchamb at gwcmail.com (Gary Chambers) Date: Wed, 21 Nov 2012 14:41:03 -0500 (EST) Subject: logger doesn't roll over In-Reply-To: <4a09e161-9185-4bac-905c-828222bdc67e@googlegroups.com> References: <4a09e161-9185-4bac-905c-828222bdc67e@googlegroups.com> Message-ID: Ron, > LOGFILE, maxBytes=(1048576*10), backupCount=5 > What am I doing wrong here, I don't get it. 10 * 1048576 = 10MB -- GC From clp2 at rebertia.com Wed Nov 21 14:46:12 2012 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 21 Nov 2012 11:46:12 -0800 Subject: mysql insert with tuple In-Reply-To: References: Message-ID: On Wed, Nov 21, 2012 at 9:19 AM, Christian wrote: > Hi , > > my purpose is a generic insert via tuple , because the number of fields and can differ. But I'm stucking . > > ilist=['hello',None,7,None,None] > > #This version works, but all varchar fields are in extra '' enclosed. > con.execute(""" INSERT INTO {} VALUES %r; """.format(table) , (tuple(ilist),)) A. "%r" is not a valid SQL/MySQLdb parameter specification (nor part of Python's format() mini-language). B. You don't need to enclose `ilist` in a singleton tuple. (Or convert it to a tuple, for that matter.) This should work: # assuming `table` isn't obtained from untrusted input... paramspec = ",".join(["%s"] * len(ilist)) con.execute("""INSERT INTO {} VALUES {};""".format(table, paramspec) , ilist) But really, I would recommend instead using a more abstract database library (e.g. SQLAlchemy, SQLObject, etc.), which safely and conveniently constructs the SQL strings for you. > #This produce (1054, "Unknown column 'None' in 'field list'"), > #but without None values it works. > con.execute(""" INSERT INTO {} VALUES %r; """.format(table) % (tuple(ilist),)) This is an SQL injection (http://en.wikipedia.org/wiki/SQL_injection ) waiting to happen! Regards, Chris P.S. Where's my mining fact? ;-) From hansmu at xs4all.nl Wed Nov 21 14:49:13 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Wed, 21 Nov 2012 20:49:13 +0100 Subject: mysql insert with tuple In-Reply-To: References: Message-ID: <50ad303a$0$6928$e4fe514c@news2.news.xs4all.nl> On 21/11/12 18:19:15, Christian wrote: > Hi , > > my purpose is a generic insert via tuple , because the number of fields and can differ. But I'm stucking . > > ilist=['hello',None,7,None,None] > > #This version works, but all varchar fields are in extra '' enclosed. > con.execute(""" INSERT INTO {} VALUES %r; """.format(table) , (tuple(ilist),)) > > #This produce (1054, "Unknown column 'None' in 'field list'"), > #but without None values it works. > con.execute(""" INSERT INTO {} VALUES %r; """.format(table) % (tuple(ilist),)) How about: con.execute("""INSERT INTO {} VALUES ({})""" .format(table, ",".join("%s" for _ in ilist)), ilist) Or perhaps break it down into smaller steps: bind_variables = ",".join("%s" for _ in ilist)) query = "INSERT INTO {} VALUES ({})".format(table, bind_variables) con.execute(query, ilist) Hope this helps, -- HansM From ron.eggler at gmail.com Wed Nov 21 14:50:20 2012 From: ron.eggler at gmail.com (Ron Eggler) Date: Wed, 21 Nov 2012 11:50:20 -0800 Subject: logger doesn't roll over In-Reply-To: References: <4a09e161-9185-4bac-905c-828222bdc67e@googlegroups.com> Message-ID: <50AD307C.5070700@gmail.com> Hooops sh*t! I outsmarted myself I guess... :o However, Thanks for the kick GC! Ron Eggler 1804 - 1122 Gilford St. Vancouver, BC V6G 2P5 (778) 230-9442 On 12-11-21 11:41 AM, Gary Chambers wrote: > Ron, > >> LOGFILE, maxBytes=(1048576*10), backupCount=5 >> What am I doing wrong here, I don't get it. > > 10 * 1048576 = 10MB > > -- > GC From rupees001 at gmail.com Wed Nov 21 15:16:13 2012 From: rupees001 at gmail.com (rupees001 at gmail.com) Date: Wed, 21 Nov 2012 12:16:13 -0800 (PST) Subject: Pakistani Girls Mopbile Number For Friendship In-Reply-To: <5f80460b-67f1-4417-b36a-9e53a6af8c35@nl1g2000pbc.googlegroups.com> References: <5f80460b-67f1-4417-b36a-9e53a6af8c35@nl1g2000pbc.googlegroups.com> Message-ID: Try This Website for Mobile Numbers of Girls http://www.pakistanigirls.com.pk/mobile-numbers From drsalists at gmail.com Wed Nov 21 15:18:49 2012 From: drsalists at gmail.com (Dan Stromberg) Date: Wed, 21 Nov 2012 12:18:49 -0800 Subject: Building python, bz2 coming up missing Message-ID: I'm attempting to build cpython 2.{5,6,7} and cpython 3.[0,1,2,3}. I find that having them all around facilitates interversion testing and discovering what works in which versions. Anyway, in 3.3, I'm getting a bz2 module, but in 3.2, I'm not - but only when compiling on Linux Mint 14. On Linux Mint 13, I got a bz2 module in all my builds. In 3.2 on Linux Mint 14, I get the familiar: Python build finished, but the necessary bits to build these modules were not found: _curses _curses_panel _dbm _gdbm _sqlite3 _ssl _tkinter bz2 ossaudiodev readline zlib To find the necessary bits, look in setup.py in detect_modules() for the module's name. Except I definitely installed a bz2 "dev" package. Actually, I also installed gdbm and Berkeley DB dev packages. Any suggestions? Thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: From giacomo.alzetta at gmail.com Wed Nov 21 15:21:46 2012 From: giacomo.alzetta at gmail.com (Giacomo Alzetta) Date: Wed, 21 Nov 2012 12:21:46 -0800 (PST) Subject: Inconsistent behaviour os str.find/str.index when providing optional parameters In-Reply-To: <50ad2a95$0$6907$e4fe514c@news2.news.xs4all.nl> References: <9ecd357d-aaaa-4f4d-a987-a478e92b2052@googlegroups.com> <50ad2a95$0$6907$e4fe514c@news2.news.xs4all.nl> Message-ID: Il giorno mercoled? 21 novembre 2012 20:25:10 UTC+1, Hans Mulder ha scritto: > On 21/11/12 17:59:05, Alister wrote: > > > On Wed, 21 Nov 2012 04:43:57 -0800, Giacomo Alzetta wrote: > > > > > >> I just came across this: > > >> > > >>>>> 'spam'.find('', 5) > > >> -1 > > >> > > >> > > >> Now, reading find's documentation: > > >> > > >>>>> print(str.find.__doc__) > > >> S.find(sub [,start [,end]]) -> int > > >> > > >> Return the lowest index in S where substring sub is found, > > >> such that sub is contained within S[start:end]. Optional arguments > > >> start and end are interpreted as in slice notation. > > >> > > >> Return -1 on failure. > > >> > > >> Now, the empty string is a substring of every string so how can find > > >> fail? > > >> find, from the doc, should be generally be equivalent to > > >> S[start:end].find(substring) + start, except if the substring is not > > >> found but since the empty string is a substring of the empty string it > > >> should never fail. > > >> > > >> Looking at the source code for find(in stringlib/find.h): > > >> > > >> Py_LOCAL_INLINE(Py_ssize_t) > > >> stringlib_find(const STRINGLIB_CHAR* str, Py_ssize_t str_len, > > >> const STRINGLIB_CHAR* sub, Py_ssize_t sub_len, > > >> Py_ssize_t offset) > > >> { > > >> Py_ssize_t pos; > > >> > > >> if (str_len < 0) > > >> return -1; > > >> > > >> I believe it should be: > > >> > > >> if (str_len < 0) > > >> return (sub_len == 0 ? 0 : -1); > > >> > > >> Is there any reason of having this unexpected behaviour or was this > > >> simply overlooked? > > > > > > why would you be searching for an empty string? > > > what result would you expect to get from such a search? > > > > > > In general, if > > > > needle in haystack[ start: ] > > > > return True, then you' expect > > > > haystack.find(needle, start) > > > > to return the smallest i >= start such that > > > > haystack[i:i+len(needle)] == needle > > > > also returns True. > > > > >>> "" in "spam"[5:] > > True > > >>> "spam"[5:5+len("")] == "" > > True > > >>> > > > > So, you'd expect that spam.find("", 5) would return 5. > > > > The only other consistent position would be that "spam"[5:] > > should raise an IndexError, because 5 is an invalid index. > > > > For that matter, I wouldn;t mind if "spam".find(s, 5) were > > to raise an IndexError. But if slicing at position 5 > > proudces an empry string, then .find should be able to > > find that empty string. > > > > -- HansM Exactly! Either string[i:] with i >= len(string) should raise an IndexError or find(string, i) should return i. Anyway, thinking about this inconsistency can be solved in a simpler way and without adding comparson. You simply check the substring length first. If it is 0 you already know that the string is a substring of the given string and you return the "offset", so the two ifs at the beginning of the function ought to be swapped. From python at mrabarnett.plus.com Wed Nov 21 15:58:46 2012 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 21 Nov 2012 20:58:46 +0000 Subject: Inconsistent behaviour os str.find/str.index when providing optional parameters In-Reply-To: <50ad2a95$0$6907$e4fe514c@news2.news.xs4all.nl> References: <9ecd357d-aaaa-4f4d-a987-a478e92b2052@googlegroups.com> <50ad2a95$0$6907$e4fe514c@news2.news.xs4all.nl> Message-ID: <50AD4086.5080306@mrabarnett.plus.com> On 2012-11-21 19:25, Hans Mulder wrote: > On 21/11/12 17:59:05, Alister wrote: >> On Wed, 21 Nov 2012 04:43:57 -0800, Giacomo Alzetta wrote: >> >>> I just came across this: >>> >>>>>> 'spam'.find('', 5) >>> -1 >>> >>> >>> Now, reading find's documentation: >>> >>>>>> print(str.find.__doc__) >>> S.find(sub [,start [,end]]) -> int >>> >>> Return the lowest index in S where substring sub is found, >>> such that sub is contained within S[start:end]. Optional arguments >>> start and end are interpreted as in slice notation. >>> >>> Return -1 on failure. >>> >>> Now, the empty string is a substring of every string so how can find >>> fail? >>> find, from the doc, should be generally be equivalent to >>> S[start:end].find(substring) + start, except if the substring is not >>> found but since the empty string is a substring of the empty string it >>> should never fail. >>> >>> Looking at the source code for find(in stringlib/find.h): >>> >>> Py_LOCAL_INLINE(Py_ssize_t) >>> stringlib_find(const STRINGLIB_CHAR* str, Py_ssize_t str_len, >>> const STRINGLIB_CHAR* sub, Py_ssize_t sub_len, >>> Py_ssize_t offset) >>> { >>> Py_ssize_t pos; >>> >>> if (str_len < 0) >>> return -1; >>> >>> I believe it should be: >>> >>> if (str_len < 0) >>> return (sub_len == 0 ? 0 : -1); >>> >>> Is there any reason of having this unexpected behaviour or was this >>> simply overlooked? >> >> why would you be searching for an empty string? >> what result would you expect to get from such a search? > > > In general, if > > needle in haystack[ start: ] > > return True, then you' expect > > haystack.find(needle, start) > > to return the smallest i >= start such that > > haystack[i:i+len(needle)] == needle > > also returns True. > >>>> "" in "spam"[5:] > True >>>> "spam"[5:5+len("")] == "" > True >>>> > > So, you'd expect that spam.find("", 5) would return 5. > > The only other consistent position would be that "spam"[5:] > should raise an IndexError, because 5 is an invalid index. > > For that matter, I wouldn;t mind if "spam".find(s, 5) were > to raise an IndexError. But if slicing at position 5 > proudces an empry string, then .find should be able to > find that empty string. > You'd expect that given: found = string.find(something, start, end) if 'something' present then the following are true: 0 <= found <= len(string) start <= found <= end (I'm assuming here that 'start' and 'end' have already been adjusted for counting from the end, ie originally they might have been negative values.) The only time that you can have found == len(string) and found == end is when something == "" and start == len(string). From no.email at nospam.invalid Wed Nov 21 16:01:19 2012 From: no.email at nospam.invalid (Paul Rubin) Date: Wed, 21 Nov 2012 13:01:19 -0800 Subject: logger doesn't roll over References: <4a09e161-9185-4bac-905c-828222bdc67e@googlegroups.com> Message-ID: <7x3902ekm8.fsf@ruckus.brouhaha.com> cerr writes: > 2.2MB and should've rolled over after ever 1MB. > LOGFILE, maxBytes=(1048576*10), backupCount=5 1048576*10 is 10MB, not 1MB. From mail at timgolden.me.uk Wed Nov 21 16:14:07 2012 From: mail at timgolden.me.uk (Tim Golden) Date: Wed, 21 Nov 2012 21:14:07 +0000 Subject: windows question: default click action points to wrong python version In-Reply-To: References: Message-ID: <50AD441F.9020300@timgolden.me.uk> On 21/11/2012 20:53, Tony the Tiger wrote: > On Wed, 21 Nov 2012 09:23:00 +0100, Gelonida N wrote: > >> What am I missing? > > The PATH environment variable? Nope. PATH doesn't affect either double-clicking or running a .py file on the command line (unless, obviously, you run it by typing "python myscript.py"). TJG From joshua.landau.ws at gmail.com Wed Nov 21 16:49:52 2012 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 21 Nov 2012 21:49:52 +0000 Subject: Inconsistent behaviour os str.find/str.index when providing optional parameters In-Reply-To: <50AD4086.5080306@mrabarnett.plus.com> References: <9ecd357d-aaaa-4f4d-a987-a478e92b2052@googlegroups.com> <50ad2a95$0$6907$e4fe514c@news2.news.xs4all.nl> <50AD4086.5080306@mrabarnett.plus.com> Message-ID: On 21 November 2012 20:58, MRAB wrote: > On 2012-11-21 19:25, Hans Mulder wrote: > >> On 21/11/12 17:59:05, Alister wrote: >> >>> On Wed, 21 Nov 2012 04:43:57 -0800, Giacomo Alzetta wrote: >>>> >>>> 'spam'.find('', 5) >>>>>>> >>>>>> -1 >>>> >>>> Now, reading find's documentation: >>>> >>>> print(str.find.__doc__) >>>>>>> >>>>>> S.find(sub [,start [,end]]) -> int >>>> >>>> Return the lowest index in S where substring sub is found, >>>> such that sub is contained within S[start:end]. Optional arguments >>>> start and end are interpreted as in slice notation. >>>> >>>> Return -1 on failure. >>> >>> >>> >>> why would you be searching for an empty string? >>> what result would you expect to get from such a search? >>> >> >> In general, if >> >> needle in haystack[ start: ] >> >> return True, then you' expect >> >> haystack.find(needle, start) >> >> to return the smallest i >= start such that >> >> haystack[i:i+len(needle)] == needle >> >> also returns True. >> >> The only other consistent position would be that "spam"[5:] >> should raise an IndexError, because 5 is an invalid index. > > > > You'd expect that given: > > found = string.find(something, start, end) > > if 'something' present then the following are true: > > 0 <= found <= len(string) > > start <= found <= end > I don't think I agree. I think *only* the second holds, but start "defaults" to 0 and end to len(string), which is consistent with Han's method and thus conclusion. Of course this is merely opinion and practicality beats purity, but it seems logical, considering the slicing synonym. Even if functionally this remains unchanged, the docstring should be edited to reflect this oddity. (I'm assuming here that 'start' and 'end' have already been adjusted > for counting from the end, ie originally they might have been negative > values.) > > The only time that you can have found == len(string) and found == end > is when something == "" and start == len(string). > -------------- next part -------------- An HTML attachment was scrubbed... URL: From borkintom at gmail.com Wed Nov 21 16:53:12 2012 From: borkintom at gmail.com (Tom Borkin) Date: Wed, 21 Nov 2012 17:53:12 -0400 Subject: Problem with subprocess.call and windows schtasks In-Reply-To: <50ACA67D.4080406@timgolden.me.uk> References: <50ACA67D.4080406@timgolden.me.uk> Message-ID: On Wed, Nov 21, 2012 at 6:01 AM, Tim Golden wrote: > subprocess.call([ > 'SchTasks', '/Create', > '/SC', 'ONCE', > '/TN', 'test', > '/TR', path, > '/ST', '23:50' > ]) > Thank you. Yes, it was the quoting of "test". Oops :-} Thanks again, Tom -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Wed Nov 21 17:12:53 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 22 Nov 2012 09:12:53 +1100 Subject: Web Frameworks Excessive Complexity In-Reply-To: <50acbe4e$0$29987$c3e8da3$5496439d@news.astraweb.com> References: <50ac31dd$0$29987$c3e8da3$5496439d@news.astraweb.com> <67gun9-kk8.ln1@satorlaser.homedns.org> <50acbe4e$0$29987$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Wed, Nov 21, 2012 at 10:43 PM, Steven D'Aprano wrote: > On Wed, 21 Nov 2012 22:21:23 +1100, Chris Angelico wrote: > >> Counting complexity by giving a score to every statement encourages code >> like this: >> >> def bletch(x,y): >> return x + {"foo":y*2,"bar":x*3+y,"quux":math.sin(y)}.get(mode,0) >> >> instead of: >> >> def bletch(x,y): >> if mode=="foo": return x+y*2 >> if mode=="bar": return x*4+y >> if mode=="quux": return x+math.sin(y) return x >> >> Okay, this is a stupid contrived example, but tell me which of those >> you'd rather work with > > > Am I being paid by the hour or the line? You're on a salary, but management specified some kind of code metrics as a means of recognizing which of their programmers are more productive, and thus who gets promoted. Oh, I'm *so* glad I work in a small company. We've only had one programmer that we "let go" (and actually, it was literally letting him go - he said he was no good, hoping that we'd beg him to stay, and we simply didn't beg him to stay), and the metric of code quality was simply that both my boss and I looked at his code and said that it wasn't good enough. Much simpler. (Though my boss and I have differing views on how many lines of code some things should be. We end up having some rather amusing debates about trivial things like line breaks.) ChrisA From rosuav at gmail.com Wed Nov 21 17:17:30 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 22 Nov 2012 09:17:30 +1100 Subject: Yet another Python textbook In-Reply-To: <50AD0962.5080002@ncf.ca> References: <31a82817-8c9b-4dd2-a468-89d8d081fd1b@googlegroups.com> <50AD0962.5080002@ncf.ca> Message-ID: On Thu, Nov 22, 2012 at 4:03 AM, Colin J. Williams wrote: > On 20/11/2012 4:00 PM, Chris Angelico wrote: >> To the OP: jmf has an unnatural hatred of Python 3.3 and PEP 393 >> strings. Take no notice; the rest of the world sees this as a huge >> advantage. Python is now in a VERY small group of languages (I'm aware >> of just one other) that have absolutely proper Unicode handling *and* >> efficient string handling. >> >> ChrisA >> > It's interesting to see that someone else finds the format function to be a > pain. Perhaps the problem lies with the documentation. Hang on, what? I'm not sure where the format function comes in. I was referring to the underlying representation. That said, though, I'm just glad that %-formatting is staying. It's an extremely expressive string formatting method, and exists in many languages (thanks to C's heritage). Pike's version is insanely powerful, Python's is more like C's, but all three are compact and convenient. str.format(), on the other hand, is flexible. It strikes me as rather more complicated than a string formatting function needs to be, but that may be a cost of its flexibility. ChrisA From andrew at nospam.example.com Wed Nov 21 17:24:06 2012 From: andrew at nospam.example.com (Andrew Cooper) Date: Wed, 21 Nov 2012 22:24:06 +0000 Subject: 10 sec poll - please reply! In-Reply-To: <50ac7c45$0$21790$c3e8da3$76491128@news.astraweb.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <20121120212544.8492f9d565883b2d8fb663c1@lavabit.com> <50ac7c45$0$21790$c3e8da3$76491128@news.astraweb.com> Message-ID: <7wcrs.959991$it2.761830@fx22.am4> On 21/11/2012 07:01, Steven D'Aprano wrote: > On Wed, 21 Nov 2012 17:35:27 +1100, Chris Angelico wrote: > >> And yet, trivial though it may seem, function naming in a permanent API >> is pretty important. Threads like this can be the difference between >> coherent and useful APIs and veritable piles of excrement. > > > "There are only two hard problems in Computer Science: cache invalidation > and naming things." -- Phil Karlton > "counting" is missing from that list of two hard problems. ~Andrew From d at davea.name Wed Nov 21 17:58:37 2012 From: d at davea.name (Dave Angel) Date: Wed, 21 Nov 2012 17:58:37 -0500 Subject: Yet another Python textbook In-Reply-To: References: <31a82817-8c9b-4dd2-a468-89d8d081fd1b@googlegroups.com> <50AD0962.5080002@ncf.ca> Message-ID: <50AD5C9D.9070302@davea.name> On 11/21/2012 05:17 PM, Chris Angelico wrote: > > > > That said, though, I'm just glad that %-formatting is staying. It's an > extremely expressive string formatting method, and exists in many > languages (thanks to C's heritage). Pike's version is insanely > powerful, Python's is more like C's, but all three are compact and > convenient. > > str.format(), on the other hand, is flexible. It strikes me as rather > more complicated than a string formatting function needs to be, but > that may be a cost of its flexibility. > > Some don't realize that one very powerful use for the .format style of working is that it makes localization much more straightforward. With the curly brace approach, one can translate the format string into another language, and if the parameters have to be substituted in another order, it's all in one place. Twenty years ago, I implemented such a thing for our product (C++), for just that reason. I'm sure that by now, the libraries exist somewhere in the C++ stdlibs, or at least in Boost. -- DaveA From ian.g.kelly at gmail.com Wed Nov 21 18:11:15 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 21 Nov 2012 16:11:15 -0700 Subject: Yet another Python textbook In-Reply-To: <50AD5C9D.9070302@davea.name> References: <31a82817-8c9b-4dd2-a468-89d8d081fd1b@googlegroups.com> <50AD0962.5080002@ncf.ca> <50AD5C9D.9070302@davea.name> Message-ID: On Wed, Nov 21, 2012 at 3:58 PM, Dave Angel wrote: > Some don't realize that one very powerful use for the .format style of > working is that it makes localization much more straightforward. With > the curly brace approach, one can translate the format string into > another language, and if the parameters have to be substituted in > another order, it's all in one place. It only matters with positional placeholders, though. You can achieve the same thing using named placeholders, which both major formatting styles support. From joshua.landau.ws at gmail.com Wed Nov 21 18:21:41 2012 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 21 Nov 2012 23:21:41 +0000 Subject: Yet another Python textbook In-Reply-To: References: <31a82817-8c9b-4dd2-a468-89d8d081fd1b@googlegroups.com> <50AD0962.5080002@ncf.ca> Message-ID: On 21 November 2012 22:17, Chris Angelico wrote: > On Thu, Nov 22, 2012 at 4:03 AM, Colin J. Williams wrote: > > On 20/11/2012 4:00 PM, Chris Angelico wrote: > >> To the OP: jmf has an unnatural hatred of Python 3.3 and PEP 393 > >> strings. Take no notice; the rest of the world sees this as a huge > >> advantage. Python is now in a VERY small group of languages (I'm aware > >> of just one other) that have absolutely proper Unicode handling *and* > >> efficient string handling. > >> > >> ChrisA > >> > > It's interesting to see that someone else finds the format function to > be a > > pain. Perhaps the problem lies with the documentation. > > Hang on, what? I'm not sure where the format function comes in. I was > referring to the underlying representation. > > That said, though, I'm just glad that %-formatting is staying. It's an > extremely expressive string formatting method, and exists in many > languages (thanks to C's heritage). Pike's version is insanely > powerful, Python's is more like C's, but all three are compact and > convenient. > > str.format(), on the other hand, is flexible. It strikes me as rather > more complicated than a string formatting function needs to be, but > that may be a cost of its flexibility. > Since we've decided to derail the conversation... "{}".format() is a blessing an "" % () should go. "%" has no relevance to strings, is hard to "get" and has an appalling* syntax. Having two syntaxes just makes things less obvious, and the right choice rarer. str.format is also really easy. I don't understand what makes you disagree. Easy vs easier: >>> "%s %s %s" % (1, 2, 3) '1 2 3' >>> "{} {} {}".format(1, 2, 3) '1 2 3' Easy vs easier: >>> "You have %(spam)s spam and %(eggs)s eggs!" % {"spam": 43, "eggs": 120} 'You have 43 spam and 120 eggs!' >>> "You have {spam} spam and {eggs} eggs!".format(spam=43, eggs=120) >>> "You have {spam} spam and {eggs} eggs!".format(**{"spam": 43, "eggs": 120}) 'You have 43 spam and 120 eggs!' Eh...? vs easy: >>> "Thing %s has state %+o!" % ("#432", 14) 'Thing #432 has state +16! >>> "Thing {} has state {:+o}!".format("#432", 14) 'Thing #432 has state +16!' *Additionally*, a = str.format is much *better* than a = str.__mod__. I have a piece of code like this: "{fuscia}{{category__name}}/{reset}{{name}} {green}{{version}}{reset}:\n {{description}}" Which *would* have looked like this: "%(fuscia)s%%(category__name)s/%(reset)s%%(name)s %(green)s%%(version)s%(reset)s:\n %%(description)s" Which would have parsed to something like: 'FUSCIA{category__name}/RESET{name} GREEN{version}RESET:\n {description}' and 'FUSCIA%(category__name)s/RESET%(name)s GREEN%(version)sRESET:\n %(description)s' Can you seriously say you don't mind the "%(name)s"s in this? * "A {} is in the {}" vs "A %s is in the %s"? -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Wed Nov 21 18:26:02 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 21 Nov 2012 23:26:02 GMT Subject: Yet another Python textbook References: <31a82817-8c9b-4dd2-a468-89d8d081fd1b@googlegroups.com> <50AD0962.5080002@ncf.ca> Message-ID: <50ad630a$0$29987$c3e8da3$5496439d@news.astraweb.com> On Wed, 21 Nov 2012 12:03:30 -0500, Colin J. Williams wrote: > On 20/11/2012 4:00 PM, Chris Angelico wrote: >> To the OP: jmf has an unnatural hatred of Python 3.3 and PEP 393 >> strings. Take no notice; the rest of the world sees this as a huge >> advantage. Python is now in a VERY small group of languages (I'm aware >> of just one other) that have absolutely proper Unicode handling *and* >> efficient string handling. >> >> ChrisA >> > It's interesting to see that someone else finds the format function to > be a pain. Perhaps the problem lies with the documentation. This is nothing to do with the format function. Chris, and JMF, is talking about the internal representation of strings in Python. Python 3.3 fixes a long-running design flaw that causes Unicode strings to be buggy (the use of so-called "surrogate pairs" for characters outside of the Basic Multilingual Plane), *and* saves up to 75% of the memory used by strings (reducing it from up to 4 bytes per character to as little as 1 byte per character), but at the cost of a trivially small slowdown under some circumstances. JMF is obsessed with the idea that Python is destroying Unicode for the benefit of the Americans. It's quite sad really. -- Steven From breamoreboy at yahoo.co.uk Wed Nov 21 18:32:34 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 21 Nov 2012 23:32:34 +0000 Subject: Yet another Python textbook In-Reply-To: References: <31a82817-8c9b-4dd2-a468-89d8d081fd1b@googlegroups.com> <50AD0962.5080002@ncf.ca> Message-ID: On 21/11/2012 23:21, Joshua Landau wrote: > On 21 November 2012 22:17, Chris Angelico wrote: > >> On Thu, Nov 22, 2012 at 4:03 AM, Colin J. Williams wrote: >>> On 20/11/2012 4:00 PM, Chris Angelico wrote: >>>> To the OP: jmf has an unnatural hatred of Python 3.3 and PEP 393 >>>> strings. Take no notice; the rest of the world sees this as a huge >>>> advantage. Python is now in a VERY small group of languages (I'm aware >>>> of just one other) that have absolutely proper Unicode handling *and* >>>> efficient string handling. >>>> >>>> ChrisA >>>> >>> It's interesting to see that someone else finds the format function to >> be a >>> pain. Perhaps the problem lies with the documentation. >> >> Hang on, what? I'm not sure where the format function comes in. I was >> referring to the underlying representation. >> >> That said, though, I'm just glad that %-formatting is staying. It's an >> extremely expressive string formatting method, and exists in many >> languages (thanks to C's heritage). Pike's version is insanely >> powerful, Python's is more like C's, but all three are compact and >> convenient. >> >> str.format(), on the other hand, is flexible. It strikes me as rather >> more complicated than a string formatting function needs to be, but >> that may be a cost of its flexibility. >> > > Since we've decided to derail the conversation... > > "{}".format() is a blessing an "" % () should go. "%" has no relevance to > strings, is hard to "get" and has an appalling* syntax. Having two syntaxes > just makes things less obvious, and the right choice rarer. > > str.format is also really easy. I don't understand what makes you disagree. > > Easy vs easier: > >>>> "%s %s %s" % (1, 2, 3) > '1 2 3' > >>>> "{} {} {}".format(1, 2, 3) > '1 2 3' > > Easy vs easier: > >>>> "You have %(spam)s spam and %(eggs)s eggs!" % {"spam": 43, "eggs": 120} > 'You have 43 spam and 120 eggs!' > >>>> "You have {spam} spam and {eggs} eggs!".format(spam=43, eggs=120) > >>>> "You have {spam} spam and {eggs} eggs!".format(**{"spam": 43, "eggs": > 120}) > 'You have 43 spam and 120 eggs!' > > Eh...? vs easy: > >>>> "Thing %s has state %+o!" % ("#432", 14) > 'Thing #432 has state +16! > >>>> "Thing {} has state {:+o}!".format("#432", 14) > 'Thing #432 has state +16!' > > *Additionally*, a = str.format is much *better* than a = str.__mod__. > > I have a piece of code like this: > "{fuscia}{{category__name}}/{reset}{{name}} {green}{{version}}{reset}:\n > {{description}}" > > Which *would* have looked like this: > "%(fuscia)s%%(category__name)s/%(reset)s%%(name)s > %(green)s%%(version)s%(reset)s:\n %%(description)s" > > Which would have parsed to something like: > 'FUSCIA{category__name}/RESET{name} GREEN{version}RESET:\n {description}' > and > 'FUSCIA%(category__name)s/RESET%(name)s GREEN%(version)sRESET:\n > %(description)s' > > Can you seriously say you don't mind the "%(name)s"s in this? > > * "A {} is in the {}" vs "A %s is in the %s"? > > > C %f style formatting is never going to go so live with it. I know as I asked maybe two years ago. On Python-dev. I think. -- Cheers. Mark Lawrence. From ian.g.kelly at gmail.com Wed Nov 21 19:19:18 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 21 Nov 2012 17:19:18 -0700 Subject: Yet another Python textbook In-Reply-To: References: <31a82817-8c9b-4dd2-a468-89d8d081fd1b@googlegroups.com> <50AD0962.5080002@ncf.ca> Message-ID: On Wed, Nov 21, 2012 at 4:21 PM, Joshua Landau wrote: > "{}".format() is a blessing an "" % () should go. "%" has no relevance to > strings, is hard to "get" and has an appalling* syntax. Having two syntaxes > just makes things less obvious, and the right choice rarer. > > str.format is also really easy. I don't understand what makes you disagree. I think it mostly depends on where you come from as a programmer. As you say, having two syntaxes muddles things up. If you come from C or C++, then the %s syntax feels natural and intuitive, and trying to learn the sort-of-similar-but-not-really {} syntax on top of it is just confusing. Conversely, if you come from Java or C#, then the {} syntax comes naturally, and having to learn %s in addition will give one a headache. And then there are those who come from Lisp and want to know why they can't just use the familiarly easy ~a syntax. None of these are really any easier than the others. But they are sort of similar at a superficial level, which just makes it that much more painful to learn one when you're already accustomed to another. I think my favorite example from the str.format documentation is this one. Apart from demonstrating the flexibility of the format system, it also manages to mix the two systems in a most unholy way: >>> import datetime >>> d = datetime.datetime(2010, 7, 4, 12, 15, 58) >>> '{:%Y-%m-%d %H:%M:%S}'.format(d) '2010-07-04 12:15:58' From alec.taylor6 at gmail.com Wed Nov 21 19:34:42 2012 From: alec.taylor6 at gmail.com (Alec Taylor) Date: Thu, 22 Nov 2012 11:34:42 +1100 Subject: Yet another Python textbook In-Reply-To: References: Message-ID: Dear Prof. Solin, Can you put your website?http://femhub.com/textbook-python/?on your github?https://github.com/femhub/nclab-textbook-python? I will then send you a pull request with a bootstrapped homepage with good UX. All the best, Alec Taylor From drsalists at gmail.com Wed Nov 21 20:43:57 2012 From: drsalists at gmail.com (Dan Stromberg) Date: Wed, 21 Nov 2012 17:43:57 -0800 Subject: Building python, bz2 coming up missing In-Reply-To: References: Message-ID: On Wed, Nov 21, 2012 at 12:18 PM, Dan Stromberg wrote: > > I'm attempting to build cpython 2.{5,6,7} and cpython 3.[0,1,2,3}. I find > that having them all around facilitates interversion testing and > discovering what works in which versions. > > Anyway, in 3.3, I'm getting a bz2 module, but in 3.2, I'm not - but only > when compiling on Linux Mint 14. On Linux Mint 13, I got a bz2 module in > all my builds. > > In 3.2 on Linux Mint 14, I get the familiar: > Python build finished, but the necessary bits to build these modules were > not found: > _curses _curses_panel _dbm > _gdbm _sqlite3 _ssl > _tkinter bz2 ossaudiodev > readline zlib > To find the necessary bits, look in setup.py in detect_modules() for the > module's name. > > Except I definitely installed a bz2 "dev" package. Actually, I also > installed gdbm and Berkeley DB dev packages. > I've worked around the issue with: export LDFLAGS="-L/usr/lib/i386-linux-gnu" ...prior to ./configure, but it'd still be nice to know a way of building things without requiring this. -------------- next part -------------- An HTML attachment was scrubbed... URL: From solin.pavel at gmail.com Wed Nov 21 21:11:28 2012 From: solin.pavel at gmail.com (Pavel Solin) Date: Wed, 21 Nov 2012 18:11:28 -0800 Subject: Yet another Python textbook In-Reply-To: References: Message-ID: Hi Alec, > Can you put your website?http://femhub.com/textbook-python/?on your > github?https://github.com/femhub/nclab-textbook-python? Done, thank you so much. I edited the textbook based on responses that I received. Based on several inquiries we also decided to add Python 3.2 to NCLab. New release is coming in one or two weeks. Cheers, Pavel On Wed, Nov 21, 2012 at 4:34 PM, Alec Taylor wrote: > Dear Prof. Solin, > > Can you put your website?http://femhub.com/textbook-python/?on your > github?https://github.com/femhub/nclab-textbook-python? > > I will then send you a pull request with a bootstrapped homepage with good > UX. > > All the best, > > Alec Taylor > -- Pavel Solin Associate Professor Applied and Computational Mathematics University of Nevada, Reno http://hpfem.org/~pavel -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjreedy at udel.edu Wed Nov 21 22:41:01 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 21 Nov 2012 22:41:01 -0500 Subject: Inconsistent behaviour os str.find/str.index when providing optional parameters In-Reply-To: <50ACD7FB.3060906@mrabarnett.plus.com> References: <9ecd357d-aaaa-4f4d-a987-a478e92b2052@googlegroups.com> <50ACD7FB.3060906@mrabarnett.plus.com> Message-ID: On 11/21/2012 8:32 AM, MRAB wrote: > On 2012-11-21 12:43, Giacomo Alzetta wrote: >> I just came across this: >>> 'spam'.find('') 0 >>> 'spam'.find('', 1) 1 >>> 'spam'.find('', 4) 4 >>>>> 'spam'.find('', 5) >> -1 >> >> >> Now, reading find's documentation: >> >>>>> print(str.find.__doc__) >> S.find(sub [,start [,end]]) -> int >> >> Return the lowest index in S where substring sub is found, >> such that sub is contained within S[start:end]. Optional >> arguments start and end are interpreted as in slice notation. This seems not to be true, as 'spam'[4:] == 'spam'[5:] == '' >> Return -1 on failure. >> >> Now, the empty string is a substring of every string so how can find >> fail? >> find, from the doc, should be generally be equivalent to >> S[start:end].find(substring) + start, except if the substring is not >> found but since the empty string is a substring of the empty string it >> should never fail. >> > [snip] > I think that returning -1 is correct (as far as returning -1 instead of > raising an exception like .index could be considered correct!) because > otherwise it whould be returning a non-existent index. For the string > "spam", the range is 0..4. I tend to agree, but perhaps the doc should be changed. In edge cases like this, there sometimes is no 'right' answer. I suspect that the current behavior is intentional. You might find a discussion on the tracker. -- Terry Jan Reedy From python at mrabarnett.plus.com Wed Nov 21 23:00:38 2012 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 22 Nov 2012 04:00:38 +0000 Subject: Inconsistent behaviour os str.find/str.index when providing optional parameters In-Reply-To: References: <9ecd357d-aaaa-4f4d-a987-a478e92b2052@googlegroups.com> <50ACD7FB.3060906@mrabarnett.plus.com> Message-ID: <50ADA366.5060407@mrabarnett.plus.com> On 2012-11-22 03:41, Terry Reedy wrote: > On 11/21/2012 8:32 AM, MRAB wrote: >> On 2012-11-21 12:43, Giacomo Alzetta wrote: >>> I just came across this: > > >>> 'spam'.find('') > 0 > >>> 'spam'.find('', 1) > 1 > >>> 'spam'.find('', 4) > 4 > >>>>>> 'spam'.find('', 5) >>> -1 >>> >>> >>> Now, reading find's documentation: >>> >>>>>> print(str.find.__doc__) >>> S.find(sub [,start [,end]]) -> int >>> >>> Return the lowest index in S where substring sub is found, >>> such that sub is contained within S[start:end]. Optional >>> arguments start and end are interpreted as in slice notation. > > This seems not to be true, as 'spam'[4:] == 'spam'[5:] == '' > It can't return 5 because 5 isn't an index in 'spam'. It can't return 4 because 4 is below the start index. >>> Return -1 on failure. >>> >>> Now, the empty string is a substring of every string so how can find >>> fail? >>> find, from the doc, should be generally be equivalent to >>> S[start:end].find(substring) + start, except if the substring is not >>> found but since the empty string is a substring of the empty string it >>> should never fail. >>> >> [snip] >> I think that returning -1 is correct (as far as returning -1 instead of >> raising an exception like .index could be considered correct!) because >> otherwise it whould be returning a non-existent index. For the string >> "spam", the range is 0..4. > > I tend to agree, but perhaps the doc should be changed. In edge cases > like this, there sometimes is no 'right' answer. I suspect that the > current behavior is intentional. You might find a discussion on the tracker. > It's a special case, but the Zen has something to say about that! :-) (The empty string is also the only substring which can start at len(S).) From tjreedy at udel.edu Wed Nov 21 23:04:26 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 21 Nov 2012 23:04:26 -0500 Subject: Yet another Python textbook In-Reply-To: References: <31a82817-8c9b-4dd2-a468-89d8d081fd1b@googlegroups.com> <50AD0962.5080002@ncf.ca> Message-ID: On 11/21/2012 6:21 PM, Joshua Landau wrote: > Since we've decided to derail the conversation... > > "{}".format() is a blessing an "" % () should go. "%" has no relevance > to strings, is hard to "get" and has an appalling* syntax. Having two > syntaxes just makes things less obvious, and the right choice rarer. > > str.format is also really easy. I don't understand what makes you disagree. > > Easy vs easier: > > >>> "%s %s %s" % (1, 2, 3) > '1 2 3' > > >>> "{} {} {}".format(1, 2, 3) > '1 2 3' You forgot the cases where % formatting has to be special cased. >>> "The answer is {}.".format(1) 'The answer is 1.' >>> "The answer is {}.".format((1,)) 'The answer is (1,).' >>> "The answer is %s" % 1 'The answer is 1' >>> "The anwser is %s" % (1,) 'The answer is 1' >>> "The answer is %s" % ((1,),) 'The answer is (1,)' -- Terry Jan Reedy From rosuav at gmail.com Thu Nov 22 00:00:46 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 22 Nov 2012 16:00:46 +1100 Subject: re.search when used within an if/else fails In-Reply-To: <2930dc36-9d8f-411d-aa7a-272fe4885ec5@i5g2000yqh.googlegroups.com> References: <50aadbe6$0$29983$c3e8da3$5496439d@news.astraweb.com> <50aadcf0$0$29983$c3e8da3$5496439d@news.astraweb.com> <2930dc36-9d8f-411d-aa7a-272fe4885ec5@i5g2000yqh.googlegroups.com> Message-ID: On Thu, Nov 22, 2012 at 3:41 AM, Kevin T wrote: > I went back and tried version a again, blam it is/does work now ?!?!? > I am not sure what changed but version a was the original code that > wouldn't work. All the examples i had read, showed version a as a > working version. I spent significant time trying version a with > parens, spacing changes, different regex values to no avail. hence > the creation of all the other versions. This is why the Short, Self-Contained, Correct Example is so important. See http://sscce.org/ for some info on that. I often find myself stuck, begin typing up a post to some appropriate mailing list, and while coalescing the problem into its smallest possible form, end up finding the cause directly. More often than not, I end up discarding the message unsent (though sometimes, I end up submitting a patch to the language/library maintainers, eg improving the docs so it's more visible next time). In any case, if you can copy and paste the exact code and error, it's a lot easier for us to replicate your problem. ChrisA From modulok at gmail.com Thu Nov 22 00:22:54 2012 From: modulok at gmail.com (Modulok) Date: Wed, 21 Nov 2012 22:22:54 -0700 Subject: Web Frameworks Excessive Complexity In-Reply-To: References: <50ac31dd$0$29987$c3e8da3$5496439d@news.astraweb.com> <67gun9-kk8.ln1@satorlaser.homedns.org> <50acbe4e$0$29987$c3e8da3$5496439d@news.astraweb.com> Message-ID: > On Wed, Nov 21, 2012 at 10:43 PM, Steven D'Aprano > wrote: >> On Wed, 21 Nov 2012 22:21:23 +1100, Chris Angelico wrote: >> >>> Counting complexity by giving a score to every statement encourages code >>> like this: >>> >>> def bletch(x,y): >>> return x + {"foo":y*2,"bar":x*3+y,"quux":math.sin(y)}.get(mode,0) >>> >>> instead of: >>> >>> def bletch(x,y): >>> if mode=="foo": return x+y*2 >>> if mode=="bar": return x*4+y >>> if mode=="quux": return x+math.sin(y) return x >>> >>> Okay, this is a stupid contrived example, but tell me which of those >>> you'd rather work with >> >> > Oh, I'm *so* glad I work in a small company. Agreed. Do we rate a contractor's quality of workmanship and efficiency by the number of nails he drives? Of course not. That would be ridiculous. A better metric of code quality and complexity would be to borrow from science and mathematics. i.e. a peer review or audit by others working on the project or in the same field of study. Unfortunately this isn't cheap or easily computed and doesn't translate nicely to a bar graph. Such is reality. -Modulok- From dieter at handshake.de Thu Nov 22 01:37:26 2012 From: dieter at handshake.de (Dieter Maurer) Date: Thu, 22 Nov 2012 07:37:26 +0100 Subject: Suitable software stacks for simple python web service References: Message-ID: <87ehjmjg7t.fsf@handshake.de> Kev Dwyer writes: > I have to build a simple web service which will: > > - receive queries from our other servers > - forward the requests to a third party SOAP service > - process the response from the third party > - send the result back to the original requester > > From the point of view of the requester, this will happen within the scope > of a single request. > > The data exchanged with the original requester will likely be encoded as > JSON; the SOAP service will be handled by SUDS. > > The load is likely to be quite light, say a few requests per hour, though > this may increase in the future. > > Given these requirements, what do you think might be a suitable software > stack, i.e. webserver and web framework (if a web framework is even > necessary)? >From your description (so far), you would not need a web framework but could use any way to integrate Python scripts into a web server, e.g. "mod_python", "cgi", "WSGI", .... Check what ways your web server will suport. From kevin.p.dwyer at gmail.com Thu Nov 22 01:42:22 2012 From: kevin.p.dwyer at gmail.com (Kev Dwyer) Date: Thu, 22 Nov 2012 06:42:22 +0000 Subject: Suitable software stacks for simple python web service References: Message-ID: Steve Petrie wrote: > On Wednesday, November 21, 2012 2:32:40 AM UTC-5, Kev Dwyer wrote: >> Hello List, >> >> >> >> I have to build a simple web service which will: >> >> >> >> - receive queries from our other servers >> >> - forward the requests to a third party SOAP service >> >> - process the response from the third party >> >> - send the result back to the original requester >> >> >> >> >From the point of view of the requester, this will happen within the >> >scope >> >> of a single request. >> >> >> >> The data exchanged with the original requester will likely be encoded as >> >> JSON; the SOAP service will be handled by SUDS. >> >> >> >> The load is likely to be quite light, say a few requests per hour, though >> >> this may increase in the future. >> >> >> >> Given these requirements, what do you think might be a suitable software >> >> stack, i.e. webserver and web framework (if a web framework is even >> >> necessary)? >> >> >> >> Candidates should be compatible with Python2.7, though I'd be happy to >> >> consider Python 3 if anyone knows of a Python3 SOAP library that has good >> >> WSDL support. >> >> >> >> Cheers, >> >> >> >> Kev > > I'm using the Bottle web framework (http://bottlepy.org) to integrate > requests and replies originating in a Drupal site, a Beanstream (payment > processor) account, and a Salesforce instance. > > Communication with Salesforce is done through the Salesforce Python > Toolkit (http://code.google.com/p/salesforce-python-toolkit/), which uses > Suds. > > Communication with the Drupal site uses Python's (and PHP's on the Drupal > side) native JSON support. > > This is under Python 2.6.8 and Apache 2.2.23 running on an AWS EC2 > instance. > > No (major) problems so far, though still in the early stages of this > project. > > Steve > > > > I chose Bottle after trying a few other frameworks because, well, I can't > remember exactly why, though thinking back it's probably because of the > clarity of Bottle's approach and the simplicity of the documentation. Hello Steve, Thanks for your comment. I'm curious, did you consider any web servers other than Apache? Kev From giacomo.alzetta at gmail.com Thu Nov 22 02:01:47 2012 From: giacomo.alzetta at gmail.com (Giacomo Alzetta) Date: Wed, 21 Nov 2012 23:01:47 -0800 (PST) Subject: Inconsistent behaviour os str.find/str.index when providing optional parameters In-Reply-To: References: <9ecd357d-aaaa-4f4d-a987-a478e92b2052@googlegroups.com> <50ACD7FB.3060906@mrabarnett.plus.com> Message-ID: <04a8334d-dc5c-4745-814a-5e02e04b1950@googlegroups.com> Il giorno gioved? 22 novembre 2012 05:00:39 UTC+1, MRAB ha scritto: > On 2012-11-22 03:41, Terry Reedy wrote: > It can't return 5 because 5 isn't an index in 'spam'. > > > > It can't return 4 because 4 is below the start index. Uhm. Maybe you are right, because returning a greater value would cause an IndexError, but then, *why* is 4 returned??? >>> 'spam'.find('', 4) 4 >>> 'spam'[4] Traceback (most recent call last): File "", line 1, in IndexError: string index out of range 4 is not a valid index either. I do not think the behaviour was completely intentional. If find should return indexes than 'spam'.find('', 4) must be -1, because 4 is not a valid index. If find should behave as if creating the slice and checking if the substring is in the slice than 'spam'.find('', i) should return i for every integer >= 4. The docstring does not describe this edge case, so I think it could be improved. If the first sentence(being an index in S) is kept, than it shouldn't say that start and end are treated as in slice notation, because that's actually not true. It should be added if start is greater or equal to len(S) then -1 is always returned(and in this case 'spam'.find('', 4) -> -1). If find should not guarantee that the value returned is a valid index(when start isn't a valid index), then the first sentence should be rephrased to avoid giving this idea(and the comparisons in stringlib/find.h should be swapped to have the correct behaviour). For example, maybe, it could be "Return the lowest index where substring sub is found (in S?), such that sub is contained in S[start:end]. ... From giacomo.alzetta at gmail.com Thu Nov 22 02:01:47 2012 From: giacomo.alzetta at gmail.com (Giacomo Alzetta) Date: Wed, 21 Nov 2012 23:01:47 -0800 (PST) Subject: Inconsistent behaviour os str.find/str.index when providing optional parameters In-Reply-To: References: <9ecd357d-aaaa-4f4d-a987-a478e92b2052@googlegroups.com> <50ACD7FB.3060906@mrabarnett.plus.com> Message-ID: <04a8334d-dc5c-4745-814a-5e02e04b1950@googlegroups.com> Il giorno gioved? 22 novembre 2012 05:00:39 UTC+1, MRAB ha scritto: > On 2012-11-22 03:41, Terry Reedy wrote: > It can't return 5 because 5 isn't an index in 'spam'. > > > > It can't return 4 because 4 is below the start index. Uhm. Maybe you are right, because returning a greater value would cause an IndexError, but then, *why* is 4 returned??? >>> 'spam'.find('', 4) 4 >>> 'spam'[4] Traceback (most recent call last): File "", line 1, in IndexError: string index out of range 4 is not a valid index either. I do not think the behaviour was completely intentional. If find should return indexes than 'spam'.find('', 4) must be -1, because 4 is not a valid index. If find should behave as if creating the slice and checking if the substring is in the slice than 'spam'.find('', i) should return i for every integer >= 4. The docstring does not describe this edge case, so I think it could be improved. If the first sentence(being an index in S) is kept, than it shouldn't say that start and end are treated as in slice notation, because that's actually not true. It should be added if start is greater or equal to len(S) then -1 is always returned(and in this case 'spam'.find('', 4) -> -1). If find should not guarantee that the value returned is a valid index(when start isn't a valid index), then the first sentence should be rephrased to avoid giving this idea(and the comparisons in stringlib/find.h should be swapped to have the correct behaviour). For example, maybe, it could be "Return the lowest index where substring sub is found (in S?), such that sub is contained in S[start:end]. ... From mining.facts at gmail.com Thu Nov 22 02:56:23 2012 From: mining.facts at gmail.com (Christian) Date: Wed, 21 Nov 2012 23:56:23 -0800 (PST) Subject: mysql insert with tuple In-Reply-To: <50ad303a$0$6928$e4fe514c@news2.news.xs4all.nl> References: <50ad303a$0$6928$e4fe514c@news2.news.xs4all.nl> Message-ID: <862ca6a1-6b29-411d-916d-ce419656cf8b@googlegroups.com> Am Mittwoch, 21. November 2012 20:49:14 UTC+1 schrieb Hans Mulder: > On 21/11/12 18:19:15, Christian wrote: > > > Hi , > > > > > > my purpose is a generic insert via tuple , because the number of fields and can differ. But I'm stucking . > > > > > > ilist=['hello',None,7,None,None] > > > > > > #This version works, but all varchar fields are in extra '' enclosed. > > > con.execute(""" INSERT INTO {} VALUES %r; """.format(table) , (tuple(ilist),)) > > > > > > #This produce (1054, "Unknown column 'None' in 'field list'"), > > > #but without None values it works. > > > con.execute(""" INSERT INTO {} VALUES %r; """.format(table) % (tuple(ilist),)) > > > > How about: > > > > con.execute("""INSERT INTO {} VALUES ({})""" > > .format(table, ",".join("%s" for _ in ilist)), ilist) > > > > Or perhaps break it down into smaller steps: > > > > bind_variables = ",".join("%s" for _ in ilist)) > > query = "INSERT INTO {} VALUES ({})".format(table, bind_variables) > > con.execute(query, ilist) > > > > > > Hope this helps, > > > > -- HansM Thank you both!. However, for future issues I'll take Chris advise about a ORM into account . It's only a sort of data crunching (offline), so SQL Injection isn't a problem. From steve+comp.lang.python at pearwood.info Thu Nov 22 03:44:21 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 22 Nov 2012 08:44:21 GMT Subject: Inconsistent behaviour os str.find/str.index when providing optional parameters References: <9ecd357d-aaaa-4f4d-a987-a478e92b2052@googlegroups.com> <50ACD7FB.3060906@mrabarnett.plus.com> <04a8334d-dc5c-4745-814a-5e02e04b1950@googlegroups.com> Message-ID: <50ade5e5$0$11104$c3e8da3@news.astraweb.com> On Wed, 21 Nov 2012 23:01:47 -0800, Giacomo Alzetta wrote: > Il giorno gioved? 22 novembre 2012 05:00:39 UTC+1, MRAB ha scritto: >> On 2012-11-22 03:41, Terry Reedy wrote: It can't return 5 because 5 >> isn't an index in 'spam'. >> >> >> >> It can't return 4 because 4 is below the start index. > > Uhm. Maybe you are right, because returning a greater value would cause > an IndexError, but then, *why* is 4 returned??? > >>>> 'spam'.find('', 4) > 4 >>>> 'spam'[4] > Traceback (most recent call last): > File "", line 1, in > IndexError: string index out of range > > 4 is not a valid index either. I do not think the behaviour was > completely intentional. The behaviour is certainly an edge case, but I think it is correct. (Correct or not, it has been the same going all the way back to Python 1.5, before strings even had methods, so it almost certainly will not be changed. Changing the behaviour now will very likely break hundreds, maybe thousands, of Python programs that expect the current behaviour.) Consider your string as a sequence of boxes, with index positions labelled above the string: 0-1-2-3-4 |s|p|a|m| The indexing model is that positions represent where you would cut *between* characters, not the character itself. Slices are the substring between cuts: "spam"[1:3] => "pa" while single indexes return the character to the right of the cut: "spam"[1] => "p" If there is no character to the right of the cut, indexing raises an error. Now, consider "spam".find(substring, start). This should return the number of the first cut immediately to the left of the substring, beginning the search at cut #start. "spam".find("pa", 1) => 1 because cut #1 is immediately to the left of "pa" at index 1. By this logic, "spam".find("", 4) should return 4, because cut #4 is immediately to the left of the empty string. So Python's current behaviour is justified. What about "spam".find("", 5)? Well, if you look at the string with the cuts marked as before: 0-1-2-3-4 |s|p|a|m| you will see that there is no cut #5. Since there is no cut #5, we can't sensibly say we found *anything* there, not even the empty string. If you have four boxes, you can't say that you found anything in the fifth box. I realise that this behaviour clashes somewhat with the slicing rule that says that if the slice indexes go past the end of the string, you get an empty string. But that rule is more for convenience than a fundamental rule about strings. I think there is legitimate room for disagreement about the "right" behaviour here, but backwards compatibility trumps logical correctness here, and it is very unlikely to be changed. > The docstring does not describe this edge case, so I think it could be > improved. If the first sentence(being an index in S) is kept, than it > shouldn't say that start and end are treated as in slice notation, > because that's actually not true. +1 I think that you are right that the documentation needs to be improved. -- Steven From paul at subsignal.org Thu Nov 22 03:53:57 2012 From: paul at subsignal.org (=?UTF-8?B?UGF1bCBLw7ZsbGU=?=) Date: Thu, 22 Nov 2012 09:53:57 +0100 Subject: Constructing JSON data structures from non-string key python dictionaries In-Reply-To: <448f6f44-2387-4a2d-9131-084b3585443e@googlegroups.com> References: <737ef2a9-f2e0-43fe-86a0-199940be2b69@googlegroups.com> <448f6f44-2387-4a2d-9131-084b3585443e@googlegroups.com> Message-ID: Am 21.11.2012 17:04, schrieb hfolch at gmail.com: > Thanks for your reply, but the javascript function expects option > names to be unquoted, otherwise it won't work. Others have shown you how to solve this, but I would like to note that the function does NOT expect JSON but a simple javascript object literal. cheers Paul From thbach at students.uni-mainz.de Thu Nov 22 03:56:52 2012 From: thbach at students.uni-mainz.de (Thomas Bach) Date: Thu, 22 Nov 2012 09:56:52 +0100 Subject: Web Frameworks Excessive Complexity In-Reply-To: <20121121124952.8942ed83a63f916350394256@lavabit.com> References: <20121121094939.2be7ebf0809c1959538f8166@lavabit.com> <20121121105703.83afe07ede3c397cf7529092@lavabit.com> <20121121124952.8942ed83a63f916350394256@lavabit.com> Message-ID: <20121122085652.GA4047@taris.box> On Wed, Nov 21, 2012 at 12:49:52PM -0800, rh wrote: > > wheezy + "myvirtualenv" = 3.3MB > pyramid = 92MB $ mkvirtualenv --no-site-packages -p python2.7 pyramid $ pip install -U distribute $ pip install pyramid $ du -h .virtualenvs/pyramid 22M .virtualenvs/pyramid $ du -s .virtualenvs/pyramid/lib/python2.7/site-packages/* | sort -n | tail -n 5 728 .virtualenvs/pyramid/lib/python2.7/site-packages/pip-1.1-py2.7.egg 1556 .virtualenvs/pyramid/lib/python2.7/site-packages/setuptools 1724 .virtualenvs/pyramid/lib/python2.7/site-packages/zope 2044 .virtualenvs/pyramid/lib/python2.7/site-packages/chameleon 6312 .virtualenvs/pyramid/lib/python2.7/site-packages/pyramid I think 22 MB are OK given the functionality Pyramid has to offer. Just my 2 cents, Thomas. From breamoreboy at yahoo.co.uk Thu Nov 22 04:20:15 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 22 Nov 2012 09:20:15 +0000 Subject: Update slider widget range In-Reply-To: References: Message-ID: On 21/11/2012 13:59, moadeep wrote: > I am trying to write a small bit of code that interactively deletes selected slices in an image series using matplotlib. I have created a button 'delete' which stores a number of indices to be deleted when the button 'update' is selected. However, I am currently unable to reset the range of my slider widget, i.e. removing the number of deleted slices from valmax. What is the pythonic solution to this problem? > > self.nframes = len(self.raw_dicom_stack) If I've read your intentions correctly the line above should read self.slider.valmax = len(self.raw_dicom_stack) If I'm wrong then please ask on the matplotlib users mailing list or possibly stackoverflow. -- Cheers. Mark Lawrence. From kevin.p.dwyer at gmail.com Thu Nov 22 04:28:45 2012 From: kevin.p.dwyer at gmail.com (Kev Dwyer) Date: Thu, 22 Nov 2012 09:28:45 +0000 Subject: Suitable software stacks for simple python web service References: <87ehjmjg7t.fsf@handshake.de> Message-ID: Dieter Maurer wrote: > > From your description (so far), you would not need a web framework > but could use any way to integrate Python scripts into a web server, > e.g. "mod_python", "cgi", "WSGI", .... > Check what ways your web server will suport. Hello Dieter Thanks for your comment. I certainly want a lightweight solution so CGI or one of the micro-frameworks are what I am considering. Cheers Kev From duncan.booth at invalid.invalid Thu Nov 22 06:35:07 2012 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 22 Nov 2012 11:35:07 GMT Subject: Simple Question regarding running .py program References: <22ae6963-6700-4ea7-b396-344d89ead7f2@googlegroups.com> <8085ebf2-9f08-4889-84c9-f786d6776001@googlegroups.com> <5f1bf680-ca10-446e-abbc-d3ba55327f2d@googlegroups.com> <6c3c5341-1fb4-4664-80b8-d3e08687830f@googlegroups.com> <50a42449$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: Grant Edwards wrote: > [1] OK, so I'm am annoyed with them after my Google phone updated to > Android 4.2 this afternoon and the lock-screen clock is now > _physically_painful_ to look at. However, I'm convinced that's > not evil -- just a complete and utter lack of visual design > ability. You can select any other lock screen widget as the default, so why not download some more widgets from Play and choose something different. e.g. "Beautiful Clock Widgets" or "HD Widgets" but there are probably others. To change the default lockscreen widget swipe left from the lockscreen until you get to a page with only a '+', add the new widget there, then long press that widget and drag it to be the rightmost page. Then you should be sorted just so long as you don't have any friends with December birthdays. -- Duncan Booth http://kupuguy.blogspot.com From cjw at ncf.ca Thu Nov 22 07:24:38 2012 From: cjw at ncf.ca (Colin J. Williams) Date: Thu, 22 Nov 2012 07:24:38 -0500 Subject: Yet another Python textbook In-Reply-To: References: <31a82817-8c9b-4dd2-a468-89d8d081fd1b@googlegroups.com> <50AD0962.5080002@ncf.ca> Message-ID: <50AE1986.2080605@ncf.ca> From Yet another Python textbook On 21/11/2012 5:17 PM, Chris Angelico wrote: > On Thu, Nov 22, 2012 at 4:03 AM, Colin J. Williams wrote: >> On 20/11/2012 4:00 PM, Chris Angelico wrote: >>> To the OP: jmf has an unnatural hatred of Python 3.3 and PEP 393 >>> strings. Take no notice; the rest of the world sees this as a huge >>> advantage. Python is now in a VERY small group of languages (I'm aware >>> of just one other) that have absolutely proper Unicode handling *and* >>> efficient string handling. >>> >>> ChrisA >>> >> It's interesting to see that someone else finds the format function to be a >> pain. Perhaps the problem lies with the documentation. > > Hang on, what? I'm not sure where the format function comes in. I was > referring to the underlying representation. The OP wrote: "The absurd flexible string representation has practically borrowed the idea to propose once Python has a teaching tool." I perhaps stretched this to refer specifically on one aspect, formatting in my comment. > > That said, though, I'm just glad that %-formatting is staying. It's an > extremely expressive string formatting method, and exists in many > languages (thanks to C's heritage). Pike's version is insanely > powerful, Python's is more like C's, but all three are compact and > convenient. > > str.format(), on the other hand, is flexible. It strikes me as rather > more complicated than a string formatting function needs to be, but > that may be a cost of its flexibility. > > ChrisA Yes is is complicated. From my reading of the docs, it seems to me that the three following should be equivalent: (a) formattingStr.format(values) with (b) format(values, formattingStr) or (c) tupleOfValues.__format__(formattingStr Example: print('{:-^14f}{:^14d}'.format(-25.61, 95 )) print(format((-25.61, 95), '{:-^14f}{:^14d}')) (-25.61, 95 ).__format__('{:-^14f}{:^14d}') The second fails, perhaps because values can only be a single value. The third fails, the reason is unclear. Steven D'Aprano earlier said that a better diagnostic tool is planned for Python 3.4. Should we retreat to %-formatting for now? Colin W. > From glicerinu at gmail.com Thu Nov 22 10:14:29 2012 From: glicerinu at gmail.com (Marc Aymerich) Date: Thu, 22 Nov 2012 07:14:29 -0800 (PST) Subject: method that can be called from a class and also from an instance Message-ID: Hi, I want to create a method within a class that is able to accept either a class or an instance. class MyClass(object): @magic_decorator def method(param): # param can be MyClass (cls) or an instance of MyClass (self) so I can do something like: instance = MyClass() MyClass.method() instance.method() I guess the way to go is implementing a custom decorator (@magic_decorator in my example), but, how can I know if the method has been called from the class o from an instance? Thank you very much!! From __peter__ at web.de Thu Nov 22 10:51:27 2012 From: __peter__ at web.de (Peter Otten) Date: Thu, 22 Nov 2012 16:51:27 +0100 Subject: method that can be called from a class and also from an instance References: Message-ID: Marc Aymerich wrote: > Hi, > > I want to create a method within a class that is able to accept either a > class or an instance. > > class MyClass(object): > @magic_decorator > def method(param): > # param can be MyClass (cls) or an instance of MyClass (self) > > so I can do something like: > > instance = MyClass() > > MyClass.method() > instance.method() > > I guess the way to go is implementing a custom decorator (@magic_decorator > in my example), but, how can I know if the method has been called from the > class o from an instance? > > Thank you very much!! Why would you overload a method that way? $ cat class_or_inst.py import functools class desc(object): def __init__(self, f): self._f = f def __get__(self, inst=None, class_=None): if inst is not None: return functools.partial(self._f, self=inst) elif class_ is not None: return functools.partial(self._f, class_=class_) raise TypeError("nobody expects the Spanish inquisition") class A(object): @desc def f(self=None, class_=None): if self is not None: return "instance" elif class_ is not None: return "class" return "unknown" $ python -i class_or_inst.py >>> A.f() 'class' >>> A().f() 'instance' >>> A.__dict__["f"].__get__() Traceback (most recent call last): File "", line 1, in File "class_or_inst.py", line 11, in __get__ raise TypeError("nobody expects the Spanish inquisition") TypeError: nobody expects the Spanish inquisition From d at davea.name Thu Nov 22 10:52:56 2012 From: d at davea.name (Dave Angel) Date: Thu, 22 Nov 2012 10:52:56 -0500 Subject: method that can be called from a class and also from an instance In-Reply-To: References: Message-ID: <50AE4A58.2030904@davea.name> On 11/22/2012 10:14 AM, Marc Aymerich wrote: > Hi, > > I want to create a method within a class that is able to accept either a class or an instance. > > class MyClass(object): > @magic_decorator > def method(param): > # param can be MyClass (cls) or an instance of MyClass (self) > > so I can do something like: > > instance = MyClass() > > MyClass.method() > instance.method() > > I guess the way to go is implementing a custom decorator (@magic_decorator in my example), but, how can I know if the method has been called from the class o from an instance? > > Thank you very much!! I haven't tried it, but how about if you do a @classmethod decorator, and then just use isinstance(param, MyClass) ? -- DaveA From thbach at students.uni-mainz.de Thu Nov 22 11:12:42 2012 From: thbach at students.uni-mainz.de (Thomas Bach) Date: Thu, 22 Nov 2012 17:12:42 +0100 Subject: method that can be called from a class and also from an instance In-Reply-To: <50AE4A58.2030904@davea.name> References: <50AE4A58.2030904@davea.name> Message-ID: <20121122161241.GA3772@taris.box> On Thu, Nov 22, 2012 at 10:52:56AM -0500, Dave Angel wrote: > On 11/22/2012 10:14 AM, Marc Aymerich wrote: > > I want to create a method within a class that is able to accept either a class or an instance. > > > > I haven't tried it, but how about if you do a @classmethod decorator, > and then just use isinstance(param, MyClass) ? > This won't work: In [22]: class Foo(object): ....: @classmethod ....: def bar(cls): ....: print repr(cls) ....: In [23]: Foo.bar() In [24]: Foo().bar() Actually help(classmethod) explicitly says so: It can be called either on the class (e.g. C.f()) or on an instance (e.g. C().f()). The instance is ignored except for its class. I think the way to go is via the descriptor protocol[1] as suggested by Peter. Regards, Thomas. Footnotes: [1] http://docs.python.org/3/howto/descriptor.html From d at davea.name Thu Nov 22 11:26:31 2012 From: d at davea.name (Dave Angel) Date: Thu, 22 Nov 2012 11:26:31 -0500 Subject: method that can be called from a class and also from an instance In-Reply-To: <20121122161241.GA3772@taris.box> References: <50AE4A58.2030904@davea.name> <20121122161241.GA3772@taris.box> Message-ID: <50AE5237.209@davea.name> On 11/22/2012 11:12 AM, Thomas Bach wrote: > On Thu, Nov 22, 2012 at 10:52:56AM -0500, Dave Angel wrote: >> On 11/22/2012 10:14 AM, Marc Aymerich wrote: >>> I want to create a method within a class that is able to accept either a class or an instance. >>> >> I haven't tried it, but how about if you do a @classmethod decorator, >> and then just use isinstance(param, MyClass) ? >> > This won't work: > > In [22]: class Foo(object): > ....: @classmethod > ....: def bar(cls): > ....: print repr(cls) > ....: > > In [23]: Foo.bar() > > > In [24]: Foo().bar() > > > Actually help(classmethod) explicitly says so: > > It can be called either on the class (e.g. C.f()) or on an instance > (e.g. C().f()). The instance is ignored except for its class. > OK, thanks. I hadn't tried it, and hadn't noticed that that decorator converts to the class. > > I think the way to go is via the descriptor protocol[1] as suggested > by Peter. > > Regards, > Thomas. > > > Footnotes: > [1] http://docs.python.org/3/howto/descriptor.html > The OP should probably use this link instead, since he's not using Python 3. http://docs.python.org/2.7/howto/descriptor.html Marc: I believe the descriptor stuff has changed in Python 3; I don't use it. But if you've got to do this, and you have to do it in Python 2.x, you'd better use the 2.x documentation. -- DaveA From glicerinu at gmail.com Thu Nov 22 11:33:12 2012 From: glicerinu at gmail.com (Marc Aymerich) Date: Thu, 22 Nov 2012 08:33:12 -0800 (PST) Subject: method that can be called from a class and also from an instance In-Reply-To: References: Message-ID: <65f5132a-0711-4b3d-af0b-e50093807c46@googlegroups.com> On Thursday, November 22, 2012 4:51:30 PM UTC+1, Peter Otten wrote: > Marc Aymerich wrote: > > > > > Hi, > > > > > > I want to create a method within a class that is able to accept either a > > > class or an instance. > > > > > > class MyClass(object): > > > @magic_decorator > > > def method(param): > > > # param can be MyClass (cls) or an instance of MyClass (self) > > > > > > so I can do something like: > > > > > > instance = MyClass() > > > > > > MyClass.method() > > > instance.method() > > > > > > I guess the way to go is implementing a custom decorator (@magic_decorator > > > in my example), but, how can I know if the method has been called from the > > > class o from an instance? > > > > > > Thank you very much!! > > > > Why would you overload a method that way? Yep, it's an strange pattern sure it can be done in a better way but this is the best I can think on, the context is: I'm developing a permission system which can give general permissions for a given class and also specific permissions for a given object. class Node(object): @role def has_perm(instance, user) if is_class(instance): then global perm for user... else: then specific perm for user... > > $ cat class_or_inst.py > > import functools > > > > class desc(object): > > def __init__(self, f): > > self._f = f > > def __get__(self, inst=None, class_=None): > > if inst is not None: > > return functools.partial(self._f, self=inst) > > elif class_ is not None: > > return functools.partial(self._f, class_=class_) > > raise TypeError("nobody expects the Spanish inquisition") > > > > > > > > class A(object): > > @desc > > def f(self=None, class_=None): > > if self is not None: > > return "instance" > > elif class_ is not None: > > return "class" > > return "unknown" > > $ python -i class_or_inst.py > > >>> A.f() > > 'class' > > >>> A().f() > > 'instance' > > >>> A.__dict__["f"].__get__() > > Traceback (most recent call last): > > File "", line 1, in > > File "class_or_inst.py", line 11, in __get__ > > raise TypeError("nobody expects the Spanish inquisition") > > TypeError: nobody expects the Spanish inquisition you are a genius! I've implemented this on my code and it works as expected ! Thanks :D From glicerinu at gmail.com Thu Nov 22 11:33:12 2012 From: glicerinu at gmail.com (Marc Aymerich) Date: Thu, 22 Nov 2012 08:33:12 -0800 (PST) Subject: method that can be called from a class and also from an instance In-Reply-To: References: Message-ID: <65f5132a-0711-4b3d-af0b-e50093807c46@googlegroups.com> On Thursday, November 22, 2012 4:51:30 PM UTC+1, Peter Otten wrote: > Marc Aymerich wrote: > > > > > Hi, > > > > > > I want to create a method within a class that is able to accept either a > > > class or an instance. > > > > > > class MyClass(object): > > > @magic_decorator > > > def method(param): > > > # param can be MyClass (cls) or an instance of MyClass (self) > > > > > > so I can do something like: > > > > > > instance = MyClass() > > > > > > MyClass.method() > > > instance.method() > > > > > > I guess the way to go is implementing a custom decorator (@magic_decorator > > > in my example), but, how can I know if the method has been called from the > > > class o from an instance? > > > > > > Thank you very much!! > > > > Why would you overload a method that way? Yep, it's an strange pattern sure it can be done in a better way but this is the best I can think on, the context is: I'm developing a permission system which can give general permissions for a given class and also specific permissions for a given object. class Node(object): @role def has_perm(instance, user) if is_class(instance): then global perm for user... else: then specific perm for user... > > $ cat class_or_inst.py > > import functools > > > > class desc(object): > > def __init__(self, f): > > self._f = f > > def __get__(self, inst=None, class_=None): > > if inst is not None: > > return functools.partial(self._f, self=inst) > > elif class_ is not None: > > return functools.partial(self._f, class_=class_) > > raise TypeError("nobody expects the Spanish inquisition") > > > > > > > > class A(object): > > @desc > > def f(self=None, class_=None): > > if self is not None: > > return "instance" > > elif class_ is not None: > > return "class" > > return "unknown" > > $ python -i class_or_inst.py > > >>> A.f() > > 'class' > > >>> A().f() > > 'instance' > > >>> A.__dict__["f"].__get__() > > Traceback (most recent call last): > > File "", line 1, in > > File "class_or_inst.py", line 11, in __get__ > > raise TypeError("nobody expects the Spanish inquisition") > > TypeError: nobody expects the Spanish inquisition you are a genius! I've implemented this on my code and it works as expected ! Thanks :D From glicerinu at gmail.com Thu Nov 22 11:34:56 2012 From: glicerinu at gmail.com (Marc Aymerich) Date: Thu, 22 Nov 2012 08:34:56 -0800 (PST) Subject: method that can be called from a class and also from an instance In-Reply-To: References: <50AE4A58.2030904@davea.name> <20121122161241.GA3772@taris.box> Message-ID: <654d5d7e-8280-470a-8384-7d8bb2c2a5f1@googlegroups.com> On Thursday, November 22, 2012 5:26:59 PM UTC+1, Dave Angel wrote: > On 11/22/2012 11:12 AM, Thomas Bach wrote: > > > On Thu, Nov 22, 2012 at 10:52:56AM -0500, Dave Angel wrote: > > >> On 11/22/2012 10:14 AM, Marc Aymerich wrote: > > >>> I want to create a method within a class that is able to accept either a class or an instance. > > >>> > > >> I haven't tried it, but how about if you do a @classmethod decorator, > > >> and then just use isinstance(param, MyClass) ? > > >> > > > This won't work: > > > > > > In [22]: class Foo(object): > > > ....: @classmethod > > > ....: def bar(cls): > > > ....: print repr(cls) > > > ....: > > > > > > In [23]: Foo.bar() > > > > > > > > > In [24]: Foo().bar() > > > > > > > > > Actually help(classmethod) explicitly says so: > > > > > > It can be called either on the class (e.g. C.f()) or on an instance > > > (e.g. C().f()). The instance is ignored except for its class. > > > > > > > OK, thanks. I hadn't tried it, and hadn't noticed that that decorator > > converts to the class. > > > > > > > > I think the way to go is via the descriptor protocol[1] as suggested > > > by Peter. > > > > > > Regards, > > > Thomas. > > > > > > > > > Footnotes: > > > [1] http://docs.python.org/3/howto/descriptor.html > > > > > The OP should probably use this link instead, since he's not using Python 3. > > > > http://docs.python.org/2.7/howto/descriptor.html > > > > Marc: I believe the descriptor stuff has changed in Python 3; I don't > > use it. But if you've got to do this, and you have to do it in Python > > 2.x, you'd better use the 2.x documentation. > thanks for the links Thomas and Dave, I'm going to read this documentation right now, I love to learn this kind of python 'internals' :) From glicerinu at gmail.com Thu Nov 22 11:34:56 2012 From: glicerinu at gmail.com (Marc Aymerich) Date: Thu, 22 Nov 2012 08:34:56 -0800 (PST) Subject: method that can be called from a class and also from an instance In-Reply-To: References: <50AE4A58.2030904@davea.name> <20121122161241.GA3772@taris.box> Message-ID: <654d5d7e-8280-470a-8384-7d8bb2c2a5f1@googlegroups.com> On Thursday, November 22, 2012 5:26:59 PM UTC+1, Dave Angel wrote: > On 11/22/2012 11:12 AM, Thomas Bach wrote: > > > On Thu, Nov 22, 2012 at 10:52:56AM -0500, Dave Angel wrote: > > >> On 11/22/2012 10:14 AM, Marc Aymerich wrote: > > >>> I want to create a method within a class that is able to accept either a class or an instance. > > >>> > > >> I haven't tried it, but how about if you do a @classmethod decorator, > > >> and then just use isinstance(param, MyClass) ? > > >> > > > This won't work: > > > > > > In [22]: class Foo(object): > > > ....: @classmethod > > > ....: def bar(cls): > > > ....: print repr(cls) > > > ....: > > > > > > In [23]: Foo.bar() > > > > > > > > > In [24]: Foo().bar() > > > > > > > > > Actually help(classmethod) explicitly says so: > > > > > > It can be called either on the class (e.g. C.f()) or on an instance > > > (e.g. C().f()). The instance is ignored except for its class. > > > > > > > OK, thanks. I hadn't tried it, and hadn't noticed that that decorator > > converts to the class. > > > > > > > > I think the way to go is via the descriptor protocol[1] as suggested > > > by Peter. > > > > > > Regards, > > > Thomas. > > > > > > > > > Footnotes: > > > [1] http://docs.python.org/3/howto/descriptor.html > > > > > The OP should probably use this link instead, since he's not using Python 3. > > > > http://docs.python.org/2.7/howto/descriptor.html > > > > Marc: I believe the descriptor stuff has changed in Python 3; I don't > > use it. But if you've got to do this, and you have to do it in Python > > 2.x, you'd better use the 2.x documentation. > thanks for the links Thomas and Dave, I'm going to read this documentation right now, I love to learn this kind of python 'internals' :) From saikari78 at gmail.com Thu Nov 22 12:05:49 2012 From: saikari78 at gmail.com (saikari78) Date: Thu, 22 Nov 2012 09:05:49 -0800 (PST) Subject: Constructing JSON data structures from non-string key python dictionaries In-Reply-To: References: <737ef2a9-f2e0-43fe-86a0-199940be2b69@googlegroups.com> <448f6f44-2387-4a2d-9131-084b3585443e@googlegroups.com> Message-ID: <33d001d3-c1d6-4bb3-9448-8781c396f561@googlegroups.com> Thanks for all these very clarifying and useful replies From saikari78 at gmail.com Thu Nov 22 12:05:49 2012 From: saikari78 at gmail.com (saikari78) Date: Thu, 22 Nov 2012 09:05:49 -0800 (PST) Subject: Constructing JSON data structures from non-string key python dictionaries In-Reply-To: References: <737ef2a9-f2e0-43fe-86a0-199940be2b69@googlegroups.com> <448f6f44-2387-4a2d-9131-084b3585443e@googlegroups.com> Message-ID: <33d001d3-c1d6-4bb3-9448-8781c396f561@googlegroups.com> Thanks for all these very clarifying and useful replies From michael.herrmann at getautoma.com Thu Nov 22 13:00:54 2012 From: michael.herrmann at getautoma.com (Michael Herrmann) Date: Thu, 22 Nov 2012 10:00:54 -0800 (PST) Subject: 10 sec poll - please reply! In-Reply-To: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> Message-ID: <818c157c-1395-441d-ad99-955a31b0d523@googlegroups.com> Dear all, thank you for your replies. After experimenting with your suggestions, we have arrived at a solution that we believe fits well with our existing API. However, before we implement this solution, we would like to ask you one last time to sign off on our proposal or raise any serious problems you see with it. We took the fact that naming our one function 'type' was so difficult to name as an indicator that it may be trying to do too many things: On the one hand, it allows you to enter plain text as in `type("Hello World!")`; on the other hand, it lets you press single keys, possibly in combination with control keys as for instance in `type(CTRL + 'a')`. We believe it won't normally be necessary to combine the two. For instance, while you could see what type(CTRL + 'a' + "Hello World!") does, we think you would be more likely to use the two separate calls type(CTRL + 'a') type("Hello World!") One of the main goals of our automation product is that using it should feel like giving instructions to a human being looking over their shoulder at a screen. For this reason, it's very useful for us if the function names in our API are short, if possible without underscores, and close to the vocabulary you would use in an everyday conversation. We hope that by offering an API with this property, we can not only make it easier to use for experienced programmers such as yourself, but also be approachable for people from a less technical background. In our gut feeling, the words apart from `type` that would most normally be used in an everyday conversation to express the three examples I have given in my first mail are: press(CTRL + 'a') enter("Hello World") press(ENTER) We really quite like the word `type`, and a few people here seem to favour it too. In particular, Steven: We're glad you accidentally clicked on our mail. Thank you for your inputs and the great quote by Phil Karlton. We think you were right in everything you said. However, some people seem to be *really* put off when you override a built-in function. Even though of course you can avoid the overriding by saying from automa.api import type *as* ..., (as Tim pointed out) we'd like to avoid irritating those people. For this reason, we would rather not use `type`. Many people here voted for send_keys(...). We agree with Dave and Neil that `type` may have too many uses already. As Chris and MRAB pointed out, 'send_keys' is used in many other automation tools. This makes it intuitive for people with knowledge of such tools. However, as I said above (and should have probably said earlier), we are also trying to reach users from a less technical background. Since these people would not normally use 'send_keys' in an everyday conversion, we are afraid that it would not be an intuitive name for them. A similar argument applies to some extent to our 'type_keys', to our 'generate_keystrokes', Ramit's 'simulate_keypress', 'simulate_key(s)_down', 'send_kb_press', 'fake_typing' and 'send_char(s)' and Tim's 'feedkeys'. We thank you for your suggestions. Hopefully you can also agree with our choice! Some suggestions were very nice, short and pretty unambiguous, such as Dennis' `emit` and particularly Alan's `strike`. However, they're unfortunately also rather rarely used and we'd be afraid that it'd be hard to remember them. Thank you though! A final point that Evan made and that also we find very important is to have verbs in our function names. Our proposed solution is to split what we previously called `type` into two functions, 'press' and 'enter' (proposed by xDog Walker). 'press' could be used to press single keys or combinations of them, at once: press(CTRL + 'a') press(ENTER) To open a menu via the keyboard, you could also supply several key combinations to be pressed, in sequence: press(ALT + 'f', 's') 'enter' on the other hand would be used to enter longer strings of plain text: enter("Hello World!") With a functionality we already have, you could supply an optional 'into' parameter that selects a text field into which the text is entered: enter("test.txt", into="File name") 'enter' currently does involve generating same system events that are fired when pressing (and releasing) sequences of keys. However, we did not want to include this technical detail in the function name - it keeps the name shorter, makes it more intuitive for users from a less technical background and also leaves us to change this implementation detail in the future. These names aren't perfect. As Emile rightly pointed out, several tools distinguish between 'press' and 'release' and a user might wonder how to release a key that was pressed using 'press'. That's an ambiguity that is certainly there, however we hope that once the user has at least seen press(ENTER) it is clear what is meant. Distinguishing between pressing and releasing could we think easily be done with, say hold_down(SHIFT) ... release(SHIFT) Another ambiguity of 'press' that I pointed out in my original mail is that it could also be understood as "pressing a button". The current idea is to raise a ValueError if the user supplies a string that is longer than one character: >>> press("OK") ValueError: 'press' generates keystrokes and can only press single letters at a time. Did you maybe mean click("OK") or press('O', 'K')? What do you think of this solution? I hope anybody read this far. I probably shouldn't have written that much but wanted to do justice to your inputs. Thanks! Michael On Tuesday, November 20, 2012 1:18:38 PM UTC+1, Michael Herrmann wrote: > Hi, > > > > I'm developing a GUI Automation library (http://www.getautoma.com) and am having difficulty picking a name for the function that simulates key strokes. I currently have it as 'type' but that clashes with the built-in function. Example uses of 'type': > > > > type(ENTER) > > > > type("Hello World!") > > > > type(CTRL + 'a') > > > > What, in your view, would be the most intuitive alternative name? > > > > Here are my thoughts so far: I could call it 'press' but then our GUI automation tool also allows you to click things and then "press" might be mistaken for "pressing a button". A less ambiguous alternative is "type_keys" but that is rather long and doesn't read as well, for instance in type_keys(ENTER). > > > > Thank you very much! From giacomo.alzetta at gmail.com Thu Nov 22 13:22:57 2012 From: giacomo.alzetta at gmail.com (Giacomo Alzetta) Date: Thu, 22 Nov 2012 10:22:57 -0800 (PST) Subject: Inconsistent behaviour os str.find/str.index when providing optional parameters In-Reply-To: <50ade5e5$0$11104$c3e8da3@news.astraweb.com> References: <9ecd357d-aaaa-4f4d-a987-a478e92b2052@googlegroups.com> <50ACD7FB.3060906@mrabarnett.plus.com> <04a8334d-dc5c-4745-814a-5e02e04b1950@googlegroups.com> <50ade5e5$0$11104$c3e8da3@news.astraweb.com> Message-ID: <091906a7-7f54-48ae-928f-3acd4f511c43@googlegroups.com> Il giorno gioved? 22 novembre 2012 09:44:21 UTC+1, Steven D'Aprano ha scritto: > On Wed, 21 Nov 2012 23:01:47 -0800, Giacomo Alzetta wrote: > > > > > Il giorno gioved? 22 novembre 2012 05:00:39 UTC+1, MRAB ha scritto: > > >> On 2012-11-22 03:41, Terry Reedy wrote: It can't return 5 because 5 > > >> isn't an index in 'spam'. > > >> > > >> > > >> > > >> It can't return 4 because 4 is below the start index. > > > > > > Uhm. Maybe you are right, because returning a greater value would cause > > > an IndexError, but then, *why* is 4 returned??? > > > > > >>>> 'spam'.find('', 4) > > > 4 > > >>>> 'spam'[4] > > > Traceback (most recent call last): > > > File "", line 1, in > > > IndexError: string index out of range > > > > > > 4 is not a valid index either. I do not think the behaviour was > > > completely intentional. > > > > > > The behaviour is certainly an edge case, but I think it is correct. > > > > (Correct or not, it has been the same going all the way back to Python > > 1.5, before strings even had methods, so it almost certainly will not be > > changed. Changing the behaviour now will very likely break hundreds, > > maybe thousands, of Python programs that expect the current behaviour.) > My point was not to change the behaviour but only to point out this possible inconsistency between what str.find/str.index do and what they claim to do in the documentation. Anyway I'm not so sure that changing the behaviour would break many programs... I mean, the change would only impact code that was looking for an empty string over the string's bounds. I don't see often using the lo and hi parameters for find/index, and I think I never saw someone using them when they get out of bounds. If you add looking for the empty string I think that the number of programs breaking will be minimum. And even if they break, it would be really easy to fix them. Anyway, I understand what you mean and maybe it's better to keep this (at least to me) odd behaviour for backwards compatibility. > > By this logic, "spam".find("", 4) should return 4, because cut #4 is > > immediately to the left of the empty string. So Python's current > > behaviour is justified. > > > > What about "spam".find("", 5)? Well, if you look at the string with the > > cuts marked as before: > > > > 0-1-2-3-4 > > |s|p|a|m| > > > > you will see that there is no cut #5. Since there is no cut #5, we can't > > sensibly say we found *anything* there, not even the empty string. If you > > have four boxes, you can't say that you found anything in the fifth box. > > > > I realise that this behaviour clashes somewhat with the slicing rule that > > says that if the slice indexes go past the end of the string, you get an > > empty string. But that rule is more for convenience than a fundamental > > rule about strings. Yeah, I understand what you say, but the logic you pointed out is never cited anywhere, while slices are cited in the docstring. > > > The docstring does not describe this edge case, so I think it could be > > > improved. If the first sentence(being an index in S) is kept, than it > > > shouldn't say that start and end are treated as in slice notation, > > > because that's actually not true. > > > > +1 > > > > I think that you are right that the documentation needs to be improved. Definitely. The sentence "Optional arguments start and end are interpreted as in slice notation." should be changed to something like: "Optional arguments start and end are interpreted as in slice notation, unless start is (strictly?) greater than the length of S or end is smaller than start, in which cases the search always fails." In this way the 'spam'.find('', 4) *is* documented because start=len(S) -> start and end are treated like in slice notation and 4 makes sense, while 'spam'.find('', 5) -> -1 because 5 > len('spam') and thus the search fails and also 'spam'.find('', 3, 2) -> -1 makes sense because 2 < 3(this edge case makes more sense, even though 'spam'[3:2] is still the empty string...). From ian.g.kelly at gmail.com Thu Nov 22 13:27:10 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 22 Nov 2012 11:27:10 -0700 Subject: Yet another Python textbook In-Reply-To: <50AE1986.2080605@ncf.ca> References: <31a82817-8c9b-4dd2-a468-89d8d081fd1b@googlegroups.com> <50AD0962.5080002@ncf.ca> <50AE1986.2080605@ncf.ca> Message-ID: On Thu, Nov 22, 2012 at 5:24 AM, Colin J. Williams wrote: > From my reading of the docs, it seems to me that the three following should > be equivalent: > > (a) formattingStr.format(values) > with > (b) format(values, formattingStr) > or > (c) tupleOfValues.__format__(formattingStr > > Example: > print('{:-^14f}{:^14d}'.format(-25.61, 95 )) > print(format((-25.61, 95), '{:-^14f}{:^14d}')) > (-25.61, 95 ).__format__('{:-^14f}{:^14d}') > > The second fails, perhaps because values can only be a single value. > The third fails, the reason is unclear. The latter two (which are more or less equivalent) fail because they are intended for invoking the formatting rules of a single value. The string argument to each of them is not a format string, but a "format specification", which in a format string is only the part that goes inside the curly braces and after the optional colon. For example, in this format string: >>> 'Hello world {0!s:_>4s}'.format(42) 'Hello world __42' The format specifier here is "_>4s": >>> format('42', '_>4s') '__42' The valid format specifiers depend upon the type of the object being formatted: >>> format(42, '04x') '002a' >>> format(datetime(2012, 11, 22, 11, 17, 0), 'The time is %Y %d %m %H:%M:%S') 'The time is 2012 22 11 11:17:00' Custom types can implement custom format specifications by overriding the __format__ method: >>> class Foo: ... def __init__(self, value): ... self.value = value ... def __format__(self, spec): ... if spec == 'a': ... return str(self.value) ... if spec == 'b': ... return ''.join(reversed(str(self.value))) ... raise ValueError("Unknown format code {!r}".format(spec)) ... >>> format(Foo(42), 'a') '42' >>> format(Foo(42), 'b') '24' The same format specifications can then also be passed to str.format: >>> '{0:a} reversed is {0:b}'.format(Foo(42)) '42 reversed is 24' Unfortunately, there does not seem to be a good reference to the format specifications available for built-in types beyond basic strings and numbers. I only knew about the datetime example because it is used in an example in the str.format docs. The datetime.__format__ implementation (which seems to be just a thin wrapper of datetime.strftime) does not seem to be documented anywhere in the datetime module docs. From miguelcoam at gmail.com Thu Nov 22 13:44:02 2012 From: miguelcoam at gmail.com (Mike) Date: Thu, 22 Nov 2012 10:44:02 -0800 (PST) Subject: Print value from array Message-ID: <4e2b0991-d090-4040-bb3b-4a4e27eed7a6@googlegroups.com> Hello, I am noob en python programing, i wrote a perl script for read from csv but now i wish print value but the value must be within double quote and I can not do this. For example now the output is: ma user at domain displayName Name SecondName givenName Name sn SecondName cn Name and i wish ma "user at domain" displayName "Name Lastname" givenName "Name" sn "SecondName" cn "Name" My script is #!/usr/bin/python import csv with open ('file.csv', 'rb') as f: reader = csv.reader (f, delimiter=';' ) for row in reader: mail = row [0] name = row [1] lastname = row [2] name2 = row [1] + ' ' + row [2] print 'ma ' + mail + ' displayName ' + name2.title() + ' givenName ' + name.title() + ' sn ' + lastname.title() + ' cn ' + name.title() # print '\n' f.close() From alister.ware at ntlworld.com Thu Nov 22 14:02:30 2012 From: alister.ware at ntlworld.com (Alister) Date: Thu, 22 Nov 2012 19:02:30 GMT Subject: Print value from array References: <4e2b0991-d090-4040-bb3b-4a4e27eed7a6@googlegroups.com> Message-ID: On Thu, 22 Nov 2012 10:44:02 -0800, Mike wrote: > Hello, > I am noob en python programing, i wrote a perl script for read from csv > but now i wish print value but the value must be within double quote and > I can not do this. > > For example now the output is: > > ma user at domain displayName Name SecondName givenName Name sn SecondName > cn Name > > and i wish > > ma "user at domain" displayName "Name Lastname" givenName "Name" sn > "SecondName" cn "Name" > > My script is > > #!/usr/bin/python import csv > > with open ('file.csv', 'rb') as f: > reader = csv.reader (f, delimiter=';' ) > for row in reader: > mail = row [0] > name = row [1] > lastname = row [2] > name2 = row [1] + ' ' + row [2] > > print 'ma ' + mail + ' displayName ' + name2.title() + ' givenName ' > + name.title() + ' sn ' + lastname.title() + ' cn ' + name.title() > > # print '\n' > > f.close() concatenation is not the python way to build strings double quotes can be included in single quoted strings ( & vice Versa) try using the string formatting options, something like print 'ma "{}" display name "{}" Given Name "{}" sn "{}" cn "{}"'.format ((mail,name2.title(),name.title(),lastname.title(),name.title())) -- "Plan to throw one away. You will anyway." - Fred Brooks, "The Mythical Man Month" From rosuav at gmail.com Thu Nov 22 14:08:36 2012 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 23 Nov 2012 06:08:36 +1100 Subject: 10 sec poll - please reply! In-Reply-To: <818c157c-1395-441d-ad99-955a31b0d523@googlegroups.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <818c157c-1395-441d-ad99-955a31b0d523@googlegroups.com> Message-ID: On Fri, Nov 23, 2012 at 5:00 AM, Michael Herrmann wrote: > In our gut feeling, the words apart from `type` that would most normally be used in an everyday conversation to express the three examples I have given in my first mail are: > press(CTRL + 'a') > enter("Hello World") > press(ENTER) Looks fairly good, except for one possible problem: The verb "enter" often means "type this, and then press the Enter key". (For instance, "open up a terminal/shell and enter this command".) Is that likely to be a point of confusion? It's plenty plausible either way. I like the multiple-keystrokes version: > press(ALT + 'f', 's') and the split API does make good sense. ChrisA From michael.herrmann at getautoma.com Thu Nov 22 16:45:34 2012 From: michael.herrmann at getautoma.com (Michael Herrmann) Date: Thu, 22 Nov 2012 13:45:34 -0800 (PST) Subject: 10 sec poll - please reply! In-Reply-To: References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <818c157c-1395-441d-ad99-955a31b0d523@googlegroups.com> Message-ID: Hi, thanks for your prompt reply; I agree that there is also this ambiguity. This would go away if we were to use `type` but as I said we don't dare to do that. That's the problem with short names - they're always ambiguous at least to some extent. The only alleviation I can offer for the valid concern you are raising is that the user will notice upon the very first use that Enter is not pressed after the text has been typed in, and then (if necessary) add a `press(ENTER)` afterwards. It's a pity that `type` is taken... It's very tempting to just use it. But then again you might have people trying to `type(ALT + TAB)`, which in our current proposal can only be input using `press`... What do the others think about this? Cheers On Thursday, November 22, 2012 8:08:39 PM UTC+1, Chris Angelico wrote: > On Fri, Nov 23, 2012 at 5:00 AM, Michael Herrmann > > <...> wrote: > > > In our gut feeling, the words apart from `type` that would most normally be used in an everyday conversation to express the three examples I have given in my first mail are: > > > press(CTRL + 'a') > > > enter("Hello World") > > > press(ENTER) > > > > Looks fairly good, except for one possible problem: The verb "enter" > > often means "type this, and then press the Enter key". (For instance, > > "open up a terminal/shell and enter this command".) Is that likely to > > be a point of confusion? > > > > It's plenty plausible either way. I like the multiple-keystrokes version: > > > press(ALT + 'f', 's') > > and the split API does make good sense. > > > > ChrisA From michael.herrmann at getautoma.com Thu Nov 22 16:45:34 2012 From: michael.herrmann at getautoma.com (Michael Herrmann) Date: Thu, 22 Nov 2012 13:45:34 -0800 (PST) Subject: 10 sec poll - please reply! In-Reply-To: References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <818c157c-1395-441d-ad99-955a31b0d523@googlegroups.com> Message-ID: Hi, thanks for your prompt reply; I agree that there is also this ambiguity. This would go away if we were to use `type` but as I said we don't dare to do that. That's the problem with short names - they're always ambiguous at least to some extent. The only alleviation I can offer for the valid concern you are raising is that the user will notice upon the very first use that Enter is not pressed after the text has been typed in, and then (if necessary) add a `press(ENTER)` afterwards. It's a pity that `type` is taken... It's very tempting to just use it. But then again you might have people trying to `type(ALT + TAB)`, which in our current proposal can only be input using `press`... What do the others think about this? Cheers On Thursday, November 22, 2012 8:08:39 PM UTC+1, Chris Angelico wrote: > On Fri, Nov 23, 2012 at 5:00 AM, Michael Herrmann > > <...> wrote: > > > In our gut feeling, the words apart from `type` that would most normally be used in an everyday conversation to express the three examples I have given in my first mail are: > > > press(CTRL + 'a') > > > enter("Hello World") > > > press(ENTER) > > > > Looks fairly good, except for one possible problem: The verb "enter" > > often means "type this, and then press the Enter key". (For instance, > > "open up a terminal/shell and enter this command".) Is that likely to > > be a point of confusion? > > > > It's plenty plausible either way. I like the multiple-keystrokes version: > > > press(ALT + 'f', 's') > > and the split API does make good sense. > > > > ChrisA From robertmilesxyz at gmail.com Thu Nov 22 17:08:58 2012 From: robertmilesxyz at gmail.com (Robert Miles) Date: Thu, 22 Nov 2012 14:08:58 -0800 (PST) Subject: Spam source (Re: Horror Horror Horror!!!!!) In-Reply-To: References: <2d2a0b98-c587-4459-9489-680b1ddc47c8@googlegroups.com> Message-ID: <0124d021-b712-46a6-8e03-1bff64359cda@googlegroups.com> On Sunday, November 18, 2012 8:18:53 PM UTC-6, Mark Lawrence wrote: > On 18/11/2012 19:31, Terry Reedy wrote: > > > The question was raised as to how much spam comes from googlegroups. > > I don't know the answer but I take the greatest pleasure in hurtling > onto the dread googlegroups and gmane to report spam. Thankfully it's > easy as the amount I receive via gmane is effectively zero. YMMV? It now takes two people reporting the same spam to get google groups to do much about it. I just reported this one as well, though. From robertmilesxyz at gmail.com Thu Nov 22 17:08:58 2012 From: robertmilesxyz at gmail.com (Robert Miles) Date: Thu, 22 Nov 2012 14:08:58 -0800 (PST) Subject: Spam source (Re: Horror Horror Horror!!!!!) In-Reply-To: References: <2d2a0b98-c587-4459-9489-680b1ddc47c8@googlegroups.com> Message-ID: <0124d021-b712-46a6-8e03-1bff64359cda@googlegroups.com> On Sunday, November 18, 2012 8:18:53 PM UTC-6, Mark Lawrence wrote: > On 18/11/2012 19:31, Terry Reedy wrote: > > > The question was raised as to how much spam comes from googlegroups. > > I don't know the answer but I take the greatest pleasure in hurtling > onto the dread googlegroups and gmane to report spam. Thankfully it's > easy as the amount I receive via gmane is effectively zero. YMMV? It now takes two people reporting the same spam to get google groups to do much about it. I just reported this one as well, though. From tjreedy at udel.edu Thu Nov 22 17:12:42 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 22 Nov 2012 17:12:42 -0500 Subject: Yet another Python textbook In-Reply-To: <50AE1986.2080605@ncf.ca> References: <31a82817-8c9b-4dd2-a468-89d8d081fd1b@googlegroups.com> <50AD0962.5080002@ncf.ca> <50AE1986.2080605@ncf.ca> Message-ID: On 11/22/2012 7:24 AM, Colin J. Williams wrote: > From my reading of the docs, it seems to me that the three following > should be equivalent: We read differently... > > (a) formattingStr.format(values) Where 'values' is multiple arguments > with > (b) format(values, formattingStr) "format(value[, format_spec]) Convert a value to a ?formatted? representation, as controlled by format_spec." I notice that you did not pass multiple args, but indeed just one. A 'format_spec' is only part of a {} formatting field. > or > (c) tupleOfValues.__format__(formattingStr >>> tuple.__format__ Which of to say, not specific to tuples. > Example: > print('{:-^14f}{:^14d}'.format(-25.61, 95 )) > print(format((-25.61, 95), '{:-^14f}{:^14d}')) "The interpretation of format_spec will depend on the type of the value argument, however there is a standard formatting syntax that is used by most built-in types: Format Specification Mini-Language." (The latter is link to the FSML. '-^14f' and '^14d' are format_specs. '{:-^14f}{:^14d}' is a format string that includes two fields with format specs. It is not a format spec in itself and is therefore invalid by the doc. > (-25.61, 95 ).__format__('{:-^14f}{:^14d}') > > The second fails, perhaps because values can only be a single value. You only passed one, but you did not pass a format spec and indeed there is none for tuples. As delivered, format specs only format strings and numbers as strings. Collection classes other than str recursively format their members using str() or repr() until they reach strings, numbers, or customs class instances with custom .__format__ methods. > Should we retreat to %-formatting for now? Nonsense. The issues above are the same for % formatting. If you try to format one object with two % format specs, it will fail for the same reason. Try the % equivalent of what failed. '%-14f%14d' % ((-25.61, 95 ),) -- Terry Jan Reedy From cjw at ncf.ca Thu Nov 22 17:41:22 2012 From: cjw at ncf.ca (Colin J. Williams) Date: Thu, 22 Nov 2012 17:41:22 -0500 Subject: Yet another Python textbook In-Reply-To: References: <31a82817-8c9b-4dd2-a468-89d8d081fd1b@googlegroups.com> <50AD0962.5080002@ncf.ca> <50AE1986.2080605@ncf.ca> Message-ID: <50AEAA12.6070603@ncf.ca> On 22/11/2012 1:27 PM, Ian Kelly wrote: > On Thu, Nov 22, 2012 at 5:24 AM, Colin J. Williams wrote: >> From my reading of the docs, it seems to me that the three following should >> be equivalent: >> >> (a) formattingStr.format(values) >> with >> (b) format(values, formattingStr) >> or >> (c) tupleOfValues.__format__(formattingStr >> >> Example: >> print('{:-^14f}{:^14d}'.format(-25.61, 95 )) >> print(format((-25.61, 95), '{:-^14f}{:^14d}')) >> (-25.61, 95 ).__format__('{:-^14f}{:^14d}') >> >> The second fails, perhaps because values can only be a single value. >> The third fails, the reason is unclear. > > The latter two (which are more or less equivalent) fail because they are > intended for invoking the formatting rules of a single value. The > string argument to each of them is not a format string, but a "format > specification", which in a format string is only the part that goes > inside the curly braces and after the optional colon. For example, in > this format string: Thanks, this is clear. I wish the docs made this clearer. You and I used __format__. I understand that the use of double underscore functions is deprecated. Is there some regular function which can achieve the same result? > >>>> 'Hello world {0!s:_>4s}'.format(42) > 'Hello world __42' > > The format specifier here is "_>4s": > >>>> format('42', '_>4s') > '__42' > > The valid format specifiers depend upon the type of the object being formatted: > >>>> format(42, '04x') > '002a' > >>>> format(datetime(2012, 11, 22, 11, 17, 0), 'The time is %Y %d %m %H:%M:%S') > 'The time is 2012 22 11 11:17:00' > > Custom types can implement custom format specifications by overriding > the __format__ method: > >>>> class Foo: > ... def __init__(self, value): > ... self.value = value > ... def __format__(self, spec): > ... if spec == 'a': > ... return str(self.value) > ... if spec == 'b': > ... return ''.join(reversed(str(self.value))) > ... raise ValueError("Unknown format code {!r}".format(spec)) > ... >>>> format(Foo(42), 'a') > '42' >>>> format(Foo(42), 'b') > '24' > > The same format specifications can then also be passed to str.format: > >>>> '{0:a} reversed is {0:b}'.format(Foo(42)) > '42 reversed is 24' > > Unfortunately, there does not seem to be a good reference to the > format specifications available for built-in types beyond basic > strings and numbers. I only knew about the datetime example because > it is used in an example in the str.format docs. The > datetime.__format__ implementation (which seems to be just a thin > wrapper of datetime.strftime) does not seem to be documented anywhere > in the datetime module docs. > From joshua.landau.ws at gmail.com Thu Nov 22 17:53:40 2012 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Thu, 22 Nov 2012 22:53:40 +0000 Subject: Inconsistent behaviour os str.find/str.index when providing optional parameters In-Reply-To: <091906a7-7f54-48ae-928f-3acd4f511c43@googlegroups.com> References: <9ecd357d-aaaa-4f4d-a987-a478e92b2052@googlegroups.com> <50ACD7FB.3060906@mrabarnett.plus.com> <04a8334d-dc5c-4745-814a-5e02e04b1950@googlegroups.com> <50ade5e5$0$11104$c3e8da3@news.astraweb.com> <091906a7-7f54-48ae-928f-3acd4f511c43@googlegroups.com> Message-ID: If you reply through Google Groups, please be careful not to do it the traditional way as us poor saps get hundreds of lines of ">" added in. I believe (but this is mere recollection) that a good way to use the site is by selecting the text you want to quote before replying (even if it is the whole post). I'm not too sure, though. On 22 November 2012 18:22, Giacomo Alzetta wrote: > > > My point was not to change the behaviour but only to point out this > possible inconsistency between what str.find/str.index do and what they > claim to do in the documentation. > > Anyway I'm not so sure that changing the behaviour would break many > programs... I mean, the change would only impact code that was looking for > an empty string over the string's bounds. I don't see often using the lo > and hi parameters for find/index, and I think I never saw someone using > them when they get out of bounds. If you add looking for the empty string I > think that the number of programs breaking will be minimum. And even if > they break, it would be really easy to fix them. > > Anyway, I understand what you mean and maybe it's better to keep this (at > least to me) odd behaviour for backwards compatibility. > > > > Yeah, I understand what you say, but the logic you pointed out is never > cited anywhere, while slices are cited in the docstring. > > > > Definitely. The sentence "Optional > arguments start and end are interpreted as in slice notation." should be > changed to something like: > "Optional arguments start and end are interpreted as in slice notation, > unless start is (strictly?) greater than the length of S or end is smaller > than start, in which cases the search always fails." > > In this way the 'spam'.find('', 4) *is* documented because start=len(S) -> > start and end are treated like in slice notation and 4 makes sense, while > 'spam'.find('', 5) -> -1 because 5 > len('spam') and thus the search fails > and also 'spam'.find('', 3, 2) -> -1 makes sense because 2 < 3(this edge > case makes more sense, even though 'spam'[3:2] is still the empty > string...). +1 -------------- next part -------------- An HTML attachment was scrubbed... URL: From joshua.landau.ws at gmail.com Thu Nov 22 17:56:59 2012 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Thu, 22 Nov 2012 22:56:59 +0000 Subject: Yet another Python textbook In-Reply-To: <50AEAA12.6070603@ncf.ca> References: <31a82817-8c9b-4dd2-a468-89d8d081fd1b@googlegroups.com> <50AD0962.5080002@ncf.ca> <50AE1986.2080605@ncf.ca> <50AEAA12.6070603@ncf.ca> Message-ID: On 22 November 2012 22:41, Colin J. Williams wrote: > On 22/11/2012 1:27 PM, Ian Kelly wrote: > >> On Thu, Nov 22, 2012 at 5:24 AM, Colin J. Williams wrote: >> >>> From my reading of the docs, it seems to me that the three following >>> should >>> be equivalent: >>> >>> (a) formattingStr.format(values) >>> with >>> (b) format(values, formattingStr) >>> or >>> (c) tupleOfValues.__format__(**formattingStr >>> >>> Example: >>> print('{:-^14f}{:^14d}'.**format(-25.61, 95 )) >>> print(format((-25.61, 95), '{:-^14f}{:^14d}')) >>> (-25.61, 95 ).__format__('{:-^14f}{:^14d}'**) >>> >>> The second fails, perhaps because values can only be a single value. >>> The third fails, the reason is unclear. >>> >> >> The latter two (which are more or less equivalent) fail because they are >> intended for invoking the formatting rules of a single value. The >> string argument to each of them is not a format string, but a "format >> specification", which in a format string is only the part that goes >> inside the curly braces and after the optional colon. For example, in >> this format string: >> > > Thanks, this is clear. I wish the docs made this clearer. > > You and I used __format__. I understand that the use of double underscore > functions is deprecated. Is there some regular function which can achieve > the same result? >>> help(format) format(...) format(value[, format_spec]) -> string Returns value.__format__(format_spec) format_spec defaults to "" *In other words, "format(a, b)" is the correct way to write "a.__format__(b)".* This is in the same way that "a.__add__(b)" should be written "a + b", or "a.__round__(b)" written "round(a, b)". -------------- next part -------------- An HTML attachment was scrubbed... URL: From thbach at students.uni-mainz.de Thu Nov 22 19:22:29 2012 From: thbach at students.uni-mainz.de (Thomas Bach) Date: Fri, 23 Nov 2012 01:22:29 +0100 Subject: Managing multiple packages In-Reply-To: <50ABF52B.1080101@wisc.edu> References: <50ABF52B.1080101@wisc.edu> Message-ID: <20121123002229.GA4908@taris.box> On Tue, Nov 20, 2012 at 03:24:59PM -0600, Evan Driscoll wrote: > > Suppose I have packages A-C. In addition to being modules in the Python > sense, they are logically distinct, probably sit in different > repositories, etc., so there's a directory layout like > > [SNIP] > > Finally, suppose that you're changing between editing all three modules. > > > How do you deal with this? I am using virtual environments and do a python setup.py develop on each package. This just creates a symbolic link to the package and all edits show up immediately. I have not come up with a good solution for tox, yet. Hope this helps, Thomas. From miguelcoam at gmail.com Thu Nov 22 20:02:43 2012 From: miguelcoam at gmail.com (Mike) Date: Thu, 22 Nov 2012 17:02:43 -0800 (PST) Subject: Print value from array In-Reply-To: References: <4e2b0991-d090-4040-bb3b-4a4e27eed7a6@googlegroups.com> Message-ID: El jueves, 22 de noviembre de 2012 16:02:30 UTC-3, Alister escribi?: > On Thu, 22 Nov 2012 10:44:02 -0800, Mike wrote: > > > > > Hello, > > > I am noob en python programing, i wrote a perl script for read from csv > > > but now i wish print value but the value must be within double quote and > > > I can not do this. > > > > > > For example now the output is: > > > > > > ma user at domain displayName Name SecondName givenName Name sn SecondName > > > cn Name > > > > > > and i wish > > > > > > ma "user at domain" displayName "Name Lastname" givenName "Name" sn > > > "SecondName" cn "Name" > > > > > > My script is > > > > > > #!/usr/bin/python import csv > > > > > > with open ('file.csv', 'rb') as f: > > > reader = csv.reader (f, delimiter=';' ) > > > for row in reader: > > > mail = row [0] > > > name = row [1] > > > lastname = row [2] > > > name2 = row [1] + ' ' + row [2] > > > > > > print 'ma ' + mail + ' displayName ' + name2.title() + > > ' givenName ' > > > + name.title() + ' sn ' + lastname.title() + ' cn ' + > > name.title() > > > > > > # print '\n' > > > > > > f.close() > > > > concatenation is not the python way to build strings > > double quotes can be included in single quoted strings ( & vice Versa) > > try using the string formatting options, something like > > > > > > print 'ma "{}" display name "{}" Given Name "{}" sn "{}" cn "{}"'.format > > ((mail,name2.title(),name.title(),lastname.title(),name.title())) > > > > > > -- > > "Plan to throw one away. You will anyway." > > - Fred Brooks, "The Mythical Man Month" Thanks Alister , i modify the print structure with your recommendation and is solved Thanks. From steve+comp.lang.python at pearwood.info Thu Nov 22 22:19:49 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 23 Nov 2012 03:19:49 GMT Subject: Spam source (Re: Horror Horror Horror!!!!!) References: <2d2a0b98-c587-4459-9489-680b1ddc47c8@googlegroups.com> <0124d021-b712-46a6-8e03-1bff64359cda@googlegroups.com> Message-ID: <50aeeb55$0$29987$c3e8da3$5496439d@news.astraweb.com> On Thu, 22 Nov 2012 14:08:58 -0800, Robert Miles wrote: > It now takes two people reporting the same spam to get google groups to > do much about it. I just reported this one as well, though. Speaking of spam, googlegroups, and other annoyances, please don't CC python-list at python.org as well as posting to the newsgroup. The newsgroup is already automatically mirrored by the mailing list, and vice versa, so by CCing all you do is needlessly, and annoyingly, duplicate your message. -- Steven From kagard at gmail.com Thu Nov 22 22:19:50 2012 From: kagard at gmail.com (kgard) Date: Thu, 22 Nov 2012 19:19:50 -0800 (PST) Subject: Migrate from Access 2010 / VBA Message-ID: <6db4ed18-a16e-4cd1-940b-c10f51207780@googlegroups.com> Greetings: I am the lone developer of db apps at a company of 350+ employees. Everything is done in MS Access 2010 and VBA. I'm frustrated with the limitations of this platform and have been considering switching to Python. I've been experimenting with the language for a year or so, and feel comfortable with the basics. I am concerned that I'll have a hard time replacing the access form and report designers. I've worked a little with TKinter, but it's a far cry from the GUI designer in Access. Finding a professional grade report designer looks like an even bigger challenge. I don't need to port any applications, but I will need to use the data (mdb/accede format), design a variety of reports with multi-level groupings, and deliver them to many individual recipients via email. Has anyone here made this transition successfully? If so, could you pass along your suggestions about how to do this as quickly and painlessly as possible? TIA Keith From steve+comp.lang.python at pearwood.info Thu Nov 22 22:26:37 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 23 Nov 2012 03:26:37 GMT Subject: Yet another Python textbook References: <31a82817-8c9b-4dd2-a468-89d8d081fd1b@googlegroups.com> <50AD0962.5080002@ncf.ca> <50AE1986.2080605@ncf.ca> <50AEAA12.6070603@ncf.ca> Message-ID: <50aeeced$0$29987$c3e8da3$5496439d@news.astraweb.com> On Thu, 22 Nov 2012 17:41:22 -0500, Colin J. Williams wrote: > You and I used __format__. I understand that the use of double > underscore functions is deprecated. Double leading and trailing underscore methods are not deprecated, they are very much part of the public interface. But they are reserved for Python's use, and under normal conditions you should not be using them by hand. For example: y = x.__add__(1) # NO y = x + 1 # YES if mylist.__len__() > 2: # NO if len(mylist) > 2: # YES > Is there some regular function which can achieve the same result? The built-in format() function is the public API for calling the dunder method __format__. So normally you would write: format(value, spec) instead of value.__format__(spec). -- Steven From dwightdhutto at gmail.com Thu Nov 22 22:33:30 2012 From: dwightdhutto at gmail.com (David Hutto) Date: Thu, 22 Nov 2012 22:33:30 -0500 Subject: Migrate from Access 2010 / VBA In-Reply-To: <6db4ed18-a16e-4cd1-940b-c10f51207780@googlegroups.com> References: <6db4ed18-a16e-4cd1-940b-c10f51207780@googlegroups.com> Message-ID: On Thu, Nov 22, 2012 at 10:19 PM, kgard wrote: > Greetings: > > I am the lone developer of db apps at a company of 350+ employees. Everything is done in MS Access 2010 and VBA. I'm frustrated with the limitations of this platform and have been considering switching to Python. I've been experimenting with the language for a year or so, and feel comfortable with the basics. > > I am concerned that I'll have a hard time replacing the access form and report designers. I've worked a little with TKinter, but it's a far cry from the GUI designer in Access. Finding a professional grade report designer looks like an even bigger challenge. > > I don't need to port any applications, but I will need to use the data (mdb/accede format), design a variety of reports with multi-level groupings, and deliver them to many individual recipients via email. > > Has anyone here made this transition successfully? If so, could you pass along your suggestions about how to do this as quickly and painlessly as possible?http://www.youtube.com/watch?v=DksSPZTZES0 > > TIA > Keith Translate function for function to the new language(return values), then adapt the GUI to represent the new functions on event activity via widgets. -- Best Regards, David Hutto CEO: http://www.hitwebdevelopment.com From alec.taylor6 at gmail.com Thu Nov 22 22:43:53 2012 From: alec.taylor6 at gmail.com (Alec Taylor) Date: Fri, 23 Nov 2012 14:43:53 +1100 Subject: Yet another Python textbook In-Reply-To: References: Message-ID: No worries, I've just sent you my pull-request :) On Thu, Nov 22, 2012 at 1:11 PM, Pavel Solin wrote: > Hi Alec, > >> Can you put your website?http://femhub.com/textbook-python/?on your >> github?https://github.com/femhub/nclab-textbook-python? > > Done, thank you so much. > > I edited the textbook based on responses that I received. Based > on several inquiries we also decided to add Python 3.2 to NCLab. > New release is coming in one or two weeks. > > Cheers, > > Pavel > > > On Wed, Nov 21, 2012 at 4:34 PM, Alec Taylor wrote: >> >> Dear Prof. Solin, >> >> Can you put your website?http://femhub.com/textbook-python/?on your >> github?https://github.com/femhub/nclab-textbook-python? >> >> I will then send you a pull request with a bootstrapped homepage with good >> UX. >> >> All the best, >> >> Alec Taylor > > > > > -- > Pavel Solin > Associate Professor > Applied and Computational Mathematics > University of Nevada, Reno > http://hpfem.org/~pavel > From steve+comp.lang.python at pearwood.info Thu Nov 22 23:23:22 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 23 Nov 2012 04:23:22 GMT Subject: method that can be called from a class and also from an instance References: Message-ID: <50aefa3a$0$29975$c3e8da3$5496439d@news.astraweb.com> On Thu, 22 Nov 2012 07:14:29 -0800, Marc Aymerich wrote: > Hi, > > I want to create a method within a class that is able to accept either a > class or an instance. You want my "dualmethod" decorator: http://code.activestate.com/recipes/577030/ -- Steven From steve+comp.lang.python at pearwood.info Thu Nov 22 23:32:31 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 23 Nov 2012 04:32:31 GMT Subject: method that can be called from a class and also from an instance References: Message-ID: <50aefc5f$0$29975$c3e8da3$5496439d@news.astraweb.com> On Thu, 22 Nov 2012 16:51:27 +0100, Peter Otten wrote: > Marc Aymerich wrote: > >> Hi, >> >> I want to create a method within a class that is able to accept either >> a class or an instance. [...] > Why would you overload a method that way? The use-case I have is that I have a number of classes with default state. Most instances don't override any of the state, so the instances don't add anything except an extra conceptual layer: instance = MyClass() # notice that there are no arguments passed instance.method(args) Since the instances don't have any state except for that already held by the class, they are redundant and pointless. Just knowing the class is enough to specify the behaviour. If I used class methods, I could do this: MyClass.method(args) But here's the thing -- sometimes I *do* have instances that override the default state: instance = MyClass(x, y, z) instance.method(args) Now if method is a class method, my per-instance state is ignored. So I want a method that can be called from the class, and see the default state, or from the instance, and see the per-instance state. Neither classmethod, staticmethod nor ordinary instance methods do the job, but my custom dualmethod does. http://code.activestate.com/recipes/577030/ -- Steven From jason at powerpull.net Thu Nov 22 23:35:21 2012 From: jason at powerpull.net (Jason Friedman) Date: Thu, 22 Nov 2012 21:35:21 -0700 Subject: Migrate from Access 2010 / VBA In-Reply-To: <6db4ed18-a16e-4cd1-940b-c10f51207780@googlegroups.com> References: <6db4ed18-a16e-4cd1-940b-c10f51207780@googlegroups.com> Message-ID: > I am the lone developer of db apps at a company of 350+ employees. Everything is done in MS Access 2010 and VBA. I'm frustrated with the limitations of this platform and have been considering switching to Python. I've been experimenting with the language for a year or so, and feel comfortable with the basics. > > I am concerned that I'll have a hard time replacing the access form and report designers. I've worked a little with TKinter, but it's a far cry from the GUI designer in Access. Finding a professional grade report designer looks like an even bigger challenge. > > I don't need to port any applications, but I will need to use the data (mdb/accede format), design a variety of reports with multi-level groupings, and deliver them to many individual recipients via email. Do the employees need to be able to create their own reports, or do you interview them and create reports based on their requirements? Is there a need to provide slice-and-dice, or is each permutation/view of the data its own report? Are the recipients internal? From steve+comp.lang.python at pearwood.info Fri Nov 23 00:41:35 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 23 Nov 2012 05:41:35 GMT Subject: 10 sec poll - please reply! References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <818c157c-1395-441d-ad99-955a31b0d523@googlegroups.com> Message-ID: <50af0c8f$0$29975$c3e8da3$5496439d@news.astraweb.com> On Thu, 22 Nov 2012 10:00:54 -0800, Michael Herrmann wrote: > We took the fact that naming our one function 'type' was so difficult to > name as an indicator that it may be trying to do too many things: I don't think it is difficult to name at all. > On the one hand, it allows you to enter plain text as in `type("Hello > World!")`; That would be called "typing". > on the other hand, it lets you press single keys, That would be called "typing". > possibly in combination with control keys as for instance in > `type(CTRL + 'a')`. That would be called "prestidigitation". Nah, just kidding. That would also be called "typing". > We believe it won't normally be necessary to combine the two. I can't imagine why you say that. You even go ahead and give a perfectly fine example of combining a control character with plain text. I don't know what operating system you are using, but under Linux, people often use strings of regular characters mixed in with control- or alt- characters. E.g. I in the shell, I might type Alt-B Shift-' END Shift-' to jump backwards one word (Alt-B), insert a double quote mark (Shift-'), jump to the end of the line I am editing (END), and insert another double quote mark. It is a needless restriction to assume that every control character must only be part of a single key press event. I even remember a Mac application back in the early 1990s or late 1980s that used combinations like Ctrl-A Y to perform commands. (Actually, such older Macs didn't have a Control key, they used Command instead, but the principle is the same.) > One of the main goals of our automation product is that using it should > feel like giving instructions to a human being looking over their > shoulder at a screen. In a word processor, I might say "Type Ctrl-A Ctrl-X to cut all the text from the document." rather than "Press Ctrl-A. Now press Ctrl-X." > We really quite like the word `type`, and a few people here seem to > favour it too. In particular, Steven: We're glad you accidentally > clicked on our mail. Thank you for your inputs and the great quote by > Phil Karlton. We think you were right in everything you said. However, > some people seem to be *really* put off when you override a built-in > function. Even though of course you can avoid the overriding by saying > from automa.api import type *as* ..., > (as Tim pointed out) we'd like to avoid irritating those people. For > this reason, we would rather not use `type`. You need to ask yourself, who is your primary audience for your software? Is it ... ? a) non-technical people who aren't very familiar with Python, and might not even know that there is a built-in function also called "type", or care if they do know; b) Python programmers who have embraced the concept of namespaces and have no fear about x.foo clashing with y.foo; c) Python programmers with a superstitious dread of using any name which is not global unique, just in case somebody accidentally shadows one function "foo" with another function "foo". I think it is downright silly to avoid using the descriptive and simple name "type" out of some superstition against re-using names which have been used elsewhere, even in the builtins. If it were possible to be confused by the two types, e.g. if they took the same arguments but did radically different things, then I would accept that it was too dangerous/confusing to re-use the name. Reasonable fears about shadowing and confusion are, well, reasonable. But nobody is going to confuse your use of type as a command: type(some_string) with the built-in use as a function if type(something) is list: MyClass = type(x, y, z) I don't think there is any point in having two functions that do exactly the same thing. Expect your users to develop all sorts of superstitions like "you can only use press() with a single key at a time", and get confused as to when you are supposed to use enter() and when press() (for whatever names you eventually choose). -- Steven From pf at artcom-gmbh.de Fri Nov 23 01:54:14 2012 From: pf at artcom-gmbh.de (Peter Funk) Date: Fri, 23 Nov 2012 07:54:14 +0100 Subject: PyPy 2.0 beta 1 released In-Reply-To: References: Message-ID: <20121123065414.GA25048@artcom0.artcom-gmbh.de> Maciej Fijalkowski wrote 22.11.2012 12:54: > We're pleased to announce the 2.0 beta 1 release of PyPy. ... > It also supports ARM machines running Linux. ... Is it be possible to use PyPy to develop Apps for Android phones and tablets? Or will it be possible to do so in the future? Regards, Peter Funk -- Peter Funk, home: ?Oldenburger Str.86, D-27777 Ganderkesee mobile:+49-179-640-8878 phone:+49-421-20419-0 office: ArtCom GmbH, ?Haferwende 2, D-28357 Bremen, Germany From markcial at gmail.com Fri Nov 23 02:25:15 2012 From: markcial at gmail.com (markcial at gmail.com) Date: Thu, 22 Nov 2012 23:25:15 -0800 (PST) Subject: Data manipulation tool built with bottle and redis Message-ID: Hi, i have little expertise on python and redis, but i started a project on github, i'm looking for any guidelines, help, or advices, hope you don't mind my noob coding. https://github.com/Markcial/BottledJinn From stefan_ml at behnel.de Fri Nov 23 02:45:40 2012 From: stefan_ml at behnel.de (Stefan Behnel) Date: Fri, 23 Nov 2012 08:45:40 +0100 Subject: Python on Android (was: PyPy 2.0 beta 1 released) In-Reply-To: <20121123065414.GA25048@artcom0.artcom-gmbh.de> References: <20121123065414.GA25048@artcom0.artcom-gmbh.de> Message-ID: Peter Funk, 23.11.2012 07:54: > Is it be possible to use PyPy to develop Apps for Android phones and tablets? > Or will it be possible to do so in the future? You can use CPython and kivy for that. Nik Klever gave a quick intro to Python on Android at this year's PyCon-DE: http://pyvideo.org/video/1443/erfahrungen-mit-py4a-sowohl-mit-sl4a-als-auch He's using it for teaching purposes. Stefan From __peter__ at web.de Fri Nov 23 03:52:25 2012 From: __peter__ at web.de (Peter Otten) Date: Fri, 23 Nov 2012 09:52:25 +0100 Subject: method that can be called from a class and also from an instance References: <50aefc5f$0$29975$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Thu, 22 Nov 2012 16:51:27 +0100, Peter Otten wrote: > >> Marc Aymerich wrote: >> >>> Hi, >>> >>> I want to create a method within a class that is able to accept either >>> a class or an instance. > [...] >> Why would you overload a method that way? > > > The use-case I have is that I have a number of classes with default > state. Most instances don't override any of the state, so the instances > don't add anything except an extra conceptual layer: > > instance = MyClass() # notice that there are no arguments passed > instance.method(args) > > Since the instances don't have any state except for that already held by > the class, they are redundant and pointless. Just knowing the class is > enough to specify the behaviour. If I used class methods, I could do this: > > MyClass.method(args) > > > But here's the thing -- sometimes I *do* have instances that override the > default state: > > instance = MyClass(x, y, z) > instance.method(args) > > Now if method is a class method, my per-instance state is ignored. So I > want a method that can be called from the class, and see the default > state, or from the instance, and see the per-instance state. Neither > classmethod, staticmethod nor ordinary instance methods do the job, but > my custom dualmethod does. > > http://code.activestate.com/recipes/577030/ Am I reading that right that you don't invoke method() as MyClass.method()? Then I'd probably use class attributes to store the default state and shade them by instance attributes as needed. class A: state = "default" def __init__(self, state=None): if state is not None: self.state = state def method(self): return self.state assert A().method() == "default" assert A("special").method() == "special" The same idea might work for the OP, too (but I'm not sure it's a good idea): class B: def inst_f(self): return "instance" @classmethod def f(class_): return "class" def __init__(self): self.f = self.inst_f assert B.f() == "class" assert B().f() == "instance" From michael.herrmann at getautoma.com Fri Nov 23 04:08:06 2012 From: michael.herrmann at getautoma.com (Michael Herrmann) Date: Fri, 23 Nov 2012 01:08:06 -0800 (PST) Subject: 10 sec poll - please reply! In-Reply-To: <50af0c8f$0$29975$c3e8da3$5496439d@news.astraweb.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <818c157c-1395-441d-ad99-955a31b0d523@googlegroups.com> <50af0c8f$0$29975$c3e8da3$5496439d@news.astraweb.com> Message-ID: Hi Steven, On Friday, November 23, 2012 6:41:35 AM UTC+1, Steven D'Aprano wrote: > On Thu, 22 Nov 2012 10:00:54 -0800, Michael Herrmann wrote: > > > > > We took the fact that naming our one function 'type' was so difficult to > > > name as an indicator that it may be trying to do too many things: > > > > I don't think it is difficult to name at all. > > > > > On the one hand, it allows you to enter plain text as in `type("Hello > > > World!")`; > > > > That would be called "typing". I agree that "typing" might be more common in this context. However, you also understand me when I say "enter". > > > > > on the other hand, it lets you press single keys, > > > > That would be called "typing". Here, I disagree. You "press" enter and you "press" ALT+TAB. You might be able to say "type ENTER" but that is much less common. Google agrees: http://bit.ly/10o8yAQ vs. http://bit.ly/WmVwyU. > > > > > possibly in combination with control keys as for instance in > > > `type(CTRL + 'a')`. > > > > That would be called "prestidigitation". > > > > Nah, just kidding. That would also be called "typing". Here too Google favours "press ctrl+a" over "type ctrl+a". > > > > > > > > > We believe it won't normally be necessary to combine the two. > > > > I can't imagine why you say that. You even go ahead and give a perfectly > > fine example of combining a control character with plain text. > > > > I don't know what operating system you are using, but under Linux, people > > often use strings of regular characters mixed in with control- or alt- > > characters. E.g. I in the shell, I might type Alt-B Shift-' END Shift-' > > to jump backwards one word (Alt-B), insert a double quote mark (Shift-'), > > jump to the end of the line I am editing (END), and insert another double > > quote mark. Unfortunately, I didn't explain what I mean by "plain text". Your example could be implemented with the "press" from our proposal, as follows: press(ALT + 'B', '"', END, '"') What I meant when I said that "press" could not be used to enter plain text was that it would not be possible to enter a sequence of multiple normal letters enclosed in single quotes, as in press("Hello World") If in your example you had wanted to add more than just a single character to the beginning or end of the line, this means you would have to write press(ALT + 'B') enter("beginning of line") press(END) enter("end of line") I agree that the following would read better here: press(ALT + 'B') type("beginning of line") press(END) type("end of line") However like Google above, I would disagree with type(ALT + 'B') type("beginning of line") type(END) type("end of line") or even type(ALT + 'B' + "beginning of line" + END + "end of line") > > > > It is a needless restriction to assume that every control character must > > only be part of a single key press event. I even remember a Mac > > application back in the early 1990s or late 1980s that used combinations > > like Ctrl-A Y to perform commands. (Actually, such older Macs didn't have > > a Control key, they used Command instead, but the principle is the same.) > > > > > > > One of the main goals of our automation product is that using it should > > > feel like giving instructions to a human being looking over their > > > shoulder at a screen. > > > > In a word processor, I might say > > > > "Type Ctrl-A Ctrl-X to cut all the text from the document." > > > > rather than > > > > "Press Ctrl-A. Now press Ctrl-X." > With the current proposal, it'd be press(CTRL + 'A', CTRL + 'X') Forgetting about `type` vs `press` for a moment, chaining the key combinations like this is OK, isn't it? > > > > We really quite like the word `type`, and a few people here seem to > > > favour it too. In particular, Steven: We're glad you accidentally > > > clicked on our mail. Thank you for your inputs and the great quote by > > > Phil Karlton. We think you were right in everything you said. However, > > > some people seem to be *really* put off when you override a built-in > > > function. Even though of course you can avoid the overriding by saying > > > from automa.api import type *as* ..., > > > (as Tim pointed out) we'd like to avoid irritating those people. For > > > this reason, we would rather not use `type`. > > > > You need to ask yourself, who is your primary audience for your software? > > > > Is it ... ? > > > > a) non-technical people who aren't very familiar with Python, and might > > not even know that there is a built-in function also called "type", or > > care if they do know; > > > > b) Python programmers who have embraced the concept of namespaces and > > have no fear about x.foo clashing with y.foo; > > > > c) Python programmers with a superstitious dread of using any name which > > is not global unique, just in case somebody accidentally shadows one > > function "foo" with another function "foo". > I agree that this is an important question to ask. however we unfortunately cannot answer it yet. We think our software should be usable by people from all three groups, but which group will use it the most we don't know yet. > > I think it is downright silly to avoid using the descriptive and simple > > name "type" out of some superstition against re-using names which have > > been used elsewhere, even in the builtins. > > > > If it were possible to be confused by the two types, e.g. if they took > > the same arguments but did radically different things, then I would > > accept that it was too dangerous/confusing to re-use the name. Reasonable > > fears about shadowing and confusion are, well, reasonable. But nobody is > > going to confuse your use of type as a command: > > > > type(some_string) > > > > with the built-in use as a function > > > > if type(something) is list: > > MyClass = type(x, y, z) Actually, when I first read your example, I was confused. I guess it's because the two different meanings of `type` were so close together I still had the first in mind when encountering the second. Nevertheless, it did confuse me. That's actually a key point: You are not confused or irritated by giving `type` a new meaning, and you have very valid reasons why. However, several people we asked in other places were surprised or irritated. You have very good points, but we won't get a chance to explain them to these other users when they first see our API. As the API designers, we have to try to find a solution that's acceptable for most people, and will therefore not be perfect for everyone. > > I don't think there is any point in having two functions that do exactly > > the same thing. Expect your users to develop all sorts of superstitions > > like "you can only use press() with a single key at a time", and get > > confused as to when you are supposed to use enter() and when press() (for > > whatever names you eventually choose). > I agree that it's not good to have two functions do exactly the same thing. However, it also has to be pointed out that it's not good for one function to do too many things. An example like type(ALT + 'B' + "beginning of line" + END + "end of line") imho tries to do too much in one go. With things like this, that you could not forbid with having only one function `type` that does everything, you would soon run into problems like "does it now press ALT all the time, or just for the first 'B'? Then your syntax/API pretty quickly explodes and you end up having to add some form of bracketing, escape sequences etc etc. That's something the splitting should hopefully avoid. Again, it's a long(ish) mail, and that's because it's very interesting to bounce our ideas off of you. Thank you for giving us a chance to do this! Michael From __peter__ at web.de Fri Nov 23 04:08:19 2012 From: __peter__ at web.de (Peter Otten) Date: Fri, 23 Nov 2012 10:08:19 +0100 Subject: Migrate from Access 2010 / VBA References: <6db4ed18-a16e-4cd1-940b-c10f51207780@googlegroups.com> Message-ID: kgard wrote: > Greetings: > > I am the lone developer of db apps at a company of 350+ employees. > Everything is done in MS Access 2010 and VBA. I'm frustrated with the > limitations of this platform and have been considering switching to > Python. I've been experimenting with the language for a year or so, and > feel comfortable with the basics. > > I am concerned that I'll have a hard time replacing the access form and > report designers. I've worked a little with TKinter, but it's a far cry > from the GUI designer in Access. Finding a professional grade report > designer looks like an even bigger challenge. > > I don't need to port any applications, but I will need to use the data > (mdb/accede format), design a variety of reports with multi-level > groupings, and deliver them to many individual recipients via email. > > Has anyone here made this transition successfully? If so, could you pass > along your suggestions about how to do this as quickly and painlessly as > possible? These guys are coming from Foxpro, but I'd expect a huge overlap of the problem spaces: http://dabodev.com/ (Disclaimer: I have not tried Dabo myself) From hansmu at xs4all.nl Fri Nov 23 04:22:13 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Fri, 23 Nov 2012 10:22:13 +0100 Subject: Print value from array In-Reply-To: <4e2b0991-d090-4040-bb3b-4a4e27eed7a6@googlegroups.com> References: <4e2b0991-d090-4040-bb3b-4a4e27eed7a6@googlegroups.com> Message-ID: <50af4045$0$6848$e4fe514c@news2.news.xs4all.nl> On 22/11/12 19:44:02, Mike wrote: > Hello, > I am noob en python programing, i wrote a perl script for read from csv but now i wish print value but the value must be within double quote and I can not do this. > > For example now the output is: > > ma user at domain displayName Name SecondName givenName Name sn SecondName cn Name > > and i wish > > ma "user at domain" displayName "Name Lastname" givenName "Name" sn "SecondName" cn "Name" > > My script is > > #!/usr/bin/python > import csv > > with open ('file.csv', 'rb') as f: > reader = csv.reader (f, delimiter=';' ) > for row in reader: > mail = row [0] > name = row [1] > lastname = row [2] > name2 = row [1] + ' ' + row [2] > > print 'ma ' + mail + ' displayName ' + name2.title() + ' givenName ' + name.title() + ' sn ' + lastname.title() + ' cn ' + name.title() > > # print '\n' > > f.close() How about: #!/usr/bin/python import csv with open('file.csv', 'rb') as f: reader = csv.reader(f, delimiter=';' ) for mail, firstname, lastname in reader: fullname = firstname + ' ' + lastname print 'ma "%s"' % mail, print 'displayname "%s"' % fullname.title(), print 'givenName "%s"' % firstname.title(), print 'sn "%s"' % lastname.title(), print 'cn "%s"' % firstname.title() f.close() -- HansM From esandin at gmail.com Fri Nov 23 07:14:25 2012 From: esandin at gmail.com (esandin at gmail.com) Date: Fri, 23 Nov 2012 04:14:25 -0800 (PST) Subject: Pass parameters/globals to eval Message-ID: <3c4c436e-4278-4643-b8db-a7f1f27300c6@googlegroups.com> I am trying to set the parameter 'a' below so that it can be used when I call eval: >>> def gp_function(): ... return 1+a ... >>> print eval("gp_function()", {'a':123, 'gp_function':gp_function}) Traceback (most recent call last): File "", line 1, in File "", line 1, in File "", line 2, in a_method NameError: global name 'a' is not defined Why isn't 'a' defined? Shouldn't you be able to define the global variables with a dict passed to eval? Is there an other way to do this, beside the two obvious: defining 'a' before calling gp_function and using a as an argument in gp_function? In case you are wondering why I want this: This is a simplification of a problem I ran into when experimenting with pyevolve. After running pyevolve: GTreeGP.getCompiledCode() I get some compiled code, and need to pass some arguments to it. Perhaps there is an easier way to do it... Kind Regards Emil From rosuav at gmail.com Fri Nov 23 07:24:36 2012 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 23 Nov 2012 23:24:36 +1100 Subject: Pass parameters/globals to eval In-Reply-To: <3c4c436e-4278-4643-b8db-a7f1f27300c6@googlegroups.com> References: <3c4c436e-4278-4643-b8db-a7f1f27300c6@googlegroups.com> Message-ID: On Fri, Nov 23, 2012 at 11:14 PM, wrote: > Why isn't 'a' defined? > Shouldn't you be able to define the global variables with a dict passed to eval? > Is there an other way to do this, beside the two obvious: defining 'a' before calling gp_function and using a as an argument in gp_function? It is defined, but not in the context of the called function. You defined that function in a particular scope, which then becomes its global scope. (I'm handwaving a lot of details here. Bear with me.) When you eval a bit of code, you define the global scope for _that code_, but not what it calls. Calling gp_function from inside there switches to the new global scope and off it goes. Normally, I'd recommend your second option, passing a as an argument. It's flexible, clear, doesn't rely on fancy names and hidden state. But to answer your actual question: Yes, there is another way to do it. In all probability, gp_function is actually defined at module scope (I'm guessing here but it seems likely based on your description). Simply assign to the module's namespace before calling it - it'll "see" that as a global. ChrisA From steve+comp.lang.python at pearwood.info Fri Nov 23 08:06:37 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 23 Nov 2012 13:06:37 GMT Subject: method that can be called from a class and also from an instance References: <50aefc5f$0$29975$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50af74dc$0$29975$c3e8da3$5496439d@news.astraweb.com> On Fri, 23 Nov 2012 09:52:25 +0100, Peter Otten wrote: > Steven D'Aprano wrote: >> http://code.activestate.com/recipes/577030/ > > Am I reading that right that you don't invoke method() as > MyClass.method()? No. I give an example and explicitly state: You can use this class without instantiating: Example.method('else') # returns 'something else' > Then I'd probably use class attributes to store the > default state and shade them by instance attributes as needed. > > class A: > state = "default" > def __init__(self, state=None): > if state is not None: > self.state = state > def method(self): return self.state That doesn't allow me to call A.method(). On the other hand, if method were a class method, then I could say A.method() or A(state).method, but in both cases I would get the default. So that isn't suitable. [...] > The same idea might work for the OP, too (but I'm not sure it's a good > idea): > > class B: This needs to be a new-style class unless you're using Python 3. > def inst_f(self): > return "instance" > @classmethod > def f(class_): > return "class" > def __init__(self): > self.f = self.inst_f > > assert B.f() == "class" > assert B().f() == "instance" Without studying that in detail, it looks like that would be an alternative solution to the same problem. The downsides are: - you have two distinct but almost identical implementations of method "f", one called "f" and one called "inst_f"; - it works by shadowing method "f" in the instance, which may strike many people as too tricky for production software. Me personally, I think the first objection is critical. Having to write the same method twice, with subtle differences, is inviting bugs. -- Steven From jarausch at skynet.be Fri Nov 23 08:15:37 2012 From: jarausch at skynet.be (Helmut Jarausch) Date: 23 Nov 2012 13:15:37 GMT Subject: python3.3 - tk_setPalette bug? Message-ID: <50af76f9$0$3109$ba620e4c@news.skynet.be> Hi, AFAIK, this should work: import tkinter as Tk root= Tk.Tk() root.tk_setPalette(background = 'AntiqueWhite1', foreground = 'blue') but python-3.3:0e4574595674+ gives Traceback (most recent call last): File "Matr_Select.py", line 174, in root.tk_setPalette(background = 'AntiqueWhite1', foreground = 'blue') File "/usr/lib64/python3.3/tkinter/__init__.py", line 390, in tk_setPalette + _flatten(args) + _flatten(kw.items())) TypeError: argument must be sequence What am I missing? Thanks, Helmut. From michael.herrmann at getautoma.com Fri Nov 23 08:42:22 2012 From: michael.herrmann at getautoma.com (Michael Herrmann) Date: Fri, 23 Nov 2012 05:42:22 -0800 (PST) Subject: 10 sec poll - please reply! In-Reply-To: <818c157c-1395-441d-ad99-955a31b0d523@googlegroups.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <818c157c-1395-441d-ad99-955a31b0d523@googlegroups.com> Message-ID: <780646bd-4caf-4080-b549-cf30a6df0fdf@googlegroups.com> Dear all, the emails are getting kind of long so to ask you briefly: What do you think of splitting `type` into two functions `press` and `enter`? Their use cases are: press(CTRL + 'a') press(ENTER) press(ALT + 'f', 's') enter("Hello World!") enter("test.txt", into="File name") Thanks, Michael On Thursday, November 22, 2012 7:00:55 PM UTC+1, Michael Herrmann wrote: > Dear all, > > > > thank you for your replies. After experimenting with your suggestions, we have arrived at a solution that we believe fits well with our existing API. However, before we implement this solution, we would like to ask you one last time to sign off on our proposal or raise any serious problems you see with it. > > > > We took the fact that naming our one function 'type' was so difficult to name as an indicator that it may be trying to do too many things: On the one hand, it allows you to enter plain text as in `type("Hello World!")`; on the other hand, it lets you press single keys, possibly in combination with control keys as for instance in `type(CTRL + 'a')`. We believe it won't normally be necessary to combine the two. For instance, while you could see what > > type(CTRL + 'a' + "Hello World!") > > does, we think you would be more likely to use the two separate calls > > type(CTRL + 'a') > > type("Hello World!") > > > > One of the main goals of our automation product is that using it should feel like giving instructions to a human being looking over their shoulder at a screen. For this reason, it's very useful for us if the function names in our API are short, if possible without underscores, and close to the vocabulary you would use in an everyday conversation. We hope that by offering an API with this property, we can not only make it easier to use for experienced programmers such as yourself, but also be approachable for people from a less technical background. > > > > In our gut feeling, the words apart from `type` that would most normally be used in an everyday conversation to express the three examples I have given in my first mail are: > > press(CTRL + 'a') > > enter("Hello World") > > press(ENTER) > > > > We really quite like the word `type`, and a few people here seem to favour it too. In particular, Steven: We're glad you accidentally clicked on our mail. Thank you for your inputs and the great quote by Phil Karlton. We think you were right in everything you said. However, some people seem to be *really* put off when you override a built-in function. Even though of course you can avoid the overriding by saying > > from automa.api import type *as* ..., > > (as Tim pointed out) we'd like to avoid irritating those people. For this reason, we would rather not use `type`. > > > > Many people here voted for send_keys(...). We agree with Dave and Neil that `type` may have too many uses already. As Chris and MRAB pointed out, 'send_keys' is used in many other automation tools. This makes it intuitive for people with knowledge of such tools. However, as I said above (and should have probably said earlier), we are also trying to reach users from a less technical background. Since these people would not normally use 'send_keys' in an everyday conversion, we are afraid that it would not be an intuitive name for them. A similar argument applies to some extent to our 'type_keys', to our 'generate_keystrokes', Ramit's 'simulate_keypress', 'simulate_key(s)_down', 'send_kb_press', 'fake_typing' and 'send_char(s)' and Tim's 'feedkeys'. We thank you for your suggestions. Hopefully you can also agree with our choice! > > > > Some suggestions were very nice, short and pretty unambiguous, such as Dennis' `emit` and particularly Alan's `strike`. However, they're unfortunately also rather rarely used and we'd be afraid that it'd be hard to remember them. Thank you though! > > > > A final point that Evan made and that also we find very important is to have verbs in our function names. > > > > Our proposed solution is to split what we previously called `type` into two functions, 'press' and 'enter' (proposed by xDog Walker). 'press' could be used to press single keys or combinations of them, at once: > > press(CTRL + 'a') > > press(ENTER) > > To open a menu via the keyboard, you could also supply several key combinations to be pressed, in sequence: > > press(ALT + 'f', 's') > > 'enter' on the other hand would be used to enter longer strings of plain text: > > enter("Hello World!") > > With a functionality we already have, you could supply an optional 'into' parameter that selects a text field into which the text is entered: > > enter("test.txt", into="File name") > > 'enter' currently does involve generating same system events that are fired when pressing (and releasing) sequences of keys. However, we did not want to include this technical detail in the function name - it keeps the name shorter, makes it more intuitive for users from a less technical background and also leaves us to change this implementation detail in the future. > > > > These names aren't perfect. As Emile rightly pointed out, several tools distinguish between 'press' and 'release' and a user might wonder how to release a key that was pressed using 'press'. That's an ambiguity that is certainly there, however we hope that once the user has at least seen > > press(ENTER) > > it is clear what is meant. Distinguishing between pressing and releasing could we think easily be done with, say > > hold_down(SHIFT) > > ... > > release(SHIFT) > > Another ambiguity of 'press' that I pointed out in my original mail is that it could also be understood as "pressing a button". The current idea is to raise a ValueError if the user supplies a string that is longer than one character: > > >>> press("OK") > > ValueError: 'press' generates keystrokes and can only press single letters at a time. Did you maybe mean click("OK") or press('O', 'K')? > > > > What do you think of this solution? I hope anybody read this far. I probably shouldn't have written that much but wanted to do justice to your inputs. > > > > Thanks! > > > > Michael > > > > On Tuesday, November 20, 2012 1:18:38 PM UTC+1, Michael Herrmann wrote: > > > Hi, > > > > > > > > > > > > I'm developing a GUI Automation library (http://www.getautoma.com) and am having difficulty picking a name for the function that simulates key strokes. I currently have it as 'type' but that clashes with the built-in function. Example uses of 'type': > > > > > > > > > > > > type(ENTER) > > > > > > > > > > > > type("Hello World!") > > > > > > > > > > > > type(CTRL + 'a') > > > > > > > > > > > > What, in your view, would be the most intuitive alternative name? > > > > > > > > > > > > Here are my thoughts so far: I could call it 'press' but then our GUI automation tool also allows you to click things and then "press" might be mistaken for "pressing a button". A less ambiguous alternative is "type_keys" but that is rather long and doesn't read as well, for instance in type_keys(ENTER). > > > > > > > > > > > > Thank you very much! From __peter__ at web.de Fri Nov 23 09:23:54 2012 From: __peter__ at web.de (Peter Otten) Date: Fri, 23 Nov 2012 15:23:54 +0100 Subject: python3.3 - tk_setPalette bug? References: <50af76f9$0$3109$ba620e4c@news.skynet.be> Message-ID: Helmut Jarausch wrote: > Hi, > > AFAIK, this should work: > > import tkinter as Tk > root= Tk.Tk() > root.tk_setPalette(background = 'AntiqueWhite1', foreground = 'blue') > > but python-3.3:0e4574595674+ gives > > Traceback (most recent call last): > File "Matr_Select.py", line 174, in > root.tk_setPalette(background = 'AntiqueWhite1', foreground = 'blue') > File "/usr/lib64/python3.3/tkinter/__init__.py", line 390, in > tk_setPalette > + _flatten(args) + _flatten(kw.items())) > TypeError: argument must be sequence > > > What am I missing? Nothing, as far as I can tell. Please file a bug report on . For now you can work around the bug with root.tk_setPalette("background", 'AntiqueWhite1', "foreground", 'blue') From runar.tenfjord at gmail.com Fri Nov 23 10:00:39 2012 From: runar.tenfjord at gmail.com (Runar Tenfjord) Date: Fri, 23 Nov 2012 15:00:39 +0000 (UTC) Subject: ANN : occmodel v0.1.0 Message-ID: I am pleased to announce the first official release of occmodel (v0.1.0) and the releated libraries geotools/gltools. Description ---------- occmodel is a small library which gives a high level access to the OpenCASCADE modelling kernel. For most users a direct use of the OpenCASCADE modelling kernel can be quite a hurdle as it is a huge library. The geometry can be visualized with the included viewer. This viewer is utilizing modern OpenGL methods like GLSL shaders and vertex buffers to ensure visual quality and maximum speed. To use the viewer OpenGL version 2.1 is needed. Home page : http://github.com/tenko/occmodel Documentation : http://tenko.github.com/occmodel/index.html In addition the following related libraries are released: geotools (required) : http://github.com/tenko/geotools Documentation : http://tenko.github.com/geotools/index.html gltools (optional) : http://github.com/tenko/gltools Documentation : http://tenko.github.com/gltools/index.html As this is the first official release some hurdles are expected Binary installers are available for the Windows platform. Best regards Runar Tenfjord From bv8bv8bv8 at gmail.com Fri Nov 23 11:03:11 2012 From: bv8bv8bv8 at gmail.com (BV BV) Date: Fri, 23 Nov 2012 08:03:11 -0800 (PST) Subject: who is that...?!! Message-ID: who is that...?!! http://www.youtube.com/watch?v=SIADfS030qg&feature=BFa&list=PLB95C1C59E12FBA96 thank you From michael.herrmann at getautoma.com Fri Nov 23 11:12:38 2012 From: michael.herrmann at getautoma.com (Michael Herrmann) Date: Fri, 23 Nov 2012 08:12:38 -0800 (PST) Subject: Is it bad style to override the built-in function `type`? Message-ID: Hi, do you think it's bad style to override the built-in function `type`? I'm co-developing a GUI automation library called Automa (http://www.getautoma.com) and 'type' would be a very fitting name for a function that generates artificial key strokes. This post is motivated by an already lengthy discussion on this mailing list (http://bit.ly/10aOy4H), where we tried to find alternative names for `type`. Many were found, but none are quite as fitting as 'type'. For the sake of avoiding a discussion that is already being lead elsewhere please confine this thread to what you generally think about overriding `type`, and post suggestions for alternative names or solutions in the other thread. Thank you very much! Michael From michael.herrmann at getautoma.com Fri Nov 23 11:15:19 2012 From: michael.herrmann at getautoma.com (Michael Herrmann) Date: Fri, 23 Nov 2012 08:15:19 -0800 (PST) Subject: 10 sec poll - please reply! In-Reply-To: References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <818c157c-1395-441d-ad99-955a31b0d523@googlegroups.com> <50af0c8f$0$29975$c3e8da3$5496439d@news.astraweb.com> Message-ID: <67873dd4-aba0-4539-b275-0a1a0ed6a68d@googlegroups.com> Hi again, Steven's points and the "feeling" for `type` are very good and maybe the problems I mentioned can be ramified. I therefore opened a new thread to find out what the general public thinks about overwriting built-in functions such as `type` here: https://groups.google.com/forum/?fromgroups=#!topic/comp.lang.python/GjZ2hAS1Wyk Thanks, Michael On Friday, November 23, 2012 10:08:06 AM UTC+1, Michael Herrmann wrote: > Hi Steven, > > > > On Friday, November 23, 2012 6:41:35 AM UTC+1, Steven D'Aprano wrote: > > > On Thu, 22 Nov 2012 10:00:54 -0800, Michael Herrmann wrote: > > > > > > > > > > > > > We took the fact that naming our one function 'type' was so difficult to > > > > > > > name as an indicator that it may be trying to do too many things: > > > > > > > > > > > > I don't think it is difficult to name at all. > > > > > > > > > > > > > On the one hand, it allows you to enter plain text as in `type("Hello > > > > > > > World!")`; > > > > > > > > > > > > That would be called "typing". > > > > I agree that "typing" might be more common in this context. However, you also understand me when I say "enter". > > > > > > > > > > > > > > > on the other hand, it lets you press single keys, > > > > > > > > > > > > That would be called "typing". > > > > Here, I disagree. You "press" enter and you "press" ALT+TAB. You might be able to say "type ENTER" but that is much less common. Google agrees: http://bit.ly/10o8yAQ vs. http://bit.ly/WmVwyU. > > > > > > > > > > > > > > > possibly in combination with control keys as for instance in > > > > > > > `type(CTRL + 'a')`. > > > > > > > > > > > > That would be called "prestidigitation". > > > > > > > > > > > > Nah, just kidding. That would also be called "typing". > > > > Here too Google favours "press ctrl+a" over "type ctrl+a". > > > > > > > > > > > > > > > > > > > > > > > > > > > We believe it won't normally be necessary to combine the two. > > > > > > > > > > > > I can't imagine why you say that. You even go ahead and give a perfectly > > > > > > fine example of combining a control character with plain text. > > > > > > > > > > > > I don't know what operating system you are using, but under Linux, people > > > > > > often use strings of regular characters mixed in with control- or alt- > > > > > > characters. E.g. I in the shell, I might type Alt-B Shift-' END Shift-' > > > > > > to jump backwards one word (Alt-B), insert a double quote mark (Shift-'), > > > > > > jump to the end of the line I am editing (END), and insert another double > > > > > > quote mark. > > > > Unfortunately, I didn't explain what I mean by "plain text". Your example could be implemented with the "press" from our proposal, as follows: > > press(ALT + 'B', '"', END, '"') > > What I meant when I said that "press" could not be used to enter plain text was that it would not be possible to enter a sequence of multiple normal letters enclosed in single quotes, as in > > press("Hello World") > > If in your example you had wanted to add more than just a single character to the beginning or end of the line, this means you would have to write > > press(ALT + 'B') > > enter("beginning of line") > > press(END) > > enter("end of line") > > I agree that the following would read better here: > > press(ALT + 'B') > > type("beginning of line") > > press(END) > > type("end of line") > > However like Google above, I would disagree with > > type(ALT + 'B') > > type("beginning of line") > > type(END) > > type("end of line") > > or even > > type(ALT + 'B' + "beginning of line" + END + "end of line") > > > > > > > > > > > > > > > > It is a needless restriction to assume that every control character must > > > > > > only be part of a single key press event. I even remember a Mac > > > > > > application back in the early 1990s or late 1980s that used combinations > > > > > > like Ctrl-A Y to perform commands. (Actually, such older Macs didn't have > > > > > > a Control key, they used Command instead, but the principle is the same.) > > > > > > > > > > > > > > > > > > > One of the main goals of our automation product is that using it should > > > > > > > feel like giving instructions to a human being looking over their > > > > > > > shoulder at a screen. > > > > > > > > > > > > In a word processor, I might say > > > > > > > > > > > > "Type Ctrl-A Ctrl-X to cut all the text from the document." > > > > > > > > > > > > rather than > > > > > > > > > > > > "Press Ctrl-A. Now press Ctrl-X." > > > > > > > With the current proposal, it'd be > > press(CTRL + 'A', CTRL + 'X') > > Forgetting about `type` vs `press` for a moment, chaining the key combinations like this is OK, isn't it? > > > > > > > > > > > > We really quite like the word `type`, and a few people here seem to > > > > > > > favour it too. In particular, Steven: We're glad you accidentally > > > > > > > clicked on our mail. Thank you for your inputs and the great quote by > > > > > > > Phil Karlton. We think you were right in everything you said. However, > > > > > > > some people seem to be *really* put off when you override a built-in > > > > > > > function. Even though of course you can avoid the overriding by saying > > > > > > > from automa.api import type *as* ..., > > > > > > > (as Tim pointed out) we'd like to avoid irritating those people. For > > > > > > > this reason, we would rather not use `type`. > > > > > > > > > > > > You need to ask yourself, who is your primary audience for your software? > > > > > > > > > > > > Is it ... ? > > > > > > > > > > > > a) non-technical people who aren't very familiar with Python, and might > > > > > > not even know that there is a built-in function also called "type", or > > > > > > care if they do know; > > > > > > > > > > > > b) Python programmers who have embraced the concept of namespaces and > > > > > > have no fear about x.foo clashing with y.foo; > > > > > > > > > > > > c) Python programmers with a superstitious dread of using any name which > > > > > > is not global unique, just in case somebody accidentally shadows one > > > > > > function "foo" with another function "foo". > > > > > > > I agree that this is an important question to ask. however we unfortunately cannot answer it yet. We think our software should be usable by people from all three groups, but which group will use it the most we don't know yet. > > > > > > > > I think it is downright silly to avoid using the descriptive and simple > > > > > > name "type" out of some superstition against re-using names which have > > > > > > been used elsewhere, even in the builtins. > > > > > > > > > > > > If it were possible to be confused by the two types, e.g. if they took > > > > > > the same arguments but did radically different things, then I would > > > > > > accept that it was too dangerous/confusing to re-use the name. Reasonable > > > > > > fears about shadowing and confusion are, well, reasonable. But nobody is > > > > > > going to confuse your use of type as a command: > > > > > > > > > > > > type(some_string) > > > > > > > > > > > > with the built-in use as a function > > > > > > > > > > > > if type(something) is list: > > > > > > MyClass = type(x, y, z) > > > > Actually, when I first read your example, I was confused. I guess it's because the two different meanings of `type` were so close together I still had the first in mind when encountering the second. Nevertheless, it did confuse me. > > > > That's actually a key point: You are not confused or irritated by giving `type` a new meaning, and you have very valid reasons why. However, several people we asked in other places were surprised or irritated. You have very good points, but we won't get a chance to explain them to these other users when they first see our API. As the API designers, we have to try to find a solution that's acceptable for most people, and will therefore not be perfect for everyone. > > > > > > > > I don't think there is any point in having two functions that do exactly > > > > > > the same thing. Expect your users to develop all sorts of superstitions > > > > > > like "you can only use press() with a single key at a time", and get > > > > > > confused as to when you are supposed to use enter() and when press() (for > > > > > > whatever names you eventually choose). > > > > > > > I agree that it's not good to have two functions do exactly the same thing. However, it also has to be pointed out that it's not good for one function to do too many things. An example like > > type(ALT + 'B' + "beginning of line" + END + "end of line") > > imho tries to do too much in one go. With things like this, that you could not forbid with having only one function `type` that does everything, you would soon run into problems like "does it now press ALT all the time, or just for the first 'B'? Then your syntax/API pretty quickly explodes and you end up having to add some form of bracketing, escape sequences etc etc. That's something the splitting should hopefully avoid. > > > > Again, it's a long(ish) mail, and that's because it's very interesting to bounce our ideas off of you. Thank you for giving us a chance to do this! > > > > Michael From joel.goldstick at gmail.com Fri Nov 23 11:22:21 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Fri, 23 Nov 2012 11:22:21 -0500 Subject: Is it bad style to override the built-in function `type`? In-Reply-To: References: Message-ID: On Fri, Nov 23, 2012 at 11:12 AM, Michael Herrmann < michael.herrmann at getautoma.com> wrote: > Hi, > > do you think it's bad style to override the built-in function `type`? I'm > co-developing a GUI automation library called Automa ( > http://www.getautoma.com) and 'type' would be a very fitting name for a > function that generates artificial key strokes. > > Personally, I think this is a horrible idea. On this list and the tutor list, people often use variable names that are already defined in the language. It leads to non obvious errors -- especially when revisting old code. Why not call the thing 'key_stroke'? > This post is motivated by an already lengthy discussion on this mailing > list (http://bit.ly/10aOy4H), where we tried to find alternative names > for `type`. Many were found, but none are quite as fitting as 'type'. > > For the sake of avoiding a discussion that is already being lead elsewhere > please confine this thread to what you generally think about overriding > `type`, and post suggestions for alternative names or solutions in the > other thread. > > Thank you very much! > Michael > -- > http://mail.python.org/mailman/listinfo/python-list > -- Joel Goldstick -------------- next part -------------- An HTML attachment was scrubbed... URL: From ramit.prasad at jpmorgan.com Fri Nov 23 11:27:53 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Fri, 23 Nov 2012 16:27:53 +0000 Subject: Getting a seeded value from a list In-Reply-To: <50ac4ee7$0$21790$c3e8da3$76491128@news.astraweb.com> References: <993b9303-dbca-43f1-9ff4-9677278776b1@googlegroups.com> <50ac4ee7$0$21790$c3e8da3$76491128@news.astraweb.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF474167D1967@SBECMX007.exchad.jpmchase.net> Steven D'Aprano wrote: > > On Wed, 21 Nov 2012 14:41:24 +1100, Chris Angelico wrote: > > > However, this still means that the player will see the exact same level > > regenerated every time, absolutely fresh. As previously stated in this > > thread, that's not usually a good thing for encounters, treasure, etc. > > Once some nasty critter has been killed, he should STAY killed! :) > > Why? That isn't true in real life, why should it be true for games? > It is not true in all games. I have seen games where treasures regenerate in the same location except for key items. Same goes for enemies (where only "bosses" do not regenerate). It really just depends on the type of game you are playing--designing in this case. ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From emile at fenx.com Fri Nov 23 11:39:10 2012 From: emile at fenx.com (Emile van Sebille) Date: Fri, 23 Nov 2012 08:39:10 -0800 Subject: Is it bad style to override the built-in function `type`? In-Reply-To: References: Message-ID: On 11/23/2012 8:12 AM, Michael Herrmann wrote: > Hi, > > do you think it's bad style to override the built-in function `type`? I'm co-developing a GUI automation library called Automa (http://www.getautoma.com) and 'type' would be a very fitting name for a function that generates artificial key strokes. > > This post is motivated by an already lengthy discussion on this mailing list (http://bit.ly/10aOy4H), where we tried to find alternative names for `type`. Many were found, but none are quite as fitting as 'type'. > > For the sake of avoiding a discussion that is already being lead elsewhere please confine this thread to what you generally think about overriding `type`, and post suggestions for alternative names or solutions in the other thread. > > Thank you very much! > Michael > http://stackoverflow.com/questions/9109333/is-it-bad-practice-to-use-a-built-in-function-name-as-an-attribute-or-method-ide From kwpolska at gmail.com Fri Nov 23 11:42:59 2012 From: kwpolska at gmail.com (Kwpolska) Date: Fri, 23 Nov 2012 17:42:59 +0100 Subject: 10 sec poll - please reply! In-Reply-To: References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <818c157c-1395-441d-ad99-955a31b0d523@googlegroups.com> <780646bd-4caf-4080-b549-cf30a6df0fdf@googlegroups.com> Message-ID: On Fri, Nov 23, 2012 at 2:42 PM, Michael Herrmann wrote: > Dear all, > > the emails are getting kind of long so to ask you briefly: What do you think of splitting `type` into two functions `press` and `enter`? Their use cases are: > press(CTRL + 'a') > press(ENTER) > press(ALT + 'f', 's') > enter("Hello World!") > enter("test.txt", into="File name") > > Thanks, > Michael First of, please don?t top-post. Second of, the type?enter split a bad idea. It would require me to think whether I should use one or the other. type() is perfectly fine, because Automa is never going to be used as from automa import *. And if it is, it?s in your shell for non-Pythonistas. And also, my general thoughts: type() is just fine. Unless you want to call it simulate_pressing_keys_on_the_keyboard_without_getting_a_mechanical_arm_out_of_the_screen_and_pressing_the_keys_with_it(), but then you will need to create simulate_using_the_mouse_without_getting_a_mechanical_arm_out_of_the_screen_and_moving_the_mouse_or_pressing_its_buttons_with_it(), too. -- Kwpolska stop html mail | always bottom-post www.asciiribbon.org | www.netmeister.org/news/learn2quote.html GPG KEY: 5EAAEA16 From roy at panix.com Fri Nov 23 11:47:34 2012 From: roy at panix.com (Roy Smith) Date: Fri, 23 Nov 2012 11:47:34 -0500 Subject: Is it bad style to override the built-in function `type`? References: Message-ID: In article , Michael Herrmann wrote: > do you think it's bad style to override the built-in function `type`? I'm > co-developing a GUI automation library called Automa > (http://www.getautoma.com) and 'type' would be a very fitting name for a > function that generates artificial key strokes. For local variable names with small scopes, I don't fret much about overriding built-in names. For a library, I would try harder to find non-conflicting names. How about one of these: type_text() enter_text() keyboard() PS, A suggestion about your website. In these days of tech exit strategies and M/A crazyness, putting "Purchase" and "Company" as adjacent items in the main menu might be misconstrued. At first glance, I read it as "Purchase company". Twitter Bootstrap, no? From pengyu.ut at gmail.com Fri Nov 23 11:57:14 2012 From: pengyu.ut at gmail.com (Peng Yu) Date: Fri, 23 Nov 2012 08:57:14 -0800 (PST) Subject: Why queue.empty() returns False even after put() is called? Message-ID: Hi, The empty() returns True even after put() has been called. Why it is empty when there some items in it? Could anybody help me understand it? Thanks! ~/linux/test/python/man/library/multiprocessing/Queue/empty$ cat main.py #!/usr/bin/env python import multiprocessing queue = multiprocessing.Queue() print queue.empty() queue.put(['a', 'b']) queue.put(['c', 'd']) print queue.empty() Regards, Peng From michael.herrmann at getautoma.com Fri Nov 23 12:08:14 2012 From: michael.herrmann at getautoma.com (Michael Herrmann) Date: Fri, 23 Nov 2012 09:08:14 -0800 (PST) Subject: Is it bad style to override the built-in function `type`? In-Reply-To: References: Message-ID: <2ddcec85-6a2e-46ca-ae48-7be733fab19f@googlegroups.com> Thanks for your replies, Emile, the link you gave is about attribute names, and does not give a definite answer. I wrote this post in hope for an answer to the question how many people here think it would be OK to override `type` in the very specific context of a GUI automation library, and how many people would not like it. Roy, thanks for your input, we already have tried hard in the thread I quoted in my first post (http://bit.ly/10aOy4H). We found some non-conflicting names, however they're not quite as fitting as `type` and I'm here hoping to find out how "bad" overriding type would be, to be able to weigh it against the drawbacks of using a worse-fitting name. Regarding the website: I had never thought of that! ;-) I guess we should rename it to "About"... I'm sorry for spamming this mailing list so much... I promise it won't be for much longer. Best, Michael On Friday, November 23, 2012 5:12:39 PM UTC+1, Michael Herrmann wrote: > Hi, > > > > do you think it's bad style to override the built-in function `type`? I'm co-developing a GUI automation library called Automa (http://www.getautoma.com) and 'type' would be a very fitting name for a function that generates artificial key strokes. > > > > This post is motivated by an already lengthy discussion on this mailing list (http://bit.ly/10aOy4H), where we tried to find alternative names for `type`. Many were found, but none are quite as fitting as 'type'. > > > > For the sake of avoiding a discussion that is already being lead elsewhere please confine this thread to what you generally think about overriding `type`, and post suggestions for alternative names or solutions in the other thread. > > > > Thank you very much! > > Michael From michael.herrmann at getautoma.com Fri Nov 23 12:29:30 2012 From: michael.herrmann at getautoma.com (Michael Herrmann) Date: Fri, 23 Nov 2012 09:29:30 -0800 (PST) Subject: 10 sec poll - please reply! In-Reply-To: References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <818c157c-1395-441d-ad99-955a31b0d523@googlegroups.com> <780646bd-4caf-4080-b549-cf30a6df0fdf@googlegroups.com> Message-ID: Hi, I see your concern with having two functions that have to be separately remembered... I personally would also be fine with type(), however some people are violently against it. I opened a new thread (https://groups.google.com/forum/?fromgroups=#!topic/comp.lang.python/GjZ2hAS1Wyk) to ask just how many people would have a problem with this. I know I'm really spamming this list and apologize. I promise it'll be over soon. Michael On Friday, November 23, 2012 5:43:08 PM UTC+1, Kwpolska wrote: > On Fri, Nov 23, 2012 at 2:42 PM, Michael Herrmann > > <...> wrote: > > > Dear all, > > > > > > the emails are getting kind of long so to ask you briefly: What do you think of splitting `type` into two functions `press` and `enter`? Their use cases are: > > > press(CTRL + 'a') > > > press(ENTER) > > > press(ALT + 'f', 's') > > > enter("Hello World!") > > > enter("test.txt", into="File name") > > > > > > Thanks, > > > Michael > > > > First of, please don?t top-post. Second of, the type?enter split a > > bad idea. It would require me to think whether I should use one or > > the other. type() is perfectly fine, because Automa is never going to > > be used as from automa import *. And if it is, it?s in your shell for > > non-Pythonistas. > > > > And also, my general thoughts: type() is just fine. Unless you want > > to call it simulate_pressing_keys_on_the_keyboard_without_getting_a_mechanical_arm_out_of_the_screen_and_pressing_the_keys_with_it(), > > but then you will need to create > > simulate_using_the_mouse_without_getting_a_mechanical_arm_out_of_the_screen_and_moving_the_mouse_or_pressing_its_buttons_with_it(), > > too. > > > > -- > > Kwpolska > > stop html mail | always bottom-post > > www.asciiribbon.org | www.netmeister.org/news/learn2quote.html > > GPG KEY: 5EAAEA16 From michael.herrmann at getautoma.com Fri Nov 23 12:29:30 2012 From: michael.herrmann at getautoma.com (Michael Herrmann) Date: Fri, 23 Nov 2012 09:29:30 -0800 (PST) Subject: 10 sec poll - please reply! In-Reply-To: References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <818c157c-1395-441d-ad99-955a31b0d523@googlegroups.com> <780646bd-4caf-4080-b549-cf30a6df0fdf@googlegroups.com> Message-ID: Hi, I see your concern with having two functions that have to be separately remembered... I personally would also be fine with type(), however some people are violently against it. I opened a new thread (https://groups.google.com/forum/?fromgroups=#!topic/comp.lang.python/GjZ2hAS1Wyk) to ask just how many people would have a problem with this. I know I'm really spamming this list and apologize. I promise it'll be over soon. Michael On Friday, November 23, 2012 5:43:08 PM UTC+1, Kwpolska wrote: > On Fri, Nov 23, 2012 at 2:42 PM, Michael Herrmann > > <...> wrote: > > > Dear all, > > > > > > the emails are getting kind of long so to ask you briefly: What do you think of splitting `type` into two functions `press` and `enter`? Their use cases are: > > > press(CTRL + 'a') > > > press(ENTER) > > > press(ALT + 'f', 's') > > > enter("Hello World!") > > > enter("test.txt", into="File name") > > > > > > Thanks, > > > Michael > > > > First of, please don?t top-post. Second of, the type?enter split a > > bad idea. It would require me to think whether I should use one or > > the other. type() is perfectly fine, because Automa is never going to > > be used as from automa import *. And if it is, it?s in your shell for > > non-Pythonistas. > > > > And also, my general thoughts: type() is just fine. Unless you want > > to call it simulate_pressing_keys_on_the_keyboard_without_getting_a_mechanical_arm_out_of_the_screen_and_pressing_the_keys_with_it(), > > but then you will need to create > > simulate_using_the_mouse_without_getting_a_mechanical_arm_out_of_the_screen_and_moving_the_mouse_or_pressing_its_buttons_with_it(), > > too. > > > > -- > > Kwpolska > > stop html mail | always bottom-post > > www.asciiribbon.org | www.netmeister.org/news/learn2quote.html > > GPG KEY: 5EAAEA16 From tjreedy at udel.edu Fri Nov 23 13:35:54 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 23 Nov 2012 13:35:54 -0500 Subject: Is it bad style to override the built-in function `type`? In-Reply-To: References: Message-ID: On 11/23/2012 11:22 AM, Joel Goldstick wrote: > > > > On Fri, Nov 23, 2012 at 11:12 AM, Michael Herrmann > > > wrote: > > Hi, > > do you think it's bad style to override the built-in function > `type`? I'm co-developing a GUI automation library called Automa > (http://www.getautoma.com) and 'type' would be a very fitting name > for a function that generates artificial key strokes. > > Personally, I think this is a horrible idea. On this list and the tutor > list, people often use variable names that are already defined in the > language. It leads to non obvious errors -- especially when revisting > old code. Why not call the thing 'key_stroke'? I agree. -- Terry Jan Reedy From michael.herrmann at getautoma.com Fri Nov 23 13:41:10 2012 From: michael.herrmann at getautoma.com (Michael Herrmann) Date: Fri, 23 Nov 2012 10:41:10 -0800 (PST) Subject: Is it bad style to override the built-in function `type`? In-Reply-To: References: Message-ID: <5b8a0a43-c737-4543-99e3-76e44c544a02@googlegroups.com> Thank you for your feedback. I sent the following reply to Joel but it didn't make it through to the mailing list: Hi Joel, thanks for your reply. I can see why you think it's a bad idea. The reason why we want to call it `type` is that this is precisely the word that is most commonly used in our context, GUI automation. There are alternative names (see http://bit.ly/10aOy4H for a huge list) , but leaving aside the fact that Python has an overwriteable meaning for `type`, none fits quite as well. One of the main goals of our tool/API is to be as close to everyday English as possible, and thus hopefully to be intuitive also for novice users. For this reason it is very tempting for us to use `type`. Several people in the original thread http://bit.ly/10aOy4H have suggested `type` because it best fits our problem domain, and have said that they don't mind it overriding a built-in function. I know it's a common beginner's mistake to incautiously override built-in functions. However, we put in a lot of research and have come to the conclusion that, if Python had not already defined it, `type` would be the best name. We are now trying to evaluate how bad the disadvantages you mention are in comparison to the advantage to having a name that is more intuitive to use in the problem domain. Can you somehow relate to my explanations, or are your experiences with overwriting built-in variables so bad that you would advise to never ever do it? Thanks! Michael On Friday, November 23, 2012 7:36:20 PM UTC+1, Terry Reedy wrote: > On 11/23/2012 11:22 AM, Joel Goldstick wrote: > > > > > > > > > > > > On Fri, Nov 23, 2012 at 11:12 AM, Michael Herrmann > > > > > > wrote: > > > > > > Hi, > > > > > > do you think it's bad style to override the built-in function > > > `type`? I'm co-developing a GUI automation library called Automa > > > (http://www.getautoma.com) and 'type' would be a very fitting name > > > for a function that generates artificial key strokes. > > > > > > Personally, I think this is a horrible idea. On this list and the tutor > > > list, people often use variable names that are already defined in the > > > language. It leads to non obvious errors -- especially when revisting > > > old code. Why not call the thing 'key_stroke'? > > > > I agree. > > > > -- > > Terry Jan Reedy From michael.herrmann at getautoma.com Fri Nov 23 13:41:10 2012 From: michael.herrmann at getautoma.com (Michael Herrmann) Date: Fri, 23 Nov 2012 10:41:10 -0800 (PST) Subject: Is it bad style to override the built-in function `type`? In-Reply-To: References: Message-ID: <5b8a0a43-c737-4543-99e3-76e44c544a02@googlegroups.com> Thank you for your feedback. I sent the following reply to Joel but it didn't make it through to the mailing list: Hi Joel, thanks for your reply. I can see why you think it's a bad idea. The reason why we want to call it `type` is that this is precisely the word that is most commonly used in our context, GUI automation. There are alternative names (see http://bit.ly/10aOy4H for a huge list) , but leaving aside the fact that Python has an overwriteable meaning for `type`, none fits quite as well. One of the main goals of our tool/API is to be as close to everyday English as possible, and thus hopefully to be intuitive also for novice users. For this reason it is very tempting for us to use `type`. Several people in the original thread http://bit.ly/10aOy4H have suggested `type` because it best fits our problem domain, and have said that they don't mind it overriding a built-in function. I know it's a common beginner's mistake to incautiously override built-in functions. However, we put in a lot of research and have come to the conclusion that, if Python had not already defined it, `type` would be the best name. We are now trying to evaluate how bad the disadvantages you mention are in comparison to the advantage to having a name that is more intuitive to use in the problem domain. Can you somehow relate to my explanations, or are your experiences with overwriting built-in variables so bad that you would advise to never ever do it? Thanks! Michael On Friday, November 23, 2012 7:36:20 PM UTC+1, Terry Reedy wrote: > On 11/23/2012 11:22 AM, Joel Goldstick wrote: > > > > > > > > > > > > On Fri, Nov 23, 2012 at 11:12 AM, Michael Herrmann > > > > > > wrote: > > > > > > Hi, > > > > > > do you think it's bad style to override the built-in function > > > `type`? I'm co-developing a GUI automation library called Automa > > > (http://www.getautoma.com) and 'type' would be a very fitting name > > > for a function that generates artificial key strokes. > > > > > > Personally, I think this is a horrible idea. On this list and the tutor > > > list, people often use variable names that are already defined in the > > > language. It leads to non obvious errors -- especially when revisting > > > old code. Why not call the thing 'key_stroke'? > > > > I agree. > > > > -- > > Terry Jan Reedy From roy at panix.com Fri Nov 23 13:46:25 2012 From: roy at panix.com (Roy Smith) Date: Fri, 23 Nov 2012 13:46:25 -0500 Subject: argparse -- mutually exclusive sets of arguments? Message-ID: My command either takes two positional arguments (in which case, both are required): $ command foo bar or the name of a config file (in which case, the positional arguments are forbidden): $ command --config file How can I represent this with argparse; add_mutually_exclusive_group() isn't quite the right thing. It could specify that foo and --config are mutually exclusive, but not (as far as I can see) the more complicated logic described above. From ian.g.kelly at gmail.com Fri Nov 23 13:53:31 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 23 Nov 2012 11:53:31 -0700 Subject: Why queue.empty() returns False even after put() is called? In-Reply-To: References: Message-ID: On Fri, Nov 23, 2012 at 9:57 AM, Peng Yu wrote: > Hi, > > The empty() returns True even after put() has been called. Why it is > empty when there some items in it? Could anybody help me understand > it? Thanks! > > ~/linux/test/python/man/library/multiprocessing/Queue/empty$ cat > main.py > #!/usr/bin/env python > > import multiprocessing > > queue = multiprocessing.Queue() > print queue.empty() > queue.put(['a', 'b']) > queue.put(['c', 'd']) > print queue.empty() According to the docs, the Queue uses a background thread to load data into it: When a process first puts an item on the queue a feeder thread is started which transfers objects from a buffer into the pipe. Most likely it still appears to be empty because this thread has not had a chance to run yet. If you try inserting a time.sleep() call, you should see the queue become non-empty once the background thread has run. From python at mrabarnett.plus.com Fri Nov 23 13:53:52 2012 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 23 Nov 2012 18:53:52 +0000 Subject: Why queue.empty() returns False even after put() is called? In-Reply-To: References: Message-ID: <50AFC640.50306@mrabarnett.plus.com> On 2012-11-23 16:57, Peng Yu wrote: > Hi, > > The empty() returns True even after put() has been called. Why it is > empty when there some items in it? Could anybody help me understand > it? Thanks! > > ~/linux/test/python/man/library/multiprocessing/Queue/empty$ cat > main.py > #!/usr/bin/env python > > import multiprocessing > > queue = multiprocessing.Queue() > print queue.empty() > queue.put(['a', 'b']) > queue.put(['c', 'd']) > print queue.empty() > It works correctly for me. From tjreedy at udel.edu Fri Nov 23 13:56:50 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 23 Nov 2012 13:56:50 -0500 Subject: argparse -- mutually exclusive sets of arguments? In-Reply-To: References: Message-ID: On 11/23/2012 1:46 PM, Roy Smith wrote: > My command either takes two positional arguments (in which case, both > are required): > > $ command foo bar > > or the name of a config file (in which case, the positional arguments > are forbidden): > > $ command --config file > > How can I represent this with argparse; add_mutually_exclusive_group() > isn't quite the right thing. It could specify that foo and --config are > mutually exclusive, but not (as far as I can see) the more complicated > logic described above. Make the two positional arguments be one duple? Or tell argparse that all three are optional and handle the 'more complicated logic' in your own code after argparse returns. -- Terry Jan Reedy From cs at zip.com.au Fri Nov 23 17:19:39 2012 From: cs at zip.com.au (Cameron Simpson) Date: Sat, 24 Nov 2012 09:19:39 +1100 Subject: Why queue.empty() returns False even after put() is called? In-Reply-To: References: Message-ID: <20121123221939.GA23056@cskk.homeip.net> On 23Nov2012 11:53, Ian Kelly wrote: | On Fri, Nov 23, 2012 at 9:57 AM, Peng Yu wrote: | > The empty() returns True even after put() has been called. Why it is | > empty when there some items in it? Could anybody help me understand | > it? Thanks! | > | > ~/linux/test/python/man/library/multiprocessing/Queue/empty$ cat | > main.py | > #!/usr/bin/env python | > | > import multiprocessing | > | > queue = multiprocessing.Queue() | > print queue.empty() | > queue.put(['a', 'b']) | > queue.put(['c', 'd']) | > print queue.empty() | | According to the docs, the Queue uses a background thread to load data into it: | | When a process first puts an item on the queue a feeder thread is | started which transfers objects from a buffer into the pipe. | | Most likely it still appears to be empty because this thread has not | had a chance to run yet. If you try inserting a time.sleep() call, | you should see the queue become non-empty once the background thread | has run. Conversely, might it not appear empty because the objects have been thrown at the pipe already, appearing to have been consumed? Or is there end-to-end handshaking controlling what .empty() tests? (Though again, the far end may have grabbed them already too.) -- Cameron Simpson The ZZR-1100 is not the bike for me, but the day they invent "nerf" roads and ban radars I'll be the first in line......AMCN From steve+comp.lang.python at pearwood.info Fri Nov 23 17:30:03 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 23 Nov 2012 22:30:03 GMT Subject: 10 sec poll - please reply! References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <818c157c-1395-441d-ad99-955a31b0d523@googlegroups.com> <780646bd-4caf-4080-b549-cf30a6df0fdf@googlegroups.com> Message-ID: <50aff8eb$0$29975$c3e8da3$5496439d@news.astraweb.com> On Fri, 23 Nov 2012 05:42:22 -0800, Michael Herrmann wrote: > Dear all, > > the emails are getting kind of long so to ask you briefly: What do you > think of splitting `type` into two functions `press` and `enter`? This invites confusion as to the rules of when you can call `press` and when you can call `enter`. Especially since you haven't explained the rules, just given a bunch of non-exhaustive examples and invited people to extrapolate what the rules are. (By the way, they aren't use-cases, they're examples.) > Their use cases are: > press(CTRL + 'a') > press(ENTER) > press(ALT + 'f', 's') > enter("Hello World!") > enter("test.txt", into="File name") Is `press('s')` allowed? What about `press('S')`, or do I have to write `press(SHIFT + 's')`? If I can write `press(ALT + 'f', 's')`, can I write `press('f', 's')`? If not, why not? Can I write `press('fs')` as a simpler version of `press('f', 's')`? If not, why not? Can I write `press(CTRL + 'i')` to get a tab? How about `press('\t')`? If I want three tabs, can I write `press('\t\t\t')`, or do I have to write press(CTRL + 'i') press(CTRL + 'i') press(CTRL + 'i') If I want a tab, a letter, and a newline, repeated three times, can I do this? press("""\tA \tB \tC """) Or do I have to do this? press(CTRL + 'i') enter('A') press(CTRL + 'i') enter('B') press(CTRL + 'i') enter('C') Speaking of enter, how do I type "Hello World!" without entering it? If I want to type "Hello World!" without ENTER, do I have to do this? press('H') press('e') press('l') press('l') ... you get the picture With a function named "press", I would expect to be able to say: press('a') time.sleep(5) release('a') How do I do something like that? -- Steven From cs at zip.com.au Fri Nov 23 17:30:11 2012 From: cs at zip.com.au (Cameron Simpson) Date: Sat, 24 Nov 2012 09:30:11 +1100 Subject: Is it bad style to override the built-in function `type`? In-Reply-To: <5b8a0a43-c737-4543-99e3-76e44c544a02@googlegroups.com> References: <5b8a0a43-c737-4543-99e3-76e44c544a02@googlegroups.com> Message-ID: <20121123223011.GA23431@cskk.homeip.net> On 23Nov2012 10:41, Michael Herrmann wrote: [...] | I know it's a common beginner's mistake to incautiously override | built-in functions. However, we put in a lot of research and have come to | the conclusion that, if Python had not already defined it, `type` would | be the best name. We are now trying to evaluate how bad the disadvantages | you mention are in comparison to the advantage to having a name that is | more intuitive to use in the problem domain. | | Can you somehow relate to my explanations, or are your experiences | with overwriting built-in variables so bad that you would advise to | never ever do it? My own experience says that it is a thing best avoiding without a truly amazing reason not to. I urge you not to: type(foo) is a very basic Python idiom and you're breaking it. One day it _will_ bite you or your users. You will understand, but I would give goods odds that some of your users will not the day they go to examine the type of an object for perfectly normal pythonic reasons. Example: I have a module that stores "objects" and they have as a primary key a "name" and a "type" - not Python types, just strings. Accordingly I have a similar situation to yours: the desire to use the word "type". Fortunately for me, as an attribute in (usually small) code chunks I can usually go: t = foo.type ... work with t here ... Where I must pass one as a parameter I use the common convention of naming the parameter "type_" at the receiving end. For the calling end, as in your case, you want to use: type(blah) Is it at all possible to make all uses of your "type" function method calls? Eg: something.type("text to type") It avoids the overloading while keeping your desired name. -- Cameron Simpson Wouldn't it be great if all emergency stopping situations occurred on your favourite bit of road......you'd probably know about it before it happened and would be able to take other evasive action. - Neville Brabet From rosuav at gmail.com Fri Nov 23 17:32:46 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 24 Nov 2012 09:32:46 +1100 Subject: Getting a seeded value from a list In-Reply-To: <5B80DD153D7D744689F57F4FB69AF474167D1967@SBECMX007.exchad.jpmchase.net> References: <993b9303-dbca-43f1-9ff4-9677278776b1@googlegroups.com> <50ac4ee7$0$21790$c3e8da3$76491128@news.astraweb.com> <5B80DD153D7D744689F57F4FB69AF474167D1967@SBECMX007.exchad.jpmchase.net> Message-ID: On Sat, Nov 24, 2012 at 3:27 AM, Prasad, Ramit wrote: > Steven D'Aprano wrote: >> >> On Wed, 21 Nov 2012 14:41:24 +1100, Chris Angelico wrote: >> >> > However, this still means that the player will see the exact same level >> > regenerated every time, absolutely fresh. As previously stated in this >> > thread, that's not usually a good thing for encounters, treasure, etc. >> > Once some nasty critter has been killed, he should STAY killed! :) >> >> Why? That isn't true in real life, why should it be true for games? >> > > It is not true in all games. I have seen games where treasures > regenerate in the same location except for key items. Same goes > for enemies (where only "bosses" do not regenerate). It really > just depends on the type of game you are playing--designing > in this case. Perhaps they regenerate, but do they regenerate from the exact same random seed? For instance, in Murkon's Refuge, the maps are handcrafted and thus constant every time you enter a particular level - but go downstairs and upstairs, and the monsters and treasure regenerate, different from last time. Of course, if the idea is that you're rewinding time, then it makes good sense for you to see the exact same pattern of enemies. ChrisA From ramit.prasad at jpmorgan.com Fri Nov 23 17:32:48 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Fri, 23 Nov 2012 22:32:48 +0000 Subject: Problem with subprocess.call and windows schtasks In-Reply-To: <50AC2E38.8050303@davea.name> References: <50AC2E38.8050303@davea.name> Message-ID: <5B80DD153D7D744689F57F4FB69AF474167E1C83@SCACMX008.exchad.jpmchase.net> Dave Angel wrote: > > On 11/20/2012 06:41 PM, Tom Borkin wrote: > > (Please don't top-post. Now we lose all the context) > > Using shlex, I now have this: > > #!\Python27\python > > import os, subprocess > > path = os.path.join("C:\\", "Program Files", "Apache Group", "Apache2", > > "htdocs", "ccc", "run_alert.py") > > #subprocess.call(['SchTasks', '/Create', '/SC', 'ONCE', '/TN', '"test"', > > '/TR', path, '/ST', '23:50']) > > subprocess.call(['SchTasks', '/Create', '/SC', 'ONCE', '/TN', '"test"', > > '/TR', 'run_alert.py', '/ST', '23:50']) > > Both of the above commands throw the same error: > > ERROR: The filename, directory name or volume label syntax is incorrect. > > I don't use Windows, but doesn't a Windows program usually have an .exe > extension? So why would you expect it to find SchTasks ? Adding > extensions is a shell feature, and you're not using the shell. > > Also, you should take a look at the value "path". On Linux, it shows up as: > > C:\\/Program Files/Apache Group/Apache2/htdocs/ccc/run_alert.py > > It'll be different under Windows, but probably still wrong. Windows 7 + Python 2.6 >>> os.path.join("C:\\", "Program Files", "Apache Group", "Apache2", ... "htdocs", "ccc", "run_alert.py") 'C:\\Program Files\\Apache Group\\Apache2\\htdocs\\ccc\\run_alert.py' ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From joshua.landau.ws at gmail.com Fri Nov 23 18:49:03 2012 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Fri, 23 Nov 2012 23:49:03 +0000 Subject: argparse -- mutually exclusive sets of arguments? In-Reply-To: References: Message-ID: On 23 November 2012 18:46, Roy Smith wrote: > My command either takes two positional arguments (in which case, both > are required): > > $ command foo bar > > or the name of a config file (in which case, the positional arguments > are forbidden): > > $ command --config file > > How can I represent this with argparse; add_mutually_exclusive_group() > isn't quite the right thing. It could specify that foo and --config are > mutually exclusive, but not (as far as I can see) the more complicated > logic described above. Do you need to use argparse? If not, I've been recommending docopt due to its power and simplicity: -----START ----- """ Command. Usage: command command --config= Options: foo The egg that spams bar The spam that eggs --config= The config that configures """ from docopt import docopt if __name__ == '__main__': arguments = docopt(__doc__) print(arguments) ----- END ---- ----- USAGE ----- %~> python simple_docopt.py foobar barfoo {'--config': None, '': 'barfoo', '': 'foobar'} %~> python simple_docopt.py foobar Usage: simple_docopt.py simple_docopt.py --config= %~> python simple_docopt.py --config=turtle.conf {'--config': 'turtle.conf', '': None, '': None} %~> python simple_docopt.py --config=turtle.conf not allowed Usage: simple_docopt.py simple_docopt.py --config= ------- END USAGE ------- -------------- next part -------------- An HTML attachment was scrubbed... URL: From ian.g.kelly at gmail.com Fri Nov 23 20:07:46 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 23 Nov 2012 18:07:46 -0700 Subject: argparse -- mutually exclusive sets of arguments? In-Reply-To: References: Message-ID: On Fri, Nov 23, 2012 at 11:46 AM, Roy Smith wrote: > My command either takes two positional arguments (in which case, both > are required): > > $ command foo bar > > or the name of a config file (in which case, the positional arguments > are forbidden): > > $ command --config file > > How can I represent this with argparse; add_mutually_exclusive_group() > isn't quite the right thing. It could specify that foo and --config are > mutually exclusive, but not (as far as I can see) the more complicated > logic described above. I don't think you could even do the former. An argument must be optional in order to be mutually exclusive with anything. This works, however: parser = argparse.ArgumentParser() group = parser.add_mutually_exclusive_group(required=True) group.add_argument('--config', type=file) group.add_argument('--foobar', nargs=2, metavar=('FOO', 'BAR')) print parser.parse_args() Downsides are that the resulting interface is a little more formal and a little less friendly, and unless you customize the action you'll wind up with a 2-element 'foobar' arg instead of separate 'foo' and 'bar' args. From sagittarus999 at gmail.com Sat Nov 24 05:20:50 2012 From: sagittarus999 at gmail.com (bakie) Date: Sat, 24 Nov 2012 02:20:50 -0800 (PST) Subject: UI error for pycharm Message-ID: <0adb87de-e60b-4371-a6ff-e27c5b85a476@googlegroups.com> when I run py script in pycharm for UI Code: import from PySide.QtCore import * from PySide.QtGui import * import urllib2 class Form(QDialog): def __init__(self, parent=None): super(Form, self).__init__(parent) date = self.getdata() rates = sorted(self.rates.keys()) dateLabel = QLabel(date) self.fromComboBox = QComboBox() self.fromComboBox.addItems(rates) self.fromSpinBox = QDoubleSpinBox() self.fromSpinBox.setRange(0.01, 10000000.00) self.fromSpinBox.setValue(1.00) self.toComboBox = QComboBox() self.toComboBox.addItems(rates) self.toLabel = QLabel("1.00") grid = QGridLayout() grid.addWidget(dateLabel, 0, 0) grid.addWidget(self.fromComboBox, 1, 0) grid.addWidget(self.fromSpinBox, 1, 1) grid.addWidget(self.toComboBox, 2, 0) grid.addWidget(self.toLabel, 2, 1) self.setLayout(grid) self.connect(self.fromComboBox, SIGNAL("currentIndexChanged(int)"), self.updateUi) self.connect(self.toComboBox, SIGNAL("currentIndexChanged(int)"), self.updateUi) self.connect(self.fromSpinBox, SIGNAL("valueChanged(double)"), self.updateUi) def updateUi(self): to = self.toComboBox.currentText() from_ = self.fromComboBox.currentText() amount = (self.rates[from_] / self.rates[to]) * self.fromSpinBox.value() self.toLabel.setText("%0.2f" % amount) def getdata(self): self.rates = {} try: date = "Unknown" fh = urllib2.urlopen("http://www.bankofcanada.ca/en/markets/csv/exchange_eng.csv") for line in fh: line = line.rstrip() if not line or line.startswith(("#", "Closing")): continue fields = line.split(",") if line.startswith("Date "): date = fields[-1] else: try: value = float(fields[-1]) self.rates[fields[0]] = value except ValueError: pass return "Exchange rates date: " + date except Exception, e: return "Failued to download:\n%s" % e app = QApplication(sys.argv) form = Form() form.show() app.exec_() I had this error , how to fix them ? Code: C:\Python27\python.exe "C:/Users/denial/PycharmProjects/currency project/alarm.py" Traceback (most recent call last): File "C:/Users/denial/PycharmProjects/currency project/alarm.py", line 2, in from PySide.QtCore import * ImportError: No module named PySide.QtCore Process finished with exit code 1 From vincent.vandevyvre at swing.be Sat Nov 24 05:33:28 2012 From: vincent.vandevyvre at swing.be (Vincent Vande Vyvre) Date: Sat, 24 Nov 2012 11:33:28 +0100 Subject: UI error for pycharm In-Reply-To: <0adb87de-e60b-4371-a6ff-e27c5b85a476@googlegroups.com> References: <0adb87de-e60b-4371-a6ff-e27c5b85a476@googlegroups.com> Message-ID: <50B0A278.9090706@swing.be> Le 24/11/12 11:20, bakie a ?crit : > when I run py script in pycharm for UI > > > Code: > import from PySide.QtCore import * from PySide.QtGui import * import urllib2 class Form(QDialog): def __init__(self, parent=None): super(Form, self).__init__(parent) date = self.getdata() rates = sorted(self.rates.keys()) dateLabel = QLabel(date) self.fromComboBox = QComboBox() self.fromComboBox.addItems(rates) self.fromSpinBox = QDoubleSpinBox() self.fromSpinBox.setRange(0.01, 10000000.00) self.fromSpinBox.setValue(1.00) self.toComboBox = QComboBox() self.toComboBox.addItems(rates) self.toLabel = QLabel("1.00") grid = QGridLayout() grid.addWidget(dateLabel, 0, 0) grid.addWidget(self.fromComboBox, 1, 0) grid.addWidget(self.fromSpinBox, 1, 1) grid.addWidget(self.toComboBox, 2, 0) grid.addWidget(self.toLabel, 2, 1) self.setLayout(grid) self.connect(self.fromComboBox, SIGNAL("currentIndexChanged(int)"), self.updateUi) self.connect(self.toComboBox, SIGNAL("currentIndexChanged(int)"), self.updateUi) self.connect(self.fromSpinBox, SIGNAL("valueChanged(double)"), self.updateUi) def > updateUi > (self): to = self.toComboBox.currentText() from_ = self.fromComboBox.currentText() amount = (self.rates[from_] / self.rates[to]) * self.fromSpinBox.value() self.toLabel.setText("%0.2f" % amount) def getdata(self): self.rates = {} try: date = "Unknown" fh = urllib2.urlopen("http://www.bankofcanada.ca/en/markets/csv/exchange_eng.csv") for line in fh: line = line.rstrip() if not line or line.startswith(("#", "Closing")): continue fields = line.split(",") if line.startswith("Date "): date = fields[-1] else: try: value = float(fields[-1]) self.rates[fields[0]] = value except ValueError: pass return "Exchange rates date: " + date except Exception, e: return "Failued to download:\n%s" % e app = QApplication(sys.argv) form = Form() form.show() app.exec_() > > > I had this error , how to fix them ? > > Code: > C:\Python27\python.exe "C:/Users/denial/PycharmProjects/currency project/alarm.py" > Traceback (most recent call last): > File "C:/Users/denial/PycharmProjects/currency project/alarm.py", line 2, in > from PySide.QtCore import * > ImportError: No module named PySide.QtCore > > Process finished with exit code 1 Install PySide. -- Vincent V.V. Oqapy . Qarte . PaQager From sagittarus999 at gmail.com Sat Nov 24 05:57:17 2012 From: sagittarus999 at gmail.com (bakie) Date: Sat, 24 Nov 2012 02:57:17 -0800 (PST) Subject: UI error for pycharm In-Reply-To: <0adb87de-e60b-4371-a6ff-e27c5b85a476@googlegroups.com> References: <0adb87de-e60b-4371-a6ff-e27c5b85a476@googlegroups.com> Message-ID: <99004e3a-72be-4d3e-ad5d-f8aba26f5514@googlegroups.com> which version can i install / http://qt-project.org/downloads From sagittarus999 at gmail.com Sat Nov 24 05:58:51 2012 From: sagittarus999 at gmail.com (bakie) Date: Sat, 24 Nov 2012 02:58:51 -0800 (PST) Subject: UI error for pycharm In-Reply-To: <0adb87de-e60b-4371-a6ff-e27c5b85a476@googlegroups.com> References: <0adb87de-e60b-4371-a6ff-e27c5b85a476@googlegroups.com> Message-ID: i used window 7 , 64 bits From amca01 at gmail.com Sat Nov 24 06:14:24 2012 From: amca01 at gmail.com (Alasdair McAndrew) Date: Sat, 24 Nov 2012 03:14:24 -0800 (PST) Subject: Resize RGB image? Message-ID: <6dc97486-b146-42f1-aa09-943afbb56da9@googlegroups.com> I can resize a 2d image "im" with a command something like: r,c = shape(im) im2 = resize(im,(r//2,c//2)) However, resize doesn't seem to work with an RGB image: r,c,n = shape(im) # returns, say 500 600 3 I then need to take each band separately, resize it, and put them all back together with dstack: imr = im[:,:,0] img = im[:,:,1] imb = im[:,:,2] imr2 = resize(imr,(r//2,c//2)) img2 = resize(img,(r//2,c//2)) imb2 = resize(imb,(r//2,c//2)) im2 = dstack((imr2,img2,imb2)) This works fine, but seems a little clumsy. Of course this could be done in one command: im2 = dstack([resize(im[:,:,i],(r//2,c//2)) for i in range(3)]) What I want to know is: is there a version of resize which can be applied directly to multi-band images, without having to apply to each band separately? Thanks, Alasdair From sagittarus999 at gmail.com Sat Nov 24 06:29:03 2012 From: sagittarus999 at gmail.com (bakie) Date: Sat, 24 Nov 2012 03:29:03 -0800 (PST) Subject: UI error for pycharm In-Reply-To: <0adb87de-e60b-4371-a6ff-e27c5b85a476@googlegroups.com> References: <0adb87de-e60b-4371-a6ff-e27c5b85a476@googlegroups.com> Message-ID: I got it , Ur anser is right . use that links http://www.lfd.uci.edu/~gohlke/pythonlibs/ ( unofficial libriares ) From sagittarus999 at gmail.com Sat Nov 24 06:36:57 2012 From: sagittarus999 at gmail.com (bakie) Date: Sat, 24 Nov 2012 03:36:57 -0800 (PST) Subject: only .exe Message-ID: in the last question I try how to run py script with pycharm . thank dear bro ... and then I wanna have that program with .exe ( standalone installer ) . How can i do that ? I tried with two ways 1) py2exe 2) cx_freeze but I don't like that It has many file when i complie my script . which way for .exe ( standalone installer ) from pycharm . Thanks U brothers From d at davea.name Sat Nov 24 06:37:46 2012 From: d at davea.name (Dave Angel) Date: Sat, 24 Nov 2012 06:37:46 -0500 Subject: Resize RGB image? In-Reply-To: <6dc97486-b146-42f1-aa09-943afbb56da9@googlegroups.com> References: <6dc97486-b146-42f1-aa09-943afbb56da9@googlegroups.com> Message-ID: <50B0B18A.8030003@davea.name> On 11/24/2012 06:14 AM, Alasdair McAndrew wrote: > I can resize a 2d image "im" with a command something like: > > r,c = shape(im) > im2 = resize(im,(r//2,c//2)) You're missing at least one import there. So how about you start by telling us what non-standard libraries you're using, what version of python you're using, and what OS ? Then supply us a working set of code, in the sense that people can see the problem you're complaining about. > However, resize doesn't seem to work with an RGB image: > > r,c,n = shape(im) # returns, say 500 600 3 Doesn't work is pretty vague. It could mean anything from "it makes the image too small by 1%" to "it crashes my system after reformatting my hard disk." If you get an error, include the entire traceback here. If it doesn't work in some other sense, > I then need to take each band separately, resize it, and put them all back together with dstack: > > imr = im[:,:,0] > img = im[:,:,1] > imb = im[:,:,2] > imr2 = resize(imr,(r//2,c//2)) > img2 = resize(img,(r//2,c//2)) > imb2 = resize(imb,(r//2,c//2)) > im2 = dstack((imr2,img2,imb2)) > > This works fine, but seems a little clumsy. Of course this could be done in one command: > > im2 = dstack([resize(im[:,:,i],(r//2,c//2)) for i in range(3)]) > > What I want to know is: is there a version of resize which can be applied directly to multi-band images, without having to apply to each band separately? > > Thanks, > Alasdair -- DaveA From d at davea.name Sat Nov 24 06:47:08 2012 From: d at davea.name (Dave Angel) Date: Sat, 24 Nov 2012 06:47:08 -0500 Subject: Resize RGB image? In-Reply-To: <50B0B18A.8030003@davea.name> References: <6dc97486-b146-42f1-aa09-943afbb56da9@googlegroups.com> <50B0B18A.8030003@davea.name> Message-ID: <50B0B3BC.7040506@davea.name> On 11/24/2012 06:37 AM, Dave Angel wrote: Oops, I sent before I was done editing. Corrections below. > On 11/24/2012 06:14 AM, Alasdair McAndrew wrote: >> I can resize a 2d image "im" with a command something like: >> >> r,c = shape(im) >> im2 = resize(im,(r//2,c//2)) > You're missing at least one import there. > > So how about you start by telling us what non-standard libraries you're > using, what version of python you're using, and what OS ? Then supply > us a working set of code, in the sense that people can see the problem > you're complaining about. > >> However, resize doesn't seem to work with an RGB image: >> >> r,c,n = shape(im) # returns, say 500 600 3 > Doesn't work is pretty vague. It could mean anything from "it makes the > image too small by 1%" to "it crashes my system after reformatting my > hard disk." If you get an error, include the entire traceback here. If > it doesn't work in some other sense, then tell us what you expected, and what it did wrong. In this case, you might have to supply us with a link to sample data that demonstrates the problem. > >> I then need to take each band separately, resize it, and put them all back together with dstack: >> >> imr = im[:,:,0] >> img = im[:,:,1] >> imb = im[:,:,2] >> imr2 = resize(imr,(r//2,c//2)) >> img2 = resize(img,(r//2,c//2)) >> imb2 = resize(imb,(r//2,c//2)) >> im2 = dstack((imr2,img2,imb2)) >> >> This works fine, but seems a little clumsy. Of course this could be done in one command: >> >> im2 = dstack([resize(im[:,:,i],(r//2,c//2)) for i in range(3)]) >> >> What I want to know is: is there a version of resize which can be applied directly to multi-band images, without having to apply to each band separately? >> >> Thanks, >> Alasdair > -- DaveA From auriocus at gmx.de Sat Nov 24 07:03:01 2012 From: auriocus at gmx.de (Christian Gollwitzer) Date: Sat, 24 Nov 2012 13:03:01 +0100 Subject: only .exe In-Reply-To: References: Message-ID: Am 24.11.12 12:36, schrieb bakie: > in the last question I try how to run py script with pycharm . > thank dear bro ... > > and then I wanna have that program with .exe ( standalone installer ) . How can i do that ? > > I tried with two ways > 1) py2exe > 2) cx_freeze > > but I don't like that It has many file when i complie my script . > which way for .exe ( standalone installer ) from pycharm . > > Thanks U brothers U cn trI to ask Ur ?n in readble English.... pyinstaller has a switch to make a single file executable, if that's what you are after. Christian From ian.g.kelly at gmail.com Sat Nov 24 07:04:57 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sat, 24 Nov 2012 05:04:57 -0700 Subject: Resize RGB image? In-Reply-To: <6dc97486-b146-42f1-aa09-943afbb56da9@googlegroups.com> References: <6dc97486-b146-42f1-aa09-943afbb56da9@googlegroups.com> Message-ID: On Sat, Nov 24, 2012 at 4:14 AM, Alasdair McAndrew wrote: > I can resize a 2d image "im" with a command something like: > > r,c = shape(im) > im2 = resize(im,(r//2,c//2)) > > However, resize doesn't seem to work with an RGB image: > > r,c,n = shape(im) # returns, say 500 600 3 > > I then need to take each band separately, resize it, and put them all back together with dstack: > > imr = im[:,:,0] > img = im[:,:,1] > imb = im[:,:,2] > imr2 = resize(imr,(r//2,c//2)) > img2 = resize(img,(r//2,c//2)) > imb2 = resize(imb,(r//2,c//2)) > im2 = dstack((imr2,img2,imb2)) > > This works fine, but seems a little clumsy. Of course this could be done in one command: > > im2 = dstack([resize(im[:,:,i],(r//2,c//2)) for i in range(3)]) > > What I want to know is: is there a version of resize which can be applied directly to multi-band images, without having to apply to each band separately? You appear to be using numpy for this, which is a numerical library, not specifically an image-processing library. I would suggest using a library more suited for the task, such as PIL. I don't think that resize calls above are doing what you want in any case. They're not going to do scaling with interpolation, as is usually intended when resizing an image. They're just taking as much of the data as will fit (i.e., the first (r//2 * c//2) bytes) and packing it into the new array in the requested shape. From amca01 at gmail.com Sat Nov 24 07:22:44 2012 From: amca01 at gmail.com (Alasdair McAndrew) Date: Sat, 24 Nov 2012 04:22:44 -0800 (PST) Subject: Resize RGB image? In-Reply-To: References: <6dc97486-b146-42f1-aa09-943afbb56da9@googlegroups.com> <50B0B18A.8030003@davea.name> Message-ID: Thank you for your speedy response! And yes, I was too hasty in firing off a request without the necessary details. And in fact the resize from the PIL Image module does work: f = Image.open('bird-of-paradise-flower-1.jpg') r,c = f.size f2 = f.resize((r//2,c//2)) f2.show() shape(f) # returns(768, 1024, 3) shape(f2) # returns (384, 512, 3) I think I was confused about all the different "resize" methods floating about. Clearly I had been using the wrong one! But if f is defined - as I've just done - as an Image image, then it will use the correct resize method. I'll crawl back under my rock now. -Alasdair From amca01 at gmail.com Sat Nov 24 07:22:44 2012 From: amca01 at gmail.com (Alasdair McAndrew) Date: Sat, 24 Nov 2012 04:22:44 -0800 (PST) Subject: Resize RGB image? In-Reply-To: References: <6dc97486-b146-42f1-aa09-943afbb56da9@googlegroups.com> <50B0B18A.8030003@davea.name> Message-ID: Thank you for your speedy response! And yes, I was too hasty in firing off a request without the necessary details. And in fact the resize from the PIL Image module does work: f = Image.open('bird-of-paradise-flower-1.jpg') r,c = f.size f2 = f.resize((r//2,c//2)) f2.show() shape(f) # returns(768, 1024, 3) shape(f2) # returns (384, 512, 3) I think I was confused about all the different "resize" methods floating about. Clearly I had been using the wrong one! But if f is defined - as I've just done - as an Image image, then it will use the correct resize method. I'll crawl back under my rock now. -Alasdair From spetrie at gmail.com Sat Nov 24 07:52:03 2012 From: spetrie at gmail.com (Steve Petrie) Date: Sat, 24 Nov 2012 04:52:03 -0800 (PST) Subject: Suitable software stacks for simple python web service In-Reply-To: References: Message-ID: <6752ebe2-6c71-45d3-8011-6d5fba4a73af@googlegroups.com> On Thursday, November 22, 2012 1:42:42 AM UTC-5, Kev Dwyer wrote: > Steve Petrie wrote: > > > > > On Wednesday, November 21, 2012 2:32:40 AM UTC-5, Kev Dwyer wrote: > > >> Hello List, > > >> > > >> > > >> > > >> I have to build a simple web service which will: > > >> > > >> > > >> > > >> - receive queries from our other servers > > >> > > >> - forward the requests to a third party SOAP service > > >> > > >> - process the response from the third party > > >> > > >> - send the result back to the original requester > > >> > > >> > > >> > > >> >From the point of view of the requester, this will happen within the > > >> >scope > > >> > > >> of a single request. > > >> > > >> > > >> > > >> The data exchanged with the original requester will likely be encoded as > > >> > > >> JSON; the SOAP service will be handled by SUDS. > > >> > > >> > > >> > > >> The load is likely to be quite light, say a few requests per hour, though > > >> > > >> this may increase in the future. > > >> > > >> > > >> > > >> Given these requirements, what do you think might be a suitable software > > >> > > >> stack, i.e. webserver and web framework (if a web framework is even > > >> > > >> necessary)? > > >> > > >> > > >> > > >> Candidates should be compatible with Python2.7, though I'd be happy to > > >> > > >> consider Python 3 if anyone knows of a Python3 SOAP library that has good > > >> > > >> WSDL support. > > >> > > >> > > >> > > >> Cheers, > > >> > > >> > > >> > > >> Kev > > > > > > I'm using the Bottle web framework (http://bottlepy.org) to integrate > > > requests and replies originating in a Drupal site, a Beanstream (payment > > > processor) account, and a Salesforce instance. > > > > > > Communication with Salesforce is done through the Salesforce Python > > > Toolkit (http://code.google.com/p/salesforce-python-toolkit/), which uses > > > Suds. > > > > > > Communication with the Drupal site uses Python's (and PHP's on the Drupal > > > side) native JSON support. > > > > > > This is under Python 2.6.8 and Apache 2.2.23 running on an AWS EC2 > > > instance. > > > > > > No (major) problems so far, though still in the early stages of this > > > project. > > > > > > Steve > > > > > > > > > > > > I chose Bottle after trying a few other frameworks because, well, I can't > > > remember exactly why, though thinking back it's probably because of the > > > clarity of Bottle's approach and the simplicity of the documentation. > > > > > > Hello Steve, > > > > Thanks for your comment. > > > > I'm curious, did you consider any web servers other than Apache? > > > > Kev You're telling me that there are other web servers? ;) I didn't try any others seriously, no. My experience is with Apache and IIS, and I try to stay away from Windows. I should mention, given Dieter Maurer's comment, that Bottle is a (fairly thin) layer built over WSGI. I've built applications directly over WSGI as well; that's another way to go, it's quite straightforward. mod_python is no longer supported: http://blog.dscpl.com.au/2010/05/modpython-project-soon-to-be-officially.html. From spetrie at gmail.com Sat Nov 24 07:52:03 2012 From: spetrie at gmail.com (Steve Petrie) Date: Sat, 24 Nov 2012 04:52:03 -0800 (PST) Subject: Suitable software stacks for simple python web service In-Reply-To: References: Message-ID: <6752ebe2-6c71-45d3-8011-6d5fba4a73af@googlegroups.com> On Thursday, November 22, 2012 1:42:42 AM UTC-5, Kev Dwyer wrote: > Steve Petrie wrote: > > > > > On Wednesday, November 21, 2012 2:32:40 AM UTC-5, Kev Dwyer wrote: > > >> Hello List, > > >> > > >> > > >> > > >> I have to build a simple web service which will: > > >> > > >> > > >> > > >> - receive queries from our other servers > > >> > > >> - forward the requests to a third party SOAP service > > >> > > >> - process the response from the third party > > >> > > >> - send the result back to the original requester > > >> > > >> > > >> > > >> >From the point of view of the requester, this will happen within the > > >> >scope > > >> > > >> of a single request. > > >> > > >> > > >> > > >> The data exchanged with the original requester will likely be encoded as > > >> > > >> JSON; the SOAP service will be handled by SUDS. > > >> > > >> > > >> > > >> The load is likely to be quite light, say a few requests per hour, though > > >> > > >> this may increase in the future. > > >> > > >> > > >> > > >> Given these requirements, what do you think might be a suitable software > > >> > > >> stack, i.e. webserver and web framework (if a web framework is even > > >> > > >> necessary)? > > >> > > >> > > >> > > >> Candidates should be compatible with Python2.7, though I'd be happy to > > >> > > >> consider Python 3 if anyone knows of a Python3 SOAP library that has good > > >> > > >> WSDL support. > > >> > > >> > > >> > > >> Cheers, > > >> > > >> > > >> > > >> Kev > > > > > > I'm using the Bottle web framework (http://bottlepy.org) to integrate > > > requests and replies originating in a Drupal site, a Beanstream (payment > > > processor) account, and a Salesforce instance. > > > > > > Communication with Salesforce is done through the Salesforce Python > > > Toolkit (http://code.google.com/p/salesforce-python-toolkit/), which uses > > > Suds. > > > > > > Communication with the Drupal site uses Python's (and PHP's on the Drupal > > > side) native JSON support. > > > > > > This is under Python 2.6.8 and Apache 2.2.23 running on an AWS EC2 > > > instance. > > > > > > No (major) problems so far, though still in the early stages of this > > > project. > > > > > > Steve > > > > > > > > > > > > I chose Bottle after trying a few other frameworks because, well, I can't > > > remember exactly why, though thinking back it's probably because of the > > > clarity of Bottle's approach and the simplicity of the documentation. > > > > > > Hello Steve, > > > > Thanks for your comment. > > > > I'm curious, did you consider any web servers other than Apache? > > > > Kev You're telling me that there are other web servers? ;) I didn't try any others seriously, no. My experience is with Apache and IIS, and I try to stay away from Windows. I should mention, given Dieter Maurer's comment, that Bottle is a (fairly thin) layer built over WSGI. I've built applications directly over WSGI as well; that's another way to go, it's quite straightforward. mod_python is no longer supported: http://blog.dscpl.com.au/2010/05/modpython-project-soon-to-be-officially.html. From kevin.p.dwyer at gmail.com Sat Nov 24 09:52:10 2012 From: kevin.p.dwyer at gmail.com (Kev Dwyer) Date: Sat, 24 Nov 2012 14:52:10 +0000 Subject: Suitable software stacks for simple python web service References: <6752ebe2-6c71-45d3-8011-6d5fba4a73af@googlegroups.com> Message-ID: Steve Petrie wrote: > On Thursday, November 22, 2012 1:42:42 AM UTC-5, Kev Dwyer wrote: >> Steve Petrie wrote: >> >> >> >> > On Wednesday, November 21, 2012 2:32:40 AM UTC-5, Kev Dwyer wrote: >> >> >> Hello List, >> >> >> >> >> >> >> >> >> >> >> >> I have to build a simple web service which will: >> >> >> >> >> >> >> >> >> >> >> >> - receive queries from our other servers >> >> >> >> >> >> - forward the requests to a third party SOAP service >> >> >> >> >> >> - process the response from the third party >> >> >> >> >> >> - send the result back to the original requester >> >> >> >> >> >> >> >> >> >> >> >> >From the point of view of the requester, this will happen within the >> >> >> >scope >> >> >> >> >> >> of a single request. >> >> >> >> >> >> >> >> >> >> >> >> The data exchanged with the original requester will likely be encoded >> >> as >> >> >> >> >> >> JSON; the SOAP service will be handled by SUDS. >> >> >> >> >> >> >> >> >> >> >> >> The load is likely to be quite light, say a few requests per hour, >> >> though >> >> >> >> >> >> this may increase in the future. >> >> >> >> >> >> >> >> >> >> >> >> Given these requirements, what do you think might be a suitable >> >> software >> >> >> >> >> >> stack, i.e. webserver and web framework (if a web framework is even >> >> >> >> >> >> necessary)? >> >> >> >> >> >> >> >> >> >> >> >> Candidates should be compatible with Python2.7, though I'd be happy to >> >> >> >> >> >> consider Python 3 if anyone knows of a Python3 SOAP library that has >> >> good >> >> >> >> >> >> WSDL support. >> >> >> >> >> >> >> >> >> >> >> >> Cheers, >> >> >> >> >> >> >> >> >> >> >> >> Kev >> >> > >> >> > I'm using the Bottle web framework (http://bottlepy.org) to integrate >> >> > requests and replies originating in a Drupal site, a Beanstream >> > (payment >> >> > processor) account, and a Salesforce instance. >> >> > >> >> > Communication with Salesforce is done through the Salesforce Python >> >> > Toolkit (http://code.google.com/p/salesforce-python-toolkit/), which >> > uses >> >> > Suds. >> >> > >> >> > Communication with the Drupal site uses Python's (and PHP's on the >> > Drupal >> >> > side) native JSON support. >> >> > >> >> > This is under Python 2.6.8 and Apache 2.2.23 running on an AWS EC2 >> >> > instance. >> >> > >> >> > No (major) problems so far, though still in the early stages of this >> >> > project. >> >> > >> >> > Steve >> >> > >> >> > >> >> > >> >> > I chose Bottle after trying a few other frameworks because, well, I >> > can't >> >> > remember exactly why, though thinking back it's probably because of the >> >> > clarity of Bottle's approach and the simplicity of the documentation. >> >> >> >> >> >> Hello Steve, >> >> >> >> Thanks for your comment. >> >> >> >> I'm curious, did you consider any web servers other than Apache? >> >> >> >> Kev > > You're telling me that there are other web servers? ;) > > I didn't try any others seriously, no. My experience is with Apache and > IIS, and I try to stay away from Windows. > > I should mention, given Dieter Maurer's comment, that Bottle is a (fairly > thin) layer built over WSGI. I've built applications directly over WSGI > as well; that's another way to go, it's quite straightforward. mod_python > is no longer supported: > http://blog.dscpl.com.au/2010/05/modpython-project-soon-to-be- officially.html. Based on Dieter's comment I'm using Bottle as a framework, with gunicorn (behind nginx) as the webserver. Even Bottle is probably overkill for my use case, but my time is rather limited, so I'm happy to use an off the shelf solution. And I must say, configuring gunicorn and nginx contrasted pleasantly with my memories of struggling with httpd.conf :) From frednotbob at hotmail.ca Sat Nov 24 12:38:55 2012 From: frednotbob at hotmail.ca (Graham Fielding) Date: Sat, 24 Nov 2012 09:38:55 -0800 Subject: Getting a seeded value from a list In-Reply-To: References: , , <993b9303-dbca-43f1-9ff4-9677278776b1@googlegroups.com>, , <50ac4ee7$0$21790$c3e8da3$76491128@news.astraweb.com>, <5B80DD153D7D744689F57F4FB69AF474167D1967@SBECMX007.exchad.jpmchase.net>, Message-ID: Just a quick update: after enlisting some help from a friend, we managed to get things working :) Thanks for all the help. In the area of items and monsters, they're both chosen from lists, based on some simple selection criteria (number per room, total per level, chance of appearance). Now, the major task is to reconfigure the distribution system to let items and monsters stay in place between levels. > Date: Sat, 24 Nov 2012 09:32:46 +1100 > Subject: Re: Getting a seeded value from a list > From: rosuav at gmail.com > To: python-list at python.org > > On Sat, Nov 24, 2012 at 3:27 AM, Prasad, Ramit > wrote: > > Steven D'Aprano wrote: > >> > >> On Wed, 21 Nov 2012 14:41:24 +1100, Chris Angelico wrote: > >> > >> > However, this still means that the player will see the exact same level > >> > regenerated every time, absolutely fresh. As previously stated in this > >> > thread, that's not usually a good thing for encounters, treasure, etc. > >> > Once some nasty critter has been killed, he should STAY killed! :) > >> > >> Why? That isn't true in real life, why should it be true for games? > >> > > > > It is not true in all games. I have seen games where treasures > > regenerate in the same location except for key items. Same goes > > for enemies (where only "bosses" do not regenerate). It really > > just depends on the type of game you are playing--designing > > in this case. > > Perhaps they regenerate, but do they regenerate from the exact same > random seed? For instance, in Murkon's Refuge, the maps are > handcrafted and thus constant every time you enter a particular level > - but go downstairs and upstairs, and the monsters and treasure > regenerate, different from last time. > > Of course, if the idea is that you're rewinding time, then it makes > good sense for you to see the exact same pattern of enemies. > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list -------------- next part -------------- An HTML attachment was scrubbed... URL: From michael.herrmann at getautoma.com Sat Nov 24 15:56:12 2012 From: michael.herrmann at getautoma.com (Michael Herrmann) Date: Sat, 24 Nov 2012 12:56:12 -0800 (PST) Subject: 10 sec poll - please reply! In-Reply-To: <50aff8eb$0$29975$c3e8da3$5496439d@news.astraweb.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <818c157c-1395-441d-ad99-955a31b0d523@googlegroups.com> <780646bd-4caf-4080-b549-cf30a6df0fdf@googlegroups.com> <50aff8eb$0$29975$c3e8da3$5496439d@news.astraweb.com> Message-ID: <12f7d5d3-58af-4c5e-a6ee-e064e198c4bd@googlegroups.com> Hi Steven, press('s') is allowed. So is press('S') - it presses shift for you and is thus equivalent to press(SHIFT + 's'). You can write press('f', 's'). You cannot write press('fs') because that's what enter(...) is for. If you try - or, as I would deem more likely, happen do to it by mistake - you get the following warning: >>> press('fs') ValueError: 'press' generates single keystrokes and is not intended to be used for typing in long plain-text strings. Did you maybe mean one of the following? * enter("fs") * click("fs") * press('f', 's') I'm not aware of CTRL + i having a connection with TAB. You cannot use it to get a tab. We haven't decided on what to do when you press('\t'). If you want three tabs, you can write press(TAB, TAB, TAB) If you want a tab, a letter and a newline, you can do enter("""\tA \tB \tC """) The reasoning is that you are entering plain text, that is a string that you would like to appear in the window you are typing into in the same way as you are supplying it to `enter`. If you want to type "Hello World!" without entering it, you call enter("Hello World!") Yes, I did write "call *enter* to do X *without entering*". You are hinting at a valid ambiguity with `enter` that Chris already pointed out. I don't think it's worse than the ambiguity of >>> type("Hello World!") We would need a new function for holding down keys to release them later. You are again pointing out an imho valid ambiguity. However, you were just happily using `press` with the understanding that it presses and releases keys, so I hope this one isn't too bad. As I said, I opened a new thread solely for overriding `type` in the context of a GUI automation library: https://groups.google.com/forum/?fromgroups=#!topic/comp.lang.python/GjZ2hAS1Wyk. So far, 4/4 people there advised against it. If only Python hadn't defined `type`... Michael On Friday, November 23, 2012 11:30:04 PM UTC+1, Steven D'Aprano wrote: > On Fri, 23 Nov 2012 05:42:22 -0800, Michael Herrmann wrote: > > > > > Dear all, > > > > > > the emails are getting kind of long so to ask you briefly: What do you > > > think of splitting `type` into two functions `press` and `enter`? > > > > This invites confusion as to the rules of when you can call `press` and > > when you can call `enter`. Especially since you haven't explained the > > rules, just given a bunch of non-exhaustive examples and invited people > > to extrapolate what the rules are. > > > > (By the way, they aren't use-cases, they're examples.) > > > > > > > Their use cases are: > > > press(CTRL + 'a') > > > press(ENTER) > > > press(ALT + 'f', 's') > > > enter("Hello World!") > > > enter("test.txt", into="File name") > > > > > > Is `press('s')` allowed? > > > > What about `press('S')`, or do I have to write `press(SHIFT + 's')`? > > > > If I can write `press(ALT + 'f', 's')`, can I write `press('f', 's')`? If > > not, why not? > > > > Can I write `press('fs')` as a simpler version of `press('f', 's')`? If > > not, why not? > > > > Can I write `press(CTRL + 'i')` to get a tab? How about `press('\t')`? > > > > If I want three tabs, can I write `press('\t\t\t')`, or do I have to write > > > > press(CTRL + 'i') > > press(CTRL + 'i') > > press(CTRL + 'i') > > > > If I want a tab, a letter, and a newline, repeated three times, can I do > > this? > > > > press("""\tA > > \tB > > \tC > > """) > > > > Or do I have to do this? > > > > press(CTRL + 'i') > > enter('A') > > press(CTRL + 'i') > > enter('B') > > press(CTRL + 'i') > > enter('C') > > > > Speaking of enter, how do I type "Hello World!" without entering it? If I > > want to type "Hello World!" without ENTER, do I have to do this? > > > > press('H') > > press('e') > > press('l') > > press('l') > > ... you get the picture > > > > > > With a function named "press", I would expect to be able to say: > > > > press('a') > > time.sleep(5) > > release('a') > > > > How do I do something like that? > > > > > > > > -- > > Steven From michael.herrmann at getautoma.com Sat Nov 24 16:07:26 2012 From: michael.herrmann at getautoma.com (Michael Herrmann) Date: Sat, 24 Nov 2012 13:07:26 -0800 (PST) Subject: 10 sec poll - please reply! In-Reply-To: References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <818c157c-1395-441d-ad99-955a31b0d523@googlegroups.com> Message-ID: Thanks for your reply Dennis, the commands sounding like they should be commands to a user is the whole point of the exercise: In doing so, we hope to make the API accessible also to users from a less technical background. You are perfectly right that system events are being generated and passed around, however that is not what these users care about. Frankly, also I coming from a very technical background don't care which events are generated and how, as long as it works. I agree that "key_down"/"key_up" has a nice symmetry to it. Maybe a solution could also be to use a context manager: with key_down(SHIFT): # some action... Cheers On Friday, November 23, 2012 11:11:34 PM UTC+1, Dennis Lee Bieber wrote: > On Thu, 22 Nov 2012 10:00:54 -0800 (PST), Michael Herrmann > > declaimed the following in > > gmane.comp.python.general: > > > > > These names aren't perfect. As Emile rightly pointed out, several tools distinguish between 'press' and 'release' and a user might wonder how to release a key that was pressed using 'press'. That's an ambiguity that is certainly there, however we hope that once the user has at least seen > > > press(ENTER) > > > it is clear what is meant. Distinguishing between pressing and releasing could we think easily be done with, say > > > hold_down(SHIFT) > > > ... > > > release(SHIFT) > > > Another ambiguity of 'press' that I pointed out in my original mail is that it could also be understood as "pressing a button". The current idea is to raise a ValueError if the user supplies a string that is longer than one character: > > > >>> press("OK") > > > ValueError: 'press' generates keystrokes and can only press single letters at a time. Did you maybe mean click("OK") or press('O', 'K')? > > > > > > > "press", "hold_down", "release" just sound so much like they should > > be commands to a /user/ not to a means of programmatically controlling > > an interface. A "user" presses a button -- but a program is just > > injecting the "event" of a button press into the input processing stream > > (it's been decades, but I still think in Amiga terms -- where all input > > events routed through one input handler and chained to the programs > > wanting to work with raw events... This meant that, but injecting the > > event codes before the input handler, a program could make another > > program [including the OS] think one had ejected/inserted floppies, > > mouse movements, keystrokes) > > > > "hold_down" and "release" would seem more memorable, and symmetric, > > if named "key_down" and "key_up"; and if "press" is limited to single > > codes -- "key" [or "keystroke"] > > > > The closest M$ .Net method is called SendKeys() and works with > > strings. > > http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.uitesting.keyboard.sendkeys%28v=vs.100%29.aspx > > > > > > > > > > > > > What do you think of this solution? I hope anybody read this far. I probably shouldn't have written that much but wanted to do justice to your inputs. > > > > > > Thanks! > > > > > > Michael > > > > > > On Tuesday, November 20, 2012 1:18:38 PM UTC+1, Michael Herrmann wrote: > > > > Hi, > > > > > > > > > > > > > > > > I'm developing a GUI Automation library (http://www.getautoma.com) and am having difficulty picking a name for the function that simulates key strokes. I currently have it as 'type' but that clashes with the built-in function. Example uses of 'type': > > > > > > > > > > > > > > > > type(ENTER) > > > > > > > > > > > > > > > > type("Hello World!") > > > > > > > > > > > > > > > > type(CTRL + 'a') > > > > > > > > > > > > > > > > What, in your view, would be the most intuitive alternative name? > > > > > > > > > > > > > > > > Here are my thoughts so far: I could call it 'press' but then our GUI automation tool also allows you to click things and then "press" might be mistaken for "pressing a button". A less ambiguous alternative is "type_keys" but that is rather long and doesn't read as well, for instance in type_keys(ENTER). > > > > > > > > > > > > > > > > Thank you very much! > > -- > > Wulfraed Dennis Lee Bieber AF6VN > > HTTP://wlfraed.home.netcom.com/ From michael.herrmann at getautoma.com Sat Nov 24 16:07:26 2012 From: michael.herrmann at getautoma.com (Michael Herrmann) Date: Sat, 24 Nov 2012 13:07:26 -0800 (PST) Subject: 10 sec poll - please reply! In-Reply-To: References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <818c157c-1395-441d-ad99-955a31b0d523@googlegroups.com> Message-ID: Thanks for your reply Dennis, the commands sounding like they should be commands to a user is the whole point of the exercise: In doing so, we hope to make the API accessible also to users from a less technical background. You are perfectly right that system events are being generated and passed around, however that is not what these users care about. Frankly, also I coming from a very technical background don't care which events are generated and how, as long as it works. I agree that "key_down"/"key_up" has a nice symmetry to it. Maybe a solution could also be to use a context manager: with key_down(SHIFT): # some action... Cheers On Friday, November 23, 2012 11:11:34 PM UTC+1, Dennis Lee Bieber wrote: > On Thu, 22 Nov 2012 10:00:54 -0800 (PST), Michael Herrmann > > declaimed the following in > > gmane.comp.python.general: > > > > > These names aren't perfect. As Emile rightly pointed out, several tools distinguish between 'press' and 'release' and a user might wonder how to release a key that was pressed using 'press'. That's an ambiguity that is certainly there, however we hope that once the user has at least seen > > > press(ENTER) > > > it is clear what is meant. Distinguishing between pressing and releasing could we think easily be done with, say > > > hold_down(SHIFT) > > > ... > > > release(SHIFT) > > > Another ambiguity of 'press' that I pointed out in my original mail is that it could also be understood as "pressing a button". The current idea is to raise a ValueError if the user supplies a string that is longer than one character: > > > >>> press("OK") > > > ValueError: 'press' generates keystrokes and can only press single letters at a time. Did you maybe mean click("OK") or press('O', 'K')? > > > > > > > "press", "hold_down", "release" just sound so much like they should > > be commands to a /user/ not to a means of programmatically controlling > > an interface. A "user" presses a button -- but a program is just > > injecting the "event" of a button press into the input processing stream > > (it's been decades, but I still think in Amiga terms -- where all input > > events routed through one input handler and chained to the programs > > wanting to work with raw events... This meant that, but injecting the > > event codes before the input handler, a program could make another > > program [including the OS] think one had ejected/inserted floppies, > > mouse movements, keystrokes) > > > > "hold_down" and "release" would seem more memorable, and symmetric, > > if named "key_down" and "key_up"; and if "press" is limited to single > > codes -- "key" [or "keystroke"] > > > > The closest M$ .Net method is called SendKeys() and works with > > strings. > > http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.uitesting.keyboard.sendkeys%28v=vs.100%29.aspx > > > > > > > > > > > > > What do you think of this solution? I hope anybody read this far. I probably shouldn't have written that much but wanted to do justice to your inputs. > > > > > > Thanks! > > > > > > Michael > > > > > > On Tuesday, November 20, 2012 1:18:38 PM UTC+1, Michael Herrmann wrote: > > > > Hi, > > > > > > > > > > > > > > > > I'm developing a GUI Automation library (http://www.getautoma.com) and am having difficulty picking a name for the function that simulates key strokes. I currently have it as 'type' but that clashes with the built-in function. Example uses of 'type': > > > > > > > > > > > > > > > > type(ENTER) > > > > > > > > > > > > > > > > type("Hello World!") > > > > > > > > > > > > > > > > type(CTRL + 'a') > > > > > > > > > > > > > > > > What, in your view, would be the most intuitive alternative name? > > > > > > > > > > > > > > > > Here are my thoughts so far: I could call it 'press' but then our GUI automation tool also allows you to click things and then "press" might be mistaken for "pressing a button". A less ambiguous alternative is "type_keys" but that is rather long and doesn't read as well, for instance in type_keys(ENTER). > > > > > > > > > > > > > > > > Thank you very much! > > -- > > Wulfraed Dennis Lee Bieber AF6VN > > HTTP://wlfraed.home.netcom.com/ From disappearedng at gmail.com Sat Nov 24 16:12:38 2012 From: disappearedng at gmail.com (disappearedng at gmail.com) Date: Sat, 24 Nov 2012 13:12:38 -0800 (PST) Subject: Setup.py not respecting package_dir during develop Message-ID: <937429b4-ad99-4486-bd05-5589082dce87@googlegroups.com> This is my current project setup: . ??? README.md ??? build ? ??? bdist.macosx-10.8-intel ? ??? lib ??? dist ? ??? giordano-0.1-py2.7.egg ??? giordano.egg-info ? ??? PKG-INFO ? ??? SOURCES.txt ? ??? dependency_links.txt ? ??? not-zip-safe ? ??? top_level.txt ??? requirements.txt ??? setup.py ??? src ? ??? giordano ? ??? spider ??? test.txt ??? venv ??? bin ??? include ??? lib ??? share And this is my setup file: from setuptools import setup setup(name='giordano', version='0.1', packages=['giordano'], package_dir={'giordano': 'src/giordano'}, zip_safe=False) When I do python setup.py install, I am able to `import giordano` in my code without problems. However, when I am doing python setup.py develop, this is the console output: [venv] fixSetup$ python setup.py develop running develop running egg_info writing giordano.egg-info/PKG-INFO writing top-level names to giordano.egg-info/top_level.txt writing dependency_links to giordano.egg-info/dependency_links.txt reading manifest file 'giordano.egg-info/SOURCES.txt' writing manifest file 'giordano.egg-info/SOURCES.txt' running build_ext Creating /Users/blah/Dropbox/projects/Giordano/venv/lib/python2.7/site-packages/giordano.egg-link (link to .) Removing giordano 0.1 from easy-install.pth file Adding giordano 0.1 to easy-install.pth file Installed /Users/blah/Dropbox/projects/Giordano Processing dependencies for giordano==0.1 Finished processing dependencies for giordano==0.1 This is what the .egg.link looks like: /Users/blah/Dropbox/projects/Giordano . I can no longer `import giordano` in my code now. **If I `touch src/__init__.py`, I am able to `from src import giordano` anywhere outside of this directory. So I am 100% certain that the egg is pointing to here rather than what package_dir specified.** Any ideas why develop is not respecting package_dir? From michael.herrmann at getautoma.com Sat Nov 24 16:59:38 2012 From: michael.herrmann at getautoma.com (Michael Herrmann) Date: Sat, 24 Nov 2012 13:59:38 -0800 (PST) Subject: 10 sec poll - please reply! In-Reply-To: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> Message-ID: <2e27a085-6e5a-4dee-906f-b6017f1fcd37@googlegroups.com> Hey, how about 'write' instead of 'enter'? write("Hello World!") write("Brick Lane", into="Street") This avoids the ambiguity of whether 'enter' ends by pressing ENTER or not. Thanks, Michael On Tuesday, November 20, 2012 1:18:38 PM UTC+1, Michael Herrmann wrote: > Hi, > > > > I'm developing a GUI Automation library (http://www.getautoma.com) and am having difficulty picking a name for the function that simulates key strokes. I currently have it as 'type' but that clashes with the built-in function. Example uses of 'type': > > > > type(ENTER) > > > > type("Hello World!") > > > > type(CTRL + 'a') > > > > What, in your view, would be the most intuitive alternative name? > > > > Here are my thoughts so far: I could call it 'press' but then our GUI automation tool also allows you to click things and then "press" might be mistaken for "pressing a button". A less ambiguous alternative is "type_keys" but that is rather long and doesn't read as well, for instance in type_keys(ENTER). > > > > Thank you very much! From michael.herrmann at getautoma.com Sat Nov 24 17:32:19 2012 From: michael.herrmann at getautoma.com (Michael Herrmann) Date: Sat, 24 Nov 2012 14:32:19 -0800 (PST) Subject: Is it bad style to override the built-in function `type`? In-Reply-To: References: <5b8a0a43-c737-4543-99e3-76e44c544a02@googlegroups.com> Message-ID: Hi, how about "write" instead of "type"? Just came to me in a flash of inspiration. I know it's also pretty general but at least it's not a built-in! Thanks! Michael On Friday, November 23, 2012 11:30:18 PM UTC+1, Cameron Simpson wrote: > On 23Nov2012 10:41, Michael Herrmann <> wrote: > > [...] > > | I know it's a common beginner's mistake to incautiously override > > | built-in functions. However, we put in a lot of research and have come to > > | the conclusion that, if Python had not already defined it, `type` would > > | be the best name. We are now trying to evaluate how bad the disadvantages > > | you mention are in comparison to the advantage to having a name that is > > | more intuitive to use in the problem domain. > > | > > | Can you somehow relate to my explanations, or are your experiences > > | with overwriting built-in variables so bad that you would advise to > > | never ever do it? > > > > My own experience says that it is a thing best avoiding without a truly > > amazing reason not to. > > > > I urge you not to: type(foo) is a very basic Python idiom and you're > > breaking it. One day it _will_ bite you or your users. You will > > understand, but I would give goods odds that some of your users will not > > the day they go to examine the type of an object for perfectly normal > > pythonic reasons. > > > > Example: I have a module that stores "objects" and they have as a > > primary key a "name" and a "type" - not Python types, just strings. > > Accordingly I have a similar situation to yours: the desire to use the > > word "type". Fortunately for me, as an attribute in (usually small) code > > chunks I can usually go: > > > > t = foo.type > > ... work with t here ... > > > > Where I must pass one as a parameter I use the common convention of > > naming the parameter "type_" at the receiving end. > > > > For the calling end, as in your case, you want to use: > > > > type(blah) > > > > Is it at all possible to make all uses of your "type" function method > > calls? Eg: > > > > something.type("text to type") > > > > It avoids the overloading while keeping your desired name. > > -- > > Cameron Simpson > > > > Wouldn't it be great if all emergency stopping situations occurred on your > > favourite bit of road......you'd probably know about it before it happened > > and would be able to take other evasive action. > > - Neville Brabet From michael.herrmann at getautoma.com Sat Nov 24 17:32:19 2012 From: michael.herrmann at getautoma.com (Michael Herrmann) Date: Sat, 24 Nov 2012 14:32:19 -0800 (PST) Subject: Is it bad style to override the built-in function `type`? In-Reply-To: References: <5b8a0a43-c737-4543-99e3-76e44c544a02@googlegroups.com> Message-ID: Hi, how about "write" instead of "type"? Just came to me in a flash of inspiration. I know it's also pretty general but at least it's not a built-in! Thanks! Michael On Friday, November 23, 2012 11:30:18 PM UTC+1, Cameron Simpson wrote: > On 23Nov2012 10:41, Michael Herrmann <> wrote: > > [...] > > | I know it's a common beginner's mistake to incautiously override > > | built-in functions. However, we put in a lot of research and have come to > > | the conclusion that, if Python had not already defined it, `type` would > > | be the best name. We are now trying to evaluate how bad the disadvantages > > | you mention are in comparison to the advantage to having a name that is > > | more intuitive to use in the problem domain. > > | > > | Can you somehow relate to my explanations, or are your experiences > > | with overwriting built-in variables so bad that you would advise to > > | never ever do it? > > > > My own experience says that it is a thing best avoiding without a truly > > amazing reason not to. > > > > I urge you not to: type(foo) is a very basic Python idiom and you're > > breaking it. One day it _will_ bite you or your users. You will > > understand, but I would give goods odds that some of your users will not > > the day they go to examine the type of an object for perfectly normal > > pythonic reasons. > > > > Example: I have a module that stores "objects" and they have as a > > primary key a "name" and a "type" - not Python types, just strings. > > Accordingly I have a similar situation to yours: the desire to use the > > word "type". Fortunately for me, as an attribute in (usually small) code > > chunks I can usually go: > > > > t = foo.type > > ... work with t here ... > > > > Where I must pass one as a parameter I use the common convention of > > naming the parameter "type_" at the receiving end. > > > > For the calling end, as in your case, you want to use: > > > > type(blah) > > > > Is it at all possible to make all uses of your "type" function method > > calls? Eg: > > > > something.type("text to type") > > > > It avoids the overloading while keeping your desired name. > > -- > > Cameron Simpson > > > > Wouldn't it be great if all emergency stopping situations occurred on your > > favourite bit of road......you'd probably know about it before it happened > > and would be able to take other evasive action. > > - Neville Brabet From cs at zip.com.au Sat Nov 24 17:47:15 2012 From: cs at zip.com.au (Cameron Simpson) Date: Sun, 25 Nov 2012 09:47:15 +1100 Subject: Is it bad style to override the built-in function `type`? In-Reply-To: References: Message-ID: <20121124224714.GA20908@cskk.homeip.net> On 24Nov2012 14:32, Michael Herrmann wrote: | how about "write" instead of "type"? Just came to me in a flash of inspiration. I know it's also pretty general but at least it's not a built-in! +1 -- Cameron Simpson Cars making a sudden U-turn are the most dangerous. They may cut you off entirely, blocking the whole roadway and leaving you no place to go. - MSF Motorcycle Operator Manual, sixth rev. 1991, page 21 From frednotbob at hotmail.ca Sat Nov 24 21:31:13 2012 From: frednotbob at hotmail.ca (Graham Fielding) Date: Sat, 24 Nov 2012 18:31:13 -0800 Subject: Is it bad style to override the built-in function `type`? In-Reply-To: References: <5b8a0a43-c737-4543-99e3-76e44c544a02@googlegroups.com>, , Message-ID: (This comes from my experience writing interactive fiction with the TADS workbench; YMMV) As a rule, it's generally unwise to modify built-in variables without thoroughly documenting your changes. Your modifications might not hold up if the built-in definitions are revised (which would break your program) and you could introduce later version/compatibility issues with other things that use/need the 'standard' definitions. That said, if done cautiously, modifying the built-ins can have a net benefit; just be careful to indicate what you've changed so you can debug it properly, and -- if possible -- distribute yoru modified code along with a copy of the original, so that if issues arise, your users can restore the stable code without digging around in the guts of the software. > > > > [...] > > > > | I know it's a common beginner's mistake to incautiously override > > > > | built-in functions. However, we put in a lot of research and have come to > > > > | the conclusion that, if Python had not already defined it, `type` would > > > > | be the best name. We are now trying to evaluate how bad the disadvantages > > > > | you mention are in comparison to the advantage to having a name that is > > > > | more intuitive to use in the problem domain. > > > > | > > > > | Can you somehow relate to my explanations, or are your experiences > > > > | with overwriting built-in variables so bad that you would advise to > > > > | never ever do it? > > > > > > > > My own experience says that it is a thing best avoiding without a truly > > > > amazing reason not to. > > > > > > > > I urge you not to: type(foo) is a very basic Python idiom and you're > > > > breaking it. One day it _will_ bite you or your users. You will > > > > understand, but I would give goods odds that some of your users will not > > > > the day they go to examine the type of an object for perfectly normal > > > > pythonic reasons. > > > > > > > > Example: I have a module that stores "objects" and they have as a > > > > primary key a "name" and a "type" - not Python types, just strings. > > > > Accordingly I have a similar situation to yours: the desire to use the > > > > word "type". Fortunately for me, as an attribute in (usually small) code > > > > chunks I can usually go: > > > > > > > > t = foo.type > > > > ... work with t here ... > > > > > > > > Where I must pass one as a parameter I use the common convention of > > > > naming the parameter "type_" at the receiving end. > > > > > > > > For the calling end, as in your case, you want to use: > > > > > > > > type(blah) > > > > > > > > Is it at all possible to make all uses of your "type" function method > > > > calls? Eg: > > > > > > > > something.type("text to type") > > > > > > > > It avoids the overloading while keeping your desired name. > > > > -- > > > > Cameron Simpson > > > > > > > > Wouldn't it be great if all emergency stopping situations occurred on your > > > > favourite bit of road......you'd probably know about it before it happened > > > > and would be able to take other evasive action. > > > > - Neville Brabet > > -- > http://mail.python.org/mailman/listinfo/python-list -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Sat Nov 24 22:56:48 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Nov 2012 03:56:48 GMT Subject: 10 sec poll - please reply! References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <818c157c-1395-441d-ad99-955a31b0d523@googlegroups.com> Message-ID: <50b19700$0$29981$c3e8da3$5496439d@news.astraweb.com> Michael, please trim your replies. There is no need to quote nearly 200 lines of previous emails that you don't make direct reference to in your response. We prefer inline quoting here (where you interleave quoted text with the direct response to that quote), but if you must top-post or bottom-post, please trim. Also, please stop sending two copies of every post: you can send an email to python-list at python.org, or you can post a news message to the newsgroup comp.lang.python, but don't do both. You're just flooding both places with two copies of everything you say. -- Steven From steve+comp.lang.python at pearwood.info Sat Nov 24 23:06:13 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Nov 2012 04:06:13 GMT Subject: Is it bad style to override the built-in function `type`? References: <5b8a0a43-c737-4543-99e3-76e44c544a02@googlegroups.com> Message-ID: <50b19934$0$29981$c3e8da3$5496439d@news.astraweb.com> On Sat, 24 Nov 2012 14:32:19 -0800, Michael Herrmann wrote: > Hi, > > how about "write" instead of "type"? Just came to me in a flash of > inspiration. I know it's also pretty general but at least it's not a > built-in! "write" is an extremely common operation with a signature very similar to that of your function you want. The typical use of your function: automata.write("Hello world") # or whatever your module is called looks exactly like writing to the file referred to by the name "automata". Writing to files is *far* more common than using type. Using the standard library for a rough-and-ready test: [steve at ando python3.3]$ grep "[.( ]write(" *.py | wc -l 475 [steve at ando python3.3]$ grep "[.( ]type(" *.py | wc -l 161 If it isn't obvious what I am doing, I am using the Linux "grep" utility to search the Python 3.3 standard library for calls to functions or methods called "write" vs "type". There are nearly three times as many calls to "write". If I inspect the way that the functions are used, the difference is clear: write is nearly always used as a procedure, while type is used as a function. Here are a couple of typical examples: copy.py: return type(x)(x.__func__, deepcopy(x.__self__, memo)) datetime.py: if type(other) != timezone: Your "simulate typing" function does not look like this. It doesn't return anything. It usually gets used as a procedure, not a function, just like the write method: base64.py: output.write(line) formatter.py: write(word) There is far more opportunity for confusion with the name "write" than "type": * writing to files is much more common than calling type, even in expert-level code; * beginners are even less likely to be using builtin type; * a call to your proposed function "type(string)" does not look like a typical call to the builtin type function; * but a call to your proposed function "write(string)" does look very similar, if not identical, to a typical call to write. This is why I maintain that fear of shadowing builtins often becomes superstition, not reasonable, reasoned advice. For fear of one (unlikely) source of confusion, you are prepared to accept a (more likely) source of greater confusion. Writing to files is a very common thing to do. Calling type() is not. Way back in the early days of Python, it was common to use code like: if type(obj) is type([]): ... but that is usually wrong (it rejects subclasses) and inelegant. Normally people will use: if isinstance(obj, list): ... or better still, avoid type-testing altogether. One thing that *doesn't* get done is call builtin type on a literal string, then ignore the result: type("Hello world!") What would be the point? That would be better written: str or even better still, not written at all, since it does nothing sensible. But calling file method "write" with a string, or a string literal, is extremely common, and sensible. Your proposed "write" will look just like writing to a file, when it does something completely different. A couple of days ago I said: [quote] If it were possible to be confused by the two types, e.g. if they took the same arguments but did radically different things, then I would accept that it was too dangerous/confusing to re-use the name. Reasonable fears about shadowing and confusion are, well, reasonable. [end quote] Your proposal to use "write" is exactly the sort of reasonable confusion that I was talking about. -- Steven From cs at zip.com.au Sun Nov 25 01:33:58 2012 From: cs at zip.com.au (Cameron Simpson) Date: Sun, 25 Nov 2012 17:33:58 +1100 Subject: Is it bad style to override the built-in function `type`? In-Reply-To: <50b19934$0$29981$c3e8da3$5496439d@news.astraweb.com> References: <50b19934$0$29981$c3e8da3$5496439d@news.astraweb.com> Message-ID: <20121125063357.GA17181@cskk.homeip.net> On 25Nov2012 04:06, Steven D'Aprano wrote: | On Sat, 24 Nov 2012 14:32:19 -0800, Michael Herrmann wrote: | > how about "write" instead of "type"? Just came to me in a flash of | > inspiration. I know it's also pretty general but at least it's not a | > built-in! | | "write" is an extremely common operation with a signature very similar to | that of your function you want. The typical use of your function: | | automata.write("Hello world") # or whatever your module is called | | looks exactly like writing to the file referred to by the name "automata". Which is actually an argument _for_ his suggestion. [...] | If I inspect the way that the functions are used, the difference is | clear: write is nearly always used as a procedure, while type is used as | a function. [...] | Your "simulate typing" function does not look like this. It doesn't | return anything. It usually gets used as a procedure, not a function, | just like the write method: Again, an argument _for_ his suggestion. | | There is far more opportunity for confusion with the name "write" than | "type": [...] | * but a call to your proposed function "write(string)" does look | very similar, if not identical, to a typical call to write. Again, an argument _for_ his suggestion. Why do I find these reasons to be plusses while you find them minuses? Because you're conveniently glossing over the fact that almost all uses of "write" in the library and common code have an object for context. And I find his suggestion good because for us old UNIX heads, the way you present typed text to a terminal is usually to write it to the master side of a pseudotty, thus: pty.write("typed text here!") The usage is _exactly_ analogous to the conventional uses of write(), because "everything is a file" (one of the UNIX mantras). Writing typed text is the natural way to express this stuff. Your argument seems to be that because his write looks and acts like other write()s it is cause for confusion. My argument is that using the name "write" is a good thing, _because_ his usage looks and acts like the other common uses of write. So I maintain it should cause less confusion. Cheers, -- Cameron Simpson It is a tale told by an idiot, full of sound and fury, signifying nothing. - William Shakespeare From maniandram01 at gmail.com Sun Nov 25 02:02:07 2012 From: maniandram01 at gmail.com (Ramchandra Apte) Date: Sat, 24 Nov 2012 23:02:07 -0800 (PST) Subject: Is it bad style to override the built-in function `type`? In-Reply-To: References: Message-ID: <542e984c-6e8e-43b8-befd-2e7709cf0f9a@googlegroups.com> On Friday, 23 November 2012 21:42:39 UTC+5:30, Michael Herrmann wrote: > Hi, > > > > do you think it's bad style to override the built-in function `type`? I'm co-developing a GUI automation library called Automa (http://www.getautoma.com) and 'type' would be a very fitting name for a function that generates artificial key strokes. > > > > This post is motivated by an already lengthy discussion on this mailing list (http://bit.ly/10aOy4H), where we tried to find alternative names for `type`. Many were found, but none are quite as fitting as 'type'. > > > > For the sake of avoiding a discussion that is already being lead elsewhere please confine this thread to what you generally think about overriding `type`, and post suggestions for alternative names or solutions in the other thread. > > > > Thank you very much! > > Michael Deleting getattr causes problems with IDLE. Also, the accepted answer to http://stackoverflow.com/questions/9109333/is-it-bad-practice-to-use-a-built-in-function-name-as-an-attribute-or-method-ide is not correct. See http://bugs.python.org/issue15113#msg163272 . From maniandram01 at gmail.com Sun Nov 25 02:52:55 2012 From: maniandram01 at gmail.com (Ramchandra Apte) Date: Sat, 24 Nov 2012 23:52:55 -0800 (PST) Subject: only .exe In-Reply-To: References: Message-ID: <5cc0a625-9dfd-4d53-814b-9d8a316ac658@googlegroups.com> On Saturday, 24 November 2012 17:06:57 UTC+5:30, bakie wrote: > in the last question I try how to run py script with pycharm . > > thank dear bro ... > > > > and then I wanna have that program with .exe ( standalone installer ) . How can i do that ? > > > > I tried with two ways > > 1) py2exe > > 2) cx_freeze > > > > but I don't like that It has many file when i complie my script . > > which way for .exe ( standalone installer ) from pycharm . > > > > Thanks U brothers Please, there are women here also. Also, it doesn't compile your script. It only bundles Python and your script in an .exe. From garabik-news-2005-05 at kassiopeia.juls.savba.sk Sun Nov 25 05:18:21 2012 From: garabik-news-2005-05 at kassiopeia.juls.savba.sk (garabik-news-2005-05 at kassiopeia.juls.savba.sk) Date: Sun, 25 Nov 2012 10:18:21 +0000 (UTC) Subject: ANN: unicode 0.9.7 Message-ID: unicode is a simple python command line utility that displays properties for a given unicode character, or searches unicode database for a given name. It was written with Linux in mind, but should work almost everywhere (including MS Windows and MacOSX), UTF-8 console is recommended. ?p??pu??s ?po???u? ??? ?o ?sn p??u??p? pu? s?ld???u???d ??? ?u??????suo??p loo? ??????p??p ?u?ll??x? u? s?? ?I ?s?u??od?po? ?u???????p ?l???ld?o? ?u??sn ?l???? 's?d?l? ?o ?????s ??l?????s ?ll?ns??? o?u?? ?x?? ??? ????uo? o? p??pu??s ?po???u? ??? ?o ???od lln? ??? s???oldx? ???? '????l???n ,?po????d, osl? su????uo? ??????d ??? Changes since previous versions: * add option to recognise binary input numerical codes * do not throw an exception when run under an undefined locale * on error, exit with nonzero existatus * preliminary python3 support * other minor tweaks and improvements URL: http://kassiopeia.juls.savba.sk/~garabik/software/unicode/ -- ----------------------------------------------------------- | Radovan Garab?k http://kassiopeia.juls.savba.sk/~garabik/ | | __..--^^^--..__ garabik @ kassiopeia.juls.savba.sk | ----------------------------------------------------------- Antivirus alert: file .signature infected by signature virus. Hi! I'm a signature virus! Copy me into your signature file to help me spread! From pg.koba at gmail.com Sun Nov 25 05:19:18 2012 From: pg.koba at gmail.com (kobayashi) Date: Sun, 25 Nov 2012 02:19:18 -0800 (PST) Subject: How to get a "screen" length of a multibyte string? Message-ID: <89a1b068-daa1-4f68-ad07-383e5c0fc7aa@googlegroups.com> Hello, Under platform that has fixed pitch font, I want to get a "screen" length of a multibyte string --- sample --- s1 = u"abcdef" s2 = u"???" # It has same "screen" length as s1's. print len(s1) # Got 6 print len(s2) # Got 3, but I want get 6. -------------- Abobe can get a "character" length of a multibyte string. Is there a way to get a "screen" length of a multibyte string? From rosuav at gmail.com Sun Nov 25 06:12:33 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 25 Nov 2012 22:12:33 +1100 Subject: How to get a "screen" length of a multibyte string? In-Reply-To: <89a1b068-daa1-4f68-ad07-383e5c0fc7aa@googlegroups.com> References: <89a1b068-daa1-4f68-ad07-383e5c0fc7aa@googlegroups.com> Message-ID: On Sun, Nov 25, 2012 at 9:19 PM, kobayashi wrote: > Hello, > > Under platform that has fixed pitch font, > I want to get a "screen" length of a multibyte string > > --- sample --- > s1 = u"abcdef" > s2 = u"???" # It has same "screen" length as s1's. > print len(s1) # Got 6 > print len(s2) # Got 3, but I want get 6. > -------------- > > Abobe can get a "character" length of a multibyte string. > Is there a way to get a "screen" length of a multibyte string? What do you mean by screen length? Do you mean the length in bytes? That depends on your encoding. Do you mean width of the displayed version? That depends on your font. ChrisA From hansmu at xs4all.nl Sun Nov 25 06:31:04 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Sun, 25 Nov 2012 12:31:04 +0100 Subject: How to get a "screen" length of a multibyte string? In-Reply-To: <89a1b068-daa1-4f68-ad07-383e5c0fc7aa@googlegroups.com> References: <89a1b068-daa1-4f68-ad07-383e5c0fc7aa@googlegroups.com> Message-ID: <50b2017a$0$6920$e4fe514c@news2.news.xs4all.nl> On 25/11/12 11:19:18, kobayashi wrote: > Hello, > > Under platform that has fixed pitch font, > I want to get a "screen" length of a multibyte string > > --- sample --- > s1 = u"abcdef" > s2 = u"???" # It has same "screen" length as s1's. > print len(s1) # Got 6 > print len(s2) # Got 3, but I want get 6. > -------------- > > Abobe can get a "character" length of a multibyte string. > Is there a way to get a "screen" length of a multibyte string? How about: from unicodedata import east_asian_width def screen_length(s): return sum(2 if east_asian_width(c) == 'W' else 1 for c in s) Hope this helps, -- HansM From alex.b.inside at gmail.com Sun Nov 25 07:11:29 2012 From: alex.b.inside at gmail.com (ALeX inSide) Date: Sun, 25 Nov 2012 04:11:29 -0800 (PST) Subject: How to pass class instance to a method? Message-ID: <3193e3dd-0507-4ff7-9026-ee80e5d9c1dd@googlegroups.com> How to "statically type" an instance of class that I pass to a method of other instance? I suppose there shall be some kind of method decorator to treat an argument as an instance of class? Generally it is needed so IDE (PyCharm) can auto-complete instance's methods and properties. Pseudo-python-code example: i = MyClass() xxx(i, 1, 2); ... def xxx(self, MyClass myclass, number, foobar): myclass.classsmethod() #myclass - is an instance of known class From alex.b.inside at gmail.com Sun Nov 25 07:13:30 2012 From: alex.b.inside at gmail.com (ALeX inSide) Date: Sun, 25 Nov 2012 04:13:30 -0800 (PST) Subject: 10 sec poll - please reply! In-Reply-To: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> Message-ID: press_keys() On Tuesday, November 20, 2012 2:18:38 PM UTC+2, Michael Herrmann wrote: > Hi, > > > > I'm developing a GUI Automation library (http://www.getautoma.com) and am having difficulty picking a name for the function that simulates key strokes. I currently have it as 'type' but that clashes with the built-in function. Example uses of 'type': > > > > type(ENTER) > > > > type("Hello World!") > > > > type(CTRL + 'a') > > > > What, in your view, would be the most intuitive alternative name? > > > > Here are my thoughts so far: I could call it 'press' but then our GUI automation tool also allows you to click things and then "press" might be mistaken for "pressing a button". A less ambiguous alternative is "type_keys" but that is rather long and doesn't read as well, for instance in type_keys(ENTER). > > > > Thank you very much! From steve+comp.lang.python at pearwood.info Sun Nov 25 07:37:35 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Nov 2012 12:37:35 GMT Subject: How to pass class instance to a method? References: <3193e3dd-0507-4ff7-9026-ee80e5d9c1dd@googlegroups.com> Message-ID: <50b2110f$0$29994$c3e8da3$5496439d@news.astraweb.com> On Sun, 25 Nov 2012 04:11:29 -0800, ALeX inSide wrote: > How to "statically type" an instance of class that I pass to a method of > other instance? Please explain what you mean by this. What do you think "statically type" means? > I suppose there shall be some kind of method decorator to treat an > argument as an instance of class? Python does not allow you to lie about the type of an argument. All objects are strongly typed. If an object is a Spam instance, it is a Spam instance, you can't pretend that it is a Cheese instance.[1] > Generally it is needed so IDE (PyCharm) can auto-complete instance's > methods and properties. > > Pseudo-python-code example: > > i = MyClass() > > xxx(i, 1, 2); > > ... > def xxx(self, MyClass myclass, number, foobar): > myclass.classsmethod() #myclass - is an instance of known class I do not understand what you are saying here. Please try to explain more carefully. [1] You cannot lie about the type of an instance, but sometimes you can actually change its type. Use this feature with care, since it rarely is useful. -- Steven From steve+comp.lang.python at pearwood.info Sun Nov 25 08:30:24 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Nov 2012 13:30:24 GMT Subject: How to get a "screen" length of a multibyte string? References: <89a1b068-daa1-4f68-ad07-383e5c0fc7aa@googlegroups.com> Message-ID: <50b21d6f$0$29994$c3e8da3$5496439d@news.astraweb.com> On Sun, 25 Nov 2012 22:12:33 +1100, Chris Angelico wrote: > On Sun, Nov 25, 2012 at 9:19 PM, kobayashi wrote: >> Hello, >> >> Under platform that has fixed pitch font, I want to get a "screen" >> length of a multibyte string >> >> --- sample --- >> s1 = u"abcdef" >> s2 = u"???" # It has same "screen" length as s1's. print len(s1) # Got >> 6 >> print len(s2) # Got 3, but I want get 6. -------------- >> >> Abobe can get a "character" length of a multibyte string. Is there a >> way to get a "screen" length of a multibyte string? > > What do you mean by screen length? Do you mean the length in bytes? That > depends on your encoding. Do you mean width of the displayed version? > That depends on your font. That's what I thought, but on doing some experimentation in my terminal, and doing some googling, I have come to the understanding that so-called monospaced (fixed-width) fonts may support *double column* characters as well as single column. So the OP's example has: s1 = u"abcdef" s2 = u"???" s1 has six single-column ("narrow") characters, while s2 has three double- column ("wide") characters, and both strings should take up the same horizontal space on screen. If you are reading this in a non-monospaced font, the width of each character is not fixed, the idea of columns doesn't really work, and the strings may not be the same width. See http://www.unicode.org/reports/tr11/tr11-19.html for more detail. Interestingly, Unicode supports wide versions of many non-EastAsian characters (presumably because pre-Unicode EastAsian encodings supported them). For example, run this code in Python: print u'\N{FULLWIDTH LATIN CAPITAL LETTER A}'; print u'AA' which should output: ? AA If your font supports this, you should see a single "A" as wide as the double "AA" beneath it. Curiously, in the monospaced font I am using to type this, the "fullwidth" (wide, two-column) A is actually 2/3rds the width of the standard ("halfwidth", narrow, one-column) A. Font designers -- can't live with them, can't take them out and shoot them. Hans Mulder's suggestion: from unicodedata import east_asian_width def screen_length(s): return sum(2 if east_asian_width(c) == 'W' else 1 for c in s) is almost right. The Unicode document above states: [quote] In a broad sense, wide characters include W, F, and A (when in East Asian context), and narrow characters include N, Na, H, and A (when not in East Asian context). [end quote] from unicodedata import east_asian_width def columns(s, eastasian_context=True): if eastasian_context: wide = 'WFA' else: wide = 'WF' return sum(2 if east_asian_width(c) in wide else 1 for c in s) ought to do it for all but the most sophisticated text layout applications. For those needing much more sophistication, see here: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c -- Steven From michael.herrmann at getautoma.com Sun Nov 25 08:41:56 2012 From: michael.herrmann at getautoma.com (Michael Herrmann) Date: Sun, 25 Nov 2012 05:41:56 -0800 (PST) Subject: 10 sec poll - please reply! In-Reply-To: References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <818c157c-1395-441d-ad99-955a31b0d523@googlegroups.com> <780646bd-4caf-4080-b549-cf30a6df0fdf@googlegroups.com> <50aff8eb$0$29975$c3e8da3$5496439d@news.astraweb.com> <12f7d5d3-58af-4c5e-a6ee-e064e198c4bd@googlegroups.com> Message-ID: <01797ea2-8458-4123-bed1-eba177a6302f@googlegroups.com> On Sunday, November 25, 2012 12:23:13 AM UTC+1, Dennis Lee Bieber wrote: > ... > Pardon? In ASCII (and encodings that share the first 128 positions), > > a TAB is x09. > > > > >>> def show(c): > > ... print "%r is 0x%2.2X" % (c, ord(c)) > > ... > > >>> show(raw_input()[0]) > > i > > 'i' is 0x69 > > >>> show(raw_input()[0]) > > > > '\t' is 0x09 > > >>> > > > > My "input" for the second was > > > > Typically, keyboard/console interfaces generate > > - 0x60 when the control key is held down. > > Lowercase "i" is 0x69; minuse 0x60 give 0x09, which is the TAB > > character. > > > > A GUI interface, however, may capture the combination for some other > > usage. Thanks! I did not know that. Michael From michael.herrmann at getautoma.com Sun Nov 25 08:45:30 2012 From: michael.herrmann at getautoma.com (Michael Herrmann) Date: Sun, 25 Nov 2012 05:45:30 -0800 (PST) Subject: 10 sec poll - please reply! In-Reply-To: <50b19700$0$29981$c3e8da3$5496439d@news.astraweb.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> <818c157c-1395-441d-ad99-955a31b0d523@googlegroups.com> <50b19700$0$29981$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sunday, November 25, 2012 4:56:49 AM UTC+1, Steven D'Aprano wrote: > Michael, please trim your replies. There is no need to quote nearly 200 > lines of previous emails that you don't make direct reference to in your > response. We prefer inline quoting here (where you interleave quoted text > with the direct response to that quote), but if you must top-post or > bottom-post, please trim. Sorry - I'm using the Web interface for Google Groups and it hides the quoted previous emails from me. I will take care in the future. > Also, please stop sending two copies of every post: you can send an email > to [email hidden], or you can post a news message to the > newsgroup comp.lang.python, but don't do both. You're just flooding both > places with two copies of everything you say. Same - the Google groups web interface sometimes did that by default. I'll double check the recipients in the future. I'm sorry! Michael From pg.koba at gmail.com Sun Nov 25 08:48:25 2012 From: pg.koba at gmail.com (kobayashi) Date: Sun, 25 Nov 2012 05:48:25 -0800 (PST) Subject: How to get a "screen" length of a multibyte string? In-Reply-To: References: <89a1b068-daa1-4f68-ad07-383e5c0fc7aa@googlegroups.com> Message-ID: <369102e2-d289-4e59-9711-2f01ee5b5c2f@googlegroups.com> Encoding is utf-8. I use "screen length" means as that; that of ascii character is 1, and that of character having double width than ascii character is 2. It's not bytes, but drawing width. As you say, it depends font. I'll be considering carefully. From pg.koba at gmail.com Sun Nov 25 08:48:25 2012 From: pg.koba at gmail.com (kobayashi) Date: Sun, 25 Nov 2012 05:48:25 -0800 (PST) Subject: How to get a "screen" length of a multibyte string? In-Reply-To: References: <89a1b068-daa1-4f68-ad07-383e5c0fc7aa@googlegroups.com> Message-ID: <369102e2-d289-4e59-9711-2f01ee5b5c2f@googlegroups.com> Encoding is utf-8. I use "screen length" means as that; that of ascii character is 1, and that of character having double width than ascii character is 2. It's not bytes, but drawing width. As you say, it depends font. I'll be considering carefully. From pg.koba at gmail.com Sun Nov 25 08:50:03 2012 From: pg.koba at gmail.com (kobayashi) Date: Sun, 25 Nov 2012 05:50:03 -0800 (PST) Subject: How to get a "screen" length of a multibyte string? In-Reply-To: <50b2017a$0$6920$e4fe514c@news2.news.xs4all.nl> References: <89a1b068-daa1-4f68-ad07-383e5c0fc7aa@googlegroups.com> <50b2017a$0$6920$e4fe514c@news2.news.xs4all.nl> Message-ID: <11c58dab-7c7e-4a2d-ae7d-db818bb073ed@googlegroups.com> Great, It's a just good solution. I use it. Thanks, From pg.koba at gmail.com Sun Nov 25 09:02:44 2012 From: pg.koba at gmail.com (kobayashi) Date: Sun, 25 Nov 2012 06:02:44 -0800 (PST) Subject: How to get a "screen" length of a multibyte string? In-Reply-To: <50b21d6f$0$29994$c3e8da3$5496439d@news.astraweb.com> References: <89a1b068-daa1-4f68-ad07-383e5c0fc7aa@googlegroups.com> <50b21d6f$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4deb7e45-624d-441e-92f3-12b094e31ae1@googlegroups.com> I'm greateful for more detailed information and better code. I learned a lot and I use it. Thanks, From feliphil at gmx.net Sun Nov 25 14:48:16 2012 From: feliphil at gmx.net (Wolfgang Keller) Date: Sun, 25 Nov 2012 20:48:16 +0100 Subject: Migrate from Access 2010 / VBA References: <6db4ed18-a16e-4cd1-940b-c10f51207780@googlegroups.com> Message-ID: <20121125204816.97f486775f8b1e7e575a7b0a@gmx.net> > I am the lone developer of db apps at a company of 350+ employees. > Everything is done in MS Access 2010 and VBA. I'm frustrated with the > limitations of this platform and have been considering switching to > Python. > > I've been experimenting with the language for a year or so, > and feel comfortable with the basics. > > I am concerned that I'll have a hard time replacing the access form > and report designers. I've worked a little with TKinter, but it's a > far cry from the GUI designer in Access. The list of Python frameworks for rapid development of desktop (i.e. non-Web) database applications currently contains: using PyQt (& Sqlalchemy): Pypapi: www.pypapi.org Camelot: www.python-camelot.com Qtalchemy: www.qtalchemy.org using PyGTK: Sqlkit: sqlkit.argolinux.org (also uses Sqlalchemy) Kiwi: www.async.com.br/projects/kiwi using wxPython: Dabo: www.dabodev.com Defis: sourceforge.net/projects/defis (Russian only) GNUe: www.gnuenterprise.org Pypapi, Camelot, Sqlkit and Dabo seem to be the most active and best documented/supported ones. > Finding a professional grade report designer looks like an even > bigger challenge. LibreOffice is imho quite useful for database reporting. It comes with a native (SDBC) driver for PostgreSQL and allows Python scripting. LibreOffice Base can even be useful for CRUD GUIs. > I don't need to port any applications, but I will need to use the > data (mdb/accede format), Don't. Put your data into an *actually* transaction-safe RDBMS (which "Jet" is *not*), such as e.g. PostgreSQL. > design a variety of reports with multi-level groupings, and deliver > them to many individual recipients via email. Sincerely, Wolfgang From storchaka at gmail.com Sun Nov 25 15:44:55 2012 From: storchaka at gmail.com (Serhiy Storchaka) Date: Sun, 25 Nov 2012 22:44:55 +0200 Subject: How to get a "screen" length of a multibyte string? In-Reply-To: <89a1b068-daa1-4f68-ad07-383e5c0fc7aa@googlegroups.com> References: <89a1b068-daa1-4f68-ad07-383e5c0fc7aa@googlegroups.com> Message-ID: On 25.11.12 12:19, kobayashi wrote: > Under platform that has fixed pitch font, > I want to get a "screen" length of a multibyte string > > --- sample --- > s1 = u"abcdef" > s2 = u"???" # It has same "screen" length as s1's. > print len(s1) # Got 6 > print len(s2) # Got 3, but I want get 6. > -------------- > > Abobe can get a "character" length of a multibyte string. > Is there a way to get a "screen" length of a multibyte string? http://bugs.python.org/issue12568 From ajel.pat at gmail.com Sun Nov 25 16:33:14 2012 From: ajel.pat at gmail.com (sush@AjelTechnologies) Date: Sun, 25 Nov 2012 13:33:14 -0800 (PST) Subject: Direct Client is looking for two C/C++ Developer in Germantown MD for longterm contract. Message-ID: <596b9a15-54b7-4839-8998-098ffacbdeba@q5g2000vbp.googlegroups.com> Dear Partner, Hope you are doing well. I am a Resource Specialist working for a rapidly growing IT Services company 'Ajel Technologies'. We have a current opportunity which might be interest for you or your consultant. We are looking for a candidate with 7 plus years of experience in C / C++ as a Developer in Germantown MD. Please send your updated resume along with expected rate and contact details at your earliest to am18 (at) ajel (dot) com. Title: Java C/C# Developer Location: Germantown MD Duration: 6 Months Contract (Possible to extend) Start Date: ASAP No of positions: 2 Direct End Client: XEROX Preferred / Desired: Bachelors degree in Computer Science or equivalent. 5 to 7 years experience in OO design and C/C++ development C/C++; PL/SQL; Stored procedures Good understanding of data modeling and object oriented concepts Excellent written and verbal communication skills Ability to multi-task across numerous activities background in large scale transaction processing and financial management systems. Expertise in developing high volume transaction processing applications, application-to-application integration, real-time transaction processing and message queuing. Experience working in a team environment where team members are geographically dispersed. Client interaction and presentation skills. Experience with PL/SQL Editor, ETL tools a plus Large scale data migration experience a plus Oracle experience a plus JMS and MQ experience a plus Education and Typical Years Experience BA / BS degree that is relevant to the position. 7+ years of hands-on software development experience in C/C++ programming. Required: 7+ years of hands-on software development experience. Work in a structured environment designing and developing java-based, enterprise class J2EE application solutions Contribute to detailed design documentation (component diagrams, sequence diagrams, etc.) Review high-level and detailed designs for accuracy and completeness Translate use cases, sequence diagrams, class diagrams into source code. Work with QA/Test team to resolve deficiencies background with systems operating in a 24x7 production environment. Experience in scripting and using source code control systems. Analysis and design skills using formal methodologies and Object Oriented Programming principles. Relational database experience, Oracle preferred. Demonstrated ability to meet schedules and multi-task. Outstanding oral and written communications skills in both technical and client facing situations. Ability to check ones ego at the door. Individual must be a team player, receptive to new ideas and concepts and willing to embrace them when final determinations are made. ** Please ignore the email in case you are not interested / over qualified *** Contact me directly for other positions country wise. We have openings with our clients throughout the country that might match your job title and skills. I am here to help you in getting your dream job. If you are available and interested please forward your Resume to me am18 (at) ajel (dot) com or please call me ASAP at (732) 476-6023. If you do respond via e-mail please include a daytime phone number so I can reach you. If you are not interested, kindly refer anyone who would be interested. We offer great referrals! Thank you in advance! Sincerely yours, AM18-Sushma Patnaik am18 at ajel.com (732) 476-6023 Ajel Technologies Inc www.ajel.com http://www.linkedin.com/in/sushmajel Note: Please allow me to reiterate that I chose to contact you either because your resume had been posted to one of the internet job sites to which we subscribe, or you had previously submitted your resume to Ajel. I assumed that you are either looking for a new employment opportunity, or you are interested in investigating the current job market. If you are not currently seeking employment, or if you would prefer I contact you at some later date, please indicate your date of availability so that I may honor your request. In any event, I respectfully recommend you continue to avail yourself to the employment options and job market information we provide with our e- mail notices. From sagittarus999 at gmail.com Sun Nov 25 17:09:49 2012 From: sagittarus999 at gmail.com (bakie) Date: Sun, 25 Nov 2012 14:09:49 -0800 (PST) Subject: only .exe In-Reply-To: References: Message-ID: how to make to complie my python code ? From sagittarus999 at gmail.com Sun Nov 25 17:12:35 2012 From: sagittarus999 at gmail.com (bakie) Date: Sun, 25 Nov 2012 14:12:35 -0800 (PST) Subject: only .exe In-Reply-To: References: Message-ID: <9d56ca2a-9b2c-44ee-a11d-ad9617527b7b@googlegroups.com> hammmm !!! Is it not complie ? plz see bro / woman http://effbot.org/zone/python-compile.htm how to make only excutable file for python code ? From greg.ewing at canterbury.ac.nz Sun Nov 25 17:22:59 2012 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Mon, 26 Nov 2012 11:22:59 +1300 Subject: How to pass class instance to a method? In-Reply-To: <3193e3dd-0507-4ff7-9026-ee80e5d9c1dd@googlegroups.com> References: <3193e3dd-0507-4ff7-9026-ee80e5d9c1dd@googlegroups.com> Message-ID: ALeX inSide wrote: > I suppose there shall be some kind of method decorator to treat an argument as an > instance of class? You can do this: xxx = MyClass.some_method and then i = MyClass() xxx(i, foo, bar) Does that help? -- Greg From steve+comp.lang.python at pearwood.info Sun Nov 25 18:50:58 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Nov 2012 23:50:58 GMT Subject: only .exe References: Message-ID: <50b2aee2$0$11104$c3e8da3@news.astraweb.com> On Sun, 25 Nov 2012 14:09:49 -0800, bakie wrote: > how to make to complie my python code ? python -m compileall filename.py If this does not answer your question, please explain your question more carefully. We are not mind-readers, we cannot tell what you are thinking, only what you post. -- Steven From driscoll at cs.wisc.edu Sun Nov 25 19:58:55 2012 From: driscoll at cs.wisc.edu (Evan Driscoll) Date: Sun, 25 Nov 2012 18:58:55 -0600 Subject: How to get a "screen" length of a multibyte string? In-Reply-To: <369102e2-d289-4e59-9711-2f01ee5b5c2f@googlegroups.com> References: <89a1b068-daa1-4f68-ad07-383e5c0fc7aa@googlegroups.com> <369102e2-d289-4e59-9711-2f01ee5b5c2f@googlegroups.com> Message-ID: <50B2BECF.7070103@cs.wisc.edu> On 11/25/2012 07:48 AM, kobayashi wrote: > Encoding is utf-8. > I use "screen length" means as that; that of ascii character is 1, and that of character having double width than ascii character is 2. > It's not bytes, but drawing width. > As you say, it depends font. I'll be considering carefully. > Don't forget also that there are combining characters. To wit: >>> "\u00e1" '?' >>> "\u0061\u0301" 'a?' (U+00e1 is an 'a' with acute accent; U+0061 is an unaccented 'a'; U+0301 is an combining acute accent.) So far the discussion has been on single Unicode code points which appear as a double-wide glyph (I did not know about those!); depending on how you want to look at it, combining characters result in sequences of Unicode code points which result in a single glyph, or combining characters are zero-width code points. Evan From smaran.harihar at gmail.com Sun Nov 25 21:09:06 2012 From: smaran.harihar at gmail.com (Smaran Harihar) Date: Sun, 25 Nov 2012 19:09:06 -0700 Subject: Convert tuples into string Message-ID: Hi Guys, I am connecting to postgres database and fetching the table names but it seems that they are being returned in How can I convert them to string? -- Thanks & Regards Smaran Harihar -------------- next part -------------- An HTML attachment was scrubbed... URL: From cs at zip.com.au Sun Nov 25 21:21:43 2012 From: cs at zip.com.au (Cameron Simpson) Date: Mon, 26 Nov 2012 13:21:43 +1100 Subject: Convert tuples into string In-Reply-To: References: Message-ID: <20121126022143.GA29892@cskk.homeip.net> On 25Nov2012 19:09, Smaran Harihar wrote: | I am connecting to postgres database and fetching the table names but it | seems that they are being returned in | | How can I convert them to string? Please show: - the code fetching the names; we do not know what library you are using or how your are asking for the table names - the tuple itself you get back Printing repr(the-tuple-you-got-back) should present something meaningful. I'd imagine it is just a tuple of table name strings, but really I have no idea from what you have said. Cheers, -- Cameron Simpson What's the point of having Nebraska if we can't put it to its highest and best use? - Patrick Bedard arguing for a 100 mph speed limit From smaran.harihar at gmail.com Sun Nov 25 21:24:56 2012 From: smaran.harihar at gmail.com (Smaran Harihar) Date: Sun, 25 Nov 2012 19:24:56 -0700 Subject: Convert tuples into string In-Reply-To: References: Message-ID: I was able to solve it using the following loop, conn=psycopg2.connect(connstr) cursor=conn.cursor() cursor.execute("SELECT tablename FROM pg_tables where tablename like '%"+ inp +"%'") records = cursor.fetchall() str="" for rec in records: if str == "": str="".join(rec) else: m="".join(rec) str=str+","+m Thanks, Smaran On Sun, Nov 25, 2012 at 7:09 PM, Smaran Harihar wrote: > Hi Guys, > > I am connecting to postgres database and fetching the table names but it > seems that they are being returned in > > How can I convert them to string? > > -- > Thanks & Regards > Smaran Harihar > > -- Thanks & Regards Smaran Harihar -------------- next part -------------- An HTML attachment was scrubbed... URL: From python.list at tim.thechases.com Sun Nov 25 22:26:48 2012 From: python.list at tim.thechases.com (Tim Chase) Date: Sun, 25 Nov 2012 21:26:48 -0600 Subject: Convert tuples into string In-Reply-To: References: Message-ID: <50B2E178.6090102@tim.thechases.com> On 11/25/12 20:24, Smaran Harihar wrote: > I was able to solve it using the following loop, > > conn=psycopg2.connect(connstr) > cursor=conn.cursor() > cursor.execute("SELECT tablename FROM pg_tables where tablename like '%"+ > inp +"%'") > records = cursor.fetchall() > str="" > for rec in records: > if str == "": > str="".join(rec) > else: > m="".join(rec) > str=str+","+m Sounds like you want the first element of the tuple which you can access either as "row[0]" or by unpacking the tuple into a one-element tuple. The first would look something like cursor.execute(...) s = ",".join(rec[0] for rec in cursor.fetchall()) while the second would look something like cursor.execute(...) s = ",".join(tablename for (tablename,) in cursor.fetchall()) Note that I'm directly building the string with .join rather than appending (the growth of which has bad performance in Python). -tkc From alain at dpt-info.u-strasbg.fr Mon Nov 26 05:08:37 2012 From: alain at dpt-info.u-strasbg.fr (Alain Ketterlin) Date: Mon, 26 Nov 2012 11:08:37 +0100 Subject: How to pass class instance to a method? References: <3193e3dd-0507-4ff7-9026-ee80e5d9c1dd@googlegroups.com> Message-ID: <87ip8s65i2.fsf@dpt-info.u-strasbg.fr> ALeX inSide writes: > How to "statically type" an instance of class that I pass to a method > of other instance? Python does not do static typing. > I suppose there shall be some kind of method decorator to treat an > argument as an instance of class? Decorators are an option. Another is the use of the new parameter annotations (param : expr) in function/method parameters. (That's python 3, not 2). > Generally it is needed so IDE (PyCharm) can auto-complete instance's > methods and properties. You can't expect static info on the class of the object referenced by any name, unless you impose strong conventions on the code. > Pseudo-python-code example: > > i = MyClass() > > xxx(i, 1, 2); > > ... > def xxx(self, MyClass myclass, number, foobar): > myclass.classsmethod() #myclass - is an instance of known class Could: xxx(self,myclass : MyClass, ...) -- Alain. From dachakku at gmail.com Mon Nov 26 05:36:02 2012 From: dachakku at gmail.com (dachakku at gmail.com) Date: Mon, 26 Nov 2012 02:36:02 -0800 (PST) Subject: how to pass "echo t | " input to subprocess.check_output() method Message-ID: Hi all, I want to list the repositories in svn using python. For this i have used below command, " res = subprocess.check_output(["svn.exe", "list", "Https://127.0.0.1:443/svn/Repos"], stderr=subprocess.STDOUT) " but it throws an exception, since it requires an user input to validate certificate, " (R)eject, accept (t)emporarily or accept (p)ermanently? " from Command prompt im able to pass the input while calling the process, and im able to get the output "echo t | svn list Https://127.0.0.1:443/svn/Repos" But i dont know how to pass the "echo t | " in subprocess.check_output while calling a process. Is there a way to do this? Please help. From clp2 at rebertia.com Mon Nov 26 05:48:10 2012 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 26 Nov 2012 02:48:10 -0800 Subject: how to pass "echo t | " input to subprocess.check_output() method In-Reply-To: References: Message-ID: On Nov 26, 2012 2:41 AM, wrote: > > Hi all, > > I want to list the repositories in svn using python. For this i have used below command, > " res = subprocess.check_output(["svn.exe", "list", " Https://127.0.0.1:443/svn/Repos"], stderr=subprocess.STDOUT) " > > but it throws an exception, since it requires an user input to validate certificate, > " (R)eject, accept (t)emporarily or accept (p)ermanently? " > > from Command prompt im able to pass the input while calling the process, and im able to get the output > > "echo t | svn list Https://127.0.0.1:443/svn/Repos" > > But i dont know how to pass the "echo t | " in subprocess.check_output while calling a process. > Is there a way to do this? Use subprocess.Popen.communicate() instead, passing "t\n" as the input. Cheers, Chris -------------- next part -------------- An HTML attachment was scrubbed... URL: From duncan.booth at invalid.invalid Mon Nov 26 05:52:40 2012 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 26 Nov 2012 10:52:40 GMT Subject: how to pass "echo t | " input to subprocess.check_output() method References: Message-ID: dachakku at gmail.com wrote: > Hi all, > > I want to list the repositories in svn using python. For this i have > used below command, " res = subprocess.check_output(["svn.exe", > "list", "Https://127.0.0.1:443/svn/Repos"], stderr=subprocess.STDOUT) > " > > but it throws an exception, since it requires an user input to > validate certificate, " (R)eject, accept (t)emporarily or accept > (p)ermanently? " > > from Command prompt im able to pass the input while calling the > process, and im able to get the output > > "echo t | svn list Https://127.0.0.1:443/svn/Repos" > > But i dont know how to pass the "echo t | " in subprocess.check_output > while calling a process. Is there a way to do this? > Please help. > Run svn once manually as the same user that is running your script then when you get the prompt verify that it is indeed the certificate and accept it permanently. That will allow your script to work proviuded the certificate doesn't change. Also, change the command you run to include the --non-interactive command line option so that if the certificate ever does change in the future the command will fail rather than prompting. Alternatively use --non-interactive --trust-server-cert to just accept any old server regardless what certificate it uses, but be aware that this impacts security. -- Duncan Booth http://kupuguy.blogspot.com From kushal.kumaran+python at gmail.com Mon Nov 26 06:02:13 2012 From: kushal.kumaran+python at gmail.com (Kushal Kumaran) Date: Mon, 26 Nov 2012 16:32:13 +0530 Subject: how to pass "echo t | " input to subprocess.check_output() method In-Reply-To: References: Message-ID: <50b34c39.0383440a.1825.ffff8fe6@mx.google.com> dachakku at gmail.com writes: > Hi all, > > I want to list the repositories in svn using python. For this i have used below command, > " res = subprocess.check_output(["svn.exe", "list", "Https://127.0.0.1:443/svn/Repos"], stderr=subprocess.STDOUT) " > > but it throws an exception, since it requires an user input to validate certificate, > " (R)eject, accept (t)emporarily or accept (p)ermanently? " > > from Command prompt im able to pass the input while calling the process, and im able to get the output > > "echo t | svn list Https://127.0.0.1:443/svn/Repos" > > But i dont know how to pass the "echo t | " in subprocess.check_output while calling a process. > Is there a way to do this? > Please help. You could pass in a stdin argument to subprocess.check_output with a value of 't\n'. However, you might want to use something like http://pysvn.tigris.org/, which is a python library for accessing subversion repositories. -- regards, kushal From clp2 at rebertia.com Mon Nov 26 06:12:30 2012 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 26 Nov 2012 03:12:30 -0800 Subject: how to pass "echo t | " input to subprocess.check_output() method In-Reply-To: <50b34c39.0383440a.1825.ffff8fe6@mx.google.com> References: <50b34c39.0383440a.1825.ffff8fe6@mx.google.com> Message-ID: On Nov 26, 2012 3:03 AM, "Kushal Kumaran" wrote: > dachakku at gmail.com writes: > > I want to list the repositories in svn using python. For this i have used below command, > > " res = subprocess.check_output(["svn.exe", "list", " Https://127.0.0.1:443/svn/Repos"], stderr=subprocess.STDOUT) " > > > > but it throws an exception, since it requires an user input to validate certificate, > > " (R)eject, accept (t)emporarily or accept (p)ermanently? " > > > > from Command prompt im able to pass the input while calling the process, and im able to get the output > > > > "echo t | svn list Https://127.0.0.1:443/svn/Repos" > > > > But i dont know how to pass the "echo t | " in subprocess.check_output while calling a process. > > You could pass in a stdin argument to subprocess.check_output with a > value of 't\n'. Strings aren't acceptable stdin values, so that wouldn't work. Cheers, Chris -------------- next part -------------- An HTML attachment was scrubbed... URL: From dachakku at gmail.com Mon Nov 26 07:05:37 2012 From: dachakku at gmail.com (dachakku at gmail.com) Date: Mon, 26 Nov 2012 04:05:37 -0800 (PST) Subject: how to pass "echo t | " input to subprocess.check_output() method In-Reply-To: References: Message-ID: <09cde9f8-9c69-4613-a31a-9586bf684d3a@googlegroups.com> On Monday, 26 November 2012 16:22:42 UTC+5:30, Duncan Booth wrote: > dachakku at gmail.com wrote: > > > > > Hi all, > > > > > > I want to list the repositories in svn using python. For this i have > > > used below command, " res = subprocess.check_output(["svn.exe", > > > "list", "Https://127.0.0.1:443/svn/Repos"], stderr=subprocess.STDOUT) > > > " > > > > > > but it throws an exception, since it requires an user input to > > > validate certificate, " (R)eject, accept (t)emporarily or accept > > > (p)ermanently? " > > > > > > from Command prompt im able to pass the input while calling the > > > process, and im able to get the output > > > > > > "echo t | svn list Https://127.0.0.1:443/svn/Repos" > > > > > > But i dont know how to pass the "echo t | " in subprocess.check_output > > > while calling a process. Is there a way to do this? > > > Please help. > > > > > > > Run svn once manually as the same user that is running your script then > > when you get the prompt verify that it is indeed the certificate and > > accept it permanently. That will allow your script to work proviuded the > > certificate doesn't change. > > > > Also, change the command you run to include the --non-interactive > > command line option so that if the certificate ever does change in the > > future the command will fail rather than prompting. > > > > Alternatively use --non-interactive --trust-server-cert to just accept > > any old server regardless what certificate it uses, but be aware that > > this impacts security. > > > > -- > > Duncan Booth http://kupuguy.blogspot.com Hi Duncan, I tried using --non-interactive --trust-server-cert, but the call fails with error message, svn: E175002: OPTIONS of 'https://127.0.0.1/svn/Repos': Server certificate verification failed: certificate issued for a different hostname, issuer is not trusted (https://127.0.0.1) that's why I want to pass an input to accept the certificate (t)emporarily or (p)ermanently. From dachakku at gmail.com Mon Nov 26 07:10:57 2012 From: dachakku at gmail.com (dachakku at gmail.com) Date: Mon, 26 Nov 2012 04:10:57 -0800 (PST) Subject: how to pass "echo t | " input to subprocess.check_output() method In-Reply-To: References: Message-ID: <09f48575-30aa-4d29-8042-2dfcaafe6ca8@googlegroups.com> On Monday, 26 November 2012 16:32:22 UTC+5:30, Kushal Kumaran wrote: > dachakku at gmail.com writes: > > > > > Hi all, > > > > > > I want to list the repositories in svn using python. For this i have used below command, > > > " res = subprocess.check_output(["svn.exe", "list", "Https://127.0.0.1:443/svn/Repos"], stderr=subprocess.STDOUT) " > > > > > > but it throws an exception, since it requires an user input to validate certificate, > > > " (R)eject, accept (t)emporarily or accept (p)ermanently? " > > > > > > from Command prompt im able to pass the input while calling the process, and im able to get the output > > > > > > "echo t | svn list Https://127.0.0.1:443/svn/Repos" > > > > > > But i dont know how to pass the "echo t | " in subprocess.check_output while calling a process. > > > Is there a way to do this? > > > Please help. > > > > > > You could pass in a stdin argument to subprocess.check_output with a > > value of 't\n'. > > > > However, you might want to use something like http://pysvn.tigris.org/, > > which is a python library for accessing subversion repositories. > > > > -- > > regards, > > kushal Hi Kushal, I tried passing the value 't\n' to check_output. But I think we cannot pass a string to stdin. When I tried the below command, subprocess.check_output([svn, "list", repos_Url], stdin='t\n', stderr=subprocess.STDOUT) I got the below error message, File "C:\Python27\lib\subprocess.py", line 786, in _get_handles p2cread = msvcrt.get_osfhandle(stdin.fileno()) AttributeError: 'str' object has no attribute 'fileno' could you tell me how to pass the value to stdin.. From dachakku at gmail.com Mon Nov 26 07:10:57 2012 From: dachakku at gmail.com (dachakku at gmail.com) Date: Mon, 26 Nov 2012 04:10:57 -0800 (PST) Subject: how to pass "echo t | " input to subprocess.check_output() method In-Reply-To: References: Message-ID: <09f48575-30aa-4d29-8042-2dfcaafe6ca8@googlegroups.com> On Monday, 26 November 2012 16:32:22 UTC+5:30, Kushal Kumaran wrote: > dachakku at gmail.com writes: > > > > > Hi all, > > > > > > I want to list the repositories in svn using python. For this i have used below command, > > > " res = subprocess.check_output(["svn.exe", "list", "Https://127.0.0.1:443/svn/Repos"], stderr=subprocess.STDOUT) " > > > > > > but it throws an exception, since it requires an user input to validate certificate, > > > " (R)eject, accept (t)emporarily or accept (p)ermanently? " > > > > > > from Command prompt im able to pass the input while calling the process, and im able to get the output > > > > > > "echo t | svn list Https://127.0.0.1:443/svn/Repos" > > > > > > But i dont know how to pass the "echo t | " in subprocess.check_output while calling a process. > > > Is there a way to do this? > > > Please help. > > > > > > You could pass in a stdin argument to subprocess.check_output with a > > value of 't\n'. > > > > However, you might want to use something like http://pysvn.tigris.org/, > > which is a python library for accessing subversion repositories. > > > > -- > > regards, > > kushal Hi Kushal, I tried passing the value 't\n' to check_output. But I think we cannot pass a string to stdin. When I tried the below command, subprocess.check_output([svn, "list", repos_Url], stdin='t\n', stderr=subprocess.STDOUT) I got the below error message, File "C:\Python27\lib\subprocess.py", line 786, in _get_handles p2cread = msvcrt.get_osfhandle(stdin.fileno()) AttributeError: 'str' object has no attribute 'fileno' could you tell me how to pass the value to stdin.. From duncan.booth at invalid.invalid Mon Nov 26 07:17:21 2012 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 26 Nov 2012 12:17:21 GMT Subject: how to pass "echo t | " input to subprocess.check_output() method References: <09cde9f8-9c69-4613-a31a-9586bf684d3a@googlegroups.com> Message-ID: dachakku at gmail.com wrote: > Hi Duncan, > > I tried using --non-interactive --trust-server-cert, but the call > fails with error message, svn: E175002: OPTIONS of > 'https://127.0.0.1/svn/Repos': Server certificate verification failed: > certificate issued for a different hostname, issuer is not trusted > (https://127.0.0.1) > > that's why I want to pass an input to accept the certificate > (t)emporarily or (p)ermanently. > I think you probably need to configure your web server so the certificate is valid for whichever hostname you use (if the svn server is also used by other machines then connect to it using the external hostname rather than localhost). Or just use http:// configured to allow access through localhost only. -- Duncan Booth http://kupuguy.blogspot.com From kagard at gmail.com Mon Nov 26 07:42:29 2012 From: kagard at gmail.com (kagard) Date: Mon, 26 Nov 2012 04:42:29 -0800 (PST) Subject: Migrate from Access 2010 / VBA References: <6db4ed18-a16e-4cd1-940b-c10f51207780@googlegroups.com> <20121125204816.97f486775f8b1e7e575a7b0a@gmx.net> Message-ID: On Nov 25, 3:48?pm, Wolfgang Keller wrote: > > I am the lone developer of db apps at a company of 350+ employees. > > Everything is done in MS Access 2010 and VBA. I'm frustrated with the > > limitations of this platform and have been considering switching to > > Python. > > > I've been experimenting with the language for a year or so, > > and feel comfortable with the basics. > > > I am concerned that I'll have a hard time replacing the access form > > and report designers. I've worked a little with TKinter, but it's a > > far cry from the GUI designer in Access. > > The list of Python frameworks for rapid development of desktop > (i.e. non-Web) database applications currently contains: > > using PyQt (& Sqlalchemy): > Pypapi:www.pypapi.org > Camelot:www.python-camelot.com > Qtalchemy:www.qtalchemy.org > > using PyGTK: > Sqlkit: sqlkit.argolinux.org (also uses Sqlalchemy) > Kiwi:www.async.com.br/projects/kiwi > > using wxPython: > Dabo:www.dabodev.com > Defis: sourceforge.net/projects/defis (Russian only) > GNUe:www.gnuenterprise.org > > Pypapi, Camelot, Sqlkit and Dabo seem to be the most active and best > documented/supported ones. > > > Finding a professional grade report designer looks like an even > > bigger challenge. > > LibreOffice is imho quite useful for database reporting. It comes with a > native (SDBC) driver for PostgreSQL and allows Python scripting. > LibreOffice Base can even be useful for CRUD GUIs. > > > I don't need to port any applications, but I will need to use the > > data (mdb/accede format), > > Don't. Put your data into an *actually* transaction-safe RDBMS (which > "Jet" is *not*), such as e.g. PostgreSQL. > > > design a variety of reports with multi-level groupings, and deliver > > them to many individual recipients via email. > > Sincerely, > > Wolfgang > > Thanks to everyone who replied. The reporting question is the one that gives me the greatest concern when I think about switching to Python. I haven't seen a simple, powerful report writer like Access has, or Crystal Reports, in Python. Is generating XML / HTML a workable alternative? (In most cases, end users don't need to design reports.) Thanks again, Keith From kushal.kumaran+python at gmail.com Mon Nov 26 08:15:14 2012 From: kushal.kumaran+python at gmail.com (Kushal Kumaran) Date: Mon, 26 Nov 2012 18:45:14 +0530 Subject: how to pass "echo t | " input to subprocess.check_output() method In-Reply-To: <09f48575-30aa-4d29-8042-2dfcaafe6ca8@googlegroups.com> References: <09f48575-30aa-4d29-8042-2dfcaafe6ca8@googlegroups.com> Message-ID: <50b36b66.c4e9440a.69e3.ffffad26@mx.google.com> dachakku at gmail.com writes: > On Monday, 26 November 2012 16:32:22 UTC+5:30, Kushal Kumaran wrote: >> dachakku at gmail.com writes: >> >> >> >> > Hi all, >> >> > >> >> > I want to list the repositories in svn using python. For this i have used below command, >> >> > " res = subprocess.check_output(["svn.exe", "list", "Https://127.0.0.1:443/svn/Repos"], stderr=subprocess.STDOUT) " >> >> > >> >> > but it throws an exception, since it requires an user input to validate certificate, >> >> > " (R)eject, accept (t)emporarily or accept (p)ermanently? " >> >> > >> >> > from Command prompt im able to pass the input while calling the process, and im able to get the output >> >> > >> >> > "echo t | svn list Https://127.0.0.1:443/svn/Repos" >> >> > >> >> > But i dont know how to pass the "echo t | " in subprocess.check_output while calling a process. >> >> > Is there a way to do this? >> >> > Please help. >> >> >> >> >> >> You could pass in a stdin argument to subprocess.check_output with a >> >> value of 't\n'. >> >> >> >> However, you might want to use something like http://pysvn.tigris.org/, >> >> which is a python library for accessing subversion repositories. >> >> >> > > Hi Kushal, > > I tried passing the value 't\n' to check_output. But I think we cannot pass a string to stdin. > > When I tried the below command, > subprocess.check_output([svn, "list", repos_Url], stdin='t\n', stderr=subprocess.STDOUT) > > I got the below error message, > File "C:\Python27\lib\subprocess.py", line 786, in _get_handles > p2cread = msvcrt.get_osfhandle(stdin.fileno()) > AttributeError: 'str' object has no attribute 'fileno' > > could you tell me how to pass the value to stdin.. Follow Chris Rebert's suggestion to use subprocess.Popen and the communicate method of the Popen object. Have you taken a look at pysvn? -- regards, kushal From undesputed.hackerz at gmail.com Mon Nov 26 08:15:56 2012 From: undesputed.hackerz at gmail.com (undesputed.hackerz at gmail.com) Date: Mon, 26 Nov 2012 05:15:56 -0800 (PST) Subject: Regular expression for different date formats in Python Message-ID: <804a4170-a317-482c-8eca-e76027a0ee85@googlegroups.com> Hello Developers, I am a beginner in python and need help with writing a regular expression for date and time to be fetched from some html documents. In the following code I am walking through the html files in a folder called event and printing the headings with h1 tag using beautifulsoup. These html pages also contains different formats of date and time. I want to fetch and display this information as well. Different formats of date in these html documents are: 21 - 27 Nov 2012 1 Dec 2012 30 Nov - 2 Dec 2012 26 Nov 2012 Can someone help me out with fetching these formats from these html documents ? Here is my code for walking through the files and fetching h1 from those html files: Code: import re import os from bs4 import BeautifulSoup for subdir, dirs, files in os.walk("/home/himanshu/event/"): for fle in files: path = os.path.join(subdir, fle) soup = BeautifulSoup(open(path)) print (soup.h1.string) #Date and Time detection From miki.tebeka at gmail.com Mon Nov 26 10:40:02 2012 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Mon, 26 Nov 2012 07:40:02 -0800 (PST) Subject: how to pass "echo t | " input to subprocess.check_output() method In-Reply-To: References: Message-ID: > But i dont know how to pass the "echo t | " in subprocess.check_output while calling a process. You need to create two subprocess and connect the stdout of the first to the stdin of the 2'nd. See http://pythonwise.blogspot.com/2008/08/pipe.html for a possible solution. From torriem at gmail.com Mon Nov 26 11:05:14 2012 From: torriem at gmail.com (Michael Torrie) Date: Mon, 26 Nov 2012 09:05:14 -0700 Subject: Regular expression for different date formats in Python In-Reply-To: <804a4170-a317-482c-8eca-e76027a0ee85@googlegroups.com> References: <804a4170-a317-482c-8eca-e76027a0ee85@googlegroups.com> Message-ID: <50B3933A.2000001@gmail.com> On 11/26/2012 06:15 AM, undesputed.hackerz at gmail.com wrote: > I am a beginner in python and need help with writing a regular > expression for date and time to be fetched from some html documents. Would the "parser" module from the third-party dateutil module work for you? http://pypi.python.org/pypi/python-dateutil http://labix.org/python-dateutil#head-c0e81a473b647dfa787dc11e8c69557ec2c3ecd2 I don't believe the library is updated for Python 3 yet, sadly. But I bet it could be ported fairly easily. I think it's pure python. From torriem at gmail.com Mon Nov 26 11:21:51 2012 From: torriem at gmail.com (Michael Torrie) Date: Mon, 26 Nov 2012 09:21:51 -0700 Subject: Migrate from Access 2010 / VBA In-Reply-To: <6db4ed18-a16e-4cd1-940b-c10f51207780@googlegroups.com> References: <6db4ed18-a16e-4cd1-940b-c10f51207780@googlegroups.com> Message-ID: <50B3971F.9000509@gmail.com> On 11/22/2012 08:19 PM, kgard wrote: > I am the lone developer of db apps at a company of 350+ employees. > Everything is done in MS Access 2010 and VBA. I'm frustrated with the > limitations of this platform and have been considering switching to > Python. I've been experimenting with the language for a year or so, > and feel comfortable with the basics. Python is just a language, just like VBA itself is just a language. You can't just replace an MS Access VBA app with one in Python. You have to replace your *tools* with open source alternatives, that hopefully python can glue together. Wolfgang provided a nice list of such tools. One program that claims to be working towards Access replacement is Kexi. It's not written in Python, but I think it does use Python as a scripting language, just as Access uses VBA. I doubt it's anywhere near Access yet, but it's worth a look: http://kexi-project.org/about.html > > Has anyone here made this transition successfully? If so, could you > pass along your suggestions about how to do this as quickly and > painlessly as possible? It will not be painless at all. There is no "transition" path, really. That's partly the result of Microsoft product lock-in, partly because you want to replace a complete system that happens to be glued together with, simply, "Python." I think Python could be a great fit if you could find the right tools to go with it, but it's not going to be easy at all. Complete MS Access replacements is one of the may extremely weak spots in the open source world. Partly because web-based apps often work better than a desktop DB solution, and you might want to go there too, perhaps using a python web development toolkit like django. From vlastimil.brom at gmail.com Mon Nov 26 11:30:17 2012 From: vlastimil.brom at gmail.com (Vlastimil Brom) Date: Mon, 26 Nov 2012 17:30:17 +0100 Subject: Regular expression for different date formats in Python In-Reply-To: <804a4170-a317-482c-8eca-e76027a0ee85@googlegroups.com> References: <804a4170-a317-482c-8eca-e76027a0ee85@googlegroups.com> Message-ID: 2012/11/26 : > Hello Developers, > > I am a beginner in python and need help with writing a regular expression for date and time to be fetched from some html documents. In the following code I am walking through the html files in a folder called event and printing the headings with h1 tag using beautifulsoup. These html pages also contains different formats of date and time. I want to fetch and display this information as well. Different formats of date in these html documents are: > > 21 - 27 Nov 2012 > 1 Dec 2012 > 30 Nov - 2 Dec 2012 > 26 Nov 2012 > > Can someone help me out with fetching these formats from these html documents ? > Here is my code for walking through the files and fetching h1 from those html files: > > > Code: > > > import re > import os > from bs4 import BeautifulSoup > > for subdir, dirs, files in os.walk("/home/himanshu/event/"): > for fle in files: > path = os.path.join(subdir, fle) > soup = BeautifulSoup(open(path)) > > print (soup.h1.string) > > #Date and Time detection > > -- > http://mail.python.org/mailman/listinfo/python-list Hi, the following pattern seems to match all of your examples, (\d{1,2} )?(Nov|Dec)?( ?- )?(\d{1,2}) (Nov|Dec) (\d{4}) however, it doesn't look like very robust - of course, you have to add the remaining months' abbreviations and check on the (parts of the) HTML documents, you are interested in. hth, vbr From nobody at nowhere.com Mon Nov 26 11:56:15 2012 From: nobody at nowhere.com (Nobody) Date: Mon, 26 Nov 2012 16:56:15 +0000 Subject: How to pass class instance to a method? References: <3193e3dd-0507-4ff7-9026-ee80e5d9c1dd@googlegroups.com> Message-ID: On Sun, 25 Nov 2012 04:11:29 -0800, ALeX inSide wrote: > How to "statically type" an instance of class that I pass to a method of > other instance? Python isn't statically typed. You can explicitly check for a specific type with e.g.: if not isinstance(arg, SomeType): raise TypeError('expected SomeType but got %s' % type(arg)) But this defeats duck typing. If you do this a lot, you're using the wrong language. > I suppose there shall be some kind of method decorator to treat an > argument as an instance of class? > > Generally it is needed so IDE (PyCharm) can auto-complete instance's > methods and properties. You have it backwards. In a dynamically-typed language such as Python, the set of acceptable types for an argument is determined by the operations which the function performs on it. This is in direct contrast to a statically-typed language, where the set of acceptable operations on an argument is determined by the type of the argument. From ramit.prasad at jpmorgan.com Mon Nov 26 15:17:40 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Mon, 26 Nov 2012 20:17:40 +0000 Subject: Getting a seeded value from a list In-Reply-To: References: <993b9303-dbca-43f1-9ff4-9677278776b1@googlegroups.com> <50ac4ee7$0$21790$c3e8da3$76491128@news.astraweb.com> <5B80DD153D7D744689F57F4FB69AF474167D1967@SBECMX007.exchad.jpmchase.net> Message-ID: <5B80DD153D7D744689F57F4FB69AF47418028229@SCACMX008.exchad.jpmchase.net> Chris Angelico wrote: > > On Sat, Nov 24, 2012 at 3:27 AM, Prasad, Ramit > wrote: > > Steven D'Aprano wrote: > >> > >> On Wed, 21 Nov 2012 14:41:24 +1100, Chris Angelico wrote: > >> > >> > However, this still means that the player will see the exact same level > >> > regenerated every time, absolutely fresh. As previously stated in this > >> > thread, that's not usually a good thing for encounters, treasure, etc. > >> > Once some nasty critter has been killed, he should STAY killed! :) > >> > >> Why? That isn't true in real life, why should it be true for games? > >> > > > > It is not true in all games. I have seen games where treasures > > regenerate in the same location except for key items. Same goes > > for enemies (where only "bosses" do not regenerate). It really > > just depends on the type of game you are playing--designing > > in this case. > > Perhaps they regenerate, but do they regenerate from the exact same > random seed? For instance, in Murkon's Refuge, the maps are > handcrafted and thus constant every time you enter a particular level > - but go downstairs and upstairs, and the monsters and treasure > regenerate, different from last time. > > Of course, if the idea is that you're rewinding time, then it makes > good sense for you to see the exact same pattern of enemies. > Hmm. I guess most of the games where I remember "regenerating" enemies are a bit older. Chrono Trigger had enemies that would regenerate if you left a map screen and came back, but then again it seems more likely that the enemies were hard coded and not "generated" at all. Either that or they [games] are like Diablo/Castlevania where monsters are constantly "generated". I still hold the opinion that the seeding behavior depends on the game. I wonder what Nethack does? ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From rosuav at gmail.com Mon Nov 26 15:24:58 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 27 Nov 2012 07:24:58 +1100 Subject: Getting a seeded value from a list In-Reply-To: <5B80DD153D7D744689F57F4FB69AF47418028229@SCACMX008.exchad.jpmchase.net> References: <993b9303-dbca-43f1-9ff4-9677278776b1@googlegroups.com> <50ac4ee7$0$21790$c3e8da3$76491128@news.astraweb.com> <5B80DD153D7D744689F57F4FB69AF474167D1967@SBECMX007.exchad.jpmchase.net> <5B80DD153D7D744689F57F4FB69AF47418028229@SCACMX008.exchad.jpmchase.net> Message-ID: On Tue, Nov 27, 2012 at 7:17 AM, Prasad, Ramit wrote: > Hmm. I guess most of the games where I remember "regenerating" > enemies are a bit older. Chrono Trigger had enemies that > would regenerate if you left a map screen and came back, but > then again it seems more likely that the enemies were hard > coded and not "generated" at all. Either that or they [games] are > like Diablo/Castlevania where monsters are constantly "generated". > > I still hold the opinion that the seeding behavior depends on the > game. I wonder what Nethack does? I don't know what Nethack does, but I know Angband - I used to maintain my own variant (never got as far as publishing, though). Levels are generated completely fresh on first entry; if you go down to level 2, then down to level 3, then up to level 2, it'll be a *different* level 2. No seeds get saved. ChrisA From ian.g.kelly at gmail.com Mon Nov 26 15:51:16 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 26 Nov 2012 13:51:16 -0700 Subject: How to pass class instance to a method? In-Reply-To: References: <3193e3dd-0507-4ff7-9026-ee80e5d9c1dd@googlegroups.com> Message-ID: On Mon, Nov 26, 2012 at 9:56 AM, Nobody wrote: > In a dynamically-typed language such as Python, the set of acceptable > types for an argument is determined by the operations which the function > performs on it. This is in direct contrast to a statically-typed language, > where the set of acceptable operations on an argument is determined by the > type of the argument. Not how I would put it. In a statically typed language, types are checked at compile-time (which does not necessarily imply that useful type information can be made available to an IDE), whereas in a dynamically typed language, some or all type checking is deferred to run-time. The description that "the set of acceptable types for an argument is determined by the operations which the function performs on it" sounds to me more like type inference, as exemplified by Haskell, which is nonetheless a statically typed language. From tolidtm at gmail.com Mon Nov 26 16:08:08 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Mon, 26 Nov 2012 22:08:08 +0100 Subject: Compare list entry from csv files Message-ID: Hello, I'm trying to complete a namebook CSV file with missing phone numbers which are in another CSV file. the namebook file is structured: First name;Lastname; Address; City; Country; Phone number, where the phone number is missing. The phonebook file is structured as: Name; phone, where the name shows first and last name and sometimes they are written together like "BillGates" or "Billgatesmicrosoft". I'm importing the files as lists ex.: phonelist" ["First name", "Last name","address","City"."Country","phone"],[etc...] in the loop I can compare the entry for ex. "Bill Gates" in the field "BillGatesmicrosoft" but I can't index it so I can only take the phone number from the file with the phones and insert it to field in the Namebook. Can you please give me an advice? Thanks import csv origf = open('c:/Working/Test_phonebook.csv', 'rt') phonelist = [] try: reader = csv.reader(origf, delimiter=';') for row in reader: phonelist.append(row) finally: origf.close() secfile = open('c:/Working/phones.csv', 'rt') phones = [] try: readersec = csv.reader(secfile, delimiter=';') for row in readersec: phones.append(row) finally: secfile.close() From miki.tebeka at gmail.com Mon Nov 26 16:57:05 2012 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Mon, 26 Nov 2012 13:57:05 -0800 (PST) Subject: Regular expression for different date formats in Python In-Reply-To: References: <804a4170-a317-482c-8eca-e76027a0ee85@googlegroups.com> Message-ID: <258e1a51-a2dc-40ff-946f-6cf37f3c36eb@googlegroups.com> On Monday, November 26, 2012 8:34:22 AM UTC-8, Michael Torrie wrote: > http://pypi.python.org/pypi/python-dateutil > ... > I don't believe the library is updated for Python 3 yet, sadly. dateutil supports 3.x since version 2.0. From miki.tebeka at gmail.com Mon Nov 26 16:57:05 2012 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Mon, 26 Nov 2012 13:57:05 -0800 (PST) Subject: Regular expression for different date formats in Python In-Reply-To: References: <804a4170-a317-482c-8eca-e76027a0ee85@googlegroups.com> Message-ID: <258e1a51-a2dc-40ff-946f-6cf37f3c36eb@googlegroups.com> On Monday, November 26, 2012 8:34:22 AM UTC-8, Michael Torrie wrote: > http://pypi.python.org/pypi/python-dateutil > ... > I don't believe the library is updated for Python 3 yet, sadly. dateutil supports 3.x since version 2.0. From d at davea.name Mon Nov 26 16:58:47 2012 From: d at davea.name (Dave Angel) Date: Mon, 26 Nov 2012 16:58:47 -0500 Subject: How to pass class instance to a method? In-Reply-To: References: <3193e3dd-0507-4ff7-9026-ee80e5d9c1dd@googlegroups.com> Message-ID: <50B3E617.3040302@davea.name> On 11/26/2012 03:51 PM, Ian Kelly wrote: > On Mon, Nov 26, 2012 at 9:56 AM, Nobody wrote: >> In a dynamically-typed language such as Python, the set of acceptable >> types for an argument is determined by the operations which the function >> performs on it. This is in direct contrast to a statically-typed language, >> where the set of acceptable operations on an argument is determined by the >> type of the argument. > Not how I would put it. In a statically typed language, types are > checked at compile-time (which does not necessarily imply that useful > type information can be made available to an IDE), whereas in a > dynamically typed language, some or all type checking is deferred to > run-time. Not how I would put it. In a statically typed language, the valid types are directly implied by the function parameter declarations, while in a dynamic language, they're defined in the documentation, and only enforced (if at all) by the body of the function. -- DaveA From d at davea.name Mon Nov 26 17:08:37 2012 From: d at davea.name (Dave Angel) Date: Mon, 26 Nov 2012 17:08:37 -0500 Subject: Compare list entry from csv files In-Reply-To: References: Message-ID: <50B3E865.9070700@davea.name> On 11/26/2012 04:08 PM, Anatoli Hristov wrote: > Hello, > > I'm trying to complete a namebook CSV file with missing phone numbers > which are in another CSV file. > the namebook file is structured: > First name;Lastname; Address; City; Country; Phone number, where the > phone number is missing. > > The phonebook file is structured as: > Name; phone, where the name shows first and last name and sometimes > they are written together like "BillGates" or "Billgatesmicrosoft". > > I'm importing the files as lists ex.: phonelist" ["First name", "Last > name","address","City"."Country","phone"],[etc...] > in the loop I can compare the entry for ex. "Bill Gates" in the field > "BillGatesmicrosoft" but I can't index it so I can only take the phone > number from the file with the phones and insert it to field in the > Namebook. Can you please give me an advice? > > Thanks > > > import csv > > origf = open('c:/Working/Test_phonebook.csv', 'rt') > phonelist = [] > > try: > reader = csv.reader(origf, delimiter=';') > for row in reader: > phonelist.append(row) > finally: > origf.close() > > secfile = open('c:/Working/phones.csv', 'rt') > phones = [] > > try: > readersec = csv.reader(secfile, delimiter=';') > for row in readersec: > phones.append(row) > finally: > secfile.close() You're trying to merge information from a second file into a first one, where the shared key is only a little bit similar. Good luck. For example., in the first file, it might say Susan; Gatley and in the other file it might say Mom. Good luck coming up with an algorthm to match those. Now if you are assured that the two will be identical except for spaces, then you could reduce both keys to the same format and then match them. Or if you want to say they're within a Soundex definition of each other. Or if you want to claim that they'll have the same words in them, but not necessarily the same order. But if these files are really as randomly connected as you say, then the best you can probably do is to write two programs. First is where you take the names from each file and produce a 3rd file associating the ones that are obvious (according to some algorithm), then build a list of exceptions. Then allow a human being to edit that file. Then the second file uses it to merge the first two files for your final pass. -- DaveA From steve+comp.lang.python at pearwood.info Mon Nov 26 17:18:29 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 26 Nov 2012 22:18:29 GMT Subject: How to pass class instance to a method? References: <3193e3dd-0507-4ff7-9026-ee80e5d9c1dd@googlegroups.com> Message-ID: <50b3eab5$0$29994$c3e8da3$5496439d@news.astraweb.com> On Mon, 26 Nov 2012 16:58:47 -0500, Dave Angel wrote: > In a statically typed language, the valid types > are directly implied by the function parameter declarations, while in a > dynamic language, they're defined in the documentation, and only > enforced (if at all) by the body of the function. Well that certainly can't be true, because you can write functions without *any* documentation at all, and hence no defined type restrictions that could be enforced: def trivial_example(x): return x+1 No documentation, and so by your definition above this should be weakly typed and operate on any type at all. Since there are no type restrictions defined, the body cannot enforce those type restrictions. But that's clearly not true. Please, everybody, before replying to this thread, please read this: http://cdsmith.wordpress.com/2011/01/09/an-old-article-i-wrote/ -- Steven From tolidtm at gmail.com Mon Nov 26 17:27:58 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Mon, 26 Nov 2012 23:27:58 +0100 Subject: Compare list entry from csv files In-Reply-To: <50B3E865.9070700@davea.name> References: <50B3E865.9070700@davea.name> Message-ID: I understand, but in my case I have for sure the field "Name" in the second file that contains at least the first or the last name on it... So probably it should be possible:) The Name "Billgatesmicrosoft" contains the word "Gates" so logically I might find a solution for it. Thanks On Mon, Nov 26, 2012 at 11:08 PM, Dave Angel wrote: > On 11/26/2012 04:08 PM, Anatoli Hristov wrote: >> Hello, >> >> I'm trying to complete a namebook CSV file with missing phone numbers >> which are in another CSV file. >> the namebook file is structured: >> First name;Lastname; Address; City; Country; Phone number, where the >> phone number is missing. >> >> The phonebook file is structured as: >> Name; phone, where the name shows first and last name and sometimes >> they are written together like "BillGates" or "Billgatesmicrosoft". >> >> I'm importing the files as lists ex.: phonelist" ["First name", "Last >> name","address","City"."Country","phone"],[etc...] >> in the loop I can compare the entry for ex. "Bill Gates" in the field >> "BillGatesmicrosoft" but I can't index it so I can only take the phone >> number from the file with the phones and insert it to field in the >> Namebook. Can you please give me an advice? >> >> Thanks >> >> >> import csv >> >> origf = open('c:/Working/Test_phonebook.csv', 'rt') >> phonelist = [] >> >> try: >> reader = csv.reader(origf, delimiter=';') >> for row in reader: >> phonelist.append(row) >> finally: >> origf.close() >> >> secfile = open('c:/Working/phones.csv', 'rt') >> phones = [] >> >> try: >> readersec = csv.reader(secfile, delimiter=';') >> for row in readersec: >> phones.append(row) >> finally: >> secfile.close() > > You're trying to merge information from a second file into a first one, > where the shared key is only a little bit similar. Good luck. > > For example., in the first file, it might say Susan; Gatley and in the > other file it might say Mom. Good luck coming up with an algorthm to > match those. > > Now if you are assured that the two will be identical except for spaces, > then you could reduce both keys to the same format and then match them. > Or if you want to say they're within a Soundex definition of each > other. Or if you want to claim that they'll have the same words in > them, but not necessarily the same order. > > But if these files are really as randomly connected as you say, then the > best you can probably do is to write two programs. First is where you > take the names from each file and produce a 3rd file associating the > ones that are obvious (according to some algorithm), then build a list > of exceptions. Then allow a human being to edit that file. Then the > second file uses it to merge the first two files for your final pass. > > > -- > > DaveA > From peter at www.pjb.com.au Mon Nov 26 17:33:40 2012 From: peter at www.pjb.com.au (Peter Billam) Date: 26 Nov 2012 22:33:40 GMT Subject: Deep Learning ? Message-ID: Hi :-) I've just been reading: http://en.wikipedia.org/wiki/Deep_Learning and watching: https://www.youtube.com/watch?v=VdIURAu1-aU and find that Very interesting... Is any of this code available to mortals, like in a module in Python or some other language? I'd love to start keeping track of what can be done with this on a normal CPU... Regards, Peter -- Peter Billam www.pjb.com.au www.pjb.com.au/comp/contact.html From emile at fenx.com Mon Nov 26 17:53:52 2012 From: emile at fenx.com (Emile van Sebille) Date: Mon, 26 Nov 2012 14:53:52 -0800 Subject: Compare list entry from csv files In-Reply-To: References: <50B3E865.9070700@davea.name> Message-ID: Anatoli Hristov wrote: > I understand, but in my case I have for sure the field "Name" in the > second file that contains at least the first or the last name on it... > So probably it should be possible:) > The Name "Billgatesmicrosoft" contains the word "Gates" so logically I > might find a solution for it. Take a look at difflib -- I've used it before for this type of 'matching' Emile From ian.g.kelly at gmail.com Mon Nov 26 18:07:10 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 26 Nov 2012 16:07:10 -0700 Subject: How to pass class instance to a method? In-Reply-To: <50B3E617.3040302@davea.name> References: <3193e3dd-0507-4ff7-9026-ee80e5d9c1dd@googlegroups.com> <50B3E617.3040302@davea.name> Message-ID: On Mon, Nov 26, 2012 at 2:58 PM, Dave Angel wrote: > Not how I would put it. In a statically typed language, the valid types > are directly implied by the function parameter declarations, As alluded to in my previous post, not all statically typed languages require parameter type declarations to perform static checking. > while in a > dynamic language, they're defined in the documentation, and only > enforced (if at all) by the body of the function. That's not even true for Python. The following example uses Python 2.x: >>> class Foo(object): ... def method(self): ... pass ... >>> Foo.method(4) Traceback (most recent call last): File "", line 1, in TypeError: unbound method method() must be called with Foo instance as first argument (got int instance instead) That's a run-time check, and it's not enforced by the body of the function. From hansmu at xs4all.nl Mon Nov 26 19:36:04 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Tue, 27 Nov 2012 01:36:04 +0100 Subject: How to pass class instance to a method? In-Reply-To: References: <3193e3dd-0507-4ff7-9026-ee80e5d9c1dd@googlegroups.com> <50B3E617.3040302@davea.name> Message-ID: <50b40af5$0$6973$e4fe514c@news2.news.xs4all.nl> On 27/11/12 00:07:10, Ian Kelly wrote: > On Mon, Nov 26, 2012 at 2:58 PM, Dave Angel wrote: >> Not how I would put it. In a statically typed language, the valid types >> are directly implied by the function parameter declarations, > > As alluded to in my previous post, not all statically typed languages > require parameter type declarations to perform static checking. > >> while in a >> dynamic language, they're defined in the documentation, and only >> enforced (if at all) by the body of the function. > > That's not even true for Python. The following example uses Python 2.x: > >>>> class Foo(object): > ... def method(self): > ... pass > ... >>>> Foo.method(4) > Traceback (most recent call last): > File "", line 1, in > TypeError: unbound method method() must be called with Foo instance as > first argument (got int instance instead) > > That's a run-time check, and it's not enforced by the body of the function. As Ian already knows, this problem has been fixed in Python 3. -- HansM From hansmu at xs4all.nl Mon Nov 26 20:01:10 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Tue, 27 Nov 2012 02:01:10 +0100 Subject: Getting a seeded value from a list In-Reply-To: References: <993b9303-dbca-43f1-9ff4-9677278776b1@googlegroups.com> <50ac4ee7$0$21790$c3e8da3$76491128@news.astraweb.com> <5B80DD153D7D744689F57F4FB69AF474167D1967@SBECMX007.exchad.jpmchase.net> Message-ID: <50b410d8$0$6904$e4fe514c@news2.news.xs4all.nl> On 26/11/12 21:17:40, Prasad, Ramit wrote: > Chris Angelico wrote: >> >> On Sat, Nov 24, 2012 at 3:27 AM, Prasad, Ramit >> wrote: >>> Steven D'Aprano wrote: >>>> >>>> On Wed, 21 Nov 2012 14:41:24 +1100, Chris Angelico wrote: >>>> >>>>> However, this still means that the player will see the exact same level >>>>> regenerated every time, absolutely fresh. As previously stated in this >>>>> thread, that's not usually a good thing for encounters, treasure, etc. >>>>> Once some nasty critter has been killed, he should STAY killed! :) >>>> >>>> Why? That isn't true in real life, why should it be true for games? >>>> >>> >>> It is not true in all games. I have seen games where treasures >>> regenerate in the same location except for key items. Same goes >>> for enemies (where only "bosses" do not regenerate). It really >>> just depends on the type of game you are playing--designing >>> in this case. >> >> Perhaps they regenerate, but do they regenerate from the exact same >> random seed? For instance, in Murkon's Refuge, the maps are >> handcrafted and thus constant every time you enter a particular level >> - but go downstairs and upstairs, and the monsters and treasure >> regenerate, different from last time. >> >> Of course, if the idea is that you're rewinding time, then it makes >> good sense for you to see the exact same pattern of enemies. >> > > Hmm. I guess most of the games where I remember "regenerating" > enemies are a bit older. Chrono Trigger had enemies that > would regenerate if you left a map screen and came back, but > then again it seems more likely that the enemies were hard > coded and not "generated" at all. Either that or they [games] are > like Diablo/Castlevania where monsters are constantly "generated". > > I still hold the opinion that the seeding behavior depends on the > game. I wonder what Nethack does? When you leave a level, Nethack saves it to the hard disk. When you return, the level is reloaded from disk, so monsters and objects appear where they were when you left the level. The map is also saved, so if you had dug an extra corridor on your first visit, it'll be there when you return. The random level generation only happens if you go to a level you haven't visited before in that game (and sometimes not even then). -- HansM From r.drew.davis at gmail.com Mon Nov 26 20:14:45 2012 From: r.drew.davis at gmail.com (Drew) Date: Mon, 26 Nov 2012 17:14:45 -0800 (PST) Subject: How to pass class instance to a method? In-Reply-To: <3193e3dd-0507-4ff7-9026-ee80e5d9c1dd@googlegroups.com> References: <3193e3dd-0507-4ff7-9026-ee80e5d9c1dd@googlegroups.com> Message-ID: <9ea139f7-7be9-4e39-81e2-06fbef7a2cb8@googlegroups.com> On Sunday, November 25, 2012 7:11:29 AM UTC-5, ALeX inSide wrote: > How to "statically type" an instance of class that I pass to a method of other instance? > > > > I suppose there shall be some kind of method decorator to treat an argument as an instance of class? > > > > Generally it is needed so IDE (PyCharm) can auto-complete instance's methods and properties. > > > > Pseudo-python-code example: > > > > i = MyClass() > > > > xxx(i, 1, 2); > > > > ... > > def xxx(self, MyClass myclass, number, foobar): > > myclass.classsmethod() #myclass - is an instance of known class I'm not sure I understand exactly what you sre asking. Python uses "duck typing". As far as Python is concerned, you can pass in any class object and so long as it has the needed methods, it'll suffice ("If it walks like a duck and it quacks like a duck, then it is a duck."). The down side to that is that if you hand an object as an argument and the method you pass doesn't behave like the expected method class would, then bad things may happen at run time. It would be a bit of a hassle to check types to make sure things at least smell OK before execution goes possibly awry, but you are certainly free to write guard code that makes those sort of checks. My reply here is a bit different from the other replies I see so far. I worry that may mean I mis-understood your question. Has this been at all helpful an answer? From driscoll at cs.wisc.edu Mon Nov 26 20:58:22 2012 From: driscoll at cs.wisc.edu (Evan Driscoll) Date: Mon, 26 Nov 2012 19:58:22 -0600 Subject: Managing multiple packages In-Reply-To: <20121123002229.GA4908@taris.box> References: <50ABF52B.1080101@wisc.edu> <20121123002229.GA4908@taris.box> Message-ID: <50B41E3E.5040100@cs.wisc.edu> On 11/22/2012 06:22 PM, Thomas Bach wrote: > I am using virtual environments and do a > > python setup.py develop > > on each package. This just creates a symbolic link to the package and > all edits show up immediately. That's awesome; I didn't know about the 'develop' command. Thanks! Is that just a setuptools thing, or can you do it with distutils too? I didn't see anything too promising so I'm not so hopeful, but I figured I'd ask anyway. I'm also pretty confused about the distutils/setuptools/distribute/distutils2 landscape and what the differences are (at least between the first 3) and what their relationships with standard Python are. (I'd prefer to use distutils because (1) it ships with Python as opposed to being something extra to install (2) setuptools annoys me being the only think I can remember that, when you tell it to install to a prefix that doesn't exist, says "you want me to MAKE DIRECTORIES?! what are you, some kind of slave driver?!". Admittedly that second reason is not so rational and very occasionally putting up with that in exchange for development mode is plenty worth it, but being able to shun setuptools would make me feel good. :-)) Evan From steve+comp.lang.python at pearwood.info Mon Nov 26 21:32:10 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 27 Nov 2012 02:32:10 GMT Subject: Python dictionaries are awesome Message-ID: <50b4262a$0$21875$c3e8da3$76491128@news.astraweb.com> ... especially when you consider how some other languages implement them. http://twistedoakgames.com/blog/?p=925 [quote] Here?s the hypothetical situation: you?re making a flash game. In that game users can create named profiles. You store the profiles, keyed by their name, so that you ca- OOPS, you just introduced a bug. What?s the problem? The dictionary. [end quote] -- Steven From d at davea.name Mon Nov 26 21:50:00 2012 From: d at davea.name (Dave Angel) Date: Mon, 26 Nov 2012 21:50:00 -0500 Subject: How to pass class instance to a method? In-Reply-To: References: <3193e3dd-0507-4ff7-9026-ee80e5d9c1dd@googlegroups.com> <50B3E617.3040302@davea.name> Message-ID: <50B42A58.4020408@davea.name> On 11/26/2012 06:07 PM, Ian Kelly wrote: > On Mon, Nov 26, 2012 at 2:58 PM, Dave Angel wrote: >> Not how I would put it. In a statically typed language, the valid types >> are directly implied by the function parameter declarations, > As alluded to in my previous post, not all statically typed languages > require parameter type declarations to perform static checking. > >> while in a >> dynamic language, they're defined in the documentation, and only >> enforced (if at all) by the body of the function. > That's not even true for Python. The following example uses Python 2.x: > >>>> class Foo(object): > ... def method(self): > ... pass > ... >>>> Foo.method(4) > Traceback (most recent call last): > File "", line 1, in > TypeError: unbound method method() must be called with Foo instance as > first argument (got int instance instead) > > That's a run-time check, and it's not enforced by the body of the function. We were talking about function arguments. I don't know of any place where they get their types declared. -- DaveA From d at davea.name Mon Nov 26 22:14:59 2012 From: d at davea.name (Dave Angel) Date: Mon, 26 Nov 2012 22:14:59 -0500 Subject: How to pass class instance to a method? In-Reply-To: <50b3eab5$0$29994$c3e8da3$5496439d@news.astraweb.com> References: <3193e3dd-0507-4ff7-9026-ee80e5d9c1dd@googlegroups.com> <50b3eab5$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50B43033.7010605@davea.name> On 11/26/2012 05:18 PM, Steven D'Aprano wrote: > On Mon, 26 Nov 2012 16:58:47 -0500, Dave Angel wrote: > >> In a statically typed language, the valid types >> are directly implied by the function parameter declarations, while in a >> dynamic language, they're defined in the documentation, and only >> enforced (if at all) by the body of the function. > > Well that certainly can't be true, because you can write functions > without *any* documentation at all, and hence no defined type > restrictions that could be enforced: That's backwards. Any body should be a bug in that case. It doesn't matter what you pass to a function that is unspecified, it's behavior is undefined. Calling it is inherently illegal. > > def trivial_example(x): > return x+1 > > No documentation, and so by your definition above this should be weakly > typed and operate on any type at all. Since there are no type > restrictions defined, the body cannot enforce those type restrictions. > But that's clearly not true. > > Please, everybody, before replying to this thread, please read this: > > http://cdsmith.wordpress.com/2011/01/09/an-old-article-i-wrote/ I read part of it, and it's more than I care to read tonight. It seems to be written by an anonymous person. By jumping around in his blog, I see a lot of interesting articles, but i haven't yet figured out who he is. Does he have a name? A degree, a job in computers, a reputation? -- DaveA From d at davea.name Mon Nov 26 22:23:50 2012 From: d at davea.name (Dave Angel) Date: Mon, 26 Nov 2012 22:23:50 -0500 Subject: Compare list entry from csv files In-Reply-To: References: <50B3E865.9070700@davea.name> Message-ID: <50B43246.2010902@davea.name> On 11/26/2012 05:27 PM, Anatoli Hristov wrote: > I understand, but in my case I have for sure the field "Name" in the > second file that contains at least the first or the last name on it... > So probably it should be possible:) > The Name "Billgatesmicrosoft" contains the word "Gates" so logically I > might find a solution for it. > (Please don't top-post. Or if you must, then delete everything after your post, as I'm doing here. Otherwise you end up with insanities like new stuff, quote-4, quote-1, quote-3, quote-2. In this case, long tradition on this forum and many like it work well, even if Microsoft mail programs and some others decide to put the cursor at the wrong end of the existing text. In most programs, it's configurable.) If you can come up with an algorithm for comparing first+last in one file to name in the other, then the problem can be solved. But you can't do it by hand-waving, you have to actually figure out a mechanism. Then we can help you code such a thing. And I can just about guarantee that if these fields are created independently by human beings, that there will be exceptions that have to fixed by human beings. -- DaveA From steve+comp.lang.python at pearwood.info Mon Nov 26 23:10:48 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 27 Nov 2012 04:10:48 GMT Subject: How to pass class instance to a method? References: <3193e3dd-0507-4ff7-9026-ee80e5d9c1dd@googlegroups.com> <50b3eab5$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50b43d48$0$21875$c3e8da3$76491128@news.astraweb.com> On Mon, 26 Nov 2012 22:14:59 -0500, Dave Angel wrote: > On 11/26/2012 05:18 PM, Steven D'Aprano wrote: >> On Mon, 26 Nov 2012 16:58:47 -0500, Dave Angel wrote: >> >>> In a statically typed language, the valid types are directly implied >>> by the function parameter declarations, while in a dynamic language, >>> they're defined in the documentation, and only enforced (if at all) by >>> the body of the function. >> >> Well that certainly can't be true, because you can write functions >> without *any* documentation at all, and hence no defined type >> restrictions that could be enforced: > > That's backwards. Any body should be a bug in that case. It doesn't > matter what you pass to a function that is unspecified, it's behavior is > undefined. Calling it is inherently illegal. Have you ever programmed before? *wink* Seriously, as much as we would like to have full documentation of every piece of code before it is written, such a thing is awfully heavyweight for all but the biggest projects. In practice, many functions never get documented at all, or only partially documented. Whether this is a good thing or not, it is a fact, and no mainstream language *requires* you to write documentation, nor is the documentation is used to determine runtime behaviour. If it did, it would be code, not documentation. In lightweight or agile software development methodologies ("Bingo!") or exploratory development, you often write the code before you know what it does, or even what you want it to do. E.g. I'll sometimes have a vague idea of what I want a function or method to do, and go through three or four iterations of writing code before it is stable enough to begin documenting it. Given the practical reality that documentation is often neglected, there is a school of thought that says that *code* is the One True source of information about what the code does, that documentation is at best a hint or at worst completely redundant. While I think that's a bit extreme, I can see the point. If function f() puts the cat on the mat, but is documented as putting the hat on the cat, how do you know whether the documentation is wrong or the code? [...] >> Please, everybody, before replying to this thread, please read this: >> >> http://cdsmith.wordpress.com/2011/01/09/an-old-article-i-wrote/ > > I read part of it, and it's more than I care to read tonight. It seems > to be written by an anonymous person. By jumping around in his blog, I > see a lot of interesting articles, but i haven't yet figured out who he > is. Does he have a name? A degree, a job in computers, a reputation? Does it matter? Surely what he says should stand or fail on its own merits, not by who he is. He has a name, although it seems to be hard to find on his current blog: Chris Smith. As for the rest, I don't know. -- Steven From roy at panix.com Mon Nov 26 23:25:48 2012 From: roy at panix.com (Roy Smith) Date: Mon, 26 Nov 2012 23:25:48 -0500 Subject: How to pass class instance to a method? References: <3193e3dd-0507-4ff7-9026-ee80e5d9c1dd@googlegroups.com> <50b3eab5$0$29994$c3e8da3$5496439d@news.astraweb.com> <50b43d48$0$21875$c3e8da3$76491128@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > Given the practical reality that documentation is often neglected, there > is a school of thought that says that *code* is the One True source of > information about what the code does, that documentation is at best a > hint or at worst completely redundant. Yes, there is such a school. Those people are full of bovine excrement. > If function f() puts the cat on the mat, but is documented as putting > the hat on the cat, how do you know whether the documentation is > wrong or the code? Documentation should describe intent and interface. Yes, the code says what the code does. But, the interface description says what it's supposed to do. Can the docs be wrong? Of course they can. Usually because somebody changed the code and didn't bother to change the docs. My take on people who never document anything is that they're just plain lazy. Go ahead, ask me how I really feel about the topic :-) From d at davea.name Tue Nov 27 00:18:55 2012 From: d at davea.name (Dave Angel) Date: Tue, 27 Nov 2012 00:18:55 -0500 Subject: How to pass class instance to a method? In-Reply-To: <50b43d48$0$21875$c3e8da3$76491128@news.astraweb.com> References: <3193e3dd-0507-4ff7-9026-ee80e5d9c1dd@googlegroups.com> <50b3eab5$0$29994$c3e8da3$5496439d@news.astraweb.com> <50b43d48$0$21875$c3e8da3$76491128@news.astraweb.com> Message-ID: <50B44D3F.4040909@davea.name> On 11/26/2012 11:10 PM, Steven D'Aprano wrote: > On Mon, 26 Nov 2012 22:14:59 -0500, Dave Angel wrote: > >> On 11/26/2012 05:18 PM, Steven D'Aprano wrote: >>> On Mon, 26 Nov 2012 16:58:47 -0500, Dave Angel wrote: >>> >>>> In a statically typed language, the valid types are directly implied >>>> by the function parameter declarations, while in a dynamic language, >>>> they're defined in the documentation, and only enforced (if at all) by >>>> the body of the function. >>> Well that certainly can't be true, because you can write functions >>> without *any* documentation at all, and hence no defined type >>> restrictions that could be enforced: >> That's backwards. Any body should be a bug in that case. It doesn't >> matter what you pass to a function that is unspecified, it's behavior is >> undefined. Calling it is inherently illegal. > Have you ever programmed before? *wink* I think you know the answer to that. But for anyone else, I've been in various aspects of software development for 42 years, longer if you count various projects which were not turned into products. And the approximately dozen patents were nearly all for things which shipped, not just theoretical concepts. > > Seriously, as much as we would like to have full documentation of every > piece of code before it is written, such a thing is awfully heavyweight > for all but the biggest projects. > > In practice, many functions never get documented at all, or only > partially documented. Whether this is a good thing or not, it is a fact, > and no mainstream language *requires* you to write documentation, nor is > the documentation is used to determine runtime behaviour. If it did, it > would be code, not documentation. > > In lightweight or agile software development methodologies ("Bingo!") or > exploratory development, you often write the code before you know what it > does, or even what you want it to do. E.g. I'll sometimes have a vague > idea of what I want a function or method to do, and go through three or > four iterations of writing code before it is stable enough to begin > documenting it. > > Given the practical reality that documentation is often neglected, there > is a school of thought that says that *code* is the One True source of > information about what the code does, that documentation is at best a > hint or at worst completely redundant. While I think that's a bit > extreme, I can see the point. If function f() puts the cat on the mat, > but is documented as putting the hat on the cat, how do you know whether > the documentation is wrong or the code? Yes, and yes. They're both wrong. Function f() is just broken. Especially it's name. I know all that, and wasn't trying to pretend that documentation is always or even frequently, adequate. But the bulk of this part of the thread has been dealing with a function call, contrasting static typing, where some work is done by the compiler, and dynamic typing, where supposedly something is done by the interpreter. And I claim that the interpreter cannot in general do anything at the call target, except count the number of the arguments (maybe) and check the keywords. So the only thing that can be done there is manual - check the docs. If they're not there, there's just no comparison with the static case. And for a non-trivial function, or generally one which calls non-trivial functions, exhaustively analyzing the code of the function, and of the function it calls, is impractical. So people just call the function and hope. Static typing isn't nearly enough, and therefore the feeling of security it gives many is dangerously misleading. There have been (and presumably still are) languages which allow subsets of types to be explicitly declared, for example the type is int, but the restriction is nonnegative int (eg. square root). Or the type is int, but the restriction is between 4 and 7. Or the parameters have an interrelationship, or a dependency on the system state: e.g. if the first parameter is a string, then the second one must be one of 8 specific strings. > > [...] >>> Please, everybody, before replying to this thread, please read this: >>> >>> http://cdsmith.wordpress.com/2011/01/09/an-old-article-i-wrote/ >> I read part of it, and it's more than I care to read tonight. It seems >> to be written by an anonymous person. By jumping around in his blog, I >> see a lot of interesting articles, but i haven't yet figured out who he >> is. Does he have a name? A degree, a job in computers, a reputation? > Does it matter? Surely what he says should stand or fail on its own > merits, not by who he is. Yes, but since it's very hard slugging at first, I was curious if it would be worth it. He uses a number of terms in ways foreign to my background, and while I admire Humpty-Dumpty-ism, it certainly slows down comprehension. Anyway, he's apparently got a reputation with you, and that alone should make it worth the trouble. Just not tonight. > He has a name, although it seems to be hard to find on his current blog: > Chris Smith. As for the rest, I don't know. > > One of the brightest people I've ever known is named Smith. But come to think of it, that's a pretty common surname. -- DaveA From d at davea.name Tue Nov 27 00:27:53 2012 From: d at davea.name (Dave Angel) Date: Tue, 27 Nov 2012 00:27:53 -0500 Subject: Python dictionaries are awesome In-Reply-To: <50b4262a$0$21875$c3e8da3$76491128@news.astraweb.com> References: <50b4262a$0$21875$c3e8da3$76491128@news.astraweb.com> Message-ID: <50B44F59.7060508@davea.name> On 11/26/2012 09:32 PM, Steven D'Aprano wrote: > ... especially when you consider how some other languages implement them. > > > http://twistedoakgames.com/blog/?p=925 > > [quote] > Here?s the hypothetical situation: you?re making a flash game. In that > game users can create named profiles. You store the profiles, keyed by > their name, so that you ca- OOPS, you just introduced a bug. What?s the > problem? The dictionary. > [end quote] > > (facetious) Whenever someone tries to create a username that happens to conflict with a pre-existing attribute, just tell them that username is already taken. Just watch out when your user HasOwnProperty tries to delete his account. (/facetious) Namespaces are great, we should have lots of them. But be very careful about reusing one without checking to see if it's already inhabited. When a language design does this sort of thing, it tends to make us skittish about continuing use of said language. -- DaveA From rosuav at gmail.com Tue Nov 27 01:16:32 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 27 Nov 2012 17:16:32 +1100 Subject: Python dictionaries are awesome In-Reply-To: <50b4262a$0$21875$c3e8da3$76491128@news.astraweb.com> References: <50b4262a$0$21875$c3e8da3$76491128@news.astraweb.com> Message-ID: On Tue, Nov 27, 2012 at 1:32 PM, Steven D'Aprano wrote: > ... especially when you consider how some other languages implement them. > > > http://twistedoakgames.com/blog/?p=925 > > [quote] > Here?s the hypothetical situation: you?re making a flash game. In that > game users can create named profiles. You store the profiles, keyed by > their name, so that you ca- OOPS, you just introduced a bug. What?s the > problem? The dictionary. > [end quote] Here's a crazy possibility. What if, instead of just the name, you use "dictkey_"+name as the key? That's unlikely to conflict with anything. Hey, I wonder if Adobe might even have been able to do that under the covers! That would have been really easy, wouldn't have cost much, and would have given full functionality! (Except that you now can't have a property named "dictkey_foo", but that could be considered a misfeature - you can use a dictionary as a sort of jump table.) Python just happened to get it right by sheer luck, of course. No designer could possibly foresee problems like that. ChrisA From phracek2 at gmail.com Tue Nov 27 03:04:03 2012 From: phracek2 at gmail.com (Stone) Date: Tue, 27 Nov 2012 00:04:03 -0800 (PST) Subject: Modules or Package for my application Message-ID: <1d039516-5507-48d3-bebf-70faab3cc6be@googlegroups.com> Dear developers, I am creating application (originally written in perl) which will take care about replication from one system to the another system over command rsync. It will simulate High-availability solution. The application will contains main module called like ha.py and another modules will have names Replication.py, Trace.py, Misc.py, Procs.py, Constants.py, Lan.py, Display.py My questions are: 1) When I would like to make a global logging to the one file is it enough to do that like TRACE='/var/ha/log/sso_trace.log' logger = logging.getLogger('sso') hdrl = logging.FileHandler(TRACE) formatter = logging.Formatter('%{asctime}s %{levelname}s %{message}s') hdrl.setFormatter(formatter) logger.addHandler(hdrl) logger.setLevel(logging.WARNING) and make for them one module or how to solve that? How to call that logger from all modules / packages? Is it necessary to create alone module? 2) Is it package necessary or simply modules are enough - Replication.py will take care about alone replication - Procs.py will take care about some processes - Constants.py will take care about definition of global constants - Lan.py will take care about LAN definition - Dipslay.py will take care about showing screen (GUI or text_based) 3) How / where to define global variables? best regards Petr From avrajit.chatterjee at gmail.com Tue Nov 27 04:30:28 2012 From: avrajit.chatterjee at gmail.com (Avrajit) Date: Tue, 27 Nov 2012 01:30:28 -0800 (PST) Subject: Python printout Message-ID: <4b917927-81e5-4bab-b29f-f95e83757130@googlegroups.com> I have multiple list and want to invoke a single prinout which will give a print preview of all list in Grid format in a different pages. I have tried wx.lib.printout.PrintTable but it takes only one table at a time. Any clues how to achieve this? From feedbackflow at gmail.com Tue Nov 27 07:53:42 2012 From: feedbackflow at gmail.com (Bart Thate) Date: Tue, 27 Nov 2012 13:53:42 +0100 Subject: changing dicts in a threaded environment ? Message-ID: Hello All ;] i wondered if somebody could bring me up to date on the following subject: i use python3 now and i need to be able to remove elements from a dict in a thread safe manner. kinda like a Queue.Queue thing but then in a dict, so i can pass arond my dict based objects as parameters arond without having to wonder if it gets properly locked. So not only do i need to get this solved: Issue #14417 : Mutating a dict during lookup now restarts the lookup instead of raising a RuntimeError (undoes issue #14205 ). i also need to lock all the actual modifying underlying "real" stuff as well not just the iterator or view or whatever i don't know yet ;] So my question is kinda like, is a dict possible in the same way the Queue.Queue is now made truely thread safe ? Also is there something like a select.select for queues ? I want to pass a dict to a thead and then have a watcher on the dicts state if result have arrived. For what i am making dicts are the basis of everything, json dumpable so the state op the object can be saved or send over the wire as well. Thnx for your time and energy ;] Grtx, Bart -------------- next part -------------- An HTML attachment was scrubbed... URL: From tolidtm at gmail.com Tue Nov 27 09:24:55 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Tue, 27 Nov 2012 15:24:55 +0100 Subject: Compare list entry from csv files In-Reply-To: <50B43246.2010902@davea.name> References: <50B3E865.9070700@davea.name> <50B43246.2010902@davea.name> Message-ID: On Tue, Nov 27, 2012 at 4:23 AM, Dave Angel wrote: > On 11/26/2012 05:27 PM, Anatoli Hristov wrote: >> I understand, but in my case I have for sure the field "Name" in the >> second file that contains at least the first or the last name on it... >> So probably it should be possible:) >> The Name "Billgatesmicrosoft" contains the word "Gates" so logically I >> might find a solution for it. >> > > (Please don't top-post. Or if you must, then delete everything after > your post, as I'm doing here. Otherwise you end up with insanities like > new stuff, quote-4, quote-1, quote-3, quote-2. In this case, long > tradition on this forum and many like it work well, even if Microsoft > mail programs and some others decide to put the cursor at the wrong end > of the existing text. In most programs, it's configurable.) > > If you can come up with an algorithm for comparing first+last in one > file to name in the other, then the problem can be solved. But you > can't do it by hand-waving, you have to actually figure out a mechanism. > Then we can help you code such a thing. And I can just about guarantee > that if these fields are created independently by human beings, that > there will be exceptions that have to fixed by human beings. > > > -- > > DaveA Thanks for your help. I will do my best for the forum :) I advanced a little bit with the algorithm and at least I can now extract and compare the fields :) For my beginner skills I think this is too much for me. Now next step is to add the second field with the number to the Namelist and copy it to a third filename I suppose. import csv origf = open('c:/Working/Test_phonebook.csv', 'rt') secfile = open('c:/Working/phones.csv', 'rt') phonelist = [] namelist = [] names = csv.reader(origf, delimiter=';') phones = csv.reader(secfile, delimiter=';') for tel in phones: phonelist.append(tel) #print "*"*25,phonelist,"*"*25 rows = 0 def finder(name_row): for ex_phone in phonelist: # phonelist.append(tel) telstr = ex_phone[0].lower() # print "*"*25 + " I got %s" % telstr # print "\nGot name from Name_Find :%s" % name_row if telstr.find(name_row) >= 0: print "\nName found: %s" % name_row else: pass return # print "\nNot found %s" % name_row def name_find(): for row in names: namelist.append(row) name_row = row[0].lower() # print "\nExtracted Name is :% s" % name_row finder(name_row) # name_find() # rows = rows +1 name_find() From neilc at norwich.edu Tue Nov 27 10:05:57 2012 From: neilc at norwich.edu (Neil Cerutti) Date: 27 Nov 2012 15:05:57 GMT Subject: Compare list entry from csv files References: <50B3E865.9070700@davea.name> <50B43246.2010902@davea.name> Message-ID: On 2012-11-27, Anatoli Hristov wrote: > Thanks for your help. I will do my best for the forum :) > > I advanced a little bit with the algorithm and at least I can > now extract and compare the fields :) For my beginner skills I > think this is too much for me. Now next step is to add the > second field with the number to the Namelist and copy it to a > third filename I suppose. I had to write a similar type of program, and I imagine it's a common problem. Sometimes new students provide incorrect SSN's or simply leave them blank. This makes it impossible for us to match their application for financial aid to their admissions record. You have to analyze how you're going to match records. In my case, missing SSN's are one case. A likeley match in this case is when the names are eerily similar. In the other case, where they simply got their SSN wrong, I have to check for both a similar SSN and a similar name. But you still have to define "similar." I looked up an algorithm on the web called Levenshtein Distance, and implemented it like so. def levenshteindistance(first, second): """Find the Levenshtein distance between two strings.""" if len(first) > len(second): first, second = second, first if len(second) == 0: return len(first) first_length = len(first) + 1 second_length = len(second) + 1 distance_matrix = [[0] * second_length for x in range(first_length)] for i in range(first_length): distance_matrix[i][0] = i for j in range(second_length): distance_matrix[0][j]=j for i in range(1, first_length): for j in range(1, second_length): deletion = distance_matrix[i-1][j] + 1 insertion = distance_matrix[i][j-1] + 1 substitution = distance_matrix[i-1][j-1] if first[i-1] != second[j-1]: substitution += 1 distance_matrix[i][j] = min(insertion, deletion, substitution) return distance_matrix[first_length-1][second_length-1] The algorithm return a count of every difference between the two strings, from 0 to the length of the longest string. Python provides difflib, which implements a similar algorithm, so I used that as well (kinda awkwardly). I used difflib.get_close_matches to get candidates, and then difflib.SequenceMatcher to provide me a score measuring the closeness. matches = difflib.get_close_matches(s1, s2) for m in matches: scorer = difflib.SequenceMatcher(None, s1, m) ratio = scorer.ratio() if ratio == 0.0: # perfect match if ratio > MAX_RATIO: # You gotta choose this. I used 0.1 # close match The two algorithms come up with different guesses, and I pass on their suggestions for fixes to a human being. Both versions of the program take roughly 5 minutes to run the comparison on 2000-12000 records between the two files. I like the results of Levenshtein distance a little better, but difflib finds some stuff that it misses. In your case, the name is munged horribly in one of the files so you'll first have to first sort it out somehow. -- Neil Cerutti From santosh.ssit at gmail.com Tue Nov 27 12:31:02 2012 From: santosh.ssit at gmail.com (san) Date: Tue, 27 Nov 2012 09:31:02 -0800 (PST) Subject: How to sort list of String without considering Special characters and with case insensitive Message-ID: Please let me know how to sort the list of String in either ascending / descending order without considering special characters and case. ex: list1=['test1_two','testOne','testTwo','test_one'] Applying the list.sort /sorted method results in sorted list ['test1_two', 'testOne', 'testTwo', 'test_one'] but the without considering the special characters and case it should be ['testOne','test_one', 'test1_two','testTwo'] OR ['test_one','testOne','testTwo', 'test1_two' ] list.sort /sorted method sorts based on the ascii value of the characters but Please let me knwo how do i achieve my expected one From kagard at gmail.com Tue Nov 27 12:36:20 2012 From: kagard at gmail.com (kagard) Date: Tue, 27 Nov 2012 09:36:20 -0800 (PST) Subject: Migrate from Access 2010 / VBA References: <6db4ed18-a16e-4cd1-940b-c10f51207780@googlegroups.com> Message-ID: <1a3050ad-4750-43d7-a3a8-787f9d5adf26@r13g2000vbd.googlegroups.com> On Nov 26, 11:21?am, Michael Torrie wrote: > On 11/22/2012 08:19 PM, kgard wrote: > > > I am the lone developer of db apps at a company of 350+ employees. > > Everything is done in MS Access 2010 and VBA. I'm frustrated with the > > limitations of this platform and have been considering switching to > > Python. I've been experimenting with the language for a year or so, > > and feel comfortable with the basics. > > Python is just a language, just like VBA itself is just a language. ?You > can't just replace an MS Access VBA app with one in Python. ?You have to > replace your *tools* with open source alternatives, that hopefully > python can glue together. ?Wolfgang provided a nice list of such tools. > > One program that claims to be working towards Access replacement is > Kexi. ?It's not written in Python, but I think it does use Python as a > scripting language, just as Access uses VBA. ?I doubt it's anywhere near > Access yet, but it's worth a look: > > http://kexi-project.org/about.html > > > > > Has anyone here made this transition successfully? If so, could you > > pass along your suggestions about how to do this as quickly and > > painlessly as possible? > > It will not be painless at all. ?There is no "transition" path, really. > ?That's partly the result of Microsoft product lock-in, partly because > you want to replace a complete system that happens to be glued together > with, simply, "Python." > > I think Python could be a great fit if you could find the right tools to > go with it, but it's not going to be easy at all. ?Complete MS Access > replacements is one of the may extremely weak spots in the open source > world. ?Partly because web-based apps often work better than a desktop > DB solution, and you might want to go there too, perhaps using a python > web development toolkit like django. > > I understand your comment about replacing tools. Since things tend to fall apart at the seams, though, I wouldn't mind keeping the seams to a minumum. That's why I had been thinking about something like Django or Web2Py. Web2Py seems to more correctly represent MVC, and I like that its template scripting mirrors Python syntal. Thanks for your reply. I will now cower at my keyboard and await my "Django kicks Web2Py's butt" lashing. From python at mrabarnett.plus.com Tue Nov 27 12:54:23 2012 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 27 Nov 2012 17:54:23 +0000 Subject: How to sort list of String without considering Special characters and with case insensitive In-Reply-To: References: Message-ID: <50B4FE4F.2060901@mrabarnett.plus.com> On 2012-11-27 17:31, san wrote: > Please let me know how to sort the list of String in either ascending / descending order without considering special characters and case. > ex: list1=['test1_two','testOne','testTwo','test_one'] > Applying the list.sort /sorted method results in sorted list ['test1_two', 'testOne', 'testTwo', 'test_one'] > but the without considering the special characters and case it should be > ['testOne','test_one', 'test1_two','testTwo'] OR ['test_one','testOne','testTwo', 'test1_two' ] > > list.sort /sorted method sorts based on the ascii value of the characters but Please let me knwo how do i achieve my expected one > (I'm using Python 3.) The .sort method accepts a 'key' argument, which lets you pass a function that transforms the value being sorted before comparison: >>> def make_key(string): return string.replace('_', '').upper() >>> list1 = ['test1_two', 'testOne', 'testTwo', 'test_one'] >>> list1.sort(key=make_key) >>> list1 ['test1_two', 'testOne', 'test_one', 'testTwo'] I don't know how you define 'special'. You could remove any characters which are special or keep any characters which are not special, depending on how many characters are defined as 'special': from string import ascii_letters # Sets are faster for this kind of thing. ascii_letters = set(ascii_letters) def make_key(string): return ''.join(c for c in string if c in ascii_letters).upper() list1 = ['test1_two', 'testOne', 'testTwo', 'test_one'] list1.sort(key=make_key) print(list1) # Output is: ['testOne', 'test_one', 'test1_two', 'testTwo'] From drew00andy at yahoo.co.uk Tue Nov 27 13:00:56 2012 From: drew00andy at yahoo.co.uk (Andrew) Date: Tue, 27 Nov 2012 20:00:56 +0200 Subject: os.popen and the subprocess module Message-ID: Hello world, I'm working on a script that will run an executable obtaine the output from the executable and do some analysis on the output. Essentially the script runs the executable analyses the data. I'm looking into os.popen and the subprocess module, implementing os.popen is easy but i hear it is depreciating however I'm finding the implemantation of subprocess daunting can anyone help Dx -- ???(shuhari)first learn, then detach, and finally transcend From ramit.prasad at jpmorgan.com Tue Nov 27 13:21:03 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Tue, 27 Nov 2012 18:21:03 +0000 Subject: How to sort list of String without considering Special characters and with case insensitive In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF47418032FF1@SCACMX008.exchad.jpmchase.net> san wrote: > > Please let me know how to sort the list of String in either ascending / descending order without considering > special characters and case. > ex: list1=['test1_two','testOne','testTwo','test_one'] > Applying the list.sort /sorted method results in sorted list ['test1_two', 'testOne', 'testTwo', 'test_one'] > but the without considering the special characters and case it should be > ['testOne','test_one', 'test1_two','testTwo'] OR ['test_one','testOne','testTwo', 'test1_two' ] > > list.sort /sorted method sorts based on the ascii value of the characters but Please let me knwo how do i > achieve my expected one You can pass a key function into list.sort() and sorted(). This allows you to customize the sorting. In the below examples I use lambda but you can use a non-lambda function (if you need more complexity). Case insensitive searches are often done by converting the strings being compared into the same case. Here I turned them all uppercase. lst = ['test1_two', 'testOne', 'testTwo', 'test_one'] lst.sort(key=lambda x: x.upper()) This will filter non-alphanumeric characters. You may be able to create and use a translation table instead. lst.sort( key=lambda x: ''.join( c.upper() for c in x if c in string.letters+string.digits ) ) ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From ramit.prasad at jpmorgan.com Tue Nov 27 13:27:52 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Tue, 27 Nov 2012 18:27:52 +0000 Subject: os.popen and the subprocess module In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF4741803302B@SCACMX008.exchad.jpmchase.net> Andrew wrote: > > Hello world, > > I'm working on a script that will run an executable obtaine the output > from the executable > and do some analysis on the output. Essentially the script runs the > executable analyses > the data. > I'm looking into os.popen and the subprocess module, implementing os.popen > is easy but i hear > it is depreciating however I'm finding the implemantation of subprocess > daunting can anyone help > Have you read the documentation? http://docs.python.org/library/subprocess.html#replacing-older-functions-with-the-subprocess-module (specifically 17.1.4.5) This may help as well. http://www.doughellmann.com/PyMOTW/subprocess/#capturing-output Note, use of shell=True is usually discouraged as it can be unsafe. ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From breamoreboy at yahoo.co.uk Tue Nov 27 13:29:31 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 27 Nov 2012 18:29:31 +0000 Subject: os.popen and the subprocess module In-Reply-To: References: Message-ID: On 27/11/2012 18:00, Andrew wrote: > Hello world, > > I'm working on a script that will run an executable obtaine the output > from the executable > and do some analysis on the output. Essentially the script runs the > executable analyses > the data. > I'm looking into os.popen and the subprocess module, implementing > os.popen is easy but i hear > it is depreciating however I'm finding the implemantation of subprocess > daunting can anyone help > > Dx os.popen is alive and kicking in Python 3.3, see here http://bugs.python.org/issue6490 for more. And I think you meant deprecated :) -- Cheers. Mark Lawrence. From tolidtm at gmail.com Tue Nov 27 13:57:53 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Tue, 27 Nov 2012 19:57:53 +0100 Subject: Compare list entry from csv files In-Reply-To: References: <50B3E865.9070700@davea.name> <50B43246.2010902@davea.name> Message-ID: On Tue, Nov 27, 2012 at 4:05 PM, Neil Cerutti wrote: > On 2012-11-27, Anatoli Hristov wrote: >> Thanks for your help. I will do my best for the forum :) >> >> I advanced a little bit with the algorithm and at least I can >> now extract and compare the fields :) For my beginner skills I >> think this is too much for me. Now next step is to add the >> second field with the number to the Namelist and copy it to a >> third filename I suppose. > > I had to write a similar type of program, and I imagine it's a > common problem. Sometimes new students provide incorrect SSN's or > simply leave them blank. This makes it impossible for us to match > their application for financial aid to their admissions record. > > You have to analyze how you're going to match records. > > In my case, missing SSN's are one case. A likeley match in this > case is when the names are eerily similar. > > In the other case, where they simply got their SSN wrong, I have > to check for both a similar SSN and a similar name. > > But you still have to define "similar." I looked up an algorithm > on the web called Levenshtein Distance, and implemented it like > so. > > def levenshteindistance(first, second): > """Find the Levenshtein distance between two strings.""" > if len(first) > len(second): > first, second = second, first > if len(second) == 0: > return len(first) > first_length = len(first) + 1 > second_length = len(second) + 1 > distance_matrix = [[0] * second_length for x in range(first_length)] > for i in range(first_length): > distance_matrix[i][0] = i > for j in range(second_length): > distance_matrix[0][j]=j > for i in range(1, first_length): > for j in range(1, second_length): > deletion = distance_matrix[i-1][j] + 1 > insertion = distance_matrix[i][j-1] + 1 > substitution = distance_matrix[i-1][j-1] > if first[i-1] != second[j-1]: > substitution += 1 > distance_matrix[i][j] = min(insertion, deletion, substitution) > return distance_matrix[first_length-1][second_length-1] > > The algorithm return a count of every difference between the two > strings, from 0 to the length of the longest string. > > Python provides difflib, which implements a similar algorithm, so > I used that as well (kinda awkwardly). I used > difflib.get_close_matches to get candidates, and then > difflib.SequenceMatcher to provide me a score measuring the > closeness. > > matches = difflib.get_close_matches(s1, s2) > for m in matches: > scorer = difflib.SequenceMatcher(None, s1, m) > ratio = scorer.ratio() > if ratio == 0.0: > # perfect match > if ratio > MAX_RATIO: # You gotta choose this. I used 0.1 > # close match > > The two algorithms come up with different guesses, and I pass on > their suggestions for fixes to a human being. Both versions of > the program take roughly 5 minutes to run the comparison on > 2000-12000 records between the two files. > > I like the results of Levenshtein distance a little better, but > difflib finds some stuff that it misses. > > In your case, the name is munged horribly in one of the files so > you'll first have to first sort it out somehow. > > -- > Neil Cerutti > -- > http://mail.python.org/mailman/listinfo/python-list Thank you all for the help, but I figured that out and the program now works perfect. I would appreciate if you have some notes about my script as I'm noob :) Here is the code: import csv origf = open('c:/Working/Test_phonebook.csv', 'rt') secfile = open('c:/Working/phones.csv', 'rt') phonelist = [] namelist = [] names = csv.reader(origf, delimiter=';') phones = csv.reader(secfile, delimiter=';') for tel in phones: phonelist.append(tel) def finder(name_row,rows): for ex_phone in phonelist: telstr = ex_phone[0].lower() if telstr.find(name_row) >= 0: print "\nName found: %s" % name_row namelist[rows][-1] = ex_phone[-1].lower() else: pass return def name_find(): rows = 0 for row in names: namelist.append(row) name_row = row[0].lower() finder(name_row,rows) rows = rows+1 name_find() ofile = open('c:/Working/ttest.csv', "wb") writer = csv.writer(wfile, delimiter=';') for insert in namelist: writer.writerow(insert) wfile.close() From tjreedy at udel.edu Tue Nov 27 14:18:15 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 27 Nov 2012 14:18:15 -0500 Subject: changing dicts in a threaded environment ? In-Reply-To: References: Message-ID: On 11/27/2012 7:53 AM, Bart Thate wrote: [Answers based on reading without thread experience.] > i use python3 now and i need to be able to remove elements from a dict > in a thread safe manner. Essentially no change from py2. > kinda like a Queue.Queue thing but then in a dict, so i can pass arond > my dict based objects as parameters arond without having to wonder if > it gets properly locked. As I understand it, dicts do not get locked unless you do it. > So not only do i need to get this solved: > > Issue #14417 : Mutating a dict during > lookup now restarts the lookup instead of raising a RuntimeError (undoes > issue #14205 ). As I understand #14417, you should be explicitly locking dicts. The issue in #14205 was that people doing mutations in just one thread and lookups in others usually got away without locking becuase of recursive retries, but occasionally crashed the interpreter because of them. The first fix was to prevent crashes by removing retries. But that would break programs that naively worked because of them. The second fix was to do iterative retries instead, so a thread might occasionally hang, but without crashing. As I understand it, it is better to not be a naive user. > i also need to lock all the actual modifying underlying "real" stuff as > well not just the iterator or view or whatever i don't know yet ;] I am not sure what you are asking. > > So my question is kinda like, is a dict possible in the same way the > Queue.Queue is now made truely thread safe ? A custom dict might be but the builtin is not. Queue is somewhat unique as a builtin designed for threads. > Also is there something like a select.select for queues ? Have you searched? > I want to pass a dict to a thead and then have a watcher on the dicts > state if result have arrived. Builtin dicts do not have an 'I have changed flag. You would need to subclass or probably better, wrap a dict using the appropriate pattern. Perhaps done already, but you want a wrapper that does both locking and watcher notification. -- Terry Jan Reedy From feliphil at gmx.net Tue Nov 27 14:24:32 2012 From: feliphil at gmx.net (Wolfgang Keller) Date: Tue, 27 Nov 2012 20:24:32 +0100 Subject: Migrate from Access 2010 / VBA References: <6db4ed18-a16e-4cd1-940b-c10f51207780@googlegroups.com> Message-ID: <20121127202432.1196793fa61ab16f0acd18eb@gmx.net> > One program that claims to be working towards Access replacement is > Kexi. It's not written in Python, but I think it does use Python as a > scripting language, just as Access uses VBA. I doubt it's anywhere > near Access yet, but it's worth a look: > > http://kexi-project.org/about.html Unfortunately, Kexi doesn't support composite keys yet, so it's essentially useless for real-world databases so far. :-( Rekall appears to be dead and so does Knoda. Sincerely, Wolfgang From feliphil at gmx.net Tue Nov 27 14:27:00 2012 From: feliphil at gmx.net (Wolfgang Keller) Date: Tue, 27 Nov 2012 20:27:00 +0100 Subject: Migrate from Access 2010 / VBA References: <6db4ed18-a16e-4cd1-940b-c10f51207780@googlegroups.com> <20121125204816.97f486775f8b1e7e575a7b0a@gmx.net> Message-ID: <20121127202700.f0cabea6307d0250fa8ff5ff@gmx.net> > The reporting question is the one that gives me the greatest concern > when I think about switching to Python. Not Python, but FOSS, cross-platform and it works with PostgreSQL: http://www.xtuple.com/openrpt Apart from that one, among the mentioned DB RAD frameworks, at least Dabo and Camelot include report builders. And, again; Libreoffice Base comes with a reporting framework, although the latest version afaik has a currently unresolved bug, so you'll have to retrograde to an older version if you want to use it. Sincerely, Wolfgang From d at davea.name Tue Nov 27 14:59:56 2012 From: d at davea.name (Dave Angel) Date: Tue, 27 Nov 2012 14:59:56 -0500 Subject: Compare list entry from csv files In-Reply-To: References: <50B3E865.9070700@davea.name> <50B43246.2010902@davea.name> Message-ID: <50B51BBC.80306@davea.name> On 11/27/2012 01:57 PM, Anatoli Hristov wrote: > > > Thank you all for the help, but I figured that out and the program now > works perfect. Wow! > I would appreciate if you have some notes about my > script as I'm noob :) > Here is the code: > > import csv > > origf = open('c:/Working/Test_phonebook.csv', 'rt') > secfile = open('c:/Working/phones.csv', 'rt') I cannot tell which of these files is which, since you use different names than you did in your example text of the OP. > phonelist = [] > namelist = [] > names = csv.reader(origf, delimiter=';') > phones = csv.reader(secfile, delimiter=';') > for tel in phones: > phonelist.append(tel) Is phonelist the one that has just two fields: name & phone number? secfile.close() del phones Why don't you populate the namelist in the same way, so that it's clear what's going on ? origf.close() del names Two many globals, being referenced directly in functions. It's fine to have them, but if they're const, like PHONELIST, then make them uppercase. If they're not, then pass them into the functions where used. > > > def finder(name_row,rows): > for ex_phone in phonelist: > telstr = ex_phone[0].lower() > if telstr.find(name_row) >= 0: So will this logic find a match between a Mr. Jones living in the city of Townsend and a Mr. Townsend that you have a phone number for? Or do I have the files backward, and this will compare the first name from one file to the phone number from the other? > print "\nName found: %s" % name_row > namelist[rows][-1] = ex_phone[-1].lower() return #otherwise, we might find multiple matches > else: > pass Need a print message here indicating that no name matched. Otherwise, you just think it worked. > return > > def name_find(): > rows = 0 No need to keep your own index, since finder() can just adjust the list directly, without having to reach into globals. > for row in names: for row_num, row in enumerate(names): #(if you actually needed such an index) > namelist.append(row) > name_row = row[0].lower() Might this actually be first_name ? i find it very confusing to store a string in a variable with the word row in it. > finder(name_row,rows) > rows = rows+1 All the initialization you had at the beginning belongs here, and it also belongs inside an if __name__ == "__main__": clause Even better, it all belongs in a function, which returns the two lists you're going to work with. > name_find() > ofile = open('c:/Working/ttest.csv', "wb") > writer = csv.writer(wfile, delimiter=';') What is wfile? Looks undefined to me. > for insert in namelist: > writer.writerow(insert) > wfile.close() I really think you need to document the matching pattern you're trying to implement, and then we can worry about whether the code correctly implements that algorithm. Finally, we can worry about clarity of the code. -- DaveA From neilc at norwich.edu Tue Nov 27 15:41:57 2012 From: neilc at norwich.edu (Neil Cerutti) Date: 27 Nov 2012 20:41:57 GMT Subject: Compare list entry from csv files References: <50B3E865.9070700@davea.name> <50B43246.2010902@davea.name> Message-ID: On 2012-11-27, Anatoli Hristov wrote: > Thank you all for the help, but I figured that out and the > program now works perfect. I would appreciate if you have some > notes about my script as I'm noob :) Here is the code: > > import csv > > origf = open('c:/Working/Test_phonebook.csv', 'rt') > secfile = open('c:/Working/phones.csv', 'rt') csv module expects files to be opened in binary mode in Python versions less than version 3.0. For Python versions >= 3.0, you use the special keyword argument, newlines='', instead. > phonelist = [] > namelist = [] The structure of your program is poor. It's workable for such a short script, and sometimes my first cuts are similar, but it's better to get out of the habit right away. Once you get this working the way you'd like you should clean up the structure as a service to your future self. > names = csv.reader(origf, delimiter=';') > phones = csv.reader(secfile, delimiter=';') You csv files don't seem to have header rows, but even so you can improve your code by providing fieldnames and using a DictReader instead. name_reader = csv.DictReader(origf, fieldnames=[ 'Name', 'Blah', 'Phone#']) Then you can read from records with name = row['Name'] instead of using bare, undocumented integers. > for tel in phones: > phonelist.append(tel) > > def finder(name_row,rows): > for ex_phone in phonelist: > telstr = ex_phone[0].lower() > if telstr.find(name_row) >= 0: This strikes me as a crude way to match names. You don't really want Donald to match perfectly with McDonald, do you? Or for Smith to match with Smithfield? Yes, a human being will clean it up, but your program can do a better job. > print "\nName found: %s" % name_row > namelist[rows][-1] = ex_phone[-1].lower() > else: > pass > return > > def name_find(): > rows = 0 > for row in names: > namelist.append(row) > name_row = row[0].lower() > finder(name_row,rows) > rows = rows+1 You can use the useful enumerate function instead of your own counter. for rows, row in enumerate(names): ...though I would find 'rownum' or 'num' or just 'i' better than the name 'rows', which I find confusing. > name_find() > ofile = open('c:/Working/ttest.csv', "wb") > writer = csv.writer(wfile, delimiter=';') > for insert in namelist: > writer.writerow(insert) > wfile.close() -- Neil Cerutti From thbach at students.uni-mainz.de Tue Nov 27 16:28:20 2012 From: thbach at students.uni-mainz.de (Thomas Bach) Date: Tue, 27 Nov 2012 22:28:20 +0100 Subject: Managing multiple packages In-Reply-To: <50B41E3E.5040100@cs.wisc.edu> References: <50ABF52B.1080101@wisc.edu> <20121123002229.GA4908@taris.box> <50B41E3E.5040100@cs.wisc.edu> Message-ID: <20121127212820.GA3875@taris.box> On Mon, Nov 26, 2012 at 07:58:22PM -0600, Evan Driscoll wrote: > I'm also pretty confused about the > distutils/setuptools/distribute/distutils2 landscape and what the > differences are (at least between the first 3) and what their > relationships with standard Python are. In my opinion packaging in Python is at the moment a disaster. The whole distutils/setuptools/whatever confusion is at least one of the major sources that this whole topic is so poorly documented and obviously violates the Zen of Python. Anyways, I think THEY are working on it. [1] is also a bit clarifying. Regards, Thomas. Footnotes: [1] http://ziade.org/2010/03/03/the-fate-of-distutils-pycon-summit-packaging-sprint-detailed-report/ From tjreedy at udel.edu Tue Nov 27 18:24:12 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 27 Nov 2012 18:24:12 -0500 Subject: Bugs: Content-Length not updated by reused urllib.request.Request / has_header() case-sensitive In-Reply-To: References: <50a12949$0$6566$9b4e6d93@newsspool3.arcor-online.net> Message-ID: On 11/12/2012 8:58 PM, Terry Reedy wrote: > On 11/12/2012 4:35 PM, Terry Reedy wrote: > >> import urllib.request >> opener = urllib.request.build_opener() >> request = urllib.request.Request("http://example.com/", headers = >> {"Content-Type": "application/x-www-form-urlencoded"}) >> >> opener.open(request, "1".encode("us-ascii")) >> print(request.data, '\n', request.header_items()) >> >> opener.open(request, "123456789".encode("us-ascii")) >> print(request.data, '\n', request.header_items()) >> >> exhibits the same behavior in 3.3.0 of printing ('Content-length', '1') >> in the last output. I agree that that looks wrong, but I do not know if >> such re-use is supposed to be supported. > > I opened http://bugs.python.org/issue16464 A patch has been written by Alexey Kachayev and pushed by Andrew Svetlov and the behavior will change in 3.4.0 to allow reuse. -- Terry Jan Reedy From lars at rational-it.com Tue Nov 27 18:59:38 2012 From: lars at rational-it.com (lars van gemerden) Date: Tue, 27 Nov 2012 15:59:38 -0800 (PST) Subject: deepcopy questions Message-ID: Hi, I get a very strange result when using deepcopy. The following code: def __deepcopy__(self, memo): independent = self.independent() if independent is self: out = type(self)() out.__dict__ = copy.deepcopy(self.__dict__, memo) print self.__dict__ print out.__dict__ #strange result return out else: return copy.deepcopy(independent, memo).find(self.id).take() prints different results for self.__dict__ and out.__dict__: {'_active_': False, 'init': {}, '_id_': 0, '_items_': [], '_name_': 'main'} {'_active_': False, 'init': {}, '_id_': 0} Two items are missing in the copy. Maybe i am missing something obvious, but i cannot figure out how this could happen. Can anyone tell me how this is possible? Cheers, Lars From db3l.net at gmail.com Tue Nov 27 19:06:21 2012 From: db3l.net at gmail.com (David Bolen) Date: Tue, 27 Nov 2012 19:06:21 -0500 Subject: Migrate from Access 2010 / VBA References: <6db4ed18-a16e-4cd1-940b-c10f51207780@googlegroups.com> Message-ID: kgard writes: > I am the lone developer of db apps at a company of 350+ > employees. Everything is done in MS Access 2010 and VBA. I'm > frustrated with the limitations of this platform and have been > considering switching to Python. I've been experimenting with the > language for a year or so, and feel comfortable with the basics. (...) > Has anyone here made this transition successfully? If so, could you > pass along your suggestions about how to do this as quickly and > painlessly as possible? I went through a very similar transition a few years ago from standalone Access databases (with GUI forms, queries and reports, as well as replication) to a pure web application with full reporting (albeit centrally designed and not a report designer for users). I suppose my best overall suggestion is to migrate the data first and independently of any other activities. Unless your uses for Access in terms of GUI or reporting are extremely limited, don't try to replace your current system in one swoop, and in particular, be willing to continue allowing Access as long as necessary for GUI/reports until you're sure you've matched any current capabilities with an alternate approach. For all its warts, as a database GUI and reporting tool, Access has a lot going for it, and it can be more complex than you may think to replicate elsewhere. So the first thing I would suggest is to plan and implement a migration of the data itself. In my case I migrated the data from Access into PostgreSQL. That process itself took some planning and testing in terms of moving the data, and then correcting various bits of the schemas and data types (if I recall, booleans didn't round-trip properly at first), so was actually a series of conversions until I was happy, during which time everyone was using Access as usual. To support the migration, I created a mirror Access database to the production version, but instead of local Jet tables, I linked all the tables to the PostgreSQL server. All other aspects of the Access database (e.g., forms, reports, queries) remained the same, just now working off of the remote data. This needed testing too - for example, some multi-level joining in Access queries can be an issue. In some cases it was easier for me to migrate selected Access query logic into a database view and then replace the query in Access to use the view. You also need to (painfully) set any UI aspects of the table definitions manually since the linking process doesn't set that up, for which I used the original Access db as a model. I ended up doing that multiple times as I evolved the linked database, and I'll admit that was seriously tedious. While not required, I also wrapped up my new linked Access database into a simple installer (InnoSetup based in my case). Prior to this everyone was just copying the mdb file around, but afterwards I had an installer they just ran to be sure they had the latest version. If you do this part carefully, for your end users, aside from installing the new database, they see absolutely no difference, but you now have easy central access to the data, and most importantly can write other applications and tools against it without touching the Access side. It turns Access into just your GUI and reporting tool. If you have power users that make local changes they can continue to design additional queries or reports in their own local mdb against the linked tables. They'll need some extra support for updates though, either instructions to re-link, or instructions on exporting and importing their local changes into a newly installed version of your master mdb. Having done this, you are then free to start implementing, for example, a web based application to start taking over functionality. The nice thing is that you need not replicate everything at once, you can start slow or with the most desirable features, letting Access continue to handle the less common or more grungy legacy stuff at first. There are innumerable discussions on best web and application frameworks, so probably not worth getting into too much. In my case I'm using a CherryPy/Genshi/SQLAlchemy/psycopg2 stack. As long as you still have Access around, you'll have to take it into consideration with schema changes, but that's not really that much harder than any other schema migration management. It's just another client to the database you can run in parallel as long as you wish. If you do change the schema, when done, just load your master Access database, update the links, and rebuild/redistribute the installer to your users. Many changes (e.g., new columns with defaults) can be backwards compatible and avoid forced upgrades. You can operate both systems in parallel for a while even for similar functionality (for testing if nothing else), but can then retire functionality from Access as the web app supports it. Ideally this will be organic by your users preferring the web. Selecting when to drop Access entirely can then be driven by user demand. Or, for example, as is true in my case, none of my end users use Access any more, but I still have one tool for administrators that isn't worth replacing yet so they still use Access. One of my partners also just feels more comfortable making ad-hoc queries/reports in Access than tools like pgAdmin3 or iReport so I let him, as long as it's just for his personal use and I don't have to guarantee it won't break if I evolve the schema. On the reporting side, I really haven't found a good Python reporting solution that includes a solid solution for the designer side of the coin. Prior to this project I most often used ReportLab for dynamically produced reports (and still do) and its great, but I was really the only one designing those reports and do most of them in code. For this project, I wanted an independent report format along with a visual designer (so others could design reports for the system to execute). I settled on JasperReports, which has a well-defined jrxml format, and a nice report design tool iReport. Someone else posted about OpenRPT which was also on my short list at the time (along, I think, with Eclipse BIRT). JasperReports (and its ecosystem) has a bit of a learning curve - then again, all these tools do, including Access - for my part, I've been happy with the choice. Now, while amongst report designers you can just exchange report design files and use iReport, for end users you'll need a way to process those reports for your web app. There is a Jasper Server tool that is designed to organize and publish reports, but I found it overly-complicated for my use case and I wanted complete control of the end user interaction (hiding the reporting behind my web app). I settled on a really small (~150 line) Java servlet, running beneath Jetty, and configured to access the same PostgreSQL database. It accepts localhost-only HTTP report requests (with report parameters as query parameters) and delivers the result as a PDF. So my main web application, when needed, makes an internal http request to the reporting server on the same host, and then delivers the returned PDF to the end user. Once up and running, I can design a new report in iReport, and then drop the jrxml design file into the reporting folder on my server and it'll be available for the web application to use. This is the only non-Python server component (as far as my own code). As with the web app itself, this reporting operates in parallel with any remaining Access functionality, so you need not replicate every Access report all at once. To put this all in context, for myself (almost zero Access experience at the start, but plenty of Python and PostgreSQL experience), my data conversion probably took about 3 months overall for a relatively small Access database until everyone was working off of PostgreSQL under the covers. But that was all on my time, the actual switchover was quick once ready to release. While some of that scales with size, most of the time was figuring out the process and testing. To be truthful, I'm here maybe 4 years later, and still have some uses of Access in the system. I suppose I could have pushed harder to completely retire it, but honestly, by following the above process the remaining uses just don't bother or interfere with me that much. If they ever do I'll replicate those remaining bits of functionality elsewhere. -- David From steve+comp.lang.python at pearwood.info Tue Nov 27 19:46:35 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 28 Nov 2012 00:46:35 GMT Subject: deepcopy questions References: Message-ID: <50b55eea$0$21750$c3e8da3$76491128@news.astraweb.com> On Tue, 27 Nov 2012 15:59:38 -0800, lars van gemerden wrote: > Hi, > > I get a very strange result when using deepcopy. The following code: > > def __deepcopy__(self, memo): > independent = self.independent() > if independent is self: > out = type(self)() > out.__dict__ = copy.deepcopy(self.__dict__, memo) > print self.__dict__ > print out.__dict__ #strange result > return out > else: > return copy.deepcopy(independent, memo).find(self.id).take() > > prints different results for self.__dict__ and out.__dict__: What makes you think that this is a strange result? What result are you expecting? > {'_active_': False, 'init': {}, '_id_': 0, '_items_': > [], '_name_': 'main'} > {'_active_': False, 'init': {}, '_id_': 0} > > Two items are missing in the copy. Maybe i am missing something obvious, > but i cannot figure out how this could happen. > > Can anyone tell me how this is possible? The most obvious guess is that the memo dict already contains _items_ and _names_, and so they get skipped. Please ensure your sample code can be run. You should create the simplest example of stand-alone code that other people can run. See more information here: http://sscce.org/ By the way, is it just me or is the documentation for deepcopy seriously lacking? http://docs.python.org/3/library/copy.html There's no mention of the additional arguments memo and _nil, and while the docs say to pass the memo dictionary to __deepcopy__ it doesn't document any restrictions on this memo, how to initialise it, or under what circumstances you would pass anything but an empty dict. -- Steven From python at mrabarnett.plus.com Tue Nov 27 19:59:13 2012 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 28 Nov 2012 00:59:13 +0000 Subject: deepcopy questions In-Reply-To: References: Message-ID: <50B561E1.4000501@mrabarnett.plus.com> On 2012-11-27 23:59, lars van gemerden wrote: > Hi, > > I get a very strange result when using deepcopy. The following code: > > def __deepcopy__(self, memo): > independent = self.independent() > if independent is self: > out = type(self)() > out.__dict__ = copy.deepcopy(self.__dict__, memo) > print self.__dict__ > print out.__dict__ #strange result > return out > else: > return copy.deepcopy(independent, memo).find(self.id).take() > > prints different results for self.__dict__ and out.__dict__: > > {'_active_': False, 'init': {}, '_id_': 0, '_items_': [], '_name_': 'main'} > {'_active_': False, 'init': {}, '_id_': 0} > > Two items are missing in the copy. Maybe i am missing something obvious, but i cannot figure out how this could happen. > > Can anyone tell me how this is possible? > I haven't been able to reproduce the problem. Could you provide a self-contained and _runnable_ piece of code that shows the problem? From dangbaminh.it at gmail.com Tue Nov 27 21:50:22 2012 From: dangbaminh.it at gmail.com (Minh Dang) Date: Tue, 27 Nov 2012 18:50:22 -0800 (PST) Subject: please help me to debud my local chat network program Message-ID: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> Hello everybody, i am doing my project: local network chat using python here is my file http://www.mediafire.com/?cc2g9tmsju0ba2m when i compile client.py, there is some bug Traceback (most recent call last): File "C:\Users\MINH_IT\workspace\project\src\project\pclient.py", line 303, in sys.exit(main()) File "C:\Users\MINH_IT\workspace\project\src\project\pclient.py", line 42, in main servIP = broadcast() File "C:\Users\MINH_IT\workspace\project\src\project\pclient.py", line 79, in broadcast udpSock.bind((broadcastIP, broadcastPort)) OSError: [WinError 10049] The requested address is not valid in its context Please help me to debud it, thank so much. From torriem at gmail.com Tue Nov 27 22:33:56 2012 From: torriem at gmail.com (Michael Torrie) Date: Tue, 27 Nov 2012 20:33:56 -0700 Subject: Migrate from Access 2010 / VBA In-Reply-To: References: <6db4ed18-a16e-4cd1-940b-c10f51207780@googlegroups.com> Message-ID: <50B58624.7020103@gmail.com> On 11/27/2012 05:06 PM, David Bolen wrote: > I went through a very similar transition a few years ago from > standalone Access databases (with GUI forms, queries and reports, as > well as replication) to a pure web application with full reporting > (albeit centrally designed and not a report designer for users). > > I suppose my best overall suggestion is to migrate the data first and > independently of any other activities. > > Having done this, you are then free to start implementing, for > example, a web based application to start taking over functionality. Very informative post, David. This is the only practical way forward, and had I not been away from the field so long (been a couple of years since I was in such a position of doing this professionally), I would have remembered this! MS Access is a very powerful tool; it just has a lousy default database engine. ODBC isn't a perfect interface, but Access does speak it, and so does a real DBM like PostgreSQL. There are a number of tools out there for converting an access database to SQL schema (if you don't have access to the schema). I used it on a commercial program once to check on their database parameters (shudder using mdb in a production program!), and actually had a script to sync it to read from it via ODBC and push it to a MySQL database. All this reminds me of a project I wanted to do, but hit a road block. My problem was I had a proprietary binary app, with an MDB data file (in the program directory no less). I'd like to share it across users, without worrying about corrupting the database, or even have a web front end. If I could set it up as David suggests with some kind of live mirroring, and so that the program didn't know it was a database server it was talking to, that would be good. But I had no control over the ODBC parameters (they were hard coded in the program). Would have loved to have separated out the access database part from the rest of the program though. I could then graft on a web front end. thanks again for the post, David. From tjreedy at udel.edu Tue Nov 27 22:48:16 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 27 Nov 2012 22:48:16 -0500 Subject: please help me to debud my local chat network program In-Reply-To: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> References: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> Message-ID: On 11/27/2012 9:50 PM, Minh Dang wrote: > Hello everybody, i am doing my project: local network chat using python > here is my file > http://www.mediafire.com/?cc2g9tmsju0ba2m I am not familiar with .rar files and whether I can open them. Better to upload a standard .zip. > when i compile client.py, there is some bug > Traceback (most recent call last): > File "C:\Users\MINH_IT\workspace\project\src\project\pclient.py", line 303, in > sys.exit(main()) sys.exit is not usually necessary. Just 'main()' should be sufficient. > File "C:\Users\MINH_IT\workspace\project\src\project\pclient.py", line 42, in main > servIP = broadcast() > File "C:\Users\MINH_IT\workspace\project\src\project\pclient.py", line 79, in broadcast > udpSock.bind((broadcastIP, broadcastPort)) > OSError: [WinError 10049] The requested address is not valid in its context The broadcastIP, which you defined elsewhere, whatever it is, is not valid for udpSock, also defined elsewhere. -- Terry Jan Reedy From rosuav at gmail.com Tue Nov 27 23:03:02 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 28 Nov 2012 15:03:02 +1100 Subject: please help me to debud my local chat network program In-Reply-To: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> References: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> Message-ID: On Wed, Nov 28, 2012 at 1:50 PM, Minh Dang wrote: > Hello everybody, i am doing my project: local network chat using python > here is my file > http://www.mediafire.com/?cc2g9tmsju0ba2m Hmm. Might I recommend some other means of sharing your code? The unrar-free utility from the Debian repo won't extract more than the first file (accounts.txt), and I don't know if that's a problem with unrar-free or your file. A better-known format like zip or tar.gz would make things easier. > when i compile client.py, there is some bug > Traceback (most recent call last): > File "C:\Users\MINH_IT\workspace\project\src\project\pclient.py", line 303, in > sys.exit(main()) > File "C:\Users\MINH_IT\workspace\project\src\project\pclient.py", line 42, in main > servIP = broadcast() > File "C:\Users\MINH_IT\workspace\project\src\project\pclient.py", line 79, in broadcast > udpSock.bind((broadcastIP, broadcastPort)) > OSError: [WinError 10049] The requested address is not valid in its context > Please help me to debud it, thank so much. What's the broadcastIP address you're using? (As mentioned above, I can't see your source code.) Is it the appropriate address for one of your interfaces? Broadcast UDP is a bit tricky sometimes. You need to explicitly enable broadcasting on the socket - I've not done this in Python but two seconds with Google suggests that this is needed: udpSock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) You may have already done this, but I don't know without seeing your code. My crystal ball tells me that you're trying to bind to a broadcast address (eg 192.168.0.255). This is incorrect; you need to instead bind to your own IP address (eg 192.168.0.17). You can probably just do this: udpSock.bind(('', broadcastPort)) However, my crystal ball has been playing up a bit lately, so it's hard to be sure. ChrisA From miki.tebeka at gmail.com Tue Nov 27 23:15:27 2012 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Tue, 27 Nov 2012 20:15:27 -0800 (PST) Subject: "non central" package management Message-ID: <8ea52e1b-2e02-40b2-8ce0-fcce7fc2ef20@googlegroups.com> Greetings, The usual package mangers (easy_install, pip ...) install packages in one central location. I'm looking for a solution that will allow every project (which run on the same machine) use it's own packages and versions of packages. (Context - we're running several applications on the same machine, and can't test *everything* when we update a package. Also these applications might ship to other machines to run - via hadoop streaming). I can see two options: 1. Use a different virtual env for every package (and will need activate the right one for every package). 2. Have a "lib" directory in each application with versioned packages (we use something like that with svn:externals for version management). 3. Write my own package manager. Anyone had the same problem? Any known solutions? Thanks, -- Miki From roy at panix.com Tue Nov 27 23:21:48 2012 From: roy at panix.com (Roy Smith) Date: Tue, 27 Nov 2012 23:21:48 -0500 Subject: "non central" package management References: <8ea52e1b-2e02-40b2-8ce0-fcce7fc2ef20@googlegroups.com> Message-ID: In article <8ea52e1b-2e02-40b2-8ce0-fcce7fc2ef20 at googlegroups.com>, Miki Tebeka wrote: > Greetings, > > The usual package mangers (easy_install, pip ...) install packages in one > central location. > > I'm looking for a solution that will allow every project (which run on the > same machine) use it's own packages and versions of packages. (Context - > we're running several applications on the same machine, and can't test > *everything* when we update a package. Also these applications might ship to > other machines to run - via hadoop streaming). You want virtualenv. Use pip to install packages, and they'll go into your (per-project) virtualenv. > 2. Have a "lib" directory in each application with versioned packages (we use > something like that with svn:externals for version management). What we do is run "pip freeze > requirements.txt" and check that into version control. When we deploy, we create a new virtualenv, then do "pip install -r requirements.txt". For hadoop jobs that we run on EMR under mrjob, we have the virtualenv setup in the bootstrap script. From miki.tebeka at gmail.com Tue Nov 27 23:30:53 2012 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Tue, 27 Nov 2012 20:30:53 -0800 (PST) Subject: "non central" package management In-Reply-To: References: <8ea52e1b-2e02-40b2-8ce0-fcce7fc2ef20@googlegroups.com> Message-ID: On Tuesday, November 27, 2012 8:21:48 PM UTC-8, Roy Smith wrote: > What we do is run "pip freeze > requirements.txt" and check that into > version control. That's the idea I was toying with, thanks for confirming. > When we deploy, we create a new virtualenv, then do > "pip install -r requirements.txt". 1. Do you do that for every run? 2. Our ops team don't like to depend on pypi, they want internal repo - but I guess I can do that with "pip install -f". Thanks, -- Miki From roy at panix.com Tue Nov 27 23:45:56 2012 From: roy at panix.com (Roy Smith) Date: Tue, 27 Nov 2012 23:45:56 -0500 Subject: "non central" package management References: <8ea52e1b-2e02-40b2-8ce0-fcce7fc2ef20@googlegroups.com> Message-ID: In article , Miki Tebeka wrote: > > When we deploy, we create a new virtualenv, then do > > "pip install -r requirements.txt". > 1. Do you do that for every run? Well, sort of. We are currently using a single virtualenv per deployment host. Each time we deploy new code, we checkout all the code into a fresh directory, but each of those shares a common virtualenv. As part of the deploy process, we do indeed execute "pip install -r requirements.txt", which picks up any new required packages. Unfortunately, that process doesn't give us a good way to back out a bad update. It's easy for us to revert to an previous version of our code, but we don't have a good way to revert the virtualenv to its previous state. Fortunately, that's been mostly a theoretical issue for us so far. In the future, the plan is to build a complete fresh virtualenv for every deployment. But we're not there yet. > 2. Our ops team don't like to depend on pypi, they want internal repo - but I > guess I can do that with "pip install -f". Listen to your ops team. Right now, we install out of pypi, but that's slow, and on occasion, fails. This is part of why we still use a shared virtualenv, and what your ops guys are trying to avoid :-) Eventually, we'll mirror all the packages we need locally, and pip install out of that. Once we've got that all working, we'll move to a new virtualenv per deployment (which sometimes is several times a day). From dangbaminh.it at gmail.com Wed Nov 28 00:47:08 2012 From: dangbaminh.it at gmail.com (Minh Dang) Date: Tue, 27 Nov 2012 21:47:08 -0800 (PST) Subject: please help me to debud my local chat network program In-Reply-To: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> References: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> Message-ID: <6dfb8bda-3a54-47ce-b511-fd6945b4d4cf@googlegroups.com> ok, here is my code, zip http://www.mediafire.com/?ob4kokda81fj6xc From prakashr85 at gmail.com Wed Nov 28 01:32:53 2012 From: prakashr85 at gmail.com (Prakash) Date: Tue, 27 Nov 2012 22:32:53 -0800 (PST) Subject: How to create an executable from python script in windows Message-ID: I had created a python script which actually uses pywin32 com. It opens few excel files and process it and produce output. Whenever I try to create an windows exe file usin py2exe it diplays error. Please guide me I am a noob in creating windows executable. Thanks in advance From rosuav at gmail.com Wed Nov 28 01:33:42 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 28 Nov 2012 17:33:42 +1100 Subject: please help me to debud my local chat network program In-Reply-To: <6dfb8bda-3a54-47ce-b511-fd6945b4d4cf@googlegroups.com> References: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> <6dfb8bda-3a54-47ce-b511-fd6945b4d4cf@googlegroups.com> Message-ID: On Wed, Nov 28, 2012 at 4:47 PM, Minh Dang wrote: > ok, here is my code, zip > http://www.mediafire.com/?ob4kokda81fj6xc Thanks! That's a distinct improvement :) That code doesn't exactly match the traceback, though; there's client.py not pclient.py and the line numbers don't match. So I have to just guess that the error is the same. However, I can see an immediate problem. def broadcast(): broadcastIP = "" broadcastPort = 9999 That's not a valid broadcast IP :) I think you probably want to bind to "" (aka "any address"). ChrisA From steve+comp.lang.python at pearwood.info Wed Nov 28 01:37:01 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 28 Nov 2012 06:37:01 GMT Subject: How to create an executable from python script in windows References: Message-ID: <50b5b10d$0$21750$c3e8da3$76491128@news.astraweb.com> On Tue, 27 Nov 2012 22:32:53 -0800, Prakash wrote: > I had created a python script which actually uses pywin32 com. It opens > few excel files and process it and produce output. Whenever I try to > create an windows exe file usin py2exe it diplays error. Please guide me > I am a noob in creating windows executable. Thanks in advance Would you like us to guess what the error is? I love guessing games! My guess is... "File not found". You need to give the right name for the file. Am I close? If my guess was wrong, you need to tell us what the error is. -- Steven From prakashr85 at gmail.com Wed Nov 28 01:43:01 2012 From: prakashr85 at gmail.com (Prakash) Date: Tue, 27 Nov 2012 22:43:01 -0800 (PST) Subject: How to create an executable from python script in windows In-Reply-To: <50b5b10d$0$21750$c3e8da3$76491128@news.astraweb.com> References: <50b5b10d$0$21750$c3e8da3$76491128@news.astraweb.com> Message-ID: <4746a3d8-4aa9-412b-8b73-2f0e422ab5bf@googlegroups.com> It says like *** copy extensions *** *** copy dlls *** copying C:\Python24\lib\site-packages\py2exe\run_w.exe -> C:\Program Files (x86)\Notepad++\dist\build-check-test.exe Adding python24.dll as resource to C:\Program Files (x86)\Notepad++\dist\build-check-test.exe The following modules appear to be missing ['win32com.shell'] *** binary dependencies *** Your executable(s) also depend on these dlls which are not included, you may or may not need to distribute them. Make sure you have the license if you distribute any of them, and make sure you don't distribute files belonging to the operating system. ole32.dll - C:\Windows\system32\ole32.dll OLEAUT32.dll - C:\Windows\system32\OLEAUT32.dll USER32.dll - C:\Windows\system32\USER32.dll IMM32.dll - C:\Windows\system32\IMM32.dll SHELL32.dll - C:\Windows\system32\SHELL32.dll ntdll.dll - C:\Windows\system32\ntdll.dll comdlg32.dll - C:\Windows\system32\comdlg32.dll COMCTL32.dll - C:\Windows\system32\COMCTL32.dll ADVAPI32.dll - C:\Windows\system32\ADVAPI32.dll MFC71.DLL - C:\Python24\lib\site-packages\Pythonwin\MFC71.DLL msvcrt.dll - C:\Windows\system32\msvcrt.dll WINSPOOL.DRV - C:\Windows\system32\WINSPOOL.DRV GDI32.dll - C:\Windows\system32\GDI32.dll VERSION.dll - C:\Windows\system32\VERSION.dll KERNEL32.dll - C:\Windows\system32\KERNEL32.dll SETUPAPI.dll - C:\Windows\system32\SETUPAPI.dll KERNELBASE.dll - C:\Windows\system32\KERNELBASE.dll RPCRT4.dll - C:\Windows\system32\RPCRT4.dll From lars at rational-it.com Wed Nov 28 04:05:20 2012 From: lars at rational-it.com (lars van gemerden) Date: Wed, 28 Nov 2012 01:05:20 -0800 (PST) Subject: deepcopy questions In-Reply-To: References: Message-ID: <39edd363-2937-48a8-b84d-54d3993e42cf@googlegroups.com> On Wednesday, November 28, 2012 12:59:38 AM UTC+1, lars van gemerden wrote: > Hi, > > > > I get a very strange result when using deepcopy. The following code: > > > > def __deepcopy__(self, memo): > > independent = self.independent() > > if independent is self: > > out = type(self)() > > out.__dict__ = copy.deepcopy(self.__dict__, memo) > > print self.__dict__ > > print out.__dict__ #strange result > > return out > > else: > > return copy.deepcopy(independent, memo).find(self.id).take() > > > > prints different results for self.__dict__ and out.__dict__: > > > > {'_active_': False, 'init': {}, '_id_': 0, '_items_': [], '_name_': 'main'} > > {'_active_': False, 'init': {}, '_id_': 0} > > > > Two items are missing in the copy. Maybe i am missing something obvious, but i cannot figure out how this could happen. > > > > Can anyone tell me how this is possible? > > > > Cheers, Lars I have just tried to make a simple runnable testcase but no luck. in my code it's part of a rather complex data structure. As I understood the documentation, the memo parameter is to hold a dictionary of data that have already been copied (e.g. to deal with circular references), and is normally only explicitly used when implementing __deepcopy__, just passing memo to calls to deepcopy within the body of __deepcopy__. If memo contains items, this should, to my understanding, not remove them from the output of deepcopy, they will just not be copied again, but instead be taken from memo and put in the output (otherwise 2 references to the same object would after deepcopying result in 2 distinct copies of that object). Anyway, since i cannot reproduce the error in a simple testcase and i have no adea what is going on, I'll implement what i need differently. Any ideas are still more then welcome, Thanks for the feedback, Lars From support at geo.au.dk Wed Nov 28 04:52:41 2012 From: support at geo.au.dk (support at geo.au.dk) Date: Wed, 28 Nov 2012 10:52:41 +0100 (CET) Subject: Python-list Digest, Vol 108, Issue 248 Message-ID: <21572011.1504.1354096361256.JavaMail.SYSTEM@www02> Message ignored: Did not match any rules -----Original Message----- Subject: Python-list Digest, Vol 108, Issue 248 To: python-list at python.org From: python-list-request at python.org Send Python-list mailing list submissions to python-list at python.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.python.org/mailman/listinfo/python-list or, via email, send a message with subject or body 'help' to python-list-request at python.org You can reach the person managing the list at python-list-owner at python.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Python-list digest..." Today's Topics: 1. #python archives? (Steven D'Aprano) 2. Re: Does os.getcwd() and os.curdir have the same effect ? (iMath) 3. Re: Does os.getcwd() and os.curdir have the same effect ? (iMath) 4. Re: python application file format (Dieter Maurer) 5. ANN: eGenix PyRun - One file Python Runtime 1.1.0 (eGenix Team: M.-A. Lemburg) 6. Re: Memory usage per top 10x usage per heapy (bryanjugglercryptographer at yahoo.com) 7. Re: Memory usage per top 10x usage per heapy (bryanjugglercryptographer at yahoo.com) 8. Module baldy compiled to pyc? (Laszlo Nagy) 9. Re: Article on the future of Python (Serhiy Storchaka) -- http://mail.python.org/mailman/listinfo/python-list From ulrich.eckhardt at dominolaser.com Wed Nov 28 05:05:40 2012 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Wed, 28 Nov 2012 11:05:40 +0100 Subject: How to create an executable from python script in windows In-Reply-To: <4746a3d8-4aa9-412b-8b73-2f0e422ab5bf@googlegroups.com> References: <50b5b10d$0$21750$c3e8da3$76491128@news.astraweb.com> <4746a3d8-4aa9-412b-8b73-2f0e422ab5bf@googlegroups.com> Message-ID: Am 28.11.2012 07:43, schrieb Prakash: > copying C:\Python24\lib\site-packages\py2exe\run_w.exe ^^^^^^^^ Python 2.4 was released 8 years ago and shouldn't be used for new development or learning any longer. The first step I would take is to upgrade to 2.7, which is the last in the 2.x series. Further, I would at least consider upgrading to Python 3.x, although this might require some tweaking of your sourcecode first. Keep in mind that 2.x is a dead end though! > The following modules appear to be missing > ['win32com.shell'] If the warning persists with 2.7 and you are actually getting errors at runtime, throw the whole message at e.g. google.com and click a few links. Hint: This is a known problem, you are not the first one! Good luck! Uli From mal at egenix.com Wed Nov 28 05:34:05 2012 From: mal at egenix.com (M.-A. Lemburg) Date: Wed, 28 Nov 2012 11:34:05 +0100 Subject: ANN: eGenix mx Base Distribution 3.2.5 (mxDateTime, mxTextTools, etc.) Message-ID: <50B5E89D.9070700@egenix.com> ________________________________________________________________________ ANNOUNCING eGenix.com mx Base Distribution Version 3.2.5 for Python 2.4 - 2.7 Open Source Python extensions providing important and useful services for Python programmers. This announcement is also available on our web-site for online reading: http://www.egenix.com/company/news/eGenix-mx-Base-Distribution-3.2.5-GA.html ________________________________________________________________________ ABOUT The eGenix.com mx Base Distribution for Python is a collection of professional quality software tools which enhance Python's usability in many important areas such as fast text searching, date/time processing and high speed data types. The tools have a proven record of being portable across many Unix and Windows platforms. You can write applications which use the tools on Windows and then run them on Unix platforms without change due to the consistent platform independent interfaces. Contents of the distribution: * mxDateTime - Easy to use Date/Time Library for Python * mxTextTools - Fast Text Parsing and Processing Tools for Python * mxProxy - Object Access Control for Python * mxBeeBase - On-disk B+Tree Based Database Kit for Python * mxURL - Flexible URL Data-Type for Python * mxUID - Fast Universal Identifiers for Python * mxStack - Fast and Memory-Efficient Stack Type for Python * mxQueue - Fast and Memory-Efficient Queue Type for Python * mxTools - Fast Everyday Helpers for Python The package also include a number of helpful smaller modules in the mx.Misc subpackage, such as mx.Misc.ConfigFile for config file parsing or mx.Misc.CommandLine to quickly write command line applications in Python. All available packages have proven their stability and usefulness in many mission critical applications and various commercial settings all around the world. For more information, please see the distribution page: http://www.egenix.com/products/python/mxBase/ ________________________________________________________________________ NEWS The 3.2.5 release of the eGenix mx Base Distribution is the latest release of our open-source Python extensions. The new patch-level version includes a few important fixes: * Fixed a compatibility problem with Python 2.7.3 on Mac OS X 10.6 and later: Removed mx_customize_compiler() in favor of the standard distutils customize_compiler(). It now installs fine again with pip on more recent Mac OS X versions. Thanks to Leonardo Santagada for bringing this problem to our attention. * mxDateTime: Fixed a possible segfault in mxDateTime that was caused by the lazy datetime module import mechanism not catching all cases where the C API was used in mxDateTime. Thanks to Joel Rosdahl for bringing this to our attention. * Fixed a bug in the mx.Misc.CSV.Reader.objects() method, which triggered a TypeError. If you are upgrading from eGenix mx Base 3.1.x, please also see the eGenix mx Base Distribution 3.2.0 release notes for details on what has changed and which new features are available: http://www.egenix.com/company/news/eGenix-mx-Base-Distribution-3.2.0-GA.html As always, we are providing pre-built binaries for all common platforms: Windows 32/64-bit, Linux 32/64-bit, FreeBSD 32/64-bit, Mac OS X 32/64-bit. Source code archives are available for installation on all other Python platforms, such as Solaris, AIX, HP-UX, etc. To simplify installation in Zope/Plone and other egg-based systems, we have also precompiled egg distributions for all platforms. These are available on our own PyPI-style index server for easy and automatic download. Whether you are using a pre-built package or the source distribution, installation is a simple "python setup.py install" command in all cases. The only difference is that the pre-built packages do not require a compiler or the Python development packages to be installed. For a full list of changes, please refer to the eGenix mx Base Distribution change log at http://www.egenix.com/products/python/mxBase/changelog.html and the change logs of the various included Python packages. ________________________________________________________________________ DOWNLOADS The download archives and instructions for installing the packages can be found on the eGenix mx Base Distribution page: http://www.egenix.com/products/python/mxBase/ ________________________________________________________________________ LICENSE The eGenix mx Base package is distributed under the eGenix.com Public License 1.1.0 which is an Open Source license similar to the Python license. You can use the packages in both commercial and non-commercial settings without fee or charge. The package comes with full source code ________________________________________________________________________ SUPPORT Commercial support for this product is available from eGenix.com. Please see http://www.egenix.com/services/support/ for details about our support offerings. Enjoy, -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Nov 28 2012) >>> Python Projects, Consulting and Support ... http://www.egenix.com/ >>> mxODBC.Zope/Plone.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ ________________________________________________________________________ ::: Try our new mxODBC.Connect Python Database Interface for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ From dangbaminh.it at gmail.com Wed Nov 28 05:52:42 2012 From: dangbaminh.it at gmail.com (Minh Dang) Date: Wed, 28 Nov 2012 02:52:42 -0800 (PST) Subject: please help me to debud my local chat network program In-Reply-To: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> References: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> Message-ID: but, how can I fix it? From dangbaminh.it at gmail.com Wed Nov 28 06:10:45 2012 From: dangbaminh.it at gmail.com (Minh Dang) Date: Wed, 28 Nov 2012 03:10:45 -0800 (PST) Subject: please help me to debud my local chat network program In-Reply-To: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> References: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> Message-ID: <0776c5d4-05c6-4bea-af12-360fcc67728e@googlegroups.com> ok, in client.py, i change broadcastIP = "broadcast" to broadcastIP = "127.0.0.1" but there are some bugs sys.exit(main()) rQueue, wQueue, xQueue = select.select(inputs, [], []) From rosuav at gmail.com Wed Nov 28 06:16:04 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 28 Nov 2012 22:16:04 +1100 Subject: please help me to debud my local chat network program In-Reply-To: References: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> Message-ID: On Wed, Nov 28, 2012 at 9:52 PM, Minh Dang wrote: > but, how can I fix it? I can't say that for sure, but did you read the bit at the end of my last post beginning "I think you probably want"? Try doing that, see what happens. ChrisA From rosuav at gmail.com Wed Nov 28 06:17:54 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 28 Nov 2012 22:17:54 +1100 Subject: please help me to debud my local chat network program In-Reply-To: <0776c5d4-05c6-4bea-af12-360fcc67728e@googlegroups.com> References: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> <0776c5d4-05c6-4bea-af12-360fcc67728e@googlegroups.com> Message-ID: On Wed, Nov 28, 2012 at 10:10 PM, Minh Dang wrote: > ok, in client.py, i change broadcastIP = "broadcast" to broadcastIP = "127.0.0.1" but there are some bugs I doubt that that will work either. And you may need to be a little clearer on what you mean by "there are some bugs" - is it doing what you don't expect? Are you getting an exception? Tips: http://catb.org/~esr/faqs/smart-questions.html ChrisA From dangbaminh.it at gmail.com Wed Nov 28 06:17:58 2012 From: dangbaminh.it at gmail.com (Minh Dang) Date: Wed, 28 Nov 2012 03:17:58 -0800 (PST) Subject: please help me to debud my local chat network program In-Reply-To: References: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> Message-ID: <4a881fad-e901-4f9a-874f-5d7e97c6ac57@googlegroups.com> ok, in client.py, i change broadcastIP = "broadcast" to broadcastIP = "127.0.0.1",it's ok, but there are some bugs sys.exit(main()) rQueue, wQueue, xQueue = select.select(inputs, [], []) From dangbaminh.it at gmail.com Wed Nov 28 06:17:58 2012 From: dangbaminh.it at gmail.com (Minh Dang) Date: Wed, 28 Nov 2012 03:17:58 -0800 (PST) Subject: please help me to debud my local chat network program In-Reply-To: References: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> Message-ID: <4a881fad-e901-4f9a-874f-5d7e97c6ac57@googlegroups.com> ok, in client.py, i change broadcastIP = "broadcast" to broadcastIP = "127.0.0.1",it's ok, but there are some bugs sys.exit(main()) rQueue, wQueue, xQueue = select.select(inputs, [], []) From dangbaminh.it at gmail.com Wed Nov 28 06:23:34 2012 From: dangbaminh.it at gmail.com (Minh Dang) Date: Wed, 28 Nov 2012 03:23:34 -0800 (PST) Subject: please help me to debud my local chat network program In-Reply-To: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> References: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> Message-ID: <80e0fe90-1f33-4544-8d9c-16c06db50f9b@googlegroups.com> yes, still have 2 bugs: sys.exit(main()) rQueue, wQueue, xQueue = select.select(inputs, [], []) From dangbaminh.it at gmail.com Wed Nov 28 06:57:41 2012 From: dangbaminh.it at gmail.com (Minh Dang) Date: Wed, 28 Nov 2012 03:57:41 -0800 (PST) Subject: please help me to debud my local chat network program In-Reply-To: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> References: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> Message-ID: <3aa89e19-05e9-4593-95c8-e142297339a6@googlegroups.com> please help me, after changing broadcast, there are 2 bugs: if __name__ == "__main__": sys.exit(main()) and rQueue, wQueue, xQueue = select.select(inputs, [], []) please help me From rosuav at gmail.com Wed Nov 28 07:24:07 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 28 Nov 2012 23:24:07 +1100 Subject: please help me to debud my local chat network program In-Reply-To: <3aa89e19-05e9-4593-95c8-e142297339a6@googlegroups.com> References: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> <3aa89e19-05e9-4593-95c8-e142297339a6@googlegroups.com> Message-ID: On Wed, Nov 28, 2012 at 10:57 PM, Minh Dang wrote: > please help me, after changing broadcast, there are 2 bugs: > if __name__ == "__main__": > sys.exit(main()) > and > > rQueue, wQueue, xQueue = select.select(inputs, [], []) > please help me By "bug", I'm guessing you mean one of two things: 1) The program fails to run, and an exception is given that points to one of these lines, or 2) The program runs, but does something different from what you expect it to do. If it's the first, what's the exception? If the second, what do you expect and what happens? You need to provide more information. Check out the link I gave you on how to ask questions; it's very helpful. ChrisA From amca01 at gmail.com Wed Nov 28 07:30:25 2012 From: amca01 at gmail.com (Alasdair McAndrew) Date: Wed, 28 Nov 2012 04:30:25 -0800 (PST) Subject: Imaging libraries in active development? Message-ID: I'm investigating Python for image processing (having used Matlab, then Octave for some years). And I'm spoiled for choice: PIL and its fork pillow, scipy.ndimage, scikits-image, mahotas, the Python interface to openCV... However, PIL doesn't seem to be in active development. What I want to know is - what are the current "standard" libraries for image processing in Python which are in active development? I have quite a few image processing student notes which I'm thinking of converting to Python, but I'd like to use the most up-to-date library. Thanks, Alasdair From dachakku at gmail.com Wed Nov 28 07:38:35 2012 From: dachakku at gmail.com (dachakku at gmail.com) Date: Wed, 28 Nov 2012 04:38:35 -0800 (PST) Subject: how to pass "echo t | " input to subprocess.check_output() method In-Reply-To: References: Message-ID: <67078169-6b32-4fca-8a53-f504998087a4@googlegroups.com> On Monday, 26 November 2012 21:10:02 UTC+5:30, Miki Tebeka wrote: > > But i dont know how to pass the "echo t | " in subprocess.check_output while calling a process. > > You need to create two subprocess and connect the stdout of the first to the stdin of the 2'nd. > > > > See http://pythonwise.blogspot.com/2008/08/pipe.html for a possible solution. Hi Miki, Thanks.. Creating two subprocesses worked for me. I did the code as below, p = subprocess.Popen("echo t |", shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) p1 = subprocess.Popen(["svn.exe", "list", "Https://127.0.0.1:443/svn/Repos"], shell=True, stdin=p.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() output = p1[0] Thanks again... :) From sachit.technerves at gmail.com Wed Nov 28 07:39:24 2012 From: sachit.technerves at gmail.com (sachit.technerves at gmail.com) Date: Wed, 28 Nov 2012 04:39:24 -0800 (PST) Subject: How to show and parse rsync progress in Python/Django? Message-ID: I am using the development version of rsync with --info-progress option. I am writing a python program which transfer files from server to local computer using rsync: finalresult = subprocess.Popen(['sshpass', '-p', password, 'rsync', '-avz', '--info=progress2', 'hostname:/filename', '/home/nfs/django/user'], stdout=subprocess.PIPE).communicate()[0] This should store the processing in the finalresult which is not happening. I just want to show the file syncing progress in my front-end Django app. How can I do that? Thanks From dangbaminh.it at gmail.com Wed Nov 28 09:10:54 2012 From: dangbaminh.it at gmail.com (Minh Dang) Date: Wed, 28 Nov 2012 06:10:54 -0800 (PST) Subject: please help me to debud my local chat network program In-Reply-To: References: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> <3aa89e19-05e9-4593-95c8-e142297339a6@googlegroups.com> Message-ID: <0a66d722-e46c-4230-a39c-2115d84b626e@googlegroups.com> yes, it's run but appear: in server: Server is ready. Accepted connection from ('127.0.0.1', 5000) End connection from ('127.0.0.1', 5000) after connected, server close. in client: Connected to server at 127.0.0.1 : 4000 Traceback (most recent call last): File "C:\Users\MINH_IT\workspace\project\src\project\client.py", line 303, in sys.exit(main()) File "C:\Users\MINH_IT\workspace\project\src\project\client.py", line 60, in main rQueue, wQueue, xQueue = select.select(inputs, [], []) OSError: [WinError 10038] An operation was attempted on something that is not a socket there are 2 problems. From dangbaminh.it at gmail.com Wed Nov 28 09:10:54 2012 From: dangbaminh.it at gmail.com (Minh Dang) Date: Wed, 28 Nov 2012 06:10:54 -0800 (PST) Subject: please help me to debud my local chat network program In-Reply-To: References: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> <3aa89e19-05e9-4593-95c8-e142297339a6@googlegroups.com> Message-ID: <0a66d722-e46c-4230-a39c-2115d84b626e@googlegroups.com> yes, it's run but appear: in server: Server is ready. Accepted connection from ('127.0.0.1', 5000) End connection from ('127.0.0.1', 5000) after connected, server close. in client: Connected to server at 127.0.0.1 : 4000 Traceback (most recent call last): File "C:\Users\MINH_IT\workspace\project\src\project\client.py", line 303, in sys.exit(main()) File "C:\Users\MINH_IT\workspace\project\src\project\client.py", line 60, in main rQueue, wQueue, xQueue = select.select(inputs, [], []) OSError: [WinError 10038] An operation was attempted on something that is not a socket there are 2 problems. From rosuav at gmail.com Wed Nov 28 09:21:14 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 29 Nov 2012 01:21:14 +1100 Subject: please help me to debud my local chat network program In-Reply-To: <0a66d722-e46c-4230-a39c-2115d84b626e@googlegroups.com> References: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> <3aa89e19-05e9-4593-95c8-e142297339a6@googlegroups.com> <0a66d722-e46c-4230-a39c-2115d84b626e@googlegroups.com> Message-ID: On Thu, Nov 29, 2012 at 1:10 AM, Minh Dang wrote: > File "C:\Users\MINH_IT\workspace\project\src\project\client.py", line 60, in main > rQueue, wQueue, xQueue = select.select(inputs, [], []) > OSError: [WinError 10038] An operation was attempted on something that is not a socket Have a look at that line of code - specifically, look at what's in inputs. One of them perhaps isn't a socket. Tip: The print function is your friend. ChrisA From miki.tebeka at gmail.com Wed Nov 28 09:46:03 2012 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Wed, 28 Nov 2012 06:46:03 -0800 (PST) Subject: "non central" package management In-Reply-To: References: <8ea52e1b-2e02-40b2-8ce0-fcce7fc2ef20@googlegroups.com> Message-ID: On Tuesday, November 27, 2012 8:45:56 PM UTC-8, Roy Smith wrote: > In the future, the plan is to build a complete fresh virtualenv for > every deployment. But we're not there yet. Maybe a repository of virtualenvs, then when deploying you can see if there's one the matches what you need and use it. Otherwise create a new one. From miki.tebeka at gmail.com Wed Nov 28 09:47:38 2012 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Wed, 28 Nov 2012 06:47:38 -0800 (PST) Subject: how to pass "echo t | " input to subprocess.check_output() method In-Reply-To: <67078169-6b32-4fca-8a53-f504998087a4@googlegroups.com> References: <67078169-6b32-4fca-8a53-f504998087a4@googlegroups.com> Message-ID: <106fc449-5ffd-44b2-b216-b425262e03e6@googlegroups.com> On Wednesday, November 28, 2012 4:38:35 AM UTC-8, dach... at gmail.com wrote: > Thanks.. Creating two subprocesses worked for me. I did the code as below, Glad it worked. From dangbaminh.it at gmail.com Wed Nov 28 10:27:06 2012 From: dangbaminh.it at gmail.com (Minh Dang) Date: Wed, 28 Nov 2012 07:27:06 -0800 (PST) Subject: please help me to debud my local chat network program In-Reply-To: References: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> <3aa89e19-05e9-4593-95c8-e142297339a6@googlegroups.com> <0a66d722-e46c-4230-a39c-2115d84b626e@googlegroups.com> Message-ID: there are still these 2 problems and i don't know how to solve it From dangbaminh.it at gmail.com Wed Nov 28 10:27:06 2012 From: dangbaminh.it at gmail.com (Minh Dang) Date: Wed, 28 Nov 2012 07:27:06 -0800 (PST) Subject: please help me to debud my local chat network program In-Reply-To: References: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> <3aa89e19-05e9-4593-95c8-e142297339a6@googlegroups.com> <0a66d722-e46c-4230-a39c-2115d84b626e@googlegroups.com> Message-ID: there are still these 2 problems and i don't know how to solve it From xenplex at gmail.com Wed Nov 28 11:39:53 2012 From: xenplex at gmail.com (Ritchie Flick) Date: Wed, 28 Nov 2012 17:39:53 +0100 Subject: Imaging libraries in active development? In-Reply-To: References: Message-ID: This may be of some interest to you: http://www.youtube.com/watch?v=Wvvxazwi2IY&feature=plcp On Wed, Nov 28, 2012 at 1:30 PM, Alasdair McAndrew wrote: > I'm investigating Python for image processing (having used Matlab, then > Octave for some years). And I'm spoiled for choice: PIL and its fork > pillow, scipy.ndimage, scikits-image, mahotas, the Python interface to > openCV... > > However, PIL doesn't seem to be in active development. What I want to > know is - what are the current "standard" libraries for image processing in > Python which are in active development? > > I have quite a few image processing student notes which I'm thinking of > converting to Python, but I'd like to use the most up-to-date library. > > Thanks, > Alasdair > -- > http://mail.python.org/mailman/listinfo/python-list > -- Ritchie Flick -------------- next part -------------- An HTML attachment was scrubbed... URL: From torriem at gmail.com Wed Nov 28 13:14:30 2012 From: torriem at gmail.com (Michael Torrie) Date: Wed, 28 Nov 2012 11:14:30 -0700 Subject: Imaging libraries in active development? In-Reply-To: References: Message-ID: <50B65486.8010306@gmail.com> On 11/28/2012 05:30 AM, Alasdair McAndrew wrote: > I'm investigating Python for image processing (having used Matlab, > then Octave for some years). And I'm spoiled for choice: PIL and its > fork pillow, scipy.ndimage, scikits-image, mahotas, the Python > interface to openCV... > > However, PIL doesn't seem to be in active development. What I want > to know is - what are the current "standard" libraries for image > processing in Python which are in active development? > > I have quite a few image processing student notes which I'm thinking > of converting to Python, but I'd like to use the most up-to-date > library. I'm curious. What features do you need that pil doesn't have? Other than updating pil to fix bugs, support new image types or new versions of Python, what kind of active development do you think it needs to have? Maybe pil has all the features the original author wanted and is pretty stable. To judge a package on how fast it's changing seems a bit odd to me. Obviously you want to know that bugs can get fixed of course. Perhaps none have been reported recently. From andriy.kornatskyy at live.com Wed Nov 28 13:52:33 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Wed, 28 Nov 2012 21:52:33 +0300 Subject: How to ship eggs with pyo files only Message-ID: There is sometimes a need to ship python egg distribution with pyo files only. There is confusion using bdist_egg command since it doesn't have any options to do that; you can read more about solution here: http://mindref.blogspot.com/2012/11/python-egg-pyo.html Thanks. Andriy Kornatskyy From christian at python.org Wed Nov 28 14:00:28 2012 From: christian at python.org (Christian Heimes) Date: Wed, 28 Nov 2012 20:00:28 +0100 Subject: Imaging libraries in active development? In-Reply-To: <50B65486.8010306@gmail.com> References: <50B65486.8010306@gmail.com> Message-ID: Am 28.11.2012 19:14, schrieb Michael Torrie: > I'm curious. What features do you need that pil doesn't have? Other > than updating pil to fix bugs, support new image types or new versions > of Python, what kind of active development do you think it needs to > have? Maybe pil has all the features the original author wanted and is > pretty stable. To judge a package on how fast it's changing seems a bit > odd to me. Obviously you want to know that bugs can get fixed of > course. Perhaps none have been reported recently. PIL is missing a bunch of features like proper TIFF support (no multipage, g3/g4 compression and more), JPEG 2000, RAW and HDR image formats, tone mapping, proper ICC support, PEP 3128 buffer support ... PIL is also rather slow. My smc.freeimage library can write JPEGs about six times faster, because it uses libjpeg-turbo. Only some Linux distributions have replaced libjpeg with the turbo implementation. From kevinintx at gmail.com Wed Nov 28 14:39:48 2012 From: kevinintx at gmail.com (Kevin T) Date: Wed, 28 Nov 2012 11:39:48 -0800 (PST) Subject: re.search when used within an if/else fails In-Reply-To: References: <50aadbe6$0$29983$c3e8da3$5496439d@news.astraweb.com> <50aadcf0$0$29983$c3e8da3$5496439d@news.astraweb.com> <2930dc36-9d8f-411d-aa7a-272fe4885ec5@i5g2000yqh.googlegroups.com> Message-ID: <11835b30-3448-45cc-9ae5-46d651f93429@googlegroups.com> I agree. Being relatively new to python, i was not sure of quirks so i posted the original code. I did find the real issue, as I found another loop that was not being executed properly. It turns out that if the indent started with spaces and ended with tabs, neither eclipse or command line execution would complain. where as if the indent begins with tabs and has spaces in the middle the tools will complain of indentation issues. i have found that the vi plugin for python is the culprit here. when the plugin does block indentation, the result is indents that begin with spaces. if i disable the vi plugin and use the regular eclipse editor these issues go away. with other languages i always expand tabs to spaces. the vi plugin does do this properly. if i change all indents to be spaces only will python behave? i inherited a good deal of the code that i am using, which is tab based. thanks kevin On Wednesday, November 21, 2012 11:00:50 PM UTC-6, Chris Angelico wrote: > On Thu, Nov 22, 2012 at 3:41 AM, Kevin T wrote: > > > I went back and tried version a again, blam it is/does work now ?!?!? > > > > This is why the Short, Self-Contained, Correct Example is so > > important. See http://sscce.org/ for some info on that. I often find > From kevinintx at gmail.com Wed Nov 28 14:39:48 2012 From: kevinintx at gmail.com (Kevin T) Date: Wed, 28 Nov 2012 11:39:48 -0800 (PST) Subject: re.search when used within an if/else fails In-Reply-To: References: <50aadbe6$0$29983$c3e8da3$5496439d@news.astraweb.com> <50aadcf0$0$29983$c3e8da3$5496439d@news.astraweb.com> <2930dc36-9d8f-411d-aa7a-272fe4885ec5@i5g2000yqh.googlegroups.com> Message-ID: <11835b30-3448-45cc-9ae5-46d651f93429@googlegroups.com> I agree. Being relatively new to python, i was not sure of quirks so i posted the original code. I did find the real issue, as I found another loop that was not being executed properly. It turns out that if the indent started with spaces and ended with tabs, neither eclipse or command line execution would complain. where as if the indent begins with tabs and has spaces in the middle the tools will complain of indentation issues. i have found that the vi plugin for python is the culprit here. when the plugin does block indentation, the result is indents that begin with spaces. if i disable the vi plugin and use the regular eclipse editor these issues go away. with other languages i always expand tabs to spaces. the vi plugin does do this properly. if i change all indents to be spaces only will python behave? i inherited a good deal of the code that i am using, which is tab based. thanks kevin On Wednesday, November 21, 2012 11:00:50 PM UTC-6, Chris Angelico wrote: > On Thu, Nov 22, 2012 at 3:41 AM, Kevin T wrote: > > > I went back and tried version a again, blam it is/does work now ?!?!? > > > > This is why the Short, Self-Contained, Correct Example is so > > important. See http://sscce.org/ for some info on that. I often find > From ian.g.kelly at gmail.com Wed Nov 28 14:57:08 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 28 Nov 2012 12:57:08 -0700 Subject: re.search when used within an if/else fails In-Reply-To: <11835b30-3448-45cc-9ae5-46d651f93429@googlegroups.com> References: <50aadbe6$0$29983$c3e8da3$5496439d@news.astraweb.com> <50aadcf0$0$29983$c3e8da3$5496439d@news.astraweb.com> <2930dc36-9d8f-411d-aa7a-272fe4885ec5@i5g2000yqh.googlegroups.com> <11835b30-3448-45cc-9ae5-46d651f93429@googlegroups.com> Message-ID: On Wed, Nov 28, 2012 at 12:39 PM, Kevin T wrote: > with other languages i always expand tabs to spaces. the vi plugin does do this properly. if i change all indents to be spaces only will python behave? i inherited a good deal of the code that i am using, which is tab based. Yes, it's best to use either tabs-only or spaces-only. Quoting from PEP 8 on the subject: Never mix tabs and spaces. > > The most popular way of indenting Python is with spaces only. The > second-most popular way is with tabs only. Code indented with a mixture of > tabs and spaces should be converted to using spaces exclusively. When > invoking the Python command line interpreter with the -t option, it issues > warnings about code that illegally mixes tabs and spaces. When using -tt > these warnings become errors. These options are highly recommended! > > For new projects, spaces-only are strongly recommended over tabs. Most > editors have features that make this easy to do. > I thought the prohibition against mixing tabs and spaces was made more strict in Python 3, but I can't find any reference to that now. Probably I was mistaken. -------------- next part -------------- An HTML attachment was scrubbed... URL: From driscoll at cs.wisc.edu Wed Nov 28 15:08:15 2012 From: driscoll at cs.wisc.edu (Evan Driscoll) Date: Wed, 28 Nov 2012 14:08:15 -0600 Subject: re.search when used within an if/else fails In-Reply-To: References: <50aadbe6$0$29983$c3e8da3$5496439d@news.astraweb.com> <50aadcf0$0$29983$c3e8da3$5496439d@news.astraweb.com> <2930dc36-9d8f-411d-aa7a-272fe4885ec5@i5g2000yqh.googlegroups.com> <11835b30-3448-45cc-9ae5-46d651f93429@googlegroups.com> Message-ID: <50B66F2F.8000209@cs.wisc.edu> On 11/28/2012 01:57 PM, Ian Kelly wrote: > Yes, it's best to use either tabs-only or spaces-only. Quoting from PEP > 8 on the subject: > > Never mix tabs and spaces. > > The most popular way of indenting Python is with spaces only. The > second-most popular way is with tabs only. Code indented with a > mixture of tabs and spaces should be converted to using spaces > exclusively. When invoking the Python command line interpreter with > the -t option, it issues warnings about code that illegally mixes > tabs and spaces. When using -tt these warnings become errors. These > options are highly recommended! > > For new projects, spaces-only are strongly recommended over tabs. > Most editors have features that make this easy to do. > > > I thought the prohibition against mixing tabs and spaces was made more > strict in Python 3, but I can't find any reference to that now. > Probably I was mistaken. I'm only entering this thread now so I'm not really in context, but you're correct; in Python 3, -tt is set by default, which makes illegal mixing an error. However, not all mixing is illegal: what it means by "code that illegally mixes tabs and spaces" is "code whose interpretation differs depending upon the interpretation of the tab width". While I'm not going to go test it, I think that if you, say, indent from the first to the second level with tabs (consistently), indent from the second to third level with spaces (consistently), and indent from the third to fourth level with tabs (consistently), it should not complain. Or perhaps I should say "it should complain that you're a bad person and should feel bad, but it won't." :-) (In fact, you could indent one block at the second level with tabs and another with spaces.) Evan -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 554 bytes Desc: OpenPGP digital signature URL: From steve+comp.lang.python at pearwood.info Wed Nov 28 15:50:07 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 28 Nov 2012 20:50:07 GMT Subject: re.search when used within an if/else fails References: <50aadbe6$0$29983$c3e8da3$5496439d@news.astraweb.com> <50aadcf0$0$29983$c3e8da3$5496439d@news.astraweb.com> <2930dc36-9d8f-411d-aa7a-272fe4885ec5@i5g2000yqh.googlegroups.com> Message-ID: <50b678fe$0$29994$c3e8da3$5496439d@news.astraweb.com> On Wed, 28 Nov 2012 11:39:48 -0800, Kevin T wrote: > with other languages i always expand tabs to spaces. the vi plugin does > do this properly. if i change all indents to be spaces only will python > behave? i inherited a good deal of the code that i am using, which is > tab based. Python will behave correctly if you use all spaces, or all tabs, for indents. If you mix spaces and tabs, anything can happen. For this reason, Python 3 is more strict and will raise an explicit error when it detects mixed spaces/tabs in an indent. See also the tabnanny tool provided with Python: [steve at ando ~]$ python -m tabnanny Usage: /usr/local/lib/python2.7/tabnanny.py [-v] file_or_directory ... -- Steven From goon12 at gmail.com Wed Nov 28 16:04:44 2012 From: goon12 at gmail.com (goon12) Date: Wed, 28 Nov 2012 13:04:44 -0800 (PST) Subject: Modules or Package for my application In-Reply-To: <1d039516-5507-48d3-bebf-70faab3cc6be@googlegroups.com> References: <1d039516-5507-48d3-bebf-70faab3cc6be@googlegroups.com> Message-ID: <00d4dd3d-b0b5-431d-abb6-2866023c605f@googlegroups.com> On Tuesday, November 27, 2012 3:04:03 AM UTC-5, Stone wrote: > Dear developers, > > > > I am creating application (originally written in perl) > > which will take care about replication from one system to the another system over command rsync. It will simulate High-availability solution. > > > > The application will contains main module called like ha.py > > and another modules will have names Replication.py, Trace.py, Misc.py, Procs.py, Constants.py, Lan.py, Display.py > > > > My questions are: > > 1) When I would like to make a global logging to the one file is it enough to do that like > > TRACE='/var/ha/log/sso_trace.log' > > logger = logging.getLogger('sso') > > hdrl = logging.FileHandler(TRACE) > > formatter = logging.Formatter('%{asctime}s %{levelname}s %{message}s') > > hdrl.setFormatter(formatter) > > logger.addHandler(hdrl) > > logger.setLevel(logging.WARNING) > > and make for them one module or how to solve that? > > How to call that logger from all modules / packages? > > Is it necessary to create alone module? > > > > 2) Is it package necessary or simply modules are enough > > - Replication.py will take care about alone replication > > - Procs.py will take care about some processes > > - Constants.py will take care about definition of global constants > > - Lan.py will take care about LAN definition > > - Dipslay.py will take care about showing screen (GUI or text_based) > > 3) How / where to define global variables? > > > > best regards > > Petr If you create the logger in your main module and this main module imports the other modules, the logger should be available, to the imported modules, by the name "sso". Now in the other modules, you get the logger using logger = logging.getLogger("sso"). In the end, I would probably put all these modules in a package and use a logging config file. Define a logger with the same name as your root package and in each module, within the package, just use "logger = logging.getLogger(__name__)" http://docs.python.org/2/howto/logging.html#logging-advanced-tutorial From grahn+nntp at snipabacken.se Wed Nov 28 16:11:07 2012 From: grahn+nntp at snipabacken.se (Jorgen Grahn) Date: 28 Nov 2012 21:11:07 GMT Subject: Imaging libraries in active development? References: <50B65486.8010306@gmail.com> Message-ID: On Wed, 2012-11-28, Christian Heimes wrote: > Am 28.11.2012 19:14, schrieb Michael Torrie: >> I'm curious. What features do you need that pil doesn't have? Other >> than updating pil to fix bugs, support new image types or new versions >> of Python, what kind of active development do you think it needs to >> have? Maybe pil has all the features the original author wanted and is >> pretty stable. To judge a package on how fast it's changing seems a bit >> odd to me. Not to me -- the slower the change, the better! >> Obviously you want to know that bugs can get fixed of >> course. Perhaps none have been reported recently. > > PIL is missing a bunch of features like proper TIFF support (no > multipage, g3/g4 compression and more), JPEG 2000, I thought those formats were dead since about a decade? (Ok, I know TIFF has niches, but JPEG 2000?) > RAW and HDR image > formats, tone mapping, proper ICC support, PEP 3128 buffer support ... I won't comment on those, but they seem likely to be valid complaints. > PIL is also rather slow. My smc.freeimage library can write JPEGs about > six times faster, because it uses libjpeg-turbo. Only some Linux > distributions have replaced libjpeg with the turbo implementation. That seems like an argument for *not* having support for many file formats in the imaging library itself -- just pipeline into the best standalone utilities available. /Jorgen -- // Jorgen Grahn O o . From grahn+nntp at snipabacken.se Wed Nov 28 16:12:54 2012 From: grahn+nntp at snipabacken.se (Jorgen Grahn) Date: 28 Nov 2012 21:12:54 GMT Subject: please help me to debud my local chat network program References: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> Message-ID: On Wed, 2012-11-28, Chris Angelico wrote: > On Wed, Nov 28, 2012 at 1:50 PM, Minh Dang wrote: >> Hello everybody, i am doing my project: local network chat using python >> here is my file >> http://www.mediafire.com/?cc2g9tmsju0ba2m > > Hmm. Might I recommend some other means of sharing your code? The > unrar-free utility from the Debian repo won't extract more than the > first file (accounts.txt), and I don't know if that's a problem with > unrar-free or your file. A better-known format like zip or tar.gz > would make things easier. Or a Git repository at github.com or similar. /Jorgen -- // Jorgen Grahn O o . From rakkitha263 at gmail.com Wed Nov 28 16:34:41 2012 From: rakkitha263 at gmail.com (Ricky) Date: Wed, 28 Nov 2012 13:34:41 -0800 (PST) Subject: Exponential arrival distribution in Python Message-ID: Hi all, I am doing a project on traffic simulation. I want to introduce exponential arrival distribution to precede this task. Therefore I want write a code in python for exponential arrival distribution. I am very new for programming and if anybody can help me on this that would be great. Cheers, Ricky From steve+comp.lang.python at pearwood.info Wed Nov 28 16:39:03 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 28 Nov 2012 21:39:03 GMT Subject: re.search when used within an if/else fails References: <50aadbe6$0$29983$c3e8da3$5496439d@news.astraweb.com> <50aadcf0$0$29983$c3e8da3$5496439d@news.astraweb.com> <2930dc36-9d8f-411d-aa7a-272fe4885ec5@i5g2000yqh.googlegroups.com> <11835b30-3448-45cc-9ae5-46d651f93429@googlegroups.com> Message-ID: <50b68476$0$29994$c3e8da3$5496439d@news.astraweb.com> On Wed, 28 Nov 2012 14:08:15 -0600, Evan Driscoll wrote: > I'm only entering this thread now so I'm not really in context, but > you're correct; in Python 3, -tt is set by default, which makes illegal > mixing an error. > > However, not all mixing is illegal: what it means by "code that > illegally mixes tabs and spaces" is "code whose interpretation differs > depending upon the interpretation of the tab width". While I'm not going > to go test it, I think that if you, say, indent from the first to the > second level with tabs (consistently), indent from the second to third > level with spaces (consistently), and indent from the third to fourth > level with tabs (consistently), it should not complain. Correct, which disappoints me. Testing with Python 3: py> if True: ... if True: # tab ... pass # tab, then four spaces ... py> I would prefer that the "pass" line would fail with an illegal indent, but it does not. But at least the following fails cleanly: py> if True: ... if True: # tab ... pass # tab, then four spaces ... pass # two spaces, tab, four spaces File "", line 4 pass # two spaces, tab, four spaces ^ TabError: inconsistent use of tabs and spaces in indentation > Or perhaps I > should say "it should complain that you're a bad person and should feel > bad, but it won't." :-) (In fact, you could indent one block at the > second level with tabs and another with spaces.) I don't mind different blocks using different indentation. You have always been able to do this: py> if True: ... pass # eight spaces ... py> if True: ... pass # two spaces ... py> If you don't like that, don't do it! Consistent indentation globally per file is a matter for coding conventions. However, I think that within a single block, Python should enforce "all tabs" or "all spaces". Perhaps it would be nice if Python honoured a directive setting indent style to spaces or indents, as it honours source code encoding lines: # -*- indent: -*- Where could be one of: space[s] Only accept spaces in indentation tab[s] Only accept tabs in indentation mixed Accept "mixed" tabs and spaces, but only if consistent with mixed the default for backward compatibility. -- Steven From ian.g.kelly at gmail.com Wed Nov 28 17:25:38 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 28 Nov 2012 15:25:38 -0700 Subject: Exponential arrival distribution in Python In-Reply-To: References: Message-ID: On Wed, Nov 28, 2012 at 2:34 PM, Ricky wrote: > > Hi all, > > I am doing a project on traffic simulation. I want to introduce > exponential arrival distribution to precede this task. Therefore I want > write a code in python for exponential arrival distribution. I am very new > for programming and if anybody can help me on this that would be great. > Are you looking for a PRNG using the the exponential distribution? The random module built into the standard library already implements this; see: http://docs.python.org/3/library/random.html#random.expovariate -------------- next part -------------- An HTML attachment was scrubbed... URL: From dwightdhutto at gmail.com Wed Nov 28 17:52:24 2012 From: dwightdhutto at gmail.com (David Hutto) Date: Wed, 28 Nov 2012 17:52:24 -0500 Subject: Exponential arrival distribution in Python In-Reply-To: References: Message-ID: >> I am doing a project on traffic simulation. I want to introduce >> exponential arrival distribution to precede this task. Therefore I want >> write a code in python for exponential arrival distribution. I am very new >> for programming and if anybody can help me on this that would be great. First, I would suggest that you give yourself a GUI like tkinter, wxpython to visualize the data. An average of the sequential(averaged per 60 second intervals) traveled by drivers on the road, and at what times. Then, and I don't know why, but it seems you want a logarithmic, scale where: for i in range(0,4) print "%i = %i" % (i * 10 **i) -- Best Regards, David Hutto CEO: http://www.hitwebdevelopment.com From dwightdhutto at gmail.com Wed Nov 28 17:55:06 2012 From: dwightdhutto at gmail.com (David Hutto) Date: Wed, 28 Nov 2012 17:55:06 -0500 Subject: Exponential arrival distribution in Python In-Reply-To: References: Message-ID: I mean this one: for i in range(0,4): print "%i = %i" % (i * 10 **i) -- Best Regards, David Hutto CEO: http://www.hitwebdevelopment.com From rosuav at gmail.com Wed Nov 28 18:04:55 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 29 Nov 2012 10:04:55 +1100 Subject: Tabs/spaces for indentation (was Re: re.search when used within an if/else fails) Message-ID: On Thu, Nov 29, 2012 at 8:39 AM, Steven D'Aprano wrote: > Perhaps it would be nice if Python honoured a directive setting indent > style to spaces or indents, as it honours source code encoding lines: > > # -*- indent: -*- > > Where could be one of: > > space[s] Only accept spaces in indentation > tab[s] Only accept tabs in indentation > mixed Accept "mixed" tabs and spaces, but only if consistent > > with mixed the default for backward compatibility. I don't know that it needs to be a declaration like that; character encodings are critical to parsing the file, but newline-followed-by-tab and newline-followed-by-space are unambiguous. But it would be of value to have something like that, as editors could then be configured to respect it - set the editor to turn tab-key into N spaces but only if "indent tab" is not set, for instance. The question is, is it worth it? The main value would be when you're editing someone else's code. ChrisA From no.email at nospam.invalid Wed Nov 28 18:25:59 2012 From: no.email at nospam.invalid (Paul Rubin) Date: Wed, 28 Nov 2012 15:25:59 -0800 Subject: Exponential arrival distribution in Python References: Message-ID: <7xd2yxmhrs.fsf@ruckus.brouhaha.com> Ricky writes: > I am doing a project on traffic simulation. I want to introduce > exponential arrival distribution to precede this task. Therefore I > want write a code in python for exponential arrival distribution. I've never heard of an "exponential arrival distribution" and googling fails. Do you mean an exponential distribution, that describes the expected arrival times in a Poisson process? Python's "random" module has the random.expovariate function for that: http://docs.python.org/3/library/random.html If you want an actual (discrete) Poisson distribution, see: http://en.wikipedia.org/wiki/Poisson_distribution#Generating_Poisson-distributed_random_variables From alberthuang314 at gmail.com Wed Nov 28 18:29:16 2012 From: alberthuang314 at gmail.com (Albert Chun-Chieh Huang) Date: Thu, 29 Nov 2012 07:29:16 +0800 Subject: Exponential arrival distribution in Python In-Reply-To: (Ricky's message of "Wed, 28 Nov 2012 13:34:41 -0800 (PST)") References: Message-ID: Hi, Ricky, Traffic simulation sounds like a good topic to use SimPy, which is a discrete-event simulation library in Python, c.f. http://simpy.sourceforge.net Professor Norm Matloff wrote a very good tutorial on SimPy, and you can download it here: http://heather.cs.ucdavis.edu/~matloff/simcourse.html Also, I've presented this SimPy package before, my slides might be helpful, http://alberthuang314.blogspot.tw/2012/01/simpy-slides-in-our-company-and-pyhug_26.html In short, SimPy is a process-oriented discrete-event simulation package, which will be easier to maintain than event-oriented discrete-event simulation. It also contains some tools to help us to visualize simulation data. It takes some time to study it, but if you would like to do some simulation projects, it will definitely be a good investment! Have fun with simulation! with Regards, Albert Huang Ricky writes: > Hi all, > > I am doing a project on traffic simulation. I want to introduce exponential arrival distribution to precede this task. Therefore I want write a code in python for exponential arrival distribution. I am very new for programming and if anybody can help me on this that would be great. > > Cheers, > Ricky From ian.g.kelly at gmail.com Wed Nov 28 19:36:35 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 28 Nov 2012 17:36:35 -0700 Subject: re.search when used within an if/else fails In-Reply-To: References: <2930dc36-9d8f-411d-aa7a-272fe4885ec5@i5g2000yqh.googlegroups.com> <11835b30-3448-45cc-9ae5-46d651f93429@googlegroups.com> <50b68476$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Wed, Nov 28, 2012 at 5:20 PM, Dennis Lee Bieber wrote: > On 28 Nov 2012 21:39:03 GMT, Steven D'Aprano > declaimed the following in > gmane.comp.python.general:> py> if True: > > ... if True: # tab > > ... pass # tab, then four spaces > > ... pass # two spaces, tab, four spaces > > File "", line 4 > > pass # two spaces, tab, four spaces > > ^ > > TabError: inconsistent use of tabs and spaces in indentation > > > > Unless there has been a major change in the parser... (I still > don't > have Python 3.x installed) > > I believe is expanded to 8-spaces -- NOT TO NEXT MULTIPLE OF > 8... > Next multiple of 8 is correct, according to the docs: Tabs are replaced (from left to right) by one to eight spaces such that the > total number of characters up to and including the replacement is a > multiple of eight (this is intended to be the same rule as used by Unix). > The total number of spaces preceding the first non-blank character then > determines the line?s indentation. Indentation cannot be split over > multiple physical lines using backslashes; the whitespace up to the first > backslash determines the indentation. > http://docs.python.org/3/reference/lexical_analysis.html#indentation Testing Steven's formulation with Python 2, I find that it runs without comment without the -tt option, but it raises the TabError with the -tt option. -------------- next part -------------- An HTML attachment was scrubbed... URL: From dangbaminh.it at gmail.com Wed Nov 28 21:48:27 2012 From: dangbaminh.it at gmail.com (Minh Dang) Date: Wed, 28 Nov 2012 18:48:27 -0800 (PST) Subject: please help me to debud my local chat network program In-Reply-To: References: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> Message-ID: ok, here is my code, zip http://www.mediafire.com/?ob4kokda81fj6xc From roy at panix.com Wed Nov 28 22:13:57 2012 From: roy at panix.com (Roy Smith) Date: Wed, 28 Nov 2012 22:13:57 -0500 Subject: Weird import failure with "nosetests --processes=1" Message-ID: I've got a minimal test script: ----------------------------- $ cat test_foo.py import pyza.models print pyza.models def test_foo(): pass ----------------------------- pyza.models is a package. Under normal conditions, I can import it fine: $ python Python 2.7.3 (default, Aug 1 2012, 05:14:39) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import pyza.models >>> print pyza.models >>> But when I run nosetests in parallel mode, the import fails in a way which has me baffled. What's going on here? $ nosetests --processes=1 -s test_foo:test_foo E ====================================================================== ERROR: Failure: AttributeError ('module' object has no attribute 'models') ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/roy/production/python/local/lib/python2.7/site-packages/nose/loade r.py", line 390, in loadTestsFromName addr.filename, addr.module) File "/home/roy/production/python/local/lib/python2.7/site-packages/nose/impor ter.py", line 39, in importFromPath return self.importFromDir(dir_path, fqname) File "/home/roy/production/python/local/lib/python2.7/site-packages/nose/impor ter.py", line 86, in importFromDir mod = load_module(part_fqname, fh, filename, desc) File "/home/roy/songza/pyza/djapi/test_foo.py", line 2, in print pyza.models AttributeError: 'module' object has no attribute 'models' ---------------------------------------------------------------------- Ran 1 test in 0.107s FAILED (errors=1) From timr at probo.com Wed Nov 28 23:14:08 2012 From: timr at probo.com (Tim Roberts) Date: Wed, 28 Nov 2012 20:14:08 -0800 Subject: os.popen and the subprocess module References: Message-ID: Andrew wrote: > >I'm working on a script that will run an executable obtaine the output > from the executable >and do some analysis on the output. Essentially the script runs the >executable analyses >the data. >I'm looking into os.popen and the subprocess module, implementing os.popen >is easy but i hear >it is depreciating however I'm finding the implemantation of subprocess >daunting can anyone help One of my favorite things about the subprocess module is that the introductory comments have examples of how to use subprocess to replace almost every use case for os.system and os.popen. -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From amca01 at gmail.com Thu Nov 29 01:53:32 2012 From: amca01 at gmail.com (Alasdair McAndrew) Date: Wed, 28 Nov 2012 22:53:32 -0800 (PST) Subject: Imaging libraries in active development? In-Reply-To: References: Message-ID: I take your point that not being actively developed doesn't necessarily mean that the software is bad - but in general healthy software is continuously updated and expanded to meet the needs of its users, or to take advantage of new algorithms or hardware. And in its current form PIL has a number of limitations: it doesn't allow linear filters of arbitrary size or shape, or non-linear filters (such as median filter) of arbitrary size. There doesn't seem to be built in support for standard imaging filters: Gaussian, Laplacian, LoG, edge detection, unsharp masking and so on. It doesn't seem to have support for color space conversions (RGB, YIQ, HSV etc). There don't seem to be standard edge detection routines (Laplacian of Gaussian, Canny, etc). And so on. Now maybe some of these can be found in other Python libraries, but I can't imagine I'm the only person who would ever want them in an imaging library. Other libraries (scipy.ndimage, scikits-image) do go a long way to addressing my concerns. Anyway, I was curious to know why PIL is lacking so much of what I would consider fairly fundamental imaging facilities, and why development seems to have stalled since 2009. On Thursday, 29 November 2012 05:14:30 UTC+11, Michael Torrie wrote: > On 11/28/2012 05:30 AM, Alasdair McAndrew wrote: > > > I'm investigating Python for image processing (having used Matlab, > > > then Octave for some years). And I'm spoiled for choice: PIL and its > > > fork pillow, scipy.ndimage, scikits-image, mahotas, the Python > > > interface to openCV... > > > > > > However, PIL doesn't seem to be in active development. What I want > > > to know is - what are the current "standard" libraries for image > > > processing in Python which are in active development? > > > > > > I have quite a few image processing student notes which I'm thinking > > > of converting to Python, but I'd like to use the most up-to-date > > > library. > > > > I'm curious. What features do you need that pil doesn't have? Other > > than updating pil to fix bugs, support new image types or new versions > > of Python, what kind of active development do you think it needs to > > have? Maybe pil has all the features the original author wanted and is > > pretty stable. To judge a package on how fast it's changing seems a bit > > odd to me. Obviously you want to know that bugs can get fixed of > > course. Perhaps none have been reported recently. From amca01 at gmail.com Thu Nov 29 01:53:32 2012 From: amca01 at gmail.com (Alasdair McAndrew) Date: Wed, 28 Nov 2012 22:53:32 -0800 (PST) Subject: Imaging libraries in active development? In-Reply-To: References: Message-ID: I take your point that not being actively developed doesn't necessarily mean that the software is bad - but in general healthy software is continuously updated and expanded to meet the needs of its users, or to take advantage of new algorithms or hardware. And in its current form PIL has a number of limitations: it doesn't allow linear filters of arbitrary size or shape, or non-linear filters (such as median filter) of arbitrary size. There doesn't seem to be built in support for standard imaging filters: Gaussian, Laplacian, LoG, edge detection, unsharp masking and so on. It doesn't seem to have support for color space conversions (RGB, YIQ, HSV etc). There don't seem to be standard edge detection routines (Laplacian of Gaussian, Canny, etc). And so on. Now maybe some of these can be found in other Python libraries, but I can't imagine I'm the only person who would ever want them in an imaging library. Other libraries (scipy.ndimage, scikits-image) do go a long way to addressing my concerns. Anyway, I was curious to know why PIL is lacking so much of what I would consider fairly fundamental imaging facilities, and why development seems to have stalled since 2009. On Thursday, 29 November 2012 05:14:30 UTC+11, Michael Torrie wrote: > On 11/28/2012 05:30 AM, Alasdair McAndrew wrote: > > > I'm investigating Python for image processing (having used Matlab, > > > then Octave for some years). And I'm spoiled for choice: PIL and its > > > fork pillow, scipy.ndimage, scikits-image, mahotas, the Python > > > interface to openCV... > > > > > > However, PIL doesn't seem to be in active development. What I want > > > to know is - what are the current "standard" libraries for image > > > processing in Python which are in active development? > > > > > > I have quite a few image processing student notes which I'm thinking > > > of converting to Python, but I'd like to use the most up-to-date > > > library. > > > > I'm curious. What features do you need that pil doesn't have? Other > > than updating pil to fix bugs, support new image types or new versions > > of Python, what kind of active development do you think it needs to > > have? Maybe pil has all the features the original author wanted and is > > pretty stable. To judge a package on how fast it's changing seems a bit > > odd to me. Obviously you want to know that bugs can get fixed of > > course. Perhaps none have been reported recently. From dieter at handshake.de Thu Nov 29 02:01:58 2012 From: dieter at handshake.de (Dieter Maurer) Date: Thu, 29 Nov 2012 08:01:58 +0100 Subject: deepcopy questions References: <39edd363-2937-48a8-b84d-54d3993e42cf@googlegroups.com> Message-ID: <87vccoq4d5.fsf@handshake.de> lars van gemerden writes: > ... "deepcopy" dropping some items ... > Any ideas are still more then welcome, "deepcopy" is implemented in Python (rather than "C"). Thus, if necessary, you can debug what it is doing and thereby determine where the items have been dropped. From christian at python.org Thu Nov 29 03:37:28 2012 From: christian at python.org (Christian Heimes) Date: Thu, 29 Nov 2012 09:37:28 +0100 Subject: Imaging libraries in active development? In-Reply-To: References: <50B65486.8010306@gmail.com> Message-ID: Am 28.11.2012 22:11, schrieb Jorgen Grahn: > I thought those formats were dead since about a decade? (Ok, I know > TIFF has niches, but JPEG 2000?) Baseline TIFF is still used a lot when a lossless image format is required. It's widely used for scientific stuff, long-time preservation, health care (e.g. MRI) and for many more applications. If you need to deal with formats like 32bit float RGBA or 128bit complex float pixels or color spaces like CMYK, CIELUV, CIEXYZ, then TIFF is your man. I'm sitting on nearly a quarter petabyte of TIFF images. The data should still be usable in 200 years. Bit rot *is* a serious issue for long periods of time. > That seems like an argument for *not* having support for many file > formats in the imaging library itself -- just pipeline into the best > standalone utilities available. An imaging library shouldn't implement all file formats on its own and rather use existing libraries. That's what I'm doing with smc.freeimage. It wraps and ties together FreeImage and lcms. FreeImage itself contains and encapsulates eight libraries (e.g. libjpeg, libtiff4, libraw, libpng ...) in a powerful API. Christian From zoom at yahoo.com Thu Nov 29 04:01:36 2012 From: zoom at yahoo.com (zoom) Date: Thu, 29 Nov 2012 10:01:36 +0100 Subject: Imaging library In-Reply-To: References: <50B65486.8010306@gmail.com> Message-ID: C'mon guys, don't be so picky. The point is that that he cannot find python library that can easily create HDR image or process RAW images (or some other image format). Also, AFAIK there is no built in support for standard imaging filters, color space conversion, etc (as Alasdair also mentioned). One can do this with scipy, and this is how I do it. But I'm also interested if there is some library that implements any of those. IMHO it would be useful if one could code the same effects easily as clicking on the effect button in GIMP or Blender. This is interesting question, and if any of you have any ideas on how this can be achieved, please share your knowledge with us. P.S. We do not need to tutor people about whether a RAW format is a specific image format or not (http://en.wikipedia.org/wiki/Raw_image_format) - we understand the point of his question albeit it is not clearly stated. Assume good will - nobody is stating that PIL or scipy are bad, we simply ask whether there is something more out there. It would be more useful if we would provide information on how to do it, or connect him with someone who can do it. Or point where he can request such feature or publish his solution. The policy not to implement every format under the sky is a legal one, but by implementing it one-by-one - together we might even get there. From dangbaminh.it at gmail.com Thu Nov 29 04:03:35 2012 From: dangbaminh.it at gmail.com (Minh Dang) Date: Thu, 29 Nov 2012 01:03:35 -0800 (PST) Subject: please help me to debud my local chat network program In-Reply-To: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> References: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> Message-ID: can anyone help me? From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Thu Nov 29 04:09:44 2012 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Thu, 29 Nov 2012 10:09:44 +0100 Subject: os.popen and the subprocess module In-Reply-To: References: Message-ID: Am 27.11.2012 19:00 schrieb Andrew: > I'm looking into os.popen and the subprocess module, implementing > os.popen is easy but i hear it is depreciating however I'm finding the > implemantation of subprocess daunting can anyone help This is only the first impression. subprocess is much more powerful, but you don't need to use the full power. For just executing and reading the data, you do not need much. First step: create your object and performing the call: sp = subprocess.Popen(['program', 'arg1', 'arg2'], stdout=subprocess.PIPE) or sp = subprocess.Popen('program arg1 arg2', shell=True, stdout=subprocess.PIPE) The variant with shell=True is more os.popen()-like, but has security flaws (e.g., what happens if there are spaces or, even worse, ";"s in the command string? Second step: Obtain output. Here you either can do stdout, stderr = sp.communicate() can be used if the whole output fits into memory at once or you really have to deal with stderr or stdin additionally. In other, simpler cases, it is possible to read from sp.stdout like from a file (with a for loop, with .read() or whatever you like). Third step: Getting the status, terminating the process. And if you have read the whole output, you do status = sp.wait() in order not to have a zombie process in your process table and to obtain the process status. Thomas From rosuav at gmail.com Thu Nov 29 04:13:30 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 29 Nov 2012 20:13:30 +1100 Subject: please help me to debud my local chat network program In-Reply-To: References: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> Message-ID: On Thu, Nov 29, 2012 at 8:03 PM, Minh Dang wrote: > can anyone help me? Look over my previous posts. I've made several suggestions that you haven't followed up on. Also, check out ESR's article on asking questions, which I also linked you to earlier. Take its advice. You'll help yourself, AND it'll be easier for us to help you. ChrisA From adnothing at gmail.com Thu Nov 29 04:29:22 2012 From: adnothing at gmail.com (Adrien) Date: Thu, 29 Nov 2012 10:29:22 +0100 Subject: Imaging libraries in active development? In-Reply-To: References: Message-ID: <50B72AF2.4050401@gmail.com> Hey Alasdair, I believe OpenCV might do the trick for you: - it contains everything you seem to need (+ much much more); - it is efficient; - it is cross-platform; - it has a usable python interface since version 2.4; - it is not going away any time soon and is constantly improved; - it has an active user base. But (there is always a but), it also has some issues: - (the main one for me) documentation is often incomplete or even sometimes cryptic: the website (http://docs.opencv.org/) is great, but, IIRC, the docstrings are automatically generated from the C++ prototypes using Boost.Python; some trial & error is often necessary to find out what the parameters of a function should be; - it may be overkill if you just want to do some basic image processing (maybe scikits-image is a better choice there?). Hope this helps, Adrien Le 29/11/2012 07:53, Alasdair McAndrew a ?crit : > I take your point that not being actively developed doesn't necessarily mean that the software is bad - but in general healthy software is continuously updated and expanded to meet the needs of its users, or to take advantage of new algorithms or hardware. > > And in its current form PIL has a number of limitations: it doesn't allow linear filters of arbitrary size or shape, or non-linear filters (such as median filter) of arbitrary size. There doesn't seem to be built in support for standard imaging filters: Gaussian, Laplacian, LoG, edge detection, unsharp masking and so on. It doesn't seem to have support for color space conversions (RGB, YIQ, HSV etc). There don't seem to be standard edge detection routines (Laplacian of Gaussian, Canny, etc). And so on. Now maybe some of these can be found in other Python libraries, but I can't imagine I'm the only person who would ever want them in an imaging library. Other libraries (scipy.ndimage, scikits-image) do go a long way to addressing my concerns. > > Anyway, I was curious to know why PIL is lacking so much of what I would consider fairly fundamental imaging facilities, and why development seems to have stalled since 2009. > > On Thursday, 29 November 2012 05:14:30 UTC+11, Michael Torrie wrote: >> On 11/28/2012 05:30 AM, Alasdair McAndrew wrote: >> >>> I'm investigating Python for image processing (having used Matlab, >>> then Octave for some years). And I'm spoiled for choice: PIL and its >>> fork pillow, scipy.ndimage, scikits-image, mahotas, the Python >>> interface to openCV... >>> However, PIL doesn't seem to be in active development. What I want >>> to know is - what are the current "standard" libraries for image >>> processing in Python which are in active development? >>> I have quite a few image processing student notes which I'm thinking >>> of converting to Python, but I'd like to use the most up-to-date >>> library. >> >> >> I'm curious. What features do you need that pil doesn't have? Other >> >> than updating pil to fix bugs, support new image types or new versions >> >> of Python, what kind of active development do you think it needs to >> >> have? Maybe pil has all the features the original author wanted and is >> >> pretty stable. To judge a package on how fast it's changing seems a bit >> >> odd to me. Obviously you want to know that bugs can get fixed of >> >> course. Perhaps none have been reported recently. From duncan.booth at invalid.invalid Thu Nov 29 04:34:14 2012 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 29 Nov 2012 09:34:14 GMT Subject: re.search when used within an if/else fails References: <2930dc36-9d8f-411d-aa7a-272fe4885ec5@i5g2000yqh.googlegroups.com> <11835b30-3448-45cc-9ae5-46d651f93429@googlegroups.com> <50b68476$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: Dennis Lee Bieber wrote: > Unless there has been a major change in the parser... (I still don't > have Python 3.x installed) > > I believe is expanded to 8-spaces -- NOT TO NEXT MULTIPLE OF > 8... > Certainly in Python 2.7 that's not the case: the tab expands to the next multiple of 8 spaces. >>> if 1: ... print "yes" # space + tab ... print "no" # eight spaces ... yes no If tab expanded to exactly 8 spaces the leading space would have forced an indentation error, but it didn't. -- Duncan Booth http://kupuguy.blogspot.com From tolidtm at gmail.com Thu Nov 29 05:22:28 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Thu, 29 Nov 2012 11:22:28 +0100 Subject: Compare list entry from csv files In-Reply-To: References: <50B3E865.9070700@davea.name> <50B43246.2010902@davea.name> Message-ID: On Tue, Nov 27, 2012 at 9:41 PM, Neil Cerutti wrote: > On 2012-11-27, Anatoli Hristov wrote: >> Thank you all for the help, but I figured that out and the >> program now works perfect. I would appreciate if you have some >> notes about my script as I'm noob :) Here is the code: >> >> import csv >> >> origf = open('c:/Working/Test_phonebook.csv', 'rt') >> secfile = open('c:/Working/phones.csv', 'rt') > > csv module expects files to be opened in binary mode in Python > versions less than version 3.0. For Python versions >= 3.0, you > use the special keyword argument, newlines='', instead. > >> phonelist = [] >> namelist = [] > > The structure of your program is poor. It's workable for such a > short script, and sometimes my first cuts are similar, but it's > better to get out of the habit right away. > > Once you get this working the way you'd like you should clean up > the structure as a service to your future self. > >> names = csv.reader(origf, delimiter=';') >> phones = csv.reader(secfile, delimiter=';') > > You csv files don't seem to have header rows, but even so you can > improve your code by providing fieldnames and using a DictReader > instead. > > name_reader = csv.DictReader(origf, fieldnames=[ > 'Name', 'Blah', 'Phone#']) > > Then you can read from records with > > name = row['Name'] > > instead of using bare, undocumented integers. > >> for tel in phones: >> phonelist.append(tel) >> >> def finder(name_row,rows): >> for ex_phone in phonelist: >> telstr = ex_phone[0].lower() >> if telstr.find(name_row) >= 0: > > This strikes me as a crude way to match names. You don't really > want Donald to match perfectly with McDonald, do you? Or for > Smith to match with Smithfield? > > Yes, a human being will clean it up, but your program can do a > better job. > >> print "\nName found: %s" % name_row >> namelist[rows][-1] = ex_phone[-1].lower() >> else: >> pass >> return >> >> def name_find(): >> rows = 0 >> for row in names: >> namelist.append(row) >> name_row = row[0].lower() >> finder(name_row,rows) >> rows = rows+1 > > You can use the useful enumerate function instead of your own > counter. > > for rows, row in enumerate(names): > > ...though I would find 'rownum' or 'num' or just 'i' better than > the name 'rows', which I find confusing. > >> name_find() >> ofile = open('c:/Working/ttest.csv', "wb") >> writer = csv.writer(wfile, delimiter=';') >> for insert in namelist: >> writer.writerow(insert) >> wfile.close() > > -- > Neil Cerutti > -- > http://mail.python.org/mailman/listinfo/python-list Hello, Tried to document a little bit the script, but I'm not that good in that too :) The only problem I have is that I cant compare other field than the first one in for ex_phone in phones: telstr = ex_phone[0].lower() When I use telstr = ex_phone[0].lower() it says out of range and the strange think is that the range is 6 I can't figure that out. So when I edit the csv I modify the look of the file and then I start the script and it works, but I wanted to use more than one condition and I can't :( import csv # Open the file with the names and addresses origf = open('c:/Working/vpharma.csv', 'rt') # Open the file with the phone numbers secfile = open('c:/Working/navori.csv', 'rt') # Creates the empty list with the names namelist = [] # Creates the empty list with the phone numbers PHONELIST = [] # Reads the file with the names # Format "Name","Phone" names = csv.reader(origf, delimiter=';') # Reads the file with the phone numbers # Format "First name","Lastname","Address","City","Country","Phone" phones = csv.reader(secfile, delimiter=';') # Creates a list with phone numbers #for tel in phones: # PHONELIST.append(tel) def finder(Compare_Name,rows): ''' Compare the names from the namelist with the names from the phonelist. If the name match - then the phone number is added to the specified field ''' for ex_phone in phones: telstr = ex_phone[0].lower() print telstr if telstr.find(Compare_Name) >= 0: print "\nName found: %s" % Compare_Name namelist[rows][-1] = ex_phone[-1].lower() else: print "Not found %s" % Compare_Name pass return def name_find(): rows = 0 for row in names: namelist.append(row) Compare_Name = row[1].lower() finder(Compare_Name,rows) rows = rows+1 if __name__ == '__main__': name_find() # Writes the list to a file wfile = open('c:/Working/ttest.csv', "wb") writer = csv.writer(wfile, delimiter=';') for insert in namelist: writer.writerow(insert) wfile.close() From nicoe at altern.org Thu Nov 29 05:46:50 2012 From: nicoe at altern.org (Nicolas =?utf-8?Q?=C3=89vrard?=) Date: Thu, 29 Nov 2012 11:46:50 +0100 Subject: Migrate from Access 2010 / VBA In-Reply-To: <20121125204816.97f486775f8b1e7e575a7b0a@gmx.net> References: <6db4ed18-a16e-4cd1-940b-c10f51207780@googlegroups.com> <20121125204816.97f486775f8b1e7e575a7b0a@gmx.net> Message-ID: <20121129104650.GC5545@balisto> * Wolfgang Keller [2012-11-25 20:48 +0100]: >> I am the lone developer of db apps at a company of 350+ employees. >> Everything is done in MS Access 2010 and VBA. I'm frustrated with the >> limitations of this platform and have been considering switching to >> Python. >> >> I've been experimenting with the language for a year or so, >> and feel comfortable with the basics. >> >> I am concerned that I'll have a hard time replacing the access form >> and report designers. I've worked a little with TKinter, but it's a >> far cry from the GUI designer in Access. > >The list of Python frameworks for rapid development of desktop >(i.e. non-Web) database applications currently contains: > >using PyQt (& Sqlalchemy): >Pypapi: www.pypapi.org >Camelot: www.python-camelot.com >Qtalchemy: www.qtalchemy.org > >using PyGTK: >Sqlkit: sqlkit.argolinux.org (also uses Sqlalchemy) >Kiwi: www.async.com.br/projects/kiwi > >using wxPython: >Dabo: www.dabodev.com >Defis: sourceforge.net/projects/defis (Russian only) >GNUe: www.gnuenterprise.org > >Pypapi, Camelot, Sqlkit and Dabo seem to be the most active and best >documented/supported ones. I'd like to add to the list Tryton http://www.tryton.org/ Which framework can be used to create a business application without even using the already existing modules (one of our customer uses only the 'party' modules in order to manage insurance products, the GNU Health (http://www.gnuhealth.org/) project uses more official modules to create their HIS software). Reporting is done through relatorio (http://relatorio.openhex.org/), which uses ODF templates to generate ODF reports (or other format thanks to unoconv) the client is written in GTk (we're writing one in JavaScript right now (and I miss python badly)). -- (?> Nicolas ?vrard ( ) Li?ge `? -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 190 bytes Desc: Digital signature URL: From amca01 at gmail.com Thu Nov 29 06:22:46 2012 From: amca01 at gmail.com (Alasdair McAndrew) Date: Thu, 29 Nov 2012 03:22:46 -0800 (PST) Subject: Imaging libraries in active development? In-Reply-To: References: Message-ID: <8346c832-10fd-4569-81d6-cc768b7cb6ab@googlegroups.com> Thanks for the heads-up about OpenCV. I have in fact briefly looked at OpenCV (well, the documentation), and it does seem remarkably complete. And what it doesn't provide, such as image transforms (FFT, DCT etc), are offered elsewhere by other Python libraries. Probably the combinations of OpenCV, Scipy.ndimage and scikits-image would cover pretty much all of my needs. Thanks, Alasdair > Hey Alasdair, > > > > I believe OpenCV might do the trick for you: > > - it contains everything you seem to need (+ much much more); > > - it is efficient; > > - it is cross-platform; > > - it has a usable python interface since version 2.4; > > - it is not going away any time soon and is constantly improved; > > - it has an active user base. > > > > But (there is always a but), it also has some issues: > > - (the main one for me) documentation is often incomplete or even > > sometimes cryptic: the website (http://docs.opencv.org/) is great, but, > > IIRC, the docstrings are automatically generated from the C++ prototypes > > using Boost.Python; some trial & error is often necessary to find out > > what the parameters of a function should be; > > - it may be overkill if you just want to do some basic image processing > > (maybe scikits-image is a better choice there?). > > > > Hope this helps, > > > > Adrien > > > > Le 29/11/2012 07:53, Alasdair McAndrew a ?crit : > > > I take your point that not being actively developed doesn't necessarily mean that the software is bad - but in general healthy software is continuously updated and expanded to meet the needs of its users, or to take advantage of new algorithms or hardware. > > > > > > And in its current form PIL has a number of limitations: it doesn't allow linear filters of arbitrary size or shape, or non-linear filters (such as median filter) of arbitrary size. There doesn't seem to be built in support for standard imaging filters: Gaussian, Laplacian, LoG, edge detection, unsharp masking and so on. It doesn't seem to have support for color space conversions (RGB, YIQ, HSV etc). There don't seem to be standard edge detection routines (Laplacian of Gaussian, Canny, etc). And so on. Now maybe some of these can be found in other Python libraries, but I can't imagine I'm the only person who would ever want them in an imaging library. Other libraries (scipy.ndimage, scikits-image) do go a long way to addressing my concerns. > > > > > > Anyway, I was curious to know why PIL is lacking so much of what I would consider fairly fundamental imaging facilities, and why development seems to have stalled since 2009. > > > > > > On Thursday, 29 November 2012 05:14:30 UTC+11, Michael Torrie wrote: > > >> On 11/28/2012 05:30 AM, Alasdair McAndrew wrote: > > >> > > >>> I'm investigating Python for image processing (having used Matlab, > > >>> then Octave for some years). And I'm spoiled for choice: PIL and its > > >>> fork pillow, scipy.ndimage, scikits-image, mahotas, the Python > > >>> interface to openCV... > > >>> However, PIL doesn't seem to be in active development. What I want > > >>> to know is - what are the current "standard" libraries for image > > >>> processing in Python which are in active development? > > >>> I have quite a few image processing student notes which I'm thinking > > >>> of converting to Python, but I'd like to use the most up-to-date > > >>> library. > > >> > > >> > > >> I'm curious. What features do you need that pil doesn't have? Other > > >> > > >> than updating pil to fix bugs, support new image types or new versions > > >> > > >> of Python, what kind of active development do you think it needs to > > >> > > >> have? Maybe pil has all the features the original author wanted and is > > >> > > >> pretty stable. To judge a package on how fast it's changing seems a bit > > >> > > >> odd to me. Obviously you want to know that bugs can get fixed of > > >> > > >> course. Perhaps none have been reported recently. From amca01 at gmail.com Thu Nov 29 06:22:46 2012 From: amca01 at gmail.com (Alasdair McAndrew) Date: Thu, 29 Nov 2012 03:22:46 -0800 (PST) Subject: Imaging libraries in active development? In-Reply-To: References: Message-ID: <8346c832-10fd-4569-81d6-cc768b7cb6ab@googlegroups.com> Thanks for the heads-up about OpenCV. I have in fact briefly looked at OpenCV (well, the documentation), and it does seem remarkably complete. And what it doesn't provide, such as image transforms (FFT, DCT etc), are offered elsewhere by other Python libraries. Probably the combinations of OpenCV, Scipy.ndimage and scikits-image would cover pretty much all of my needs. Thanks, Alasdair > Hey Alasdair, > > > > I believe OpenCV might do the trick for you: > > - it contains everything you seem to need (+ much much more); > > - it is efficient; > > - it is cross-platform; > > - it has a usable python interface since version 2.4; > > - it is not going away any time soon and is constantly improved; > > - it has an active user base. > > > > But (there is always a but), it also has some issues: > > - (the main one for me) documentation is often incomplete or even > > sometimes cryptic: the website (http://docs.opencv.org/) is great, but, > > IIRC, the docstrings are automatically generated from the C++ prototypes > > using Boost.Python; some trial & error is often necessary to find out > > what the parameters of a function should be; > > - it may be overkill if you just want to do some basic image processing > > (maybe scikits-image is a better choice there?). > > > > Hope this helps, > > > > Adrien > > > > Le 29/11/2012 07:53, Alasdair McAndrew a ?crit : > > > I take your point that not being actively developed doesn't necessarily mean that the software is bad - but in general healthy software is continuously updated and expanded to meet the needs of its users, or to take advantage of new algorithms or hardware. > > > > > > And in its current form PIL has a number of limitations: it doesn't allow linear filters of arbitrary size or shape, or non-linear filters (such as median filter) of arbitrary size. There doesn't seem to be built in support for standard imaging filters: Gaussian, Laplacian, LoG, edge detection, unsharp masking and so on. It doesn't seem to have support for color space conversions (RGB, YIQ, HSV etc). There don't seem to be standard edge detection routines (Laplacian of Gaussian, Canny, etc). And so on. Now maybe some of these can be found in other Python libraries, but I can't imagine I'm the only person who would ever want them in an imaging library. Other libraries (scipy.ndimage, scikits-image) do go a long way to addressing my concerns. > > > > > > Anyway, I was curious to know why PIL is lacking so much of what I would consider fairly fundamental imaging facilities, and why development seems to have stalled since 2009. > > > > > > On Thursday, 29 November 2012 05:14:30 UTC+11, Michael Torrie wrote: > > >> On 11/28/2012 05:30 AM, Alasdair McAndrew wrote: > > >> > > >>> I'm investigating Python for image processing (having used Matlab, > > >>> then Octave for some years). And I'm spoiled for choice: PIL and its > > >>> fork pillow, scipy.ndimage, scikits-image, mahotas, the Python > > >>> interface to openCV... > > >>> However, PIL doesn't seem to be in active development. What I want > > >>> to know is - what are the current "standard" libraries for image > > >>> processing in Python which are in active development? > > >>> I have quite a few image processing student notes which I'm thinking > > >>> of converting to Python, but I'd like to use the most up-to-date > > >>> library. > > >> > > >> > > >> I'm curious. What features do you need that pil doesn't have? Other > > >> > > >> than updating pil to fix bugs, support new image types or new versions > > >> > > >> of Python, what kind of active development do you think it needs to > > >> > > >> have? Maybe pil has all the features the original author wanted and is > > >> > > >> pretty stable. To judge a package on how fast it's changing seems a bit > > >> > > >> odd to me. Obviously you want to know that bugs can get fixed of > > >> > > >> course. Perhaps none have been reported recently. From michael.herrmann at getautoma.com Thu Nov 29 06:45:52 2012 From: michael.herrmann at getautoma.com (Michael Herrmann) Date: Thu, 29 Nov 2012 03:45:52 -0800 (PST) Subject: 10 sec poll - please reply! In-Reply-To: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> References: <3d71f175-164e-494c-a521-2eaa5679b524@googlegroups.com> Message-ID: <43b119c9-fdd0-4617-8bf6-69ed19351af7@googlegroups.com> Hey everyone, this is my final mail. With all your help we have decided on names for our function. It was a surprisingly difficult process but your inputs helped tremendously. I have described our experiences (very good ones here, but somewhat mixed ones with StackOverflow) in a blog entry: http://www.getautoma.com/blog/New-version-of-Automa-with-improved-API I will now stop spamming this list. Thank you so much again! If you're interested in how we're getting on in the future, do follow us on Twitter @BugFreeSoftware or on our blog http://www.getautoma.com/blog! Thanks again, Michael Co-founder and lead developer @BugFreeSoftware http://www.getautoma.com On Tuesday, November 20, 2012 1:18:38 PM UTC+1, Michael Herrmann wrote: > Hi, > > > > I'm developing a GUI Automation library (http://www.getautoma.com) and am having difficulty picking a name for the function that simulates key strokes. I currently have it as 'type' but that clashes with the built-in function. Example uses of 'type': > > > > type(ENTER) > > > > type("Hello World!") > > > > type(CTRL + 'a') > > > > What, in your view, would be the most intuitive alternative name? > > > > Here are my thoughts so far: I could call it 'press' but then our GUI automation tool also allows you to click things and then "press" might be mistaken for "pressing a button". A less ambiguous alternative is "type_keys" but that is rather long and doesn't read as well, for instance in type_keys(ENTER). > > > > Thank you very much! From michael.herrmann at getautoma.com Thu Nov 29 06:46:29 2012 From: michael.herrmann at getautoma.com (Michael Herrmann) Date: Thu, 29 Nov 2012 03:46:29 -0800 (PST) Subject: Is it bad style to override the built-in function `type`? In-Reply-To: References: Message-ID: <22490968-d528-41b0-b9ba-3ee375bd91de@googlegroups.com> Hey everyone, this is my final mail. With all your help we have decided on names for our function. It was a surprisingly difficult process but your inputs helped tremendously. I have described our experiences (very good ones here, but somewhat mixed ones with StackOverflow) in a blog entry: http://www.getautoma.com/blog/New-version-of-Automa-with-improved-API I will now stop spamming this list. Thank you so much again! If you're interested in how we're getting on in the future, do follow us on Twitter @BugFreeSoftware or on our blog http://www.getautoma.com/blog! Thanks again, Michael Co-founder and lead developer @BugFreeSoftware http://www.getautoma.com On Friday, November 23, 2012 5:12:39 PM UTC+1, Michael Herrmann wrote: > Hi, > > > > do you think it's bad style to override the built-in function `type`? I'm co-developing a GUI automation library called Automa (http://www.getautoma.com) and 'type' would be a very fitting name for a function that generates artificial key strokes. > > > > This post is motivated by an already lengthy discussion on this mailing list (http://bit.ly/10aOy4H), where we tried to find alternative names for `type`. Many were found, but none are quite as fitting as 'type'. > > > > For the sake of avoiding a discussion that is already being lead elsewhere please confine this thread to what you generally think about overriding `type`, and post suggestions for alternative names or solutions in the other thread. > > > > Thank you very much! > > Michael From thbach at students.uni-mainz.de Thu Nov 29 07:07:34 2012 From: thbach at students.uni-mainz.de (Thomas Bach) Date: Thu, 29 Nov 2012 13:07:34 +0100 Subject: Compare list entry from csv files In-Reply-To: References: <50B3E865.9070700@davea.name> <50B43246.2010902@davea.name> Message-ID: <20121129120734.GA3771@taris.box> Can you please cut the message you are responding to the relevant parts? On Thu, Nov 29, 2012 at 11:22:28AM +0100, Anatoli Hristov wrote: > The only problem I have is that I cant compare other field than the > first one in > for ex_phone in phones: > telstr = ex_phone[0].lower() > When I use telstr = ex_phone[0].lower() it says out of range and the > strange think is that the range is 6 I can't figure that out. As I understood it phones is an csv.reader instance and you are iterating repeatedly over it. But, csv.reader does not work this way. You either have to reinstantiate phones with a fresh file-descriptor (not so good) or cache the values in an appropriate data structure (better) e.g. a list. > import csv > > # Open the file with the names and addresses > origf = open('c:/Working/vpharma.csv', 'rt') > # Open the file with the phone numbers > secfile = open('c:/Working/navori.csv', 'rt') Note that you never close origf and secfile. > [?] > # Reads the file with the phone numbers > # Format "First name","Lastname","Address","City","Country","Phone" > phones = csv.reader(secfile, delimiter=';') So this should probably be PHONES = list(csv.reader(secfile, delimiter=';')) (in uppercase letters as it is a global) > [?] > if __name__ == '__main__': > name_find() > > # Writes the list to a file > wfile = open('c:/Working/ttest.csv', "wb") > writer = csv.writer(wfile, delimiter=';') > for insert in namelist: > writer.writerow(insert) > wfile.close() This should go either in the "if __name__ = ?" part or in a function on its own. Also have a look at the with statement you can use it in several places of your code. There are several other improvements you can make: + instead of having the file-names hard coded try to use argparse to get them from the command-line, + let functions stand at their own and use less globals, + try to avoid the use of the type of the data structure in the name (e.g. names is IMHO a better name then namelist), + add tests. Regards, Thomas From feedbackflow at gmail.com Thu Nov 29 07:20:15 2012 From: feedbackflow at gmail.com (Bart Thate) Date: Thu, 29 Nov 2012 13:20:15 +0100 Subject: changing dicts in a threaded environment ? In-Reply-To: References: Message-ID: Terry ! thnx for replying, feedback is the thing i need ;] sorry for the late reply, after your mail i have been coding like hurt (so many hours nonstop hacking hurts in the chair). As i started with all the code in 1 file (what i get when i think something is cool), i splitted the stuff up into proper modules, so i can show that to explain what i am up to. I come to this code a long time ago, during the programming of my previous bots, where co coder maze introduced me to what he called a LazyDict. Dict with dotted access for attributes instead of __getitem__, i like how the code looks when dotted acces is used. While this contruct found its way into my code, i had to deal with something different and this is "how to adapt my data - storage - to changes in the code" problem. All the databases i tried used some sort of schema to define the data and onces that needs to change, reflection needed to take place to update the storage of data. I need to be able to add attributes to objects without no need to change or reflect anything. So i took the LazyDict and added json dump and load functionality to it. This worked great in terms of flexibility in the code, but load on it is very low, so not to be compared with the scale etc that normal databases have to deal with. Soon i want to add a .send(JID) to it so i can send a object via JSON to the other side and have it reconstruct there. Kinda like Pyro based on JSON, been experimenting with that with Aim during our endless "let's code a new collective" attempts, where the "send the pickle over the wire" stuff was not secure, the use of XML to make it secure made it a true bandwidth hog, and parsing JSON is still considered secure, because no eval gets used when deconstructing. In first i am not interested in deconstructing objects from the JSON yet, my first intention is to make a JSON dict replication system, where i can put data in a dict, put JSON of that dict over the wire to various other pieces of my software and have it possible to authenticate the correctness of the data. This is because i want - eventually - to have my code run in a device where the data it generates can be used as proof in a court of law. This is to make some modern version of a diary, where one can register all things happening during a period of time. So i have to make this 1 LazyDict, JSON saveable and sendable etc, objects thats is going to be the core of what i doing and i need to properly design it and todo that properly i need feedback on it ;] Thank for your feedback Terry, it is clear to me now that i need to do the locking myself, thing is i need to have this object behave excactly as normal standard object (dict in this case) so i want to know if there are any pointers to docs that discuss the semantics of dicts esp. what needs to be locked properly to ensure that there is only one accessor at the time. Question to all of you is something like "if you want 1 basic type added to python 4, what would it's semantics be ?" See https://github.com/feedbackflow/life for the code i am working on now ;] https://github.com/feedbackflow/life/blob/master/life/__init__.py - for the big O class that is to become the core of it all, pun intended ;] Looking forward to hear what you and all the others reading this are thinking. Bart, thinking of "what if i stored the JSON in git ?" and "would mercurial then not be better" haha p.s. code is not in a "good shape" yet, it is just that i would take too long for me to respond and 20 hours work days are killing me to get this right "the first time" ;] p.s.s trbs your code is in the moment plugs are going to be added, need the core solid first ;] On Tue, Nov 27, 2012 at 8:18 PM, Terry Reedy wrote: > On 11/27/2012 7:53 AM, Bart Thate wrote: > > [Answers based on reading without thread experience.] > > > i use python3 now and i need to be able to remove elements from a dict >> in a thread safe manner. >> > > Essentially no change from py2. > > > kinda like a Queue.Queue thing but then in a dict, so i can pass arond >> my dict based objects as parameters arond without having to wonder if >> it gets properly locked. >> > > As I understand it, dicts do not get locked unless you do it. > > So not only do i need to get this solved: >> >> Issue #14417 **: Mutating a dict during >> >> lookup now restarts the lookup instead of raising a RuntimeError (undoes >> issue #14205 **). >> > > As I understand #14417, you should be explicitly locking dicts. The issue > in #14205 was that people doing mutations in just one thread and lookups in > others usually got away without locking becuase of recursive retries, but > occasionally crashed the interpreter because of them. The first fix was to > prevent crashes by removing retries. But that would break programs that > naively worked because of them. The second fix was to do iterative retries > instead, so a thread might occasionally hang, but without crashing. > > As I understand it, it is better to not be a naive user. > > > i also need to lock all the actual modifying underlying "real" stuff as >> well not just the iterator or view or whatever i don't know yet ;] >> > > I am not sure what you are asking. > > >> So my question is kinda like, is a dict possible in the same way the >> Queue.Queue is now made truely thread safe ? >> > > A custom dict might be but the builtin is not. Queue is somewhat unique as > a builtin designed for threads. > > > Also is there something like a select.select for queues ? >> > > Have you searched? > > > I want to pass a dict to a thead and then have a watcher on the dicts >> state if result have arrived. >> > > Builtin dicts do not have an 'I have changed flag. You would need to > subclass or probably better, wrap a dict using the appropriate pattern. > Perhaps done already, but you want a wrapper that does both locking and > watcher notification. > > -- > Terry Jan Reedy > > -- > http://mail.python.org/**mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From mauriceling at gmail.com Thu Nov 29 09:26:59 2012 From: mauriceling at gmail.com (mauriceling@acm.org) Date: Thu, 29 Nov 2012 06:26:59 -0800 (PST) Subject: =?windows-1252?Q?=5BANN=5D_New_article_published=3A_TMSTAF_=96_The_Extended?= =?windows-1252?Q?_Use_of_STAF_on_Test_Automation?= Message-ID: <87e57b3e-6de8-43a2-a8b2-9a05b1168267@googlegroups.com> http://ojs.pythonpapers.org/index.php/tpp/article/view/247 Abstract As software packages becomes increasingly large and complex, the time required for testing them, throughout the development lifecycle, has also increased. Because testing activities consume the majority of software Quality Assurance (QA) resources, a test platform was needed to speed up test cycles without any decrease in test result accuracy. The use of Python 2.6 scripting language to create a faster, automated testing platform is reported here. Trend Micro Software Testing Automation Framework (TMSTAF) was developed using Python 2.6, based on Software Testing Automation Framework (STAF), to improve automated testing. We found that TMSTAF not only decreased testing time, it provided faster process integration, test feedback, and improved overall software quality. Using the TMSTAF automation environment for development and execution is simple to set up. Test cases can be created for use in both manual and automated tasks, converted to automated scripts, and implemented with structural and flexible mechanisms. Automation script pre-runs and debugging make troubleshooting more efficiently; TMSTAF test report data can be used to identify quality issues quickly. TMSTAF can be seamlessly integrated into the build release process, making it a smart option for software QA engineers. From lars at rational-it.com Thu Nov 29 09:59:37 2012 From: lars at rational-it.com (lars van gemerden) Date: Thu, 29 Nov 2012 06:59:37 -0800 (PST) Subject: weird isinstance/issubclass behavior? Message-ID: <1cad82ec-8241-486c-9c8b-c7d6ca427adc@googlegroups.com> Hi, I have encountered some strange behavior of isinstance(/issubclass): depending on the import path used for classes i get different output, while the classes i compare are in the same file. Basically if i import a class as: from mod1.mod2 import A or: from mod0.mod1.mod2 import A which both result in importing the same class, a call to isinstance(inst, A) in another module can have a different output. In this module print type(inst), A, isinstance(inst, A), issubclass(type(inst), A) gives: False False resp. True True which seems somewhat logical, but in my case a strange gotcha. My question is: Is this intended, inevitable or a bug? Cheers, Lars PS: this is somewhat simpler than the actual case i've encountered, and i haven't tested this exact case, but for now i hope this is enough to get some of your insight. From rosuav at gmail.com Thu Nov 29 10:08:20 2012 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 30 Nov 2012 02:08:20 +1100 Subject: weird isinstance/issubclass behavior? In-Reply-To: <1cad82ec-8241-486c-9c8b-c7d6ca427adc@googlegroups.com> References: <1cad82ec-8241-486c-9c8b-c7d6ca427adc@googlegroups.com> Message-ID: On Fri, Nov 30, 2012 at 1:59 AM, lars van gemerden wrote: > Basically if i import a class as: > > from mod1.mod2 import A > > or: > > from mod0.mod1.mod2 import A > > which both result in importing the same class, a call to isinstance(inst, A) in another module can have a different output. What you may be seeing there is that you've imported the module twice. There are two entirely separate module objects, taken from the same file. Something instantiated from one class isn't an instance of the other class even if they're indistinguishable classes; a little monkeypatching might show you what's going on (fiddle with one class, check the other, fiddle hasn't happened). As a general rule, importing a module in different ways is considered dangerous. Be consistent, and then this sort of oddity won't occur - and you also won't have other oddities, eg with other global state. ChrisA From kagard at gmail.com Thu Nov 29 10:43:57 2012 From: kagard at gmail.com (kagard) Date: Thu, 29 Nov 2012 07:43:57 -0800 (PST) Subject: Migrate from Access 2010 / VBA References: <6db4ed18-a16e-4cd1-940b-c10f51207780@googlegroups.com> Message-ID: <62fbd558-ac46-4de9-90ca-b099a36b31e2@f17g2000vbz.googlegroups.com> On Nov 27, 7:06?pm, David Bolen wrote: > kgard writes: > > I am the lone developer of db apps at a company of 350+ > > employees. Everything is done in MS Access 2010 and VBA. I'm > > frustrated with the limitations of this platform and have been > > considering switching to Python. I've been experimenting with the > > language for a year or so, and feel comfortable with the basics. > (...) > > Has anyone here made this transition successfully? If so, could you > > pass along your suggestions about how to do this as quickly and > > painlessly as possible? > > I went through a very similar transition a few years ago from > standalone Access databases (with GUI forms, queries and reports, as > well as replication) to a pure web application with full reporting > (albeit centrally designed and not a report designer for users). > > I suppose my best overall suggestion is to migrate the data first and > independently of any other activities. ?Unless your uses for Access in > terms of GUI or reporting are extremely limited, don't try to replace > your current system in one swoop, and in particular, be willing to > continue allowing Access as long as necessary for GUI/reports until > you're sure you've matched any current capabilities with an alternate > approach. ?For all its warts, as a database GUI and reporting tool, > Access has a lot going for it, and it can be more complex than you may > think to replicate elsewhere. > > So the first thing I would suggest is to plan and implement a > migration of the data itself. ?In my case I migrated the data from > Access into PostgreSQL. ?That process itself took some planning and > testing in terms of moving the data, and then correcting various bits > of the schemas and data types (if I recall, booleans didn't round-trip > properly at first), so was actually a series of conversions until I > was happy, during which time everyone was using Access as usual. > > To support the migration, I created a mirror Access database to the > production version, but instead of local Jet tables, I linked all the > tables to the PostgreSQL server. All other aspects of the Access > database (e.g., forms, reports, queries) remained the same, just now > working off of the remote data. ?This needed testing too - for > example, some multi-level joining in Access queries can be an issue. > In some cases it was easier for me to migrate selected Access query > logic into a database view and then replace the query in Access to use > the view. ?You also need to (painfully) set any UI aspects of the > table definitions manually since the linking process doesn't set that > up, for which I used the original Access db as a model. ?I ended up doing > that multiple times as I evolved the linked database, and I'll admit that > was seriously tedious. > > While not required, I also wrapped up my new linked Access database > into a simple installer (InnoSetup based in my case). ?Prior to this > everyone was just copying the mdb file around, but afterwards I had an > installer they just ran to be sure they had the latest version. > > If you do this part carefully, for your end users, aside from > installing the new database, they see absolutely no difference, but > you now have easy central access to the data, and most importantly can > write other applications and tools against it without touching the > Access side. ?It turns Access into just your GUI and reporting tool. > > If you have power users that make local changes they can continue to > design additional queries or reports in their own local mdb against > the linked tables. ?They'll need some extra support for updates > though, either instructions to re-link, or instructions on exporting > and importing their local changes into a newly installed version of > your master mdb. > > Having done this, you are then free to start implementing, for > example, a web based application to start taking over functionality. > The nice thing is that you need not replicate everything at once, you > can start slow or with the most desirable features, letting Access > continue to handle the less common or more grungy legacy stuff at > first. ?There are innumerable discussions on best web and application > frameworks, so probably not worth getting into too much. ?In my case > I'm using a CherryPy/Genshi/SQLAlchemy/psycopg2 stack. > > As long as you still have Access around, you'll have to take it into > consideration with schema changes, but that's not really that much > harder than any other schema migration management. ?It's just another > client to the database you can run in parallel as long as you wish. > If you do change the schema, when done, just load your master Access > database, update the links, and rebuild/redistribute the installer to > your users. ?Many changes (e.g., new columns with defaults) can be > backwards compatible and avoid forced upgrades. > > You can operate both systems in parallel for a while even for similar > functionality (for testing if nothing else), but can then retire > functionality from Access as the web app supports it. ?Ideally this > will be organic by your users preferring the web. ?Selecting when to > drop Access entirely can then be driven by user demand. ?Or, for > example, as is true in my case, none of my end users use Access any > more, but I still have one tool for administrators that isn't worth > replacing yet so they still use Access. ?One of my partners also just > feels more comfortable making ad-hoc queries/reports in Access than > tools like pgAdmin3 or iReport so I let him, as long as it's just for > his personal use and I don't have to guarantee it won't break if I > evolve the schema. > > On the reporting side, I really haven't found a good Python reporting > solution that includes a solid solution for the designer side of the > coin. ?Prior to this project I most often used ReportLab for > dynamically produced reports (and still do) and its great, but I was > really the only one designing those reports and do most of them in > code. ?For this project, I wanted an independent report format along > with a visual designer (so others could design reports for the system > to execute). ?I settled on JasperReports, which has a well-defined > jrxml format, and a nice report design tool iReport. ?Someone else > posted about OpenRPT which was also on my short list at the time > (along, I think, with Eclipse BIRT). ?JasperReports (and its > ecosystem) has a bit of a learning curve - then again, all these tools > do, including Access - for my part, I've been happy with the choice. > > Now, while amongst report designers you can just exchange report > design files and use iReport, for end users you'll need a way to > process those reports for your web app. ?There is a Jasper Server tool > that is designed to organize and publish reports, but I found it > overly-complicated for my use case and I wanted complete control of > the end user interaction (hiding the reporting behind my web app). > > I settled on a really small (~150 line) Java servlet, running beneath > Jetty, and configured to access the same PostgreSQL database. ?It > accepts localhost-only HTTP report requests (with report parameters as > query parameters) and delivers the result as a PDF. ?So my main web > application, when needed, makes an internal http request to the > reporting server on the same host, and then delivers the returned PDF > to the end user. ?Once up and running, I can design a new report in > iReport, and then drop the jrxml design file into the reporting folder > on my server and it'll be available for the web application to use. > This is the only non-Python server component (as far as my own code). > > As with the web app itself, this reporting operates in parallel with > any remaining Access functionality, so you need not replicate every > Access report all at once. > > To put this all in context, for myself (almost zero Access experience > at the start, but plenty of Python and PostgreSQL experience), my data > conversion probably took about 3 months overall for a relatively small > Access database until everyone was working off of PostgreSQL under the > covers. ?But that was all on my time, the actual switchover was quick > once ready to release. ?While some of that scales with size, most of > the time was figuring out the process and testing. > > To be truthful, I'm here maybe 4 years later, and still have some uses > of Access in the system. ?I suppose I could have pushed harder to > completely retire it, but honestly, by following the above process the > remaining uses just don't bother or interfere with me that much. ?If > they ever do I'll replicate those remaining bits of functionality > elsewhere. > > -- David > > Thanks, David, for all the helpful insights. I really appreciate the time you took to reply. Thanks to everyone who pitched in. You've given me a lot to think about. Keith From joel.goldstick at gmail.com Thu Nov 29 11:05:39 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Thu, 29 Nov 2012 11:05:39 -0500 Subject: Migrate from Access 2010 / VBA In-Reply-To: <62fbd558-ac46-4de9-90ca-b099a36b31e2@f17g2000vbz.googlegroups.com> References: <6db4ed18-a16e-4cd1-940b-c10f51207780@googlegroups.com> <62fbd558-ac46-4de9-90ca-b099a36b31e2@f17g2000vbz.googlegroups.com> Message-ID: On Thu, Nov 29, 2012 at 10:43 AM, kagard wrote: > On Nov 27, 7:06 pm, David Bolen wrote: > > kgard writes: > > > I am the lone developer of db apps at a company of 350+ > > > employees. Everything is done in MS Access 2010 and VBA. I'm > > > frustrated with the limitations of this platform and have been > > > considering switching to Python. I've been experimenting with the > > > language for a year or so, and feel comfortable with the basics. > > (...) > > > > -- David > > > > > > Thanks, David, for all the helpful insights. I really appreciate the > time you took to reply. Thanks to everyone who pitched in. You've > given me a lot to think about. > > Keith > -- > http://mail.python.org/mailman/listinfo/python-list > This looks promising: http://www.codediesel.com/data/migrating-access-mdb-to-mysql/ -- Joel Goldstick -------------- next part -------------- An HTML attachment was scrubbed... URL: From lars at rational-it.com Thu Nov 29 11:07:15 2012 From: lars at rational-it.com (lars van gemerden) Date: Thu, 29 Nov 2012 08:07:15 -0800 (PST) Subject: weird isinstance/issubclass behavior? In-Reply-To: <1cad82ec-8241-486c-9c8b-c7d6ca427adc@googlegroups.com> References: <1cad82ec-8241-486c-9c8b-c7d6ca427adc@googlegroups.com> Message-ID: On Thursday, November 29, 2012 3:59:37 PM UTC+1, lars van gemerden wrote: > Hi, > > > > I have encountered some strange behavior of isinstance(/issubclass): depending on the import path used for classes i get different output, while the classes i compare are in the same file. > > > > Basically if i import a class as: > > > > from mod1.mod2 import A > > > > or: > > > > from mod0.mod1.mod2 import A > > > > which both result in importing the same class, a call to isinstance(inst, A) in another module can have a different output. In this module > > > > print type(inst), A, isinstance(inst, A), issubclass(type(inst), A) > > > > gives: > > > > False False > > > > resp. > > > > True True > > > > which seems somewhat logical, but in my case a strange gotcha. My question is: > > Is this intended, inevitable or a bug? > > > > Cheers, Lars > > > > PS: this is somewhat simpler than the actual case i've encountered, and i haven't tested this exact case, but for now i hope this is enough to get some of your insight. I know for sure that the imports both import the same file, though if i understand you correctly, it creates 2 different module objects? Are module object only created on an import statement? The 2 parameters of isinstance() follow a very different import path. Anyway, to not spend too much time us this, i'll just file it under 'dangerous' and use the second import statement. It does solve my problem. Thanks, Lars From buzzard at invalid.invalid Thu Nov 29 11:24:20 2012 From: buzzard at invalid.invalid (duncan smith) Date: Thu, 29 Nov 2012 16:24:20 +0000 Subject: Exponential arrival distribution in Python In-Reply-To: References: Message-ID: <50b78c40$0$12518$a8266bb1@newsreader.readnews.com> On 28/11/12 21:34, Ricky wrote: > > Hi all, > > I am doing a project on traffic simulation. I want to introduce exponential arrival distribution to precede this task. Therefore I want write a code in python for exponential arrival distribution. I am very new for programming and if anybody can help me on this that would be great. > > Cheers, > Ricky > Maybe you mean something like, >>> from random import expovariate >>> expovariate(1) 0.09133428954823608 >>> expovariate(1) 2.5388809393383407 >>> Duncan From hansmu at xs4all.nl Thu Nov 29 11:32:37 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Thu, 29 Nov 2012 17:32:37 +0100 Subject: Weird import failure with "nosetests --processes=1" In-Reply-To: References: Message-ID: <50b78e26$0$6945$e4fe514c@news2.news.xs4all.nl> On 29/11/12 04:13:57, Roy Smith wrote: > I've got a minimal test script: > > ----------------------------- > $ cat test_foo.py > import pyza.models > print pyza.models > > def test_foo(): > pass > ----------------------------- > > pyza.models is a package. Under normal conditions, I can import it fine: > > $ python > Python 2.7.3 (default, Aug 1 2012, 05:14:39) > [GCC 4.6.3] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> import pyza.models >>>> print pyza.models > '/home/roy/deploy/current/pyza/models/__init__.pyc'> >>>> > > But when I run nosetests in parallel mode, the import fails in a way > which has me baffled. What's going on here? > > $ nosetests --processes=1 -s test_foo:test_foo > '/home/roy/deploy/current/pyza/models/__init__.pyc'> > E > ====================================================================== > ERROR: Failure: AttributeError ('module' object has no attribute > 'models') > ---------------------------------------------------------------------- > Traceback (most recent call last): > File > "/home/roy/production/python/local/lib/python2.7/site-packages/nose/loade > r.py", line 390, in loadTestsFromName > addr.filename, addr.module) > File > "/home/roy/production/python/local/lib/python2.7/site-packages/nose/impor > ter.py", line 39, in importFromPath > return self.importFromDir(dir_path, fqname) > File > "/home/roy/production/python/local/lib/python2.7/site-packages/nose/impor > ter.py", line 86, in importFromDir > mod = load_module(part_fqname, fh, filename, desc) > File "/home/roy/songza/pyza/djapi/test_foo.py", line 2, in > print pyza.models > AttributeError: 'module' object has no attribute 'models' > > ---------------------------------------------------------------------- > Ran 1 test in 0.107s > > FAILED (errors=1) That is baffling indeed. It looks like nose is adding some directory to sys.path, which contains a module pyza.py instead of a package. One thing you could try, is changing line 2 of you script to just print pyza , to see if pyza is being loaded from /home/roy/deploy/current/pyza/__init__.pyc, as you'd expect. If it isn't, you'll be one step closer to a solution. Another idea might be to delete all *.pyc files below /home/roy/deploy/current/ and /home/roy/songza/pyza/djapi/. That would help if a .pyc file is somehow out of sync with the corresponding .py file. That's not supposed to happen, but if it does, you can get weird results. One last idea: put these lines at the top of test_foo.py import pdb pdb.set_trace() Running under nosetest should then drop you in the debugger. Single step into the next statement ("import pyza.models") to see where this gets you. It should import pyza, and then pyza.models. Add some print statements to the __init__.py files of those packages, so that there's somewhere for pdb to stop and prompt. Hope this helps, -- HansM From ian.g.kelly at gmail.com Thu Nov 29 11:41:22 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 29 Nov 2012 09:41:22 -0700 Subject: weird isinstance/issubclass behavior? In-Reply-To: References: <1cad82ec-8241-486c-9c8b-c7d6ca427adc@googlegroups.com> Message-ID: On Thu, Nov 29, 2012 at 9:07 AM, lars van gemerden wrote: > > PS: this is somewhat simpler than the actual case i've encountered, and > i haven't tested this exact case, but for now i hope this is enough to get > some of your insight. > > I know for sure that the imports both import the same file, though if i > understand you correctly, it creates 2 different module objects? Are module > object only created on an import statement? > > Yes, Python sees imports of two different absolute module paths and does not recognize them as being the same file, so it imports each separately. This scenario typically arises when your sys.path includes a folder that is actually a module itself. Recommended usage: don't do that. Your sys.path entries should contain modules and packages, not point inside them. -------------- next part -------------- An HTML attachment was scrubbed... URL: From torriem at gmail.com Thu Nov 29 12:22:56 2012 From: torriem at gmail.com (Michael Torrie) Date: Thu, 29 Nov 2012 10:22:56 -0700 Subject: Migrate from Access 2010 / VBA In-Reply-To: References: <6db4ed18-a16e-4cd1-940b-c10f51207780@googlegroups.com> <62fbd558-ac46-4de9-90ca-b099a36b31e2@f17g2000vbz.googlegroups.com> Message-ID: <50B799F0.6010908@gmail.com> On 11/29/2012 09:05 AM, Joel Goldstick wrote: > This looks promising: > http://www.codediesel.com/data/migrating-access-mdb-to-mysql/ Unfortunately I have not found mdb tools to be sufficient. You can use them to convert the schema to sql, and to reveal any mdb password (great for looking at the data structures of compiled apps), but it can't handle all the data types properly. To get data out of an mdb file, I wrote a simple python program that used pyodbc to get the data. pyodbc implements a standard python db api interface. I opened the access database file with: MDB = 'C:/Path/to/frs_or_mdb_file' DRV = '{Microsoft Access Driver (*.mdb)}' PWD = 'ifneeded' conn = pyodbc.connect('DRIVER=%s;DBQ=%s;UID=admin;PWD=%s' % (DRV,MDB,PWD)) curs = conn.cursor() Then you can run queries with standard python db abi calls in standard SQL syntax, and it's pretty easy to pull out the data and insert it into a MySQL or PostgreSQL database. This is for python on Windows of course, and has to have Access installed, or at least the access engine. From nobody at nowhere.com Thu Nov 29 13:39:01 2012 From: nobody at nowhere.com (Nobody) Date: Thu, 29 Nov 2012 18:39:01 +0000 Subject: os.popen and the subprocess module References: Message-ID: On Thu, 29 Nov 2012 10:09:44 +0100, Thomas Rachel wrote: > The variant with shell=True is more os.popen()-like, but has security > flaws (e.g., what happens if there are spaces or, even worse, ";"s in the > command string? I think that you're conflating the shell= option with whether the command is a given as a list or a string. Attempting to construct a command string risks introducing security flaws (or other bugs). Wherever possible, the first argument should be a list. A string should only be used if that's what you're given (e.g. via a configuration file), in which case it should be used literally, without any attempt to substitute filenames or other parameters. On Windows, list-versus-string and shell= are orthogonal. A list will always be converted to a string, as that's what the underlying CreateProcess() function requires. shell=True prepends "cmd /c " ("cmd" is replaced by the value of %comspec% if that is defined); this allows execution of batch files, scripts, etc based upon their associations. On Unix, passing a list with shell=True is rarely useful. It just prepends ['/bin/sh', '-c'] to the list, so the first item is the shell command while subsequent items provide the values for the shell variables $1, $2, etc. From nobody at nowhere.com Thu Nov 29 13:45:54 2012 From: nobody at nowhere.com (Nobody) Date: Thu, 29 Nov 2012 18:45:54 +0000 Subject: Imaging libraries in active development? References: Message-ID: On Wed, 28 Nov 2012 04:30:25 -0800, Alasdair McAndrew wrote: > What I want to know is - what are the current "standard" libraries for > image processing in Python which are in active development? NumPy/SciPy. PIL is fine for loading/saving image files (although if you're using a GUI toolkit, that probably has its own equivalents). But for any non-trivial processing, I normally end up using either NumPy or (if speed is an issue) PyOpenGL/GLSL. From subhabangalore at gmail.com Thu Nov 29 14:17:00 2012 From: subhabangalore at gmail.com (subhabangalore at gmail.com) Date: Thu, 29 Nov 2012 11:17:00 -0800 (PST) Subject: Few Issues on Parsing and Visualization Message-ID: <7ebadbbd-de72-46ec-853b-01ba71b2da27@googlegroups.com> Dear Group, I am looking for some Python based Natural Language Tools. (i)Parsers (either syntactic or semantic). NLTK has but there I have to input the grammar. I am looking for straight built in library like nltk tagging module. (ii) I am looking for some ner extraction tools. NLTK has I am looking for another, pyner is there but could not download. (iii) I am looking for relation extraction tool NLTK has but I am looking for another. (iv) I am looking for one information extraction library, I found GenSim but could not use it properly. (v) I am looking for a visualization library, found networkx,matplotlib,vtk but I am looking for a visual analytics library. I am looking all these tools for the use in Windows(XP/7) with Python2.6/2.7. And if anyone can kindly let me know on how to use the Python binding of Stanford Parser. Thanking in Advance, Regards, Subhabrata. From tjreedy at udel.edu Thu Nov 29 16:00:48 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 29 Nov 2012 16:00:48 -0500 Subject: weird isinstance/issubclass behavior? In-Reply-To: <1cad82ec-8241-486c-9c8b-c7d6ca427adc@googlegroups.com> References: <1cad82ec-8241-486c-9c8b-c7d6ca427adc@googlegroups.com> Message-ID: On 11/29/2012 9:59 AM, lars van gemerden wrote: > Hi, > > I have encountered some strange behavior of isinstance(/issubclass): depending on the import path used for classes i get different output, while the classes i compare are in the same file. > > Basically if i import a class as: > > from mod1.mod2 import A > or: > from mod0.mod1.mod2 import A > > which both result in importing the same class, As other said, both import the same abstract class but create two different concrete class objects. > a call to isinstance(inst, A) in another module can have a different output. > In this module > print type(inst), A, isinstance(inst, A), issubclass(type(inst), A) > gives: > False False Add print id(type(inst)), id(A) and you will see that they are different objects. The isinstance and issubclass calls compare the classes by identity (the default meaning of ==) and so False, False are correct. -- Terry Jan Reedy From ramit.prasad at jpmorgan.com Thu Nov 29 17:53:20 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Thu, 29 Nov 2012 22:53:20 +0000 Subject: re.search when used within an if/else fails In-Reply-To: References: <2930dc36-9d8f-411d-aa7a-272fe4885ec5@i5g2000yqh.googlegroups.com> <11835b30-3448-45cc-9ae5-46d651f93429@googlegroups.com> <50b68476$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF47418040F38@SCACMX008.exchad.jpmchase.net> Dennis Lee Bieber wrote: > > Unless there has been a major change in the parser... (I still don't > have Python 3.x installed) > > I believe is expanded to 8-spaces -- NOT TO NEXT MULTIPLE OF > 8... A tab is *one* character. Your *editor* may show tabs visually "expanded" or convert them to spaces. This is entirely editor dependent. My current (python) editor, does expands tabs to the next *multiple* of 4. It helps keep code aligned, and I have no need for 4 hard spaced tabs without regards to alignment (yet). I have had editors that did 4 hard spaced tabs, so it might be a developer/application preference. >>> with open(r'c:\ramit\mix_tab_space.txt')as f: ... d = f.read() ... >>> print repr(d) '\tblah\n test\n\t' >>> print d[0] + 'goo' goo >>> print repr(d[0] + 'goo') '\tgoo' > > So the first is 8+4 => 12 spaces, the second is 2+8+4 => 14 spaces. > > Does 2 + + 2 vs 4 + vs + 4 succeed? That would > confirm the treatment. > > The main concern with mixed tab and spaces, as I recall, was due to > having /editors/ and /terminals/ configured to show as a four > space (or anything other than an eight space) increment; so visually > four spaces and one might look the same... One user might have the > editor showing 4-space indents on but entering text using 4 spaces > on input -- which now is mis-aligned if the source file HAD in it. ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From ramit.prasad at jpmorgan.com Thu Nov 29 17:57:00 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Thu, 29 Nov 2012 22:57:00 +0000 Subject: re.search when used within an if/else fails References: <2930dc36-9d8f-411d-aa7a-272fe4885ec5@i5g2000yqh.googlegroups.com> <11835b30-3448-45cc-9ae5-46d651f93429@googlegroups.com> <50b68476$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF47418040F5F@SCACMX008.exchad.jpmchase.net> Ramit Prasad wrote: > > Dennis Lee Bieber wrote: > > > > Unless there has been a major change in the parser... (I still don't > > have Python 3.x installed) > > > > I believe is expanded to 8-spaces -- NOT TO NEXT MULTIPLE OF > > 8... > > A tab is *one* character. Your *editor* may show tabs visually > "expanded" or convert them to spaces. This is entirely editor dependent. > My current (python) editor, does expands tabs to the next *multiple* of 4. > It helps keep code aligned, and I have no need for 4 hard spaced tabs > without regards to alignment (yet). I have had editors that did 4 hard > spaced tabs, so it might be a developer/application preference. > > >>> with open(r'c:\ramit\mix_tab_space.txt')as f: > ... d = f.read() > ... > >>> print repr(d) > '\tblah\n test\n\t' > >>> print d[0] + 'goo' > goo > >>> print repr(d[0] + 'goo') > '\tgoo' Apologies, I missed that it was talking about the parser and not editor/file. Please disregard my previous post. ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From ramit.prasad at jpmorgan.com Thu Nov 29 18:04:40 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Thu, 29 Nov 2012 23:04:40 +0000 Subject: Imaging libraries in active development? In-Reply-To: References: <50B65486.8010306@gmail.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF47418040FB0@SCACMX008.exchad.jpmchase.net> Christian Heimes wrote: > > Am 28.11.2012 19:14, schrieb Michael Torrie: > > I'm curious. What features do you need that pil doesn't have? Other > > than updating pil to fix bugs, support new image types or new versions > > of Python, what kind of active development do you think it needs to > > have? Maybe pil has all the features the original author wanted and is > > pretty stable. To judge a package on how fast it's changing seems a bit > > odd to me. Obviously you want to know that bugs can get fixed of > > course. Perhaps none have been reported recently. > > PIL is missing a bunch of features like proper TIFF support (no > multipage, g3/g4 compression and more), JPEG 2000, RAW and HDR image > formats, tone mapping, proper ICC support, PEP 3128 buffer support ... > > PIL is also rather slow. My smc.freeimage library can write JPEGs about > six times faster, because it uses libjpeg-turbo. Only some Linux > distributions have replaced libjpeg with the turbo implementation. > Have you tried libtiff? I believe the author may read this list. (He posted here for at least one release). http://code.google.com/p/pylibtiff/ Hope that helps, Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From ramit.prasad at jpmorgan.com Thu Nov 29 18:13:25 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Thu, 29 Nov 2012 23:13:25 +0000 Subject: Compare list entry from csv files In-Reply-To: References: <50B3E865.9070700@davea.name> <50B43246.2010902@davea.name> Message-ID: <5B80DD153D7D744689F57F4FB69AF47418040FEC@SCACMX008.exchad.jpmchase.net> Anatoli Hristov wrote: > Hello, > > Tried to document a little bit the script, but I'm not that good in that too :) > > The only problem I have is that I cant compare other field than the > first one in > for ex_phone in phones: > telstr = ex_phone[0].lower() > When I use telstr = ex_phone[0].lower() it says out of range and the > strange think is that the range is 6 I can't figure that out. So when > I edit the csv I modify the look of the file and then I start the > script and it works, but I wanted to use more than one condition and I > can't :( > > Can you print ex_phone first. You are opening the files in text mode so I wonder if the line endings are causing you to read and extra "line" in between. Can you try reading the csv as "rb" instead of "rt"? > > > import csv > > # Open the file with the names and addresses > origf = open('c:/Working/vpharma.csv', 'rt') > # Open the file with the phone numbers > secfile = open('c:/Working/navori.csv', 'rt') > > # Creates the empty list with the names > namelist = [] > # Creates the empty list with the phone numbers > PHONELIST = [] > > > # Reads the file with the names > # Format "Name","Phone" > names = csv.reader(origf, delimiter=';') > > # Reads the file with the phone numbers > # Format "First name","Lastname","Address","City","Country","Phone" > phones = csv.reader(secfile, delimiter=';') > > # Creates a list with phone numbers > #for tel in phones: > # PHONELIST.append(tel) > > > def finder(Compare_Name,rows): > ''' > Compare the names from the namelist with the names from the phonelist. > If the name match - then the phone number is added to the specified field > ''' > for ex_phone in phones: > telstr = ex_phone[0].lower() > print telstr > if telstr.find(Compare_Name) >= 0: > print "\nName found: %s" % Compare_Name > namelist[rows][-1] = ex_phone[-1].lower() > else: > print "Not found %s" % Compare_Name > pass > return > > def name_find(): > rows = 0 > for row in names: > namelist.append(row) > Compare_Name = row[1].lower() > finder(Compare_Name,rows) > rows = rows+1 > > if __name__ == '__main__': > name_find() > > # Writes the list to a file > wfile = open('c:/Working/ttest.csv', "wb") > writer = csv.writer(wfile, delimiter=';') > for insert in namelist: > writer.writerow(insert) > wfile.close() ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From akegb3 at gmail.com Thu Nov 29 20:30:27 2012 From: akegb3 at gmail.com (akegb3 at gmail.com) Date: Thu, 29 Nov 2012 17:30:27 -0800 (PST) Subject: How to add CC and BCC while sending mails using python In-Reply-To: References: <46da4e770809300727j368a27b8qc570a8451a53e470@mail.gmail.com> Message-ID: On Tuesday, September 30, 2008 8:00:16 AM UTC-8, Bernhard Walle wrote: > Hi, > > * cindy jones [2008-09-30 19:57]: > > > > Can someone tel me how to add cc's and bcc's while sending mails using > > python > > Following (tested) snippet should help: > > ------------------ 8< ------------------------------ > from smtplib import SMTP > from email.mime.image import MIMEImage > from email.mime.text import MIMEText > from email.mime.multipart import MIMEMultipart > > to = 'to_address at example.invalid' > cc = 'cc_address at example.invalid' > bcc = 'bcc_address at example.invalid' > > msg = MIMEMultipart() > msg['To'] = to > msg['Cc'] = cc > msg['From'] = 'bernhard at bwalle.de' > msg['Subject'] = 'Test' > text = MIMEText('Das ist ein Test') > text.add_header("Content-Disposition", "inline") > msg.attach(text) > > s = SMTP('test.smtp.relay') > s.sendmail(msg['From'], [to, cc, bcc], msg.as_string()) > s.quit() > ------------------ >8 ------------------------------ > > > Regards, > Bernhard Hello Bernhard, I've tried the above code and the bcc address does not receive the message, on the To & CC addresses receive it. Here are snippets from my code, perhaps something will stand out to you? to = 'ed at domain.gov' cc = 'ed at gmailmail.com' bcc = 'ed at home.net' msg = MIMEMultipart() msg['To'] = to msg['Cc'] = cc msg['Subject'] = 'My Subject text here' msg['From'] = 'edsboss at domain.gov' smtp = SMTP("smtpserver") smtp.ehlo() smtp.sendmail(msg['From'], [to, cc, bcc], msg.as_string()) Thanks in advance..hope you're still out there!! ~Ed From akegb3 at gmail.com Thu Nov 29 20:30:27 2012 From: akegb3 at gmail.com (akegb3 at gmail.com) Date: Thu, 29 Nov 2012 17:30:27 -0800 (PST) Subject: How to add CC and BCC while sending mails using python In-Reply-To: References: <46da4e770809300727j368a27b8qc570a8451a53e470@mail.gmail.com> Message-ID: On Tuesday, September 30, 2008 8:00:16 AM UTC-8, Bernhard Walle wrote: > Hi, > > * cindy jones [2008-09-30 19:57]: > > > > Can someone tel me how to add cc's and bcc's while sending mails using > > python > > Following (tested) snippet should help: > > ------------------ 8< ------------------------------ > from smtplib import SMTP > from email.mime.image import MIMEImage > from email.mime.text import MIMEText > from email.mime.multipart import MIMEMultipart > > to = 'to_address at example.invalid' > cc = 'cc_address at example.invalid' > bcc = 'bcc_address at example.invalid' > > msg = MIMEMultipart() > msg['To'] = to > msg['Cc'] = cc > msg['From'] = 'bernhard at bwalle.de' > msg['Subject'] = 'Test' > text = MIMEText('Das ist ein Test') > text.add_header("Content-Disposition", "inline") > msg.attach(text) > > s = SMTP('test.smtp.relay') > s.sendmail(msg['From'], [to, cc, bcc], msg.as_string()) > s.quit() > ------------------ >8 ------------------------------ > > > Regards, > Bernhard Hello Bernhard, I've tried the above code and the bcc address does not receive the message, on the To & CC addresses receive it. Here are snippets from my code, perhaps something will stand out to you? to = 'ed at domain.gov' cc = 'ed at gmailmail.com' bcc = 'ed at home.net' msg = MIMEMultipart() msg['To'] = to msg['Cc'] = cc msg['Subject'] = 'My Subject text here' msg['From'] = 'edsboss at domain.gov' smtp = SMTP("smtpserver") smtp.ehlo() smtp.sendmail(msg['From'], [to, cc, bcc], msg.as_string()) Thanks in advance..hope you're still out there!! ~Ed From jaorozco at estudiantes.uci.cu Thu Nov 29 21:27:19 2012 From: jaorozco at estudiantes.uci.cu (Jorge Alberto Diaz Orozco) Date: Thu, 29 Nov 2012 21:27:19 -0500 Subject: ntlm authentication for urllib2 Message-ID: <50B81987.1020803@estudiantes.uci.cu> Hi there: I'm working with urllib2 to open some urls and grab some data. The url will be inserted by the user and my script will open it and parse the page for results. the thing is I'm behind a ntlm proxy, and I've tried with a lot of things to authenticate but it still doesn't work at all. I did some research and found pytho-ntlm but I'm not sure how to use it. I wrote something like this and it still giving me an authentication error: Can someone help me??? import urllib2 from HTTPNtlmAuthHandler import HTTPNtlmAuthHandler url = 'http://url.i.want.to.parse' user = u'DOMAIN\\myuser' password = 'mypass' passman = urllib2.HTTPPasswordMgrWithDefaultRealm() passman.add_password(None, url, user , password) auth_NTLM = HTTPNtlmAuthHandler(passman) proxy_handler = urllib2.ProxyHandler({'http': 'http://10.0.0.1:8080'}) opener = urllib2.build_opener(proxy_handler, auth_NTLM) urllib2.install_opener(opener) response = urllib2.urlopen(url) print 'done' print(response.read()) 10mo. ANIVERSARIO DE LA CREACION DE LA UNIVERSIDAD DE LAS CIENCIAS INFORMATICAS... CONECTADOS AL FUTURO, CONECTADOS A LA REVOLUCION http://www.uci.cu http://www.facebook.com/universidad.uci http://www.flickr.com/photos/universidad_uci From d at davea.name Thu Nov 29 22:17:09 2012 From: d at davea.name (Dave Angel) Date: Thu, 29 Nov 2012 22:17:09 -0500 Subject: Compare list entry from csv files In-Reply-To: References: <50B3E865.9070700@davea.name> <50B43246.2010902@davea.name> Message-ID: <50B82535.8050301@davea.name> On 11/29/2012 05:22 AM, Anatoli Hristov wrote: > > Hello, > > Tried to document a little bit the script, but I'm not that good in that too :) > > The only problem I have is that I cant compare other field than the > first one in > for ex_phone in phones: > telstr = ex_phone[0].lower() > When I use telstr = ex_phone[0].lower() it says out of range and the > strange think is that the range is 6 I can't figure that out. So when > I edit the csv I modify the look of the file and then I start the > script and it works, but I wanted to use more than one condition and I > can't :( > > > > > import csv > > # Open the file with the names and addresses > origf = open('c:/Working/vpharma.csv', 'rt') > # Open the file with the phone numbers > secfile = open('c:/Working/navori.csv', 'rt') > > # Creates the empty list with the names > namelist = [] > # Creates the empty list with the phone numbers > PHONELIST = [] > > > # Reads the file with the names > # Format "Name","Phone" > names = csv.reader(origf, delimiter=';') > > # Reads the file with the phone numbers > # Format "First name","Lastname","Address","City","Country","Phone" > phones = csv.reader(secfile, delimiter=';') > > # Creates a list with phone numbers > #for tel in phones: > # PHONELIST.append(tel) Without populating the PHONELIST here, you have a serious problem. Why is it commented out? > > def finder(Compare_Name,rows): > ''' > Compare the names from the namelist with the names from the phonelist. > If the name match - then the phone number is added to the specified field > ''' > for ex_phone in phones: You should be using PHONELIST here as well. phones is a pseudo-file, which can only be traversed once. A list can be traversed as many times as you like, which is quite a few in your code. > telstr = ex_phone[0].lower() > print telstr > if telstr.find(Compare_Name) >= 0: > print "\nName found: %s" % Compare_Name > namelist[rows][-1] = ex_phone[-1].lower() > else: > print "Not found %s" % Compare_Name > pass > return > > def name_find(): > rows = 0 > for row in names: > namelist.append(row) > Compare_Name = row[1].lower() > finder(Compare_Name,rows) > rows = rows+1 > > if __name__ == '__main__': > name_find() > > # Writes the list to a file > wfile = open('c:/Working/ttest.csv', "wb") > writer = csv.writer(wfile, delimiter=';') > for insert in namelist: > writer.writerow(insert) > wfile.close() As I said before, process both files into lists, one that you treat as constant (and therefore capitalized) and the other containing the data you intend to modify. It'd be much cleaner if you did all that input file parsing stuff in one function, returning only the lists. Call it just before calling name_find(). Similarly, the part you have at the end belongs in a different function, called just after calling name_find(). There's lots of other stuff that should be cleaner, but you've ignored nearly all the suggestions from various people. -- DaveA From menthamnarendra at gmail.com Thu Nov 29 23:17:27 2012 From: menthamnarendra at gmail.com (menthamnarendra at gmail.com) Date: Thu, 29 Nov 2012 20:17:27 -0800 (PST) Subject: KAJOL SEX VIDEOS''''''''''''''''''' In-Reply-To: References: Message-ID: From roy at panix.com Thu Nov 29 23:18:48 2012 From: roy at panix.com (Roy Smith) Date: Thu, 29 Nov 2012 23:18:48 -0500 Subject: Weird import failure with "nosetests --processes=1" References: <50b78e26$0$6945$e4fe514c@news2.news.xs4all.nl> Message-ID: In article <50b78e26$0$6945$e4fe514c at news2.news.xs4all.nl>, Hans Mulder wrote: > That is baffling indeed. It looks like nose is adding some > directory to sys.path, which contains a module pyza.py instead > of a package. We finally figured it out. As it turns out, that's pretty close. > Another idea might be to delete all *.pyc files below > /home/roy/deploy/current/ and /home/roy/songza/ I've got sys.path.insert(0, '/home/roy/deploy/current') in my virtualenv's usercustomize.py. Also, though historical accident, I had (but don't any longer) PYTHONPATH=/home/roy/songza in my .profile. And /home/roy/deploy/current is a symlink to /home/roy/songza! So, I had two different paths to the same directory. Why this only showed up as a problem when running under nose in multiprocess mode, I have no clue. And how it ended up thinking pyza was a module instead of a package, I also have no idea. Crazy. From dorimeshi at gmail.com Fri Nov 30 01:03:49 2012 From: dorimeshi at gmail.com (Doron) Date: Thu, 29 Nov 2012 22:03:49 -0800 (PST) Subject: pyHook and time libraries Message-ID: <9cc06bdd-8254-4f6b-9ce3-0a43b229ca14@googlegroups.com> Hey, I'm tring to create a software that records the keyboard/mouse and sends email of the log every predetermined period. I've manage to make the recorder and the auto-email sender, but I still can't make both of them work simultaneously. Can someone help me with this please? I thought about threading but again... It just sends blank mails without the logs. Thanks alot ahead! From tolidtm at gmail.com Fri Nov 30 04:26:51 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Fri, 30 Nov 2012 10:26:51 +0100 Subject: Compare list entry from csv files In-Reply-To: <5B80DD153D7D744689F57F4FB69AF47418040FEC@SCACMX008.exchad.jpmchase.net> References: <50B3E865.9070700@davea.name> <50B43246.2010902@davea.name> <5B80DD153D7D744689F57F4FB69AF47418040FEC@SCACMX008.exchad.jpmchase.net> Message-ID: > Can you print ex_phone first. You are opening the files in text mode > so I wonder if the line endings are causing you to read and extra > "line" in between. Can you try reading the csv as "rb" instead of > "rt"? Yes I did this: use the global list PHONELIST and opening the CSV in binary - it works now Thanks Anatoli From tolidtm at gmail.com Fri Nov 30 04:29:09 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Fri, 30 Nov 2012 10:29:09 +0100 Subject: Compare list entry from csv files In-Reply-To: <50B82535.8050301@davea.name> References: <50B3E865.9070700@davea.name> <50B43246.2010902@davea.name> <50B82535.8050301@davea.name> Message-ID: > As I said before, process both files into lists, one that you treat as > constant (and therefore capitalized) and the other containing the data > you intend to modify. > > It'd be much cleaner if you did all that input file parsing stuff in one > function, returning only the lists. Call it just before calling > name_find(). Similarly, the part you have at the end belongs in a > different function, called just after calling name_find(). > > There's lots of other stuff that should be cleaner, but you've ignored > nearly all the suggestions from various people. I'm not ignoring anything I just need more time :) I will clean all up and will keep you updated - I promise Regards Anatoli From gialloporpora at gmail.com Fri Nov 30 05:07:02 2012 From: gialloporpora at gmail.com (gialloporpora) Date: Fri, 30 Nov 2012 11:07:02 +0100 Subject: Problem with the MSI installer for Windows XP Message-ID: Dear all, I have problem installing Python 3.3 and Python 2.7.3. I have MS Windows XP with Python 2.7 installed and running but I would to update to 2.7.3 and, if possible, starting to use the 3 version in parallel. I have downloaded the MSI package from the site and when I try to install the action fail after the first step. It appears the first dialog box from where I select ?Install for all users?, when I click Next the installer ends up with a message saying that an error is occured in Windows Installer and it will be closed. The same thing happens with the version 3.3, I have enabled the log file, but I don't understand the problem. I'll copy the whole log if somebody could give me some suggestions. http://pastebin.com/R9KWsAj6 Thanks Sandro -- *Antivirus aggiornato: e siamo sicuri che basti? * - http://bit.ly/SXuDAc Sto ascoltando: *Robert Plant ? One More Cup of Coffee (Valley Below) * - http://bit.ly/TtyHMq From andrea.crotti.0 at gmail.com Fri Nov 30 06:11:28 2012 From: andrea.crotti.0 at gmail.com (andrea crotti) Date: Fri, 30 Nov 2012 11:11:28 +0000 Subject: amazing scope? Message-ID: I wrote a script, refactored it and then introducing a bug as below: def record_things(): out.write("Hello world") if __name__ == '__main__': with open('output', 'w') as out: record_things() but the shocking thing is that it didn't actually stopped working, it still works perfectly! What my explanation might be is that the "out" is declared at module level somehow, but that's not really intuitive and looks wrong, and works both on Python 2.7 and 3.2.. From andrea.crotti.0 at gmail.com Fri Nov 30 06:20:15 2012 From: andrea.crotti.0 at gmail.com (andrea crotti) Date: Fri, 30 Nov 2012 11:20:15 +0000 Subject: amazing scope? In-Reply-To: References: Message-ID: 2012/11/30 andrea crotti : > I wrote a script, refactored it and then introducing a bug as below: > > def record_things(): > out.write("Hello world") > > if __name__ == '__main__': > with open('output', 'w') as out: > record_things() > > > but the shocking thing is that it didn't actually stopped working, it > still works perfectly! > > What my explanation might be is that the "out" is declared at module > level somehow, > but that's not really intuitive and looks wrong, and works both on > Python 2.7 and 3.2.. Already changing it to: def record_things(): out.write("Hello world") def main(): with open('output', 'w') as out: record_things() if __name__ == '__main__': main() makes it stops working as expected, so it's really just a corner case of using the if __name__ == '__main__' which I had never encountered before.. From ulrich.eckhardt at dominolaser.com Fri Nov 30 06:23:10 2012 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Fri, 30 Nov 2012 12:23:10 +0100 Subject: amazing scope? In-Reply-To: References: Message-ID: Am 30.11.2012 12:11, schrieb andrea crotti: > I wrote a script, refactored it and then introducing a bug as below: > > def record_things(): > out.write("Hello world") This is a function. Since "out" is not a local variable, it is looked up in the surrounding namespace at the time the function is called. > if __name__ == '__main__': > with open('output', 'w') as out: > record_things() This is not in a function, so binding variables affects the containing namespace. This includes binding the context manager to "out". Note that you could have moved the whole code into a function (e.g. one called main()) and then you would have gotten the expected failure. > What my explanation might be is that the "out" is declared at module > level somehow, but that's not really intuitive and looks wrong, and > works both on Python 2.7 and 3.2.. Other than in C/C++/Java and others, indention doesn't introduce a new scope, but I understand your intuition. Even simpler, this is how my early Python code looked like, in the absence of the C ternary operator and the distinction between a variable declaration and an assignment: foo = None if some_condition: foo = 'bar' else: foo = 'baz' More idiomatic would have been this: if some_condition: foo = 'bar' else: foo = 'baz' Summary: I'd say that everything is fine. ;) Uli From jeanmichel at sequans.com Fri Nov 30 06:24:59 2012 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Fri, 30 Nov 2012 12:24:59 +0100 (CET) Subject: amazing scope? In-Reply-To: Message-ID: <1021299804.175846.1354274699765.JavaMail.root@sequans.com> ----- Original Message ----- > I wrote a script, refactored it and then introducing a bug as below: > > def record_things(): > out.write("Hello world") > > if __name__ == '__main__': > with open('output', 'w') as out: > record_things() > > > but the shocking thing is that it didn't actually stopped working, it > still works perfectly! > > What my explanation might be is that the "out" is declared at module > level somehow, > but that's not really intuitive and looks wrong, and works both on > Python 2.7 and 3.2.. > -- > http://mail.python.org/mailman/listinfo/python-list > From what I understand from the with documentation http://docs.python.org/2/reference/compound_stmts.html#with The with block has nothing to do with scopes. It does not define the target within that block, well, actually it does but it defines it for the current scope. The with block only identifies where to call __enter__ and __exit__. So I would say that "with open('output', 'w') as out" assignes "out" at the module level. You may have been mislead by some other languages where a with block purpose is to limit the scope of a variable. JM -- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you. From jeanmichel at sequans.com Fri Nov 30 06:31:49 2012 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Fri, 30 Nov 2012 12:31:49 +0100 (CET) Subject: amazing scope? In-Reply-To: Message-ID: <1976787706.176828.1354275109714.JavaMail.root@sequans.com> ----- Original Message ----- > 2012/11/30 andrea crotti : > > Already changing it to: > > def record_things(): > out.write("Hello world") > > def main(): > with open('output', 'w') as out: > record_things() > > if __name__ == '__main__': > main() > > makes it stops working as expected, so it's really just a corner case > of using the if __name__ == '__main__' > which I had never encountered before.. You do realize that foo = 5 # define at the module level if __name__ == '__main__': bar = 6 # also at the module level if True: ham = 8 # still at the module level def func() # scope has changed, only at the func level jam = 9 Nothing magic about if __name__ == '__main__' Cheers, JM -- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you. From andrea.crotti.0 at gmail.com Fri Nov 30 11:05:40 2012 From: andrea.crotti.0 at gmail.com (andrea crotti) Date: Fri, 30 Nov 2012 16:05:40 +0000 Subject: amazing scope? In-Reply-To: References: Message-ID: Well I knew that this works fine, even if I feel a bit guilty to do this, and better is: foo = 'bar' if some_condition else 'baz' Anyway for me the suprise is that something that is defined *later* at the module scope is found in a function which is defined *earlier*. From rosuav at gmail.com Fri Nov 30 11:08:45 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 1 Dec 2012 03:08:45 +1100 Subject: amazing scope? In-Reply-To: References: Message-ID: On Sat, Dec 1, 2012 at 3:05 AM, andrea crotti wrote: > Well I knew that this works fine, even if I feel a bit guilty to do > this, and better is: > > foo = 'bar' if some_condition else 'baz' > > Anyway for me the suprise is that something that is defined *later* at > the module scope is found in a function which is defined *earlier*. It's assigned to earlier. It's no different from C code like this: int foo; void blah() { /* do stuff with foo */ } int main() { foo = 1; } In Python, the "int foo;" is implicit, but the scope is the same. ChrisA From jeanmichel at sequans.com Fri Nov 30 11:47:17 2012 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Fri, 30 Nov 2012 17:47:17 +0100 (CET) Subject: amazing scope? In-Reply-To: Message-ID: <1795846619.455414.1354294037270.JavaMail.root@sequans.com> ----- Original Message ----- > Well I knew that this works fine, even if I feel a bit guilty to do > this, and better is: > > foo = 'bar' if some_condition else 'baz' > > Anyway for me the suprise is that something that is defined *later* > at > the module scope is found in a function which is defined *earlier*. > -- > http://mail.python.org/mailman/listinfo/python-list > What really matters for function is when they're executed, not defined (except for their parameter default value). foo = 'I am foo' bar = 'I am bar' def funcA(param1=foo): print param1 print bar foo = 'I am another foo' bar = 'I am another bar' def funcB(param1=foo): print param1 print bar funcA() funcB() I am foo I am another bar I am another foo I am another bar cheers, JM -- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you. From rustompmody at gmail.com Fri Nov 30 12:54:00 2012 From: rustompmody at gmail.com (rusi) Date: Fri, 30 Nov 2012 09:54:00 -0800 (PST) Subject: Is it bad style to override the built-in function `type`? References: Message-ID: On Nov 23, 9:12?pm, Michael Herrmann wrote: > Hi, > > do you think it's bad style to override the built-in function `type`? I'm co-developing a GUI automation library called Automa (http://www.getautoma.com) and 'type' would be a very fitting name for a function that generates artificial key strokes. > > This post is motivated by an already lengthy discussion on this mailing list (http://bit.ly/10aOy4H), where we tried to find alternative names for `type`. Many were found, but none are quite as fitting as 'type'. > > For the sake of avoiding a discussion that is already being lead elsewhere please confine this thread to what you generally think about overriding `type`, and post suggestions for alternative names or solutions in the other thread. > > Thank you very much! > Michael Im entering this thread late (was off mail for a week), so pardon me if someone has already said this -- but have you looked at the difference between internal and external dsls: http://martinfowler.com/bliki/DomainSpecificLanguage.html (and links therein) ? Roughly speaking if what you are making is an external dsl, then its not really python (it may be python-inspired but thats not really germane) and so reusing python lexemes/structures etc in ways not exactly consistent with python usage should be no issue If its an internal DSL, you are headed for causing/suffering grief. I looked at your site [yes it looked almost interesting -- if only it ran on linux :-( ] and I cant really decide whether to classify it as external or internal From emile at fenx.com Fri Nov 30 13:01:30 2012 From: emile at fenx.com (emile) Date: Fri, 30 Nov 2012 10:01:30 -0800 Subject: os.popen and the subprocess module In-Reply-To: References: Message-ID: On 11/29/2012 10:39 AM, Nobody wrote: From emile at fenx.com Fri Nov 30 13:16:01 2012 From: emile at fenx.com (emile) Date: Fri, 30 Nov 2012 10:16:01 -0800 Subject: Migrate from Access 2010 / VBA In-Reply-To: <20121129104650.GC5545@balisto> References: <6db4ed18-a16e-4cd1-940b-c10f51207780@googlegroups.com> <20121125204816.97f486775f8b1e7e575a7b0a@gmx.net> <20121129104650.GC5545@balisto> Message-ID: On 11/29/2012 02:46 AM, Nicolas ?vrard wrote: > > I'd like to add to the list > Tryton http://www.tryton.org/ > > Which framework can be used to create a business application Let me second this, although for openERP (the parent from which Tryton was forked)... > Reporting is done through relatorio (http://relatorio.openhex.org/), > which uses ODF templates to generate ODF reports (or other format > thanks to unoconv) the client is written in GTk (we're writing one in > JavaScript right now (and I miss python badly)). ... for which the web client is fully up and running. In any case it's a rich environment for business applications. Emile From ramit.prasad at jpmorgan.com Fri Nov 30 14:21:55 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Fri, 30 Nov 2012 19:21:55 +0000 Subject: amazing scope? In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF474180438F9@SCACMX008.exchad.jpmchase.net> andrea crotti > > I wrote a script, refactored it and then introducing a bug as below: > > def record_things(): > out.write("Hello world") > > if __name__ == '__main__': > with open('output', 'w') as out: > record_things() > > > but the shocking thing is that it didn't actually stopped working, it > still works perfectly! > > What my explanation might be is that the "out" is declared at module > level somehow, > but that's not really intuitive and looks wrong, and works both on > Python 2.7 and 3.2.. Makes sense to me. `out` is declared in an if statement. If statements have no "scope" and it is not in a function so it gets added to the module's namespace. ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From ramit.prasad at jpmorgan.com Fri Nov 30 14:47:57 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Fri, 30 Nov 2012 19:47:57 +0000 Subject: pyHook and time libraries In-Reply-To: <9cc06bdd-8254-4f6b-9ce3-0a43b229ca14@googlegroups.com> References: <9cc06bdd-8254-4f6b-9ce3-0a43b229ca14@googlegroups.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF47418043954@SCACMX008.exchad.jpmchase.net> Doron wrote: > > Hey, I'm tring to create a software that records the keyboard/mouse and sends email of the log every > predetermined period. > > I've manage to make the recorder and the auto-email sender, but I still can't make both of them work > simultaneously. > > Can someone help me with this please? > I thought about threading but again... It just sends blank mails without the logs. > > Thanks alot ahead! I am not sure how to even begin helping you. I do not even know what is wrong other than "can't make both of them work simultaneously". What version of Python and OS? Are you using any 3rd party modules? What is the code you use? What happens and what do you expect? How are you getting the logs for email? Are they being passed in or are you using a log file? ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From subhabangalore at gmail.com Fri Nov 30 14:48:30 2012 From: subhabangalore at gmail.com (subhabangalore at gmail.com) Date: Fri, 30 Nov 2012 11:48:30 -0800 (PST) Subject: Textmining Message-ID: <4a65cf82-9368-4c29-95a0-bc552cd0effd@googlegroups.com> Dear Group, Python has one textming library, but I am failing to install it in Windows. If any one can kindly help. Regards, Subhabrata. From ramit.prasad at jpmorgan.com Fri Nov 30 15:11:24 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Fri, 30 Nov 2012 20:11:24 +0000 Subject: please help me to debud my local chat network program In-Reply-To: References: <8edb652f-c48f-4758-a315-3ecc8070267d@googlegroups.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF474180439E6@SCACMX008.exchad.jpmchase.net> Minh Dang wrote: > > can anyone help me? Chris Angelico has given you some good comments which should give you a direction to investigate. This list is a global list and you seem a tad impatient. It is normal to hear back from a few hours to a day or two. Even if I wanted to help, without context in this email I might not know what you need help with. I could probably look it up in an archive of the list, but it is unlikely I will. Help yourself by keeping relevant text in the email and respond inline (or below) the quoted text. That being said, did Chris's suggestion work? If not what happened? ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From akegb3 at gmail.com Fri Nov 30 15:25:37 2012 From: akegb3 at gmail.com (Ed) Date: Fri, 30 Nov 2012 12:25:37 -0800 (PST) Subject: send email with bcc Message-ID: <9fedf732-2a8e-4445-bdce-b88d53f4540c@googlegroups.com> Hi, I have found many different suggestions on "how to" code python to send a bcc email. None of which have worked in my environment - yet. Following is what I have at this time, which is erroring. Most of the code I've found will successfully send the message to the TO & CC recipients, but not the BCC. Any help is very much appreciated. Thx, Ed . from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.mime.application import MIMEApplication from email.mime.multipart import MIMEMultipart from smtplib import SMTP to = 'ed at domain.gov' cc = 'ed at gmailmail.com' bcc = 'ed at domain.net' sender = 'edsboss at domain.gov' msg = MIMEMultipart() msg['To'] = to msg['Cc'] = cc msg['Subject'] = 'Monthly Report' msg['From'] = sender smtp = SMTP("smtp.server.com") # Start the server: smtp.ehlo() # Send the email smtp.sendmail(sender, [to] + bcc, msg.as_string()) The above generates the following error: Traceback (most recent call last): File "/opt/batch/ebtest/example4.py", line 46, in smtp.sendmail(sender, [to] + bcc, msg.as_string()) Other iterations of the code have worked without error, but do not send mail to the BCC recipient. Thanks in advance for any assistance! ~Ed From mail at timgolden.me.uk Fri Nov 30 15:40:27 2012 From: mail at timgolden.me.uk (Tim Golden) Date: Fri, 30 Nov 2012 20:40:27 +0000 Subject: send email with bcc In-Reply-To: <9fedf732-2a8e-4445-bdce-b88d53f4540c@googlegroups.com> References: <9fedf732-2a8e-4445-bdce-b88d53f4540c@googlegroups.com> Message-ID: <50B919BB.4080202@timgolden.me.uk> On 30/11/2012 20:25, Ed wrote: > to = 'ed at domain.gov' > bcc = 'ed at domain.net' [... snippage ...] > smtp.sendmail(sender, [to] + bcc, msg.as_string()) Well, you crucially don't show us the rest of the traceback. But I imagine you'd have got something like this: ActivePython 2.7.2.5 (ActiveState Software Inc.) based on Python 2.7.2 (default, Jun 24 2011, 12:21:10) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> to = 'ed at domain.gov' >>> bcc = 'ed at domain.net' >>> [to] + bcc Traceback (most recent call last): File "", line 1, in TypeError: can only concatenate list (not "str") to list >>> which suggests, fairly clearly, that you're trying to concatenate a string and a list. TJG From airween at gmail.com Fri Nov 30 16:03:29 2012 From: airween at gmail.com (Ervin =?utf-8?Q?Heged=C3=BCs?=) Date: Fri, 30 Nov 2012 22:03:29 +0100 Subject: send email with bcc In-Reply-To: <9fedf732-2a8e-4445-bdce-b88d53f4540c@googlegroups.com> References: <9fedf732-2a8e-4445-bdce-b88d53f4540c@googlegroups.com> Message-ID: <20121130210328.GB14773@arxnet.hu> Hello, On Fri, Nov 30, 2012 at 12:25:37PM -0800, Ed wrote: > > # Send the email > smtp.sendmail(sender, [to] + bcc, msg.as_string()) > > The above generates the following error: > Traceback (most recent call last): > File "/opt/batch/ebtest/example4.py", line 46, in > smtp.sendmail(sender, [to] + bcc, msg.as_string()) didn't you forgot to attach the reason of the error, I mean what is the Exception type? > Other iterations of the code have worked without error, but do not send mail to the BCC recipient. you could't concatenate a list and a string at this way. You can do that one of these: l = ['foo'] s = 'bar' l.append(s) or n = l+[s] a. -- I ? UTF-8 From neilc at norwich.edu Fri Nov 30 16:20:31 2012 From: neilc at norwich.edu (Neil Cerutti) Date: 30 Nov 2012 21:20:31 GMT Subject: Textmining References: <4a65cf82-9368-4c29-95a0-bc552cd0effd@googlegroups.com> Message-ID: On 2012-11-30, subhabangalore at gmail.com wrote: > Python has one textming library, but I am failing to install it > in Windows. If any one can kindly help. More information needed. -- Neil Cerutti From akegb3 at gmail.com Fri Nov 30 17:35:14 2012 From: akegb3 at gmail.com (Ed) Date: Fri, 30 Nov 2012 14:35:14 -0800 (PST) Subject: send email with bcc In-Reply-To: <9fedf732-2a8e-4445-bdce-b88d53f4540c@googlegroups.com> References: <9fedf732-2a8e-4445-bdce-b88d53f4540c@googlegroups.com> Message-ID: <323db439-f559-49f3-a890-59a911f586ac@googlegroups.com> Oops. Sorry 'bout that. I somehow didn't grab the entire traceback info. Well, turns out one if not more of my BCC attempts is working. Just thought to check my spam filter of the BCC email address and there are quite a few messages in there. I've got it working now. Thanks to you both for the quick responses!! ~Ed From d at davea.name Fri Nov 30 17:57:44 2012 From: d at davea.name (Dave Angel) Date: Fri, 30 Nov 2012 17:57:44 -0500 Subject: amazing scope? In-Reply-To: References: Message-ID: <50B939E8.3020508@davea.name> On 11/30/2012 11:05 AM, andrea crotti wrote: > Well I knew that this works fine, even if I feel a bit guilty to do > this, and better is: > > foo = 'bar' if some_condition else 'baz' > > Anyway for me the suprise is that something that is defined *later* at > the module scope is found in a function which is defined *earlier*. It would have been nice if you had indicated in your original post just *what* you considered "shocking" about the code sample. The code was valid, and we all had to guess what about it was puzzling you. Like most others, I first guessed you were confused about the definition being in an if statement. Then I thought you were confused by the with statement, since it's not as obvious that "as" binds the name to the object in the same way as assignment. But I never would have guessed that you were confused about the order of execution in a module. The source code in a module is executed in order, including function definitions defined at top level. However, such a function definition's execution creates a function object, and assigns a global name (usually) to that object. It does not "execute" the object. When that function is executed later, it then looks for global variables for stuff that's not defined within its own scope. So by the time the function references the 'out' name, it's been successfully added to the global namespace. -- DaveA From d at davea.name Fri Nov 30 18:43:17 2012 From: d at davea.name (Dave Angel) Date: Fri, 30 Nov 2012 18:43:17 -0500 Subject: Textmining In-Reply-To: <4a65cf82-9368-4c29-95a0-bc552cd0effd@googlegroups.com> References: <4a65cf82-9368-4c29-95a0-bc552cd0effd@googlegroups.com> Message-ID: <50B94495.8050504@davea.name> On 11/30/2012 02:48 PM, subhabangalore at gmail.com wrote: > Dear Group, > Python has one textming library, but I am failing to install it in Windows. > If any one can kindly help. > Regards, > Subhabrata. Please think about what you're asking, if you want people to help you. You say Python has a testming library, But CPython's standard library version 3.3 does NOT have a library called testming. Neither does 2.7 in case you're running that one. Now, maybe some other version of Python has other stuff in its standard library, or maybe it's only available on the Amiga port of python. But you give no clues to which one it was. I repeated the search with a keyword of testmining, in case that's the actual name of the library. Still in neither 2.7 nor 3.3. So I'm forced to gaze even closer into my crystal ball. How about http://scripts.downloadroute.com/textmining-95300f9f.html http://www.testmine.com/ http://webscripts.softpedia.com/script/E-Commerce/Catalogs/textmining-66084.html http://www.christianpeccei.com/projects/textmining/ http://pybrary.net/pyPdf/ http://code.activestate.com/recipes/511465/ http://www.unixuser.org/~euske/python/pdfminer/index.html http://orange.biolab.si/ http://www.amazon.com/Python-Text-Processing-NLTK-Cookbook/dp/1849513600?tag=duckduckgo-d-20 http://linux.softpedia.com/get/Utilities/textmining-61802.shtml http://pypi.python.org/pypi/textmining http://orange-text.readthedocs.org/en/latest/ I could call a mechanic, and tell him my car makes a funny nose, and no matter how hard I kick the right front tire the noise doesn't go away, and he's unlikely to be able to help. He'd probably like some fundamental facts about the problem. So do we. What version of Windows OS are you running? What version of what implementation of Python are you running? What library, located at what URL did you try to install? How did you try the installation? What happened? How did you know you failed? In many of these answers, you should paste actual program output rather than paraphrasing. Certainly, if you got an exception, you should paste the entire stack trace. And if you got that far, a minimal code example that shows the problem. -- DaveA